Repository navigation
wasm-opt: add crate with a local cleanup pass - #284
Merged
Merged
Conversation
Add `starstream-wasm-opt`, a Wasm-to-Wasm pass over the core module `starstream-to-wasm` emits, run at the end of `Compiler::finish` unless `CompileOptions::optimize` is off. Its first pass removes redundant local traffic: set-then-get becomes a tee, stores to never-read locals become drops, pushes that are immediately dropped go away, and declared locals nothing refers to are removed and the rest renumbered. Every other section, and every body the pass leaves alone, is copied byte for byte. Signed-off-by: rvcas <x@rvcas.dev>
SpaceManiac
approved these changes
Oct 28, 2026
SpaceManiac
left a comment
Contributor
There was a problem hiding this comment.
The location it's inserted into starstream-to-wasm looks good. I didn't spot any mistakes in the changed snapshots.
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
Adds
starstream-wasm-opt, a Wasm-to-Wasm pass over the core modulestarstream-to-wasmemits. Neo-Wasm proves an execution by single-stepping it, so every executed operator is one trace row, and every guest-to-guest call adds a row per declared local of the callee to zero it. The passes here aim to execute fewer operators and declare fewer locals.It runs at the end of
Compiler::finish, before componentization, so the published bytes (and the contract id hashed from them) are the optimized ones.CompileOptions::optimizeturns it off; it defaults to on, and the CLI turns it on.The first pass cleans up how values move through locals. Codegen often stores a value in a local only to read it right back. The rules, applied until none applies:
local.set N; local.get N→local.tee Nlocal.tee N; drop→local.set Nlocal.tee N, where N is never read → nothinglocal.set N, where N is never read →droplocal.get,global.getor a constant, thendrop→ nothingDeclared locals no instruction refers to anymore are then removed and the rest renumbered. Parameters are never removed. Every other section, and every body the pass leaves alone, is copied byte for byte. Neo-Wasm already supports
local.tee.Breaking:
CompileOptionsgains a publicoptimizefield, so struct literals need it.Results
From the trace-row census (
starstream-proving-runtime/tests/trace_rows.rs),mainvs this branch:mainAcross the 17 changed codegen snapshots, the net static change is
local.get−56,local.set−69,local.tee+13,drop+2,global.get−9,i32.const−2. I checked with a script that nothing else changed in those diffs.Testing
starstream-wasm-optfor each rule, renumbering after parameters, and byte-for-byte copying of the other sections (memory, globals, exports, data, thecomponent-typecustom section). Each test also validates the output and checks the pass is idempotent.starstream-to-wasmtests pass, including the overflow and division tests, which run the optimized modules.starstream-proving-runtime,starstream-runtime,starstream-ledgerandstarstream-clitests pass.