1
2
3
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
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 func writeSIMDEnlargeTables(ops []Operation) *bytes.Buffer {
41
42
43 type narrowArg struct{ arg, bits int }
44 var enlarge []string
45 narrow := make(map[narrowArg][]string)
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
70
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
78
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
91
92
93
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