| 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 |
// Experiment.baseline 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", "loong64", "ppc64le", "ppc64", "riscv64": |
| 66 |
regabiAlwaysOn = true |
| 67 |
regabiSupported = true |
| 68 |
case "s390x": |
| 69 |
regabiSupported = true |
| 70 |
} |
| 71 |
|
| 72 |
// Older versions (anything before V16) of dsymutil don't handle |
| 73 |
// the .debug_rnglists section in DWARF5. See |
| 74 |
// https://github.com/golang/go/issues/26379#issuecomment-2677068742 |
| 75 |
// for more context. This disables all DWARF5 on mac, which is not |
| 76 |
// ideal (would be better to disable just for cases where we know |
| 77 |
// the build will use external linking). In the GOOS=aix case, the |
| 78 |
// XCOFF format (as far as can be determined) doesn't seem to |
| 79 |
// support the necessary section subtypes for DWARF-specific |
| 80 |
// things like .debug_addr (needed for DWARF 5). |
| 81 |
dwarf5Supported := (goos != "darwin" && goos != "ios" && goos != "aix") |
| 82 |
|
| 83 |
baseline := goexperiment.Flags{ |
| 84 |
RegabiWrappers: regabiSupported, |
| 85 |
RegabiArgs: regabiSupported, |
| 86 |
Dwarf5: dwarf5Supported, |
| 87 |
RandomizedHeapBase64: true, |
| 88 |
GreenTeaGC: true, |
| 89 |
} |
| 90 |
flags := &ExperimentFlags{ |
| 91 |
Flags: baseline, |
| 92 |
baseline: baseline, |
| 93 |
} |
| 94 |
|
| 95 |
// Pick up any changes to the baseline configuration from the |
| 96 |
// GOEXPERIMENT environment. This can be set at make.bash time |
| 97 |
// and overridden at build time. |
| 98 |
if goexp != "" { |
| 99 |
// Create a map of known experiment names. |
| 100 |
names := make(map[string]func(bool)) |
| 101 |
rv := reflect.ValueOf(&flags.Flags).Elem() |
| 102 |
rt := rv.Type() |
| 103 |
for i := 0; i < rt.NumField(); i++ { |
| 104 |
field := rv.Field(i) |
| 105 |
names[strings.ToLower(rt.Field(i).Name)] = field.SetBool |
| 106 |
} |
| 107 |
|
| 108 |
// "regabi" is an alias for all working regabi |
| 109 |
// subexperiments, and not an experiment itself. Doing |
| 110 |
// this as an alias make both "regabi" and "noregabi" |
| 111 |
// do the right thing. |
| 112 |
names["regabi"] = func(v bool) { |
| 113 |
flags.RegabiWrappers = v |
| 114 |
flags.RegabiArgs = v |
| 115 |
} |
| 116 |
|
| 117 |
// Parse names. |
| 118 |
for f := range strings.SplitSeq(goexp, ",") { |
| 119 |
if f == "" { |
| 120 |
continue |
| 121 |
} |
| 122 |
if f == "none" { |
| 123 |
// GOEXPERIMENT=none disables all experiment flags. |
| 124 |
// This is used by cmd/dist, which doesn't know how |
| 125 |
// to build with any experiment flags. |
| 126 |
flags.Flags = goexperiment.Flags{} |
| 127 |
continue |
| 128 |
} |
| 129 |
val := true |
| 130 |
if strings.HasPrefix(f, "no") { |
| 131 |
f, val = f[2:], false |
| 132 |
} |
| 133 |
set, ok := names[f] |
| 134 |
if !ok { |
| 135 |
return nil, fmt.Errorf("unknown GOEXPERIMENT %s", f) |
| 136 |
} |
| 137 |
set(val) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if regabiAlwaysOn { |
| 142 |
flags.RegabiWrappers = true |
| 143 |
flags.RegabiArgs = true |
| 144 |
} |
| 145 |
// regabi is only supported on amd64, arm64, loong64, riscv64, s390x, ppc64 and ppc64le. |
| 146 |
if !regabiSupported { |
| 147 |
flags.RegabiWrappers = false |
| 148 |
flags.RegabiArgs = false |
| 149 |
} |
| 150 |
// Check regabi dependencies. |
| 151 |
if flags.RegabiArgs && !flags.RegabiWrappers { |
| 152 |
return nil, fmt.Errorf("GOEXPERIMENT regabiargs requires regabiwrappers") |
| 153 |
} |
| 154 |
return flags, nil |
| 155 |
} |
| 156 |
|
| 157 |
// String returns the canonical GOEXPERIMENT string to enable this experiment |
| 158 |
// configuration. (Experiments in the same state as in the baseline are elided.) |
| 159 |
func (exp *ExperimentFlags) String() string { |
| 160 |
return strings.Join(expList(&exp.Flags, &exp.baseline, false), ",") |
| 161 |
} |
| 162 |
|
| 163 |
// expList returns the list of lower-cased experiment names for |
| 164 |
// experiments that differ from base. base may be nil to indicate no |
| 165 |
// experiments. If all is true, then include all experiment flags, |
| 166 |
// regardless of base. |
| 167 |
func expList(exp, base *goexperiment.Flags, all bool) []string { |
| 168 |
var list []string |
| 169 |
rv := reflect.ValueOf(exp).Elem() |
| 170 |
var rBase reflect.Value |
| 171 |
if base != nil { |
| 172 |
rBase = reflect.ValueOf(base).Elem() |
| 173 |
} |
| 174 |
rt := rv.Type() |
| 175 |
for i := 0; i < rt.NumField(); i++ { |
| 176 |
name := strings.ToLower(rt.Field(i).Name) |
| 177 |
val := rv.Field(i).Bool() |
| 178 |
baseVal := false |
| 179 |
if base != nil { |
| 180 |
baseVal = rBase.Field(i).Bool() |
| 181 |
} |
| 182 |
if all || val != baseVal { |
| 183 |
if val { |
| 184 |
list = append(list, name) |
| 185 |
} else { |
| 186 |
list = append(list, "no"+name) |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
return list |
| 191 |
} |
| 192 |
|
| 193 |
// Enabled returns a list of enabled experiments, as |
| 194 |
// lower-cased experiment names. |
| 195 |
func (exp *ExperimentFlags) Enabled() []string { |
| 196 |
return expList(&exp.Flags, nil, false) |
| 197 |
} |
| 198 |
|
| 199 |
// All returns a list of all experiment settings. |
| 200 |
// Disabled experiments appear in the list prefixed by "no". |
| 201 |
func (exp *ExperimentFlags) All() []string { |
| 202 |
return expList(&exp.Flags, nil, true) |
| 203 |
} |