Repository navigation
v0.7.10.4
v0.7.10.4 — optimize_load_fast 1:1 port
Date: 2026-04-30
Spec: notes/Spec/1500/1562_gocopy_v07104_optimize_load_fast.md
Roadmap: notes/Spec/1500/1558_gocopy_v0710x_release_roadmap.md
v0.7.10.4 ports CPython 3.14
Python/flowgraph.c::optimize_load_fast into the gocopy
flowgraph package as the second flowgraph pass. The visitor
stops emitting LOAD_FAST_BORROW directly; the new pass
performs the strength reduction from the per-block lifetime
analysis CPython uses.
Added
compiler/flowgraph/optimize_load_fast.go—OptimizeLoadFast(mirrors Python/flowgraph.c:2776
optimize_load_fast).killLocal(mirrors Python/flowgraph.c:2707kill_local).storeLocal(mirrors Python/flowgraph.c:2718
store_local).loadFastPushBlock(mirrors Python/flowgraph.c:2728
load_fast_push_block) — implemented inline as the
pushBlockclosure insideOptimizeLoadFast.numPoppedPushed— narrow port of the
_PyOpcode_num_popped/_PyOpcode_num_pushedpair for
the opcodes the visitor currently emits at this pipeline
stage.
compiler/flowgraph/optimize_load_fast_test.go— 7 unit
tests covering: trivial RETURN_VALUE promotion, the
STORED_AS_LOCAL stays-LOAD_FAST guard, BINARY_OP both
borrow, pre-fused LFLF → LFLBLFLB, REF_UNCONSUMED stays
LOAD_FAST, the SUPPORT_KILLED interaction with STORE_FAST,
and the nil-seq edge case.
Changed
-
compiler/optimize/optimize.go::Run— pipeline now runs
flowgraph.OptimizeLoadFast(seq)between
flowgraph.InsertSuperinstructions(seq)and
resolveJumps(seq), approximating CPython's
_PyCfg_OptimizedCfgToInstructionSequenceordering
(Python/flowgraph.c:4062 — runs immediately before
_PyCfg_ToInstructionSequence). -
compiler/codegen/scope_ops.go— bothemitNameLoadand
emitNameLoadCallswitch theirnameOpFastarms from
LOAD_FAST_BORROWto plainLOAD_FAST. The promotion is
now owned byoptimize_load_fast. -
compiler/codegen/visit_funcbody_stmt.go::emitFuncBodyAugAssign
— the LOAD_FAST_BORROW for the AugAssign target becomes
LOAD_FAST. Header comment rewritten to drop the
pre-classifier story and point at
optimize_load_fastas the owner of the borrow
promotion. -
compiler/codegen/visit_funcbody_stmt.go::emitFuncBodyTernaryReturn
— the visitor now emits a CPython-shaped IfExp CFG: the
true branch ends withJUMP_FORWARD → endLabel, the false
branch falls through to a labelled end-merge block whose
sole instruction isRETURN_VALUE. The previous shape
duplicatedRETURN_VALUEinto both branches and
pre-classified the false-branch Name as plain LOAD_FAST.
The new shape letsoptimize_load_fastderive the same
outcome from the CFG: the JUMP_FORWARD predecessor is
inlined (true branch becomes scope-exit-terminated, LF
promoted to LFB), the fall-through predecessor leaves its
load with REF_UNCONSUMED set and stays LF. -
compiler/codegen/visit_functiondef_stmt.go— the
hand-codedLOAD_FAST_BORROW 0at the trivial
def f(arg): return argbody becomesLOAD_FAST 0. The
optimizer promotes it back. Header doc-comment updated. -
compiler/codegen/visit_closuredef_stmt.go— both
hand-codedLOAD_FAST_BORROWinstructions (cell-tuple
build at outer block, inner-function return) become
LOAD_FAST. The optimizer promotes both back. Header
doc-comment updated. -
compiler/optimize/inline_small_exit_blocks.go::inlineSmallExitBlocks
— narrowed to inline a candidate merge tail into
JUMP_FORWARDpredecessors only. Fall-through
predecessors are no longer inlined, and the merge block
survives whenever a fall-through predecessor remains.
This matches CPython's
basicblock_inline_small_or_no_lineno_blocks, which only
acts on blocks ending with an unconditional jump. The old
fall-through inlining over-consumed refs within the
falling-through predecessor and would have caused the new
optimize_load_fastto incorrectly promote aLOAD_FAST
that CPython leaves unpromoted (visible in the IfExp
ternary fixtures 185 / 186).
Removed
-
compiler/flowgraph/insert_superinstructions.go::makeSuperInstruction
— the v0.7.10.3 deviation case
LOAD_FAST_BORROW + LOAD_FAST_BORROW → LFLBLFLBis gone.
With the visitor no longer emittingLOAD_FAST_BORROW,
the pass is a strict 1:1 port of CPython's three pairs:
LF+LF,SF+LF,SF+SF. Header comment rewritten. -
compiler/flowgraph/insert_superinstructions_test.go—
TestInsertSuperinstructions_LfbPairBridgerenamed to
TestInsertSuperinstructions_LfbPairNotFusedand rewritten
to assert thatLOAD_FAST_BORROWpairs are NOT fused at
this stage.
Tests
- 254/254 fixtures byte-identical (
bash tests/run.sh). TestVisitorParity:
byte-identical=213 not-implemented=41 mismatched=0 errored=0 skipped=0 (total=254).
Unchanged from v0.7.10.3 — refactor of where the borrow
promotion runs, not a coverage change.TestOptimizeLoadFast_{TrivialReturn,StoredAsLocal, BinopBothBorrow,LFLF_Pair,RefUnconsumed,KilledByStore, NilSeq}— 7 new unit tests, all PASS.go build / go vet / go test ./...clean.
Out of scope — moves to v0.7.10.5+
LOAD_SMALL_INTquirk removal lands v0.7.10.5.optimize_cfgsub-passes (basicblock-level peephole,
remove_redundant_nops,eliminate_unreachable_code)
land v0.7.10.6.- Wider argument-shape coverage (kw-only, vararg, kwarg,
pos-only) lands v0.7.10.7.