Source file src/cmd/compile/internal/ssacompile/simdenlarge.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 ssacompile 6 7 import ( 8 "internal/goarch" 9 10 "cmd/compile/internal/ssa" 11 ) 12 13 // simdEnlarge bypasses Enlarge for SIMD operations that read only the low part 14 // of an input. 15 // 16 // x.Enlarge() puts x in the low half of a vector twice as wide, zeroing the 17 // upper half. Some operations, such as the widening ExtendTo* and 18 // ConvertToFloat64 conversions, read only the low part of an input. If that 19 // part lies within x, the operation can read x directly and the Enlarge is 20 // unnecessary. For example, for an Int8x16 x, Int8x32.ExtendToInt32 reads only 21 // the low 128 bits of x.Enlarge(), which are x, so this rewrites 22 // 23 // (ExtendToInt32Int8x32 (EnlargeInt8x16 x)) -> (ExtendToInt32Int8x32 x) 24 // 25 // This also looks through chains of Enlarge, as in 26 // x.Enlarge().Enlarge().ExtendToInt64(). 27 // 28 // This works on generic ops, just before lowering, so the lowering rules 29 // (including the rules that combine loads with ops, and those that form 30 // masked and merging machine ops) see the results of this optimization. 31 // 32 // Bypassing an Enlarge gives the op an input whose type is narrower than 33 // the op's input type. That's safe because lowering passes the input 34 // through to the machine op unchanged, and codegen names the input's 35 // register by the size the instruction reads, not by the input's type. 36 // 37 // simdgen generates the tables of Enlarge ops and ops with narrow inputs. 38 func simdEnlarge(f *ssa.Func) { 39 if f.MaxCPUFeatures == ssa.CPUNone { 40 return 41 } 42 // AMD64 is the only architecture with multiple fixed-width vector 43 // sizes, so it's the only one with Enlarge. 44 if f.Config.Ctxt.Arch.Family != goarch.AMD64 { 45 return 46 } 47 48 for _, b := range f.Blocks { 49 for _, v := range b.Values { 50 i, bits := simdAMD64NarrowArg(v.Op) 51 if i < 0 { 52 continue 53 } 54 // v reads only the low bits of its i'th argument. 55 // Unwind any Enlarges of that argument that don't 56 // affect those bits. 57 x := v.Args[i] 58 for simdAMD64IsEnlarge(x.Op) && x.Args[0].Type.Size()*8 >= bits { 59 x = x.Args[0] 60 } 61 if x != v.Args[i] { 62 if f.Pass.Debug > 0 { 63 f.Warnl(v.Pos, "bypassed Enlarge for %v", v.Op) 64 } 65 v.SetArg(i, x) 66 } 67 } 68 } 69 } 70