gh-121246: LOAD_CONST optimizer now uses a helper function to get the next non-NOP instruction possibly in a following block #121305
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_VALUE
are each supposed to be optimized into aRETURN_CONST
but were not. In the former case, it was because aNOP
was betweenLOAD_CONST
andRETURN_VALUE
as a result of a prior optimization so the peephole optmizer could not obtainRETURN_VALUE
as the next instruction fromLOAD_CONST
. In the latter case, it was becauseRETURN_VALUE
was 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.