Files
f30ec292fd Support the wasm SIMD proposal with native asm lowering (arm64/amd64) (#40)
* Support the wasm SIMD proposal with native asm lowering (arm64/amd64)

One revision spanning the SIMD (0xfd v128) proposal end to end: SSA
representation, lowering, scalar reference helpers, and a gcasm asm
backend that turns the hot paths into native SIMD — measured on a
full llama.cpp build transpiled to pure Go, which this makes run at
V8-class speed (prompt eval beats V8 on arm64).

Frontend and SSA:
- v128 values are [2]uint64 pairs in little-endian lane order; pure
  ops lower to CSE-able helper calls, memory ops keep statement
  order, v128.const/shuffle ride as [2]uint64 aux. The full scalar
  helper set is the reference semantics (saturation, NaN
  propagation, round-to-even, swizzle OOB, shift masking).
- Exception handling lowers to plain branches instead of
  panic/recover — a check-and-branch after each may-raise call, a
  per-try dispatch block running the catch-clause chain — covering
  the C++-shaped patterns (multi-clause catch, delegate targets,
  rethrow depths) without runtime unwinding, so EH bodies stay
  eligible for the asm backend. This replaces the closure/recover
  lowering that #39 extended; its tests are retained unchanged.
- Giant functions outline into tractable units (whole loops
  included, packed u64 boundaries for v128 values), keeping gc
  compile times and register pressure sane on llama.cpp-sized
  modules.

gcasm backend:
- SIMD helper calls splice inline as native NEON/SSE instead of
  marshalled calls; v128 SSA values travel register-to-register in
  pair form. Call-tree windows fuse into single splices with region
  invariant hoisting, bounds-check coalescing, store sinks and
  scalar-chain internalization; whole countdown loops fuse with an
  in-splice unroll lane.
- Superinstruction selection inside fused regions: 8-bit dot
  patterns (SMULL+SADALP), SDOT for full 16-byte dots, FMUL by
  element, scale-lookup addressing, inlined integer division, the
  software fp32->fp16 idiom lowered to FCVT (both the table-gather
  and the packed store form), and ggml q8_0 row/column pairing via
  the traits nrows patch with an SMMLA 2x2 tile kernel.
- Runtime CPU-feature dispatch gates spliced bodies (dotprod on
  arm64, x86-64-v2 SSE on amd64); WASM2GO_FAST_MATH opts into
  raw-SDOT/FMLA/dual-accumulator forms that trade wasm bit-exactness
  for native-build-style rounding, validated by token-level
  equivalence against native llama.cpp.
- Guard-page linear memory eliminates per-access bounds checks;
  jump tables replace the compare tree for br_table.

Tooling and CI:
- WASM2GO_F16_TABLE asserts the address of ggml's runtime-built f16
  table; the transpiler warns when the asserted address matches no
  gather site (the address moves with every data-layout shift, and a
  stale assertion silently costs ~7% decode throughput).
- The coverage gate runs with -count=1: a cached package result
  merges incomplete coverage data into the -coverpkg profile,
  deflating the total by 10+ points depending on what the runner's
  restored cache contained — the gate must never consume cached
  results.
- Coverage threshold moves to 83% for the enlarged tree; transpile
  output is deterministic and outline rejections are explained in
  the build log.

Co-Authored-By: Claude Fable 5 <[email protected]>

* Replace environment-variable switches with typed Options; drop debug code

Configuration now flows through one channel: transpile.Options gains
OutlineMinValues, SIMDUnroll, FuseLoops, FuseLoopUnroll, F16TableAddr
and FastMath (with matching CLI flags), the gcasm backend receives
them as a Config threaded through ModuleOffsets, and no code path
branches on an environment variable anymore. Integrator entry points
(the wasmify protoc plugin, the CLI) convert their external
configuration into these options exactly once.

Deleted outright, per the no-env rule and because they were
diagnostics rather than build modes:
- every WASM2GO_NO_* kill switch and bisection knob (SSA pass
  disabling, fallback ranges, fuse-window caps, outline limits,
  inliner windows) — git history keeps them for the next hunt;
- every env-gated debug printer (fuse/inline/outline/nrc2/f16/SSA
  dumps and the near-miss tracers) and the capture-tree keepers;
- the dormant multi-exit loop structuring (opt-in, measured slower)
  and the SSA f32->f16 rewrite entry superseded by the gcasm
  peephole (its shared idiom matchers stay for the f16 store pass);
- the runtime WASM2GO_CPU_PORTABLE override in the generated CPU
  feature probes.

The nil-offsets error paths exposed by the threading get nil-safe
Config accessors, and the outline share/v128-min/inliner caps become
plain constants at their measured production values.

Behavior under default options is unchanged: transpiled output for
the non-SIMD and EH fixtures is byte-identical to the previous
commit, and the only SIMD-bundle difference is the removed env check
in the emitted cpufeat runtime files.

Co-Authored-By: Claude Fable 5 <[email protected]>

---------

Co-authored-by: Claude Fable 5 <[email protected]>
2026-08-08 23:28:14 +09:00

290 lines
13 KiB
WebAssembly Text Format

;; A counted SIMD accumulation loop in the exact shape ggml's dot
;; kernels lower to: a do-while over v128 loads with a countdown
;; counter and pointer bumps. Exercises the SIMD loop unrolling pass
;; end to end (guard blocks, remainder loop, exit-join phis) together
;; with bounds-check coalescing and window fusion over the unrolled
;; body.
(module
(memory (export "mem") 1)
;; seed8(addr, v): poke one byte, so tests can fill memory patterns.
(func (export "seed8") (param $a i32) (param $v i32)
(i32.store8 (local.get $a) (local.get $v)))
;; loopsum(p, n): sum the four i32 lanes of the i32x4-sum of n
;; consecutive 16-byte blocks at p.
(func (export "loopsum") (param $p i32) (param $n i32) (result i32)
(local $acc v128)
(local $i i32)
(local.set $i (local.get $n))
(block $exit
(br_if $exit (i32.eqz (local.get $n)))
(loop $l
(local.set $acc
(i32x4.add (local.get $acc) (v128.load (local.get $p))))
(local.set $p (i32.add (local.get $p) (i32.const 16)))
(local.set $i (i32.sub (local.get $i) (i32.const 1)))
(br_if $l (local.get $i))))
(i32.add
(i32.add (i32x4.extract_lane 0 (local.get $acc))
(i32x4.extract_lane 1 (local.get $acc)))
(i32.add (i32x4.extract_lane 2 (local.get $acc))
(i32x4.extract_lane 3 (local.get $acc)))))
;; loopsum64(p, n): loopsum with an INT64 countdown — proves the
;; wide-counter fused-loop form end to end (carried accumulator,
;; 64-bit counter parameter, arithmetic and exit result).
(func (export "loopsum64") (param $p i32) (param $n i64) (result i32)
(local $acc v128)
(local $i i64)
(local.set $i (local.get $n))
(block $exit
(br_if $exit (i64.eqz (local.get $n)))
(loop $l
(local.set $acc
(i32x4.add (local.get $acc) (v128.load (local.get $p))))
(local.set $p (i32.add (local.get $p) (i32.const 16)))
(local.set $i (i64.sub (local.get $i) (i64.const 1)))
(br_if $l (i64.ne (local.get $i) (i64.const 0)))))
(i32.add
(i32.add (i32x4.extract_lane 0 (local.get $acc))
(i32x4.extract_lane 1 (local.get $acc)))
(i32.add (i32x4.extract_lane 2 (local.get $acc))
(i32x4.extract_lane 3 (local.get $acc)))))
;; gemm4(a, b, n, stride): the f32 GEMM microkernel shape — four
;; carried accumulators fed by two shared loads and two shared
;; splats (a 2x2 outer-product step), an int64 countdown and a
;; runtime row stride. Mirrors ggml's fp32 matmul inner loop.
(func (export "gemm4") (param $a i32) (param $b i32) (param $n i64) (param $s i32) (result i32)
(local $acc00 v128)
(local $acc01 v128)
(local $acc10 v128)
(local $acc11 v128)
(local $x0 v128)
(local $x1 v128)
(local $s0 v128)
(local $s1 v128)
(local $i i64)
(local.set $i (local.get $n))
(block $exit
(br_if $exit (i64.eqz (local.get $n)))
(loop $l
(local.set $x0 (v128.load (local.get $b)))
(local.set $x1 (v128.load offset=16 (local.get $b)))
(local.set $s0 (v128.load32_splat (local.get $a)))
(local.set $s1 (v128.load32_splat offset=128 (local.get $a)))
(local.set $acc00 (f32x4.add (local.get $acc00) (f32x4.mul (local.get $x0) (local.get $s0))))
(local.set $acc01 (f32x4.add (local.get $acc01) (f32x4.mul (local.get $x0) (local.get $s1))))
(local.set $acc10 (f32x4.add (local.get $acc10) (f32x4.mul (local.get $x1) (local.get $s0))))
(local.set $acc11 (f32x4.add (local.get $acc11) (f32x4.mul (local.get $x1) (local.get $s1))))
(local.set $a (i32.add (local.get $a) (i32.const 4)))
(local.set $b (i32.add (local.get $b) (local.get $s)))
(local.set $i (i64.sub (local.get $i) (i64.const 1)))
(br_if $l (i64.ne (local.get $i) (i64.const 0)))))
(local.set $x0
(i32x4.add
(i32x4.add (local.get $acc00) (local.get $acc01))
(i32x4.add (local.get $acc10) (local.get $acc11))))
(i32.add
(i32.add (i32x4.extract_lane 0 (local.get $x0))
(i32x4.extract_lane 1 (local.get $x0)))
(i32.add (i32x4.extract_lane 2 (local.get $x0))
(i32x4.extract_lane 3 (local.get $x0)))))
;; quantnarrow(p, n): the quantize-kernel shape — float loads scaled,
;; rounded, saturating-truncated, then narrowed twice (i32x4→i16x8→
;; i8x16) and lane-summed. The narrowing second halves (sqxtn2-class)
;; write only their destination's high half and PRESERVE the low —
;; the destination-as-input shape that register renumbering must
;; refuse (a bug here once produced garbage generation).
(func (export "quantnarrow") (param $p i32) (param $n i32) (result i32)
(local $acc i32)
(local $i i32)
(local $v v128)
(local.set $i (local.get $n))
(block $exit
(br_if $exit (i32.eqz (local.get $n)))
(loop $l
(local.set $v
(i8x16.narrow_i16x8_s
(i16x8.narrow_i32x4_s
(i32x4.trunc_sat_f32x4_s
(f32x4.nearest
(f32x4.mul (v128.load (local.get $p))
(f32x4.splat (f32.const 0.5)))))
(i32x4.trunc_sat_f32x4_s
(f32x4.nearest
(f32x4.mul (v128.load offset=16 (local.get $p))
(f32x4.splat (f32.const 0.25))))))
(i16x8.narrow_i32x4_s
(i32x4.trunc_sat_f32x4_s
(f32x4.nearest
(f32x4.mul (v128.load offset=32 (local.get $p))
(f32x4.splat (f32.const 2.0)))))
(i32x4.trunc_sat_f32x4_s
(f32x4.nearest
(f32x4.mul (v128.load offset=48 (local.get $p))
(f32x4.splat (f32.const 4.0))))))))
(local.set $acc
(i32.add (local.get $acc)
(i32.add
(i32.add (i8x16.extract_lane_s 0 (local.get $v))
(i8x16.extract_lane_s 5 (local.get $v)))
(i32.add (i8x16.extract_lane_s 10 (local.get $v))
(i8x16.extract_lane_s 15 (local.get $v))))))
(local.set $p (i32.add (local.get $p) (i32.const 64)))
(local.set $i (i32.sub (local.get $i) (i32.const 1)))
(br_if $l (local.get $i))))
(local.get $acc))
;; scaledot(x, y, n): the quantized-kernel scale-lookup shape — each
;; 18-byte block starts with a u16 table index whose f32 entry
;; (table at 768) scales the block product. The u16 load, shift,
;; table-base add, f32 loads and f32 multiply form the scalar chain
;; the fused-region chase internalizes.
(func (export "scaledot") (param $x i32) (param $y i32) (param $n i32) (result i32)
(local $acc v128)
(local $i i32)
(local.set $i (local.get $n))
(block $exit
(br_if $exit (i32.eqz (local.get $n)))
(loop $l
(local.set $acc
(f32x4.add (local.get $acc)
(f32x4.mul
(f32x4.splat
(f32.mul
(f32.load (i32.add
(i32.shl (i32.load16_u (local.get $x)) (i32.const 2))
(i32.const 768)))
(f32.load (i32.add
(i32.shl (i32.load16_u (local.get $y)) (i32.const 2))
(i32.const 768)))))
(f32x4.mul
(f32x4.convert_i32x4_s (v128.load offset=2 (local.get $x)))
(f32x4.convert_i32x4_s (v128.load offset=2 (local.get $y)))))))
(local.set $x (i32.add (local.get $x) (i32.const 18)))
(local.set $y (i32.add (local.get $y) (i32.const 18)))
(local.set $i (i32.sub (local.get $i) (i32.const 1)))
(br_if $l (local.get $i))))
(i32.add
(i32.add
(i32x4.extract_lane 0 (i32x4.trunc_sat_f32x4_s (local.get $acc)))
(i32x4.extract_lane 1 (i32x4.trunc_sat_f32x4_s (local.get $acc))))
(i32.add
(i32x4.extract_lane 2 (i32x4.trunc_sat_f32x4_s (local.get $acc)))
(i32x4.extract_lane 3 (i32x4.trunc_sat_f32x4_s (local.get $acc))))))
;; axpy(x, y, n): the store-sink shape — per block, y is rewritten
;; as the f32 sum of the converted x and y blocks. Exercises stores
;; as fused-window sinks (memory-op ordering, the no-result region
;; form, and the checked store splice).
(func (export "axpy") (param $x i32) (param $y i32) (param $n i32)
(local $i i32)
(local.set $i (local.get $n))
(block $exit
(br_if $exit (i32.eqz (local.get $n)))
(loop $l
(v128.store (local.get $y)
(f32x4.add
(f32x4.convert_i32x4_s (v128.load (local.get $y)))
(f32x4.convert_i32x4_s (v128.load (local.get $x)))))
(local.set $x (i32.add (local.get $x) (i32.const 16)))
(local.set $y (i32.add (local.get $y) (i32.const 16)))
(local.set $i (i32.sub (local.get $i) (i32.const 1)))
(br_if $l (local.get $i)))))
;; strideaxpy(x, y, n, stride): axpy whose pointer advance is a
;; RUNTIME stride — the loop-fusion bump must ride as a delta
;; parameter instead of an immediate.
(func (export "strideaxpy") (param $x i32) (param $y i32) (param $n i32) (param $s i32)
(local $i i32)
(local.set $i (local.get $n))
(block $exit
(br_if $exit (i32.eqz (local.get $n)))
(loop $l
(v128.store (local.get $y)
(f32x4.add
(f32x4.convert_i32x4_s (v128.load (local.get $y)))
(f32x4.convert_i32x4_s (v128.load (local.get $x)))))
(local.set $x (i32.add (local.get $x) (local.get $s)))
(local.set $y (i32.add (local.get $y) (local.get $s)))
(local.set $i (i32.sub (local.get $i) (i32.const 1)))
(br_if $l (local.get $i)))))
;; axpy64(x, y, n): axpy driven by an INT64 countdown — the fused
;; loop's counter parameter, arithmetic and exit result must all be
;; 64-bit while the pointers stay i32.
(func (export "axpy64") (param $x i32) (param $y i32) (param $n i64)
(local $i i64)
(local.set $i (local.get $n))
(block $exit
(br_if $exit (i64.eqz (local.get $n)))
(loop $l
(v128.store (local.get $y)
(f32x4.add
(f32x4.convert_i32x4_s (v128.load (local.get $y)))
(f32x4.convert_i32x4_s (v128.load (local.get $x)))))
(local.set $x (i32.add (local.get $x) (i32.const 16)))
(local.set $y (i32.add (local.get $y) (i32.const 16)))
(local.set $i (i64.sub (local.get $i) (i64.const 1)))
(br_if $l (i64.ne (local.get $i) (i64.const 0))))))
;; gathersum(p, n): the lane-insert gather shape — a vector built
;; from four strided 32-bit loads (zero + three lane inserts) plus a
;; widening 16x4 load, accumulated as i32x4. Exercises the fused
;; lane-load vocabulary.
(func (export "gathersum") (param $p i32) (param $n i32) (result i32)
(local $acc v128)
(local $i i32)
(local $v v128)
(local.set $i (local.get $n))
(block $exit
(br_if $exit (i32.eqz (local.get $n)))
(loop $l
(local.set $v (v128.load32_zero (local.get $p)))
(local.set $v (v128.load32_lane offset=20 1 (local.get $p) (local.get $v)))
(local.set $v (v128.load32_lane offset=40 2 (local.get $p) (local.get $v)))
(local.set $v (v128.load32_lane offset=60 3 (local.get $p) (local.get $v)))
(local.set $acc (i32x4.add (local.get $acc) (local.get $v)))
(local.set $acc (i32x4.add (local.get $acc)
(v128.load16x4_u offset=8 (local.get $p))))
(local.set $p (i32.add (local.get $p) (i32.const 24)))
(local.set $i (i32.sub (local.get $i) (i32.const 1)))
(br_if $l (local.get $i))))
(i32.add
(i32.add (i32x4.extract_lane 0 (local.get $acc))
(i32x4.extract_lane 1 (local.get $acc)))
(i32.add (i32x4.extract_lane 2 (local.get $acc))
(i32x4.extract_lane 3 (local.get $acc)))))
;; dot2(x, y, n): two interleaved streams with widening multiplies —
;; the q8-style shape that the multi-root window fusion targets.
(func (export "dot2") (param $x i32) (param $y i32) (param $n i32) (result i32)
(local $acc v128)
(local $i i32)
(local.set $i (local.get $n))
(block $exit
(br_if $exit (i32.eqz (local.get $n)))
(loop $l
(local.set $acc
(i32x4.add
(local.get $acc)
(i32x4.add
(i32x4.dot_i16x8_s
(i16x8.extend_low_i8x16_s (v128.load (local.get $x)))
(i16x8.extend_low_i8x16_s (v128.load (local.get $y))))
(i32x4.dot_i16x8_s
(i16x8.extend_high_i8x16_s (v128.load (local.get $x)))
(i16x8.extend_high_i8x16_s (v128.load (local.get $y)))))))
(local.set $x (i32.add (local.get $x) (i32.const 16)))
(local.set $y (i32.add (local.get $y) (i32.const 16)))
(local.set $i (i32.sub (local.get $i) (i32.const 1)))
(br_if $l (local.get $i))))
(i32.add
(i32.add (i32x4.extract_lane 0 (local.get $acc))
(i32x4.extract_lane 1 (local.get $acc)))
(i32.add (i32x4.extract_lane 2 (local.get $acc))
(i32x4.extract_lane 3 (local.get $acc))))))