Files
5bd861b54c gcasm: gc-capture assembly backend (amd64 + arm64), replacing internal/asmgen (#19)
Replace the hand-written own-emitter asm backend (internal/asmgen) with
gcasm: compile the generated pure-Go bodies with the standard Go
compiler, capture the `-S` listing, and mechanically transform it into a
shippable Plan9 `.s`. The transform strips gc's prologue/stack-check/
epilogue/morestack, regenerates them from the declared frame, and
marshals gc's ABIInternal calls to ABI0 forwarders. Wired into
transpile.Translate and cmd/wasm2go as THE asm backend; a `purego` build
tag still selects the pure bodies on every other GOARCH.

Why ship asm at all — the pure-Go bodies are the faster runtime. The asm
path exists to skip the consumer's golangci-lint/parse cost of the huge
generated bodies (measured ~36.5s pure vs ~3.3s asm for one package;
golangci-lint doesn't parse assembly). gcasm keeps gc's in-body register
allocation and only pays an ABI0 marshalling tax at call boundaries
(register-to-register calls are impossible for tool-emitted asm: the
assembler forbids non-runtime code from declaring ABIInternal).

gcasm vs the own-emitter asm it replaces — strictly better on every
measured axis (go-python microbenchmarks, count=5):
  - runtime arm64 native: 1.7-3.3x faster (Fib 0.30, LoopSum 0.50, Startup 0.58)
  - runtime amd64:        1.05-1.85x faster
  - binary size arm64:    14% smaller (33.87 vs 39.32 MB)
  - cold-cache build:     8.1 vs 10.0s
  - memory:               identical (555 B/op, 11 allocs)
  - lint benefit:         same (both skip pure bodies); 11x faster than pure

What's in internal/gcasm:
  - capture: `-gcflags=<pkg>=-S`, GOARCH-pinned (amd64 + arm64).
  - amd64 + arm64 transforms (peers): prologue/epilogue strip, ABI0 call
    marshalling, jump-table lowering to binary-search trees, const/float/
    type-descriptor pools, runtime-call rewrites, cross-package linkname
    forwarders.
  - dual-arch bundle: gcasm_<arch>.s + gcasm_decls_<arch>.go + keepalive,
    guarded by //go:build <arch>. Functions the transform can't express
    stay pure fallbacks (transparent to callers — same ABI0 forward /
    //go:linkname either way).

Fallback triggers: signatures that stack-assign in ABIInternal (>16 int
args), and functions gc compiles to a DUFFZERO/DUFFCOPY pseudo-op — a
textual duff op nil-derefs the assembler (go1.25 and go1.26 alike; only
compiler-generated progs get the runtime.duffzero target symbol), so the
union across arches is routed to pure.

Delete the superseded backend. gcasm being sole means internal/asmgen
(the SSA-to-plan9 own emitter) and internal/regalloc (its linear-scan
allocator, no other importer) were dead — codegen.Translate generated the
old asm bundle only for transpile.Translate to discard and replace it.
~20k lines removed:
  - delete internal/asmgen and internal/regalloc.
  - codegen.Translate now emits only pure-Go bodies (shared file +
    <pkg>_pure.go); the per-arch asm is entirely gcasm's job. Removed the
    asmgen call sites and the now-dead asm_bundle.go helpers.
  - drop asm_bundle_test.go (codegen+asmgen integration); the wasm ->
    runnable-asm path is covered by gcasm's gates + consumer e2e. The
    remaining codegen tests build/run codegen's PURE output, stripping the
    guard to activate it on the host; production output is unchanged.
Proven inert: regenerating the python AND googlesql bundles after the
deletion yields byte-identical output.

Coverage: the deleted integration test contributed broad cross-package
coverage the removed code no longer needs; a new transpile end-to-end test
(TestTranspileEndToEnd — Parse+Translate+gcasm, build+run on the host) plus
gcasm's gates and the consumer e2e cover the live path. Live-code coverage
lands at 84.7%, so the Makefile COVERAGE_THRESHOLD is recalibrated 85 -> 84.

Notable correctness fixes surfaced at consumer scale (bisected via the
GCASM_FALLBACK_RANGE knob; none visible to fixture gates):
  - linker method pruning of import ifaces -> init()-guarded call pins
  - narrow-int register extension (MOVLQSX / ArgU32 loads)
  - bare (SP) zero-offset outgoing store missed by the frame shift
  - wide indirect calls' stack-assigned args -> ADJSP wrap
  - EFLAGS live across jump-table dispatch -> replay the flag-setter
  - arm64 large-frame prologue/epilogue (SUBS+BLO guard, MOVD $c,R27 /
    ADD R27,RSP teardown, shape-agnostic stack-check sweep)
  - arm64 hardware-SP outgoing-arg shift: a >16-int-arg ABIInternal call
    spills the overflow arg to a hardware N(RSP) slot that must be
    shifted above our ABI0 scratch so the marshaller re-reads it; missing
    it read uninitialised stack (low 32 bits of the spilled module
    pointer) and faulted ~3.4GB outside linear memory (go-googlesql
    TestLifetime_AnalyzerOutputKeepAlive on arm64). Gate: TestA64Spill.

Verification (judged by exit code):
  - go1.25.0 (CI toolchain) AND go1.26.2: `go test -race -covermode=atomic
    -coverpkg=./... ./...` all ok; make lint 0 issues; CodeQL clean
    (FnN-index parses use strconv.ParseUint(s,10,32), a bounded conversion).
  - Consumers, GOARCH=amd64 AND arm64 (native), gcasm bundles via replace:
      googlesqlite  go test -race ./...  -> 35 ok each arch
      go-googlesql  go test ./...        -> ok each arch
      go-python     go test ./... (+race amd64) -> ok each arch
  - arm64 execution gates assemble+link on any host and execute only on an
    arm64 host (skip elsewhere), so CI on linux/amd64 stays green.


Claude-Session: https://claude.ai/code/session_01BJSha2EbWhruLZQTZE87oL

Co-authored-by: Claude Fable 5 <[email protected]>
2026-07-06 21:01:27 +09:00

175 lines
4.5 KiB
Go

package gcasm
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
)
// TestA64Gate0 is the arm64 end-to-end gate: capture gc's arm64
// compilation of a recursive function + a cross-function call, transform
// to ABI0 arm64 .s, assemble on GOARCH=arm64, and run vs the pure
// reference over deep recursion (real stack growth).
func TestA64Gate0(t *testing.T) {
dir := t.TempDir()
libSrc := `package lib
//go:noinline
func Rec(n int32) int32 {
if n <= 1 {
return 1
}
var buf [128]int64
buf[0] = int64(n)
return Rec(n-1) + Rec(n-2) + int32(buf[0])
}
//go:noinline
func G(a, b, c int32) int32 { return a*b + c }
//go:noinline
func F(x int32) int32 {
r := G(x, x+1, x+2)
return r + x
}
//go:noinline
func Med(n int32) int32 {
var buf [900]int64 // ~7.2KB frame → SUBS + BLO wraparound guard
for i := range buf { buf[i] = int64(n) + int64(i) }
if n <= 1 { return int32(buf[1]) }
return Med(n-1) + int32(buf[n&511])
}
`
for name, content := range map[string]string{
"go.mod": "module a64gate\n\ngo 1.25.0\n",
"lib/lib.go": libSrc,
} {
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 := captureArch(dir, "a64gate/lib", "arm64")
if err != nil {
t.Fatal(err)
}
dm := map[string]*DataSym{}
for _, d := range datas {
dm[d.Name] = d
}
byName := map[string]*Fn{}
for _, f := range fns {
byName[f.Name[strings.LastIndex(f.Name, ".")+1:]] = f
}
i32 := func(n int) []ArgKind {
out := make([]ArgKind, n)
for i := range out {
out[i] = ArgI32
}
return out
}
sig := map[string]struct {
params []ArgKind
names []string
}{
"Rec": {i32(1), []string{"n"}},
"G": {i32(3), []string{"a", "b", "c"}},
"F": {i32(1), []string{"x"}},
"Med": {i32(1), []string{"n"}},
}
calleeSig := func(sym string) ([]ArgKind, bool, ArgKind, string, bool) {
n := sym[strings.LastIndex(sym, ".")+1:]
if s, ok := sig[n]; ok && strings.Contains(sym, "a64gate/lib") {
return s.params, true, ArgI32, "·" + n, true
}
return nil, false, 0, "", false
}
var asm strings.Builder
asm.WriteString("#include \"textflag.h\"\n#include \"funcdata.h\"\n\n")
for _, name := range []string{"Rec", "G", "F", "Med"} {
s := sig[name]
body, err := TransformARM64(byName[name], TransformOptions{
SymName: name,
CalleeSig: calleeSig,
Params: s.params,
HasResult: true,
Result: ArgI32,
ArgNames: s.names,
Datas: dm,
})
if err != nil {
// gc may compile a stack array to DUFFZERO on some toolchains;
// such a function is a pure fallback in Build, so this asm gate
// does not apply to it. The fixtures cover the asm path.
if errors.Is(err, errUnsupportedDuff) {
t.Skipf("%s compiled to a DUFF pseudo-op on this toolchain; it is a pure fallback in Build", name)
}
t.Fatalf("transform %s: %v", name, err)
}
asm.WriteString(body)
asm.WriteString("\n")
}
run := t.TempDir()
files := map[string]string{
"go.mod": "module a64run\n\ngo 1.25.0\n",
"decl.go": "//go:build arm64\n\npackage a64run\n\n" +
"func Rec(n int32) int32\nfunc G(a, b, c int32) int32\nfunc F(x int32) int32\nfunc Med(n int32) int32\n",
"ref.go": "package a64run\n\n" + strings.Replace(strings.Replace(libSrc, "package lib", "", 1),
"func Rec", "func recRef", 1),
"body_arm64.s": asm.String(),
"run_test.go": `package a64run
import "testing"
func recRefWrap(n int32) int32 {
if n <= 1 {
return 1
}
var buf [128]int64
buf[0] = int64(n)
return recRefWrap(n-1) + recRefWrap(n-2) + int32(buf[0])
}
func medRefWrap(n int32) int32 {
var buf [900]int64
for i := range buf {
buf[i] = int64(n) + int64(i)
}
if n <= 1 {
return int32(buf[1])
}
return medRefWrap(n-1) + int32(buf[n&511])
}
func TestRun(t *testing.T) {
for _, n := range []int32{1, 2, 10, 26, 30} {
if got, want := Rec(n), recRefWrap(n); got != want {
t.Fatalf("Rec(%d)=%d want %d", n, got, want)
}
}
for _, n := range []int32{1, 2, 10, 40} {
if got, want := Med(n), medRefWrap(n); got != want {
t.Fatalf("Med(%d)=%d want %d", n, got, want)
}
}
}
`,
}
// The ref.go rename left a stray recRef; simplest: drop ref.go and
// inline the reference in run_test.go (done above via recRefWrap).
delete(files, "ref.go")
for name, content := range files {
if err := os.WriteFile(filepath.Join(run, name), []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
runArm64Gate(t, run, ".", "TestRun", asm.String())
}