Skip to content

Commit 2b96102

Browse files
[3.12] 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) (#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]>
1 parent c794103 commit 2b96102

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Lib/test/test_compile.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,11 @@ def f(x):
12691269
while x:
12701270
0 if 1 else 0
12711271

1272+
def test_remove_redundant_nop_edge_case(self):
1273+
# See gh-109889
1274+
def f():
1275+
a if (1 if b else c) else d
1276+
12721277
@requires_debug_ranges()
12731278
class TestSourcePositions(unittest.TestCase):
12741279
# Ensure that compiled code snippets have correct line and column numbers
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the compiler's redundant NOP detection algorithm to skip over NOPs with
2+
no line number when looking for the next instruction's lineno.

Python/flowgraph.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,17 @@ remove_redundant_nops(basicblock *bb) {
981981
}
982982
/* or if last instruction in BB and next BB has same line number */
983983
if (next) {
984-
if (lineno == next->b_instr[0].i_loc.lineno) {
984+
location next_loc = NO_LOCATION;
985+
for (int next_i=0; next_i < next->b_iused; next_i++) {
986+
cfg_instr *instr = &next->b_instr[next_i];
987+
if (instr->i_opcode == NOP && instr->i_loc.lineno == NO_LOCATION.lineno) {
988+
/* Skip over NOPs without location, they will be removed */
989+
continue;
990+
}
991+
next_loc = instr->i_loc;
992+
break;
993+
}
994+
if (lineno == next_loc.lineno) {
985995
continue;
986996
}
987997
}

0 commit comments

Comments
 (0)