You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile: small fixed array [2]uint64 fails scalar replacement of aggregates (SROA) unlike equivalent struct #81192
However, for [2]uint64 in Add128Array, the SSA backend fails to decompose the array into scalar registers. It generates 46 bytes of machine code with stack allocation, stack reads/writes, and zeroing:
When 128-bit operations are chained in loops, this asymmetry causes severe register spilling to the stack for small arrays, expanding the stack frame and emitting dead bounds check branches that are completely absent with structs or scalars.
What did you expect to see?
For small non-escaping fixed arrays (e.g. [2]uint64, [2]uint32, [4]uint32), cmd/compile's SSA pass should perform scalar replacement into GPR registers with the same efficiency as equivalent structs (struct{Lo, Hi uint64}).
My SROA pass can handle partially, if Add128Array is called with local variables in its caller side, and it's inlined, then SROA can promote them.
But the function itself won't be SROA-ed, because my SROA currently don't optimize params, and also CanSSA decides to put the 2 arrays in memory already, and I am not sure if it's possible to change the ABI decision at SSA stage.
Supporting SROA on params is in my plan though, it might not completely move the value to register, but everything following a dominating load can be registerized I think. :D
Thanks for the clarification on the SROA / SSA pipeline!
To provide some concrete context on why this matters so much for 128-bit/256-bit arithmetic and crypto kernels (like Poly1305, ChaCha20, AES-OCB), we ran an isolated SSA assembly comparison and micro-benchmark on Go 1.27 (GOEXPERIMENT=simd, AMD64) comparing [2]uint64 vs struct{Lo, Hi uint64} vs discrete scalars.
A. Array parameter Add128_Array(a, b [2]uint64) [2]uint64 (46 bytes, 8 stack operations):
LEAQ main.~r0+40(SP),AXMOVUPS X15, (AX)MOVQ main.a+8(SP),AX // Load a[0] from stackMOVQ main.b+24(SP),CX // Load b[0] from stackMOVQ main.a+16(SP),DX // Load a[1] from stackMOVQ main.b+32(SP),BX // Load b[1] from stackADDQ AX,CX // Add loMOVQCX, main.~r0+40(SP) // Store lo to stackADCQ BX,DX // Add hi with carryMOVQDX, main.~r0+48(SP) // Store hi to stackRET
B. Struct parameter Add128_Struct(a, b U128Struct) U128Struct (7 bytes, 0 stack operations):
The reason [2]uint64 is sent to stack memory in the first place is that types/size.go:619 assigns the sentinel MaxUint8 to [N>1]T, whereas a struct with <= 4 fields (MaxStruct) is allocated direct GPR registers in abiutils.go.
While SROA on PPARAM with a dominating load (CL 743100) will be a massive improvement for loop bodies, treating small fixed-size arrays (<= 4 scalar words, 32 bytes max) identically to equivalent structs in types/size.go and CanSSA would eliminate the caller/callee stack overhead entirely.
@hazyhaar I think the decision to registerize array params with len > 1 shouldn't be made by SROA, it belongs in the frontend, where CanSSA/param decisions live. This pending CL makes small arrays SSA-able and could be extended to cover params. Note the CL currently excludes params deliberately (decomposing a copy-dominated param pessimizes whole-copies), so the extension probably wants a use-sensitivity check alongside its existing dynamic-index analysis.
Actually passing arrays params in registers would additionally need an ABIInternal change (the register-assignment algorithm stack-assigns arrays of length > 1) plus matching updates in reflect and the runtime, that's separable, and per the ABI doc's own rationale probably not worth it on its own.
@randall77 Wdyt? Is that CL a good candidate for the param extension, or should we do it ourselves? Or should we do it at all? :P
I'm not sure if we should change the ABI at this point. It will help cases where we use all constant indexes, sure, but I suspect that's a minority of cases. Otherwise, I think it just makes things worse. And we can't in general tell whether the callee uses constant indexes or not (e.g. for an interface call).
func f(a [4]int, i int) int {
return a[i]
}
This function currently compiles to a single indexed load. If a came in registers, then we need 4 stores + an indexed load (or some mess of conditional moves or masking to simulate indexing).
Of course, at the call site we would need fewer stores.
f([4]int{3,4,5,6}, 1)
Currently needs 4 constant stores to set up the array. If we passed in registers, we'd need 4 constant moves.
So passing in registers seems to me at least a slight pessimism (same number of stores, maybe a few extra reg-reg ops) in the case where indexes aren't constant.
Putting arrays in registers can also quickly exhaust the set of registers. For f, what if we had only 4 registers? Then i is not in a register anymore.
Thanks @randall77 and @JunyangShao for taking the time to explain the underlying trade-offs.
It helps me understand much better why general-purpose Go makes this choice around dynamic indexing (a[i]), and why keeping [N>1]T on the stack ABI makes sense at the language scale.
Looking forward to Junyang's CanSSA / SROA work landing in 1.28. In the meantime, we will continue using dedicated structs (struct { Lo, Hi uint64 }) for our static 128-bit/crypto kernels.
Appreciate the great discussion, and thanks a lot for 1.27, it's awesome!
Activity
gabyhelp commentedon Aug 28, 2026
Related Issues
Related Code Changes
Related Discussions
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
randall77 commentedon Aug 28, 2026
The tricky part of registerizing arrays is what to do about non-constant indexes. That problem cannot happen for structs.
That said @JunyangShao has a set of CLs that would probably fix this for 1.28.
JunyangShao commentedon Aug 28, 2026
My SROA pass can handle partially, if
Add128Arrayis called with local variables in its caller side, and it's inlined, then SROA can promote them.But the function itself won't be SROA-ed, because my SROA currently don't optimize params, and also
CanSSAdecides to put the 2 arrays in memory already, and I am not sure if it's possible to change the ABI decision at SSA stage.Supporting SROA on params is in my plan though, it might not completely move the value to register, but everything following a dominating load can be registerized I think. :D
hazyhaar commentedon Aug 28, 2026
Hi Junyang,
Thanks for the clarification on the SROA / SSA pipeline!
To provide some concrete context on why this matters so much for 128-bit/256-bit arithmetic and crypto kernels (like Poly1305, ChaCha20, AES-OCB), we ran an isolated SSA assembly comparison and micro-benchmark on Go 1.27 (
GOEXPERIMENT=simd, AMD64) comparing[2]uint64vsstruct{Lo, Hi uint64}vs discrete scalars.1. Disassembly Comparison (
go build -gcflags="-S")A. Array parameter
Add128_Array(a, b [2]uint64) [2]uint64(46 bytes, 8 stack operations):B. Struct parameter
Add128_Struct(a, b U128Struct) U128Struct(7 bytes, 0 stack operations):2. Benchmark Results (i9-14900K, Go 1.27.0):
3. Takeaway & Observation on
types/size.go:[2]uint64is sent to stack memory in the first place is thattypes/size.go:619assigns the sentinelMaxUint8to[N>1]T, whereas astructwith <= 4 fields (MaxStruct) is allocated direct GPR registers inabiutils.go.PPARAMwith a dominating load (CL 743100) will be a massive improvement for loop bodies, treating small fixed-size arrays (<= 4 scalar words, 32 bytes max) identically to equivalent structs intypes/size.goandCanSSAwould eliminate the caller/callee stack overhead entirely.Looking forward to your SROA CL landing!
JunyangShao commentedon Aug 29, 2026
@hazyhaar I think the decision to registerize array params with len > 1 shouldn't be made by SROA, it belongs in the frontend, where CanSSA/param decisions live. This pending CL makes small arrays SSA-able and could be extended to cover params. Note the CL currently excludes params deliberately (decomposing a copy-dominated param pessimizes whole-copies), so the extension probably wants a use-sensitivity check alongside its existing dynamic-index analysis.
Actually passing arrays params in registers would additionally need an ABIInternal change (the register-assignment algorithm stack-assigns arrays of length > 1) plus matching updates in reflect and the runtime, that's separable, and per the ABI doc's own rationale probably not worth it on its own.
@randall77 Wdyt? Is that CL a good candidate for the param extension, or should we do it ourselves? Or should we do it at all? :P
randall77 commentedon Aug 29, 2026
I'm not sure if we should change the ABI at this point. It will help cases where we use all constant indexes, sure, but I suspect that's a minority of cases. Otherwise, I think it just makes things worse. And we can't in general tell whether the callee uses constant indexes or not (e.g. for an interface call).
This function currently compiles to a single indexed load. If
acame in registers, then we need 4 stores + an indexed load (or some mess of conditional moves or masking to simulate indexing).Of course, at the call site we would need fewer stores.
Currently needs 4 constant stores to set up the array. If we passed in registers, we'd need 4 constant moves.
So passing in registers seems to me at least a slight pessimism (same number of stores, maybe a few extra reg-reg ops) in the case where indexes aren't constant.
Putting arrays in registers can also quickly exhaust the set of registers. For
f, what if we had only 4 registers? Theniis not in a register anymore.hazyhaar commentedon Aug 29, 2026
Thanks @randall77 and @JunyangShao for taking the time to explain the underlying trade-offs.
It helps me understand much better why general-purpose Go makes this choice around dynamic indexing (
a[i]), and why keeping[N>1]Ton the stack ABI makes sense at the language scale.Looking forward to Junyang's
CanSSA/ SROA work landing in 1.28. In the meantime, we will continue using dedicated structs (struct { Lo, Hi uint64 }) for our static 128-bit/crypto kernels.Appreciate the great discussion, and thanks a lot for 1.27, it's awesome!
JunyangShao commentedon Sep 8, 2026
We will do the SROA passes to optimize local variables, but not yet for params.