Skip to content

gh-109889: fix compiler's redundant NOP detection to look past NOPs with no lineno when looking for the next instruction's lineno #109987

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

Merged
merged 2 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,11 @@ def f(x):
while x:
0 if 1 else 0

def test_remove_redundant_nop_edge_case(self):
# See gh-109889
def f():
a if (1 if b else c) else d

@requires_debug_ranges()
class TestSourcePositions(unittest.TestCase):
# Ensure that compiled code snippets have correct line and column numbers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the compiler's redundant NOP detection algorithm to skip over NOPs with
no line number when looking for the next instruction's lineno.
12 changes: 11 additions & 1 deletion Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Copy link
Member

@carljm carljm Sep 28, 2023

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.

Copy link
Member Author

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.

cfg_instr *instr = &next->b_instr[next_i];
if (instr->i_opcode == NOP && instr->i_loc.lineno == NO_LOCATION.lineno) {
/* Skip over NOPs without location, they will be removed */
continue;
}
next_loc = instr->i_loc;
break;
}
if (lineno == next_loc.lineno) {
continue;
}
}
Expand Down