Skip to content

Commit b1bedc0

Browse files
nimelehinmdempsky
authored andcommitted
cmd/go: add GOAMD64 environment variable
The variable represents the microarchitecture level for which to compile. Valid values are v1 (default), v2, v3, v4. Updates #45453 Change-Id: I095197fc9239d79f98896d7e745e2341354daca4 GitHub-Last-Rev: f83ed17 GitHub-Pull-Request: #48359 Reviewed-on: https://go-review.googlesource.com/c/go/+/349595 Reviewed-by: Matthew Dempsky <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Bryan C. Mills <[email protected]>
1 parent 04f5116 commit b1bedc0

File tree

10 files changed

+71
-1
lines changed

10 files changed

+71
-1
lines changed

src/cmd/dist/build.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var (
3232
goos string
3333
goarm string
3434
go386 string
35+
goamd64 string
3536
gomips string
3637
gomips64 string
3738
goppc64 string
@@ -145,6 +146,12 @@ func xinit() {
145146
}
146147
go386 = b
147148

149+
b = os.Getenv("GOAMD64")
150+
if b == "" {
151+
b = "v1"
152+
}
153+
goamd64 = b
154+
148155
b = os.Getenv("GOMIPS")
149156
if b == "" {
150157
b = "hardfloat"
@@ -217,6 +224,7 @@ func xinit() {
217224

218225
// For tools being invoked but also for os.ExpandEnv.
219226
os.Setenv("GO386", go386)
227+
os.Setenv("GOAMD64", goamd64)
220228
os.Setenv("GOARCH", goarch)
221229
os.Setenv("GOARM", goarm)
222230
os.Setenv("GOHOSTARCH", gohostarch)
@@ -1181,6 +1189,9 @@ func cmdenv() {
11811189
if goarch == "386" {
11821190
xprintf(format, "GO386", go386)
11831191
}
1192+
if goarch == "amd64" {
1193+
xprintf(format, "GOAMD64", goamd64)
1194+
}
11841195
if goarch == "mips" || goarch == "mipsle" {
11851196
xprintf(format, "GOMIPS", gomips)
11861197
}

src/cmd/dist/buildruntime.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func mkbuildcfg(file string) {
6060
fmt.Fprintf(&buf, "import \"runtime\"\n")
6161
fmt.Fprintln(&buf)
6262
fmt.Fprintf(&buf, "const defaultGO386 = `%s`\n", go386)
63+
fmt.Fprintf(&buf, "const defaultGOAMD64 = `%s`\n", goamd64)
6364
fmt.Fprintf(&buf, "const defaultGOARM = `%s`\n", goarm)
6465
fmt.Fprintf(&buf, "const defaultGOMIPS = `%s`\n", gomips)
6566
fmt.Fprintf(&buf, "const defaultGOMIPS64 = `%s`\n", gomips64)

src/cmd/go/alldocs.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ var (
263263
// Used in envcmd.MkEnv and build ID computations.
264264
GOARM = envOr("GOARM", fmt.Sprint(buildcfg.GOARM))
265265
GO386 = envOr("GO386", buildcfg.GO386)
266+
GOAMD64 = envOr("GOAMD64", fmt.Sprintf("%s%d", "v", buildcfg.GOAMD64))
266267
GOMIPS = envOr("GOMIPS", buildcfg.GOMIPS)
267268
GOMIPS64 = envOr("GOMIPS64", buildcfg.GOMIPS64)
268269
GOPPC64 = envOr("GOPPC64", fmt.Sprintf("%s%d", "power", buildcfg.GOPPC64))
@@ -289,6 +290,8 @@ func GetArchEnv() (key, val string) {
289290
return "GOARM", GOARM
290291
case "386":
291292
return "GO386", GO386
293+
case "amd64":
294+
return "GOAMD64", GOAMD64
292295
case "mips", "mipsle":
293296
return "GOMIPS", GOMIPS
294297
case "mips64", "mips64le":

src/cmd/go/internal/help/helpdoc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,10 @@ Architecture-specific environment variables:
592592
GO386
593593
For GOARCH=386, how to implement floating point instructions.
594594
Valid values are sse2 (default), softfloat.
595+
GOAMD64
596+
For GOARCH=GOAMD64, the microarchitecture level for which to compile.
597+
Valid values are v1 (default), v2, v3, v4.
598+
See https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levels.
595599
GOMIPS
596600
For GOARCH=mips{,le}, whether to use floating point instructions.
597601
Valid values are hardfloat (default), softfloat.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,11 @@ func asmArgs(a *Action, p *load.Package) []interface{} {
379379
args = append(args, "-D", "GO386_"+cfg.GO386)
380380
}
381381

382+
if cfg.Goarch == "amd64" {
383+
// Define GOAMD64_value from cfg.GOAMD64.
384+
args = append(args, "-D", "GOAMD64_"+cfg.GOAMD64)
385+
}
386+
382387
if cfg.Goarch == "mips" || cfg.Goarch == "mipsle" {
383388
// Define GOMIPS_value from cfg.GOMIPS.
384389
args = append(args, "-D", "GOMIPS_"+cfg.GOMIPS)

src/internal/buildcfg/cfg.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var (
2525
GOARCH = envOr("GOARCH", defaultGOARCH)
2626
GOOS = envOr("GOOS", defaultGOOS)
2727
GO386 = envOr("GO386", defaultGO386)
28+
GOAMD64 = goamd64()
2829
GOARM = goarm()
2930
GOMIPS = gomips()
3031
GOMIPS64 = gomips64()
@@ -52,6 +53,21 @@ func envOr(key, value string) string {
5253
return value
5354
}
5455

56+
func goamd64() int {
57+
switch v := envOr("GOAMD64", defaultGOAMD64); v {
58+
case "v1":
59+
return 1
60+
case "v2":
61+
return 2
62+
case "v3":
63+
return 3
64+
case "v4":
65+
return 4
66+
}
67+
Error = fmt.Errorf("invalid GOAMD64: must be v1, v2, v3, v4")
68+
return int(defaultGOAMD64[len("v")] - '0')
69+
}
70+
5571
func goarm() int {
5672
def := defaultGOARM
5773
if GOOS == "android" && GOARCH == "arm" {

src/internal/buildcfg/cfg_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
"os"
9+
"testing"
10+
)
11+
12+
func TestConfigFlags(t *testing.T) {
13+
os.Setenv("GOAMD64", "v1")
14+
if goamd64() != 1 {
15+
t.Errorf("Wrong parsing of GOAMD64=v1")
16+
}
17+
os.Setenv("GOAMD64", "v4")
18+
if goamd64() != 4 {
19+
t.Errorf("Wrong parsing of GOAMD64=v4")
20+
}
21+
os.Setenv("GOAMD64", "1")
22+
if goamd64() != 1 {
23+
t.Errorf("Wrong parsing of GOAMD64=1")
24+
}
25+
}

src/internal/cfg/cfg.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const KnownEnv = `
3333
GCCGO
3434
GO111MODULE
3535
GO386
36+
GOAMD64
3637
GOARCH
3738
GOARM
3839
GOBIN

test/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1753,7 +1753,7 @@ var (
17531753
// are the supported variants.
17541754
archVariants = map[string][]string{
17551755
"386": {"GO386", "sse2", "softfloat"},
1756-
"amd64": {},
1756+
"amd64": {"GOAMD64", "v1", "v2", "v3", "v4"},
17571757
"arm": {"GOARM", "5", "6", "7"},
17581758
"arm64": {},
17591759
"mips": {"GOMIPS", "hardfloat", "softfloat"},

0 commit comments

Comments
 (0)