From 0251780a12c39f9026962ff21b6845b335766b9d Mon Sep 17 00:00:00 2001 From: iritkatriel Date: Fri, 22 Sep 2023 14:16:47 +0100 Subject: [PATCH 1/5] gh-109719: Fix missing jump target labels when compiler reorders cold/warm blocks. --- Lib/test/test_compile.py | 11 +++++++++++ Python/flowgraph.c | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index f4e28559194dd68..d3a5517963c5400 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -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): diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 2df9b4811817f2f..379912be38aebf7 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -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) { @@ -2141,9 +2143,13 @@ 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; + explicit_jump->b_label.id = next_lbl++; b->b_next = explicit_jump; /* set target */ From f9eb3016085e7d5b12e3d35f25caffec4b55fb7f Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 22 Sep 2023 13:38:21 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-09-22-13-38-17.gh-issue-109719.fx5OTz.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-09-22-13-38-17.gh-issue-109719.fx5OTz.rst 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 000000000000000..83be54c9ca793e4 --- /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. From 8aad689a55bfbfc1e86eb4afd1d20a34e06a23e1 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 22 Sep 2023 13:38:23 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-09-22-13-38-19.gh-issue-109719.fx5OTz.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-09-22-13-38-19.gh-issue-109719.fx5OTz.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-09-22-13-38-19.gh-issue-109719.fx5OTz.rst b/Misc/NEWS.d/next/Core and Builtins/2023-09-22-13-38-19.gh-issue-109719.fx5OTz.rst new file mode 100644 index 000000000000000..83be54c9ca793e4 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-09-22-13-38-19.gh-issue-109719.fx5OTz.rst @@ -0,0 +1 @@ +Fix missing jump target labels when compiler reorders cold/warm blocks. From 9cb6a3daa3b8d19748687410f617d6dff2f1525a Mon Sep 17 00:00:00 2001 From: iritkatriel Date: Fri, 22 Sep 2023 15:04:55 +0100 Subject: [PATCH 4/5] =?UTF-8?q?Revert=20"=F0=9F=93=9C=F0=9F=A4=96=20Added?= =?UTF-8?q?=20by=20blurb=5Fit."?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 8aad689a55bfbfc1e86eb4afd1d20a34e06a23e1. --- .../2023-09-22-13-38-19.gh-issue-109719.fx5OTz.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-09-22-13-38-19.gh-issue-109719.fx5OTz.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-09-22-13-38-19.gh-issue-109719.fx5OTz.rst b/Misc/NEWS.d/next/Core and Builtins/2023-09-22-13-38-19.gh-issue-109719.fx5OTz.rst deleted file mode 100644 index 83be54c9ca793e4..000000000000000 --- a/Misc/NEWS.d/next/Core and Builtins/2023-09-22-13-38-19.gh-issue-109719.fx5OTz.rst +++ /dev/null @@ -1 +0,0 @@ -Fix missing jump target labels when compiler reorders cold/warm blocks. From 6a5e675c3f20a1521e9b515d20915bbafcca52dd Mon Sep 17 00:00:00 2001 From: iritkatriel Date: Fri, 22 Sep 2023 15:17:28 +0100 Subject: [PATCH 5/5] explicit_jump does not need a label --- Python/flowgraph.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 379912be38aebf7..adfcef33895a538 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -2149,7 +2149,6 @@ push_cold_blocks_to_end(cfg_builder *g) { 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; - explicit_jump->b_label.id = next_lbl++; b->b_next = explicit_jump; /* set target */