mirror of
https://github.com/hanzoai/wasm2go.git
synced 2026-10-30 15:06:48 +00:00
* gcasm: keep the x64 const-shuffle emulation inside the scratch set The fused-tree emission of i8x16_shuffle_const staged its biased PSHUFB patterns in X4/X5, outside the splicer's X0-X3 scratch set. Pool values live in X4+ for the whole loop body and the window path's relocate-out-of-reach scan does not run there, so any value the allocator parked in those registers was corrupted — on the repack gemv loop the activation-scale splat died and every output came out garbage on amd64 builds with the splices enabled (GOAMD64=v2+; v1 uses the pure fallback and CI only ran v1-shaped suites for this path until the numeric kernel tests landed). Reload the pattern from the constant pool instead of holding a second biased copy and apply the bias constants as memory operands: three registers suffice and the emulation stays inside X0-X2. The regression test emits a loop whose splat must survive a const-shuffle and rejects any X4+ mention in the emulation body. Verified end to end under GOAMD64=v2: the repack numeric kernel tests and the full engine suite pass against a bundle transpiled with this fix; arm64 output is byte-identical. Co-Authored-By: Claude Fable 5 <[email protected]> * ci: execute the amd64 splices under GOAMD64=v2 The default job builds at v1, whose binaries keep the pure fallback — the spliced amd64 code paths were never executed by any CI until a consumer suite tripped over them. Run the execution-heavy suites with GOAMD64=v2 so the splices run on the runner AVX2 hardware. Co-Authored-By: Claude Fable 5 <[email protected]> * ci: scope the GOAMD64=v2 job to the full-backend suites codegen.Translate output carries its pure bodies under the asm-dormancy tag with no asm bundle by design, so internal/codegen runtime tests cannot build at v2; the spliced execution surface is the transpile backend and gcasm. Co-Authored-By: Claude Fable 5 <[email protected]> * ci: execute the arm64 splices on native hardware The flagship arm64 backend (SDOT/SMMLA splices, the fused loops) had the same execution blind spot the amd64-v2 job just closed: nothing in this repo ever RAN it. Run the full-backend suites on a native arm64 runner, mirroring the v2 job scope. Co-Authored-By: Claude Fable 5 <[email protected]> * gcasm: skip the amd64-fixture harnesses on other hosts The direct-asm gate/jump-table/perf fixtures generate amd64-only assembly and build+run it on the host, so they can only execute on amd64; on the new native arm64 CI runner the generated package has no bodies (the pure fallback is tagged out) and vet fails with undefined symbols. Declare the host requirement with an explicit skip. The arm64 job still executes the full transpile backend and the arch-neutral gcasm suites natively. Co-Authored-By: Claude Fable 5 <[email protected]> --------- Co-authored-by: Claude Fable 5 <[email protected]>
136 lines
4.5 KiB
Go
136 lines
4.5 KiB
Go
package gcasm
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// flagDispatchSrc builds a dense switch whose selector value ALSO feeds
|
|
// a comparison right before the dispatch, and whose case bodies branch
|
|
// on the flags that comparison sets. gc keeps that compare's flags live
|
|
// ACROSS its (flag-preserving) jump-table dispatch and lets the case
|
|
// bodies consume them. The transform's compare tree clobbers flags, so
|
|
// each leaf must REPLAY the pre-dispatch compare — this fixture pins
|
|
// that: an unreplayed (or mis-detected) flag leak makes some case pick
|
|
// the wrong branch and the run-comparison below fails.
|
|
//
|
|
// Shape: `if sel < 40 { switch sel { case i: <use flags of (sel<40)> } }`
|
|
// — gc commonly emits the `CMP sel,$40 / Jcc` and reuses those flags
|
|
// inside the arms.
|
|
func flagDispatchSrc(fnName string) string {
|
|
var b strings.Builder
|
|
fmt.Fprintf(&b, "func %s(sel int32, v0 int32) int32 {\n", fnName)
|
|
b.WriteString("\tc := sel < 40\n\tswitch sel {\n")
|
|
for i := 0; i < 48; i++ {
|
|
fmt.Fprintf(&b, "\tcase %d:\n\t\tif c {\n\t\t\tv0 = v0*%d + %d\n\t\t} else {\n\t\t\tv0 = v0 - %d\n\t\t}\n\t\tgoto done\n", i, i+3, i, i)
|
|
}
|
|
b.WriteString("\tdefault:\n\t\tv0 = -1\n\t}\ndone:\n\treturn v0\n}\n")
|
|
return b.String()
|
|
}
|
|
|
|
// TestJumpTableFlagReplayRun pins the jump-table flag-replay correctness
|
|
// fix: transform a dispatch whose targets consume the pre-dispatch
|
|
// compare's flags, assemble, and check every selector against the pure
|
|
// reference. Before the fix, the flag-consumption detection had
|
|
// false-negative holes and some leaves got no replay, so the tree's
|
|
// leftover flags leaked into the arms and picked the wrong branch.
|
|
func TestJumpTableFlagReplayRun(t *testing.T) {
|
|
if runtime.GOARCH != "amd64" {
|
|
t.Skip("fixture emits amd64-only asm; the harness builds and runs it on the host")
|
|
}
|
|
dir := t.TempDir()
|
|
src := "package lib\n\n//go:noinline\n" + flagDispatchSrc("FDispatch")
|
|
for name, content := range map[string]string{
|
|
"go.mod": "module fjtgate\n\ngo 1.25.0\n",
|
|
"lib/lib.go": src,
|
|
} {
|
|
p := filepath.Join(dir, name)
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
fns, datas, err := Capture(dir, "fjtgate/lib")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var disp *Fn
|
|
for _, f := range fns {
|
|
if strings.HasSuffix(f.Name, ".FDispatch") {
|
|
disp = f
|
|
}
|
|
}
|
|
if disp == nil {
|
|
t.Fatalf("FDispatch not captured; got %d fns", len(fns))
|
|
}
|
|
dm := map[string]*DataSym{}
|
|
for _, d := range datas {
|
|
dm[d.Name] = d
|
|
}
|
|
jt := &JTTable{}
|
|
body, err := Transform(disp, TransformOptions{
|
|
SymName: "fdispatchAsm",
|
|
CalleeSig: func(string) ([]ArgKind, bool, ArgKind, string, bool) { return nil, false, 0, "", false },
|
|
Params: []ArgKind{ArgI32, ArgI32},
|
|
HasResult: true,
|
|
Result: ArgI32,
|
|
ArgNames: []string{"sel", "v0"},
|
|
Datas: dm,
|
|
JT: jt,
|
|
})
|
|
if err != nil {
|
|
// If gc chose NOT to emit a jump table (compiler-version
|
|
// dependent), there is nothing to pin here — skip rather than
|
|
// fail, matching the existing gate's tolerance.
|
|
if strings.Contains(err.Error(), "jump") {
|
|
t.Skipf("no jump table emitted for this fixture on this toolchain: %v", err)
|
|
}
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(body, "_jt") || strings.Contains(body, ".jump") {
|
|
t.Skipf("gc did not emit a jump table for FDispatch on this toolchain")
|
|
}
|
|
body += jt.EmitAsm("amd64")
|
|
|
|
run := t.TempDir()
|
|
files := map[string]string{
|
|
"go.mod": "module fjtrun\n\ngo 1.25.0\n",
|
|
"decl.go": "package fjtrun\n\nimport \"unsafe\"\n\nvar _ unsafe.Pointer\n\nfunc fdispatchAsm(sel int32, v0 int32) (r0 int32)\n\n//go:noinline\n" +
|
|
flagDispatchSrc("fdispatchRef") + "\n" + jt.EmitGo("amd64"),
|
|
"body_amd64.s": "#include \"textflag.h\"\n#include \"funcdata.h\"\n\n" + body,
|
|
"run_test.go": `package fjtrun
|
|
|
|
import "testing"
|
|
|
|
func TestFDispatch(t *testing.T) {
|
|
for sel := int32(-5); sel <= 55; sel++ {
|
|
for _, v0 := range []int32{0, 1, -7, 1 << 20} {
|
|
if got, want := fdispatchAsm(sel, v0), fdispatchRef(sel, v0); got != want {
|
|
t.Fatalf("fdispatch(%d,%d)=%d want %d", sel, v0, got, want)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
`,
|
|
}
|
|
for name, content := range files {
|
|
if err := os.WriteFile(filepath.Join(run, name), []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
cmd := exec.Command("go", "test", "-run", "TestFDispatch", ".")
|
|
cmd.Dir = run
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("jt flag-replay gate run failed: %v\n%s\n--- transformed body ---\n%s", err, out, body)
|
|
}
|
|
}
|