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

View as plain text