mirror of
https://github.com/hanzoai/wasm2go.git
synced 2026-10-30 15:06:48 +00:00
* codegen: keep fused-tree operands ahead of their consumers after the f16 gather rewrite rewriteF16GatherGroup appends the collapsed v128_load16x4_u at the end of the node list and points the chain's tail (now f16x4_cvt) at it, a forward reference. The asm splicers walk the tree recursively and never noticed, but the pure twin body emits nodes in list order, so any tree that reached it through this rewrite declared the load after its use: "undefined: n6 / declared and not used: n6" from the gcasm capture build. It surfaced once ggml's wasm f16 vector loads started producing the gather idiom everywhere (vec_dot_f16, vec_mad_f16, flash attention) instead of only in the repack kernels. compactDeadNodes, which every rewrite already runs, now places a node's operands before the node while otherwise keeping list order — the collapsed load lands where the chain's last lane load used to be. * helpers: detect FEAT_I8MM on linux/arm64 (HWCAP2 bit 13) The kernel-override dispatch and the SMMLA splices gate on it; darwin already reported it through hw.optional.arm.FEAT_I8MM. Co-Authored-By: Claude Fable 5.1 <[email protected]> * Replace the engine-specific body retargets with project-supplied kernel overrides wasm2go's job is to read a wasm module and emit Go and, where it can do better than gc, assembly derived from that wasm. Two features had grown past that line: the repack GEMM retarget swapped the body of an export named dbg_gemm_q8_0_4x4 for a hand-written kernel that knows one engine's quantized block layout, and the vec_dot row/column pairing recognized one engine's trait-table layout and handed the paired companion to another hand-written kernel of the same kind. Neither body came from the wasm, and both tied the transpiler to that engine. On the current engine build the pairing also no longer measures (prompt and decode throughput within noise with it off), because the repack path carries the hot matmuls. Both are removed: the retarget and its -no-repack-gemm switch, the pairing and its -vec-dot-pair-entry / -vec-dot-rows switches, the kernels and the trait-table recognizer. The passive-segment placement scan the f16-table detection shares moves to its own file. In their place, a generic mechanism: -kernel-overrides <manifest>. The project that produced the wasm may supply the assembly body of an exported leaf function, per architecture and CPU feature level. wasm2go validates the manifest against the module (export present, signature and pointer width as declared), wraps each body in a fixed ABI — the linear-memory base and size in named registers, arguments by the ABI0 layout rule, a kov_oob label that reaches the module's out-of-bounds trap — emits a feature-dispatch stub (i8mm > dotprod > neon; avx512vnni > avx2 > sse4) with the lowered body as the portable fallback, and pins the exports out of line. It never inspects what a body computes; the body must stay inside the wasm execution model (linear memory through the given base and size, bounds-checked, no calls), and the manifest loader rejects TEXT, calls, system instructions and foreign symbol references. docs/kernel-overrides.md states the contract and the verification the project owes. Tests: manifest validation, ABI text and dispatch shape, and an end-to-end transpile of a module with and without an override body on the host architecture — same sums, and the override traps past the end of memory. Co-Authored-By: Claude Fable 5.1 <[email protected]> * helpers: detect FEAT_I8MM on darwin/arm64 too The linux and fallback twins gained CPUI8MM; a darwin build of a bundle whose kernel overrides declare an i8mm body needs the same symbol in the base package. Co-Authored-By: Claude Fable 5.1 <[email protected]> * Rename kernel overrides to assembly overrides "Kernel" is numeric-computing vocabulary; the mechanism itself knows nothing about what the function computes — it replaces the body of an exported function with project-supplied assembly. The name now says that: -asm-overrides / Options.AsmOverrides, docs/asm-overrides.md, the manifest's "functions" array, the ovr_oob label and ovr_ data prefix, and Fn<idx>ovr<feature> body symbols. Co-Authored-By: Claude Fable 5.1 <[email protected]> --------- Co-authored-by: Claude Fable 5.1 <[email protected]>
166 lines
6.1 KiB
Go
166 lines
6.1 KiB
Go
package codegen
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/goccy/wasm2go/internal/simdfuse"
|
|
"github.com/goccy/wasm2go/internal/ssa"
|
|
)
|
|
|
|
func mustExpr(t *testing.T, src string) ast.Expr {
|
|
t.Helper()
|
|
e, err := parser.ParseExpr(src)
|
|
if err != nil {
|
|
t.Fatalf("parse %q: %v", src, err)
|
|
}
|
|
return unparen(e)
|
|
}
|
|
|
|
// unparen removes ParenExpr nodes the parser introduces for grouping;
|
|
// the emitter's ASTs never contain them (precedence is structural).
|
|
func unparen(e ast.Expr) ast.Expr {
|
|
switch x := e.(type) {
|
|
case *ast.ParenExpr:
|
|
return unparen(x.X)
|
|
case *ast.BinaryExpr:
|
|
x.X, x.Y = unparen(x.X), unparen(x.Y)
|
|
case *ast.CallExpr:
|
|
for i := range x.Args {
|
|
x.Args[i] = unparen(x.Args[i])
|
|
}
|
|
case *ast.StarExpr:
|
|
x.X = unparen(x.X)
|
|
case *ast.UnaryExpr:
|
|
x.X = unparen(x.X)
|
|
}
|
|
return e
|
|
}
|
|
|
|
func TestF16WordLaneMatch(t *testing.T) {
|
|
fb := &fusedTreeBuilder{sc: &simdScalarizer{constBind: map[string]int32{}}}
|
|
|
|
// Lane 0: (w & 0xffff) << 2, in the emitter's masked-shift spelling.
|
|
name, tadd, ok := fb.f16WordLane(mustExpr(t, "int64(v9&65535<<(uint(2)%64))"), 0)
|
|
if !ok || name != "v9" || tadd != 0 {
|
|
t.Fatalf("lane0: got name=%q tadd=%d ok=%v", name, tadd, ok)
|
|
}
|
|
|
|
// Lanes 1..3: ((w >>u (16k-2)) & 0x3fffc) + table, with the
|
|
// unsigned-view helper around the word.
|
|
for lane, shift := range map[int]int{1: 14, 2: 30, 3: 46} {
|
|
src := "int64(int64(base.Ui64(v9)>>(uint(" + strconv.Itoa(shift) + ")%64))&262140 + 8798416)"
|
|
name, tadd, ok := fb.f16WordLane(mustExpr(t, src), lane)
|
|
if !ok || name != "v9" || tadd != 8798416 {
|
|
t.Fatalf("lane%d: got name=%q tadd=%d ok=%v", lane, name, tadd, ok)
|
|
}
|
|
}
|
|
|
|
// Wrong shift for the lane must not match.
|
|
if _, _, ok := fb.f16WordLane(mustExpr(t, "int64(int64(base.Ui64(v9)>>(uint(30)%64))&262140+8798416)"), 1); ok {
|
|
t.Fatal("lane1 with lane2's shift matched")
|
|
}
|
|
// A mask that does not select exactly halfword*4 must not match.
|
|
if _, _, ok := fb.f16WordLane(mustExpr(t, "int64(int64(base.Ui64(v9)>>(uint(14)%64))&262136+8798416)"), 1); ok {
|
|
t.Fatal("wrong mask matched")
|
|
}
|
|
// Lane 0 with a short mask must not match.
|
|
if _, _, ok := fb.f16WordLane(mustExpr(t, "int64(v9&32767<<(uint(2)%64))"), 0); ok {
|
|
t.Fatal("short lane0 mask matched")
|
|
}
|
|
// Lane 0 with a wrong shift must not match.
|
|
if _, _, ok := fb.f16WordLane(mustExpr(t, "int64(v9&65535<<(uint(3)%64))"), 0); ok {
|
|
t.Fatal("lane0 shl3 matched")
|
|
}
|
|
}
|
|
|
|
func TestArgOffsetByAddPeel(t *testing.T) {
|
|
// Node 0: an opaque load; node 1: node0 + 4; node 2: node0 + 6.
|
|
fb := &fusedTreeBuilder{}
|
|
fb.nodes = []simdfuse.Node{
|
|
{Op: "scalar_i32_load16_u", Args: []simdfuse.Arg{{Kind: simdfuse.ArgScalar, Index: 0}}},
|
|
{Op: "scalar_i32_add", Args: []simdfuse.Arg{
|
|
{Kind: simdfuse.ArgNode, Index: 0}, {Kind: simdfuse.ArgConst, Const: 4}}},
|
|
{Op: "scalar_i32_add", Args: []simdfuse.Arg{
|
|
{Kind: simdfuse.ArgNode, Index: 0}, {Kind: simdfuse.ArgConst, Const: 6}}},
|
|
}
|
|
bare := simdfuse.Arg{Kind: simdfuse.ArgNode, Index: 0}
|
|
plus4 := simdfuse.Arg{Kind: simdfuse.ArgNode, Index: 1}
|
|
plus6 := simdfuse.Arg{Kind: simdfuse.ArgNode, Index: 2}
|
|
|
|
// Bare base vs peeled add: node/node with differing ops.
|
|
if !fb.argOffsetBy(bare, plus4, 4) {
|
|
t.Fatal("bare vs base+4 with delta 4 not proven")
|
|
}
|
|
if fb.argOffsetBy(bare, plus4, 2) {
|
|
t.Fatal("bare vs base+4 with delta 2 wrongly proven")
|
|
}
|
|
// Two peeled adds against each other: both sides peel.
|
|
if !fb.argOffsetBy(plus4, plus6, 2) {
|
|
t.Fatal("base+4 vs base+6 with delta 2 not proven")
|
|
}
|
|
// Reverse direction carries a negative delta.
|
|
if !fb.argOffsetBy(plus6, plus4, -2) {
|
|
t.Fatal("base+6 vs base+4 with delta -2 not proven")
|
|
}
|
|
// Scalar vs peeled-node forms.
|
|
sc := simdfuse.Arg{Kind: simdfuse.ArgScalar, Index: 0}
|
|
fb.nodes = append(fb.nodes, simdfuse.Node{Op: "scalar_i32_add", Args: []simdfuse.Arg{
|
|
{Kind: simdfuse.ArgScalar, Index: 0}, {Kind: simdfuse.ArgConst, Const: 2}}})
|
|
scPlus2 := simdfuse.Arg{Kind: simdfuse.ArgNode, Index: 3}
|
|
if !fb.argOffsetBy(sc, scPlus2, 2) {
|
|
t.Fatal("scalar vs scalar+2 node with delta 2 not proven")
|
|
}
|
|
if !fb.argOffsetBy(scPlus2, sc, -2) {
|
|
t.Fatal("scalar+2 node vs scalar with delta -2 not proven")
|
|
}
|
|
}
|
|
|
|
func TestWordOperandIdent(t *testing.T) {
|
|
if n, ok := wordOperandIdent(mustExpr(t, "v3140")); !ok || n != "v3140" {
|
|
t.Fatalf("plain ident: %q %v", n, ok)
|
|
}
|
|
if n, ok := wordOperandIdent(mustExpr(t, "base.Ui64(v3140)")); !ok || n != "v3140" {
|
|
t.Fatalf("Ui64 wrap: %q %v", n, ok)
|
|
}
|
|
if n, ok := wordOperandIdent(mustExpr(t, "uint64(int64(v7))")); !ok || n != "v7" {
|
|
t.Fatalf("conversion wraps: %q %v", n, ok)
|
|
}
|
|
if _, ok := wordOperandIdent(mustExpr(t, "v1+v2")); ok {
|
|
t.Fatal("binary expr accepted as word operand")
|
|
}
|
|
}
|
|
|
|
// compactDeadNodes must leave no forward references: the gather
|
|
// rewrite appends its collapsed load after the f16x4_cvt that consumes
|
|
// it, and the pure twin body emits nodes in list order.
|
|
func TestCompactDeadNodesHoistsForwardOperands(t *testing.T) {
|
|
fb := &fusedTreeBuilder{}
|
|
fb.nodes = []simdfuse.Node{
|
|
{Op: "f16x4_cvt", Args: []simdfuse.Arg{{Kind: simdfuse.ArgNode, Index: 3}}},
|
|
{Op: "f32x4_mul", Args: []simdfuse.Arg{{Kind: simdfuse.ArgNode, Index: 0}, {Kind: simdfuse.ArgPairIn, Index: 0}}},
|
|
{Op: "v128_load32_zero", Args: []simdfuse.Arg{{Kind: simdfuse.ArgConst, Const: 64}, {Kind: simdfuse.ArgConst, Const: 0}}}, // dead
|
|
{Op: "v128_load16x4_u", Args: []simdfuse.Arg{{Kind: simdfuse.ArgConst, Const: 128}, {Kind: simdfuse.ArgConst, Const: 0}}},
|
|
}
|
|
fb.nodeVals = make([]*ssa.Value, len(fb.nodes))
|
|
roots := fb.compactDeadNodes([]int{1})
|
|
if len(fb.nodes) != 3 {
|
|
t.Fatalf("live nodes = %d, want 3 (dead load32_zero dropped): %+v", len(fb.nodes), fb.nodes)
|
|
}
|
|
for i, nd := range fb.nodes {
|
|
for _, a := range nd.Args {
|
|
if a.Kind == simdfuse.ArgNode && a.Index >= i {
|
|
t.Fatalf("node %d %s references node %d at or after itself: %+v", i, nd.Op, a.Index, fb.nodes)
|
|
}
|
|
}
|
|
}
|
|
if fb.nodes[0].Op != "v128_load16x4_u" || fb.nodes[1].Op != "f16x4_cvt" || fb.nodes[2].Op != "f32x4_mul" {
|
|
t.Fatalf("order = %s %s %s, want load, cvt, mul", fb.nodes[0].Op, fb.nodes[1].Op, fb.nodes[2].Op)
|
|
}
|
|
if len(roots) != 1 || roots[0] != 2 {
|
|
t.Fatalf("roots = %v, want [2]", roots)
|
|
}
|
|
}
|