Commit 8661cc1ede for golang
commit 8661cc1edeaf6d34eed61241f3310d328fa31739
Author: Austin Clements <austin@google.com>
Date: Tue Dec 1 12:29:50 2026 -0500
reflect: panic on calls with SIMD vector parameters or results
On amd64 and arm64, the compiler passes each SIMD vector or mask value
in a single vector register. The reflect register assignment algorithm
doesn't know this and stack-assigns these values as ordinary structs,
and abi.RegArgs can only hold the low 8 bytes of a float register.
As a result, Value.Call, Method calls, MakeFunc, and method values
silently pass SIMD values in the wrong place. Because this also shifts
the placement of later arguments, it isn't even memory safe: the callee
can read the lanes of a vector as pointers.
Until reflect supports SIMD values, make it panic instead. Add a flag,
abi.UFlagSIMD, which the compiler sets on SIMD types. This goes in
UncommonType.Flags rather than using one of the few remaining TFlag
bits: SIMD types are rare, and since they're all defined types, they
already have an UncommonType, so the flag costs nothing. Reject any
function type with a parameter or result that is, or is an array or
struct containing, a type with this flag. This is checked once per
function type, when reflect computes its ABI, so it applies to Call,
MakeFunc, and creating a method value, and panics from ordinary Go
code rather than from the assembly stubs.
A method value's receiver is always passed as a pointer-shaped word,
so methods like String that take only a SIMD receiver keep working.
Platforms that pass arguments in memory, like wasm, already handle SIMD
values correctly, as do the emulated simd types (GODEBUG=simd=0), so
neither is affected.
Update the BUG notes in simd and simd/archsimd.
For #83549
Change-Id: I8f54a1b63a92e6a41fd1033d837e909c6a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/868843
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Austin Clements <austin@google.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
diff --git a/src/cmd/compile/internal/reflectdata/reflect.go b/src/cmd/compile/internal/reflectdata/reflect.go
index 1e10131b86..7de4c0c91f 100644
--- a/src/cmd/compile/internal/reflectdata/reflect.go
+++ b/src/cmd/compile/internal/reflectdata/reflect.go
@@ -345,7 +345,11 @@ func dextratype(lsym *obj.LSym, off int64, t *types.Type, dataAdd int) {
c.Field("Mcount").WriteUint16(uint16(mcount))
c.Field("Xcount").WriteUint16(uint16(xcount))
c.Field("Moff").WriteUint32(uint32(dataAdd))
- // Note: no UFlags are defined yet, so Flags uint16 is left zero.
+ var uflags abi.UFlag
+ if t.IsSIMD() {
+ uflags |= abi.UFlagSIMD
+ }
+ c.Field("Flags").WriteUint16(uint16(uflags))
// Note: there is an unused uint16 of padding here.
// Write the backing array for the []method field.
diff --git a/src/internal/abi/type.go b/src/internal/abi/type.go
index 6a0bcd4f9c..1a446fcd10 100644
--- a/src/internal/abi/type.go
+++ b/src/internal/abi/type.go
@@ -245,6 +245,16 @@ type UncommonType struct {
// can have these properties.
type UFlag uint16
+const (
+ // UFlagSIMD means the type is a SIMD vector or mask type: one of the
+ // vector or mask types of simd/archsimd, or a type defined in terms
+ // of one. These are all defined types, so they all have an
+ // UncommonType anyway, so using an uncommon type flag costs nothing.
+ // An array or struct type that contains a SIMD type doesn't have this
+ // flag.
+ UFlagSIMD UFlag = 1 << 0
+)
+
func (t *UncommonType) Methods() []Method {
if t.Mcount == 0 {
return nil
diff --git a/src/reflect/abi.go b/src/reflect/abi.go
index bcd0a95f1b..56a5c684a9 100644
--- a/src/reflect/abi.go
+++ b/src/reflect/abi.go
@@ -385,6 +385,17 @@ func dumpPtrBitMap(b abi.IntArgRegBitmap) {
}
func newAbiDesc(t *funcType, rcvr *abi.Type) abiDesc {
+ // For ABIs that pass arguments in floating-point registers, the
+ // compiler also passes SIMD values in vector registers, but reflect
+ // doesn't support this yet (see checkNoSIMD). ABIs that pass arguments
+ // in memory (for example, wasm) pass SIMD values in memory, too, so
+ // they work. This checks the constant rather than floatArgRegs because
+ // what matters is how the compiler passes arguments, which tests that
+ // change floatArgRegs don't change.
+ if abi.FloatArgRegs > 0 {
+ checkNoSIMD(t)
+ }
+
// We need to add space for this argument to
// the frame so that it can spill args into it.
//
@@ -467,6 +478,57 @@ func newAbiDesc(t *funcType, rcvr *abi.Type) abiDesc {
return abiDesc{in, out, stackCallArgsSize, retOffset, spill, stackPtrs, inRegPtrs, outRegPtrs}
}
+// checkNoSIMD panics if any parameter or result of t is, or contains, a SIMD
+// vector or mask type.
+//
+// The compiler passes each SIMD value in a single vector register, but the
+// register assignment algorithm in this file doesn't model that, and
+// abi.RegArgs can't hold a whole vector register. For now, just panic if we
+// see this.
+//
+// A method receiver is not checked: it is always passed as a single
+// pointer-shaped word (see addRcvr), so methods such as String that take only
+// a SIMD receiver can still be called.
+//
+// TODO(go.dev/issue/83549): Support SIMD values in reflect calls.
+func checkNoSIMD(t *funcType) {
+ for _, arg := range t.InSlice() {
+ if containsSIMD(arg) {
+ panicSIMD(t)
+ }
+ }
+ for _, res := range t.OutSlice() {
+ if containsSIMD(res) {
+ panicSIMD(t)
+ }
+ }
+}
+
+func panicSIMD(t *funcType) {
+ panic("reflect: function type " + stringFor(&t.Type) +
+ " has SIMD vector parameters or results, which reflect does not support")
+}
+
+// containsSIMD reports whether t is a SIMD vector or mask type, or an array or
+// struct that contains one.
+func containsSIMD(t *abi.Type) bool {
+ if t.UFlags()&abi.UFlagSIMD != 0 {
+ return true
+ }
+ switch t.Kind() {
+ case abi.Array:
+ return containsSIMD(t.Elem())
+ case abi.Struct:
+ st := (*structType)(unsafe.Pointer(t))
+ for i := range st.Fields {
+ if containsSIMD(st.Fields[i].Typ) {
+ return true
+ }
+ }
+ }
+ return false
+}
+
// intFromReg loads an argSize sized integer from reg and places it at to.
//
// argSize must be non-zero, fit in a register, and a power-of-two.
diff --git a/src/reflect/simd_test.go b/src/reflect/simd_test.go
new file mode 100644
index 0000000000..aa03b77d10
--- /dev/null
+++ b/src/reflect/simd_test.go
@@ -0,0 +1,176 @@
+// Copyright 2026 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.
+
+//go:build goexperiment.simdstable && (amd64 || arm64 || wasm)
+
+// TODO(go.dev/issue/83549): reflect doesn't yet support calls that pass SIMD
+// values as parameters or results on platforms that pass arguments in
+// registers, and panics instead. Once it does, these tests should check that
+// such calls return the right result on all platforms, rather than that they
+// panic.
+
+package reflect_test
+
+import (
+ "internal/abi"
+ . "reflect"
+ "runtime"
+ "simd/archsimd"
+ "strings"
+ "testing"
+)
+
+// These platforms all support Int32x4.
+
+//go:noinline
+func simdAdd(x, y archsimd.Int32x4) archsimd.Int32x4 { return x.Add(y) }
+
+//go:noinline
+func simdResult() archsimd.Int32x4 { return archsimd.BroadcastInt32x4(7) }
+
+type simdStruct struct {
+ V archsimd.Int32x4
+ P *int
+}
+
+//go:noinline
+func simdInStruct(s simdStruct) int { return *s.P + int(s.V.Elem(0)) }
+
+type myInt32x4 archsimd.Int32x4
+
+//go:noinline
+func simdDefined(x myInt32x4) myInt32x4 { return x }
+
+//go:noinline
+func simdPointer(p *archsimd.Int32x4, s []archsimd.Int32x4) int32 {
+ return p.Add(s[0]).Elem(0)
+}
+
+// skipIfNoInt32x4 skips the test if the CPU doesn't support the Int32x4
+// operations these tests use. On amd64, they require AVX. arm64 and wasm always
+// support them.
+func skipIfNoInt32x4(t *testing.T) {
+ if runtime.GOARCH == "amd64" && !archsimd.X86.AVX() {
+ t.Skip("requires AVX")
+ }
+}
+
+// simdRegABI reports whether SIMD values are passed in registers, in which
+// case reflect calls that pass them must panic.
+const simdRegABI = abi.FloatArgRegs > 0
+
+// checkSIMDCall checks a reflect call, made by call, that passes SIMD values as
+// parameters or results. If SIMD values are passed in registers, call must
+// panic. Otherwise, it must return want.
+func checkSIMDCall(t *testing.T, name string, want any, call func() any) {
+ t.Helper()
+ var got, r any
+ func() {
+ defer func() { r = recover() }()
+ got = call()
+ }()
+ if simdRegABI {
+ if s, ok := r.(string); !ok || !strings.Contains(s, "SIMD") {
+ t.Errorf("%s: got result %v and panic %v, want SIMD panic", name, got, r)
+ }
+ return
+ }
+ if r != nil {
+ t.Errorf("%s: unexpected panic: %v", name, r)
+ } else if got != want {
+ t.Errorf("%s: got %v, want %v", name, got, want)
+ }
+}
+
+// TestSIMDCall checks each reflect API that makes calls, using simd/archsimd
+// types, which are always hardware vectors. TestReflectCall in package simd
+// covers the portable simd types, which can also be emulated.
+func TestSIMDCall(t *testing.T) {
+ skipIfNoInt32x4(t)
+
+ x := archsimd.LoadInt32x4([]int32{1, 2, 3, 4})
+ y := archsimd.LoadInt32x4([]int32{10, 20, 30, 40})
+ sum := simdAdd(x, y)
+ typ := TypeOf(x)
+
+ checkSIMDCall(t, "Call", sum, func() any {
+ return ValueOf(simdAdd).Call([]Value{ValueOf(x), ValueOf(y)})[0].Interface()
+ })
+ checkSIMDCall(t, "Call with only a result", simdResult(), func() any {
+ return ValueOf(simdResult).Call(nil)[0].Interface()
+ })
+ k := 5
+ checkSIMDCall(t, "Call with a struct", 6, func() any {
+ return ValueOf(simdInStruct).Call([]Value{ValueOf(simdStruct{x, &k})})[0].Interface()
+ })
+ checkSIMDCall(t, "Call with a defined type", myInt32x4(x), func() any {
+ return ValueOf(simdDefined).Call([]Value{ValueOf(myInt32x4(x))})[0].Interface()
+ })
+ checkSIMDCall(t, "Method", sum, func() any {
+ return ValueOf(x).MethodByName("Add").Call([]Value{ValueOf(y)})[0].Interface()
+ })
+ checkSIMDCall(t, "Method.Func", sum, func() any {
+ m, _ := typ.MethodByName("Add")
+ return m.Func.Call([]Value{ValueOf(x), ValueOf(y)})[0].Interface()
+ })
+ checkSIMDCall(t, "method value", sum, func() any {
+ f := ValueOf(x).MethodByName("Add").Interface().(func(archsimd.Int32x4) archsimd.Int32x4)
+ return f(y)
+ })
+ checkSIMDCall(t, "MakeFunc", sum, func() any {
+ var f func(x, y archsimd.Int32x4) archsimd.Int32x4
+ ValueOf(&f).Elem().Set(MakeFunc(TypeOf(f), func(in []Value) []Value {
+ return []Value{ValueOf(simdAdd(in[0].Interface().(archsimd.Int32x4), in[1].Interface().(archsimd.Int32x4)))}
+ }))
+ return f(x, y)
+ })
+ checkSIMDCall(t, "FuncOf", x, func() any {
+ f := MakeFunc(FuncOf([]Type{typ}, []Type{typ}, false), func(in []Value) []Value { return in })
+ return f.Call([]Value{ValueOf(x)})[0].Interface()
+ })
+ checkSIMDCall(t, "StructOf", x, func() any {
+ st := StructOf([]StructField{{Name: "V", Type: typ}})
+ f := MakeFunc(FuncOf([]Type{st}, []Type{typ}, false), func(in []Value) []Value {
+ return []Value{in[0].Field(0)}
+ })
+ arg := New(st).Elem()
+ arg.Field(0).Set(ValueOf(x))
+ return f.Call([]Value{arg})[0].Interface()
+ })
+ checkSIMDCall(t, "ArrayOf", x, func() any {
+ at := ArrayOf(1, typ)
+ f := MakeFunc(FuncOf([]Type{at}, []Type{typ}, false), func(in []Value) []Value {
+ return []Value{in[0].Index(0)}
+ })
+ arg := New(at).Elem()
+ arg.Index(0).Set(ValueOf(x))
+ return f.Call([]Value{arg})[0].Interface()
+ })
+}
+
+// TestSIMDCallAllowed checks reflect calls that involve SIMD types but don't
+// pass a SIMD value as a parameter or result. These work on all platforms.
+func TestSIMDCallAllowed(t *testing.T) {
+ skipIfNoInt32x4(t)
+
+ x := archsimd.LoadInt32x4([]int32{1, 2, 3, 4})
+ y := archsimd.LoadInt32x4([]int32{10, 20, 30, 40})
+
+ // A method value's receiver is passed as a pointer.
+ if got, want := ValueOf(x).MethodByName("String").Call(nil)[0].String(), x.String(); got != want {
+ t.Errorf("String: got %q, want %q", got, want)
+ }
+ if got, want := ValueOf(x).MethodByName("Len").Call(nil)[0].Int(), int64(x.Len()); got != want {
+ t.Errorf("Len: got %d, want %d", got, want)
+ }
+ if got, want := ValueOf(x).MethodByName("String").Interface().(func() string)(), x.String(); got != want {
+ t.Errorf("String method value: got %q, want %q", got, want)
+ }
+
+ // Pointers to and slices of SIMD values are passed like any other.
+ got := ValueOf(simdPointer).Call([]Value{ValueOf(&x), ValueOf([]archsimd.Int32x4{y})})[0].Int()
+ if want := int64(simdPointer(&x, []archsimd.Int32x4{y})); got != want {
+ t.Errorf("simdPointer: got %d, want %d", got, want)
+ }
+}
diff --git a/src/simd/archsimd/doc.go b/src/simd/archsimd/doc.go
index e006a04eb1..3cccf355fd 100644
--- a/src/simd/archsimd/doc.go
+++ b/src/simd/archsimd/doc.go
@@ -75,4 +75,8 @@
// allocate it in the heap, or put it in an aggregate type.
package archsimd
-// BUG(cherry): Using reflect Call to call a vector function/method may not work.
+// BUG(go.dev/issue/83549): On amd64 and arm64, reflect panics when calling or
+// creating a function that has vector or mask parameters or results, for
+// example with [reflect.Value.Call] or [reflect.MakeFunc]. Calling a method
+// through [reflect.Value.Method] works if the method's only vector is its
+// receiver.
diff --git a/src/simd/doc.go b/src/simd/doc.go
index 573478ab5c..d5e6110f77 100644
--- a/src/simd/doc.go
+++ b/src/simd/doc.go
@@ -95,5 +95,9 @@ See the [Int16s.ToArch] example for converting between [simd] and
*/
package simd
-// BUG(reflection): Calls won't work, and there may be other bugs.
+// BUG(go.dev/issue/83549): Unless [Emulated] is true, on amd64 and arm64,
+// reflect panics when calling or creating a function that has SIMD vector or
+// mask parameters or results, for example with [reflect.Value.Call] or
+// [reflect.MakeFunc]. Calling a method through [reflect.Value.Method] works if
+// the method's only SIMD value is its receiver.
// BUG(modified names): Modified names may appear in stack traces and debugging.
diff --git a/src/simd/reflect_test.go b/src/simd/reflect_test.go
new file mode 100644
index 0000000000..5e28644d63
--- /dev/null
+++ b/src/simd/reflect_test.go
@@ -0,0 +1,58 @@
+// Copyright 2026 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.
+
+//go:build goexperiment.simdstable
+
+package simd_test
+
+import (
+ "reflect"
+ "runtime"
+ "simd"
+ "strings"
+ "testing"
+)
+
+//go:noinline
+func reflectAdd(x, y simd.Int32s) simd.Int32s { return x.Add(y) }
+
+// TestReflectCall checks that a reflect call passing SIMD values either works
+// or panics. It panics when the simd types are implemented with hardware
+// vectors on a platform that passes them in registers, and works otherwise.
+//
+// TestSIMDCall in package reflect checks each reflect API that makes calls,
+// using simd/archsimd types. This test is here instead because it depends on
+// the GODEBUG=simd setting, which "go test simd -run-godebugs" varies: in
+// emulated mode, the simd types are ordinary structs, so calls work.
+//
+// TODO(go.dev/issue/83549): Once reflect supports calls that pass SIMD values,
+// check that the call works in all configurations.
+func TestReflectCall(t *testing.T) {
+ wantPanic := !simd.Emulated() && (runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64")
+
+ xs := make([]int32, simd.VectorBits()/32)
+ ys := make([]int32, len(xs))
+ for i := range xs {
+ xs[i] = int32(i + 1)
+ ys[i] = int32(10 * (i + 1))
+ }
+ x, y := simd.LoadInt32s(xs), simd.LoadInt32s(ys)
+
+ defer func() {
+ r := recover()
+ if !wantPanic {
+ if r != nil {
+ t.Fatalf("unexpected panic: %v", r)
+ }
+ return
+ }
+ if s, ok := r.(string); !ok || !strings.Contains(s, "SIMD") {
+ t.Fatalf("got panic %v, want SIMD panic", r)
+ }
+ }()
+ got := reflect.ValueOf(reflectAdd).Call([]reflect.Value{reflect.ValueOf(x), reflect.ValueOf(y)})[0].Interface()
+ if want := reflectAdd(x, y); got != want {
+ t.Errorf("got %v, want %v", got, want)
+ }
+}