Source file
src/simd/godebug_test.go
1
2
3
4
5
6
7 package simd_test
8
9 import (
10 "flag"
11 "fmt"
12 "internal/cpu"
13 "internal/testenv"
14 "os"
15 "os/exec"
16 "runtime"
17 "simd"
18 "slices"
19 "strings"
20 "testing"
21 )
22
23 var (
24 listGODEBUGs = flag.Bool("list-godebugs", false, "print the GODEBUG settings to also run the tests under, then exit")
25 runGODEBUGs = flag.Bool("run-godebugs", false, "run the tests under the current GODEBUG setting and each one that -list-godebugs prints")
26 printConfig = flag.Bool("print-config", false, "print how the simd package is configured, then exit")
27 )
28
29 func TestMain(m *testing.M) {
30 flag.Parse()
31 switch {
32 case *listGODEBUGs:
33
34
35 for _, s := range godebugSettings() {
36 if !s.current {
37 fmt.Println("GODEBUG=" + s.godebug)
38 }
39 }
40 case *runGODEBUGs:
41 os.Exit(runUnderGODEBUGs())
42 case *printConfig:
43 fmt.Println(config())
44 default:
45 os.Exit(m.Run())
46 }
47 }
48
49
50
51
52
53
54
55
56 func runUnderGODEBUGs() int {
57 exe, err := os.Executable()
58 if err != nil {
59 fmt.Println("FAIL: -run-godebugs:", err)
60 return 1
61 }
62
63 var args []string
64 for _, arg := range os.Args[1:] {
65 if name, _, _ := strings.Cut(strings.TrimLeft(arg, "-"), "="); name == "run-godebugs" {
66 continue
67 }
68 args = append(args, arg)
69 }
70 status := 0
71 for _, s := range godebugSettings() {
72 fmt.Printf("# GODEBUG=%s\n", s.godebug)
73 cmd := exec.Command(exe, args...)
74 cmd.Env = append(os.Environ(), "GODEBUG="+s.godebug)
75 cmd.Stdout = os.Stdout
76 cmd.Stderr = os.Stderr
77 if err := cmd.Run(); err != nil {
78 fmt.Printf("FAIL: GODEBUG=%s: %v\n", s.godebug, err)
79 status = 1
80 }
81 }
82 return status
83 }
84
85
86
87 type godebugSetting struct {
88 godebug string
89 current bool
90
91
92
93 wantConfig string
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107 func godebugSettings() []godebugSetting {
108 current := os.Getenv("GODEBUG")
109 settings := []godebugSetting{{godebug: current, current: true}}
110 if simd.Emulated() {
111
112 return settings
113 }
114
115
116
117 prefix := current
118 if prefix != "" {
119 prefix += ","
120 }
121
122 for bits := simd.VectorBits() / 2; bits >= 128; bits /= 2 {
123 settings = append(settings, godebugSetting{
124 godebug: prefix + fmt.Sprintf("simd=%d", bits),
125 wantConfig: fmt.Sprintf("VectorBits=%d Emulated=false", bits),
126 })
127 }
128
129 settings = append(settings, godebugSetting{
130 godebug: prefix + "simd=0",
131 wantConfig: "Emulated=true",
132 })
133
134 if !cpuOptionsWork() {
135 return settings
136 }
137
138
139 switch runtime.GOARCH {
140 case "amd64":
141
142
143
144 for bits := simd.VectorBits(); bits >= 128; bits /= 2 {
145 size := prefix + fmt.Sprintf("simd=%d,", bits)
146 want := fmt.Sprintf("VectorBits=%d Emulated=false", bits)
147 if bits > 128 && cpu.X86.HasVPCLMULQDQ {
148
149
150 settings = append(settings, godebugSetting{godebug: size + "cpu.vpclmulqdq=off", wantConfig: want + " CLMUL=Uint64x2"})
151 }
152 if cpu.X86.HasPCLMULQDQ {
153
154 settings = append(settings, godebugSetting{godebug: size + "cpu.pclmulqdq=off", wantConfig: want + " CLMUL=software"})
155 }
156 }
157 case "arm64":
158
159
160
161
162 if cpu.ARM64.HasPMULL {
163 settings = append(settings, godebugSetting{
164 godebug: prefix + "cpu.pmull=off",
165 wantConfig: "Emulated=false CLMUL=Uint8x16",
166 })
167 }
168 }
169 return settings
170 }
171
172
173
174
175
176 func cpuOptionsWork() bool {
177 switch runtime.GOOS {
178 case "aix", "darwin", "ios", "dragonfly", "freebsd", "netbsd", "openbsd", "illumos", "solaris", "linux":
179 return true
180 }
181 return false
182 }
183
184
185 func config() string {
186 return fmt.Sprintf("VectorBits=%d Emulated=%t CLMUL=%s",
187 simd.VectorBits(), simd.Emulated(), simd.InternalCLMULImpl())
188 }
189
190
191
192
193 func TestGODEBUGSettings(t *testing.T) {
194 settings := godebugSettings()
195 if len(settings) == 1 {
196 t.Log("simd is emulated, so there are no other implementations to select")
197 return
198 }
199 exe := testenv.Executable(t)
200 for _, s := range settings {
201 if s.current {
202 continue
203 }
204 t.Run(s.godebug, func(t *testing.T) {
205 cmd := testenv.Command(t, exe, "-print-config")
206 cmd.Env = append(cmd.Environ(), "GODEBUG="+s.godebug)
207 out, err := cmd.CombinedOutput()
208 if err != nil {
209 t.Fatalf("with GODEBUG=%s: %v\n%s", s.godebug, err, out)
210 }
211
212
213 got := strings.Fields(string(out))
214 for _, want := range strings.Fields(s.wantConfig) {
215 if !slices.Contains(got, want) {
216 t.Errorf("with GODEBUG=%s, simd is configured as %q, want %s", s.godebug, strings.TrimSpace(string(out)), want)
217 }
218 }
219 })
220 }
221 }
222
View as plain text