Source file src/cmd/compile/internal/types/simd.go
1 // Copyright 2026 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package types 6 7 import ( 8 "internal/buildcfg" 9 "strings" 10 11 "cmd/compile/internal/base" 12 ) 13 14 // IsSIMD reports whether t is a SIMD type: one of the SSA vector and mask 15 // types, such as TypeVec128, or one of the vector and mask types of 16 // simd/archsimd, or a type defined in terms of one of those, such as 17 // bridge.Int32x4 in package simd/internal/bridge, which is defined as 18 // archsimd.Int32x4. Values of SIMD types live in vector registers. The tag 19 // types that mark the vector and mask types of simd/archsimd are not SIMD 20 // types. 21 func (t *Type) IsSIMD() bool { 22 return t.flags&typeIsSIMD != 0 23 } 24 25 // ContainsSIMD reports whether t is a SIMD type, or an array or struct 26 // that contains one. 27 func (t *Type) ContainsSIMD() bool { 28 if t.IsSIMD() { 29 return true 30 } 31 switch t.Kind() { 32 case TARRAY: 33 return t.Elem().ContainsSIMD() 34 case TSTRUCT: 35 for _, f := range t.Fields() { 36 if f.Type.ContainsSIMD() { 37 return true 38 } 39 } 40 } 41 return false 42 } 43 44 // SIMDShape describes the elements of a SIMD vector or mask type. 45 type SIMDShape struct { 46 // Elem is the element type. For example, it's float32 for Float32x4. 47 // 48 // The Elem of a mask is the signed integer type of the size of the 49 // vector elements it governs: int32 for Mask32x4 and for Mask32s. 50 Elem *Type 51 52 // Lanes is the number of elements of a fixed-width type, or 0 for a 53 // scalable type. 54 Lanes int64 55 56 // Scalable reports whether this is an arm64 SVE type, whose number of 57 // elements is only known at run time. 58 Scalable bool 59 60 // Mask reports whether this is a mask type, such as Mask32x4 or 61 // Mask32s. 62 // 63 // The fixed-width masks are stored as vectors whose elements are 0 or 64 // -1, so most of the compiler can treat them like vectors. But a 65 // scalable mask on arm64 is an SVE predicate: it's stored as a bitmap 66 // with one bit per byte of a scalable vector rather than as an array 67 // of Elem, and lives in a predicate register. 68 Mask bool 69 } 70 71 // SIMDShape returns the shape of SIMD vector or mask type t, and whether t is 72 // such a type. It works on the vector and mask types of simd/archsimd, and 73 // types defined in terms of them, such as the types of package simd on 74 // platforms where it isn't emulated. Each is a struct whose first field is a 75 // SIMD tag and whose second field holds its elements. 76 func (t *Type) SIMDShape() (shape SIMDShape, ok bool) { 77 if !t.IsSIMD() || !t.IsStruct() { 78 // Not a SIMD vector or mask type declared in Go. This also 79 // excludes the SSA SIMD types, which aren't structs. 80 return SIMDShape{}, false 81 } 82 if t.NumFields() != 2 || t.Field(0).Type.flags&typeIsSIMDTag == 0 { 83 base.Fatalf("SIMD type %v isn't a tag followed by its elements", t) 84 } 85 tag, vals := t.Field(0), t.Field(1).Type 86 // The tag field of a mask type is named after the mask, like mask32x4 87 // or mask32s, and that of a vector type after the vector, like 88 // float32x4. The rest of their fields can be the same. 89 shape.Mask = strings.HasPrefix(tag.Sym.Name, "mask") 90 91 switch tag.Type.Sym().Name { 92 case "psve": 93 // SVE predicate mask. Its second field is just storage for its 94 // bits, so the name of its tag field records the size of the 95 // vector elements it governs. 96 shape.Scalable = true 97 switch tag.Sym.Name { 98 case "mask8s": 99 shape.Elem = Types[TINT8] 100 case "mask16s": 101 shape.Elem = Types[TINT16] 102 case "mask32s": 103 shape.Elem = Types[TINT32] 104 case "mask64s": 105 shape.Elem = Types[TINT64] 106 default: 107 base.Fatalf("SVE predicate %v has unexpected tag field %v", t, tag.Sym) 108 } 109 return shape, true 110 case "v256": 111 // On arm64, v256 is the tag of the SVE scalable vectors. 112 shape.Scalable = buildcfg.GOARCH == "arm64" 113 } 114 if !vals.IsArray() { 115 base.Fatalf("SIMD type %v has elements of non-array type %v", t, vals) 116 } 117 shape.Elem = vals.Elem() 118 if !shape.Scalable { 119 shape.Lanes = vals.NumElem() 120 } 121 return shape, true 122 } 123