| // Copyright 2026 The Go Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style | |
| // license that can be found in the LICENSE file. | |
| //go:build ignore | |
| // Mkoptions generates the lists of the X86 features that each GOAMD64 level | |
| // requires: goamd64_gen.go, which lists their GODEBUG options, and the | |
| // compiler's cmd/compile/internal/ssacompile/goamd64_gen.go, which lists the | |
| // fields whose loads the compiler can replace with true. Generating both from | |
| // one table keeps them in sync. | |
| package main | |
| import ( | |
| "bytes" | |
| "fmt" | |
| "go/format" | |
| "log" | |
| "os" | |
| ) | |
| type feature struct { | |
| option string // GODEBUG option name | |
| field string // field of X86 | |
| } | |
| // levels lists the X86 features that each GOAMD64 level requires, beyond the | |
| // ones that the levels below it require. | |
| var levels = []struct { | |
| level int | |
| features []feature | |
| }{ | |
| {2, []feature{ | |
| {"popcnt", "HasPOPCNT"}, | |
| {"sse3", "HasSSE3"}, | |
| {"sse41", "HasSSE41"}, | |
| {"sse42", "HasSSE42"}, | |
| {"ssse3", "HasSSSE3"}, | |
| }}, | |
| {3, []feature{ | |
| {"avx", "HasAVX"}, | |
| {"avx2", "HasAVX2"}, | |
| {"bmi1", "HasBMI1"}, | |
| {"bmi2", "HasBMI2"}, | |
| {"fma", "HasFMA"}, | |
| }}, | |
| {4, []feature{ | |
| {"avx512f", "HasAVX512F"}, | |
| {"avx512cd", "HasAVX512CD"}, | |
| {"avx512bw", "HasAVX512BW"}, | |
| {"avx512dq", "HasAVX512DQ"}, | |
| {"avx512vl", "HasAVX512VL"}, | |
| }}, | |
| } | |
| // derived lists the X86 fields that doDerived computes from other features. | |
| var derived = []struct { | |
| field string // field of X86 | |
| level int // lowest GOAMD64 level that guarantees the features it's derived from | |
| }{ | |
| {"HasAVX512", 4}, | |
| } | |
| func main() { | |
| var b bytes.Buffer | |
| fmt.Fprintf(&b, `// Code generated by mkoptions.go; DO NOT EDIT. | |
| //go:build 386 || amd64 | |
| package cpu | |
| // appendLevelOptions appends the GODEBUG options for the features that are not | |
| // guaranteed at the given GOAMD64 level. GODEBUG can't turn off the features | |
| // that are guaranteed at level. | |
| func appendLevelOptions(options []option, level int32) []option { | |
| `) | |
| for _, l := range levels { | |
| fmt.Fprintf(&b, "\tif level < %d {\n\t\toptions = append(options,\n", l.level) | |
| for _, f := range l.features { | |
| fmt.Fprintf(&b, "\t\t\toption{Name: %q, Feature: &X86.%s},\n", f.option, f.field) | |
| } | |
| fmt.Fprintf(&b, "\t\t)\n\t}\n") | |
| } | |
| fmt.Fprintf(&b, "\treturn options\n}\n") | |
| writeFile("goamd64_gen.go", b.Bytes()) | |
| b.Reset() | |
| fmt.Fprintf(&b, `// Code generated by internal/cpu/mkoptions.go; DO NOT EDIT. | |
| package ssacompile | |
| // x86FeatureLevels maps each internal/cpu.X86 feature flag that some GOAMD64 | |
| // level guarantees to the lowest such level. internal/cpu doesn't let GODEBUG | |
| // turn these flags off at that level, and the runtime refuses to start on a | |
| // CPU without them, so they are always true. | |
| var x86FeatureLevels = map[string]int{ | |
| `) | |
| for _, l := range levels { | |
| for _, f := range l.features { | |
| fmt.Fprintf(&b, "\t%q: %d,\n", "X86."+f.field, l.level) | |
| } | |
| } | |
| for _, d := range derived { | |
| fmt.Fprintf(&b, "\t%q: %d,\n", "X86."+d.field, d.level) | |
| } | |
| fmt.Fprintf(&b, "}\n") | |
| writeFile("../../cmd/compile/internal/ssacompile/goamd64_gen.go", b.Bytes()) | |
| } | |
| func writeFile(path string, src []byte) { | |
| src, err := format.Source(src) | |
| if err != nil { | |
| log.Fatalf("formatting %s: %v", path, err) | |
| } | |
| if err := os.WriteFile(path, src, 0666); err != nil { | |
| log.Fatal(err) | |
| } | |
| } |