mirror of
https://github.com/hanzoai/wasm2go.git
synced 2026-10-30 15:06:48 +00:00
* ssa,lower,gcasm: mid-end memopt + pure backend + eqz fusion + wasm-level leaf inliner
Optimization series targeting the wasm2go-vs-wasmtime gap on the
spidermonkey cpubench workload (fib(20)+array, the go-spidermonkey /
tests-cpubench.cc benchmark). Measured on native arm64 M5, interleaved
same-session A/B, verified across all consumers via the wasmify pipeline.
Phase A — internal/ssa/pass/memopt.go: intra-block redundant-load
elimination + store-to-load forwarding over the unsafe linear-memory
accesses gc cannot reason across. Runtime NEUTRAL (input wasm is already
clang -O2; gc re-optimizes emitted pure values), kept for the small size
win and cleaner IR. Gated OFF for shared-memory (threads) modules, where
eliding a non-atomic re-read would drop a peer agent's store — defensive,
pinned by cg_memopt_shared_test.go.
PureOnly (-pure flag / WASM2GO_PURE): emit the pure-Go backend with no
asm bundle and no arch build tags — the ABIInternal reference for
benchmarking codegen quality without the gcasm ABI0 marshalling. Pinned
by transpile/pure_only_test.go (multi-package e2e builds+runs natively).
1e — eqz->comparison fusion (internal/lower): i32/i64.eqz lower to
first-class OpEq{32,64}(x,0) TypeBool so branches fuse to a direct Go
comparison instead of an i32_eqz helper round-trip, and eqz joins
ConstProp/CSE. Runtime NEUTRAL (gc already folded the old inlined shape);
kept for -5% pure binary size and IR cleanliness.
1b — wasm-level leaf inliner (internal/lower/inline.go): small leaf
callees (no call/call_indirect) are spliced into callers during lowering
instead of emitting OpCallDirect. gc cannot do this — cross-chunk calls
go through //go:linkname forwarders opaque to its inliner, and on gcasm
every surviving call pays ABI0 marshalling + a gcasmFwd trampoline that
inlining deletes with the call. Leaf-only, no-EH, size-capped, all
env-tunable (WASM2GO_INLINE*). This is the real win: gcasm cpubench
-12.9% (150.2->130.8 ms), pure -5.3%; wasmtime gap 1.91x -> ~1.66x.
Two latent gcasm-transform bugs the inliner exposed (the inliner lowering
itself is correct — the pure inline bundle passed every consumer):
- internal/codegen/emit_memops.go: an SSA-constant memory-access base
hoisted into a local escaped the AST-shape _consts guard; gc re-folded
the multi-MB constant into an arm64 addressing-mode immediate
("constant is not in pool"). Detect SSA-constant bases (ssaConstBase)
and route large totals through _consts regardless of AST shape.
- internal/gcasm/transform.go: jump-table flag-replay had false-negative
holes — gc keeps a pre-dispatch compare's EFLAGS live across its
flag-preserving table jump and lets case bodies consume them, but the
compare tree clobbers flags; an undetected flag-consuming target got
no replay and leaked the tree's flags into a downstream branch (a
go-python `print(1+1)` SIGSEGV). Now: when a clean replayable compare
sits just before the dispatch, ALWAYS replay it at the leaves
(harmless when unread), eliminating the detection dependency. Pinned
by jumptable_flags_test.go.
Verified (all exit 0): wasm2go go test ./... (9 pkgs); go-python full
suite; go-spidermonkey full suite + -race (shared memory); googlesqlite
35 pkgs / 0 FAIL. Design/analysis in docs/optimization-plan-v2.md.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
* docs: handoff notes — depth-2 inlining attempted and reverted; follow-up bug recorded
Add the handoff section (branch state, verified −14% committed win, the
verification discipline, disk caveats, saved artifacts) and document the
bounded depth-2 inlining attempt: it reached wasm2go 9pkg/0FAIL + e2e but
exposed a latent arm64 literal-pool emit bug (a memory base of the form
OpAdd32(runtime, OpConst32[large]) leaves the large constant as an
addressing-mode immediate — memOffsetExpr's _consts routing covers only
the AuxInt offset and a pure-constant base, not "runtime + large-const").
Reverted depth-2 (low predicted ROI ≤1-2%, the post-1b hot set is
dominated by four functions far over the inline size cap; and the robust
fix needs a non-trivial emit refactor). Leaf-only 1b stands as the final
form. Next steps and the follow-up bug's fix sketch are recorded for the
handoff.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
* ssa,codegen: fold large constant base addends into the memory-access offset
A memory-access base of the form Add32(x, Const32[large]) bypassed the
emitter's _consts literal-pool guard: memOffsetExpr's routing only
covers the AuxInt offset and a PURE-constant base (ssaConstBase), so
the large addend stayed inline in the base expression and gc folded it
into an arm64 addressing-mode immediate — failing to assemble inside
very large functions ("LDPSW 27325896(R2): constant is not in pool",
the follow-up bug recorded in the depth-2 inlining postmortem).
Fix: a new SSA pass, pass.FoldMemAddend, rewrites
Load/Store [off] (Add32 x (Const32 [c]))
=> Load/Store [off + uint32(c)] x
for addends that would need the _consts detour (positive totals >=
largeConstThreshold, negative addends of threshold magnitude — those
sign-extend into the same out-of-range immediate). The folded total
lands on the emitter's existing runtime-base large-offset path, which
already routes through _consts, so no emit changes are needed. Small
addends stay in the base sum where they fold into an in-range
immediate.
The pass runs ONCE after the optimization fixpoint, never inside it:
the fold assumes the remaining base stays a runtime value, whose emit
path wraps the address to uint32 (mod-2^32, exact for wasm). If a
later ConstProp iteration could still constify the base, the emitter's
pure-constant path — which computes the unwrapped u33 total like a
genuine memarg offset — would mis-address folded sums that relied on
i32 wraparound.
Pinned by pass unit tests (positive/negative/commuted/copy-peeled/
sub-threshold/const-const shapes) and testdata/cg_memaddr.wat: wazero-
oracle runtime equivalence including the mod-2^32 wrap of a negative
addend, plus an emit-shape assertion that no access site keeps the
addend as an inline int32 literal.
The pass fires at scale on real inputs (~2,700 additional _consts-
routed sites on python.wasm) and measured a large runtime WIN on the
spidermonkey cpubench (pure lane −18%, gcasm lane part of −24%; see
bench-metrics.md memaddr entry for the interleaved A/B).
Co-Authored-By: Claude Fable 5 <[email protected]>
* gcasm: port the jump-table flag-replay fix to arm64
The amd64 findJumpTables fix (d4b6ca3) - replay the pre-dispatch
flag-setter at every compare-tree leaf, or fall back to pure when
flags might be consumed and no clean setter exists - was never ported
to a64FindJumpTables. The arm64 compare tree (CMPW/BHS) clobbers NZCV
exactly like the amd64 one clobbers EFLAGS, and gc's captured dispatch
preserves it, so a dispatch target consuming the pre-dispatch bounds-
check flags read the tree's leftovers instead: a downstream branch
went the wrong way and corrupted linear memory.
Observed: deterministic go-python arm64 gcasm SIGSEGV at the previous
commit - Fn3705's dispatch corrupted a list header, and the victim
Fn3708 stored through a ~4 GB wild offset. Isolated by pure-arm64
being green, GCASM_FALLBACK_RANGE halving, and the fix regen going
green.
The port also hardens the replayability rule relative to amd64: a
compare whose operands include the jump-table base or target register
is NOT cleanly replayable - the captured dispatch clobbers both
between the compare and the leaf - and such a site falls back to pure
instead of replaying a clobbered operand.
Pinned by TestA64JumpTableFlagReplayDetect (synthetic-instruction
subtests for replay-at-leaves, consume-without-setter fallback,
clobbered-operand fallback, and flag-neutral pass-through) and
TestA64JumpTableFlagReplayRun (capture, transform, assemble, and
native-run parity against the pure reference, mirroring the amd64
gate).
Co-Authored-By: Claude Fable 5 <[email protected]>
* docs: handoff update — memaddr fold shipped, arm64 flag-replay miscompile found+fixed, gap 1.66x->1.25x
Co-Authored-By: Claude Fable 5 <[email protected]>
* docs: retract the -24%/-18% perf claim - cross-arch A/B artifact; both fixes are runtime-neutral
The benchmark deltas claimed in the previous two commits (4a77ed9's
"large runtime WIN", eca0e2f's "gap 1.66x->1.25x") are RETRACTED. The
saved baseline cpubench binaries were Mach-O x86_64 running under
Rosetta, while the new binaries were native arm64 - the A/B compared
architectures, not wasm2go versions. Discovered during the pprof
attribution pass, when the "hot function" disassembly came out as
amd64 instructions.
Clean re-measurement (input wasm sha256-pinned before and after each
generation, both lanes arm64-native, only wasm2go differing, five
interleaved pairs, idle machine):
gcasm: baseline 96.82 ms vs fix 97.66 ms (+0.9%, noise)
pure: baseline 83.12 ms vs fix 82.44 ms (-0.8%, noise)
The memaddr fold and the arm64 flag-replay port are correctness fixes
with NEUTRAL runtime - the fourth confirmation that source-shape
changes to pure computation are redundant with gc.
The real finding is about the measurement history: the true native
arm64 wasmtime gap at the previous commit was ALREADY gcasm ~1.24x /
pure ~1.05x (96.8 ms / 83.1 ms vs wasmtime 78.3 ms, wasmtime 46.0.1).
The recorded 1.91x -> 1.66x gap trajectory was measured with Rosetta
Go lanes against native wasmtime and is systematically inflated; pure
is effectively at wasmtime parity, and the remaining gcasm gap is the
ABI0 call-marshalling tax.
The handoff notes now carry the corrected numbers, the revised
next-step ranking, and the new methodology rules (record every bench
binary's architecture via `file`; pin the input wasm sha across any
generation used in an A/B - it was observed rebuilding mid-session,
shifting function indices and invalidating cross-boundary pprof
diffs).
Co-Authored-By: Claude Fable 5 <[email protected]>
* docs: untrack the working plan documents
The optimization plan / handoff notes are session-working documents,
not part of the library. Keep them as local untracked files (like
bench-metrics.md and the other local docs) instead of committing them.
Co-Authored-By: Claude Fable 5 <[email protected]>
* lower: parse WASM2GO_INLINE_ONLY_CALLEES entries with ParseUint
strconv.Atoi yields a platform-width int that was converted to uint32
without a bounds check (CodeQL go/incorrect-integer-conversion).
ParseUint with bitSize 32 rejects negatives and out-of-range values up
front, so the conversion cannot truncate; bad entries keep the existing
warn-and-skip behavior.
Co-Authored-By: Claude Fable 5 <[email protected]>
---------
Co-authored-by: Claude Opus 4.8 <[email protected]>
128 lines
3.5 KiB
Go
128 lines
3.5 KiB
Go
package transpile_test
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/goccy/wasm2go/transpile"
|
|
)
|
|
|
|
// TestPureOnlySingleFile pins the PureOnly contract in single-file
|
|
// mode: the emitted source carries no arch build gates and Translate
|
|
// produces no asm bundle (no .s, no decls files) — the pure bodies
|
|
// compile on every GOARCH.
|
|
func TestPureOnlySingleFile(t *testing.T) {
|
|
m, err := transpile.Parse(bytes.NewReader(realAddWasm()))
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
restore := transpile.SetMultiPackageThreshold(1 << 30)
|
|
defer restore()
|
|
var buf bytes.Buffer
|
|
res, err := transpile.Translate(&buf, m, transpile.Options{
|
|
Package: "pkg",
|
|
OutputImportPath: "gentest/pkg",
|
|
PureOnly: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Translate: %v", err)
|
|
}
|
|
assertNoAsmArtifacts(t, buf.Bytes(), res)
|
|
}
|
|
|
|
// TestPureOnlyMultiPackageEndToEnd forces the multi-package layout
|
|
// with PureOnly and runs the generated bundle natively. On amd64/arm64
|
|
// this only works when the pure bodies are emitted WITHOUT the
|
|
// `!amd64 && !arm64` dormancy gate — i.e. it proves the pure backend
|
|
// is buildable and correct on the asm-target GOARCHs, which is the
|
|
// whole point of the flag (the ABIInternal benchmarking reference).
|
|
func TestPureOnlyMultiPackageEndToEnd(t *testing.T) {
|
|
m, err := transpile.Parse(bytes.NewReader(realAddWasm()))
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
restore := transpile.SetMultiPackageThreshold(0)
|
|
defer restore()
|
|
var buf bytes.Buffer
|
|
res, err := transpile.Translate(&buf, m, transpile.Options{
|
|
Package: "pkg",
|
|
OutputImportPath: "gentest/pkg",
|
|
PureOnly: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Translate: %v", err)
|
|
}
|
|
assertNoAsmArtifacts(t, buf.Bytes(), res)
|
|
|
|
dir := t.TempDir()
|
|
writeFile := func(rel string, data []byte) {
|
|
p := filepath.Join(dir, rel)
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, data, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
writeFile("go.mod", []byte("module gentest\n\ngo 1.25.0\n"))
|
|
if buf.Len() > 0 {
|
|
writeFile("pkg/gen.go", buf.Bytes())
|
|
}
|
|
for rel, data := range res.Files {
|
|
writeFile("pkg/"+rel, data)
|
|
}
|
|
for name, data := range res.Sidecars {
|
|
writeFile("pkg/"+name, data)
|
|
}
|
|
// Multi-package exports are top-level funcs taking the *base.Module.
|
|
writeFile("main.go", []byte(`package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"gentest/pkg"
|
|
)
|
|
|
|
func main() {
|
|
m := pkg.New()
|
|
fmt.Println(pkg.Add(m, 7, 35))
|
|
}
|
|
`))
|
|
|
|
cmd := exec.Command("go", "run", ".")
|
|
cmd.Dir = dir
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("go run failed: %v\n%s", err, out)
|
|
}
|
|
if got := strings.TrimSpace(string(out)); got != "42" {
|
|
t.Fatalf("Add(7,35) printed %q, want 42\n%s", got, out)
|
|
}
|
|
}
|
|
|
|
// assertNoAsmArtifacts checks that a PureOnly translation contains no
|
|
// asm bundle files and no arch-gating build tags anywhere.
|
|
func assertNoAsmArtifacts(t *testing.T, main []byte, res transpile.Result) {
|
|
t.Helper()
|
|
check := func(name string, data []byte) {
|
|
if strings.HasSuffix(name, ".s") {
|
|
t.Errorf("PureOnly emitted an asm file: %s", name)
|
|
}
|
|
if strings.Contains(filepath.Base(name), "decls_") {
|
|
t.Errorf("PureOnly emitted a gcasm decls file: %s", name)
|
|
}
|
|
src := string(data)
|
|
if strings.Contains(src, "!amd64") || strings.Contains(src, "amd64 || arm64") {
|
|
t.Errorf("PureOnly output %s still carries an arch build gate", name)
|
|
}
|
|
}
|
|
check("main", main)
|
|
for name, data := range res.Files {
|
|
check(name, data)
|
|
}
|
|
}
|