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 / encoder / bw_enc_sm121.om

679 lines · 15513 bytes Raw Blame History
1note bw_enc_sm121.om: the sm_121 instruction encoder of CUDApp (aien-dev/CUDApp#79, OM-GPU-RETIRE).
2note It turns a solved BlackwellIR program into machine code: four 32-bit words per instruction,
3note little-endian, padded with NOPs to a multiple of 8 and at least 32 instructions.
4note It replaces encode_single_insn and omega_bw_encode_program of src/omega_blackwell_codegen.c,
5note which stays compiled only behind CUDAPP_MIGRATION_PENDING_C_ENCODER as the differential oracle
6note or the explicit CUDAPP_MIGRATION_PENDING_C_NO_POSIX oracle switch; both defaults use Omega.
7note
8note Input (bw_om_encode in src/omega_blackwell_codegen.c packs it; 8 ulong per instruction, at most 1024 instructions):
9note 0 op (the BlackwellIROpcode number) 1 dst_vreg 2 src1_vreg 3 src2_vreg 4 src3_vreg
10note 5 ureg (the five vreg fields are C int, passed as 64-bit two's complement: negative = none)
11note 6 imm or control shl 32
12note 7 is_uniform or predicate_p0 shl 1 or predicate_not shl 2 or bar_reg shl 8
13note or dst_subreg shl 16 or src1_subreg shl 24 or src2_subreg shl 32 or src3_subreg shl 40
14note phys: vreg_to_phys 0..255 then uvreg_to_phys at 256..287 (C int, two's complement).
15note pad 1: omega_bw_encode_program (pad to the multiple of 8, at least 32); pad 0: exactly n instructions.
16note Result: the bytes written, or 0 when an instruction is refused (unknown op, an offset outside its
17note field, a BSSY delta below 2, a barrier register above 15, a P0 guard on LDCU / LDCU.64) or n is above 1024.
18note Every constant is the decimal form of the C encoder's hex word (conversions by Wolfram,
19note evidence/CUDAPP-OM-ENCODER/wolfram-constants.txt).
20
21const NEG ulong 9223372036854775808
22const NOPW ulong 31000
23const NOPC ulong 1032192
24
25fn put4 w edit ulong 4 a ulong b ulong c ulong d ulong to void
26put w 0 a
27put w 1 b
28put w 2 c
29put w 3 d
30end
31
32note the physical register of one operand field: phys + subreg, or dflt when the vreg is negative
33fn preg phys view ulong 288 v ulong sb ulong dflt ulong to ulong
34if ge v NEG
35ret dflt
36end
37ret add get phys v sb
38end
39
40note 1 when a vreg field is given (not negative), else 0
41fn pres v ulong to ulong
42if ge v NEG
43ret 0
44end
45ret 1
46end
47
48note Only constant loads have uniform destinations; IMAD uses is_uniform for its source.
49fn isuld op ulong to bool
50ret or eq op 6 eq op 7
51end
52
53fn opnds ins view ulong 8192 i ulong phys view ulong 288 o edit ulong 16 to void
54b = mul i 8
55f = get ins add b 7
56put o 0 get ins b
57put o 1 preg phys get ins add b 1 and shr f 16 255 0
58put o 2 preg phys get ins add b 2 and shr f 24 255 0
59put o 3 preg phys get ins add b 3 and shr f 32 255 0
60put o 4 preg phys get ins add b 4 and shr f 40 255 255
61u = get ins add b 5
62put o 5 0
63if lt u NEG
64put o 5 get phys add u 256
65end
66put o 14 pres u
67x = get ins add b 6
68put o 6 and x 4294967295
69put o 7 shr x 32
70put o 8 and f 1
71put o 9 and shr f 1 1
72put o 10 and shr f 2 1
73put o 11 and shr f 8 255
74put o 12 pres get ins add b 2
75put o 13 pres get ins add b 3
76d = get ins add b 1
77put o 15 pres d
78if and isuld get o 0 lt d NEG
79put o 1 get phys add d 256
80end
81end
82
83note the control word: the instruction's own, else the op's default
84fn ctl o view ulong 16 dflt ulong to ulong
85c = get o 7
86if ne c 0
87ret c
88end
89ret dflt
90end
91
92note low 8 bits of operand slot k
93fn rb o view ulong 16 k ulong to ulong
94ret and get o k 255
95end
96
97note slot k when its vreg (presence slot h) is given, else RZ
98fn rz o view ulong 16 k ulong h ulong to ulong
99if ne get o h 0
100ret rb o k
101end
102ret 255
103end
104
105fn wd o view ulong 16 base ulong to ulong
106ret or base shl rb o 1 16
107end
108
109fn wa o view ulong 16 base ulong to ulong
110ret or base shl rb o 2 24
111end
112
113fn wda o view ulong 16 base ulong to ulong
114ret or wd o base shl rb o 2 24
115end
116
117note the 4-bit guard predicate field: 7 always, 0 @P0, 8 @!P0
118fn pred o view ulong 16 to ulong
119if eq get o 9 0
120ret 7
121end
122if eq get o 10 0
123ret 0
124end
125ret 8
126end
127
128note a signed 24-bit byte offset (imm read as int32) fits
129fn off24 imm ulong to bool
130ret or lt imm 8388608 ge imm 4286578688
131end
132
133note ops 0..14: moves, constants, integer multiply-add, compares and global memory
134fn enc_a o view ulong 16 w edit ulong 4 to ulong
135op = get o 0
136imm = get o 6
137if eq op 0
138put4 w NOPW 0 0 ctl o NOPC
139ret 0
140end
141if eq op 1
142put4 w wd o 29186 255 3840 ctl o 1040896
143ret 0
144end
145if eq op 2
146put4 w wd o 30722 imm 3840 ctl o 1040896
147ret 0
148end
149if eq op 3
150put4 w wd o 31001 0 shl and imm 255 8 ctl o 926208
151ret 0
152end
153if eq op 4
154put4 w wd o 4278221698 and shl imm 6 16776960 2048 ctl o 1040896
155ret 0
156end
157if eq op 5
158put4 w wd o 4278221698 and shl imm 6 16776960 2560 ctl o 926208
159ret 0
160end
161if eq op 6
162put4 w wd o 4278220716 and shl imm 5 16776960 134219776 ctl o 926208
163ret 0
164end
165if eq op 7
166put4 w wd o 4278220716 and shl imm 5 16776960 134220288 ctl o 945664
167ret 0
168end
169if eq op 8
170if ne get o 13 0
171put4 w wda o 29220 rb o 3 or 126747136 rb o 4 ctl o 2083328
172ret 0
173end
174if and ne get o 8 0 ne get o 14 0
175put4 w wda o 31780 rb o 5 or 260964864 rb o 4 ctl o 2083328
176ret 0
177end
178put4 w wda o 30756 imm or 126747136 rb o 4 ctl o 2083328
179ret 0
180end
181if eq op 9
182put4 w wda o 30757 imm or 126746624 rb o 4 ctl o 2083840
183ret 0
184end
185if eq op 10
186if and ne get o 8 0 ne get o 14 0
187put4 w wda o 31756 rb o 5 200302704 ctl o 3136000
188ret 0
189end
190put4 w wda o 30732 imm 66085488 ctl o 2087424
191ret 0
192end
193if eq op 11
194note Existing C LDG.E signed byte-offset form (#373), identical to the wide-load field below.
195if not off24 imm
196ret 1
197end
198put4 w wda o 31105 or rb o 5 shl and imm 16777215 8 203299072 ctl o 991744
199ret 0
200end
201if eq op 12
202if not off24 imm
203ret 1
204end
205put4 w wa o 31110 or rb o 3 shl and imm 16777215 8 or 202381568 rb o 5 ctl o 1040896
206ret 0
207end
208if eq op 13
209put4 w wa o 31110 rb o 3 or 202447104 rb o 5 ctl o 1040896
210ret 0
211end
212if eq op 14
213put4 w wda o 29200 rb o 3 134209791 ctl o 17811968
214ret 0
215end
216ret 1
217end
218
219note ops 15..29: tensor core, shifts, wide and strong loads, stores, fences, atomics
220fn enc_b o view ulong 16 w edit ulong 4 to ulong
221op = get o 0
222imm = get o 6
223if or eq op 15 eq op 16
224p = ulong 0
225if eq op 16
226p = 262144
227end
228put4 w wda o 29244 rb o 3 or or rb o 4 6144 p ctl o 1040896
229ret 0
230end
231if eq op 17
232put4 w or wd o 30745 4278190080 imm or 71168 rb o 2 ctl o 2083328
233ret 0
234end
235if eq op 18
236put4 w wda o 30738 imm 126796031 ctl o 2083328
237ret 0
238end
239if eq op 19
240put4 w wda o 31105 rb o 5 203298048 ctl o 991744
241ret 0
242end
243if eq op 20
244put4 w wda o 31105 rb o 5 203380992 ctl o 991744
245ret 0
246end
247if eq op 21
248put4 w wda o 31105 rb o 5 203364608 ctl o 991744
249ret 0
250end
251if eq op 22
252a = wa o 31110
253if ne get o 9 0
254a = or wa o 2438 shl pred o 12
255end
256put4 w a rb o 3 or 202463488 rb o 5 ctl o 1040896
257ret 0
258end
259if eq op 23
260put4 w 31122 0 45056 ctl o 1043456
261ret 0
262end
263if eq op 24
264put4 w 31122 0 12288 ctl o 1043456
265ret 0
266end
267if eq op 25
268put4 w 4278221199 0 33554432 ctl o 1042432
269ret 0
270end
271if eq op 26
272put4 w wda o 31144 or rb o 3 2147483648 or 136270080 rb o 5 ctl o 3284480
273ret 0
274end
275if eq op 27
276put4 w wda o 31144 or rb o 3 2147483648 or 203378944 rb o 5 ctl o 3284480
277ret 0
278end
279if eq op 28
280put4 w wa o 29196 rb o 3 66084976 ctl o 2087424
281ret 0
282end
283if eq op 29
284put4 w wda o 29202 rb o 3 126762239 ctl o 2083328
285ret 0
286end
287ret 1
288end
289
290note ops 30..45: floating point, special functions, shuffles, shared memory, exit
291fn enc_c o view ulong 16 w edit ulong 4 to ulong
292op = get o 0
293imm = get o 6
294if eq op 30
295put4 w wda o 29217 rb o 3 0 ctl o 5227520
296ret 0
297end
298if eq op 31
299put4 w wda o 29217 or rb o 3 2147483648 0 ctl o 5227520
300ret 0
301end
302if eq op 32
303put4 w wda o 29216 rb o 3 4194304 ctl o 5227520
304ret 0
305end
306if eq op 33
307put4 w wda o 29219 rb o 3 rb o 4 ctl o 5227520
308ret 0
309end
310if eq op 34
311c = ulong 66084864
312if ne imm 0
313c = imm
314end
315a = or or 29195 shl and get o 1 7 16 shl rb o 2 24
316put4 w a rb o 3 c ctl o 5227520
317ret 0
318end
319if eq op 35
320put4 w wda o 29192 rb o 3 and get o 4 7 ctl o 1034752
321ret 0
322end
323if eq op 36
324put4 w wda o 29193 rb o 3 58720256 ctl o 5227520
325ret 0
326end
327if eq op 37
328put4 w wda o 29193 rb o 3 125829120 ctl o 5227520
329ret 0
330end
331if eq op 38
332put4 w wd o 29253 rb o 2 2102272 ctl o 5235200
333ret 0
334end
335if eq op 39
336put4 w wd o 29445 rb o 2 2158848 ctl o 5120512
337ret 0
338end
339if eq op 40
340put4 w wd o 29448 rb o 2 4096 ctl o 926720
341ret 0
342end
343if eq op 41
344put4 w wd o 29448 rb o 2 5120 ctl o 926720
345ret 0
346end
347if eq op 42
348put4 w wda o 32649 or 134225664 shl and imm 31 21 917504 ctl o 926720
349ret 0
350end
351if eq op 43
352put4 w wda o 31108 0 and imm 65535 ctl o 1040896
353ret 0
354end
355if eq op 44
356put4 w wa o 29576 rb o 3 and imm 65535 ctl o 1040896
357ret 0
358end
359if eq op 45
360a = ulong 31053
361put4 w a 0 58720256 ctl o 1042944
362ret 0
363end
364ret 1
365end
366
367note op 46: BRA. rel = imm - 1 as int32; low 6 bits at w0 18..23, rel arithmetic-shifted by 6 at w1 bit 2
368fn enc_bra o view ulong 16 w edit ulong 4 to ulong
369imm = get o 6
370rel = and sub imm 1 4294967295
371s = shr rel 6
372c = ulong 58720256
373if ge rel 2147483648
374s = or s 4227858432
375c = 58982399
376end
377d = get o 7
378if eq d 0
379d = 1042944
380if and eq imm 0 eq get o 9 0
381d = NOPC
382end
383end
384a = or or 2375 shl pred o 12 shl and rel 63 18
385put4 w a and shl s 2 4294967295 c d
386ret 0
387end
388
389note ops 46..59: branches, reconvergence, barriers, wide shared loads, three-source integer ops
390fn enc_d o view ulong 16 w edit ulong 4 to ulong
391op = get o 0
392imm = get o 6
393bar = get o 11
394if eq op 46
395ret enc_bra o w
396end
397if eq op 47
398put4 w wd o 29448 rb o 2 2048 ctl o 926720
399ret 0
400end
401if eq op 48
402put4 w 31517 0 65536 ctl o 1043456
403ret 0
404end
405if eq op 49
406put4 w wda o 31108 255 134219776 ctl o 927744
407ret 0
408end
409if eq op 50
410put4 w wa o 31112 rb o 3 134220031 ctl o 1040896
411ret 0
412end
413if eq op 51
414a = or or 29202 shl rb o 1 16 shl rz o 2 12 24
415put4 w a rz o 3 13 or or 126746624 shl and imm 255 8 rb o 4 ctl o 2083328
416ret 0
417end
418if eq op 52
419a = or or 29209 shl rb o 1 16 shl rz o 2 12 24
420put4 w a rz o 3 13 or 1536 rb o 4 ctl o 2083328
421ret 0
422end
423if eq op 53
424a = or or 29223 shl rb o 1 16 shl rz o 2 12 24
425put4 w a rz o 3 13 or 126746624 rb o 4 ctl o 2083328
426ret 0
427end
428if eq op 54
429if or or lt imm 2 ge imm 2147483648 ge bar 16
430ret 1
431end
432a = or or 2373 shl pred o 12 shl bar 16
433put4 w a and mul sub imm 1 16 4294967295 58720768 ctl o 1040896
434ret 0
435end
436if eq op 55
437if ge bar 16
438ret 1
439end
440a = or or 2369 shl pred o 12 shl bar 16
441put4 w a 0 58720768 ctl o 1042944
442ret 0
443end
444if eq op 56
445if not off24 imm
446ret 1
447end
448put4 w wda o 31105 or rb o 5 shl and imm 16777215 8 203299584 ctl o 991744
449ret 0
450end
451if eq op 57
452if not off24 imm
453ret 1
454end
455put4 w wda o 31105 or rb o 5 shl and imm 16777215 8 203300096 ctl o 991744
456ret 0
457end
458if eq op 58
459if ge imm 8388608
460ret 1
461end
462put4 w wda o 31108 shl and imm 16777215 8 3072 ctl o 1042432
463ret 0
464end
465if eq op 59
466put4 w wd o 29448 rb o 2 3072 ctl o 926720
467ret 0
468end
469ret 1
470end
471
472note ops 60..63 (Training R5): SHFL.BFLY (SHFL.DOWN's words with w1 bit 26), F2FP.BF16.F32.PACK_AB,
473note ATOMG.E.ADD.F32.FTZ.RN.STRONG.SYS and .GPU (no dst vreg encodes RZ, presence slot 15).
474note Constants by Wolfram: FromDigits of 0c001f00 7f89 000e0000 000e2400 723e 10ff 004fc400 79a3 80000000
475note 0c1f5300 0c1ef300 00321e00 base 16 = 201334528 32649 917504 926720 29246 4351 5227520 31139 2147483648
476note 203379456 203354880 3284480.
477note op 64 (Omega NG kernel traps, Hive NG D10): ATOMG.E.MIN.64.STRONG.SYS, ATOMG.E.ADD's words with w2 op bits
478note 23..26 = 1 and type bits 9..11 = 2 (nvdisasm 13.0 SM121); no dst vreg encodes RZ. Wolfram: FromDigits of 79a8
479note 089f5500 base 16 = 31144 144659712.
480fn enc_e o view ulong 16 w edit ulong 4 to ulong
481op = get o 0
482if eq op 60
483put4 w wda o 32649 or 201334528 shl and get o 6 31 21 917504 ctl o 926720
484ret 0
485end
486if eq op 61
487put4 w wda o 29246 rb o 3 4351 ctl o 5227520
488ret 0
489end
490if or eq op 62 eq op 63
491s = ulong 203379456
492if eq op 63
493s = 203354880
494end
495a = or or 31139 shl rz o 1 15 16 shl rb o 2 24
496put4 w a or rb o 3 2147483648 or s rb o 5 ctl o 3284480
497ret 0
498end
499if eq op 64
500a = or or 31144 shl rz o 1 15 16 shl rb o 2 24
501put4 w a or rb o 3 2147483648 or 144659712 rb o 5 ctl o 3284480
502ret 0
503end
504note WolframLanguageEvaluator: FromDigits[#,16]& /@ {"7245","7305","00201000","0020f000","004fe200","004e2200"}
505note -> {29253,29445,2101248,2158592,5235200,5120512}.
506if eq op 65
507put4 w wd o 29253 rb o 2 2101248 ctl o 5235200
508ret 0
509end
510if eq op 66
511put4 w wd o 29445 rb o 2 2158592 ctl o 5120512
512ret 0
513end
514note Native half forms are the E1_CVT sm_121 fixtures in src/omega_numeric.c.
515note Widening is HADD2.F32 Rd,-RZ,Ra.H0_H0; narrowing is F2F.F16.F32 Rd,Ra.
516note WolframLanguageEvaluator: FromDigits[#,16]& /@ {"ff007230","20000000","4100","7304","00200800","010fca00","010e2800"}
517note -> {4278219312,536870912,16640,29444,2099200,17811968,17704960}.
518if eq op 67
519put4 w wd o 4278219312 or 536870912 rb o 2 16640 ctl o 17811968
520ret 0
521end
522if eq op 68
523put4 w wd o 29444 rb o 2 2099200 ctl o 17704960
524ret 0
525end
526ret 1
527end
528
529note IEEE binary64 register forms, independently derived using nvcc 13.0.88 sm_121 and nvdisasm.
530note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
531note WolframLanguageEvaluator: FromDigits[#,16]& /@ {"7229","7228","722b","722a","7308","1800","1c00","7310","301000","201800","7312","201c00","7311","30d100","03f06000","000e2400"}
532note -> {29225,29224,29227,29226,29448,6144,7168,29456,3149824,2103296,29458,2104320,29457,3199232,66084864,926720}.
533note R255 is RZ, not the upper half of a writable double pair.
534fn dpair o view ulong 16 k ulong to ulong
535r = get o k
536if or ne and r 1 0 ge r 254
537ret 0
538end
539ret 1
540end
541
542fn pick c bool a ulong b ulong to ulong
543if c
544ret a
545end
546ret b
547end
548
549fn enc_f o view ulong 16 w edit ulong 4 to ulong
550op = get o 0
551if gt op 79
552ret 1
553end
554if le op 72
555if eq and dpair o 2 dpair o 3 0
556ret 1
557end
558if eq op 72
559if ne get o 15 0
560ret 1
561end
562put4 w wa o 29226 rb o 3 pick ne get o 6 0 get o 6 66084864 ctl o 926720
563ret 0
564end
565if eq dpair o 1 0
566ret 1
567end
568if eq op 69
569put4 w wda o 29225 0 rb o 3 ctl o 926720
570ret 0
571end
572if eq op 70
573put4 w wda o 29224 rb o 3 0 ctl o 926720
574ret 0
575end
576if and ne get o 4 255 eq dpair o 4 0
577ret 1
578end
579put4 w wda o 29227 rb o 3 rb o 4 ctl o 926720
580ret 0
581end
582if le op 74
583put4 w wd o 29448 rb o 2 pick eq op 73 6144 7168 ctl o 926720
584ret 0
585end
586if or eq op 76 eq op 79
587if eq dpair o 2 0
588ret 1
589end
590else
591if eq dpair o 1 0
592ret 1
593end
594end
595if le op 76
596put4 w wd o 29456 rb o 2 pick eq op 75 2103296 3149824 ctl o 926720
597ret 0
598end
599if le op 78
600put4 w wd o 29458 rb o 2 pick eq op 77 2104320 2103296 ctl o 926720
601ret 0
602end
603put4 w wd o 29457 rb o 2 3199232 ctl o 926720
604ret 0
605end
606
607fn encraw o view ulong 16 w edit ulong 4 to ulong
608op = get o 0
609if lt op 15
610ret enc_a o w
611end
612if lt op 30
613ret enc_b o w
614end
615if lt op 46
616ret enc_c o w
617end
618if lt op 60
619ret enc_d o w
620end
621if lt op 69
622ret enc_e o w
623end
624ret enc_f o w
625end
626
627note A P0 guard is not a uniform guard: LDCU and LDCU.64 must refuse it (DRA-483).
628note See PROVENANCE.md and guard_pred_test.om for the independent instruction oracle.
629fn enc1 o view ulong 16 w edit ulong 4 to ulong
630r = encraw o w
631if or ne r 0 eq get o 9 0
632ret r
633end
634op = get o 0
635if isuld op
636ret 1
637end
638x = get w 0
639put w 0 or sub x and x 61440 shl pred o 12
640ret 0
641end
642
643note the 16 bytes of instruction i, each word little-endian
644fn store code edit uchar 16384 i ulong w view ulong 4 to void
645for k ulong 4
646x = get w k
647q = add mul i 16 mul k 4
648for j ulong 4
649put code add q j uchar and shr x mul j 8 255
650end
651end
652end
653
654fn bw_enc_sm121 ins view ulong 8192 phys view ulong 288 n ulong pad ulong code edit uchar 16384 to ulong
655o = new ulong 16 0
656w = new ulong 4 0
657if gt n 1024
658ret 0
659end
660cnt = n
661if ne pad 0
662cnt = mul div add n 7 8 8
663if lt cnt 32
664cnt = 32
665end
666end
667for i ulong cnt
668if lt i n
669opnds ins i phys edit o
670if ne enc1 view o edit w 0
671ret 0
672end
673else
674put4 edit w NOPW 0 0 NOPC
675end
676store code i view w
677end
678ret mul cnt 16
679end