Source file src/internal/buildcfg/exp.go

     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", "loong64", "ppc64le", "ppc64", "riscv64":
    66  		regabiAlwaysOn = true
    67  		regabiSupported = true
    68  	}
    69  
    70  	baseline := goexperiment.Flags{
    71  		RegabiWrappers:   regabiSupported,
    72  		RegabiArgs:       regabiSupported,
    73  		CoverageRedesign: true,
    74  		AliasTypeParams:  true,
    75  		SwissMap:         true,
    76  	}
    77  
    78  	// Start with the statically enabled set of experiments.
    79  	flags := &ExperimentFlags{
    80  		Flags:    baseline,
    81  		baseline: baseline,
    82  	}
    83  
    84  	// Pick up any changes to the baseline configuration from the
    85  	// GOEXPERIMENT environment. This can be set at make.bash time
    86  	// and overridden at build time.
    87  	if goexp != "" {
    88  		// Create a map of known experiment names.
    89  		names := make(map[string]func(bool))
    90  		rv := reflect.ValueOf(&flags.Flags).Elem()
    91  		rt := rv.Type()
    92  		for i := 0; i < rt.NumField(); i++ {
    93  			field := rv.Field(i)
    94  			names[strings.ToLower(rt.Field(i).Name)] = field.SetBool
    95  		}
    96  
    97  		// "regabi" is an alias for all working regabi
    98  		// subexperiments, and not an experiment itself. Doing
    99  		// this as an alias make both "regabi" and "noregabi"
   100  		// do the right thing.
   101  		names["regabi"] = func(v bool) {
   102  			flags.RegabiWrappers = v
   103  			flags.RegabiArgs = v
   104  		}
   105  
   106  		// Parse names.
   107  		for _, f := range strings.Split(goexp, ",") {
   108  			if f == "" {
   109  				continue
   110  			}
   111  			if f == "none" {
   112  				// GOEXPERIMENT=none disables all experiment flags.
   113  				// This is used by cmd/dist, which doesn't know how
   114  				// to build with any experiment flags.
   115  				flags.Flags = goexperiment.Flags{}
   116  				continue
   117  			}
   118  			val := true
   119  			if strings.HasPrefix(f, "no") {
   120  				f, val = f[2:], false
   121  			}
   122  			set, ok := names[f]
   123  			if !ok {
   124  				return nil, fmt.Errorf("unknown GOEXPERIMENT %s", f)
   125  			}
   126  			set(val)
   127  		}
   128  	}
   129  
   130  	if regabiAlwaysOn {
   131  		flags.RegabiWrappers = true
   132  		flags.RegabiArgs = true
   133  	}
   134  	// regabi is only supported on amd64, arm64, loong64, riscv64, ppc64 and ppc64le.
   135  	if !regabiSupported {
   136  		flags.RegabiWrappers = false
   137  		flags.RegabiArgs = false
   138  	}
   139  	// Check regabi dependencies.
   140  	if flags.RegabiArgs && !flags.RegabiWrappers {
   141  		return nil, fmt.Errorf("GOEXPERIMENT regabiargs requires regabiwrappers")
   142  	}
   143  	return flags, nil
   144  }
   145  
   146  // String returns the canonical GOEXPERIMENT string to enable this experiment
   147  // configuration. (Experiments in the same state as in the baseline are elided.)
   148  func (exp *ExperimentFlags) String() string {
   149  	return strings.Join(expList(&exp.Flags, &exp.baseline, false), ",")
   150  }
   151  
   152  // expList returns the list of lower-cased experiment names for
   153  // experiments that differ from base. base may be nil to indicate no
   154  // experiments. If all is true, then include all experiment flags,
   155  // regardless of base.
   156  func expList(exp, base *goexperiment.Flags, all bool) []string {
   157  	var list []string
   158  	rv := reflect.ValueOf(exp).Elem()
   159  	var rBase reflect.Value
   160  	if base != nil {
   161  		rBase = reflect.ValueOf(base).Elem()
   162  	}
   163  	rt := rv.Type()
   164  	for i := 0; i < rt.NumField(); i++ {
   165  		name := strings.ToLower(rt.Field(i).Name)
   166  		val := rv.Field(i).Bool()
   167  		baseVal := false
   168  		if base != nil {
   169  			baseVal = rBase.Field(i).Bool()
   170  		}
   171  		if all || val != baseVal {
   172  			if val {
   173  				list = append(list, name)
   174  			} else {
   175  				list = append(list, "no"+name)
   176  			}
   177  		}
   178  	}
   179  	return list
   180  }
   181  
   182  // Enabled returns a list of enabled experiments, as
   183  // lower-cased experiment names.
   184  func (exp *ExperimentFlags) Enabled() []string {
   185  	return expList(&exp.Flags, nil, false)
   186  }
   187  
   188  // All returns a list of all experiment settings.
   189  // Disabled experiments appear in the list prefixed by "no".
   190  func (exp *ExperimentFlags) All() []string {
   191  	return expList(&exp.Flags, nil, true)
   192  }
   193  

View as plain text