gh-121246: LOAD_CONST optimizer now uses a helper function to get the next non-NOP instruction possibly in a following block#121305
Closed
blhsing wants to merge 1 commit intopython:mainfrom
Conversation
…next non-NOP instruction possibly in the next block
iritkatriel
reviewed
Jul 3, 2024
Comment on lines
+1000
to
+1004
| # gh-121246: Check that the CPython-specific peephole optimizer would | ||
| # properly convert pairs of LOAD_CONST + RETURN_VALUE into | ||
| # RETURN_CONST even if they are separated by NOP or in different | ||
| # blocks of instructions as a result of prior optimizations for | ||
| # returning different constants from a ternary operation |
Member
There was a problem hiding this comment.
Typically here we just write "see gh-121346" in a comment. The discussion should be on the issue.
| next_instruction(basicblock **bb, cfg_instr **inst, int *i) | ||
| { | ||
| ++*inst; | ||
| ++*i; |
Member
There was a problem hiding this comment.
This function probably isn't quite right. If the current instruction is a jump then then b_next is not the next block executed. It also ignores line numbers - sometimes a NOP is there because it represents a line of code that needs to show as "executed" in tracing or coverage. We can't just skip over such NOPs (you should have a test where the ifexpr spans multiple lines).
I think rather than skip over NOPs here, we may need to add a call to remove_redundant_nops somewhere (maybe at the end of inline_small_or_no_lineno_blocks, only when changes > 0?).
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As reported in gh-121246, the following Python code:
would be compiled into the following bytecode:
The two pairs of
LOAD_CONST+RETURN_VALUEare each supposed to be optimized into aRETURN_CONSTbut were not. In the former case, it was because aNOPwas betweenLOAD_CONSTandRETURN_VALUEas a result of a prior optimization so the peephole optmizer could not obtainRETURN_VALUEas the next instruction fromLOAD_CONST. In the latter case, it was becauseRETURN_VALUEwas in a block following that ofLOAD_CONST. The current approach of using&bb->b_instr[i + 1]to obtain the next instruction would fail for both cases.This PR made the
LOAD_CONST-specific peephole optimizer obtain the next instruction instead by using a helper function that skips NOP instructions and turns to the next block if at the end of the current block.The other optimizer functions may be able benefit from switching to the same helper function, but I decided not to make such changes to other optimizer functions unnecessarily unless there is an actual need.