]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add GOAMD64 environment variable
authornimelehin <nimelehin@gmail.com>
Wed, 15 Sep 2021 04:30:49 +0000 (04:30 +0000)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 16 Sep 2021 17:44:44 +0000 (17:44 +0000)
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: f83ed17204606264073be5b9831f9d24f2f9dbc4
GitHub-Pull-Request: golang/go#48359
Reviewed-on: https://go-review.googlesource.com/c/go/+/349595
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Bryan C. Mills <bcmills@google.com>

src/cmd/dist/build.go
src/cmd/dist/buildruntime.go
src/cmd/go/alldocs.go
src/cmd/go/internal/cfg/cfg.go
src/cmd/go/internal/help/helpdoc.go
src/cmd/go/internal/work/gc.go
src/internal/buildcfg/cfg.go
src/internal/buildcfg/cfg_test.go [new file with mode: 0644]
src/internal/cfg/cfg.go
test/run.go

index 33a329e48b71712564be4ba8f1ae67655940a09c..39f016e3157b182b21e8ad93fd5f8de2cf0b4565 100644 (file)
@@ -32,6 +32,7 @@ var (
        goos             string
        goarm            string
        go386            string
+       goamd64          string
        gomips           string
        gomips64         string
        goppc64          string
@@ -145,6 +146,12 @@ func xinit() {
        }
        go386 = b
 
+       b = os.Getenv("GOAMD64")
+       if b == "" {
+               b = "v1"
+       }
+       goamd64 = b
+
        b = os.Getenv("GOMIPS")
        if b == "" {
                b = "hardfloat"
@@ -217,6 +224,7 @@ func xinit() {
 
        // For tools being invoked but also for os.ExpandEnv.
        os.Setenv("GO386", go386)
+       os.Setenv("GOAMD64", goamd64)
        os.Setenv("GOARCH", goarch)
        os.Setenv("GOARM", goarm)
        os.Setenv("GOHOSTARCH", gohostarch)
@@ -1181,6 +1189,9 @@ func cmdenv() {
        if goarch == "386" {
                xprintf(format, "GO386", go386)
        }
+       if goarch == "amd64" {
+               xprintf(format, "GOAMD64", goamd64)
+       }
        if goarch == "mips" || goarch == "mipsle" {
                xprintf(format, "GOMIPS", gomips)
        }
index 54e935ad3bec1d35c5bf37732ed1bb659965792c..fdc1d257744fcdba092d5db466acdcf4e321ac13 100644 (file)
@@ -60,6 +60,7 @@ func mkbuildcfg(file string) {
        fmt.Fprintf(&buf, "import \"runtime\"\n")
        fmt.Fprintln(&buf)
        fmt.Fprintf(&buf, "const defaultGO386 = `%s`\n", go386)
+       fmt.Fprintf(&buf, "const defaultGOAMD64 = `%s`\n", goamd64)
        fmt.Fprintf(&buf, "const defaultGOARM = `%s`\n", goarm)
        fmt.Fprintf(&buf, "const defaultGOMIPS = `%s`\n", gomips)
        fmt.Fprintf(&buf, "const defaultGOMIPS64 = `%s`\n", gomips64)
index b7e82127959b852b2fd7f7be5c625b2bf5a9fbe5..35c60744b87d15089b290ec405cabbde36e852af 100644 (file)
 //     GO386
 //             For GOARCH=386, how to implement floating point instructions.
 //             Valid values are sse2 (default), softfloat.
+//     GOAMD64
+//             For GOARCH=GOAMD64, the microarchitecture level for which to compile.
+//             Valid values are v1 (default), v2, v3, v4.
+//             See https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levels.
 //     GOMIPS
 //             For GOARCH=mips{,le}, whether to use floating point instructions.
 //             Valid values are hardfloat (default), softfloat.
index da616ee1dd5c9d7c75b921eadf6ff7652598b4d3..5f4465e06b1d45a2e0277d3596c325b84b759e96 100644 (file)
@@ -263,6 +263,7 @@ var (
        // Used in envcmd.MkEnv and build ID computations.
        GOARM    = envOr("GOARM", fmt.Sprint(buildcfg.GOARM))
        GO386    = envOr("GO386", buildcfg.GO386)
+       GOAMD64  = envOr("GOAMD64", fmt.Sprintf("%s%d", "v", buildcfg.GOAMD64))
        GOMIPS   = envOr("GOMIPS", buildcfg.GOMIPS)
        GOMIPS64 = envOr("GOMIPS64", buildcfg.GOMIPS64)
        GOPPC64  = envOr("GOPPC64", fmt.Sprintf("%s%d", "power", buildcfg.GOPPC64))
@@ -289,6 +290,8 @@ func GetArchEnv() (key, val string) {
                return "GOARM", GOARM
        case "386":
                return "GO386", GO386
+       case "amd64":
+               return "GOAMD64", GOAMD64
        case "mips", "mipsle":
                return "GOMIPS", GOMIPS
        case "mips64", "mips64le":
index 490ff1fb7cf05bd5a7e107c1ea40bb44174cc0db..91876cefe0c417d2bb3183c519dad32dc3240cf7 100644 (file)
@@ -592,6 +592,10 @@ Architecture-specific environment variables:
        GO386
                For GOARCH=386, how to implement floating point instructions.
                Valid values are sse2 (default), softfloat.
+       GOAMD64
+               For GOARCH=GOAMD64, the microarchitecture level for which to compile.
+               Valid values are v1 (default), v2, v3, v4.
+               See https://en.wikipedia.org/wiki/X86-64#Microarchitecture_levels.
        GOMIPS
                For GOARCH=mips{,le}, whether to use floating point instructions.
                Valid values are hardfloat (default), softfloat.
index 1cce5d4dd5d3be34b157c2dcb43eebca75e4d848..800800f7881319db4387c6cfcf9a9bb3378b1454 100644 (file)
@@ -379,6 +379,11 @@ func asmArgs(a *Action, p *load.Package) []interface{} {
                args = append(args, "-D", "GO386_"+cfg.GO386)
        }
 
+       if cfg.Goarch == "amd64" {
+               // Define GOAMD64_value from cfg.GOAMD64.
+               args = append(args, "-D", "GOAMD64_"+cfg.GOAMD64)
+       }
+
        if cfg.Goarch == "mips" || cfg.Goarch == "mipsle" {
                // Define GOMIPS_value from cfg.GOMIPS.
                args = append(args, "-D", "GOMIPS_"+cfg.GOMIPS)
index 9fe7f211fb3024951e24ce2f0f6f66f9572f2fb0..68c10a282465887f1b3b5aa2fb3f436888e8cf40 100644 (file)
@@ -25,6 +25,7 @@ var (
        GOARCH   = envOr("GOARCH", defaultGOARCH)
        GOOS     = envOr("GOOS", defaultGOOS)
        GO386    = envOr("GO386", defaultGO386)
+       GOAMD64  = goamd64()
        GOARM    = goarm()
        GOMIPS   = gomips()
        GOMIPS64 = gomips64()
@@ -52,6 +53,21 @@ func envOr(key, value string) string {
        return value
 }
 
+func goamd64() int {
+       switch v := envOr("GOAMD64", defaultGOAMD64); v {
+       case "v1":
+               return 1
+       case "v2":
+               return 2
+       case "v3":
+               return 3
+       case "v4":
+               return 4
+       }
+       Error = fmt.Errorf("invalid GOAMD64: must be v1, v2, v3, v4")
+       return int(defaultGOAMD64[len("v")] - '0')
+}
+
 func goarm() int {
        def := defaultGOARM
        if GOOS == "android" && GOARCH == "arm" {
diff --git a/src/internal/buildcfg/cfg_test.go b/src/internal/buildcfg/cfg_test.go
new file mode 100644 (file)
index 0000000..9180441
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package buildcfg
+
+import (
+       "os"
+       "testing"
+)
+
+func TestConfigFlags(t *testing.T) {
+       os.Setenv("GOAMD64", "v1")
+       if goamd64() != 1 {
+               t.Errorf("Wrong parsing of GOAMD64=v1")
+       }
+       os.Setenv("GOAMD64", "v4")
+       if goamd64() != 4 {
+               t.Errorf("Wrong parsing of GOAMD64=v4")
+       }
+       os.Setenv("GOAMD64", "1")
+       if goamd64() != 1 {
+               t.Errorf("Wrong parsing of GOAMD64=1")
+       }
+}
index 815994b679ac3c70658822a155d9981e4576da54..4cb3fbd4f342a4615a8483f352b45974b3a4ec79 100644 (file)
@@ -33,6 +33,7 @@ const KnownEnv = `
        GCCGO
        GO111MODULE
        GO386
+       GOAMD64
        GOARCH
        GOARM
        GOBIN
index 3fb87af397ce1953a5932b9a6f7555a492278388..7317e8019e7b2b7a1f1cb606e01ac0e87c5bb193 100644 (file)
@@ -1753,7 +1753,7 @@ var (
        // are the supported variants.
        archVariants = map[string][]string{
                "386":     {"GO386", "sse2", "softfloat"},
-               "amd64":   {},
+               "amd64":   {"GOAMD64", "v1", "v2", "v3", "v4"},
                "arm":     {"GOARM", "5", "6", "7"},
                "arm64":   {},
                "mips":    {"GOMIPS", "hardfloat", "softfloat"},