Skip to content

Commit e73afaa

Browse files
aclementsgopherbot
authored andcommitted
internal/cpu: add AVX-512-CD and DQ, and derived "basic AVX-512"
This adds detection for the CD and DQ sub-features of x86 AVX-512. Building on these, we also add a "derived" AVX-512 feature that bundles together the basic usable subset of subfeatures. Despite the F in AVX-512-F standing for "foundation", AVX-512-F+BW+DQ+VL together really form the basic usable subset of AVX-512 functionality. These have also all been supported together by almost every CPU, and are guaranteed by GOAMD64=v4, so there's little point in separating them out. This is a cherry-pick of CL 680899 from the dev.simd branch. Change-Id: I34356502bd1853ba2372e48db0b10d55cffe07a1 Reviewed-on: https://go-review.googlesource.com/c/go/+/693396 Reviewed-by: Cherry Mui <[email protected]> Auto-Submit: Austin Clements <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent cef381b commit e73afaa

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/internal/cpu/cpu.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ var X86 struct {
3131
HasADX bool
3232
HasAVX bool
3333
HasAVX2 bool
34+
HasAVX512 bool // Virtual feature: F+CD+BW+DQ+VL
3435
HasAVX512F bool
36+
HasAVX512CD bool
3537
HasAVX512BW bool
38+
HasAVX512DQ bool
3639
HasAVX512VL bool
40+
HasAVX512VPCLMULQDQ bool
3741
HasBMI1 bool
3842
HasBMI2 bool
3943
HasERMS bool
@@ -48,7 +52,6 @@ var X86 struct {
4852
HasSSSE3 bool
4953
HasSSE41 bool
5054
HasSSE42 bool
51-
HasAVX512VPCLMULQDQ bool
5255
_ CacheLinePad
5356
}
5457

@@ -161,13 +164,20 @@ var RISCV64 struct {
161164
//go:linkname S390X
162165
//go:linkname RISCV64
163166

167+
// doDerived, if non-nil, is called after processing GODEBUG to set "derived"
168+
// feature flags.
169+
var doDerived func()
170+
164171
// Initialize examines the processor and sets the relevant variables above.
165172
// This is called by the runtime package early in program initialization,
166173
// before normal init functions are run. env is set by runtime if the OS supports
167174
// cpu feature options in GODEBUG.
168175
func Initialize(env string) {
169176
doinit()
170177
processOptions(env)
178+
if doDerived != nil {
179+
doDerived()
180+
}
171181
}
172182

173183
// options contains the cpu debug options that can be used in GODEBUG.

src/internal/cpu/cpu_x86.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ const (
3636
cpuid_BMI2 = 1 << 8
3737
cpuid_ERMS = 1 << 9
3838
cpuid_AVX512F = 1 << 16
39+
cpuid_AVX512DQ = 1 << 17
3940
cpuid_ADX = 1 << 19
41+
cpuid_AVX512CD = 1 << 28
4042
cpuid_SHA = 1 << 29
4143
cpuid_AVX512BW = 1 << 30
4244
cpuid_AVX512VL = 1 << 31
@@ -89,7 +91,9 @@ func doinit() {
8991
// they can be turned off.
9092
options = append(options,
9193
option{Name: "avx512f", Feature: &X86.HasAVX512F},
94+
option{Name: "avx512cd", Feature: &X86.HasAVX512CD},
9295
option{Name: "avx512bw", Feature: &X86.HasAVX512BW},
96+
option{Name: "avx512dq", Feature: &X86.HasAVX512DQ},
9397
option{Name: "avx512vl", Feature: &X86.HasAVX512VL},
9498
)
9599
}
@@ -154,7 +158,9 @@ func doinit() {
154158

155159
X86.HasAVX512F = isSet(ebx7, cpuid_AVX512F) && osSupportsAVX512
156160
if X86.HasAVX512F {
161+
X86.HasAVX512CD = isSet(ebx7, cpuid_AVX512CD)
157162
X86.HasAVX512BW = isSet(ebx7, cpuid_AVX512BW)
163+
X86.HasAVX512DQ = isSet(ebx7, cpuid_AVX512DQ)
158164
X86.HasAVX512VL = isSet(ebx7, cpuid_AVX512VL)
159165
X86.HasAVX512VPCLMULQDQ = isSet(ecx7, cpuid_AVX512VPCLMULQDQ)
160166
}
@@ -170,6 +176,17 @@ func doinit() {
170176

171177
_, _, _, edxExt1 := cpuid(0x80000001, 0)
172178
X86.HasRDTSCP = isSet(edxExt1, cpuid_RDTSCP)
179+
180+
doDerived = func() {
181+
// Rather than carefully gating on fundamental AVX-512 features, we have
182+
// a virtual "AVX512" feature that captures F+CD+BW+DQ+VL. BW, DQ, and
183+
// VL have a huge effect on which AVX-512 instructions are available,
184+
// and these have all been supported on everything except the earliest
185+
// Phi chips with AVX-512. No CPU has had CD without F, so we include
186+
// it. GOAMD64=v4 also implies exactly this set, and these are all
187+
// included in AVX10.1.
188+
X86.HasAVX512 = X86.HasAVX512F && X86.HasAVX512CD && X86.HasAVX512BW && X86.HasAVX512DQ && X86.HasAVX512VL
189+
}
173190
}
174191

175192
func isSet(hwc uint32, value uint32) bool {

0 commit comments

Comments
 (0)