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: instructions being lifted before arch support tests #78997
package main
import "math/bits"
func f(x uint64) int {
r := 0
for range 5 {
r += bits.OnesCount64(x)
}
return r
}
GOAMD64=v1 go build -gcflags=-S test.go
In the assembly output, the POPCNTQ instruction has been lifted to before the start of the loop. We can't do that, unless we also lift the x86HasPOPCNT test that wraps POPCNTQ instructions in v1 mode.
I think we need something like faultOnNilArg0 but for CPUID bits.
That's a possibility. I was thinking it might be safer to go in the other direction - mark instructions we know are safe to lift. That might be more verbose though.
This issue reveals a pretty fundamental flaw in our SSA representation. There's no ordering edge between a test that an instruction is ok to use, and the instruction itself. It would be really nice if there was an SSA edge that represented that ordering somehow.
We have a similar problem with nil checks, which we kind of hack around. It would be nice if there was an SSA edge from the nil check to the load/store that requires it.
I was working on a patch to lift up all possible instructions from the likely edge of a branch to before branche even if they are only used in the likely branch (basically pulls the whole graph up until the first mem producer or phi node and any of it's transient consumer).
But this has the same issue.
func f(n int, x uint64) uint64 {
var r uint64
for range n {
if archsimd.X86.AVX() {
v := archsimd.BroadcastUint64x2(x)
v = v.Mul(v)
r += v.GetElem(0)
}
}
return r
}
It is trickier because that's a user-level guarding test, not one inserted by the compiler.
func f(n int, x uint64) uint64 {
var r uint64
if !archsimd.X86.AVX() {
n = 0
}
for range n {
v := archsimd.BroadcastUint64x2(x)
v = v.Mul(v)
r += v.GetElem(0)
}
return r
}
This one is tricky because it doesn't even have the test in the loop. Nevertheless, we still can't lift the arch-gated instructions out of the loop.
This issue still exists on the latest master branch when you bury the feature gate in another condition or use two feature gates. I noticed this when working adding SVE support but it affects AVX and PMULL tests too. Here are some AI generated test cases:
amd64
// Minimal reproducer: an AVX512 instruction executes on the path taken// because AVX512 is absent.//// GOARCH=amd64 GOEXPERIMENT=simd go build -gcflags=-S//// The two guarded regions each need a 512-bit zero. cse merges the two into one// value, and it is then materialized on every path reaching the merge --// including the path out of the failed guard. On amd64 a vector zero is a// reference to the fixed all-zeros register, which is free as an operand but// costs a move when it has to be materialized into a specific register, and at// 512 bits that move is VMOVDQU64, which is EVEX-encoded and needs AVX512F.//// Verified on master 5c51011e82, default GOAMD64=v1://// 0x000a 00010 JEQ 73 <- AVX512 absent// 0x000c 00012 VMOVDQU64 Z15, Z0 <- inside the guard: fine// ...// 0x0049 00073 VMOVDQU64 Z15, Z0 <- the absent path: faults//// One guarded region is not enough, and neither is one region plus a value to// merge with: both keep the move inside the guard. Two regions are the minimum.//// The 256-bit form does the same with VMOVDQU (VEX, needs AVX). The 128-bit// form is harmless, because there the move is MOVUPS, which is SSE2 and part of// the amd64 baseline.package main
import (
"fmt""simd/archsimd"
)
//go:noinlinefunctwoRegions() int {
n:=0ifarchsimd.X86.AVX512() {
varv archsimd.Int32x16n+=int(v.ReduceSum())
}
ifarchsimd.X86.AVX512() {
varw archsimd.Int32x16n+=int(w.ReduceSum())
}
returnn
}
funcmain() {
fmt.Println("OK", twoRegions(), archsimd.X86.AVX512())
}
arm64
// Negative result: a NEON feature gate does not reproduce the escape.//// GOEXPERIMENT=simd go build -gcflags=-S//// PMULL is an Advanced SIMD (Neon) instruction behind an optional feature, with// a real check -- archsimd.ARM64.PMULL() -- and pmull is already in// internal/cpu's arm64 options, so GODEBUG=cpu.pmull=off works on an unmodified// toolchain. It looked like a way to demonstrate the class without SVE.//// It does not work. On master 5c51011e82, VPMULL stays inside its guard in all// three shapes below://// oneRegion flag load 4, TBZ 12, VMOVI 16, VPMULL 20// twoRegions VMOVI hoisted to 0 (baseline Neon, harmless); VPMULL 20 and 56,// each after its own TBZ// selectable flag load 4, TBZ 12, VPMULL 20//// Two reasons. VPMULL takes two vector arguments, and zcse only relocates// argument-less values, so mechanism 1 cannot reach it. And branchelim declines// to if-convert these regions, so mechanism 2 does not either.//// The conclusion is that arm64's exposure comes specifically from SVE having// argument-less ops that are real instructions -- ScalableVectorLen is RDVL, and// a scalable or predicate ZeroSIMD is ZDUP or PFALSE. Neon itself has no feature// check because it is the arm64 baseline, and its 128-bit zero is VMOVI, which// is always legal wherever it lands.package main
import (
"fmt""simd/archsimd"
)
//go:noinlinefunconeRegion() uint64 {
ifarchsimd.ARM64.PMULL() {
vara, b archsimd.Uint64x2returna.CarrylessMultiplyEven(b).GetElem(0)
}
return0
}
//go:noinlinefunctwoRegions(cbool) uint64 {
varnuint64ifc {
ifarchsimd.ARM64.PMULL() {
vara, b archsimd.Uint64x2n+=a.CarrylessMultiplyEven(b).GetElem(0)
}
}
ifarchsimd.ARM64.PMULL() {
vara, b archsimd.Uint64x2n+=a.CarrylessMultiplyEven(b).GetElem(0)
}
returnn
}
//go:noinlinefuncselectable(cbool) uint64 {
varnuint64ifc {
n=1
}
ifarchsimd.ARM64.PMULL() {
vara, b archsimd.Uint64x2n+=a.CarrylessMultiplyEven(b).GetElem(0)
}
returnn
}
funcmain() {
fmt.Println("OK", oneRegion(), twoRegions(true), selectable(true), archsimd.ARM64.PMULL())
}
Activity
Jorropo commentedon Apr 28, 2026
I think we need something like
faultOnNilArg0but for CPUID bits.gabyhelp commentedon Apr 28, 2026
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
randall77 commentedon Apr 28, 2026
That's a possibility. I was thinking it might be safer to go in the other direction - mark instructions we know are safe to lift. That might be more verbose though.
randall77 commentedon Apr 28, 2026
This issue reveals a pretty fundamental flaw in our SSA representation. There's no ordering edge between a test that an instruction is ok to use, and the instruction itself. It would be really nice if there was an SSA edge that represented that ordering somehow.
We have a similar problem with nil checks, which we kind of hack around. It would be nice if there was an SSA edge from the nil check to the load/store that requires it.
Not sure how to do that.
Jorropo commentedon Apr 28, 2026
In an other issue I wanted to add nilcheck to the mem chain but this has other negative downsides.
prattmic commentedon Apr 29, 2026
cc @golang/compiler
Jorropo commentedon Apr 29, 2026
I was working on a patch to lift up all possible instructions from the likely edge of a branch to before branche even if they are only used in the likely branch (basically pulls the whole graph up until the first
memproducer orphinode and any of it's transient consumer).But this has the same issue.
prattmic commentedon Apr 29, 2026
This is new in 1.27, so I'm tentatively marking this as a release blocker while we decide what to do.
1 remaining item
randall77 commentedon Apr 29, 2026
Two more tricky cases:
It is trickier because that's a user-level guarding test, not one inserted by the compiler.
This one is tricky because it doesn't even have the test in the loop. Nevertheless, we still can't lift the arch-gated instructions out of the loop.
gopherbot commentedon May 1, 2026
Change https://go.dev/cl/772962 mentions this issue:
cmd/compile: make lifting ops out of loops opt-in instead of opt-outgopherbot commentedon May 1, 2026
Change https://go.dev/cl/771481 mentions this issue:
cmd/compile: test for issue 78997JunyangShao commentedon May 20, 2026
@randall77 do we have any update on this?
randall77 commentedon May 20, 2026
The CL stack just needs review. Feel free.
(It's a bug fix, so it doesn't need to happen today.)
cmd/compile: test for issue 78997
cmd/compile: make lifting ops out of loops opt-in instead of opt-out
AWSjswinney commentedon Sep 16, 2026
@randall77
This issue still exists on the latest master branch when you bury the feature gate in another condition or use two feature gates. I noticed this when working adding SVE support but it affects AVX and PMULL tests too. Here are some AI generated test cases:
amd64
arm64
gopherbot commentedon Sep 16, 2026
Change https://go.dev/cl/833984 mentions this issue:
cmd/compile: teach zcse to not lift arch-feature-gated instructionscmd/compile: teach zcse to not lift arch-feature-gated instructions