Skip to content

cmd/compile: GOEXPERIMENT=simd allocates 512-bit vector values from 15 of the 32 zmm registers #80828

Closed as duplicate

Description

@scttfrdmn

Go version

go1.26.5 (toolchain host darwin/arm64, cross-compiling and executing on linux/amd64), GOEXPERIMENT=simd.

What did you do?

Wrote an FMA-saturation loop over N independent archsimd.Float32x16 accumulators, with distinct starting values so CSE cannot merge the chains, and swept N:

package main

import "simd/archsimd"

//go:noinline
func chains(x []float32, n int) archsimd.Float32x16 {
	v := archsimd.LoadFloat32x16Slice(x)
	a0 := archsimd.LoadFloat32x16Slice(x[0:])
	a1 := archsimd.LoadFloat32x16Slice(x[16:])
	// ... aN-1
	for k := 0; k < n; k++ {
		a0 = v.MulAdd(v, a0)
		a1 = v.MulAdd(v, a1)
		// ... aN-1
	}
	s := a0
	s = s.Add(a1)
	// ... aN-1
	return s
}

func main() {
	x := make([]float32, 1024)
	_ = chains(x, 3)
}

(Accumulators must be distinct named variables rather than an array element — an
array is not SROA'd here and goes to memory for an unrelated reason, cf. #79322.)

Then counted, in the steady-state loop of the -gcflags=-S listing, the vector
stack traffic and the set of Z registers named anywhere in the function.

What did you expect to see?

AVX-512 has 32 architectural vector registers, and _gen/AMD64Ops.go:128 declares
the operand mask used by the AVX-512 ops as 31 of them:

w = buildReg("X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X16 X17 X18 X19 X20 X21 X22 X23 X24 X25 X26 X27 X28 X29 X30 X31")

VFMADD213PS512 and friends use the w31 shape built from it, so the op
definitions say Z16–Z31 are legal operands. I expected the frontier — the largest
N with no spilling — to be about 29 (31 registers, minus the shared multiplicand,
minus one scratch), and I expected to see registers above Z15 in the listing.

What did you see instead?

The frontier is 13, and no register above Z14 is ever named, in any of the
functions, at any N:

N (accumulators) insns in loop vector stack refs per iteration register copies
13 43 0 27
14 45 14 14
15 51 19 14
16 55 24 12
20 79 44 12
$ GOEXPERIMENT=simd GOARCH=amd64 GOOS=linux go build -gcflags=-S . 2>&1 \
    | grep -oE '\bZ[0-9]+\b' | sort -u -V | tr '\n' ' '
Z0 Z1 Z2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14

At N=20 the loop makes 44 stack references per iteration to hold 20 values, on a
machine with 32 registers of which 17 are never touched.

The frontier of 13 is 15 − 1 (the shared multiplicand) − 1 (a scratch register, see below), i.e. exactly the 15-register budget.

Where it comes from

regalloc.go:942 picks a value's candidate register set by type:

if t.IsSIMD() {
	if t.Size() > 8 {
		return s.f.Config.fpRegMask & s.allocatable
	}
	// K mask
	return s.f.Config.gpRegMask & s.allocatable
}

and s.allocatable (regalloc.go:721) is the union of exactly three masks:

s.allocatable = s.f.Config.gpRegMask | s.f.Config.fpRegMask | s.f.Config.specialRegMask

On amd64 (opGen.go:96119–96121):

mask value bits registers
fpRegMaskAMD64 2147418112 = 0x7FFF0000 16–30 X0–X14
specialRegMaskAMD64 71494644084506624 49–55 K1–K7
gpRegMaskAMD64 49135 = 0xBFEF 0–13, 15 integer, minus BP/g

registersAMD64 numbers X15 as 31 and X16–X31 as 32–47. Bit 31 is
excluded deliberately (X15 is the ABI zero register). Bits 32–47 appear in no
mask at all, so Z16–Z31 are not allocatable to any value, and the register shapes
that list them are trimmed by the intersection with s.allocatable.

So the mismatch is between the op definitions (31 registers legal) and the
allocator (15 offered). Nothing warns; the only symptom is spilling.

Why it matters

This is the binding constraint on writing a competitive dense-linear-algebra
microkernel in pure Go with GOEXPERIMENT=simd. In a project doing exactly that
(a float32 BLAS subset, https://github.com/scttfrdmn/keel), a 115-shape sweep of
SGEMM microkernel tiles finds the zero-spill frontier at 8 accumulators, where
a 32-register file supports 24. Two consequences that are not obvious:

  • A hard floor on memory traffic. With MR rows and V 16-lane vectors along
    N, a tile reads 1/MR + 1/V vectors per FMA. Getting below 0.75 needs MR ≥ 3
    and V ≥ 3, hence ≥9 accumulators — one past the frontier. So no zero-spill
    shape of any size can read less than three quarters of a vector per FMA. With 24
    accumulators available the floor would be 0.42.
  • A measured throughput ceiling on one class of host. The instruction count
    the 15-register budget forces (4.625 instructions per FMA on the best shipped
    shape) is itself the binding constraint on a 2-FMA/cycle, ~4-wide-issue part:
    on an i9-9960X (Skylake-X) that kernel reaches 46.0% of the machine's measured
    single-core FMA peak, and 94.6% of the ceiling its own instruction mix implies —
    i.e. it is at the front end's limit, not the FMA units'. The two Zen hosts
    (7950X3D, Ryzen AI MAX+ 395), with wider front ends, reach 96.6% and 64.2%.

The same clamp is presumably why the 213-form register copies in the table above
are so expensive: at 13 accumulators there is no free register for an FMA result
to land in, and the loop pays 27 copies for 13 FMAs. (That half is a separate
issue about the FMA forms available, filed separately.)

Question

Is the exclusion of X16–X31 from fpRegMaskAMD64 deliberate — e.g. something in
the ABI, the register-mask width (regMask is 64 bits and bits 32–47 are already
spoken for), or save/restore cost — or is it an oversight that predates
GOEXPERIMENT=simd having ops that can name those registers? If it is deliberate,
it seems worth documenting alongside #78979, since "AVX-512 with half the register
file" is a materially different target for library authors than AVX-512.

cc #73787 #78979

Activity

  1. added a commit that references this issue on Aug 11, 2026
  2. JunyangShao commented on Aug 13, 2026

    @JunyangShao
    Contributor

    I think we have a plan to support the high-number Z registers, don't know the status.
    @cherrymui

  3. cherrymui commented on Aug 13, 2026

    @cherrymui
    Member

    Could you try Go 1.27RC3 or tip? I believe it should already work now.

  4. added
    WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
    on Aug 13, 2026
  5. added a commit that references this issue on Aug 30, 2026
  6. scttfrdmn commented on Aug 30, 2026

    @scttfrdmn
    Author

    Confirmed fixed on go1.27.0 — thank you for the pointer.

    First, the correction I owe: this issue duplicates #78753, which was already closed when I
    filed it, and the fix is CL 768262 ([dev.simd] cmd/compile: make X/Y/Z16-31 allocatable,
    merged 2026-04-23). gabyhelp listed #78753 in the first comment here and I didn't follow it.

    The allocator now offers 31 of 32. regalloc.go unions four masks rather than three —
    simdRegMask is new — and regMask is now a two-word struct, which answers the width
    question this issue asked:

    s.allocatable = s.f.Config.gpRegMask.union(s.f.Config.fpRegMask).union(s.f.Config.specialRegMask).union(s.f.Config.simdRegMask)

    specialRegMaskAMD64 went from 71494644084506624 to 71776114766249984. The difference
    is 281470681743360, which is exactly bits 32–47 — X16–X31 under registersAMD64's
    numbering. Only X15, the ABI zero register, is now in no mask, and that was always
    deliberate.

    Re-running the sweep: the register count improved much more than the code did. Same
    repro, ported (LoadFloat32x16Slice is now LoadFloat32x16), counted the same way in the
    steady-state loop, highest Z named anywhere in the function. No go1.26.5 toolchain is
    installed here, so every 1.26.5 column is quoted from this issue's own table rather than
    re-run — the N=13 row is the control on that.

    N insns 1.26.5 → 1.27.0 vector stack refs register copies highest Z
    13 43 → 43 0 → 0 27 → 27 Z14
    14 45 → 45 14 → 2 14 → 26 Z16
    15 51 → 51 19 → 3 14 → 30 Z17
    16 55 → 54 24 → 3 12 → 32 Z18
    20 79 → 68 44 → 9 12 → 36 Z23
    24 — → 82 — → 15 — → 40 Z29
    31 — → 107 — → 43 — → 30 Z31

    Two controls on that table. N=13 reproduces all three of its go1.26.5 columns exactly,
    which is what you would expect below the frontier and says the ported repro is still the
    same measurement. And every row on both toolchains closes as
    insns = N + refs + copies + 3, so nothing outside those two columns moved.

    The spilling that went away came back as register-to-register copies, nearly one for
    one.
    At N=14, −12 refs and +26−14 = +12 copies, for a net of zero instructions. At
    N=15, −16 and +16, again zero. At N=16 the net is 1 instruction out of 55. Only at
    N=20 does it pay, and there it is 11 out of 79. The frontier — largest N with no vector
    stack traffic — is still 13, unchanged.

    The reason is property 2 of the original report, which is #80829: with no 231/132 FMA
    form, a = v.MulAdd(v, a) cannot land in a, so every accumulator needs a copy per
    iteration. At 15 registers that surfaced as spills; at 31 it surfaces as copies. For this
    shape the register fix is close to neutral until #80829 lands. I've left the measurement
    there rather than reopening this.

    Same story on a real kernel — a 6×32 float32 GEMM microkernel, 12 accumulators, unrolled
    by 4. Recorded at 270 instructions with 90 vector stack references for 48 FMAs on
    go1.26.5; re-measured on go1.27.0 it is 219 with 44, and it now allocates Z16–Z23.
    But 45 of those 219 are register copies, only 3 of the 12 accumulators still spill, and 36
    of the 44 remaining stack references are broadcast-scalar round-trips through legacy-SSE
    MOVUPS, which is #80835.

    Identical under GOAMD64 v1, v3 and v4. I checked because CL 767380 had proposed
    gating on v4; the CL that actually landed is unconditional and behaves that way.

    One thing I could not reconcile, in case it is a leftover rather than intent:
    simdRegMaskAMD64 is still 2147418112, i.e. X0–X14, and compatRegs returns
    simdRegMask.intersect(s.allocatable) for SIMD values wider than 8 bytes — yet the
    listing allocates Z16–Z23 freely, including plain VMOVDQU64 Z0, Z16 copies and a reload
    into Z19. So something other than compatRegs is choosing those registers and I may be
    reading the wrong path. Is simdRegMaskAMD64 still X0–X14 on purpose?

    Happy for this to be closed as a duplicate of #78753.

  7. added a commit that references this issue on Aug 30, 2026
  8. cherrymui commented on Aug 31, 2026

    @cherrymui
    Member

    Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions