mirror of
https://github.com/hanzoai/wasm2go.git
synced 2026-10-30 15:06:48 +00:00
* 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]>
340 lines
9.4 KiB
Go
340 lines
9.4 KiB
Go
package gcasm
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// f16TestSite is the canonical gc-compiled shape of the software
|
|
// fp32→fp16 idiom (register-allocated like the common llama sites:
|
|
// input R0, shl1 R9, result R3, sign/arm temp R4, scratch R10/R11,
|
|
// floats F1/F2). Branch targets are patched to instruction offsets
|
|
// by f16TestInsns.
|
|
var f16TestSite = []string{
|
|
"ADD\tR0, R0, R9",
|
|
"AND\t$2147483647, R0, R10",
|
|
"FMOVS\tR10, F1",
|
|
"FMOVS\t$f32.77800000(SB), F2",
|
|
"FMULS\tF2, F1, F1",
|
|
"FMOVS\t$f32.08800000(SB), F2",
|
|
"FMULS\tF2, F1, F1",
|
|
"NOP",
|
|
"HINT\t$0",
|
|
"MOVD\t$1895825408, R10",
|
|
"CMPW\tR10, R9",
|
|
"MOVD\t$1895825408, R10",
|
|
"CSEL\tLS, R10, R9, R10",
|
|
"UBFX\t$1, R10, $31, R10",
|
|
"AND\t$2139095040, R10, R10",
|
|
"MOVD\t$125829120, R11",
|
|
"ADD\tR11, R10, R10",
|
|
"FMOVS\tR10, F2",
|
|
"FADDS\tF1, F2, F1",
|
|
"MOVD\t$16777216, R10",
|
|
"CMNW\tR10, R9",
|
|
"BLS\t%A",
|
|
"MOVD\t$32256, R3",
|
|
"JMP\t%B",
|
|
"FMOVS\tF1, R3", // %A
|
|
"UBFX\t$13, R3, $19, R4",
|
|
"AND\t$31744, R4, R4",
|
|
"AND\t$4095, R3, R3",
|
|
"ADD\tR4, R3, R3",
|
|
"UBFX\t$16, R0, $16, R4", // %B
|
|
"AND\t$32768, R4, R4",
|
|
"ORR\tR4, R3, R3",
|
|
}
|
|
|
|
// f16TestInsns lays out pre + site + post with 4-byte offsets and
|
|
// patches the %A/%B branch targets to the right offsets.
|
|
func f16TestInsns(pre, post []string) []Insn {
|
|
var lines []string
|
|
lines = append(lines, pre...)
|
|
siteStart := len(lines)
|
|
lines = append(lines, f16TestSite...)
|
|
lines = append(lines, post...)
|
|
offA := 4 * (siteStart + 24) // FMOVS F1, R3
|
|
offB := 4 * (siteStart + 29) // UBFX $16, R0, $16, R4
|
|
var insns []Insn
|
|
for i, l := range lines {
|
|
l = strings.ReplaceAll(l, "%A", itoa(offA))
|
|
l = strings.ReplaceAll(l, "%B", itoa(offB))
|
|
insns = append(insns, Insn{Off: 4 * i, Text: l})
|
|
}
|
|
return insns
|
|
}
|
|
|
|
func itoa(n int) string {
|
|
if n == 0 {
|
|
return "0"
|
|
}
|
|
var b []byte
|
|
for n > 0 {
|
|
b = append([]byte{byte('0' + n%10)}, b...)
|
|
n /= 10
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func f16NoResolve(string) ([]ArgKind, bool, ArgKind, string, bool) {
|
|
return nil, false, 0, "", false
|
|
}
|
|
|
|
func TestF16PeepholeRewritesCanonicalSite(t *testing.T) {
|
|
insns := f16TestInsns(
|
|
[]string{"MOVW\t(R20)(R25), R0"},
|
|
[]string{
|
|
// Redefine the dyn scratch so the deadness walk passes.
|
|
"FMOVS\tR5, F1",
|
|
"FMOVS\tR5, F2",
|
|
"MOVH\tR3, (R5)(R6)", // the store consuming the result
|
|
"RET",
|
|
})
|
|
out := a64F16Peephole(insns, f16NoResolve, "")
|
|
text := ""
|
|
for _, in := range out {
|
|
text += in.Text + "\n"
|
|
}
|
|
if !strings.Contains(text, "FCVTSH\tF1, F1") {
|
|
t.Fatalf("no native convert emitted:\n%s", text)
|
|
}
|
|
for _, gone := range []string{"FMULS", "FADDS", "BLS", "$f32.77800000"} {
|
|
if strings.Contains(text, gone) {
|
|
t.Errorf("deleted idiom instruction %q still present:\n%s", gone, text)
|
|
}
|
|
}
|
|
// Ledger finals: the shl1, sign, and constant registers must end
|
|
// with their original values; the store must still read R3.
|
|
for _, want := range []string{
|
|
"ADD\tR0, R0, R9",
|
|
"UBFX\t$16, R0, $16, R4",
|
|
"AND\t$32768, R4, R4",
|
|
"MOVD\t$16777216, R10",
|
|
"MOVD\t$125829120, R11",
|
|
"CSEL\tVS, ",
|
|
"FCMPS\tF1, F1",
|
|
"MOVH\tR3, (R5)(R6)",
|
|
} {
|
|
if !strings.Contains(text, want) {
|
|
t.Errorf("missing %q in rewrite:\n%s", want, text)
|
|
}
|
|
}
|
|
if len(out) >= len(insns) {
|
|
t.Errorf("rewrite did not shrink the site: %d -> %d insns", len(insns), len(out))
|
|
}
|
|
}
|
|
|
|
func TestF16PeepholeKeepsSiteWhenScratchLive(t *testing.T) {
|
|
insns := f16TestInsns(nil, []string{
|
|
"FADDS\tF1, F0, F0", // reads the float scratch: not dead
|
|
"RET",
|
|
})
|
|
out := a64F16Peephole(insns, f16NoResolve, "")
|
|
for _, in := range out {
|
|
if strings.Contains(in.Text, "FCVTSH") {
|
|
t.Fatal("site rewritten although the float scratch is live afterwards")
|
|
}
|
|
}
|
|
if len(out) != len(insns) {
|
|
t.Fatalf("instruction count changed on a rejected site: %d -> %d", len(insns), len(out))
|
|
}
|
|
}
|
|
|
|
func TestF16PeepholeKeepsSiteOnInboundBranch(t *testing.T) {
|
|
// A branch from outside targeting the region interior must veto
|
|
// the rewrite (the interior instructions disappear).
|
|
interior := 4 * (1 + 9) // the first MOVD $1895825408 inside the site
|
|
insns := f16TestInsns(
|
|
[]string{"CBZW\tR7, " + itoa(interior)},
|
|
[]string{"FMOVS\tR5, F1", "FMOVS\tR5, F2", "RET"})
|
|
out := a64F16Peephole(insns, f16NoResolve, "")
|
|
for _, in := range out {
|
|
if strings.Contains(in.Text, "FCVTSH") {
|
|
t.Fatal("site rewritten despite an inbound branch into the region")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestF16PeepholeMergeIntoSignRegister(t *testing.T) {
|
|
// gc sometimes lands the sign merge in the SIGN register
|
|
// (ORR x, r, x) and keeps the nonsign register's value live-out;
|
|
// the rewrite must then reproduce the nonsign final too.
|
|
site := make([]string, len(f16TestSite))
|
|
copy(site, f16TestSite)
|
|
site[len(site)-1] = "ORR\tR4, R3, R4"
|
|
saved := f16TestSite
|
|
defer func() { copy(f16TestSite, saved) }()
|
|
copy(f16TestSite, site)
|
|
insns := f16TestInsns(nil, []string{
|
|
"FMOVS\tR5, F1",
|
|
"FMOVS\tR5, F2",
|
|
"MOVH\tR4, (R5)(R6)",
|
|
"RET",
|
|
})
|
|
out := a64F16Peephole(insns, f16NoResolve, "")
|
|
text := ""
|
|
for _, in := range out {
|
|
text += in.Text + "\n"
|
|
}
|
|
if !strings.Contains(text, "FCVTSH") {
|
|
t.Fatalf("merge-into-sign form not rewritten:\n%s", text)
|
|
}
|
|
if !strings.Contains(text, "AND\t$32767, R4, R3") {
|
|
t.Errorf("nonsign register final not reproduced:\n%s", text)
|
|
}
|
|
}
|
|
|
|
// f16TestInsnsAt assembles pre + site + post like f16TestInsns but
|
|
// accepts an already-customized site (e.g. with interleaved
|
|
// instructions inserted); the %A/%B targets are located by content.
|
|
func f16TestInsnsAt(pre, site, post []string) []Insn {
|
|
var lines []string
|
|
lines = append(lines, pre...)
|
|
lines = append(lines, site...)
|
|
lines = append(lines, post...)
|
|
offA, offB := -1, -1
|
|
for i, l := range lines {
|
|
if l == "FMOVS\tF1, R3" {
|
|
offA = 4 * i
|
|
}
|
|
if l == "UBFX\t$16, R0, $16, R4" {
|
|
offB = 4 * i
|
|
}
|
|
}
|
|
var insns []Insn
|
|
for i, l := range lines {
|
|
l = strings.ReplaceAll(l, "%A", itoa(offA))
|
|
l = strings.ReplaceAll(l, "%B", itoa(offB))
|
|
insns = append(insns, Insn{Off: 4 * i, Text: l})
|
|
}
|
|
return insns
|
|
}
|
|
|
|
func TestF16PeepholeSkipsInterleavedInstructions(t *testing.T) {
|
|
// gc may schedule unrelated arithmetic between idiom steps; the
|
|
// matcher must retry over skippable instructions that touch no
|
|
// bound register and keep them in the output.
|
|
site := make([]string, 0, len(f16TestSite)+2)
|
|
for _, l := range f16TestSite {
|
|
site = append(site, l)
|
|
if l == "FMULS\tF2, F1, F1" {
|
|
site = append(site, "MOVWU\tR8, R12")
|
|
}
|
|
if l == "AND\t$2139095040, R10, R10" {
|
|
site = append(site, "ADD\t$4, R13, R13")
|
|
}
|
|
}
|
|
insns := f16TestInsnsAt(
|
|
[]string{"MOVW\t(R20)(R25), R0"},
|
|
site,
|
|
[]string{
|
|
"FMOVS\tR5, F1",
|
|
"FMOVS\tR5, F2",
|
|
"MOVH\tR3, (R5)(R6)",
|
|
"RET",
|
|
})
|
|
out := a64F16Peephole(insns, f16NoResolve, "")
|
|
text := ""
|
|
for _, in := range out {
|
|
text += in.Text + "\n"
|
|
}
|
|
if !strings.Contains(text, "FCVTSH\tF1, F1") {
|
|
t.Fatalf("site with interleave not rewritten:\n%s", text)
|
|
}
|
|
for _, kept := range []string{"MOVWU\tR8, R12", "ADD\t$4, R13, R13"} {
|
|
if !strings.Contains(text, kept) {
|
|
t.Errorf("interleaved instruction %q dropped:\n%s", kept, text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestF16PeepholeKeepsSiteWhenInterleaveTouchesBoundReg(t *testing.T) {
|
|
// An interleaved instruction WRITING a bound register (the shl1
|
|
// register R9) must veto the rewrite.
|
|
site := make([]string, 0, len(f16TestSite)+1)
|
|
for _, l := range f16TestSite {
|
|
site = append(site, l)
|
|
if l == "AND\t$2139095040, R10, R10" {
|
|
site = append(site, "ADDW\t$1, R9, R9")
|
|
}
|
|
}
|
|
insns := f16TestInsnsAt(nil, site, []string{
|
|
"FMOVS\tR5, F1",
|
|
"FMOVS\tR5, F2",
|
|
"RET",
|
|
})
|
|
out := a64F16Peephole(insns, f16NoResolve, "")
|
|
for _, in := range out {
|
|
if strings.Contains(in.Text, "FCVTSH") {
|
|
t.Fatal("site rewritten although interleave clobbers a bound register")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestF16PeepholeDeadnessAcrossBranches(t *testing.T) {
|
|
// The scratch-deadness walk follows both arms of a forward
|
|
// branch: both arms redefine the float scratches, so the site is
|
|
// rewritable.
|
|
nSite := len(f16TestSite) + 1 // pre line + site
|
|
armA := 4 * (nSite + 4) // first arm-A instruction
|
|
join := 4 * (nSite + 6) // join block
|
|
insns := f16TestInsnsAt(
|
|
[]string{"MOVW\t(R20)(R25), R0"},
|
|
f16TestSite,
|
|
[]string{
|
|
"CBZW\tR7, " + itoa(armA),
|
|
"FMOVS\tR5, F1",
|
|
"FMOVS\tR5, F2",
|
|
"JMP\t" + itoa(join),
|
|
"FMOVS\tR6, F1", // armA
|
|
"FMOVS\tR6, F2",
|
|
"MOVH\tR3, (R5)(R6)", // join
|
|
"RET",
|
|
})
|
|
out := a64F16Peephole(insns, f16NoResolve, "")
|
|
text := ""
|
|
for _, in := range out {
|
|
text += in.Text + "\n"
|
|
}
|
|
if !strings.Contains(text, "FCVTSH\tF1, F1") {
|
|
t.Fatalf("branchy-but-dead scratch not rewritten:\n%s", text)
|
|
}
|
|
}
|
|
|
|
func TestF16PeepholeDeadnessBranchArmReads(t *testing.T) {
|
|
// One arm reads the float scratch before redefining it: not dead,
|
|
// no rewrite.
|
|
nSite := len(f16TestSite) + 1
|
|
armA := 4 * (nSite + 4)
|
|
insns := f16TestInsnsAt(
|
|
[]string{"MOVW\t(R20)(R25), R0"},
|
|
f16TestSite,
|
|
[]string{
|
|
"CBZW\tR7, " + itoa(armA),
|
|
"FMOVS\tR5, F1",
|
|
"FMOVS\tR5, F2",
|
|
"RET",
|
|
"FADDS\tF1, F0, F0", // armA: reads F1
|
|
"RET",
|
|
})
|
|
out := a64F16Peephole(insns, f16NoResolve, "")
|
|
for _, in := range out {
|
|
if strings.Contains(in.Text, "FCVTSH") {
|
|
t.Fatal("site rewritten although one branch arm reads the scratch")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestF16Skippable(t *testing.T) {
|
|
for _, yes := range []string{"MOVD\tR1, R2", "ADDW\t$1, R3, R3", "UBFX\t$1, R2, $3, R2", "SXTW\tR2, R2"} {
|
|
if !f16Skippable(yes) {
|
|
t.Errorf("%q should be skippable", yes)
|
|
}
|
|
}
|
|
for _, no := range []string{"FADDS\tF1, F2, F1", "CBZW\tR1, 8", "RET", "NOP"} {
|
|
if f16Skippable(no) {
|
|
t.Errorf("%q should not be skippable", no)
|
|
}
|
|
}
|
|
}
|