[go] internal/cpu: disallow disabling options that are required for microarch

26 views
Skip to first unread message

Keith Randall (Gerrit)

unread,
Mar 10, 2022, 6:27:46 PM3/10/22
to Martin Möhrmann, goph...@pubsubhelper.golang.org, Keith Randall, golang-co...@googlegroups.com

Attention is currently required from: Martin Möhrmann.

Keith Randall would like Martin Möhrmann to review this change.

View Change

internal/cpu: disallow disabling options that are required for microarch

e.g., if GOAMD64=v3, don't allow GODEBUG=cpu.XXX=off for XXX which
are required for v3.

Change-Id: Ib58a4c8b13c5464ba476448ba44bbb261218787c
---
M src/internal/cpu/cpu_x86.go
M src/internal/cpu/cpu_x86.s
2 files changed, 53 insertions(+), 10 deletions(-)

diff --git a/src/internal/cpu/cpu_x86.go b/src/internal/cpu/cpu_x86.go
index 81d5cee..6fd979a 100644
--- a/src/internal/cpu/cpu_x86.go
+++ b/src/internal/cpu/cpu_x86.go
@@ -14,6 +14,9 @@
// xgetbv with ecx = 0 is implemented in cpu_x86.s.
func xgetbv() (eax, edx uint32)

