Source file src/simd/archsimd/_gen/simdgen/gen_enlarge.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 main
     6  
     7  import (
     8  	"bytes"
     9  	"cmp"
    10  	"fmt"
    11  	"maps"
    12  	"simd/archsimd/_gen/sgutil"
    13  	"slices"
    14  	"strings"
    15  )
    16  
    17  // writeSIMDEnlargeTables writes the tables the compiler's "simd enlarge" pass
    18  // uses to bypass Enlarge for operations that read only the low part of an
    19  // input. It writes nothing if the architecture has no Enlarge.
    20  //
    21  // x.Enlarge() puts x in the low half of a vector twice as wide (zeroing the
    22  // upper half). An operation with a narrow input (see [Operand.NarrowReg])
    23  // reads only the low part of that input, so if that part lies within x, the
    24  // operation can read x directly. For example, Int8x32.ExtendToInt32 reads only
    25  // the low 128 bits of its input, so for an Int8x16 x, the pass rewrites
    26  // x.Enlarge().ExtendToInt32() to take x as its input, which then lowers to a
    27  // single VPMOVSXBD from x's register.
    28  //
    29  // The pass works on generic ops, before lowering, so the tables describe
    30  // generic ops. This means the masked and merging forms of each machine op need
    31  // nothing extra: they're formed by lowering rules from the unmasked op, which
    32  // already has the bypassed input.
    33  //
    34  // After the pass, the op's input is narrower than its Go type. This is fine:
    35  // lowering passes the input through to the machine op, codegen names the
    36  // input's register by the size the instruction reads (see simdNarrowArg), and
    37  // writeSIMDRules generates rules that combine the instruction with a load of
    38  // any width that contains that part, so a load of x is still combined with
    39  // it.
    40  func writeSIMDEnlargeTables(ops []Operation) *bytes.Buffer {
    41  	// narrowArg identifies an op's narrow input: the index of the argument
    42  	// and how many bits of it the op reads.
    43  	type narrowArg struct{ arg, bits int }
    44  	var enlarge []string
    45  	narrow := make(map[narrowArg][]string) // -> generic op names
    46  	seen := make(map[string]bool)
    47  	for _, opr := range ops {
    48  		if opr.NoGenericOps != nil && *opr.NoGenericOps == "true" {
    49  			continue
    50  		}
    51  		if opr.SkipMaskedMethod() {
    52  			continue
    53  		}
    54  		_, _, maskType, immType, gOp, _ := opr.shape()
    55  		name := gOp.GenericName()
    56  		if seen[name] {
    57  			continue
    58  		}
    59  		seen[name] = true
    60  
    61  		if gOp.Go == "Enlarge" {
    62  			if maskType != NoMask || len(gOp.In) != 1 || len(gOp.Out) != 1 || !gOp.Out[0].NarrowReg() {
    63  				panic(fmt.Errorf("simdgen: unexpected shape of Enlarge: %v", gOp))
    64  			}
    65  			enlarge = append(enlarge, name)
    66  			continue
    67  		}
    68  
    69  		// Find if gOp has any narrow inputs. The generic op's
    70  		// arguments are gOp.In, in order, with no immediates.
    71  		arg := -1
    72  		for i, in := range gOp.In {
    73  			if in.Class != "vreg" || !in.NarrowReg() {
    74  				continue
    75  			}
    76  			if arg >= 0 {
    77  				// TODO: We could support this, but it's not
    78  				// needed right now.
    79  				panic(fmt.Errorf("simdgen: Enlarge bypass supports one narrow input, found more: %v", gOp))
    80  			}
    81  			arg = i
    82  		}
    83  		if arg < 0 {
    84  			continue
    85  		}
    86  		if gOp.OperandOrder != nil || opr.implicitPredCount() > 0 {
    87  			panic(fmt.Errorf("simdgen: Enlarge bypass needs generic op arguments in operand order: %v", gOp))
    88  		}
    89  		if immType != NoImm {
    90  			// No operation with a narrow input takes an
    91  			// immediate today. Supporting one requires
    92  			// accounting for the immediate when numbering the
    93  			// generic op's arguments.
    94  			panic(fmt.Errorf("simdgen: Enlarge bypass does not support immediates: %v", gOp))
    95  		}
    96  		key := narrowArg{arg, gOp.In[arg].Bits.N()}
    97  		narrow[key] = append(narrow[key], name)
    98  	}
    99  	if len(enlarge) == 0 {
   100  		return nil
   101  	}
   102  
   103  	archInfo := CurrentArch()
   104  	arch := archInfo.SIMDTag
   105  	var buf bytes.Buffer
   106  	fmt.Fprintf(&buf, "%s\n\n", archInfo.GeneratedHeader)
   107  	fmt.Fprintf(&buf, "package ssacompile\n\n")
   108  	fmt.Fprintf(&buf, "import \"cmd/compile/internal/ssa/ssaop\"\n\n")
   109  
   110  	caseList := func(names []string) string {
   111  		slices.SortFunc(names, sgutil.CompareNatural)
   112  		var b strings.Builder
   113  		for i, n := range names {
   114  			if i > 0 {
   115  				b.WriteString(",\n\t\t")
   116  			}
   117  			b.WriteString("ssaop.Op" + n)
   118  		}
   119  		return b.String()
   120  	}
   121  
   122  	fmt.Fprintf(&buf, "// simd%sIsEnlarge reports whether op is an Enlarge generic op.\n", arch)
   123  	fmt.Fprintf(&buf, "func simd%sIsEnlarge(op ssaop.Op) bool {\n", arch)
   124  	fmt.Fprintf(&buf, "\tswitch op {\n\tcase %s:\n\t\treturn true\n\t}\n\treturn false\n}\n\n", caseList(enlarge))
   125  
   126  	fmt.Fprintf(&buf, "// simd%sNarrowArg reports whether a generic op reads only the low part of\n", arch)
   127  	fmt.Fprintf(&buf, "// one of its arguments. If so, it returns the index of that argument and how\n")
   128  	fmt.Fprintf(&buf, "// many of its low bits the op reads. Otherwise, it returns -1, 0.\n")
   129  	fmt.Fprintf(&buf, "func simd%sNarrowArg(op ssaop.Op) (arg int, bits int64) {\n", arch)
   130  	fmt.Fprintf(&buf, "\tswitch op {\n")
   131  	keys := slices.SortedFunc(maps.Keys(narrow), func(a, b narrowArg) int {
   132  		return cmp.Or(cmp.Compare(a.arg, b.arg), cmp.Compare(a.bits, b.bits))
   133  	})
   134  	for _, k := range keys {
   135  		fmt.Fprintf(&buf, "\tcase %s:\n\t\treturn %d, %d\n", caseList(narrow[k]), k.arg, k.bits)
   136  	}
   137  	fmt.Fprintf(&buf, "\t}\n\treturn -1, 0\n}\n")
   138  	return &buf
   139  }
   140  

View as plain text