1
0
mirror of https://github.com/golang/go synced 2024-11-11 20:50:23 -07:00

cmd/internal/objabi: clarify initialization of Experiments

Currently objabi.Experiments is set via side-effect from an init
function, which makes their initialization process somewhat unclear
(indeed, I've messed this up before) and opens the possibility of
accessing them from another init function before it's initialized.

Originally, this init function set several variables, but at this
point it sets only objabi.Experiments, so switch to just using a
variable initializer to make the initialization process clear.

Change-Id: Id0d2ac76ae463824bbf37a9305e8643a275f1365
Reviewed-on: https://go-review.googlesource.com/c/go/+/307821
Trust: Austin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Austin Clements 2021-04-06 16:31:40 -04:00
parent 5159c83641
commit 8752454ece

View File

@ -18,7 +18,7 @@ import (
//
// (This is not necessarily the set of experiments the compiler itself
// was built with.)
var Experiment goexperiment.Flags
var Experiment goexperiment.Flags = parseExperiments()
// FramePointerEnabled enables the use of platform conventions for
// saving frame pointers.
@ -29,9 +29,9 @@ var Experiment goexperiment.Flags
// Note: must agree with runtime.framepointer_enabled.
var FramePointerEnabled = GOARCH == "amd64" || GOARCH == "arm64"
func init() {
// Start with the baseline configuration.
Experiment = goexperiment.BaselineFlags
func parseExperiments() goexperiment.Flags {
// Start with the statically enabled set of experiments.
flags := goexperiment.BaselineFlags
// Pick up any changes to the baseline configuration from the
// GOEXPERIMENT environment. This can be set at make.bash time
@ -41,7 +41,7 @@ func init() {
if env != "" {
// Create a map of known experiment names.
names := make(map[string]reflect.Value)
rv := reflect.ValueOf(&Experiment).Elem()
rv := reflect.ValueOf(&flags).Elem()
rt := rv.Type()
for i := 0; i < rt.NumField(); i++ {
field := rv.Field(i)
@ -56,7 +56,7 @@ func init() {
if f == "none" {
// GOEXPERIMENT=none restores the baseline configuration.
// (This is useful for overriding make.bash-time settings.)
Experiment = goexperiment.BaselineFlags
flags = goexperiment.BaselineFlags
continue
}
val := true
@ -74,29 +74,30 @@ func init() {
// regabi is only supported on amd64.
if GOARCH != "amd64" {
Experiment.Regabi = false
Experiment.RegabiWrappers = false
Experiment.RegabiG = false
Experiment.RegabiReflect = false
Experiment.RegabiDefer = false
Experiment.RegabiArgs = false
flags.Regabi = false
flags.RegabiWrappers = false
flags.RegabiG = false
flags.RegabiReflect = false
flags.RegabiDefer = false
flags.RegabiArgs = false
}
// Setting regabi sets working sub-experiments.
if Experiment.Regabi {
Experiment.RegabiWrappers = true
Experiment.RegabiG = true
Experiment.RegabiReflect = true
Experiment.RegabiDefer = true
if flags.Regabi {
flags.RegabiWrappers = true
flags.RegabiG = true
flags.RegabiReflect = true
flags.RegabiDefer = true
// Not ready yet:
//Experiment.RegabiArgs = true
//flags.RegabiArgs = true
}
// Check regabi dependencies.
if Experiment.RegabiG && !Experiment.RegabiWrappers {
if flags.RegabiG && !flags.RegabiWrappers {
panic("GOEXPERIMENT regabig requires regabiwrappers")
}
if Experiment.RegabiArgs && !(Experiment.RegabiWrappers && Experiment.RegabiG && Experiment.RegabiReflect && Experiment.RegabiDefer) {
if flags.RegabiArgs && !(flags.RegabiWrappers && flags.RegabiG && flags.RegabiReflect && flags.RegabiDefer) {
panic("GOEXPERIMENT regabiargs requires regabiwrappers,regabig,regabireflect,regabidefer")
}
return flags
}
// expList returns the list of lower-cased experiment names for