Debian Codegraph @

build/sortedset/dedupe_asm.go

raw
1 // +build ignore
2
3 package main
4
5 import (
6 "fmt"
7 "math/bits"
8
9 . "github.com/mmcloughlin/avo/build"
10 . "github.com/mmcloughlin/avo/operand"
11 . "github.com/mmcloughlin/avo/reg"
12 . "github.com/segmentio/asm/build/internal/asm"
13 . "github.com/segmentio/asm/build/internal/x86"
14 "github.com/segmentio/asm/cpu"
15 )
16
17 func init() {
18 ConstraintExpr("!purego")
19 }
20
21 func main() {
22 generateDedupe(new(dedupe1))
23 generateDedupe(new(dedupe2))
24 generateDedupe(new(dedupe4))
25 generateDedupe(new(dedupe8))
26 generateDedupe(new(dedupe16))
27 generateDedupe(new(dedupe32))
28 Generate()
29 }
30
31 type dedupe interface {
32 size() int
33 init(p, w GPVirtual)
34 copy(p, w GPVirtual)
35 }
36
37 type dedupeAVX2 interface {
38 dedupe
39 vec() VecVirtual
40 vsize() int
41 vlanes() int
42 vinit(p, w GPVirtual)
43 vcopy(src0, src1, dst VecVirtual, off GPVirtual)
44 }
45
46 type dedupe1 struct{}
47
48 func (*dedupe1) size() int { return 1 }
49 func (*dedupe1) init(p, w GPVirtual) { move(MOVB, GP8(), p, w) }
50 func (*dedupe1) copy(p, w GPVirtual) { generateDedupeX86(MOVB, CMPB, GP8, p, w, 1) }
51
52 type dedupe2 struct{}
53
54 func (*dedupe2) size() int { return 2 }
55 func (*dedupe2) init(p, w GPVirtual) { move(MOVW, GP16(), p, w) }
56 func (*dedupe2) copy(p, w GPVirtual) { generateDedupeX86(MOVW, CMPW, GP16, p, w, 2) }
57
58 type dedupe4 struct {
59 shuf GPVirtual
60 incr GPVirtual
61 }
62
63 func (*dedupe4) size() int { return 4 }
64 func (*dedupe4) init(p, w GPVirtual) { move(MOVL, GP32(), p, w) }
65 func (*dedupe4) copy(p, w GPVirtual) { generateDedupeX86(MOVL, CMPL, GP32, p, w, 4) }
66
67 func (d *dedupe4) vec() VecVirtual { return XMM() }
68 func (d *dedupe4) vsize() int { return 16 }
69 func (d *dedupe4) vlanes() int { return 8 }
70 func (d *dedupe4) vinit(p, w GPVirtual) {
71 move(MOVL, GP32(), p, w)
72
73 d.shuf = GP64()
74 LEAQ(
75 ConstShuffleMask32("dedupe4_shuffle_mask",
76 0, 1, 2, 3, // 0b0000
77 1, 2, 3, 0, // 0b0001
78 0, 2, 3, 1, // 0b0010
79 2, 3, 0, 1, // 0b0011
80
81 0, 1, 3, 2, // 0b0100
82 1, 3, 0, 2, // 0b0101
83 0, 3, 1, 2, // 0b0110
84 3, 0, 1, 2, // 0b0111
85
86 0, 1, 2, 3, // 0b1000
87 1, 2, 0, 3, // 0b1001
88 0, 3, 1, 2, // 0b1010
89 2, 0, 1, 3, // 0b1011
90
91 0, 1, 2, 3, // 0b1100
92 1, 0, 2, 3, // 0b1101
93 0, 1, 2, 3, // 0b1110
94 0, 1, 2, 3, // 0b1111
95 ),
96 d.shuf,
97 )
98
99 d.incr = GP64()
100 LEAQ(
101 // A table indexing the number of bytes to advance the write pointer by,
102 // depending on how many 4 bytes items were equal.
103 ConstArray32("dedupe4_offset_array",
104 // 0b0000, 0b0001, 0b0010, 0b0011
105 16, 12, 12, 8,
106 // 0b0100, 0b0101, 0b0110, 0b0111
107 12, 8, 8, 4,
108 // 0b1000, 0b1001, 0b1010, 0b1011
109 12, 8, 8, 4,
110 // 0b1100, 0b1101, 0b1110, 0b1111
111 8, 4, 4, 0,
112 ),
113 d.incr,
114 )
115 }
116
117 func (d *dedupe4) vcopy(src0, src1, dst VecVirtual, off GPVirtual) {
118 VPCMPEQD(src1, src0, src0)
119 VMOVMSKPS(src0, off.As32())
120 // 16 possible states:
121 // * 0b0000
122 // * 0b0001
123 // * 0b0010
124 // * 0b0011
125 // * ...
126 // * 0b1111
127 // We multiply the mask by 4 (left shift 2) to use the value as index into
128 // the shuffle mask table (128 bits) and offset array (32 bits).
129 SHLQ(Imm(2), off)
130 VPSHUFB(Mem{Base: d.shuf}.Idx(off, 4), src1, dst)
131 MOVL(Mem{Base: d.incr}.Idx(off, 1), off.As32())
132 }
133
134 type dedupe8 struct {
135 shuf GPVirtual
136 incr GPVirtual
137 }
138
139 func (*dedupe8) size() int { return 8 }
140 func (*dedupe8) init(p, w GPVirtual) { move(MOVQ, GP64(), p, w) }
141 func (*dedupe8) copy(p, w GPVirtual) { generateDedupeX86(MOVQ, CMPQ, GP64, p, w, 8) }
142
143 func (*dedupe8) vec() VecVirtual { return XMM() }
144 func (*dedupe8) vsize() int { return 16 }
145 func (*dedupe8) vlanes() int { return 8 }
146 func (d *dedupe8) vinit(p, w GPVirtual) {
147 move(MOVQ, GP64(), p, w)
148 d.shuf = GP64()
149 d.incr = GP64()
150 LEAQ(
151 ConstShuffleMask64("dedupe8_shuffle_mask",
152 // We use the interesting property that the first and second masks
153 // overlap on their respective upper and lower 64 bits to use a
154 // shuffle mask of 64 bits elements.
155 //
156 // This technique saves a shift instruction in the vcopy
157 // implementation which would otherwise be required to convert the
158 // bit mask values (0, 1, 2, 3) to indices into an array of 128 bits
159 // elements (since only 1, 2, 4, and 8 scales are supported).
160 //
161 // This is the layout:
162 // * (0b00 x 8)[128:0] => [0, 1]; copy all 128 bits
163 // * (0b01 x 8)[128:0] => [1, 0]; copy the upper 64 bits (lower 64 bits are discarded)
164 // * (0b10 x 8)[128:0] => [0, 0]; copy the lower 64 bits (upper 64 bits are discarded)
165 // * (0b11 x 8)[128:0] => [0, 0]; all 128 bits are discarded
166 0, 1, 0, 0, 0,
167 ),
168 d.shuf,
169 )
170 LEAQ(
171 ConstArray64("dedupe8_offset_array", 16, 8, 8, 0),
172 d.incr,
173 )
174 }
175
176 func (d *dedupe8) vcopy(src0, src1, dst VecVirtual, off GPVirtual) {
177 VPCMPEQQ(src1, src0, src0)
178 VMOVMSKPD(src0, off.As32())
179 VPSHUFB(Mem{Base: d.shuf}.Idx(off, 8), src1, dst)
180 MOVQ(Mem{Base: d.incr}.Idx(off, 8), off)
181 }
182
183 type dedupe16 struct {
184 nop GPVirtual
185 inc GPVirtual
186 }
187
188 func (*dedupe16) size() int { return 16 }
189
190 func (*dedupe16) init(p, w GPVirtual) { move(MOVOU, XMM(), p, w) }
191
192 func (*dedupe16) copy(p, w GPVirtual) {
193 next := GP64()
194 MOVQ(w, next)
195 ADDQ(Imm(16), next)
196 xmm0, xmm1 := XMM(), XMM()
197 MOVOU(Mem{Base: p}, xmm0)
198 MOVOU(Mem{Base: p}.Offset(16), xmm1)
199 MOVOU(xmm1, Mem{Base: w})
200 mask := GP32()
201 PCMPEQQ(xmm0, xmm1)
202 PMOVMSKB(xmm1, mask)
203 CMPL(mask, U32(0xFFFF))
204 CMOVQNE(next, w)
205 }
206
207 func (*dedupe16) vec() VecVirtual { return XMM() }
208
209 func (*dedupe16) vsize() int { return 16 }
210
211 func (*dedupe16) vlanes() int { return 8 }
212
213 func (d *dedupe16) vinit(p, w GPVirtual) {
214 move(VMOVDQU, XMM(), p, w)
215 d.nop = GP64()
216 d.inc = GP64()
217 XORQ(d.nop, d.nop)
218 MOVQ(U64(16), d.inc)
219 }
220
221 func (d *dedupe16) vcopy(src0, src1, dst VecVirtual, off GPVirtual) {
222 if src1 != dst {
223 VMOVDQA(src1, dst)
224 }
225 VPCMPEQQ(src1, src0, src0)
226 // This gives a bitmask with these possible values:
227 // * 0b00
228 // * 0b01
229 // * 0b10
230 // * 0b11
231 // We only care about the last case, which indicates that both 64 bits lanes
232 // of the XMM register were equal.
233 VMOVMSKPD(src0, off.As32())
234 CMPQ(off, Imm(3))
235 CMOVQEQ(d.nop, off)
236 CMOVQNE(d.inc, off)
237 }
238
239 type dedupe32 struct {
240 nop GPVirtual
241 inc GPVirtual
242 }
243
244 func (*dedupe32) size() int { return 32 }
245
246 func (*dedupe32) init(p, w GPVirtual) {
247 lo, hi := XMM(), XMM()
248 MOVOU(Mem{Base: p}, lo)
249 MOVOU(Mem{Base: p}.Offset(16), hi)
250 MOVOU(lo, Mem{Base: w})
251 MOVOU(hi, Mem{Base: w}.Offset(16))
252 }
253
254 func (*dedupe32) copy(p, w GPVirtual) {
255 next := GP64()
256 MOVQ(w, next)
257 ADDQ(Imm(32), next)
258 loP, hiP := XMM(), XMM()
259 loQ, hiQ := XMM(), XMM()
260 MOVOU(Mem{Base: p}, loP)
261 MOVOU(Mem{Base: p}.Offset(16), hiP)
262 MOVOU(Mem{Base: p}.Offset(32), loQ)
263 MOVOU(Mem{Base: p}.Offset(48), hiQ)
264 MOVOU(loQ, Mem{Base: w})
265 MOVOU(hiQ, Mem{Base: w}.Offset(16))
266 mask0, mask1 := GP32(), GP32()
267 PCMPEQQ(loP, loQ)
268 PCMPEQQ(hiP, hiQ)
269 PMOVMSKB(loQ, mask0)
270 PMOVMSKB(hiQ, mask1)
271 ANDL(mask1, mask0)
272 CMPL(mask0, U32(0xFFFF))
273 CMOVQNE(next, w)
274 }
275
276 func (*dedupe32) vec() VecVirtual { return YMM() }
277
278 func (*dedupe32) vsize() int { return 32 }
279
280 func (*dedupe32) vlanes() int { return 8 }
281
282 func (d *dedupe32) vinit(p, w GPVirtual) {
283 move(VMOVDQU, YMM(), p, w)
284 d.nop = GP64()
285 d.inc = GP64()
286 XORQ(d.nop, d.nop)
287 MOVQ(U64(32), d.inc)
288 }
289
290 func (d *dedupe32) vcopy(src0, src1, dst VecVirtual, off GPVirtual) {
291 if src1 != dst {
292 VMOVDQA(src1, dst)
293 }
294 VPCMPEQQ(src1, src0, src0)
295 // This gives a bitmask with these possible values:
296 // * 0b0000
297 // * 0b0001
298 // * ...
299 // * 0b1111
300 //
301 // We only care about the last case because it indicates that the full 32
302 // bytes are equal.
303 //
304 // We want to divide by 15, which will either produce a result of 0 or 1.
305 // Rather than dividing, we add 1 and shift right by 4.
306 VMOVMSKPD(src0, off.As32())
307 CMPQ(off, Imm(15))
308 CMOVQEQ(d.nop, off)
309 CMOVQNE(d.inc, off)
310 }
311
312 func generateDedupe(dedupe dedupe) {
313 size := dedupe.size()
314 TEXT(fmt.Sprintf("dedupe%d", size), NOSPLIT, "func(dst, src []byte) int")
315
316 n := Load(Param("src").Len(), GP64())
317 CMPQ(n, Imm(0))
318 JE(LabelRef("short"))
319
320 dst := Load(Param("dst").Base(), GP64())
321 src := Load(Param("src").Base(), GP64())
322 // `p` is the read pointer that will be advanced through the input array
323 // testing for equal pairs.
324 //
325 // `w` points to the position in the output buffer where the next item
326 // is to be written.
327 p := GP64()
328 w := GP64()
329 MOVQ(src, p)
330 MOVQ(dst, w)
331 SUBQ(Imm(uint64(size)), n)
332
333 if avx, ok := dedupe.(dedupeAVX2); ok {
334 CMPQ(n, Imm(uint64(avx.vsize())))
335 JL(LabelRef("init"))
336 JumpIfFeature("avx2", cpu.AVX2)
337 }
338
339 Label("init")
340 dedupe.init(p, w)
341 ADDQ(Imm(uint64(size)), w)
342
343 Label("tail")
344 CMPQ(n, Imm(0))
345 JE(LabelRef("done"))
346
347 Label("generic")
348 dedupe.copy(p, w)
349 ADDQ(Imm(uint64(size)), p)
350 SUBQ(Imm(uint64(size)), n)
351 CMPQ(n, Imm(0))
352 JG(LabelRef("generic"))
353
354 Label("done")
355 SUBQ(dst, w)
356 Store(w, ReturnIndex(0))
357 RET()
358
359 Label("short")
360 Store(n, ReturnIndex(0))
361 RET()
362
363 if avx, ok := dedupe.(dedupeAVX2); ok {
364 avxLanes := avx.vlanes()
365 avxChunk := avx.vsize() * avxLanes
366 Label("avx2")
367
368 src := make([]VecVirtual, avxLanes)
369 dst := make([]VecVirtual, avxLanes)
370 off := make([]GPVirtual, avxLanes)
371 for i := range src {
372 src[i] = avx.vec()
373 dst[i] = avx.vec()
374 off[i] = GP64()
375 }
376
377 avx.vinit(p, w)
378 ADDQ(Imm(uint64(size)), w)
379
380 // This bit of magic aligns the tail chunk size on the first power of
381 // two smaller than the chunk size used in the loop.
382 //
383 // This is useful when the number of lanes in not a power of two.
384 tailChunk := 1 << (63 - bits.LeadingZeros(uint(avxChunk)))
385 if tailChunk == avxChunk {
386 tailChunk /= 2
387 }
388
389 CMPQ(n, U32(avxChunk))
390 if tailChunk >= avx.vsize() {
391 JL(LabelRef(fmt.Sprintf("avx2_tail%d", tailChunk)))
392 } else {
393 JL(LabelRef("avx2_tail"))
394 }
395
396 Label(fmt.Sprintf("avx2_loop%d", avxChunk))
397 generateDedupeAVX2(p, w, src, dst, off, avx)
398 ADDQ(U32(uint64(avxChunk)), p)
399 SUBQ(U32(uint64(avxChunk)), n)
400 CMPQ(n, U32(avxChunk))
401 JGE(LabelRef(fmt.Sprintf("avx2_loop%d", avxChunk)))
402
403 for chunk := tailChunk; chunk >= avx.vsize(); chunk /= 2 {
404 Label(fmt.Sprintf("avx2_tail%d", chunk))
405 CMPQ(n, Imm(uint64(chunk)))
406 if next := chunk / 2; next >= avx.vsize() {
407 JL(LabelRef(fmt.Sprintf("avx2_tail%d", chunk/2)))
408 } else {
409 JL(LabelRef("avx2_tail"))
410 }
411 lanes := chunk / avx.vsize()
412 generateDedupeAVX2(p, w, src[:lanes], dst[:lanes], off[:lanes], avx)
413 ADDQ(Imm(uint64(chunk)), p)
414 SUBQ(Imm(uint64(chunk)), n)
415 }
416
417 Label("avx2_tail")
418 VZEROUPPER()
419 JMP(LabelRef("tail"))
420 }
421 }
422
423 func generateDedupeX86(mov func(Op, Op), cmp func(Op, Op), reg func() GPVirtual, p, w GPVirtual, size int) {
424 next := GP64()
425 MOVQ(w, next)
426 ADDQ(Imm(uint64(size)), next)
427 r0, r1 := reg(), reg()
428 mov(Mem{Base: p}, r0)
429 mov(Mem{Base: p}.Offset(size), r1)
430 mov(r1, Mem{Base: w})
431 cmp(r0, r1)
432 CMOVQNE(next, w)
433 }
434
435 func generateDedupeAVX2(p, w GPVirtual, src, dst []VecVirtual, off []GPVirtual, dedupe dedupeAVX2) {
436 size := dedupe.size()
437 step := dedupe.vsize()
438 moves := make(map[int]VecVirtual)
439
440 for i := range src {
441 VMOVDQU(Mem{Base: p}.Offset(i*step), src[i])
442 moves[i*step] = src[i]
443 }
444
445 reg := make([]VecVirtual, len(src))
446
447 for i := range dst {
448 // Elide moves from memory if possible by reusing registers that
449 // already contain the required data chunk.
450 //
451 // Care must be given in the implementations of AVX2 specializations
452 // not to write to the second source, as it may unexpectedly mutate
453 // the src0 or dst registers.
454 if prev := moves[i*step+size]; prev != nil {
455 reg[i] = prev
456 } else {
457 reg[i] = dst[i]
458 VMOVDQU(Mem{Base: p}.Offset(i*step+size), dst[i])
459 }
460 }
461
462 for i := range src {
463 dedupe.vcopy(src[i], reg[i], dst[i], off[i])
464 if i > 0 {
465 // Compute the cumulative offsets so we can use indexes relative to the
466 // write pointer, which allows the CPU to pipeline the writes to memory.
467 //
468 // There are still strong data dependencies between these instructions,
469 // but I'm not sure there is a great alternative. Moving the values to a
470 // vector register and using SIMD seems like a lost of heavy lifting for
471 // the limited number of registers we have.
472 ADDQ(off[i-1], off[i])
473 }
474 }
475
476 for i := range dst {
477 if i == 0 {
478 VMOVDQU(dst[i], Mem{Base: w})
479 } else {
480 VMOVDQU(dst[i], Mem{Base: w}.Idx(off[i-1], 1))
481 }
482 }
483
484 ADDQ(off[len(off)-1], w)
485 }
486
487 func move(mov func(Op, Op), tmp Register, src, dst GPVirtual) {
488 mov(Mem{Base: src}, tmp)
489 mov(tmp, Mem{Base: dst})
490 }