Skip to content

gh-109719: Fix missing jump target labels when compiler reorders cold/warm blocks #109734

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 6 commits into from
Sep 22, 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
11 changes: 11 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,17 @@ def f():
except:
pass

def test_cold_block_moved_to_end(self):
# See gh-109719
def f():
while name:
try:
break
except:
pass
else:
1 if 1 else 1


@requires_debug_ranges()
class TestSourcePositions(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix missing jump target labels when compiler reorders cold/warm blocks.
5 changes: 5 additions & 0 deletions Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -2133,6 +2133,8 @@ push_cold_blocks_to_end(cfg_builder *g) {
}
RETURN_IF_ERROR(mark_cold(entryblock));

int next_lbl = get_max_label(g->g_entryblock) + 1;

/* If we have a cold block with fallthrough to a warm block, add */
/* an explicit jump instead of fallthrough */
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
Expand All @@ -2141,6 +2143,9 @@ push_cold_blocks_to_end(cfg_builder *g) {
if (explicit_jump == NULL) {
return ERROR;
}
if (!IS_LABEL(b->b_next->b_label)) {
b->b_next->b_label.id = next_lbl++;
}
basicblock_addop(explicit_jump, JUMP, b->b_next->b_label.id, NO_LOCATION);
explicit_jump->b_cold = 1;
explicit_jump->b_next = b->b_next;
Expand Down