-
-
Notifications
You must be signed in to change notification settings - Fork 31.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-109889: fix compiler's redundant NOP detection to look past NOPs with no lineno when looking for the next instruction's lineno #109987
Conversation
…o lineno when looking for the next instruction's lineno
@@ -1017,7 +1017,17 @@ remove_redundant_nops(basicblock *bb) { | |||
} | |||
/* or if last instruction in BB and next BB has same line number */ | |||
if (next) { | |||
if (lineno == next->b_instr[0].i_loc.lineno) { | |||
location next_loc = NO_LOCATION; | |||
for (int next_i=0; next_i < next->b_iused; next_i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially, I thought it would be simpler to have the same behavior whether the next instruction is in the same basicblock or the next one; i.e. it would be clearer to just match the behavior above on lines 1004-1011. If the next instruction has no location, set it to this NOP's location and remove this NOP.
But then I realized that this is not actually equivalent in the case where we are crossing to the next basicblock, because the next basicblock may be a jump target, and that jump should not report the location from the current NOP. So we can't safely use the "copy location to next instr without location" approach across the basicblock boundary.
I'm not sure if this subtlety will be obvious to future readers/modifiers of this code?
We could safely use this new behavior (skip NOP without lineno that will be removed) in all cases, and restructure the code so that first we find the next "relevant" instruction (whether in current or next basic block), and then compare its location with the current NOP. I think this would be somewhat clearer and simpler, but it's up to you whether you agree / think it's worth it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could safely use this new behavior (skip NOP without lineno that will be removed) in all cases
I think we are a bit more aggressive when it's within the same basic block: we will hand the location on to any instruction that doesn't have a line number, not just a NOP.
If I remove the assert statement on main and compile the test function, I see no
Is the change to the optimizer necessary, is there a case where it generates better output? |
If that's the case then we should try to understand where the NOP gets removed. I wouldn't remove the assertion before I understand what's going on. |
Thanks @iritkatriel for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12. |
…NOPs with no lineno when looking for the next instruction's lineno (pythonGH-109987) (cherry picked from commit f580edc) Co-authored-by: Irit Katriel <[email protected]>
GH-110048 is a backport of this pull request to the 3.12 branch. |
I don't know why we do this in the optimizer and not in the assembler, after line numbers are propagated so it's easier to see what's redundant and what's not. |
|
…NOPs with no lineno when looking for the next instruction's lineno (python#109987)
… NOPs with no lineno when looking for the next instruction's lineno (GH-109987) (#110048) gh-109889: fix compiler's redundant NOP detection to look past NOPs with no lineno when looking for the next instruction's lineno (GH-109987) (cherry picked from commit f580edc) Co-authored-by: Irit Katriel <[email protected]>
…NOPs with no lineno when looking for the next instruction's lineno (python#109987)
Fixes #109889.