Commit 55e83f8fbb for golang
commit 55e83f8fbb2794503340e7e25e85f95421103bb0
Author: David Chase <drchase@google.com>
Date: Sat Nov 28 19:42:20 2026 -0500
cmd/compile: fix AVX-512 IsNaN mask combine rules
Two AVX-512 rewrite rules in AMD64.rules intended to fold
x.IsNaN().Or(y.IsNaN()) into a single unordered comparison
(VCMPPD512/VCMPPS512 with predicate 3) mistakenly matched KANDB and
KANDW instead of KORB and KORW. The KANDB rule also wrote
(VCMPPD512 [3] x x) instead of (VCMPPD512 [3] x y), dropping y.
Fix the rules to match KORB and KORW and produce (VCMPPD512 [3] x y)
and (VCMPPS512 [3] x y), and regenerate rewriteAMD64.go.
Fixes #83471
Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-goamd64v4
Change-Id: I714f5c8b35e666321c55eca7aba41df106f81995
Reviewed-on: https://go-review.googlesource.com/c/go/+/866870
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
diff --git a/src/cmd/compile/internal/ssa/_gen/AMD64.rules b/src/cmd/compile/internal/ssa/_gen/AMD64.rules
index 4ea2789770..40f229fb93 100644
--- a/src/cmd/compile/internal/ssa/_gen/AMD64.rules
+++ b/src/cmd/compile/internal/ssa/_gen/AMD64.rules
@@ -1873,8 +1873,8 @@
(VPMOVMToVec64x8 (VCMPPD512 [3] x y))
// Include these rules because you never know about rewrite order
-(KANDB (VCMPPD512 [3] x x) (VCMPPD512 [3] y y)) => (VCMPPD512 [3] x x) // 512 = 64x8 -> KANDB
-(KANDW (VCMPPS512 [3] x x) (VCMPPS512 [3] y y)) => (VCMPPS512 [3] x y) // 512 = 32x16 -> KANDW
+(KORB (VCMPPD512 [3] x x) (VCMPPD512 [3] y y)) => (VCMPPD512 [3] x y) // 512 = 64x8 -> KORB
+(KORW (VCMPPS512 [3] x x) (VCMPPS512 [3] y y)) => (VCMPPS512 [3] x y) // 512 = 32x16 -> KORW
// These larger simplifying rules must come before the smaller simplifying rules that might break them).
// Note that (VPANDN x y) computes ^x & y, while (KANDN x y) computes x & ^y,
diff --git a/src/cmd/compile/internal/ssa/rewrite/amd64/rewriteAMD64.go b/src/cmd/compile/internal/ssa/rewrite/amd64/rewriteAMD64.go
index 1c51bd0400..a90d8c625d 100644
--- a/src/cmd/compile/internal/ssa/rewrite/amd64/rewriteAMD64.go
+++ b/src/cmd/compile/internal/ssa/rewrite/amd64/rewriteAMD64.go
@@ -263,10 +263,6 @@ func RewriteValue(v *ssa.Value) bool {
return rewriteValue_OpAMD64HMULQ(v)
case ssaop.OpAMD64HMULQU:
return rewriteValue_OpAMD64HMULQU(v)
- case ssaop.OpAMD64KANDB:
- return rewriteValue_OpAMD64KANDB(v)
- case ssaop.OpAMD64KANDW:
- return rewriteValue_OpAMD64KANDW(v)
case ssaop.OpAMD64KMOVBk:
return rewriteValue_OpAMD64KMOVBk(v)
case ssaop.OpAMD64KMOVDk:
@@ -283,6 +279,10 @@ func RewriteValue(v *ssa.Value) bool {
return rewriteValue_OpAMD64KNOTQ(v)
case ssaop.OpAMD64KNOTW:
return rewriteValue_OpAMD64KNOTW(v)
+ case ssaop.OpAMD64KORB:
+ return rewriteValue_OpAMD64KORB(v)
+ case ssaop.OpAMD64KORW:
+ return rewriteValue_OpAMD64KORW(v)
case ssaop.OpAMD64KXORB:
return rewriteValue_OpAMD64KXORB(v)
case ssaop.OpAMD64KXORD:
@@ -24587,54 +24587,6 @@ func rewriteValue_OpAMD64HMULQU(v *ssa.Value) bool {
}
return false
}
-func rewriteValue_OpAMD64KANDB(v *ssa.Value) bool {
- v_1 := v.Args[1]
- v_0 := v.Args[0]
- // match: (KANDB (VCMPPD512 [3] x x) (VCMPPD512 [3] y y))
- // result: (VCMPPD512 [3] x x)
- for {
- if v_0.Op != ssaop.OpAMD64VCMPPD512 || ssa.AuxIntToUint8(v_0.AuxInt) != 3 {
- break
- }
- x := v_0.Args[1]
- if x != v_0.Args[0] || v_1.Op != ssaop.OpAMD64VCMPPD512 || ssa.AuxIntToUint8(v_1.AuxInt) != 3 {
- break
- }
- y := v_1.Args[1]
- if y != v_1.Args[0] {
- break
- }
- v.Reset(ssaop.OpAMD64VCMPPD512)
- v.AuxInt = ssa.Uint8ToAuxInt(3)
- v.AddArg2(x, x)
- return true
- }
- return false
-}
-func rewriteValue_OpAMD64KANDW(v *ssa.Value) bool {
- v_1 := v.Args[1]
- v_0 := v.Args[0]
- // match: (KANDW (VCMPPS512 [3] x x) (VCMPPS512 [3] y y))
- // result: (VCMPPS512 [3] x y)
- for {
- if v_0.Op != ssaop.OpAMD64VCMPPS512 || ssa.AuxIntToUint8(v_0.AuxInt) != 3 {
- break
- }
- x := v_0.Args[1]
- if x != v_0.Args[0] || v_1.Op != ssaop.OpAMD64VCMPPS512 || ssa.AuxIntToUint8(v_1.AuxInt) != 3 {
- break
- }
- y := v_1.Args[1]
- if y != v_1.Args[0] {
- break
- }
- v.Reset(ssaop.OpAMD64VCMPPS512)
- v.AuxInt = ssa.Uint8ToAuxInt(3)
- v.AddArg2(x, y)
- return true
- }
- return false
-}
func rewriteValue_OpAMD64KMOVBk(v *ssa.Value) bool {
v_0 := v.Args[0]
// match: (KMOVBk l:(MOVBload [off] {sym} ptr mem))
@@ -24799,6 +24751,54 @@ func rewriteValue_OpAMD64KNOTW(v *ssa.Value) bool {
}
return false
}
+func rewriteValue_OpAMD64KORB(v *ssa.Value) bool {
+ v_1 := v.Args[1]
+ v_0 := v.Args[0]
+ // match: (KORB (VCMPPD512 [3] x x) (VCMPPD512 [3] y y))
+ // result: (VCMPPD512 [3] x y)
+ for {
+ if v_0.Op != ssaop.OpAMD64VCMPPD512 || ssa.AuxIntToUint8(v_0.AuxInt) != 3 {
+ break
+ }
+ x := v_0.Args[1]
+ if x != v_0.Args[0] || v_1.Op != ssaop.OpAMD64VCMPPD512 || ssa.AuxIntToUint8(v_1.AuxInt) != 3 {
+ break
+ }
+ y := v_1.Args[1]
+ if y != v_1.Args[0] {
+ break
+ }
+ v.Reset(ssaop.OpAMD64VCMPPD512)
+ v.AuxInt = ssa.Uint8ToAuxInt(3)
+ v.AddArg2(x, y)
+ return true
+ }
+ return false
+}
+func rewriteValue_OpAMD64KORW(v *ssa.Value) bool {
+ v_1 := v.Args[1]
+ v_0 := v.Args[0]
+ // match: (KORW (VCMPPS512 [3] x x) (VCMPPS512 [3] y y))
+ // result: (VCMPPS512 [3] x y)
+ for {
+ if v_0.Op != ssaop.OpAMD64VCMPPS512 || ssa.AuxIntToUint8(v_0.AuxInt) != 3 {
+ break
+ }
+ x := v_0.Args[1]
+ if x != v_0.Args[0] || v_1.Op != ssaop.OpAMD64VCMPPS512 || ssa.AuxIntToUint8(v_1.AuxInt) != 3 {
+ break
+ }
+ y := v_1.Args[1]
+ if y != v_1.Args[0] {
+ break
+ }
+ v.Reset(ssaop.OpAMD64VCMPPS512)
+ v.AuxInt = ssa.Uint8ToAuxInt(3)
+ v.AddArg2(x, y)
+ return true
+ }
+ return false
+}
func rewriteValue_OpAMD64KXORB(v *ssa.Value) bool {
v_1 := v.Args[1]
v_0 := v.Args[0]
diff --git a/src/simd/archsimd/internal/simd_test/compare_amd64_test.go b/src/simd/archsimd/internal/simd_test/compare_amd64_test.go
index 46821e1c3b..8d4e8330a4 100644
--- a/src/simd/archsimd/internal/simd_test/compare_amd64_test.go
+++ b/src/simd/archsimd/internal/simd_test/compare_amd64_test.go
@@ -7,6 +7,7 @@
package simd_test
import (
+ "math"
"simd/archsimd"
"testing"
)
@@ -246,3 +247,34 @@ func TestIsNaN(t *testing.T) {
}, want64)
}
}
+
+//go:noinline
+func bothNaN64x8(xs, ys []float64) archsimd.Mask64x8 {
+ return archsimd.LoadFloat64x8(xs).IsNaN().And(archsimd.LoadFloat64x8(ys).IsNaN())
+}
+
+//go:noinline
+func bothNaN32x16(xs, ys []float32) archsimd.Mask32x16 {
+ return archsimd.LoadFloat32x16(xs).IsNaN().And(archsimd.LoadFloat32x16(ys).IsNaN())
+}
+
+func TestIsNaNAnd(t *testing.T) {
+ // this is a regression test for #83471
+ if !archsimd.X86.AVX512() {
+ t.Skip("CPU lacks AVX-512")
+ }
+
+ nan64 := math.NaN()
+ x64 := []float64{nan64, 1, nan64, 1, 1, 1, 1, 1}
+ y64 := []float64{1, 1, 1, 1, 1, 1, 1, 1}
+ if m := bothNaN64x8(x64, y64); m.Any() {
+ t.Errorf("Float64x8 IsNaN().And(): got mask %s (Any=true), want all false", m)
+ }
+
+ nan32 := float32(math.NaN())
+ x32 := []float32{2, nan32, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
+ y32 := []float32{3, 3, 3, 3, 3, 3, 3, 3, 3, nan32, 3, 3, 3, 3, 3, 3}
+ if m := bothNaN32x16(x32, y32); m.Any() {
+ t.Errorf("Float32x16 IsNaN().And(): got mask %s (Any=true), want all false", m)
+ }
+}