Forwarded: https://github.com/klauspost/reedsolomon/issues/343 From: Klaus Post Date: Tue, 11 Aug 2026 17:39:53 +0200 Subject: [PATCH] tests: Fix no-gfni avx512 crash, clean up tests (#344) Fixes #343 Updates test matrices. Always respect test flags and use clean ladder for ISA testing on amd64 and arm64. --- options.go | 51 +++++++++++++++++--- options_test.go | 57 ++++++++++++++++++++++ reedsolomon_test.go | 114 +++++++++++++++++++++++++++++++------------- 3 files changed, 181 insertions(+), 41 deletions(-) create mode 100644 options_test.go diff --git a/options.go b/options.go index 6cf5819..e7dbc1e 100644 --- a/options.go +++ b/options.go @@ -168,18 +168,20 @@ func WithStreamBlockSize(n int) Option { // WithSSSE3 allows to enable/disable SSSE3 instructions. // If not set, SSSE3 will be turned on or off automatically based on CPU ID information. +// Enabling has no effect if the CPU does not support it. func WithSSSE3(enabled bool) Option { return func(o *options) { - o.useSSSE3 = enabled + o.useSSSE3 = enabled && defaultOptions.useSSSE3 } } // WithAVX2 allows to enable/disable AVX2 instructions. // If not set, AVX will be turned on or off automatically based on CPU ID information. // This will also disable AVX GFNI instructions. +// Enabling has no effect if the CPU does not support it. func WithAVX2(enabled bool) Option { return func(o *options) { - o.useAVX2 = enabled + o.useAVX2 = enabled && defaultOptions.useAVX2 if o.useAvxGNFI { o.useAvxGNFI = enabled } @@ -188,33 +190,68 @@ func WithAVX2(enabled bool) Option { // WithSSE2 allows to enable/disable SSE2 instructions. // If not set, SSE2 will be turned on or off automatically based on CPU ID information. +// Enabling has no effect if the CPU does not support it. func WithSSE2(enabled bool) Option { return func(o *options) { - o.useSSE2 = enabled + o.useSSE2 = enabled && defaultOptions.useSSE2 } } // WithAVX512 allows to enable/disable AVX512 (and GFNI) instructions. +// Enabling has no effect if the CPU does not support it. +// AVX512 and GFNI are separate CPU features, so this will only enable +// GFNI if the CPU supports it as well. func WithAVX512(enabled bool) Option { return func(o *options) { - o.useAVX512 = enabled - o.useAvx512GFNI = enabled + o.useAVX512 = enabled && defaultOptions.useAVX512 + o.useAvx512GFNI = enabled && defaultOptions.useAvx512GFNI } } // WithGFNI allows to enable/disable AVX512+GFNI instructions. // If not set, GFNI will be turned on or off automatically based on CPU ID information. +// Enabling has no effect if the CPU does not support it. func WithGFNI(enabled bool) Option { return func(o *options) { - o.useAvx512GFNI = enabled + o.useAvx512GFNI = enabled && defaultOptions.useAvx512GFNI } } // WithAVXGFNI allows to enable/disable GFNI with AVX instructions. // If not set, GFNI will be turned on or off automatically based on CPU ID information. +// Enabling has no effect if the CPU does not support it. func WithAVXGFNI(enabled bool) Option { return func(o *options) { - o.useAvxGNFI = enabled + o.useAvxGNFI = enabled && defaultOptions.useAvxGNFI + } +} + +// WithNEON allows to enable/disable NEON instructions. +// If not set, NEON will be turned on or off automatically based on CPU ID information. +// This will also disable SVE instructions. +// Enabling has no effect if the CPU does not support it. +func WithNEON(enabled bool) Option { + return func(o *options) { + o.useNEON = enabled && defaultOptions.useNEON + if !o.useNEON { + o.useSVE = false + o.vectorLength = 32 + } + } +} + +// WithSVE allows to enable/disable SVE instructions. +// If not set, SVE will be turned on or off automatically based on CPU ID information. +// Enabling has no effect if the CPU does not support it. +func WithSVE(enabled bool) Option { + return func(o *options) { + o.useSVE = enabled && defaultOptions.useSVE + // SVE uses the hardware vector length, everything else wants 32 byte tables. + if o.useSVE { + o.vectorLength = defaultOptions.vectorLength + } else { + o.vectorLength = 32 + } } } diff --git a/options_test.go b/options_test.go new file mode 100644 index 0000000..21cac0f --- /dev/null +++ b/options_test.go @@ -0,0 +1,57 @@ +package reedsolomon + +import "testing" + +// Enabling an instruction set must never exceed what the CPU actually supports, +// or the generated assembly will fault. AVX512 and GFNI in particular are +// separate CPU features; CPUs with AVX512 but no GFNI exist (e.g. Skylake-SP). +func TestOptionsCannotForceUnsupported(t *testing.T) { + var o options + for _, opt := range []Option{ + WithSSE2(true), WithSSSE3(true), WithAVX2(true), + WithAVX512(true), WithGFNI(true), WithAVXGFNI(true), + WithNEON(true), WithSVE(true), + } { + opt(&o) + } + for _, tt := range []struct { + name string + enabled, have bool + }{ + {"useSSE2", o.useSSE2, defaultOptions.useSSE2}, + {"useSSSE3", o.useSSSE3, defaultOptions.useSSSE3}, + {"useAVX2", o.useAVX2, defaultOptions.useAVX2}, + {"useAVX512", o.useAVX512, defaultOptions.useAVX512}, + {"useAvx512GFNI", o.useAvx512GFNI, defaultOptions.useAvx512GFNI}, + {"useAvxGNFI", o.useAvxGNFI, defaultOptions.useAvxGNFI}, + {"useNEON", o.useNEON, defaultOptions.useNEON}, + {"useSVE", o.useSVE, defaultOptions.useSVE}, + } { + if tt.enabled && !tt.have { + t.Errorf("%s was enabled, but CPU does not support it", tt.name) + } + } +} + +// The NEON kernels read fixed 32 byte tables, while SVE uses the hardware vector +// length - 16 bytes on a 128 bit core, as set by the galois_arm64.go init. Turning +// SVE off must restore the table size, or NEON silently reads the wrong layout. +func TestSVEVectorLength(t *testing.T) { + for _, tt := range []struct { + name string + opt Option + }{ + {"WithSVE(false)", WithSVE(false)}, + {"WithNEON(false)", WithNEON(false)}, + } { + o := defaultOptions + o.useSVE, o.vectorLength = true, 16 + tt.opt(&o) + if o.useSVE { + t.Errorf("%s: SVE still enabled", tt.name) + } + if o.vectorLength != 32 { + t.Errorf("%s: vectorLength = %d, want 32", tt.name, o.vectorLength) + } + } +} diff --git a/reedsolomon_test.go b/reedsolomon_test.go index 2b7d212..2a454e0 100644 --- a/reedsolomon_test.go +++ b/reedsolomon_test.go @@ -15,6 +15,7 @@ import ( "math/rand" "os" "runtime" + "slices" "strconv" "testing" "time" @@ -26,6 +27,8 @@ var noAVX2 = flag.Bool("no-avx2", !defaultOptions.useAVX2, "Disable AVX2") var noAVX512 = flag.Bool("no-avx512", !defaultOptions.useAVX512, "Disable AVX512") var noGNFI = flag.Bool("no-gfni", !defaultOptions.useAvx512GFNI, "Disable AVX512+GFNI") var noAVX2GNFI = flag.Bool("no-avx-gfni", !defaultOptions.useAvxGNFI, "Disable AVX+GFNI") +var noNEON = flag.Bool("no-neon", !defaultOptions.useNEON, "Disable NEON") +var noSVE = flag.Bool("no-sve", !defaultOptions.useSVE, "Disable SVE") func TestMain(m *testing.M) { flag.Parse() @@ -38,8 +41,10 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } -func testOptions(o ...Option) []Option { - o = append(o, WithFastOneParityMatrix()) +// isaOptions returns the instruction set overrides requested with -no-* flags. +// These must be applied last, so they win over any explicitly enabled set. +func isaOptions() []Option { + var o []Option if *noSSSE3 { o = append(o, WithSSSE3(false)) } @@ -58,9 +63,20 @@ func testOptions(o ...Option) []Option { if *noAVX2GNFI { o = append(o, WithAVXGFNI(false)) } + if *noNEON { + o = append(o, WithNEON(false)) + } + if *noSVE { + o = append(o, WithSVE(false)) + } return o } +func testOptions(o ...Option) []Option { + o = append(o, WithFastOneParityMatrix()) + return append(o, isaOptions()...) +} + func isIncreasingAndContainsDataRow(indices []int) bool { cols := len(indices) for i := 0; i < cols-1; i++ { @@ -175,19 +191,68 @@ func TestBuildMatrixPAR1Singular(t *testing.T) { t.Logf("matrix %s has singular sub-matrix %s", m, singularSubMatrix) } +// withISA appends the -no-* overrides to every set. The base sets inherit +// whatever was detected, so the flags only take effect if applied to all of them. +func withISA(opts [][]Option) [][]Option { + isa := isaOptions() + if len(isa) == 0 { + return opts + } + for i, o := range opts { + opts[i] = append(slices.Clone(o), isa...) + } + return opts +} + +// isaSets are the instruction sets ordered weakest to strongest, each with the +// option that turns it off. The amd64 and arm64 sets are independent; levels for +// the other architecture collapse into pure Go and get dropped. +var isaSets = []Option{ + WithSSE2(false), + WithSSSE3(false), + WithAVX2(false), + WithAVX512(false), + WithAVXGFNI(false), + WithGFNI(false), + WithNEON(false), + WithSVE(false), +} + +// isaLadder returns instruction set configurations from pure Go up to everything +// the CPU supports. Level i disables every set from i upwards, so the result +// never depends on what was detected and never enables something the CPU lacks. +// Levels that collapse into another one - unsupported, or turned off with a +// -no-* flag - are dropped. +func isaLadder() [][]Option { + var out [][]Option + seen := map[string]bool{} + for i := range len(isaSets) + 1 { + level := slices.Clone(isaSets[i:]) + o := defaultOptions + for _, f := range append(slices.Clone(level), isaOptions()...) { + f(&o) + } + if k := o.cpuOptions(); !seen[k] { + seen[k] = true + out = append(out, level) + } + } + return out +} + func testOpts() [][]Option { if testing.Short() { - return [][]Option{ + return withISA([][]Option{ {WithCauchyMatrix()}, {WithLeopardGF16(true)}, {WithLeopardGF(true)}, - } + }) } - opts := [][]Option{ + base := [][]Option{ {WithPAR1Matrix()}, {WithCauchyMatrix()}, {WithFastOneParityMatrix()}, {WithPAR1Matrix(), WithFastOneParityMatrix()}, {WithCauchyMatrix(), WithFastOneParityMatrix()}, - {WithMaxGoroutines(1), WithMinSplitSize(500), WithSSSE3(false), WithAVX2(false), WithAVX512(false)}, - {WithMaxGoroutines(5000), WithMinSplitSize(50), WithSSSE3(false), WithAVX2(false), WithAVX512(false)}, - {WithMaxGoroutines(5000), WithMinSplitSize(500000), WithSSSE3(false), WithAVX2(false), WithAVX512(false)}, - {WithMaxGoroutines(1), WithMinSplitSize(500000), WithSSSE3(false), WithAVX2(false), WithAVX512(false)}, + {WithMaxGoroutines(1), WithMinSplitSize(500)}, + {WithMaxGoroutines(5000), WithMinSplitSize(50)}, + {WithMaxGoroutines(5000), WithMinSplitSize(500000)}, + {WithMaxGoroutines(1), WithMinSplitSize(500000)}, {WithAutoGoroutines(50000), WithMinSplitSize(500)}, {WithInversionCache(false)}, {WithJerasureMatrix()}, @@ -195,33 +260,14 @@ func testOpts() [][]Option { {WithLeopardGF(true)}, } - for _, o := range opts[:] { - if defaultOptions.useSSSE3 { - n := make([]Option, len(o), len(o)+1) - copy(n, o) - n = append(n, WithSSSE3(true)) - opts = append(opts, n) - } - if defaultOptions.useAVX2 { - n := make([]Option, len(o), len(o)+1) - copy(n, o) - n = append(n, WithAVX2(true)) - opts = append(opts, n) - } - if defaultOptions.useAVX512 { - n := make([]Option, len(o), len(o)+1) - copy(n, o) - n = append(n, WithAVX512(true)) - opts = append(opts, n) - } - if defaultOptions.useAvx512GFNI { - n := make([]Option, len(o), len(o)+1) - copy(n, o) - n = append(n, WithGFNI(false)) - opts = append(opts, n) + ladder := isaLadder() + opts := make([][]Option, 0, len(base)*len(ladder)) + for _, b := range base { + for _, isa := range ladder { + opts = append(opts, append(slices.Clone(b), isa...)) } } - return opts + return withISA(opts) } func parallelIfNotShort(t *testing.T) { -- 2.54.0