Debian Codegraph @

build/internal/x86/reg.go

raw
1 package x86
2
3 import (
4 . "github.com/mmcloughlin/avo/build"
5 . "github.com/mmcloughlin/avo/operand"
6 . "github.com/mmcloughlin/avo/reg"
7 )
8
9 var all = map[Spec][]VecPhysical{
10 S128: []VecPhysical{X0, X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17, X18, X19, X20, X21, X22, X23, X24, X25, X26, X27, X28, X29, X30, X31},
11 S256: []VecPhysical{Y0, Y1, Y2, Y3, Y4, Y5, Y6, Y7, Y8, Y9, Y10, Y11, Y12, Y13, Y14, Y15, Y16, Y17, Y18, Y19, Y20, Y21, Y22, Y23, Y24, Y25, Y26, Y27, Y28, Y29, Y30, Y31},
12 S512: []VecPhysical{Z0, Z1, Z2, Z3, Z4, Z5, Z6, Z7, Z8, Z9, Z10, Z11, Z12, Z13, Z14, Z15, Z16, Z17, Z18, Z19, Z20, Z21, Z22, Z23, Z24, Z25, Z26, Z27, Z28, Z29, Z30, Z31},
13 }
14
15 func gp(c func() GPVirtual, g ...Register) Register {
16 if len(g) == 0 {
17 return c()
18 }
19 return g[0]
20 }
21
22 // load either emits a size-specific MOV operation based on the input immediate
23 // value, or returns the existing register. The optional destination register
24 // must be a general purpose register if provided. If not provided, a virtual
25 // register will be allocated if needed.
26 func load(ir Op, dst ...Register) (Register, uint) {
27 switch v := ir.(type) {
28 default:
29 panic("unsupported input operand")
30 case U8, I8:
31 g := gp(GP32, dst...)
32 MOVB(v, g.(GP).As8())
33 return g, 1
34 case U16, I16:
35 g := gp(GP32, dst...)
36 MOVW(v, g.(GP).As16())
37 return g, 2
38 case U32, I32:
39 g := gp(GP32, dst...)
40 MOVL(v, g)
41 return g, 4
42 case U64, I64:
43 g := gp(GP64, dst...)
44 MOVQ(v, g)
45 return g, 8
46 case Register:
47 return v, v.Size()
48 }
49 }
50
51 // VecList returns a slice of vector registers for the given Spec.
52 func VecList(s Spec, max int) []VecPhysical {
53 return all[s][:max]
54 }
55
56 // VecBroadcast broadcasts an immediate or general purpose register into a
57 // vector register. The broadcast size is based on the input operand size.
58 // If the input is a register, it may be necessary to convert it to the
59 // desired size. For example:
60 // reg := GP32()
61 // XORL(reg, reg)
62 // MOVB(U8(0x7F), reg.As8())
63 // mask := VecBroadcast(reg, XMM()) // will broadcast 0x0000007F0000007F0000007F0000007F
64 // mask := VecBroadcast(reg.As8(), XMM()) // will broadcast 0x7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F7F
65 //
66 // If the `reg` register isn't needed, it would preferrable to use:
67 // mask := VecBroadcast(U8(0x7F), XMM())
68 func VecBroadcast(ir Op, xyz Register) Register {
69 vec := xyz.(Vec)
70 reg, size := load(ir)
71
72 // PINSR{B,W} accept either m{8,16} or r32. If the input was
73 // r{8,16} we need to cast to 32 bits.
74 if reg.Size() < 4 {
75 if gp, ok := reg.(GP); ok {
76 reg = gp.As32()
77 } else {
78 r32 := GP32()
79 switch reg.Size() {
80 case 1:
81 MOVBLZX(reg, r32)
82 case 2:
83 MOVWLZX(reg, r32)
84 }
85 reg = r32
86 }
87 }
88
89 switch size {
90 default:
91 panic("unsupported register size")
92 case 1:
93 PINSRB(Imm(0), reg, vec.AsX())
94 VPBROADCASTB(vec.AsX(), xyz)
95 case 2:
96 PINSRW(Imm(0), reg, vec.AsX())
97 VPBROADCASTW(vec.AsX(), xyz)
98 case 4:
99 PINSRD(Imm(0), reg, vec.AsX())
100 VPBROADCASTD(vec.AsX(), xyz)
101 case 8:
102 PINSRQ(Imm(0), reg, vec.AsX())
103 VPBROADCASTQ(vec.AsX(), xyz)
104 }
105
106 return xyz
107 }
108
109 // VectorLane is an interface for abstracting allocating and loading memory into
110 // vector registers. This is used during the map phase of the Vectorizer.
111 type VectorLane interface {
112 Read(Mem) Register
113 Offset(Mem) Mem
114 Alloc() Register
115 }
116
117 // Vectorizer is a map/reduce-based helper for constructing parallelized
118 // instruction pipelines.
119 type Vectorizer struct {
120 max int // total registers allowed
121 mapper func(VectorLane) Register // function to map the main operation to an output register
122 reducer func(a, b Register) Register // function to reduce the mapped output registers into one
123 }
124
125 // NewVectorizer creates a new vectorizing utility utilizing a max number of
126 // registers and a mapper function.
127 func NewVectorizer(max int, mapper func(VectorLane) Register) *Vectorizer {
128 return &Vectorizer{max: max, mapper: mapper}
129 }
130
131 // Reduce sets the reducer function in the Vectorizer.
132 func (v *Vectorizer) Reduce(h func(a, b Register) Register) *Vectorizer {
133 v.reducer = h
134 return v
135 }
136
137 // Compile runs the map and reduce phases for the given register size and
138 // parallel lane count. This can be called multiple times using different
139 // configurations to produce separate execution strides. The returned slice
140 // is dependent on the presence of a reducer. If no reducer is used, the
141 // slice will be all of the output registers from the map phase. If a reducer
142 // is defined, the result is slice containing the final reduced register.
143 func (v *Vectorizer) Compile(spec Spec, lanes int) []Register {
144 alloc := NewVectorAlloc(VecList(spec, v.max), lanes)
145 var out []Register
146
147 for alloc.NextLane() {
148 r := v.mapper(alloc)
149 out = append(out, r)
150 }
151
152 if v.reducer != nil {
153 for len(out) > 1 {
154 r := v.reducer(out[0], out[1])
155 out = append(out[2:], r)
156 }
157 }
158
159 return out
160 }
161
162 // ReduceOr performs a bitwise OR between two registers and returns the result.
163 // This can be used as the reducer for a Vectorizer.
164 func ReduceOr(a, b Register) Register {
165 VPOR(b, a, a)
166 return a
167 }
168
169 // ReduceAnd performs a bitwise AND between two registers and returns the result.
170 // This can be used as the reducer for a Vectorizer.
171 func ReduceAnd(a, b Register) Register {
172 VPAND(b, a, a)
173 return a
174 }
175
176 // VectorAlloc is a lower-level lane-driven register allocator. This pulls
177 // registers from a fixed list of physical registers for a given number of
178 // lanes. Registers are allocated in distinct blocks; one block for each lane.
179 type VectorAlloc struct {
180 vec []VecPhysical // all available physical registers
181 rd map[Mem]vecRead // loaded register index
182 off map[Mem]int // offset index
183 lanes int // number of lanes being compiled
184 lane int // current lane being compiled
185 size int // register size
186 }
187
188 type vecRead struct {
189 reg []Register
190 idx int
191 }
192
193 // NewVectorAlloc creates a new VectorAlloc instance.
194 func NewVectorAlloc(vec []VecPhysical, lanes int) *VectorAlloc {
195 return &VectorAlloc{
196 vec: vec,
197 rd: map[Mem]vecRead{},
198 off: map[Mem]int{},
199 lanes: lanes,
200 lane: -1,
201 size: int(vec[0].Size()),
202 }
203 }
204
205 // NextLane is used to advance the allocator to the next lane available lane.
206 // This returns false when no more lanes are available.
207 func (a *VectorAlloc) NextLane() bool {
208 next := a.lane + 1
209 if next < a.lanes {
210 a.lane = next
211 return true
212 }
213 return false
214 }
215
216 // Read implements the VectorLane interface. This reads the next register-sized
217 // memory region. Each read within a single lane will load adjacent memory
218 // regions. Subsequent lanes will read adjacent memory after the last read of
219 // the prior lane. This has special handling so that all reads are batched
220 // together. Because of this, Read calls should appear first in the mapper.
221 func (a *VectorAlloc) Read(mem Mem) Register {
222 if _, ok := a.off[mem]; ok {
223 panic("Offset and Read cannot current be combined for the same memory region")
224 }
225
226 rd := a.rd[mem]
227
228 if a.lane == 0 {
229 for i := 0; i < a.lanes; i++ {
230 r := a.Alloc()
231 VMOVDQU(mem.Offset(len(rd.reg)*a.size), r)
232 rd.reg = append(rd.reg, r)
233 }
234 }
235
236 r := rd.reg[rd.idx]
237 rd.idx++
238
239 a.rd[mem] = rd
240
241 return r
242 }
243
244 // Offset implements the VectorLane interface. This calculates the next
245 // register-sized memory offset. Each offset within a single lane will refer
246 // to adjacent memory regions. Subsequent lanes will obtain an offset of
247 // adjacent memory after the last offset of the prior lane.
248 func (a *VectorAlloc) Offset(mem Mem) Mem {
249 if _, ok := a.rd[mem]; ok {
250 panic("Read and Offset cannot current be combined for the same memory region")
251 }
252
253 n := a.off[mem]
254 a.off[mem] = n + 1
255
256 return mem.Offset(n * a.size)
257 }
258
259 // Alloc implements the VectorLane interface. This allocates a register for the
260 // current lane.
261 func (a *VectorAlloc) Alloc() Register {
262 if len(a.vec) == 0 {
263 panic("not enough vector registers available")
264 }
265
266 r := a.vec[0]
267 a.vec = a.vec[1:]
268
269 return r
270 }