Go Assembly Is Not Assembly: The 58% You Pay for Crossing ABI0
Two instructions of hand-written assembly made a function 58% slower. The cause is not the instructions. It is the calling convention the toolchain...
Two instructions of hand-written assembly made a function 58% slower. The cause is not the instructions. It is the calling convention the toolchain inserts at both ends of them — and the same toolchain will accept your mistakes without a warning.
The File You Write Is a Suggestion, Not a Program
Go has no assembler pass. The compiler emits a semi-abstract instruction stream and performs instruction selection partly after code generation. When you write MOVQ, the toolchain may emit a load, a clear, or a genuine move. Sometimes it emits exactly what you wrote.
That one fact explains every rule that surprises you later: why a MOVQ can disappear, why a TEXT block cannot fall through, why an argument you addressed at 0(FP) arrives in AX. The compiler listing for a package containing one assembly function makes it visible.
$ go build -gcflags=-S ./... 2>&1 | grep TEXT
add.go:7 TEXT asmlab.addGo(SB), NOSPLIT|NOFRAME|ABIInternal, $0-16
add.go:7 ADDQ BX, AX
<autogenerated>:1 TEXT asmlab.add(SB), DUPOK|NOSPLIT|ABIWRAPPER|ABIInternal, $32-16
Read the third line again. The symbol named asmlab.add is not the function I wrote. It is a compiler-generated wrapper: a prologue, two stores, a call, and a register restore. My body lives at asmlab.add.abi0, a name that appears only after the linker merges the objects.
The listing from
-gcflags=-Sis the compiler's view of the package. It is not the linker's view of the binary. They are different programs.
Verification has its own trap. go tool objdump in go1.24.4 mis-decodes the three-byte VEX prefix: it consumes c5 fd ef c0 as three bytes, prints OUTL AX, DX, and desynchronises for the rest of the function.
$ go tool objdump -s 'asmlab\.sumInt64AVX2' s.bin
0x468faa c5fdef OUTL AX, DX
0x468fad c04889ca RORB $0xca, -0x77(AX)
0x468fb7 c5 ?
0x468fb8 fd STD
$ objdump -d --start-address=0x468faa --stop-address=0x468fb7 s.bin
468faa: c5 fd ef c0 vpxor %ymm0,%ymm0,%ymm0
468fae: 48 89 ca mov %rcx,%rdx
Same bytes, two decoders, one of them wrong. If a disassembly mentions OUTL or STD in code you wrote with vector instructions, stop reading it and cross-check with binutils.
The rewriting extends to your addressing. FP is a pseudo-register the linker resolves against the frame size it read from your TEXT line. I declared ret+16(FP) in a function with a 32-byte frame; the emitted instruction was MOVQ %rax, 0x40(%rsp). The same line in a zero-frame function emits 0x18(%rsp). Change the frame number and every argument reference moves with it, which is why the caller has to be told the same number.
ABI0 Is a Stable Contract With a Measured Toll at the Boundary
Go runs two calling conventions. ABIInternal passes arguments in registers and is explicitly allowed to change between releases. ABI0 passes everything on the stack and is frozen — that is the contract your .s file is written against.
On amd64, ABIInternal uses nine integer registers in order — AX, BX, CX, DI, SI, R8, R9, R10, R11 — plus fifteen XMM registers. Go 1.17 switched linux/amd64 to that scheme; the release notes claim about 5% on a representative benchmark set and roughly 2% smaller binaries.
Your assembly does not participate in it. The compiler spills the arguments to the caller's outgoing frame slots, calls the .abi0 symbol, then repairs the two registers ABIInternal gives fixed meaning to.
export.go:4 MOVQ $0x1, 0(SP)
export.go:4 MOVQ $0x2, 0x8(SP)
export.go:4 CALL asmlab.add.abi0(SB)
export.go:4 XORPS X15, X15
export.go:4 MOVQ FS:0xfffffff8, R14
export.go:4 MOVQ 0x10(SP), AX
X15 must read as zero and R14 must point at the current goroutine. ABI0 promises nothing about either, so every return crosses back through a zeroing instruction and a thread-local load.
I measured what that crossing costs. Same operation, three implementations, eight runs each, single CPU:
| Variant | ns/op (median) | vs Go |
|---|---|---|
Go, //go:noinline | 1.866 | baseline |
| Assembly, direct call (ABI0) | 2.954 | +58% |
| Assembly, through a function value | 3.774 | +102% |
A two-instruction body costs 2.954 ns written in assembly and 1.866 ns written by the compiler. Route the same function through a func value and the ABI wrapper adds another 0.82 ns.
TEXT asmlab.add(SB), DUPOK|NOSPLIT|ABIWRAPPER|ABIInternal, $32-16
PUSHQ BP
MOVQ SP, BP
SUBQ $24, SP
MOVQ AX, 0(SP) // register args to stack
MOVQ BX, 8(SP)
CALL asmlab.add.abi0(SB)
XORPS X15, X15
MOVQ FS:0, R14
MOVQ 0x10(SP), AX // result back to a register
The wrapper exists because a function value can be called by arbitrary Go code following ABIInternal, so the compiler generates the conversion once per symbol: 46 bytes, a 32-byte frame, two stores, two restores, and a reload of the result from the stack.
That also explains why the direct call is cheaper. Before compiling a package, go build runs the assembler with -gensymabis to extract the ABI of every symbol in the .s files, and hands the result to the compiler through -symabis. The compiler then knows it can emit the stack-based call itself and skip the wrapper entirely. Only indirect calls pay for the conversion.
ABI0 buys stability. It does not buy speed, and on a small body it costs more than the body.
Frame Size, Argument Size, and the 792-Byte Cliff
A TEXT directive carries two numbers and a flag set. $framesize-argsize declares how many bytes of local frame the function needs and how many bytes of arguments live in the caller's frame. Get either wrong and the assembler says nothing.
Four pseudo-registers do the addressing. FP for arguments, SP for locals at negative offsets, SB for globals, PC for branches. Plain 0(FP) is rejected: every argument reference needs a name, even though the name is semantically irrelevant to the toolchain.
#include "textflag.h"
// func add(a, b int64) int64
TEXT ·add(SB), NOSPLIT, $0-24
MOVQ a+0(FP), AX
ADDQ b+8(FP), AX
MOVQ AX, ret+16(FP)
RET
go vet is the only thing that checks that declaration against the Go prototype. These are real errors from a package where I declared the wrong sizes on purpose:
$ go vet ./...
./x_amd64.s:3:1: [amd64] add: wrong argument size 16; expected $...-24
./x_amd64.s:5:1: [amd64] add: RET without writing to 8-byte ret+16(FP)
./x_amd64.s:11:1: [amd64] noSize: wrong argument size 0; expected $...-16
Note what go build did with the same package: exit code 0. The argument-size rule and the return-value rule are enforced by asmdecl in vet, not by the assembler. If go vet is not in CI, an assembly function that never writes its result builds clean and returns whatever the stack held.
NOSPLIT is the harder constraint. It removes the stack-growth preamble, so the frame plus everything it calls must fit inside the 792-byte guard window. Exceed it and the linker refuses to produce a binary:
$ go build -o /tmp/asmerr3.bin .
# asmerr3
main.small: nosplit stack over 792 byte limit
main.small<0>
grows 16 bytes, calls main.big<0>
grows 4104 bytes
3328 bytes over limit
4104 bytes for a $4096 frame; the extra eight are the frame's own bookkeeping. The useful part is the call path, which names the NOSPLIT function that made an impossible promise.
What Vectorised Assembly Buys: A 3x Ceiling and a 16-Element Floor
Go's compiler does not autovectorise. A range loop over []int64 issues one scalar add per element, so the vector width sits unused. That gap is the only honest reason to write .s files in application code.
// func sumInt64AVX2(p *int64, n int) int64
TEXT ·sumInt64AVX2(SB), NOSPLIT, $0-24
MOVQ p+0(FP), SI
MOVQ n+8(FP), CX
VPXOR Y0, Y0, Y0
MOVQ CX, DX
SHRQ $2, DX
JZ tail
loop:
VPADDQ (SI), Y0, Y0
ADDQ $32, SI
DECQ DX
JNZ loop
tail:
VEXTRACTI128 $1, Y0, X1
VPADDQ X1, X0, X0
VPSHUFD $0x4E, X0, X1
VPADDQ X1, X0, X0
VMOVQ X0, AX
ANDQ $3, CX
JZ done
rest:
ADDQ (SI), AX
ADDQ $8, SI
DECQ CX
JNZ rest
done:
MOVQ AX, ret+16(FP)
RET
Four int64 per VPADDQ, one accumulator, a scalar tail for the remainder, and a horizontal reduction at the end. The reduction is where I shipped a bug.
After the loop Y0 holds four partial sums. Extract the high lane, add it, then shuffle with VPSHUFD $0xB1 and add again — that is the standard two-step reduction for four 32-bit integers. VPSHUFD shuffles dwords. On 64-bit lanes the $0xB1 pattern does not swap halves; it moves half of each lane across the boundary.
For an eight-element input the answer is 28. The kernel returned 120,259,084,316, which is 28 shifted left 32 bits plus 28. Every line assembled without complaint. The test caught it, and one $0x4E shuffle replaced the broken step.
| Elements | Go loop | AVX2 kernel | Speedup |
|---|---|---|---|
| 4 | 5.62 ns | 5.33 ns | 1.05x |
| 16 | 10.31 ns | 7.21 ns | 1.43x |
| 64 | 33.06 ns | 14.52 ns | 2.28x |
| 1,024 | 560.3 ns | 203.0 ns | 2.76x |
| 65,536 | 37,694 ns | 12,229 ns | 3.08x |
Below sixteen elements the boundary toll eats the vector width; at four elements the kernel is a rounding error ahead of the loop. At 512 KiB it moves 42.9 GB/s against 13.9 GB/s scalar.
Three caveats ride with that 3.08x. The kernel is amd64-only. It assumes AVX2 with no CPUID check, so it faults on a pre-Haswell part. And it needs a build-tagged Go fallback for every other architecture you ship. The speedup is real; the surface around it is larger than the kernel.
The Rule I Use Now
Assembly earns its place when the per-call work is large enough to amortise the ABI0 crossing, or when the operation cannot be expressed in Go at all. For a loop of a few dozen elements or more that the compiler refuses to vectorise, the numbers above justify it.
Everything else is a loss. Small leaf functions, string helpers, bit tricks the SSA backend already handles — writing those in assembly costs 58% and buys nothing. That backend is not passive; the 8x gap in Go's netipaddr shows what it does once the code is shaped for it.
What you accept when you do write it: a per-architecture file set, a fallback path, a differential test across many lengths rather than one, go vet in CI, and a second disassembler. And the intrinsics route keeps improving — the seam around archsimd still costs 65x at small sizes, which is the same regime where ABI0 loses.
The assembler is a compiler IR wearing Plan 9 syntax. Treat every line as a request, verify the emitted bytes with a disassembler you trust, and benchmark the boundary before you benchmark the loop.