Skip to content

perf(optimize): give homed references their proven type, dup adjacent uses - #324

Merged
siyul-park merged 2 commits into
mainfrom
perf/optimize-ref-types
Nov 10, 2026
Merged

siyul-park merged 2 commits into
mainfrom
perf/optimize-ref-types

Conversation

@siyul-park

Copy link
Copy Markdown
Owner

Problem

After #323, two kernels were still well below O0 at O3:

  • fannkuch ran at 0.19x under the JIT and 0.78x threaded. A homed []i32 ref got an any local, because SSA refs carry no static type. The JIT and threaded fusion key on declared slot types. Separately, the local.tee after array.slice kept an untyped value on the stack, so the JIT could not type array.get.
  • closure-counter ran at 0.84x. A value used by two adjacent ops was homed in a new local.

Change (internal/transform/emit.go)

  • Proven ref types. A homed reference's local takes the type its definition guarantees, checked in this order:

    1. the slot type of a local, global or upvalue load;
    2. a constant's type;
    3. the common type of a block param's incoming values;
    4. the type of the slots it is stored to, but only when every such store is in its defining block ahead of every other use;
    5. otherwise any.

    emit now receives the source function, so it can read the capture types.

  • No tee into typed ref slots. A typed ref slot is written and then reloaded, so the reload carries the declared type. any slots still tee.

  • dup. A value whose uses in its own block consume it in turn is duplicated instead of homed. This is skipped ahead of an op that traps or enters a frame. A wrong placement fails the walk and falls back to homing.

  • docs/pass-system.md (Bytecode) updated.

Evidence: O3 speed vs O0 (local, min-of-runs)

kernel threaded before → after jit before → after
fannkuch 0.78 → 0.94 0.19 → 0.98
closure-counter 0.84 → 0.98 0.87 → 1.13
n-body 1.07 → 1.06 0.98 → 0.98
spectral-norm 0.96 → 0.95 1.04 → 1.04
recursive-fib-20 1.00 → 1.01 1.08 → 1.07

Every result matches O0. CI's benchmark A/B does not run the optimizer.

Tests (TestSSAPass_Run), red first

  • Fannkuch shape: a slice kept across blocks stays in a []i32 local, with no any local and no tee.
  • Join of different ref types stays any: red under "take the first incoming type".
  • Ref stored to slots of different types is any: red under "take the first store's type".
  • Struct regression from review: an upvalue load stored to a struct{f64} slot only on a branch that does not run. It must keep its capture type and return 41, not trap.
  • Global verify case: a typed global load, homed, must still verify.
  • dup:
    • counter shape;
    • ref variant (balanced RefCount threaded and jit);
    • exact opcode list where a later operand intervenes (red without the fallback);
    • no dup into calls.

Review

Two Opus rounds.

  • Round 1 blocked on a HIGH. The store-type rule narrowed a value by a store on an unexecuted path. The repro trapped with type mismatch where O0 returned 41. Fixed: the definition type wins, and stores count only in the defining block ahead of every other use.
  • Round 2: no new regression. Verified:
    • probes for store-after-use, reload-from-other-type, and equal-typed stores into a join;
    • dup ref totals on trap paths;
    • tee scope.
  • Trap state. The leftover stack and ip after an uncaught trap differ from O0. That is acceptable: trap parity is defined for the same bytecode (fusion.md), and the optimizer must only repair handlers.

Not in this PR. It already fails at main, so it is a follow-up:

  • An op-produced ref (e.g. array.slice of a []i8) homed as any and then stored to a []i8 global fails program.Verify, although the input verified.

Validation

  • go test ./internal/transform ./optimize ./interp
  • go test -race ./internal/transform -run TestSSAPass_Run
  • make check
  • git diff --check

🤖 Generated with Claude Code

… uses

At O3, fannkuch still ran 5x slower under the JIT: a homed array ref
got an `any` local, because SSA refs carry no static type, and the JIT
and threaded fusion key on declared slot types. closure-counter lost
14% to a value homed only because two adjacent ops consumed it.

- A homed reference's local takes the type its definition guarantees:
  the slot type of a local, global or upvalue load, a constant's type,
  or the common type of a block param's incoming values; else the type
  of the slots it is stored to when every such store is in its defining
  block ahead of every other use; else `any`.
- A typed reference slot is reloaded rather than teed, so the reload
  carries the declared type.
- A value whose uses in its own block consume it in turn is duplicated
  instead of homed, except ahead of an op that traps or enters a frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 02a8db61-a23a-44bb-b564-c6f6e2f1edb0
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@siyul-park
siyul-park merged commit 6fc3320 into main Nov 10, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant