Source file src/simd/archsimd/_gen/sgutil/stablearch.go
1 // Copyright 2026 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package sgutil 6 7 import ( 8 "fmt" 9 "go/ast" 10 "go/build" 11 "go/build/constraint" 12 "go/token" 13 "io" 14 "path/filepath" 15 "slices" 16 "strings" 17 ) 18 19 // stableArchsimdGOARCHes lists the architectures whose archsimd API can be 20 // generally available. The archsimd API for any other architecture is 21 // experimental until its architecture is added here. 22 var stableArchsimdGOARCHes = []string{"amd64", "arm64", "wasm"} 23 24 // maxBuildTags limits the number of distinct build tags in a //go:build line 25 // that [ParseBuildConstraints] has to try every assignment of. 26 const maxBuildTags = 16 27 28 // BuildConstraints are the build constraints of a SIMD source file: its 29 // //go:build line, and the GOARCH that its file name implies, if any. 30 type BuildConstraints struct { 31 expr constraint.Expr // the //go:build constraint, or nil 32 nameArch string // the GOARCH in the file name, or "" if there's no GOARCH in the file name or a GOARCH that isn't in stableArchsimdGOARCHes 33 34 phase Phase 35 stableArchOnly bool 36 37 free []string // tags whose assignments we had to try (for testing) 38 } 39 40 // ParseBuildConstraints returns the build constraints of file, which must have 41 // been parsed with comments using fset. It's an error if the file never 42 // builds, or if its //go:build line is too complicated to analyze. 43 func ParseBuildConstraints(fset *token.FileSet, file *ast.File) (*BuildConstraints, error) { 44 expr, err := buildConstraint(file) 45 if err != nil { 46 return nil, err 47 } 48 c := &BuildConstraints{ 49 expr: expr, 50 nameArch: nameGOARCH(filepath.Base(fset.File(file.Pos()).Name())), 51 } 52 53 // We work out the phase and whether the file is only for stable archsimd 54 // GOARCHes without knowing which build tags are GOARCHes, or which 55 // GOARCHes exist. Every tag other than goexperiment.simd and 56 // goexperiment.simdstable is unknown, even the stable archsimd GOARCH 57 // tags, and we try every assignment of true or false to the unknown tags 58 // with each GOEXPERIMENT setting: neither experiment, simdstable alone, 59 // and simd+simdstable. We check every satisfying assignment to see whether 60 // it's satisfied even with no stable archsimd GOARCH set. For example, 61 // "goexperiment.simdstable && (amd64 || mips)" builds with simdstable 62 // alone, so it's PhaseStable, and it builds with mips set and no stable 63 // archsimd GOARCH, so it isn't only for stable archsimd GOARCHes. 64 // 65 // Trying every assignment is exact, except that some assignments can't 66 // happen in a real build, such as one that sets two GOARCHes. Those can 67 // only make us report that a file isn't only for stable archsimd GOARCHes 68 // when it is, never the reverse, and they don't arise from realistic 69 // constraints. 70 // 71 // A file name that restricts the file to one stable archsimd GOARCH, as 72 // ops_amd64.go or slicepart_wasm_test.go does, is like an additional 73 // conjunct with that GOARCH. Other file names don't matter: a file named 74 // ops_riscv64.go just looks like a file that isn't restricted to stable 75 // archsimd GOARCHes. 76 full := expr 77 if c.nameArch != "" { 78 tag := &constraint.TagExpr{Tag: c.nameArch} 79 if full == nil { 80 full = tag 81 } else { 82 full = &constraint.AndExpr{X: full, Y: tag} 83 } 84 } 85 if full == nil { 86 // The file always builds, for every GOARCH and in any phase. 87 return c, nil 88 } 89 90 // A tag reachable from the root only through &&s must be set for the file 91 // to build, and a !tag reachable that way must be unset (and likewise 92 // through the ||s of a negated expression, by De Morgan's laws). Fix 93 // those, so we only have to try assignments of the other ("free") tags. 94 // This leaves nothing to try for most real constraints, such as 95 // "goexperiment.simdstable && amd64". sense is whether x must be true. 96 fixed := make(map[string]bool) 97 conflict := false 98 var conjuncts func(x constraint.Expr, sense bool) 99 conjuncts = func(x constraint.Expr, sense bool) { 100 switch x := x.(type) { 101 case *constraint.AndExpr: 102 if sense { 103 conjuncts(x.X, sense) 104 conjuncts(x.Y, sense) 105 } 106 case *constraint.OrExpr: 107 if !sense { 108 conjuncts(x.X, sense) 109 conjuncts(x.Y, sense) 110 } 111 case *constraint.NotExpr: 112 conjuncts(x.X, !sense) 113 case *constraint.TagExpr: 114 if v, ok := fixed[x.Tag]; ok && v != sense { 115 // The tag must be both set and unset. 116 conflict = true 117 } 118 fixed[x.Tag] = sense 119 } 120 } 121 conjuncts(full, true) 122 if conflict { 123 return nil, fmt.Errorf("build constraint %s never holds", full) 124 } 125 126 // Number the free tags: those that aren't fixed, other than the SIMD 127 // GOEXPERIMENT tags, whose values depend on the phase we try. 128 free := make(map[string]int) 129 var collect func(x constraint.Expr) 130 collect = func(x constraint.Expr) { 131 switch x := x.(type) { 132 case *constraint.TagExpr: 133 _, isFixed := fixed[x.Tag] 134 _, seen := free[x.Tag] 135 if !isFixed && !seen && !slices.Contains(AllPhaseTags, x.Tag) { 136 free[x.Tag] = len(free) 137 } 138 case *constraint.NotExpr: 139 collect(x.X) 140 case *constraint.AndExpr: 141 collect(x.X) 142 collect(x.Y) 143 case *constraint.OrExpr: 144 collect(x.X) 145 collect(x.Y) 146 default: 147 panic(fmt.Sprintf("unexpected constraint expression %T", x)) 148 } 149 } 150 collect(full) 151 if len(free) > maxBuildTags { 152 return nil, fmt.Errorf("build constraint %s has too many tags to check; simplify the //go:build line", expr) 153 } 154 // Record free tags for testing. 155 for tag := range free { 156 c.free = append(c.free, tag) 157 } 158 159 // try reports whether the file builds with the SIMD GOEXPERIMENT tags in 160 // expTags set (and the others unset), and if so, whether it only builds 161 // when a stable archsimd GOARCH is set. 162 try := func(expTags []string) (builds, stableArchOnly bool) { 163 stableArchOnly = true 164 // Enumerate all possible assignments of free tags. 165 for bits := 0; bits < 1<<len(free); bits++ { 166 isSet := func(tag string) bool { 167 // Is it a SIMD phase tag? 168 if slices.Contains(AllPhaseTags, tag) { 169 return slices.Contains(expTags, tag) 170 } 171 if v, ok := fixed[tag]; ok { 172 return v 173 } 174 // Every tag in the expression is fixed or free, so this 175 // is only !ok for tags that aren't in the expression at 176 // all, such as stable archsimd GOARCHes it doesn't 177 // mention. Those are unset. 178 i, ok := free[tag] 179 return ok && bits&(1<<i) != 0 180 } 181 if !full.Eval(isSet) { 182 continue 183 } 184 // We found some configuration that builds. 185 builds = true 186 if !slices.ContainsFunc(stableArchsimdGOARCHes, isSet) { 187 // We found a configuration that builds even if we're 188 // not on a stable GOARCH. 189 stableArchOnly = false 190 break 191 } 192 } 193 return builds, stableArchOnly 194 } 195 196 // The phase is the first, from least to most experimental, in which the 197 // file builds. 198 for _, phase := range []Phase{0, PhaseStable, PhaseExperiment} { 199 builds, stableArchOnly := try(phase.BuildTags()) 200 if builds { 201 c.phase, c.stableArchOnly = phase, stableArchOnly 202 return c, nil 203 } 204 } 205 return nil, fmt.Errorf("build constraint %s never holds", full) 206 } 207 208 // Phase returns the phase whose GOEXPERIMENT the file needs: [PhaseStable] if 209 // it builds with GOEXPERIMENT=simdstable, [PhaseExperiment] if it builds only 210 // with GOEXPERIMENT=simd (which implies simdstable), or 0 if it builds with 211 // neither. 212 func (c *BuildConstraints) Phase() Phase { return c.phase } 213 214 // StableArchOnly reports whether, with the GOEXPERIMENT of its phase, the file 215 // only builds for architectures whose archsimd API can be generally 216 // available (amd64, arm64 and wasm). Only such a file may declare generally 217 // available archsimd API. 218 func (c *BuildConstraints) StableArchOnly() bool { return c.stableArchOnly } 219 220 // buildConstraint returns file's //go:build constraint, or nil if it has none. 221 func buildConstraint(file *ast.File) (constraint.Expr, error) { 222 // The //go:build line must come before the package clause. 223 for _, cg := range file.Comments { 224 if cg.Pos() > file.Package { 225 break 226 } 227 for _, c := range cg.List { 228 if constraint.IsGoBuild(c.Text) { 229 return constraint.Parse(c.Text) 230 } 231 } 232 } 233 return nil, nil 234 } 235 236 // nameGOARCH returns the stable archsimd GOARCH that the file name filename 237 // restricts a file to, or "" if it doesn't restrict the GOARCH or restricts it 238 // to an unknown GOARCH. 239 func nameGOARCH(filename string) string { 240 // Internally, go/build knows how to parse any GOARCH out of a file name, 241 // but it doesn't expose that functionality directly. Instead, ask it 242 // whether the name matches each stable archsimd GOARCH, one at a time. 243 var match []string 244 for _, arch := range stableArchsimdGOARCHes { 245 // Use the same GOOS for every GOARCH, so that a GOOS in the name 246 // doesn't make it match some of them and not others. This happens 247 // to work for wasm, too, even though linux/wasm isn't a valid 248 // platform. 249 ctxt := build.Context{GOARCH: arch, GOOS: "linux", Compiler: "gc"} 250 // We process the go:build line ourselves, so give go/build an empty 251 // source file. 252 ctxt.OpenFile = func(string) (io.ReadCloser, error) { 253 return io.NopCloser(strings.NewReader("package p\n")), nil 254 } 255 if ok, err := ctxt.MatchFile(".", filename); err == nil && ok { 256 match = append(match, arch) 257 } 258 } 259 switch len(match) { 260 case 0: 261 // Doesn't build on any stable GOARCH. 262 return "" 263 case 1: 264 return match[0] 265 case len(stableArchsimdGOARCHes): 266 // The name doesn't restrict the GOARCH. 267 return "" 268 } 269 panic(fmt.Sprintf("file name %s matches more than one GOARCH: %v", filename, match)) 270 } 271