+// getGOAMD64level is implemented in cpu_x86.s. Returns number in [1,4].
+func getGOAMD64level() int32
+
const (
// edx bits
cpuid_SSE2 = 1 << 26
@@ -47,19 +50,30 @@
options = []option{
{Name: "adx", Feature: &X86.HasADX},
{Name: "aes", Feature: &X86.HasAES},
- {Name: "avx", Feature: &X86.HasAVX},
- {Name: "avx2", Feature: &X86.HasAVX2},
- {Name: "bmi1", Feature: &X86.HasBMI1},
- {Name: "bmi2", Feature: &X86.HasBMI2},
{Name: "erms", Feature: &X86.HasERMS},
- {Name: "fma", Feature: &X86.HasFMA},
{Name: "pclmulqdq", Feature: &X86.HasPCLMULQDQ},
- {Name: "popcnt", Feature: &X86.HasPOPCNT},
{Name: "rdtscp", Feature: &X86.HasRDTSCP},
- {Name: "sse3", Feature: &X86.HasSSE3},
- {Name: "sse41", Feature: &X86.HasSSE41},
- {Name: "sse42", Feature: &X86.HasSSE42},
- {Name: "ssse3", Feature: &X86.HasSSSE3},
+ }
+ level := getGOAMD64level()
+ if level < 2 {
+ // These options are required at level 2. At lower levels
+ // they can be turned off.
+ options = append(options,
+ option{Name: "popcnt", Feature: &X86.HasPOPCNT},
+ option{Name: "sse3", Feature: &X86.HasSSE3},
+ option{Name: "sse41", Feature: &X86.HasSSE41},
+ option{Name: "sse42", Feature: &X86.HasSSE42},
+ option{Name: "ssse3", Feature: &X86.HasSSSE3})
+ }
+ if level < 3 {
+ // These options are required at level 3. At lower levels
+ // they can be turned off.
+ options = append(options,
+ option{Name: "avx", Feature: &X86.HasAVX},
+ option{Name: "avx2", Feature: &X86.HasAVX2},
+ option{Name: "bmi1", Feature: &X86.HasBMI1},
+ option{Name: "bmi2", Feature: &X86.HasBMI2},
+ option{Name: "fma", Feature: &X86.HasFMA})
}

maxID, _, _, _ := cpuid(0, 0)
diff --git a/src/internal/cpu/cpu_x86.s b/src/internal/cpu/cpu_x86.s
index edef219..2ee8eca 100644
--- a/src/internal/cpu/cpu_x86.s
+++ b/src/internal/cpu/cpu_x86.s
@@ -24,3 +24,20 @@
MOVL AX, eax+0(FP)
MOVL DX, edx+4(FP)
RET
+
+// func getGOAMD64level() int32
+TEXT ·getGOAMD64level(SB),NOSPLIT,$0-4
+#ifdef GOAMD64_v4
+ MOVL $4, ret+0(FP)
+#else
+#ifdef GOAMD64_v3
+ MOVL $3, ret+0(FP)
+#else
+#ifdef GOAMD64_v2
+ MOVL $2, ret+0(FP)
+#else
+ MOVL $1, ret+0(FP)
+#endif
+#endif
+#endif
+ RET

To view, visit change 391694. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ib58a4c8b13c5464ba476448ba44bbb261218787c
Gerrit-Change-Number: 391694
Gerrit-PatchSet: 1
Gerrit-Owner: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
Gerrit-Attention: Martin Möhrmann <moeh...@google.com>
Gerrit-MessageType: newchange

Keith Randall (Gerrit)

unread,
Mar 10, 2022, 6:29:57 PM3/10/22
to Keith Randall, goph...@pubsubhelper.golang.org, Gopher Robot, Martin Möhrmann, golang-co...@googlegroups.com

Attention is currently required from: Martin Möhrmann.

View Change

1 comment:

  • Patchset:

    • Patch Set #1:

      This CL will lead to getting "unknown cpu feature" for features that are required and you're trying to modify. But this is kind of a specialized feature anyway, I don't think that's a big downside.

      We'd also need a similar change to x/sys, I guess?

To view, visit change 391694. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ib58a4c8b13c5464ba476448ba44bbb261218787c
Gerrit-Change-Number: 391694
Gerrit-PatchSet: 1
Gerrit-Owner: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Martin Möhrmann <moeh...@google.com>
Gerrit-Comment-Date: Thu, 10 Mar 2022 23:29:53 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Martin Möhrmann (Gerrit)

unread,
Mar 14, 2022, 5:06:51 PM3/14/22
to Keith Randall, goph...@pubsubhelper.golang.org, Gopher Robot, Martin Möhrmann, golang-co...@googlegroups.com

Attention is currently required from: Martin Möhrmann, Keith Randall.

Patch set 1:Code-Review +2

View Change

    To view, visit change 391694. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ib58a4c8b13c5464ba476448ba44bbb261218787c
    Gerrit-Change-Number: 391694
    Gerrit-PatchSet: 1
    Gerrit-Owner: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Martin Möhrmann <mar...@golang.org>
    Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
    Gerrit-Attention: Martin Möhrmann <moeh...@google.com>
    Gerrit-Attention: Keith Randall <k...@golang.org>
    Gerrit-Comment-Date: Mon, 14 Mar 2022 21:06:44 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Keith Randall (Gerrit)

    unread,
    Mar 14, 2022, 5:23:41 PM3/14/22
    to Keith Randall, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Martin Möhrmann, Gopher Robot, Martin Möhrmann, golang-co...@googlegroups.com

    Keith Randall submitted this change.

    View Change


    Approvals: Martin Möhrmann: Looks good to me, approved Keith Randall: Trusted; Run TryBots Gopher Robot: TryBots succeeded
    internal/cpu: disallow disabling options that are required for microarch

    e.g., if GOAMD64=v3, don't allow GODEBUG=cpu.XXX=off for XXX which
    are required for v3.

    Change-Id: Ib58a4c8b13c5464ba476448ba44bbb261218787c
    Reviewed-on: https://go-review.googlesource.com/c/go/+/391694
    Trust: Keith Randall <k...@golang.org>
    Run-TryBot: Keith Randall <k...@golang.org>
    TryBot-Result: Gopher Robot <go...@golang.org>
    Reviewed-by: Martin Möhrmann <mar...@golang.org>

    ---
    M src/internal/cpu/cpu_x86.go
    M src/internal/cpu/cpu_x86.s
    2 files changed, 58 insertions(+), 10 deletions(-)

    To view, visit change 391694. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ib58a4c8b13c5464ba476448ba44bbb261218787c
    Gerrit-Change-Number: 391694
    Gerrit-PatchSet: 2
    Gerrit-Owner: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Martin Möhrmann <mar...@golang.org>
    Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
    Gerrit-MessageType: merged

    Tobias Klauser (Gerrit)

    unread,
    Mar 15, 2022, 4:20:37 AM3/15/22
    to Keith Randall, goph...@pubsubhelper.golang.org, Martin Möhrmann, Gopher Robot, Martin Möhrmann, golang-co...@googlegroups.com

    View Change

    1 comment:

    To view, visit change 391694. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ib58a4c8b13c5464ba476448ba44bbb261218787c
    Gerrit-Change-Number: 391694
    Gerrit-PatchSet: 2
    Gerrit-Owner: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Martin Möhrmann <mar...@golang.org>
    Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
    Gerrit-CC: Tobias Klauser <tobias....@gmail.com>
    Gerrit-Comment-Date: Tue, 15 Mar 2022 08:20:31 +0000
    Reply all
    Reply to author
    Forward
    0 new messages