interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / om / scan / scan_ir.om

682 lines · 15946 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note scan_ir.om: the CUDA++ scan and radix-sort kernels (row C3 of docs/OMEGA.md) for the GB10 (sm_121), emitted as
3note BlackwellIR records. Kinds 0..4 reproduce the C generator gen() of src/omega_gpu_scan.c record for record
4note (the C stays as the migration-pending differential oracle, CUDAPP_MIGRATION_PENDING_C_SCAN):
5note kind 0/1/2 one reduction level (sum/max/min, f32), 3 one f32 tile-scan level, 4 the f32 carry pass,
6note 5 one u32 tile-scan level, 6 the u32 carry pass, 7 radix-sort digit histogram, 8 radix-sort scatter of keys,
7note 9 radix-sort scatter of key-value pairs (values in the second half of the buffer).
8note Arguments at constant bank 0x380: in u64, out u64, tot u64, len, tiles, warps, shift (kinds 7..9).
9note Record (8 ulong): op, dst, src1, src2, src3 (vreg or 2^64-1 for none), ureg, imm | control << 32,
10note uniform | p0 << 1. Control words follow src/omega_bw_em.h (emit kinds, region, tail; no pair schedule).
11note Meta: 0 records, 1 vregs, 2 uvregs, 3 pending wait mask, 4 error, 5 closed loops, 6 open loops,
12note 8..11 open loop starts, 16 + 2L / 17 + 2L closed loop L start / end, 64 + v vreg kind, 320 + u uvreg kind.
13note Mutants (red checks, argument a): 1 the reduction skips d = 1, 2 the tile scans skip d = 16, 3 the carry adds
14note S[tile] instead of S[tile - 1], 4 the scatter rank counts the element itself, 5 the scatter ranks in lane
15note order instead of position order (unstable), 6 the histogram scan skips d = 16.
16
17const NONE ulong 65535
18const ALL ulong 18446744073709551615
19const LO32 ulong 4294967295
20const YIELD ulong 8192
21const NOPC ulong 1032192
22const PADSUM ulong 2147483648
23const PADMM ulong 2143289344
24const KFIX ulong 0
25const KPRED ulong 1
26const KVAR ulong 2
27const KVARM ulong 3
28const KUVAR ulong 4
29const KSTORE ulong 5
30const KEXIT ulong 6
31const KBRA ulong 7
32const KBSSY ulong 8
33const KBSYNC ulong 9
34const KRAW ulong 10
35const MOVRZ ulong 1
36const MOVI ulong 2
37const S2R ulong 3
38const LDC ulong 4
39const LDC64 ulong 5
40const LDCU64 ulong 7
41const IMAD ulong 8
42const IMADW ulong 9
43const LDG ulong 11
44const STG ulong 12
45const IADD3 ulong 14
46const SHFR ulong 17
47const LAND ulong 18
48const ISGEU ulong 28
49const FADD ulong 30
50const FMIN ulong 36
51const FMAX ulong 37
52const SHFLD ulong 42
53const EXIT ulong 45
54const BRA ulong 46
55const LUT ulong 51
56const SHFLU ulong 52
57const BSSY ulong 54
58const BSYNC ulong 55
59
60note virtual registers, in the allocation order of gen() (uvreg 0 is the global memory descriptor)
61const UD ulong 0
62const PIN ulong 0
63const POUT ulong 1
64const PTOT ulong 2
65const LEN ulong 3
66const TILES ulong 4
67const WARPS ulong 5
68const TID ulong 6
69const CTA ulong 7
70const LANE ulong 8
71const GW ulong 9
72const ONE ulong 10
73const TILE ulong 11
74const EL ulong 12
75const V ulong 13
76const T ulong 14
77const A64 ulong 15
78const B64 ulong 16
79const RL ulong 17
80const NZ ulong 18
81const C31 ulong 19
82const M1 ulong 20
83const OFF ulong 21
84const TM1 ulong 22
85const MASK ulong 23
86note the sort kernels' further registers (after the five masks)
87const SH ulong 28
88const CJ1 ulong 29
89const CJ2 ulong 30
90const C3 ulong 31
91const KEY ulong 32
92const DIG ulong 33
93const D3 ulong 34
94const VAL ulong 35
95const Q ulong 36
96const X ulong 37
97const R0 ulong 38
98const MQ0 ulong 42
99const ACC ulong 46
100const VV ulong 47
101const DST ulong 48
102
103note ---- record building
104
105fn cw st ulong wb ulong rb ulong wt ulong to ulong
106ret or or shl st 9 shl wb 14 or shl rb 17 shl wt 20
107end
108
109fn fld r ulong to ulong
110if eq r NONE
111ret ALL
112end
113ret r
114end
115
116fn r4 d ulong a ulong b ulong c ulong to ulong
117ret or or d shl a 16 or shl b 32 shl c 48
118end
119
120fn okk op ulong k ulong fl ulong ur ulong to ulong
121ret or or op shl k 8 or shl fl 16 shl ur 40
122end
123
124note control word of one record kind (src/omega_bw_em.h emit): returns control | next pending mask << 32
125fn kc k ulong fl ulong pend ulong imm ulong to ulong
126if eq k KPRED
127ret cw 14 7 7 pend
128end
129if eq k KVAR
130ret or or cw 4 0 1 pend YIELD shl ulong 3 32
131end
132if eq k KVARM
133ret or or cw 4 0 1 and pend 2 YIELD shl or pend 3 32
134end
135if eq k KUVAR
136ret or or cw 4 2 7 pend YIELD shl ulong 4 32
137end
138if eq k KSTORE
139ret or or cw 4 7 1 pend YIELD shl ulong 2 32
140end
141if or eq k KEXIT eq k KBRA
142if ne and fl 2 0
143ret or cw 5 7 7 pend shl pend 32
144end
145ret cw 5 7 7 pend
146end
147if eq k KBSSY
148ret or cw 1 7 7 pend YIELD
149end
150if eq k KBSYNC
151ret or cw 5 7 7 pend YIELD
152end
153if eq k KRAW
154ret or shr imm 32 shl pend 32
155end
156ret cw 6 7 7 pend
157end
158
159note one record: ok = op | kind << 8 | flags << 16 | ureg << 40; rg = r4 operands; imm low 32 bits
160note (a raw record carries its control word in imm bits 32..63)
161note record words 0..5 at b: op and the operand fields of rg, ureg from ok
162fn wops ins edit ulong 8192 b ulong ok ulong rg ulong to void
163put ins b and ok 255
164put ins add b 1 fld and rg 65535
165put ins add b 2 fld and shr rg 16 65535
166put ins add b 3 fld and shr rg 32 65535
167put ins add b 4 fld shr rg 48
168put ins add b 5 fld and shr ok 40 65535
169end
170
171fn emit ins edit ulong 8192 m edit ulong 512 ok ulong rg ulong imm ulong to void
172n = get m 0
173if ge n 1024
174put m 4 1
175ret
176end
177x = and shr ok 8 255
178if gt x KRAW
179put m 4 2
180ret
181end
182x = kc x and shr ok 16 255 get m 3 imm
183put m 0 add n 1
184n = mul n 8
185wops ins n ok rg
186put ins add n 6 or and imm LO32 shl and x LO32 32
187put ins add n 7 and shr ok 16 7
188put m 3 shr x 32
189end
190
191fn nv m edit ulong 512 kind ulong to void
192v = get m 1
193if ge v 256
194put m 4 3
195ret
196end
197put m add 64 v kind
198put m 1 add v 1
199end
200
201note ---- instruction helpers (the names of src/omega_bw_em.h)
202
203fn fix ins edit ulong 8192 m edit ulong 512 op ulong rg ulong imm ulong to void
204emit ins m okk op KFIX 0 NONE rg imm
205end
206
207fn movi ins edit ulong 8192 m edit ulong 512 d ulong imm ulong to void
208fix ins m MOVI r4 d NONE NONE NONE imm
209end
210
211fn movrz ins edit ulong 8192 m edit ulong 512 d ulong to void
212fix ins m MOVRZ r4 d NONE NONE NONE 0
213end
214
215note d = a * imm + c
216fn imadi ins edit ulong 8192 m edit ulong 512 d ulong a ulong imm ulong c ulong to void
217fix ins m IMAD r4 d a NONE c imm
218end
219
220note d = a * b + c
221fn imadr ins edit ulong 8192 m edit ulong 512 d ulong a ulong b ulong c ulong to void
222fix ins m IMAD r4 d a b c 0
223end
224
225note d:d+1 = a * imm + c:c+1
226fn imadw ins edit ulong 8192 m edit ulong 512 d ulong a ulong imm ulong c ulong to void
227fix ins m IMADW r4 d a NONE c imm
228end
229
230fn shri ins edit ulong 8192 m edit ulong 512 d ulong a ulong imm ulong to void
231fix ins m SHFR r4 d a NONE NONE imm
232end
233
234fn andi ins edit ulong 8192 m edit ulong 512 d ulong a ulong imm ulong to void
235fix ins m LAND r4 d a NONE NONE imm
236end
237
238note d = a << b (b a register below 32)
239fn shlr ins edit ulong 8192 m edit ulong 512 d ulong a ulong b ulong to void
240fix ins m SHFLU r4 d a b NONE 0
241end
242
243note LOP3(a, b, c, lut) with a = 0xF0, b = 0xCC, c = 0xAA; rg = r4 d a b c (a function takes at most 6 parameters)
244fn lop3 ins edit ulong 8192 m edit ulong 512 rg ulong f ulong to void
245fix ins m LUT rg f
246end
247
248note d = a op b for the fixed two-operand ops (IADD3, FADD, FMNMX)
249fn two ins edit ulong 8192 m edit ulong 512 op ulong d ulong a ulong b ulong to void
250fix ins m op r4 d a b NONE 0
251end
252
253note the variable-latency reads: op LDC, LDC64, S2R (imm offset or special register)
254fn vimm ins edit ulong 8192 m edit ulong 512 op ulong d ulong imm ulong to void
255emit ins m okk op KVAR 0 NONE r4 d NONE NONE NONE imm
256end
257
258fn ldcu64 ins edit ulong 8192 m edit ulong 512 imm ulong to void
259emit ins m okk LDCU64 KUVAR 1 NONE r4 UD NONE NONE NONE imm
260end
261
262note d = global u32 at a:a+1; first 1 starts a load group, 0 joins it
263fn ldg32 ins edit ulong 8192 m edit ulong 512 d ulong a ulong first ulong to void
264k = KVARM
265if ne first 0
266k = KVAR
267end
268emit ins m okk LDG k 0 UD r4 d a NONE NONE 0
269end
270
271fn stg ins edit ulong 8192 m edit ulong 512 a ulong imm ulong v ulong to void
272emit ins m okk STG KSTORE 0 UD r4 NONE a v NONE imm
273end
274
275fn shfl ins edit ulong 8192 m edit ulong 512 d ulong a ulong off ulong to void
276emit ins m okk SHFLD KVAR 0 NONE r4 d a NONE NONE off
277end
278
279note P0 = a >= b (unsigned)
280fn isetp ins edit ulong 8192 m edit ulong 512 a ulong b ulong to void
281emit ins m okk ISGEU KPRED 0 NONE r4 NONE a b NONE 0
282end
283
284note ---- branches, regions and loops
285
286note @P0 BRA forward; returns its record index for patch
287fn bfwd ins edit ulong 8192 m edit ulong 512 to ulong
288at = get m 0
289emit ins m okk BRA KBRA 2 NONE r4 NONE NONE NONE NONE 1
290ret at
291end
292
293fn patch ins edit ulong 8192 at ulong tgt ulong to void
294b = add mul at 8 6
295x = get ins b
296put ins b or and x 18446744069414584320 and sub tgt at LO32
297end
298
299note a region the lanes with P0 set skip (BSSY, @P0 BRA); returns bssy | bra << 16
300fn rbeg ins edit ulong 8192 m edit ulong 512 to ulong
301at = get m 0
302emit ins m okk BSSY KBSSY 0 NONE r4 NONE NONE NONE NONE 2
303b = bfwd ins m
304ret or at shl b 16
305end
306
307fn rend ins edit ulong 8192 m edit ulong 512 r ulong to void
308at = get m 0
309emit ins m okk BSYNC KBSYNC 0 NONE r4 NONE NONE NONE NONE 0
310patch ins shr r 16 at
311patch ins and r 65535 add at 1
312end
313
314fn bra ins edit ulong 8192 m edit ulong 512 top ulong to void
315emit ins m okk BRA KBRA 0 NONE r4 NONE NONE NONE NONE and sub top get m 0 LO32
316end
317
318fn tail ins edit ulong 8192 m edit ulong 512 to void
319emit ins m okk EXIT KEXIT 0 NONE r4 NONE NONE NONE NONE 0
320emit ins m okk BRA KRAW 0 NONE r4 NONE NONE NONE NONE shl NOPC 32
321end
322
323fn lbeg m edit ulong 512 to void
324d = get m 6
325if ge d 4
326put m 4 4
327ret
328end
329put m add 8 d get m 0
330put m 6 add d 1
331end
332
333fn lend m edit ulong 512 to void
334d = get m 6
335nl = get m 5
336if or eq d 0 ge nl 24
337put m 4 5
338ret
339end
340d1 = sub d 1
341put m add 16 mul nl 2 get m add 8 d1
342put m add 17 mul nl 2 sub get m 0 1
343put m 5 add nl 1
344put m 6 d1
345end
346
347note ---- the kernels
348
349fn istile k ulong to bool
350ret or eq k 3 eq k 5
351end
352
353fn issort k ulong to bool
354ret ge k 7
355end
356
357note the registers of gen(), then (sort) SH .. DST
358fn regs m edit ulong 512 k ulong to void
359u = get m 2
360put m add 320 u 2
361put m 2 add u 1
362nv m 2
363nv m 2
364nv m 2
365for i ulong 12
366nv m 1
367end
368nv m 2
369nv m 2
370for i ulong 6
371nv m 1
372end
373if or istile k issort k
374for i ulong 5
375nv m 1
376end
377end
378if issort k
379for i ulong 21
380nv m 1
381end
382end
383end
384
385fn prolog ins edit ulong 8192 m edit ulong 512 k ulong to void
386ldcu64 ins m 856
387vimm ins m LDC64 PIN 896
388if ne k 7
389vimm ins m LDC64 POUT 904
390end
391if or istile k issort k
392vimm ins m LDC64 PTOT 912
393end
394vimm ins m LDC LEN 920
395vimm ins m LDC TILES 924
396vimm ins m LDC WARPS 928
397if issort k
398vimm ins m LDC SH 932
399end
400vimm ins m S2R TID 33
401vimm ins m S2R CTA 37
402movi ins m ONE 1
403movi ins m M1 LO32
404if or eq k 5 eq k 6
405movi ins m NZ 0
406else
407movi ins m NZ PADSUM
408end
409andi ins m LANE TID 31
410shri ins m T TID 5
411imadi ins m GW CTA 8 T
412if or eq k 4 eq k 6
413imadi ins m TILE GW 1 ONE
414else
415imadi ins m TILE GW 1 NONE
416end
417if or istile k issort k
418movi ins m C31 31
419lop3 ins m r4 RL LANE C31 NONE 60
420for s ulong 5
421movi ins m T shl 1 s
422two ins m IADD3 add MASK s LANE T
423shri ins m add MASK s add MASK s 5
424imadi ins m add MASK s add MASK s 1 M1
425end
426end
427if issort k
428movi ins m CJ1 1
429movi ins m CJ2 2
430movi ins m C3 3
431end
432end
433
434fn red ins edit ulong 8192 m edit ulong 512 k ulong mut ulong to void
435imadi ins m EL TILE 32 LANE
436if eq k 0
437movi ins m V PADSUM
438else
439movi ins m V PADMM
440end
441isetp ins m EL LEN
442r = rbeg ins m
443imadw ins m A64 EL 4 PIN
444ldg32 ins m V A64 1
445rend ins m r
446op = FADD
447if eq k 1
448op = FMAX
449end
450if eq k 2
451op = FMIN
452end
453d = ulong 16
454for i ulong 5
455if or ne mut 1 ne d 1
456shfl ins m T V d
457two ins m op V V T
458end
459d = shr d 1
460end
461isetp ins m LANE ONE
462r = rbeg ins m
463imadw ins m A64 TILE 4 POUT
464stg ins m A64 0 V
465rend ins m r
466end
467
468fn tilek ins edit ulong 8192 m edit ulong 512 k ulong mut ulong to void
469op = FADD
470pad = PADSUM
471if eq k 5
472op = IADD3
473pad = ulong 0
474end
475imadi ins m EL TILE 32 RL
476movi ins m V pad
477isetp ins m EL LEN
478r = rbeg ins m
479imadw ins m A64 EL 4 PIN
480ldg32 ins m V A64 1
481rend ins m r
482for s ulong 5
483if or ne mut 2 ne s 4
484shfl ins m T V shl 1 s
485lop3 ins m r4 T T add MASK s NZ 226
486two ins m op V T V
487end
488end
489isetp ins m EL LEN
490r = rbeg ins m
491imadw ins m A64 EL 4 POUT
492stg ins m A64 0 V
493rend ins m r
494isetp ins m LANE ONE
495r = rbeg ins m
496imadw ins m A64 TILE 4 PTOT
497stg ins m A64 0 V
498rend ins m r
499end
500
501fn addk ins edit ulong 8192 m edit ulong 512 k ulong mut ulong to void
502op = FADD
503if eq k 6
504op = IADD3
505end
506imadi ins m EL TILE 32 LANE
507isetp ins m EL LEN
508r = rbeg ins m
509if eq mut 3
510imadi ins m TM1 TILE 1 NONE
511else
512imadi ins m TM1 TILE 1 M1
513end
514imadw ins m B64 TM1 4 POUT
515imadw ins m A64 EL 4 PIN
516ldg32 ins m OFF B64 1
517ldg32 ins m V A64 0
518two ins m op V OFF V
519stg ins m A64 0 V
520rend ins m r
521end
522
523note the digit of KEY (bits shift..shift+3 counted from the top: (KEY << SH) >> 28), its byte one-hot VAL in the
524note counter word Q = digit >> 2, and MQj = all ones when Q == j: (((Q ^ j) + 3) >> 2) - 1
525fn digit ins edit ulong 8192 m edit ulong 512 to void
526shlr ins m X KEY SH
527shri ins m DIG X 28
528andi ins m D3 DIG 3
529imadi ins m X D3 8 NONE
530shlr ins m VAL ONE X
531shri ins m Q DIG 2
532for j ulong 4
533if eq j 0
534two ins m IADD3 X Q C3
535else
536lop3 ins m r4 X Q add CJ1 sub j 1 NONE 60
537two ins m IADD3 X X C3
538end
539shri ins m X X 2
540imadi ins m add MQ0 j X 1 M1
541lop3 ins m r4 add R0 j VAL add MQ0 j NONE 192
542end
543end
544
545note per lane: the four counter words R0..R3 (16 digit counters of 8 bits) summed over the tile positions up to
546note this lane's (an inclusive scan in reversed lanes, as the tile scan); lane 0 ends with the tile's totals
547fn count ins edit ulong 8192 m edit ulong 512 k ulong mut ulong to void
548if and ne k 7 eq mut 5
549imadi ins m EL TILE 32 LANE
550else
551imadi ins m EL TILE 32 RL
552end
553for j ulong 4
554movrz ins m add R0 j
555end
556isetp ins m EL LEN
557r = rbeg ins m
558imadw ins m A64 EL 4 PIN
559ldg32 ins m KEY A64 1
560digit ins m
561rend ins m r
562for s ulong 5
563if or or ne k 7 ne mut 6 ne s 4
564for j ulong 4
565shfl ins m T add R0 j shl 1 s
566lop3 ins m r4 T T add MASK s NONE 192
567two ins m IADD3 add R0 j T add R0 j
568end
569end
570end
571end
572
573note lane 0 stores the 16 tile counts digit-major: cnt[1 + dig * tiles + tile] (cnt = PTOT), and cnt[0] = 0
574note (every tile's lane 0 stores the same zero, so the exclusive prefix the scatter reads starts at 0)
575fn hist ins edit ulong 8192 m edit ulong 512 to void
576isetp ins m LANE ONE
577r = rbeg ins m
578for dg ulong 16
579src = add R0 shr dg 2
580b = mul and dg 3 8
581if eq b 0
582andi ins m X src 255
583else
584shri ins m X src b
585andi ins m X X 255
586end
587imadi ins m DST TILES dg TILE
588imadw ins m A64 DST 4 PTOT
589stg ins m A64 4 X
590end
591movrz ins m X
592stg ins m PTOT 0 X
593rend ins m r
594end
595
596note out[cnt[dig * tiles + tile] + rank] = key, rank = the earlier keys of the tile with the same digit
597fn scat ins edit ulong 8192 m edit ulong 512 k ulong mut ulong to void
598isetp ins m EL LEN
599r = rbeg ins m
600lop3 ins m r4 ACC R0 MQ0 NONE 192
601for j ulong 3
602lop3 ins m r4 ACC add R0 add j 1 add MQ0 add j 1 ACC 234
603end
604lop3 ins m r4 X D3 C3 NONE 60
605imadi ins m X X 8 NONE
606shlr ins m X ACC X
607shri ins m X X 24
608if eq mut 4
609imadi ins m X X 1 NONE
610else
611imadi ins m X X 1 M1
612end
613imadr ins m DST DIG TILES TILE
614imadw ins m A64 DST 4 PTOT
615if eq k 9
616two ins m IADD3 T EL LEN
617imadw ins m B64 T 4 PIN
618end
619ldg32 ins m DST A64 1
620if eq k 9
621ldg32 ins m VV B64 0
622end
623two ins m IADD3 DST DST X
624imadw ins m A64 DST 4 POUT
625stg ins m A64 0 KEY
626if eq k 9
627two ins m IADD3 T DST LEN
628imadw ins m B64 T 4 POUT
629stg ins m B64 0 VV
630end
631rend ins m r
632end
633
634fn kern ins edit ulong 8192 m edit ulong 512 k ulong mut ulong to void
635regs m k
636prolog ins m k
637top = get m 0
638lbeg m
639isetp ins m TILE TILES
640done = bfwd ins m
641if le k 2
642red ins m k mut
643end
644if istile k
645tilek ins m k mut
646end
647if or eq k 4 eq k 6
648addk ins m k mut
649end
650if issort k
651count ins m k mut
652if eq k 7
653hist ins m
654else
655scat ins m k mut
656end
657end
658two ins m IADD3 TILE TILE WARPS
659bra ins m top
660lend m
661patch ins done get m 0
662tail ins m
663end
664
665note ---- entry: kind 0..9, a mutant 0..6 (b, c 0). Returns 0 built, 1 refused arguments, 2 emitter error
666note (meta 4 holds the code).
667fn scan_ir kind ulong a ulong b ulong c ulong ins edit ulong 8192 meta edit ulong 512 to ulong
668for i ulong 512
669put meta i 0
670end
671if or or gt kind 9 gt a 6 or ne b 0 ne c 0
672ret 1
673end
674kern ins meta kind a
675if ne get meta 4 0
676ret 2
677end
678if ne get meta 6 0
679ret 2
680end
681ret 0
682end