From 0005e65bf8eeb42be77f2241c5f7001d5a754e96 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Fri, 22 Sep 2023 17:59:35 +0100 Subject: [PATCH] gh-109719: Fix missing jump target labels when compiler reorders cold/warm blocks (GH-109734) (cherry picked from commit 7c553991724d8d537f8444db73f016008753d77a) Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Lib/test/test_compile.py | 11 +++++++++++ .../2023-09-22-13-38-17.gh-issue-109719.fx5OTz.rst | 1 + Python/flowgraph.c | 5 +++++ 3 files changed, 17 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-09-22-13-38-17.gh-issue-109719.fx5OTz.rst diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 00fb61988505ee..2e5763eb3d61e9 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1225,6 +1225,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): diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-09-22-13-38-17.gh-issue-109719.fx5OTz.rst b/Misc/NEWS.d/next/Core and Builtins/2023-09-22-13-38-17.gh-issue-109719.fx5OTz.rst new file mode 100644 index 00000000000000..83be54c9ca793e --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-09-22-13-38-17.gh-issue-109719.fx5OTz.rst @@ -0,0 +1 @@ +Fix missing jump target labels when compiler reorders cold/warm blocks. diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 0ea32345f9f9be..d19fe686d01c94 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -1945,6 +1945,8 @@ push_cold_blocks_to_end(cfg_builder *g, int code_flags) { } 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) { @@ -1953,6 +1955,9 @@ push_cold_blocks_to_end(cfg_builder *g, int code_flags) { 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;