Skip to content

wasm: inline @byteSwap, SIMD cmp->bitselect, reused-local DSE, and multivalue inlining - #238

Merged
cataggar merged 1 commit into
wazigfrom
perf/wazig-0171-opts
Nov 11, 2026
Merged

cataggar merged 1 commit into
wazigfrom
perf/wazig-0171-opts

Conversation

@cataggar

Copy link
Copy Markdown
Owner

Summary

Pre-v0.17.1-wazig.1 Wasm backend & post-link optimizer improvements driven by knots Wasmtime guest CPU profiling:

  • src/codegen/wasm/CodeGen.zig (Inline @byteSwap): Lower @byteSwap inline for <= 128-bit signed and unsigned integers using i32.rotl and byte-mask shifts instead of out-of-line __bswapsi2 / __bswapdi2 calls.
  • src/codegen/wasm/CodeGen.zig (SIMD cmp_vector -> v128.bitselect): Extend CmpVectorReduceAnalysis and airSelect so cmp_vector results consumed by width-matching @select stay in v128 registers and lower directly to v128.bitselect without spilling to a shadow-stack [N]bool buffer.
  • src/link/Wasm/component/Optimizer.zig (Backward Reused-Local Overwrite DSE & Multi-Value Inlining):
    • Eliminate dead local.set / local.tee overwrites across reused LocalPool locals within basic blocks and pop pure non-trapping producer expressions.
    • Raise max_inline_results to 8 to match #237's multivalue ABI and allow tiny 1-call wrappers (e.g. 13B ArrayList.appendAssumeCapacity) in .small and ensureUnusedCapacity in .fast.

Verification & Impact

  • Optimizer.zig (140/140), Optimize.zig (28/28), and test/behavior/wasm.zig (wasm32-wasi Debug/ReleaseSmall/ReleaseFast +simd128 and wasm64-wasip3) all pass.
  • Self-compiled wazig.wasm (0.17.1-wazig.1) shrinks by -105.5 KB (31,829,228 B -> 31,723,703 B; wazig.tar.zst 8.08 MiB).
  • On identical knots source, ReleaseSmall shrinks by -21.3 KB while speeding up total_bench_ms by 6.6%–8.8% (matching LLVM 22 -fllvm overall at 250.56ms vs 250.21ms), and ReleaseFast +simd128 beats LLVM 22 overall (213.78ms vs 230.48ms).

… and multivalue inlining

- CodeGen.zig: lower @byteswap inline for <=128-bit integers using i32.rotl / byte-mask shifts instead of out-of-line __bswapsi2 / __bswapdi2 calls.
- CodeGen.zig: keep direct SIMD cmp_vector lane masks in v128 locals when consumed by matching @select, emitting v128.bitselect directly without shadow-stack [N]bool round-trips.
- Optimizer.zig: add conservative intra-block backward dead-overwrite elimination for reused Wasm locals + pure-expression popping, shrink ReleaseSmall binaries (-21 KB on knots, -105 KB on wazig.wasm), and align inliner thresholds with 8-result multivalue ABI and 1-call capacity wrappers.
- test/behavior/wasm.zig & Optimizer.zig: add unit and runtime behavior tests across signed/unsigned byteSwap widths, SIMD vector select, and reused-local overwrite DSE.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@cataggar
cataggar enabled auto-merge (squash) November 11, 2026 04:32
@cataggar
cataggar merged commit bf33c96 into wazig Nov 11, 2026
2 checks passed
@cataggar
cataggar deleted the perf/wazig-0171-opts branch November 11, 2026 04:38
cataggar added a commit to cataggar/knots that referenced this pull request Nov 11, 2026
…aths and update 0.17.1-wazig.1 benchmarks (#36)

- unicode/itemize/linebreak: add 8-byte SWAR ASCII fast paths for decodeUtf8 and properties, single-pass .latn ASCII itemization, comptime ASCII UAX#14 class table + common-pair fast checks, and unwrapped single-line fast path when wrap_width <= 0.
- TextSystem/TextLayout/GlyphStore/Outline: replace Face.glyphs hash map with dense O(1) glyph_slots array, reuse persistent LayoutScratch and outline_vertices buffers, consolidate TextLayout into a single contiguous slab allocation, bypass word cache on plain space runs, encode glyphs directly into GlyphStore.words with reciprocal quantization, and pre-validate Outline.Points bounds.
- UI/DrawList: cache measured/reflowed TextLayout on Decoration.Text across a frame, hoist uniform f16 color conversion in paintText, and replace per-command gridCoord float divisions with reciprocal multiplies.
- benchmarks: update bench-wazig.py, WAZIG_BENCHMARKS.md, and wazig-vs-zig.json for wazig v0.17.1-wazig.1 (with cataggar/zig#238), v0.17.0-wazig.6, v0.17.0-wazigl.6, and zig v0.17.1.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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