]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.simd] simd, cmd/compile: mark simd vectors uncomparable
authorCherry Mui <cherryyz@google.com>
Mon, 22 Sep 2025 15:57:19 +0000 (11:57 -0400)
committerCherry Mui <cherryyz@google.com>
Thu, 25 Sep 2025 17:32:03 +0000 (10:32 -0700)
SIMD vector types are opqaue, and are expected to be operated with
methods. It is not always possible to compare the two vectors
efficiently. Instead of adding more magic to the compiler to
handle the == operator, mark the vector types uncomparable.

Change-Id: I4ca5d5e80ca7d8992dffa7b3c0386b75eb19cfa8
Reviewed-on: https://go-review.googlesource.com/c/go/+/705855
Reviewed-by: Junyang Shao <shaojunyang@google.com>
TryBot-Bypass: Cherry Mui <cherryyz@google.com>
Reviewed-by: David Chase <drchase@google.com>
src/cmd/compile/internal/types/size.go
src/simd/_gen/simdgen/gen_simdTypes.go
src/simd/internal/simd_test/simd_test.go
src/simd/types_amd64.go

index 2aa437b56ffe7e26c013e8c7b09aba628aff1041..a4ec67e4637e0c5fc506354f3e168796362db333 100644 (file)
@@ -465,7 +465,7 @@ func CalcSize(t *Type) {
 // by the compiler except for the space that they reserve.
 func simdify(st *Type, isTag bool) {
        st.align = 8
-       st.alg = AMEM
+       st.alg = ANOALG // not comparable with ==
        st.intRegs = 0
        st.isSIMD = true
        if isTag {
index 22d19be0e2acd5333d7709946b9340498b7ff977..0d5d08b7edd08d2f7251ba2f8e2eb4ef1e5025b2 100644 (file)
@@ -129,7 +129,7 @@ const simdTypesTemplates = `
 {{define "sizeTmpl"}}
 // v{{.}} is a tag type that tells the compiler that this is really {{.}}-bit SIMD
 type v{{.}} struct {
-       _{{.}} struct{}
+       _{{.}} [0]func() // uncomparable
 }
 {{end}}
 
index e43bea1e1221a62b2091bbc57b01a1a160973714..f05c6d6f66e88b5f22f5be09a8f2d54a0509704e 100644 (file)
@@ -54,6 +54,20 @@ func TestType(t *testing.T) {
        }
 }
 
+func TestUncomparable(t *testing.T) {
+       // Test that simd vectors are not comparable
+       var x, y any = simd.LoadUint32x4(&[4]uint32{1, 2, 3, 4}), simd.LoadUint32x4(&[4]uint32{5, 6, 7, 8})
+       shouldPanic := func(fn func()) {
+               defer func() {
+                       if recover() == nil {
+                               panic("did not panic")
+                       }
+               }()
+               fn()
+       }
+       shouldPanic(func() { _ = x == y })
+}
+
 func TestFuncValue(t *testing.T) {
        // Test that simd intrinsic can be used as a function value.
        xv := [4]int32{1, 2, 3, 4}
index f70a6a214b5f7a5283ae6b4f543d7ff5fe181261..72547c760275f2e336ee892d969fb6f954b5cc64 100644 (file)
@@ -6,7 +6,7 @@ package simd
 
 // v128 is a tag type that tells the compiler that this is really 128-bit SIMD
 type v128 struct {
-       _128 struct{}
+       _128 [0]func() // uncomparable
 }
 
 // Float32x4 is a 128-bit SIMD vector of 4 float32
@@ -433,7 +433,7 @@ func (x Mask64x2) ToBits() uint8
 
 // v256 is a tag type that tells the compiler that this is really 256-bit SIMD
 type v256 struct {
-       _256 struct{}
+       _256 [0]func() // uncomparable
 }
 
 // Float32x8 is a 256-bit SIMD vector of 8 float32
@@ -860,7 +860,7 @@ func (x Mask64x4) ToBits() uint8
 
 // v512 is a tag type that tells the compiler that this is really 512-bit SIMD
 type v512 struct {
-       _512 struct{}
+       _512 [0]func() // uncomparable
 }
 
 // Float32x16 is a 512-bit SIMD vector of 16 float32