Repository navigation
perf(optimize): fold constant control flow, simplify the CFG, and optimize module code - #341
Merged
Merged
Conversation
…imize module code The bytecode optimizer left most of its opportunities on the table: top-level code with a loop was never rewritten, constant branches and constant block params were never folded, and the benchmark harness never ran the optimizer. SSA passes (shared by `optimize` and the JIT tiers): - Fold turns a branch or table on a constant, or a branch with identical edges, into a jump; reduces integer identities, absorbing constants and same-operand forms; and treats a block param every surviving edge feeds the same constant as that constant. Bitwise results keep their operands' kind. - SimplifyPass replaces trivial block params and merges a block into its only predecessor's jump. - CSE keys commutative integer ops on sorted operands. - Forward makes a stored non-ref, non-i64 value the held value of its slot. - LICM hoists shape guards and unstored local loads out of loops that release or allocate but contain no call. - O1/O2/O3 run Simplify; O3 folds again after promotion. Bytecode emitter: - Module code may home values in locals, so top-level loops are rewritten. - Leading operands are loaded ahead of the expression tree that produces a later operand, instead of homing its result. - Exit blocks go last; a loop header's invertible branch falls through into the body. Contracts: - Module locals are not program semantics. - Object representation is immutable: Interpreter.Store rejects a representation change with ErrTypeMismatch (scalar cells excepted). - Host code, including finalizers, must not write caller locals or globals: SetLocal/SetGlobal return ErrHostCall; a caught host panic resets the count. Benchmarks: a new `optimized` mode (O3 bytecode, threaded) and the `naive-checksum` fixture modelling unoptimized frontend output. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
Merged
4 of 5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The bytecode optimizer left most of its opportunities unused: top-level code with a loop was never rewritten, constant branches and constant block params were never folded, and the benchmark harness never ran the optimizer at all.
SSA passes (shared by
optimizeand the JIT tiers)x*0,x&0,x|-1) and same-operand forms (x-x,x==x) fold on either side; a block param every surviving edge feeds the same constant is that constant. Bitwise results keep their operands' kind tag. Irreducible graphs retry without collapsing terminators.Fold, Simplify, DCE; O2 addsCSE, Guard; O3 runs Simplify and Fold again after Promote.Bytecode emitter
Contracts (documented + enforced)
docs/pass-system.md).Interpreter.Storerejects a representation change withErrTypeMismatch(scalar cells excepted) (docs/memory-model.md).SetLocal/SetGlobalreturnErrHostCall; a caught host panic resets the count (docs/host-integration.md).Benchmarks
optimizedmode (O3 bytecode, threaded) and an Optimized column in the report.naive-checksumfixture modelling unoptimized frontend output (existing fixtures are already hand-optimized, so they hid optimizer gains).Measurements (local interleaved A/B vs main, Apple M-series, n=6–8)
CI regenerates
docs/benchmarks.md; no locally measured table is committed.Known follow-ups
REF_SETscalar()check cost unmeasured in isolation.Test plan
make check(lint, generate check, all package tests incl. JIT)git diff --check🤖 Generated with Claude Code