Skip to content

Commit

Permalink
pythongh-109823: Adjust labels in compiler when removing an empty bas…
Browse files Browse the repository at this point in the history
…ic block which is a jump target
  • Loading branch information
iritkatriel committed Sep 25, 2023
1 parent f2eaa92 commit 29070c5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,11 @@ def f():
else:
1 if 1 else 1

def test_remove_empty_basic_block_with_jump_target_label(self):
# See gh-109823
def f(x):
while x:
0 if 1 else 0

@requires_debug_ranges()
class TestSourcePositions(unittest.TestCase):
Expand Down
9 changes: 8 additions & 1 deletion Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ eliminate_empty_basic_blocks(cfg_builder *g) {
while(g->g_entryblock && g->g_entryblock->b_iused == 0) {
g->g_entryblock = g->g_entryblock->b_next;
}
int next_lbl = get_max_label(g->g_entryblock) + 1;
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
assert(b->b_iused > 0);
for (int i = 0; i < b->b_iused; i++) {
Expand All @@ -969,7 +970,13 @@ eliminate_empty_basic_blocks(cfg_builder *g) {
while (target->b_iused == 0) {
target = target->b_next;
}
instr->i_target = target;
if (instr->i_target != target) {
if (!IS_LABEL(target->b_label)) {
target->b_label.id = next_lbl++;
}
instr->i_target = target;
instr->i_oparg = target->b_label.id;
}
assert(instr->i_target && instr->i_target->b_iused > 0);
}
}
Expand Down

0 comments on commit 29070c5

Please sign in to comment.