Skip to content

Commit f580edc

Browse files
authored
gh-109889: fix compiler's redundant NOP detection to look past NOPs with no lineno when looking for the next instruction's lineno (#109987)
1 parent b14f0ab commit f580edc

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
@@ -1278,6 +1278,11 @@ def f(x):
12781278
while x:
12791279
0 if 1 else 0
12801280

1281+
def test_remove_redundant_nop_edge_case(self):
1282+
# See gh-109889
1283+
def f():
1284+
a if (1 if b else c) else d
1285+
12811286
@requires_debug_ranges()
12821287
class TestSourcePositions(unittest.TestCase):
12831288
# 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
@@ -1017,7 +1017,17 @@ remove_redundant_nops(basicblock *bb) {
10171017
}
10181018
/* or if last instruction in BB and next BB has same line number */
10191019
if (next) {
1020-
if (lineno == next->b_instr[0].i_loc.lineno) {
1020+
location next_loc = NO_LOCATION;
1021+
for (int next_i=0; next_i < next->b_iused; next_i++) {
1022+
cfg_instr *instr = &next->b_instr[next_i];
1023+
if (instr->i_opcode == NOP && instr->i_loc.lineno == NO_LOCATION.lineno) {
1024+
/* Skip over NOPs without location, they will be removed */
1025+
continue;
1026+
}
1027+
next_loc = instr->i_loc;
1028+
break;
1029+
}
1030+
if (lineno == next_loc.lineno) {
10211031
continue;
10221032
}
10231033
}

0 commit comments

Comments
 (0)