Skip to content

v0.7.10.4

Choose a tag to compare

@github-actions github-actions released this 29 Apr 19:39
· 10 commits to main since this release
v0.7.10.4
dff8849

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:2707 kill_local).
    • storeLocal (mirrors Python/flowgraph.c:2718
      store_local).
    • loadFastPushBlock (mirrors Python/flowgraph.c:2728
      load_fast_push_block) — implemented inline as the
      pushBlock closure inside OptimizeLoadFast.
    • numPoppedPushed — narrow port of the
      _PyOpcode_num_popped / _PyOpcode_num_pushed pair 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_OptimizedCfgToInstructionSequence ordering
    (Python/flowgraph.c:4062 — runs immediately before
    _PyCfg_ToInstructionSequence).

  • compiler/codegen/scope_ops.go — both emitNameLoad and
    emitNameLoadCall switch their nameOpFast arms from
    LOAD_FAST_BORROW to plain LOAD_FAST. The promotion is
    now owned by optimize_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_fast as 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 with JUMP_FORWARD → endLabel, the false
    branch falls through to a labelled end-merge block whose
    sole instruction is RETURN_VALUE. The previous shape
    duplicated RETURN_VALUE into both branches and
    pre-classified the false-branch Name as plain LOAD_FAST.
    The new shape lets optimize_load_fast derive 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-coded LOAD_FAST_BORROW 0 at the trivial
    def f(arg): return arg body becomes LOAD_FAST 0. The
    optimizer promotes it back. Header doc-comment updated.

  • compiler/codegen/visit_closuredef_stmt.go — both
    hand-coded LOAD_FAST_BORROW instructions (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_FORWARD predecessors 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_fast to incorrectly promote a LOAD_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 → LFLBLFLB is gone.
    With the visitor no longer emitting LOAD_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_LfbPairBridge renamed to
    TestInsertSuperinstructions_LfbPairNotFused and rewritten
    to assert that LOAD_FAST_BORROW pairs 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_INT quirk removal lands v0.7.10.5.
  • optimize_cfg sub-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.