From 5a78e1a4a1c79185e86b5c18efffba2a9b9d3739 Mon Sep 17 00:00:00 2001 From: Cherry Mui Date: Mon, 22 Sep 2025 11:57:19 -0400 Subject: [PATCH] [dev.simd] simd, cmd/compile: mark simd vectors uncomparable 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 TryBot-Bypass: Cherry Mui Reviewed-by: David Chase --- src/cmd/compile/internal/types/size.go | 2 +- src/simd/_gen/simdgen/gen_simdTypes.go | 2 +- src/simd/internal/simd_test/simd_test.go | 14 ++++++++++++++ src/simd/types_amd64.go | 6 +++--- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/cmd/compile/internal/types/size.go b/src/cmd/compile/internal/types/size.go index 2aa437b56f..a4ec67e463 100644 --- a/src/cmd/compile/internal/types/size.go +++ b/src/cmd/compile/internal/types/size.go @@ -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 { diff --git a/src/simd/_gen/simdgen/gen_simdTypes.go b/src/simd/_gen/simdgen/gen_simdTypes.go index 22d19be0e2..0d5d08b7ed 100644 --- a/src/simd/_gen/simdgen/gen_simdTypes.go +++ b/src/simd/_gen/simdgen/gen_simdTypes.go @@ -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}} diff --git a/src/simd/internal/simd_test/simd_test.go b/src/simd/internal/simd_test/simd_test.go index e43bea1e12..f05c6d6f66 100644 --- a/src/simd/internal/simd_test/simd_test.go +++ b/src/simd/internal/simd_test/simd_test.go @@ -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} diff --git a/src/simd/types_amd64.go b/src/simd/types_amd64.go index f70a6a214b..72547c7602 100644 --- a/src/simd/types_amd64.go +++ b/src/simd/types_amd64.go @@ -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 -- 2.52.0