Skip to content

Commit 7c55399

Browse files
authored
gh-109719: Fix missing jump target labels when compiler reorders cold/warm blocks (#109734)
1 parent 73ccfa2 commit 7c55399

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Lib/test/test_compile.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,17 @@ def f():
12611261
except:
12621262
pass
12631263

1264+
def test_cold_block_moved_to_end(self):
1265+
# See gh-109719
1266+
def f():
1267+
while name:
1268+
try:
1269+
break
1270+
except:
1271+
pass
1272+
else:
1273+
1 if 1 else 1
1274+
12641275

12651276
@requires_debug_ranges()
12661277
class TestSourcePositions(unittest.TestCase):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix missing jump target labels when compiler reorders cold/warm blocks.

Python/flowgraph.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,8 @@ push_cold_blocks_to_end(cfg_builder *g) {
21332133
}
21342134
RETURN_IF_ERROR(mark_cold(entryblock));
21352135

2136+
int next_lbl = get_max_label(g->g_entryblock) + 1;
2137+
21362138
/* If we have a cold block with fallthrough to a warm block, add */
21372139
/* an explicit jump instead of fallthrough */
21382140
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
@@ -2141,6 +2143,9 @@ push_cold_blocks_to_end(cfg_builder *g) {
21412143
if (explicit_jump == NULL) {
21422144
return ERROR;
21432145
}
2146+
if (!IS_LABEL(b->b_next->b_label)) {
2147+
b->b_next->b_label.id = next_lbl++;
2148+
}
21442149
basicblock_addop(explicit_jump, JUMP, b->b_next->b_label.id, NO_LOCATION);
21452150
explicit_jump->b_cold = 1;
21462151
explicit_jump->b_next = b->b_next;

0 commit comments

Comments
 (0)