From 610738b9f62f81efdede87e8f35b9e747d45cda6 Mon Sep 17 00:00:00 2001 From: cocolato Date: Thu, 26 Mar 2026 22:56:25 +0800 Subject: [PATCH 1/3] optimize CFG bb inline --- Lib/test/test_peepholer.py | 8 ++++++++ Python/flowgraph.c | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py index e0cc010f15513b2..1d8c6422faeaa33 100644 --- a/Lib/test/test_peepholer.py +++ b/Lib/test/test_peepholer.py @@ -1129,6 +1129,14 @@ def f(self): print(ExecError) self.assertInBytecode(f, "LOAD_FAST_BORROW", "self") + def test_ternary_expression_uses_load_fast_borrow(self): + def f(a, b): + return a if a < b else b + + self.assertNotInBytecode(f, "LOAD_FAST") + self.assertInBytecode(f, "LOAD_FAST_BORROW", "a") + self.assertInBytecode(f, "LOAD_FAST_BORROW", "b") + class DirectCfgOptimizerTests(CfgOptimizationTestCase): def cfg_optimization_test(self, insts, expected_insts, diff --git a/Python/flowgraph.c b/Python/flowgraph.c index f446a87ee694321..36d57b0fbed1c0d 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -1218,6 +1218,7 @@ basicblock_has_no_lineno(basicblock *b) { /* If this block ends with an unconditional jump to a small exit block or * a block that has no line numbers (and no fallthrough), then * remove the jump and extend this block with the target. + * Also handles the case where a block falls through to a small exit block. * Returns 1 if extended, 0 if no change, and -1 on error. */ static int @@ -1227,6 +1228,15 @@ basicblock_inline_small_or_no_lineno_blocks(basicblock *bb) { return 0; } if (!IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)) { + // If the block ends with a fallthrough to a small exit block, inline it. + if (BB_HAS_FALLTHROUGH(bb) && bb->b_next != NULL && !is_jump(last)) { + basicblock *next = bb->b_next; + if (basicblock_exits_scope(next) && next->b_iused <= MAX_COPY_SIZE) { + RETURN_IF_ERROR(basicblock_append_instructions(bb, next)); + next->b_predecessors--; + return 1; + } + } return 0; } basicblock *target = last->i_target; From d930c34da9c858409aded4a46c2669e06d01795d Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 27 Mar 2026 04:16:10 +0000 Subject: [PATCH 2/3] =?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 --- .../2026-03-27-04-16-08.gh-issue-145629.eLFhwn.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-03-27-04-16-08.gh-issue-145629.eLFhwn.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-27-04-16-08.gh-issue-145629.eLFhwn.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-27-04-16-08.gh-issue-145629.eLFhwn.rst new file mode 100644 index 000000000000000..705057a6ab95a12 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-27-04-16-08.gh-issue-145629.eLFhwn.rst @@ -0,0 +1 @@ +Fix missed ``LOAD_FAST_BORROW`` optimization for ternary expressions by improving basic block inlining during the compiler's control flow graph optimization phase. From 15ace3bb66840ed42997574fcf23a41f0447789d Mon Sep 17 00:00:00 2001 From: cocolato Date: Tue, 17 Nov 2026 09:46:45 +0000 Subject: [PATCH 3/3] address review --- ...-03-27-04-16-08.gh-issue-145629.eLFhwn.rst | 2 +- Python/flowgraph.c | 33 +++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-27-04-16-08.gh-issue-145629.eLFhwn.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-27-04-16-08.gh-issue-145629.eLFhwn.rst index 705057a6ab95a12..9bea5c06f3c5ee2 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-27-04-16-08.gh-issue-145629.eLFhwn.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-27-04-16-08.gh-issue-145629.eLFhwn.rst @@ -1 +1 @@ -Fix missed ``LOAD_FAST_BORROW`` optimization for ternary expressions by improving basic block inlining during the compiler's control flow graph optimization phase. +Use ``LOAD_FAST_BORROW`` in both branches of a returned conditional expression by merging single-predecessor exit blocks in the CFG optimizer. diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 333f2b46afcb9e5..829a5f7a57b2937 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -1226,7 +1226,6 @@ basicblock_has_no_lineno(basicblock *b) { /* If this block ends with an unconditional jump to a small exit block or * a block that has no line numbers (and no fallthrough), then * remove the jump and extend this block with the target. - * Also handles the case where a block falls through to a small exit block. * Returns 1 if extended, 0 if no change, and -1 on error. */ static int @@ -1236,15 +1235,6 @@ basicblock_inline_small_or_no_lineno_blocks(basicblock *bb) { return 0; } if (!IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)) { - // If the block ends with a fallthrough to a small exit block, inline it. - if (BB_HAS_FALLTHROUGH(bb) && bb->b_next != NULL && !is_jump(last)) { - basicblock *next = bb->b_next; - if (basicblock_exits_scope(next) && next->b_iused <= MAX_COPY_SIZE) { - RETURN_IF_ERROR(basicblock_append_instructions(bb, next)); - next->b_predecessors--; - return 1; - } - } return 0; } basicblock *target = last->i_target; @@ -1288,6 +1278,28 @@ inline_small_or_no_lineno_blocks(basicblock *entryblock) { return changes; } +/* Merge an exit block into its fallthrough predecessor when it is the sole + * predecessor (requires b_predecessors to be up to date). + */ +static int +merge_single_predecessor_exit_blocks(basicblock *entryblock) +{ + for (basicblock *b = entryblock; b != NULL; b = b->b_next) { + cfg_instr *last = basicblock_last_instr(b); + basicblock *next = b->b_next; + if (last == NULL || next == NULL || is_jump(last) || + !BB_HAS_FALLTHROUGH(b) || next->b_predecessors != 1 || + !basicblock_exits_scope(next)) + { + continue; + } + RETURN_IF_ERROR(basicblock_append_instructions(b, next)); + next->b_iused = 0; + next->b_predecessors = 0; + } + return SUCCESS; +} + // Attempt to eliminate jumps to jumps by updating inst to jump to // target->i_target using the provided opcode. Return whether or not the // optimization was successful. @@ -2687,6 +2699,7 @@ optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache, RETURN_IF_ERROR(check_cfg(g)); RETURN_IF_ERROR(inline_small_or_no_lineno_blocks(g->g_entryblock)); RETURN_IF_ERROR(remove_unreachable(g->g_entryblock)); + RETURN_IF_ERROR(merge_single_predecessor_exit_blocks(g->g_entryblock)); RETURN_IF_ERROR(resolve_line_numbers(g, firstlineno)); RETURN_IF_ERROR(optimize_load_const(const_cache, g, consts, consts_index)); for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {