Skip to content

perf(optimize): strip deopt states, prune dead loop params, inline pure leaves - #327

Merged
siyul-park merged 3 commits into
mainfrom
perf/optimize-pipeline
Nov 11, 2026
Merged

siyul-park merged 3 commits into
mainfrom
perf/optimize-pipeline

Conversation

@siyul-park

Copy link
Copy Markdown
Owner

Summary

The bytecode optimizer (optimize → transform.SSAPass) now:

  • Inlines at O3. It runs InlinePass with a trapless rule, so only pure scalar leaves whose body cannot trap are inlined. A division or remainder by a nonzero constant is allowed. Callees are read from copies taken before any rewrite, so a rewrite that is later rolled back leaves nothing in a caller.
  • Strips deopt states before the pipeline runs (StripPass), because emission never reads them. Stripped SSA is outside the ssa.Verify contract; program.Verify still checks the emitted bytecode.
  • Prunes dead non-entry block params in DCE, together with the edge arguments that feed them. Entry params and their edge arguments are always kept. This pruning also runs in the JIT pipeline, but there a param named by a state frame stays live.

The JIT tiers do not use SSAPass, so they keep their states.

Known behaviour change: when O3 inlines a call, it also removes that call's frame and stack use. A frame or stack limit that was reached only at that call no longer traps. A test pins this, and docs/pass-system.md documents it.

Evidence

All 29 kernels give the same results as O0 at O1, O2 and O3, on both the threaded and the jit backends.

Threaded, O3 vs O0. Each run is the min over interleaved repeats; the table shows the median of 3 runs.

kernel before after
mandelbrot 0.95 1.15
spectral-norm 0.95 1.13
string-compare 0.88 1.00
permutation-flips 0.94 1.00
fannkuch 0.94 0.97
n-body 1.07 1.08

Every other kernel is 1.00 ±0.02.

JIT, O3 vs O0.

kernel before after
mandelbrot 0.92 1.12
spectral-norm 0.94 0.91
n-body 0.88 0.91
i64-wide-fib 1.00 0.91

The rest are within noise. When re-run, i64-wide-fib reads 0.92–0.93 and spectral-norm reads 0.85–0.97 on both builds, so I treat both rows as noise.

Tests were written red first, and mutation checks cover each new rule.

Validation

  • go test ./internal/... ./optimize ./interp ./program
  • make check (pre-commit hook)
  • git diff --check

Docs

  • docs/pass-system.md: covers inlining in the bytecode path, the trapless rule and the removed call frame, StripPass, and DCE param pruning.

Review

An adversarial Opus review found no CRITICAL issues. The second commit fixes both MEDIUM and all LOW findings.

🤖 Generated with Claude Code

siyul-park and others added 2 commits November 11, 2026 06:19
…aves

The bytecode path never reads a deopt state, but the states kept dead loop
temporaries alive, so the emitter carried them through every back edge. The
SSA round trip now strips states ahead of its pipeline, and DCE removes
block params nothing reads together with the edge arguments feeding them.
Entry params are always kept; the JIT tiers keep their states.

optimize.O3 also inlines pure scalar leaves ahead of the strip. The bytecode
path inlines only a body that cannot trap, since a trap would otherwise be
reported in the caller's frame.

Threaded O3 vs O0: mandelbrot 0.95 -> 1.15, spectral-norm 0.95 -> 1.13,
permutation-flips 0.94 -> 1.00, string-compare 0.88 -> 1.00, fannkuch
0.94 -> 0.97; no kernel regresses beyond noise. JIT ratios are unchanged
within noise.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…llees

DCE pushed the edge arguments of live non-entry params only, so a back edge
into block 0 lost its argument; it now pushes them for entry params too.

The bytecode inliner read callee code that an earlier round trip had
already rewritten in place, which a later rollback would not undo in the
caller. SSAPass now resolves callees through copies taken before any
rewrite. Its option layer is a bool, and the inliner flag is renamed
trapless so it no longer shares a name with LICM's quiet.

Docs and tests state that stripped SSA is outside the ssa.Verify
contract, that PromotePass may add a state back, that inlining removes a
call's frame and stack use (pinned by a frame-limit test), and that a
constant zero divisor keeps its call.

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

coderabbitai Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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: 9c56bae3-7cf8-4910-80d0-a0aa05dab108
Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • 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 d880d8e into main Nov 11, 2026
1 check 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