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

View as plain text