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.
12 "internal/goexperiment"
15 // ExperimentFlags represents a set of GOEXPERIMENT flags relative to a baseline
16 // (platform-default) experiment configuration.
17 type ExperimentFlags struct {
19 baseline goexperiment.Flags
22 // Experiment contains the toolchain experiments enabled for the
25 // (This is not necessarily the set of experiments the compiler itself
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))
35 return ExperimentFlags{}
40 // DefaultGOEXPERIMENT is the embedded default GOEXPERIMENT string.
41 // It is not guaranteed to be canonical.
42 const DefaultGOEXPERIMENT = defaultGOEXPERIMENT
44 // FramePointerEnabled enables the use of platform conventions for
45 // saving frame pointers.
47 // This used to be an experiment, but now it's always enabled on
48 // platforms that support it.
50 // Note: must agree with runtime.framepointer_enabled.
51 var FramePointerEnabled = GOARCH == "amd64" || GOARCH == "arm64"
53 // ParseGOEXPERIMENT parses a (GOOS, GOARCH, GOEXPERIMENT)
54 // configuration tuple and returns the enabled and baseline experiment
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
63 var regabiSupported, regabiAlwaysOn bool
65 case "amd64", "arm64", "ppc64le", "ppc64", "riscv64":
67 regabiSupported = true
70 baseline := goexperiment.Flags{
71 RegabiWrappers: regabiSupported,
72 RegabiArgs: regabiSupported,
73 CoverageRedesign: true,
76 // Start with the statically enabled set of experiments.
77 flags := &ExperimentFlags{
82 // Pick up any changes to the baseline configuration from the
83 // GOEXPERIMENT environment. This can be set at make.bash time
84 // and overridden at build time.
86 // Create a map of known experiment names.
87 names := make(map[string]func(bool))
88 rv := reflect.ValueOf(&flags.Flags).Elem()
90 for i := 0; i < rt.NumField(); i++ {
92 names[strings.ToLower(rt.Field(i).Name)] = field.SetBool
95 // "regabi" is an alias for all working regabi
96 // subexperiments, and not an experiment itself. Doing
97 // this as an alias make both "regabi" and "noregabi"
98 // do the right thing.
99 names["regabi"] = func(v bool) {
100 flags.RegabiWrappers = v
105 for _, f := range strings.Split(goexp, ",") {
110 // GOEXPERIMENT=none disables all experiment flags.
111 // This is used by cmd/dist, which doesn't know how
112 // to build with any experiment flags.
113 flags.Flags = goexperiment.Flags{}
117 if strings.HasPrefix(f, "no") {
118 f, val = f[2:], false
122 return nil, fmt.Errorf("unknown GOEXPERIMENT %s", f)
129 flags.RegabiWrappers = true
130 flags.RegabiArgs = true
132 // regabi is only supported on amd64, arm64, riscv64, ppc64 and ppc64le.
133 if !regabiSupported {
134 flags.RegabiWrappers = false
135 flags.RegabiArgs = false
137 // Check regabi dependencies.
138 if flags.RegabiArgs && !flags.RegabiWrappers {
139 return nil, fmt.Errorf("GOEXPERIMENT regabiargs requires regabiwrappers")
144 // String returns the canonical GOEXPERIMENT string to enable this experiment
145 // configuration. (Experiments in the same state as in the baseline are elided.)
146 func (exp *ExperimentFlags) String() string {
147 return strings.Join(expList(&exp.Flags, &exp.baseline, false), ",")
150 // expList returns the list of lower-cased experiment names for
151 // experiments that differ from base. base may be nil to indicate no
152 // experiments. If all is true, then include all experiment flags,
153 // regardless of base.
154 func expList(exp, base *goexperiment.Flags, all bool) []string {
156 rv := reflect.ValueOf(exp).Elem()
157 var rBase reflect.Value
159 rBase = reflect.ValueOf(base).Elem()
162 for i := 0; i < rt.NumField(); i++ {
163 name := strings.ToLower(rt.Field(i).Name)
164 val := rv.Field(i).Bool()
167 baseVal = rBase.Field(i).Bool()
169 if all || val != baseVal {
171 list = append(list, name)
173 list = append(list, "no"+name)
180 // Enabled returns a list of enabled experiments, as
181 // lower-cased experiment names.
182 func (exp *ExperimentFlags) Enabled() []string {
183 return expList(&exp.Flags, nil, false)
186 // All returns a list of all experiment settings.
187 // Disabled experiments appear in the list prefixed by "no".
188 func (exp *ExperimentFlags) All() []string {
189 return expList(&exp.Flags, nil, true)