github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/internal/buildcfg/exp.go (about) 1 // Copyright 2021 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 buildcfg 6 7 import ( 8 "fmt" 9 "reflect" 10 "strings" 11 12 "internal/goexperiment" 13 ) 14 15 // ExperimentFlags represents a set of GOEXPERIMENT flags relative to a baseline 16 // (platform-default) experiment configuration. 17 type ExperimentFlags struct { 18 goexperiment.Flags 19 baseline goexperiment.Flags 20 } 21 22 // Experiment contains the toolchain experiments enabled for the 23 // current build. 24 // 25 // (This is not necessarily the set of experiments the compiler itself 26 // was built with.) 27 // 28 // experimentBaseline specifies the experiment flags that are enabled by 29 // default in the current toolchain. This is, in effect, the "control" 30 // configuration and any variation from this is an experiment. 31 var Experiment ExperimentFlags = func() ExperimentFlags { 32 flags, err := ParseGOEXPERIMENT(GOOS, GOARCH, envOr("GOEXPERIMENT", defaultGOEXPERIMENT)) 33 if err != nil { 34 Error = err 35 return ExperimentFlags{} 36 } 37 return *flags 38 }() 39 40 // DefaultGOEXPERIMENT is the embedded default GOEXPERIMENT string. 41 // It is not guaranteed to be canonical. 42 const DefaultGOEXPERIMENT = defaultGOEXPERIMENT 43 44 // FramePointerEnabled enables the use of platform conventions for 45 // saving frame pointers. 46 // 47 // This used to be an experiment, but now it's always enabled on 48 // platforms that support it. 49 // 50 // Note: must agree with runtime.framepointer_enabled. 51 var FramePointerEnabled = GOARCH == "amd64" || GOARCH == "arm64" 52 53 // ParseGOEXPERIMENT parses a (GOOS, GOARCH, GOEXPERIMENT) 54 // configuration tuple and returns the enabled and baseline experiment 55 // flag sets. 56 // 57 // TODO(mdempsky): Move to internal/goexperiment. 58 func ParseGOEXPERIMENT(goos, goarch, goexp string) (*ExperimentFlags, error) { 59 // regabiSupported is set to true on platforms where register ABI is 60 // supported and enabled by default. 61 // regabiAlwaysOn is set to true on platforms where register ABI is 62 // always on. 63 var regabiSupported, regabiAlwaysOn bool 64 switch goarch { 65 case "amd64", "arm64", "ppc64le", "ppc64": 66 regabiAlwaysOn = true 67 regabiSupported = true 68 case "riscv64": 69 regabiSupported = true 70 } 71 72 baseline := goexperiment.Flags{ 73 RegabiWrappers: regabiSupported, 74 RegabiArgs: regabiSupported, 75 Unified: true, 76 CoverageRedesign: true, 77 } 78 79 // Start with the statically enabled set of experiments. 80 flags := &ExperimentFlags{ 81 Flags: baseline, 82 baseline: baseline, 83 } 84 85 // Pick up any changes to the baseline configuration from the 86 // GOEXPERIMENT environment. This can be set at make.bash time 87 // and overridden at build time. 88 if goexp != "" { 89 // Create a map of known experiment names. 90 names := make(map[string]func(bool)) 91 rv := reflect.ValueOf(&flags.Flags).Elem() 92 rt := rv.Type() 93 for i := 0; i < rt.NumField(); i++ { 94 field := rv.Field(i) 95 names[strings.ToLower(rt.Field(i).Name)] = field.SetBool 96 } 97 98 // "regabi" is an alias for all working regabi 99 // subexperiments, and not an experiment itself. Doing 100 // this as an alias make both "regabi" and "noregabi" 101 // do the right thing. 102 names["regabi"] = func(v bool) { 103 flags.RegabiWrappers = v 104 flags.RegabiArgs = v 105 } 106 107 // Parse names. 108 for _, f := range strings.Split(goexp, ",") { 109 if f == "" { 110 continue 111 } 112 if f == "none" { 113 // GOEXPERIMENT=none disables all experiment flags. 114 // This is used by cmd/dist, which doesn't know how 115 // to build with any experiment flags. 116 flags.Flags = goexperiment.Flags{} 117 continue 118 } 119 val := true 120 if strings.HasPrefix(f, "no") { 121 f, val = f[2:], false 122 } 123 set, ok := names[f] 124 if !ok { 125 return nil, fmt.Errorf("unknown GOEXPERIMENT %s", f) 126 } 127 set(val) 128 } 129 } 130 131 if regabiAlwaysOn { 132 flags.RegabiWrappers = true 133 flags.RegabiArgs = true 134 } 135 // regabi is only supported on amd64, arm64, riscv64, ppc64 and ppc64le. 136 if !regabiSupported { 137 flags.RegabiWrappers = false 138 flags.RegabiArgs = false 139 } 140 // Check regabi dependencies. 141 if flags.RegabiArgs && !flags.RegabiWrappers { 142 return nil, fmt.Errorf("GOEXPERIMENT regabiargs requires regabiwrappers") 143 } 144 return flags, nil 145 } 146 147 // String returns the canonical GOEXPERIMENT string to enable this experiment 148 // configuration. (Experiments in the same state as in the baseline are elided.) 149 func (exp *ExperimentFlags) String() string { 150 return strings.Join(expList(&exp.Flags, &exp.baseline, false), ",") 151 } 152 153 // expList returns the list of lower-cased experiment names for 154 // experiments that differ from base. base may be nil to indicate no 155 // experiments. If all is true, then include all experiment flags, 156 // regardless of base. 157 func expList(exp, base *goexperiment.Flags, all bool) []string { 158 var list []string 159 rv := reflect.ValueOf(exp).Elem() 160 var rBase reflect.Value 161 if base != nil { 162 rBase = reflect.ValueOf(base).Elem() 163 } 164 rt := rv.Type() 165 for i := 0; i < rt.NumField(); i++ { 166 name := strings.ToLower(rt.Field(i).Name) 167 val := rv.Field(i).Bool() 168 baseVal := false 169 if base != nil { 170 baseVal = rBase.Field(i).Bool() 171 } 172 if all || val != baseVal { 173 if val { 174 list = append(list, name) 175 } else { 176 list = append(list, "no"+name) 177 } 178 } 179 } 180 return list 181 } 182 183 // Enabled returns a list of enabled experiments, as 184 // lower-cased experiment names. 185 func (exp *ExperimentFlags) Enabled() []string { 186 return expList(&exp.Flags, nil, false) 187 } 188 189 // All returns a list of all experiment settings. 190 // Disabled experiments appear in the list prefixed by "no". 191 func (exp *ExperimentFlags) All() []string { 192 return expList(&exp.Flags, nil, true) 193 }