Skip to content

nix: apply patched wasm-opt (LSB-test → ctz) to RTS wasm files#5967

Draft
ggreif wants to merge 4 commits intomasterfrom
gabor/rts-wasm-opt
Draft

nix: apply patched wasm-opt (LSB-test → ctz) to RTS wasm files#5967
ggreif wants to merge 4 commits intomasterfrom
gabor/rts-wasm-opt

Conversation

@ggreif
Copy link
Copy Markdown
Contributor

@ggreif ggreif commented Apr 1, 2026

Summary

  • Adds ggreif/binaryen@gabor/lsb-if-ctz-flake as a flake input, which exposes a patched wasm-opt implementing the optimization from WebAssembly/binaryen#8562
  • In nix/rts.nix, runs wasm-opt --optimize-instructions on the three non-debug RTS variants during installPhase
  • Debug variants are left unoptimized intentionally

The optimization

The patched wasm-opt folds LSB-test patterns common in the RTS into i32.ctz:

i32.const 1; i32.and; i32.eqz; br_if  →  i32.ctz; br_if
i32.const 1; i32.and; if T E           →  i32.ctz; if E T

These patterns appear 26–105 times per RTS variant, primarily in the is_skewed/is_scalar pointer-tagging checks in the GC hot path.

Benchmark results (EOP RTS, gross effect of --optimize-instructions)

Benchmark Before After Δ
heap-32 (run 1) 1,153,792,735 instr 1,151,398,207 instr −0.21%
heap-32 (run 2) 1,256,407,315 instr 1,253,408,059 instr −0.24%
heap-64 (run 1) 1,324,057,357 instr 1,321,855,449 instr −0.17%
heap-64 (run 2) 1,295,845,087 instr 1,293,744,743 instr −0.16%
bignum 2,504,499 cycles 2,504,383 cycles −116
candid-subtype-cost 1,115,011 cycles 1,114,823 cycles −188

Numbers are gross effects (other rules in --optimize-instructions also contribute).

Notes

  • Once WebAssembly/binaryen#8562 lands upstream, this can be switched to the official nixpkgs binaryen and the fork dependency dropped.

🤖 Generated with Claude Code

Add ggreif/binaryen (branch gabor/lsb-if-ctz-flake) as a flake input,
exposing a patched wasm-opt that folds LSB-test `i32.and X 1` patterns
into `i32.ctz` (WebAssembly/binaryen#8562). Apply it to the non-debug
RTS variants in installPhase, yielding ~0.2% instruction count reductions
in GC-heavy benchmarks.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@ggreif ggreif requested a review from a team as a code owner April 1, 2026 10:43
@ggreif ggreif marked this pull request as draft April 1, 2026 10:43
@ggreif ggreif self-assigned this Apr 1, 2026
@ggreif ggreif added the performance Affects only gas usage or code size label Apr 1, 2026
ggreif and others added 3 commits April 1, 2026 13:16
… check)

The overrideAttrs build was failing because pkgs.binaryen's preConfigure
expected test/spec/testsuite to exist. Updated flake disables tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…fference)

macOS nix computes a different NAR hash than Linux for the same GitHub
commit. Update to the hash Linux CI computes.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 1, 2026

Comparing from 18aedb0 to a1c0a71:
In terms of gas, 5 tests improved and the mean change is -0.0%.
In terms of size, 5 tests improved and the mean change is -0.3%.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Affects only gas usage or code size

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant