Skip to content

Commit d73c12b

Browse files
authored
pythongh-109823: Adjust labels in compiler when removing an empty basic block which is a jump target (python#109839)
1 parent 88a6137 commit d73c12b

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Lib/test/test_compile.py

+5
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,11 @@ def f():
12721272
else:
12731273
1 if 1 else 1
12741274

1275+
def test_remove_empty_basic_block_with_jump_target_label(self):
1276+
# See gh-109823
1277+
def f(x):
1278+
while x:
1279+
0 if 1 else 0
12751280

12761281
@requires_debug_ranges()
12771282
class TestSourcePositions(unittest.TestCase):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bug where compiler does not adjust labels when removing an empty basic
2+
block which is a jump target.

Python/flowgraph.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@ eliminate_empty_basic_blocks(cfg_builder *g) {
960960
while(g->g_entryblock && g->g_entryblock->b_iused == 0) {
961961
g->g_entryblock = g->g_entryblock->b_next;
962962
}
963+
int next_lbl = get_max_label(g->g_entryblock) + 1;
963964
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
964965
assert(b->b_iused > 0);
965966
for (int i = 0; i < b->b_iused; i++) {
@@ -969,7 +970,13 @@ eliminate_empty_basic_blocks(cfg_builder *g) {
969970
while (target->b_iused == 0) {
970971
target = target->b_next;
971972
}
972-
instr->i_target = target;
973+
if (instr->i_target != target) {
974+
if (!IS_LABEL(target->b_label)) {
975+
target->b_label.id = next_lbl++;
976+
}
977+
instr->i_target = target;
978+
instr->i_oparg = target->b_label.id;
979+
}
973980
assert(instr->i_target && instr->i_target->b_iused > 0);
974981
}
975982
}

0 commit comments

Comments
 (0)