Source file src/internal/cpu/mkoptions.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  //go:build ignore
     6  
     7  // Mkoptions generates the lists of the X86 features that each GOAMD64 level
     8  // requires: goamd64_gen.go, which lists their GODEBUG options, and the
     9  // compiler's cmd/compile/internal/ssacompile/goamd64_gen.go, which lists the
    10  // fields whose loads the compiler can replace with true. Generating both from
    11  // one table keeps them in sync.
    12  package main
    13  
    14  import (
    15  	"bytes"
    16  	"fmt"
    17  	"go/format"
    18  	"log"
    19  	"os"
    20  )
    21  
    22  type feature struct {
    23  	option string // GODEBUG option name
    24  	field  string // field of X86
    25  }
    26  
    27  // levels lists the X86 features that each GOAMD64 level requires, beyond the
    28  // ones that the levels below it require.
    29  var levels = []struct {
    30  	level    int
    31  	features []feature
    32  }{
    33  	{2, []feature{
    34  		{"popcnt", "HasPOPCNT"},
    35  		{"sse3", "HasSSE3"},
    36  		{"sse41", "HasSSE41"},
    37  		{"sse42", "HasSSE42"},
    38  		{"ssse3", "HasSSSE3"},
    39  	}},
    40  	{3, []feature{
    41  		{"avx", "HasAVX"},
    42  		{"avx2", "HasAVX2"},
    43  		{"bmi1", "HasBMI1"},
    44  		{"bmi2", "HasBMI2"},
    45  		{"fma", "HasFMA"},
    46  	}},
    47  	{4, []feature{
    48  		{"avx512f", "HasAVX512F"},
    49  		{"avx512cd", "HasAVX512CD"},
    50  		{"avx512bw", "HasAVX512BW"},
    51  		{"avx512dq", "HasAVX512DQ"},
    52  		{"avx512vl", "HasAVX512VL"},
    53  	}},
    54  }
    55  
    56  // derived lists the X86 fields that doDerived computes from other features.
    57  var derived = []struct {
    58  	field string // field of X86
    59  	level int    // lowest GOAMD64 level that guarantees the features it's derived from
    60  }{
    61  	{"HasAVX512", 4},
    62  }
    63  
    64  func main() {
    65  	var b bytes.Buffer
    66  	fmt.Fprintf(&b, `// Code generated by mkoptions.go; DO NOT EDIT.
    67  
    68  //go:build 386 || amd64
    69  
    70  package cpu
    71  
    72  // appendLevelOptions appends the GODEBUG options for the features that are not
    73  // guaranteed at the given GOAMD64 level. GODEBUG can't turn off the features
    74  // that are guaranteed at level.
    75  func appendLevelOptions(options []option, level int32) []option {
    76  `)
    77  	for _, l := range levels {
    78  		fmt.Fprintf(&b, "\tif level < %d {\n\t\toptions = append(options,\n", l.level)
    79  		for _, f := range l.features {
    80  			fmt.Fprintf(&b, "\t\t\toption{Name: %q, Feature: &X86.%s},\n", f.option, f.field)
    81  		}
    82  		fmt.Fprintf(&b, "\t\t)\n\t}\n")
    83  	}
    84  	fmt.Fprintf(&b, "\treturn options\n}\n")
    85  	writeFile("goamd64_gen.go", b.Bytes())
    86  
    87  	b.Reset()
    88  	fmt.Fprintf(&b, `// Code generated by internal/cpu/mkoptions.go; DO NOT EDIT.
    89  
    90  package ssacompile
    91  
    92  // x86FeatureLevels maps each internal/cpu.X86 feature flag that some GOAMD64
    93  // level guarantees to the lowest such level. internal/cpu doesn't let GODEBUG
    94  // turn these flags off at that level, and the runtime refuses to start on a
    95  // CPU without them, so they are always true.
    96  var x86FeatureLevels = map[string]int{
    97  `)
    98  	for _, l := range levels {
    99  		for _, f := range l.features {
   100  			fmt.Fprintf(&b, "\t%q: %d,\n", "X86."+f.field, l.level)
   101  		}
   102  	}
   103  	for _, d := range derived {
   104  		fmt.Fprintf(&b, "\t%q: %d,\n", "X86."+d.field, d.level)
   105  	}
   106  	fmt.Fprintf(&b, "}\n")
   107  	writeFile("../../cmd/compile/internal/ssacompile/goamd64_gen.go", b.Bytes())
   108  }
   109  
   110  func writeFile(path string, src []byte) {
   111  	src, err := format.Source(src)
   112  	if err != nil {
   113  		log.Fatalf("formatting %s: %v", path, err)
   114  	}
   115  	if err := os.WriteFile(path, src, 0666); err != nil {
   116  		log.Fatal(err)
   117  	}
   118  }
   119  

View as plain text