Skip to content

Commit 7fc638d

Browse files
committed
cmd: move GOEXPERIMENT knob from make.bash to cmd/go
This CL changes GOEXPERIMENT to act like other GO[CONFIG] environment variables. Namely, that it can be set at make.bash time to provide a default value used by the toolchain, but then can be manually set when running either cmd/go or the individual tools (compiler, assembler, linker). For example, it's now possible to test rsc.io/tmp/fieldtrack by simply running: GOEXPERIMENT=fieldtrack go test -gcflags=-l rsc.io/tmp/fieldtrack \ -ldflags=-k=rsc.io/tmp/fieldtrack.tracked without needing to re-run make.bash. (-gcflags=-l is needed because the compiler's inlining abilities have improved, so calling a function with a for loop is no longer sufficient to suppress inlining.) Fixes #42681. Change-Id: I2cf8995d5d0d05f6785a2ee1d3b54b2cfb3331ca Reviewed-on: https://go-review.googlesource.com/c/go/+/300991 Trust: Matthew Dempsky <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent b3896fc commit 7fc638d

File tree

12 files changed

+51
-48
lines changed

12 files changed

+51
-48
lines changed

src/cmd/asm/internal/lex/input.go

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ func NewInput(name string) *Input {
4545
// predefine installs the macros set by the -D flag on the command line.
4646
func predefine(defines flags.MultiFlag) map[string]*Macro {
4747
macros := make(map[string]*Macro)
48+
49+
if *flags.CompilingRuntime && objabi.Regabi_enabled != 0 {
50+
const name = "GOEXPERIMENT_REGABI"
51+
macros[name] = &Macro{
52+
name: name,
53+
args: nil,
54+
tokens: Tokenize("1"),
55+
}
56+
}
57+
4858
for _, name := range defines {
4959
value := "1"
5060
i := strings.IndexRune(name, '=')

src/cmd/dist/build.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var (
3939
goextlinkenabled string
4040
gogcflags string // For running built compiler
4141
goldflags string
42+
goexperiment string
4243
workdir string
4344
tooldir string
4445
oldgoos string
@@ -194,6 +195,9 @@ func xinit() {
194195
goextlinkenabled = b
195196
}
196197

198+
goexperiment = os.Getenv("GOEXPERIMENT")
199+
// TODO(mdempsky): Validate known experiments?
200+
197201
gogcflags = os.Getenv("BOOT_GO_GCFLAGS")
198202
goldflags = os.Getenv("BOOT_GO_LDFLAGS")
199203

@@ -834,18 +838,6 @@ func runInstall(pkg string, ch chan struct{}) {
834838
goasmh := pathf("%s/go_asm.h", workdir)
835839
if IsRuntimePackagePath(pkg) {
836840
asmArgs = append(asmArgs, "-compiling-runtime")
837-
if os.Getenv("GOEXPERIMENT") == "regabi" {
838-
// In order to make it easier to port runtime assembly
839-
// to the register ABI, we introduce a macro
840-
// indicating the experiment is enabled.
841-
//
842-
// Note: a similar change also appears in
843-
// cmd/go/internal/work/gc.go.
844-
//
845-
// TODO(austin): Remove this once we commit to the
846-
// register ABI (#40724).
847-
asmArgs = append(asmArgs, "-D=GOEXPERIMENT_REGABI=1")
848-
}
849841
}
850842

851843
// Collect symabis from assembly code.

src/cmd/dist/buildruntime.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
// package sys
2121
//
2222
// const TheVersion = <version>
23-
// const Goexperiment = <goexperiment>
2423
// const StackGuardMultiplier = <multiplier value>
2524
//
2625
func mkzversion(dir, file string) {
@@ -30,7 +29,6 @@ func mkzversion(dir, file string) {
3029
fmt.Fprintf(&buf, "package sys\n")
3130
fmt.Fprintln(&buf)
3231
fmt.Fprintf(&buf, "const TheVersion = `%s`\n", findgoversion())
33-
fmt.Fprintf(&buf, "const Goexperiment = `%s`\n", os.Getenv("GOEXPERIMENT"))
3432
fmt.Fprintf(&buf, "const StackGuardMultiplierDefault = %d\n", stackGuardMultiplierDefault())
3533

3634
writefile(buf.String(), file, writeSkipSame)
@@ -48,10 +46,10 @@ func mkzversion(dir, file string) {
4846
// const defaultGOPPC64 = <goppc64>
4947
// const defaultGOOS = runtime.GOOS
5048
// const defaultGOARCH = runtime.GOARCH
49+
// const defaultGOEXPERIMENT = <goexperiment>
5150
// const defaultGO_EXTLINK_ENABLED = <goextlinkenabled>
5251
// const version = <version>
5352
// const stackGuardMultiplierDefault = <multiplier value>
54-
// const goexperiment = <goexperiment>
5553
//
5654
// The use of runtime.GOOS and runtime.GOARCH makes sure that
5755
// a cross-compiled compiler expects to compile for its own target
@@ -77,11 +75,11 @@ func mkzbootstrap(file string) {
7775
fmt.Fprintf(&buf, "const defaultGOPPC64 = `%s`\n", goppc64)
7876
fmt.Fprintf(&buf, "const defaultGOOS = runtime.GOOS\n")
7977
fmt.Fprintf(&buf, "const defaultGOARCH = runtime.GOARCH\n")
78+
fmt.Fprintf(&buf, "const defaultGOEXPERIMENT = `%s`\n", goexperiment)
8079
fmt.Fprintf(&buf, "const defaultGO_EXTLINK_ENABLED = `%s`\n", goextlinkenabled)
8180
fmt.Fprintf(&buf, "const defaultGO_LDSO = `%s`\n", defaultldso)
8281
fmt.Fprintf(&buf, "const version = `%s`\n", findgoversion())
8382
fmt.Fprintf(&buf, "const stackGuardMultiplierDefault = %d\n", stackGuardMultiplierDefault())
84-
fmt.Fprintf(&buf, "const goexperiment = `%s`\n", os.Getenv("GOEXPERIMENT"))
8583

8684
writefile(buf.String(), file, writeSkipSame)
8785
}

src/cmd/go/internal/cfg/cfg.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,13 @@ var (
252252
GOMODCACHE = envOr("GOMODCACHE", gopathDir("pkg/mod"))
253253

254254
// Used in envcmd.MkEnv and build ID computations.
255-
GOARM = envOr("GOARM", fmt.Sprint(objabi.GOARM))
256-
GO386 = envOr("GO386", objabi.GO386)
257-
GOMIPS = envOr("GOMIPS", objabi.GOMIPS)
258-
GOMIPS64 = envOr("GOMIPS64", objabi.GOMIPS64)
259-
GOPPC64 = envOr("GOPPC64", fmt.Sprintf("%s%d", "power", objabi.GOPPC64))
260-
GOWASM = envOr("GOWASM", fmt.Sprint(objabi.GOWASM))
255+
GOARM = envOr("GOARM", fmt.Sprint(objabi.GOARM))
256+
GO386 = envOr("GO386", objabi.GO386)
257+
GOMIPS = envOr("GOMIPS", objabi.GOMIPS)
258+
GOMIPS64 = envOr("GOMIPS64", objabi.GOMIPS64)
259+
GOPPC64 = envOr("GOPPC64", fmt.Sprintf("%s%d", "power", objabi.GOPPC64))
260+
GOWASM = envOr("GOWASM", fmt.Sprint(objabi.GOWASM))
261+
GOEXPERIMENT = envOr("GOEXPERIMENT", objabi.GOEXPERIMENT)
261262

262263
GOPROXY = envOr("GOPROXY", "https://proxy.golang.org,direct")
263264
GOSUMDB = envOr("GOSUMDB", "sum.golang.org")

src/cmd/go/internal/work/exec.go

+8
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID {
276276
key, val := cfg.GetArchEnv()
277277
fmt.Fprintf(h, "%s=%s\n", key, val)
278278

279+
if exp := cfg.Getenv("GOEXPERIMENT"); exp != "" {
280+
fmt.Fprintf(h, "GOEXPERIMENT=%q\n", exp)
281+
}
282+
279283
// TODO(rsc): Convince compiler team not to add more magic environment variables,
280284
// or perhaps restrict the environment variables passed to subprocesses.
281285
// Because these are clumsy, undocumented special-case hacks
@@ -1246,6 +1250,10 @@ func (b *Builder) printLinkerConfig(h io.Writer, p *load.Package) {
12461250
key, val := cfg.GetArchEnv()
12471251
fmt.Fprintf(h, "%s=%s\n", key, val)
12481252

1253+
if exp := cfg.Getenv("GOEXPERIMENT"); exp != "" {
1254+
fmt.Fprintf(h, "GOEXPERIMENT=%q\n", exp)
1255+
}
1256+
12491257
// The linker writes source file paths that say GOROOT_FINAL, but
12501258
// only if -trimpath is not specified (see ld() in gc.go).
12511259
gorootFinal := cfg.GOROOT_FINAL

src/cmd/go/internal/work/gc.go

-12
Original file line numberDiff line numberDiff line change
@@ -343,18 +343,6 @@ func asmArgs(a *Action, p *load.Package) []interface{} {
343343
}
344344
if objabi.IsRuntimePackagePath(pkgpath) {
345345
args = append(args, "-compiling-runtime")
346-
if objabi.Regabi_enabled != 0 {
347-
// In order to make it easier to port runtime assembly
348-
// to the register ABI, we introduce a macro
349-
// indicating the experiment is enabled.
350-
//
351-
// Note: a similar change also appears in
352-
// cmd/dist/build.go.
353-
//
354-
// TODO(austin): Remove this once we commit to the
355-
// register ABI (#40724).
356-
args = append(args, "-D=GOEXPERIMENT_REGABI=1")
357-
}
358346
}
359347

360348
if cfg.Goarch == "mips" || cfg.Goarch == "mipsle" {

src/cmd/internal/objabi/util.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ func envOr(key, value string) string {
2121
var (
2222
defaultGOROOT string // set by linker
2323

24-
GOROOT = envOr("GOROOT", defaultGOROOT)
25-
GOARCH = envOr("GOARCH", defaultGOARCH)
26-
GOOS = envOr("GOOS", defaultGOOS)
27-
GO386 = envOr("GO386", defaultGO386)
28-
GOARM = goarm()
29-
GOMIPS = gomips()
30-
GOMIPS64 = gomips64()
31-
GOPPC64 = goppc64()
32-
GOWASM = gowasm()
33-
GO_LDSO = defaultGO_LDSO
34-
Version = version
24+
GOROOT = envOr("GOROOT", defaultGOROOT)
25+
GOARCH = envOr("GOARCH", defaultGOARCH)
26+
GOOS = envOr("GOOS", defaultGOOS)
27+
GOEXPERIMENT = envOr("GOEXPERIMENT", defaultGOEXPERIMENT)
28+
GO386 = envOr("GO386", defaultGO386)
29+
GOARM = goarm()
30+
GOMIPS = gomips()
31+
GOMIPS64 = gomips64()
32+
GOPPC64 = goppc64()
33+
GOWASM = gowasm()
34+
GO_LDSO = defaultGO_LDSO
35+
Version = version
3536
)
3637

3738
const (
@@ -124,7 +125,7 @@ func Getgoextlinkenabled() string {
124125
}
125126

126127
func init() {
127-
for _, f := range strings.Split(goexperiment, ",") {
128+
for _, f := range strings.Split(GOEXPERIMENT, ",") {
128129
if f != "" {
129130
addexp(f)
130131
}

src/cmd/link/internal/ld/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ func Main(arch *sys.Arch, theArch Arch) {
119119
addstrdata1(ctxt, "runtime.defaultGOROOT="+final)
120120
addstrdata1(ctxt, "cmd/internal/objabi.defaultGOROOT="+final)
121121

122+
addstrdata1(ctxt, "runtime/internal/sys.GOEXPERIMENT="+objabi.GOEXPERIMENT)
123+
122124
// TODO(matloob): define these above and then check flag values here
123125
if ctxt.Arch.Family == sys.AMD64 && objabi.GOOS == "plan9" {
124126
flag.BoolVar(&flag8, "8", false, "use 64-bit addresses in symbol table")

src/internal/cfg/cfg.go

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const KnownEnv = `
3939
GOCACHE
4040
GOENV
4141
GOEXE
42+
GOEXPERIMENT
4243
GOFLAGS
4344
GOGCCFLAGS
4445
GOHOSTARCH

src/runtime/heapdump.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ func dumpparams() {
532532
dumpint(uint64(arenaStart))
533533
dumpint(uint64(arenaEnd))
534534
dumpstr(sys.GOARCH)
535-
dumpstr(sys.Goexperiment)
535+
dumpstr(sys.GOEXPERIMENT)
536536
dumpint(uint64(ncpu))
537537
}
538538

src/runtime/internal/sys/arch.go

+2
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,5 @@ const MinFrameSize = _MinFrameSize
5252
// StackAlign is the required alignment of the SP register.
5353
// The stack must be at least word aligned, but some architectures require more.
5454
const StackAlign = _StackAlign
55+
56+
var GOEXPERIMENT string // set by cmd/link

src/runtime/proc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6027,7 +6027,7 @@ func setMaxThreads(in int) (out int) {
60276027
}
60286028

60296029
func haveexperiment(name string) bool {
6030-
x := sys.Goexperiment
6030+
x := sys.GOEXPERIMENT
60316031
for x != "" {
60326032
xname := ""
60336033
i := bytealg.IndexByteString(x, ',')

0 commit comments

Comments
 (0)