Debian Codegraph @

debian/patches/0001-tests-Fix-no-gfni-avx512-crash-clean-up-tests-344.patch

raw
1 Forwarded: https://github.com/klauspost/reedsolomon/issues/343
2 From: Klaus Post <[email protected]>
3 Date: Tue, 11 Aug 2026 17:39:53 +0200
4 Subject: [PATCH] tests: Fix no-gfni avx512 crash, clean up tests (#344)
5
6 Fixes #343
7
8 Updates test matrices. Always respect test flags and use clean ladder for ISA testing on amd64 and arm64.
9 ---
10 options.go | 51 +++++++++++++++++---
11 options_test.go | 57 ++++++++++++++++++++++
12 reedsolomon_test.go | 114 +++++++++++++++++++++++++++++++-------------
13 3 files changed, 181 insertions(+), 41 deletions(-)
14 create mode 100644 options_test.go
15
16 diff --git a/options.go b/options.go
17 index 6cf5819..e7dbc1e 100644
18 --- a/options.go
19 +++ b/options.go
20 @@ -168,18 +168,20 @@ func WithStreamBlockSize(n int) Option {
21
22 // WithSSSE3 allows to enable/disable SSSE3 instructions.
23 // If not set, SSSE3 will be turned on or off automatically based on CPU ID information.
24 +// Enabling has no effect if the CPU does not support it.
25 func WithSSSE3(enabled bool) Option {
26 return func(o *options) {
27 - o.useSSSE3 = enabled
28 + o.useSSSE3 = enabled && defaultOptions.useSSSE3
29 }
30 }
31
32 // WithAVX2 allows to enable/disable AVX2 instructions.
33 // If not set, AVX will be turned on or off automatically based on CPU ID information.
34 // This will also disable AVX GFNI instructions.
35 +// Enabling has no effect if the CPU does not support it.
36 func WithAVX2(enabled bool) Option {
37 return func(o *options) {
38 - o.useAVX2 = enabled
39 + o.useAVX2 = enabled && defaultOptions.useAVX2
40 if o.useAvxGNFI {
41 o.useAvxGNFI = enabled
42 }
43 @@ -188,33 +190,68 @@ func WithAVX2(enabled bool) Option {
44
45 // WithSSE2 allows to enable/disable SSE2 instructions.
46 // If not set, SSE2 will be turned on or off automatically based on CPU ID information.
47 +// Enabling has no effect if the CPU does not support it.
48 func WithSSE2(enabled bool) Option {
49 return func(o *options) {
50 - o.useSSE2 = enabled
51 + o.useSSE2 = enabled && defaultOptions.useSSE2
52 }
53 }
54
55 // WithAVX512 allows to enable/disable AVX512 (and GFNI) instructions.
56 +// Enabling has no effect if the CPU does not support it.
57 +// AVX512 and GFNI are separate CPU features, so this will only enable
58 +// GFNI if the CPU supports it as well.
59 func WithAVX512(enabled bool) Option {
60 return func(o *options) {
61 - o.useAVX512 = enabled
62 - o.useAvx512GFNI = enabled
63 + o.useAVX512 = enabled && defaultOptions.useAVX512
64 + o.useAvx512GFNI = enabled && defaultOptions.useAvx512GFNI
65 }
66 }
67
68 // WithGFNI allows to enable/disable AVX512+GFNI instructions.
69 // If not set, GFNI will be turned on or off automatically based on CPU ID information.
70 +// Enabling has no effect if the CPU does not support it.
71 func WithGFNI(enabled bool) Option {
72 return func(o *options) {
73 - o.useAvx512GFNI = enabled
74 + o.useAvx512GFNI = enabled && defaultOptions.useAvx512GFNI
75 }
76 }
77
78 // WithAVXGFNI allows to enable/disable GFNI with AVX instructions.
79 // If not set, GFNI will be turned on or off automatically based on CPU ID information.
80 +// Enabling has no effect if the CPU does not support it.
81 func WithAVXGFNI(enabled bool) Option {
82 return func(o *options) {
83 - o.useAvxGNFI = enabled
84 + o.useAvxGNFI = enabled && defaultOptions.useAvxGNFI
85 + }
86 +}
87 +
88 +// WithNEON allows to enable/disable NEON instructions.
89 +// If not set, NEON will be turned on or off automatically based on CPU ID information.
90 +// This will also disable SVE instructions.
91 +// Enabling has no effect if the CPU does not support it.
92 +func WithNEON(enabled bool) Option {
93 + return func(o *options) {
94 + o.useNEON = enabled && defaultOptions.useNEON
95 + if !o.useNEON {
96 + o.useSVE = false
97 + o.vectorLength = 32
98 + }
99 + }
100 +}
101 +
102 +// WithSVE allows to enable/disable SVE instructions.
103 +// If not set, SVE will be turned on or off automatically based on CPU ID information.
104 +// Enabling has no effect if the CPU does not support it.
105 +func WithSVE(enabled bool) Option {
106 + return func(o *options) {
107 + o.useSVE = enabled && defaultOptions.useSVE
108 + // SVE uses the hardware vector length, everything else wants 32 byte tables.
109 + if o.useSVE {
110 + o.vectorLength = defaultOptions.vectorLength
111 + } else {
112 + o.vectorLength = 32
113 + }
114 }
115 }
116
117 diff --git a/options_test.go b/options_test.go
118 new file mode 100644
119 index 0000000..21cac0f
120 --- /dev/null
121 +++ b/options_test.go
122 @@ -0,0 +1,57 @@
123 +package reedsolomon
124 +
125 +import "testing"
126 +
127 +// Enabling an instruction set must never exceed what the CPU actually supports,
128 +// or the generated assembly will fault. AVX512 and GFNI in particular are
129 +// separate CPU features; CPUs with AVX512 but no GFNI exist (e.g. Skylake-SP).
130 +func TestOptionsCannotForceUnsupported(t *testing.T) {
131 + var o options
132 + for _, opt := range []Option{
133 + WithSSE2(true), WithSSSE3(true), WithAVX2(true),
134 + WithAVX512(true), WithGFNI(true), WithAVXGFNI(true),
135 + WithNEON(true), WithSVE(true),
136 + } {
137 + opt(&o)
138 + }
139 + for _, tt := range []struct {
140 + name string
141 + enabled, have bool
142 + }{
143 + {"useSSE2", o.useSSE2, defaultOptions.useSSE2},
144 + {"useSSSE3", o.useSSSE3, defaultOptions.useSSSE3},
145 + {"useAVX2", o.useAVX2, defaultOptions.useAVX2},
146 + {"useAVX512", o.useAVX512, defaultOptions.useAVX512},
147 + {"useAvx512GFNI", o.useAvx512GFNI, defaultOptions.useAvx512GFNI},
148 + {"useAvxGNFI", o.useAvxGNFI, defaultOptions.useAvxGNFI},
149 + {"useNEON", o.useNEON, defaultOptions.useNEON},
150 + {"useSVE", o.useSVE, defaultOptions.useSVE},
151 + } {
152 + if tt.enabled && !tt.have {
153 + t.Errorf("%s was enabled, but CPU does not support it", tt.name)
154 + }
155 + }
156 +}
157 +
158 +// The NEON kernels read fixed 32 byte tables, while SVE uses the hardware vector
159 +// length - 16 bytes on a 128 bit core, as set by the galois_arm64.go init. Turning
160 +// SVE off must restore the table size, or NEON silently reads the wrong layout.
161 +func TestSVEVectorLength(t *testing.T) {
162 + for _, tt := range []struct {
163 + name string
164 + opt Option
165 + }{
166 + {"WithSVE(false)", WithSVE(false)},
167 + {"WithNEON(false)", WithNEON(false)},
168 + } {
169 + o := defaultOptions
170 + o.useSVE, o.vectorLength = true, 16
171 + tt.opt(&o)
172 + if o.useSVE {
173 + t.Errorf("%s: SVE still enabled", tt.name)
174 + }
175 + if o.vectorLength != 32 {
176 + t.Errorf("%s: vectorLength = %d, want 32", tt.name, o.vectorLength)
177 + }
178 + }
179 +}
180 diff --git a/reedsolomon_test.go b/reedsolomon_test.go
181 index 2b7d212..2a454e0 100644
182 --- a/reedsolomon_test.go
183 +++ b/reedsolomon_test.go
184 @@ -15,6 +15,7 @@ import (
185 "math/rand"
186 "os"
187 "runtime"
188 + "slices"
189 "strconv"
190 "testing"
191 "time"
192 @@ -26,6 +27,8 @@ var noAVX2 = flag.Bool("no-avx2", !defaultOptions.useAVX2, "Disable AVX2")
193 var noAVX512 = flag.Bool("no-avx512", !defaultOptions.useAVX512, "Disable AVX512")
194 var noGNFI = flag.Bool("no-gfni", !defaultOptions.useAvx512GFNI, "Disable AVX512+GFNI")
195 var noAVX2GNFI = flag.Bool("no-avx-gfni", !defaultOptions.useAvxGNFI, "Disable AVX+GFNI")
196 +var noNEON = flag.Bool("no-neon", !defaultOptions.useNEON, "Disable NEON")
197 +var noSVE = flag.Bool("no-sve", !defaultOptions.useSVE, "Disable SVE")
198
199 func TestMain(m *testing.M) {
200 flag.Parse()
201 @@ -38,8 +41,10 @@ func TestMain(m *testing.M) {
202 os.Exit(m.Run())
203 }
204
205 -func testOptions(o ...Option) []Option {
206 - o = append(o, WithFastOneParityMatrix())
207 +// isaOptions returns the instruction set overrides requested with -no-* flags.
208 +// These must be applied last, so they win over any explicitly enabled set.
209 +func isaOptions() []Option {
210 + var o []Option
211 if *noSSSE3 {
212 o = append(o, WithSSSE3(false))
213 }
214 @@ -58,9 +63,20 @@ func testOptions(o ...Option) []Option {
215 if *noAVX2GNFI {
216 o = append(o, WithAVXGFNI(false))
217 }
218 + if *noNEON {
219 + o = append(o, WithNEON(false))
220 + }
221 + if *noSVE {
222 + o = append(o, WithSVE(false))
223 + }
224 return o
225 }
226
227 +func testOptions(o ...Option) []Option {
228 + o = append(o, WithFastOneParityMatrix())
229 + return append(o, isaOptions()...)
230 +}
231 +
232 func isIncreasingAndContainsDataRow(indices []int) bool {
233 cols := len(indices)
234 for i := 0; i < cols-1; i++ {
235 @@ -175,19 +191,68 @@ func TestBuildMatrixPAR1Singular(t *testing.T) {
236 t.Logf("matrix %s has singular sub-matrix %s", m, singularSubMatrix)
237 }
238
239 +// withISA appends the -no-* overrides to every set. The base sets inherit
240 +// whatever was detected, so the flags only take effect if applied to all of them.
241 +func withISA(opts [][]Option) [][]Option {
242 + isa := isaOptions()
243 + if len(isa) == 0 {
244 + return opts
245 + }
246 + for i, o := range opts {
247 + opts[i] = append(slices.Clone(o), isa...)
248 + }
249 + return opts
250 +}
251 +
252 +// isaSets are the instruction sets ordered weakest to strongest, each with the
253 +// option that turns it off. The amd64 and arm64 sets are independent; levels for
254 +// the other architecture collapse into pure Go and get dropped.
255 +var isaSets = []Option{
256 + WithSSE2(false),
257 + WithSSSE3(false),
258 + WithAVX2(false),
259 + WithAVX512(false),
260 + WithAVXGFNI(false),
261 + WithGFNI(false),
262 + WithNEON(false),
263 + WithSVE(false),
264 +}
265 +
266 +// isaLadder returns instruction set configurations from pure Go up to everything
267 +// the CPU supports. Level i disables every set from i upwards, so the result
268 +// never depends on what was detected and never enables something the CPU lacks.
269 +// Levels that collapse into another one - unsupported, or turned off with a
270 +// -no-* flag - are dropped.
271 +func isaLadder() [][]Option {
272 + var out [][]Option
273 + seen := map[string]bool{}
274 + for i := range len(isaSets) + 1 {
275 + level := slices.Clone(isaSets[i:])
276 + o := defaultOptions
277 + for _, f := range append(slices.Clone(level), isaOptions()...) {
278 + f(&o)
279 + }
280 + if k := o.cpuOptions(); !seen[k] {
281 + seen[k] = true
282 + out = append(out, level)
283 + }
284 + }
285 + return out
286 +}
287 +
288 func testOpts() [][]Option {
289 if testing.Short() {
290 - return [][]Option{
291 + return withISA([][]Option{
292 {WithCauchyMatrix()}, {WithLeopardGF16(true)}, {WithLeopardGF(true)},
293 - }
294 + })
295 }
296 - opts := [][]Option{
297 + base := [][]Option{
298 {WithPAR1Matrix()}, {WithCauchyMatrix()},
299 {WithFastOneParityMatrix()}, {WithPAR1Matrix(), WithFastOneParityMatrix()}, {WithCauchyMatrix(), WithFastOneParityMatrix()},
300 - {WithMaxGoroutines(1), WithMinSplitSize(500), WithSSSE3(false), WithAVX2(false), WithAVX512(false)},
301 - {WithMaxGoroutines(5000), WithMinSplitSize(50), WithSSSE3(false), WithAVX2(false), WithAVX512(false)},
302 - {WithMaxGoroutines(5000), WithMinSplitSize(500000), WithSSSE3(false), WithAVX2(false), WithAVX512(false)},
303 - {WithMaxGoroutines(1), WithMinSplitSize(500000), WithSSSE3(false), WithAVX2(false), WithAVX512(false)},
304 + {WithMaxGoroutines(1), WithMinSplitSize(500)},
305 + {WithMaxGoroutines(5000), WithMinSplitSize(50)},
306 + {WithMaxGoroutines(5000), WithMinSplitSize(500000)},
307 + {WithMaxGoroutines(1), WithMinSplitSize(500000)},
308 {WithAutoGoroutines(50000), WithMinSplitSize(500)},
309 {WithInversionCache(false)},
310 {WithJerasureMatrix()},
311 @@ -195,33 +260,14 @@ func testOpts() [][]Option {
312 {WithLeopardGF(true)},
313 }
314
315 - for _, o := range opts[:] {
316 - if defaultOptions.useSSSE3 {
317 - n := make([]Option, len(o), len(o)+1)
318 - copy(n, o)
319 - n = append(n, WithSSSE3(true))
320 - opts = append(opts, n)
321 - }
322 - if defaultOptions.useAVX2 {
323 - n := make([]Option, len(o), len(o)+1)
324 - copy(n, o)
325 - n = append(n, WithAVX2(true))
326 - opts = append(opts, n)
327 - }
328 - if defaultOptions.useAVX512 {
329 - n := make([]Option, len(o), len(o)+1)
330 - copy(n, o)
331 - n = append(n, WithAVX512(true))
332 - opts = append(opts, n)
333 - }
334 - if defaultOptions.useAvx512GFNI {
335 - n := make([]Option, len(o), len(o)+1)
336 - copy(n, o)
337 - n = append(n, WithGFNI(false))
338 - opts = append(opts, n)
339 + ladder := isaLadder()
340 + opts := make([][]Option, 0, len(base)*len(ladder))
341 + for _, b := range base {
342 + for _, isa := range ladder {
343 + opts = append(opts, append(slices.Clone(b), isa...))
344 }
345 }
346 - return opts
347 + return withISA(opts)
348 }
349
350 func parallelIfNotShort(t *testing.T) {
351 --
352 2.54.0
353