interplane.Get Omega
Read in your language

sovereign-intelligence

Front door to Drake Stapleton's sovereign stack.

Brought by drakestapleton

branch main Branches Tags History

sovereign-intelligence / gpu / cuda-crack / gen / bw_enc_sm121.om

551 lines · 12113 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 (and in the freestanding build, which has no native binding yet).
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) 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
194put4 w wda o 31105 rb o 5 203299072 ctl o 991744
195ret 0
196end
197if eq op 12
198if not off24 imm
199ret 1
200end
201put4 w wa o 31110 or rb o 3 shl and imm 16777215 8 or 202381568 rb o 5 ctl o 1040896
202ret 0
203end
204if eq op 13
205put4 w wa o 31110 rb o 3 or 202447104 rb o 5 ctl o 1040896
206ret 0
207end
208if eq op 14
209put4 w wda o 29200 rb o 3 134209791 ctl o 17811968
210ret 0
211end
212ret 1
213end
214
215note ops 15..29: tensor core, shifts, wide and strong loads, stores, fences, atomics
216fn enc_b o view ulong 16 w edit ulong 4 to ulong
217op = get o 0
218imm = get o 6
219if or eq op 15 eq op 16
220p = ulong 0
221if eq op 16
222p = 262144
223end
224put4 w wda o 29244 rb o 3 or or rb o 4 6144 p ctl o 1040896
225ret 0
226end
227if eq op 17
228put4 w or wd o 30745 4278190080 imm or 71168 rb o 2 ctl o 2083328
229ret 0
230end
231if eq op 18
232put4 w wda o 30738 imm 126796031 ctl o 2083328
233ret 0
234end
235if eq op 19
236put4 w wda o 31105 rb o 5 203298048 ctl o 991744
237ret 0
238end
239if eq op 20
240put4 w wda o 31105 rb o 5 203380992 ctl o 991744
241ret 0
242end
243if eq op 21
244put4 w wda o 31105 rb o 5 203364608 ctl o 991744
245ret 0
246end
247if eq op 22
248a = wa o 31110
249if ne get o 9 0
250a = or wa o 2438 shl pred o 12
251end
252put4 w a rb o 3 or 202463488 rb o 5 ctl o 1040896
253ret 0
254end
255if eq op 23
256put4 w 31122 0 45056 ctl o 1043456
257ret 0
258end
259if eq op 24
260put4 w 31122 0 12288 ctl o 1043456
261ret 0
262end
263if eq op 25
264put4 w 4278221199 0 33554432 ctl o 1042432
265ret 0
266end
267if eq op 26
268put4 w wda o 31144 or rb o 3 2147483648 or 136270080 rb o 5 ctl o 3284480
269ret 0
270end
271if eq op 27
272put4 w wda o 31144 or rb o 3 2147483648 or 203378944 rb o 5 ctl o 3284480
273ret 0
274end
275if eq op 28
276put4 w wa o 29196 rb o 3 66084976 ctl o 2087424
277ret 0
278end
279if eq op 29
280put4 w wda o 29202 rb o 3 126762239 ctl o 2083328
281ret 0
282end
283ret 1
284end
285
286note ops 30..45: floating point, special functions, shuffles, shared memory, exit
287fn enc_c o view ulong 16 w edit ulong 4 to ulong
288op = get o 0
289imm = get o 6
290if eq op 30
291put4 w wda o 29217 rb o 3 0 ctl o 5227520
292ret 0
293end
294if eq op 31
295put4 w wda o 29217 or rb o 3 2147483648 0 ctl o 5227520
296ret 0
297end
298if eq op 32
299put4 w wda o 29216 rb o 3 4194304 ctl o 5227520
300ret 0
301end
302if eq op 33
303put4 w wda o 29219 rb o 3 rb o 4 ctl o 5227520
304ret 0
305end
306if eq op 34
307c = ulong 66084864
308if ne imm 0
309c = imm
310end
311a = or or 29195 shl and get o 1 7 16 shl rb o 2 24
312put4 w a rb o 3 c ctl o 5227520
313ret 0
314end
315if eq op 35
316put4 w wda o 29192 rb o 3 and get o 4 7 ctl o 1034752
317ret 0
318end
319if eq op 36
320put4 w wda o 29193 rb o 3 58720256 ctl o 5227520
321ret 0
322end
323if eq op 37
324put4 w wda o 29193 rb o 3 125829120 ctl o 5227520
325ret 0
326end
327if eq op 38
328put4 w wd o 29253 rb o 2 2102272 ctl o 5235200
329ret 0
330end
331if eq op 39
332put4 w wd o 29445 rb o 2 2158848 ctl o 5120512
333ret 0
334end
335if eq op 40
336put4 w wd o 29448 rb o 2 4096 ctl o 926720
337ret 0
338end
339if eq op 41
340put4 w wd o 29448 rb o 2 5120 ctl o 926720
341ret 0
342end
343if eq op 42
344put4 w wda o 32649 or 134225664 shl and imm 31 21 917504 ctl o 926720
345ret 0
346end
347if eq op 43
348put4 w wda o 31108 0 and imm 65535 ctl o 1040896
349ret 0
350end
351if eq op 44
352put4 w wa o 29576 rb o 3 and imm 65535 ctl o 1040896
353ret 0
354end
355if eq op 45
356a = ulong 31053
357if ne get o 9 0
358a = 2381
359end
360put4 w a 0 58720256 ctl o 1042944
361ret 0
362end
363ret 1
364end
365
366note op 46: BRA. rel = imm - 1 as int32; low 6 bits at w0 18..23, rel arithmetic-shifted by 6 at w1 bit 2
367fn enc_bra o view ulong 16 w edit ulong 4 to ulong
368imm = get o 6
369rel = and sub imm 1 4294967295
370s = shr rel 6
371c = ulong 58720256
372if ge rel 2147483648
373s = or s 4227858432
374c = 58982399
375end
376d = get o 7
377if eq d 0
378d = 1042944
379if and eq imm 0 eq get o 9 0
380d = NOPC
381end
382end
383a = or or 2375 shl pred o 12 shl and rel 63 18
384put4 w a and shl s 2 4294967295 c d
385ret 0
386end
387
388note ops 46..59: branches, reconvergence, barriers, wide shared loads, three-source integer ops
389fn enc_d o view ulong 16 w edit ulong 4 to ulong
390op = get o 0
391imm = get o 6
392bar = get o 11
393if eq op 46
394ret enc_bra o w
395end
396if eq op 47
397put4 w wd o 29448 rb o 2 2048 ctl o 926720
398ret 0
399end
400if eq op 48
401put4 w 31517 0 65536 ctl o 1043456
402ret 0
403end
404if eq op 49
405put4 w wda o 31108 255 134219776 ctl o 927744
406ret 0
407end
408if eq op 50
409put4 w wa o 31112 rb o 3 134220031 ctl o 1040896
410ret 0
411end
412if eq op 51
413a = or or 29202 shl rb o 1 16 shl rz o 2 12 24
414put4 w a rz o 3 13 or or 126746624 shl and imm 255 8 rb o 4 ctl o 2083328
415ret 0
416end
417if eq op 52
418a = or or 29209 shl rb o 1 16 shl rz o 2 12 24
419put4 w a rz o 3 13 or 1536 rb o 4 ctl o 2083328
420ret 0
421end
422if eq op 53
423a = or or 29223 shl rb o 1 16 shl rz o 2 12 24
424put4 w a rz o 3 13 or 126746624 rb o 4 ctl o 2083328
425ret 0
426end
427if eq op 54
428if or or lt imm 2 ge imm 2147483648 ge bar 16
429ret 1
430end
431a = or or 2373 shl pred o 12 shl bar 16
432put4 w a and mul sub imm 1 16 4294967295 58720768 ctl o 1040896
433ret 0
434end
435if eq op 55
436if ge bar 16
437ret 1
438end
439a = or or 2369 shl pred o 12 shl bar 16
440put4 w a 0 58720768 ctl o 1042944
441ret 0
442end
443if eq op 56
444if not off24 imm
445ret 1
446end
447put4 w wda o 31105 or rb o 5 shl and imm 16777215 8 203299584 ctl o 991744
448ret 0
449end
450if eq op 57
451if not off24 imm
452ret 1
453end
454put4 w wda o 31105 or rb o 5 shl and imm 16777215 8 203300096 ctl o 991744
455ret 0
456end
457if eq op 58
458if ge imm 8388608
459ret 1
460end
461put4 w wda o 31108 shl and imm 16777215 8 3072 ctl o 1042432
462ret 0
463end
464if eq op 59
465put4 w wd o 29448 rb o 2 3072 ctl o 926720
466ret 0
467end
468ret 1
469end
470
471note ops 60..63 (Training R5): SHFL.BFLY (SHFL.DOWN's words with w1 bit 26), F2FP.BF16.F32.PACK_AB,
472note ATOMG.E.ADD.F32.FTZ.RN.STRONG.SYS and .GPU (no dst vreg encodes RZ, presence slot 15).
473note Constants by Wolfram: FromDigits of 0c001f00 7f89 000e0000 000e2400 723e 10ff 004fc400 79a3 80000000
474note 0c1f5300 0c1ef300 00321e00 base 16 = 201334528 32649 917504 926720 29246 4351 5227520 31139 2147483648
475note 203379456 203354880 3284480.
476fn enc_e o view ulong 16 w edit ulong 4 to ulong
477op = get o 0
478if eq op 60
479put4 w wda o 32649 or 201334528 shl and get o 6 31 21 917504 ctl o 926720
480ret 0
481end
482if eq op 61
483put4 w wda o 29246 rb o 3 4351 ctl o 5227520
484ret 0
485end
486if or eq op 62 eq op 63
487s = ulong 203379456
488if eq op 63
489s = 203354880
490end
491a = or or 31139 shl rz o 1 15 16 shl rb o 2 24
492put4 w a or rb o 3 2147483648 or s rb o 5 ctl o 3284480
493ret 0
494end
495ret 1
496end
497
498fn enc1 o view ulong 16 w edit ulong 4 to ulong
499op = get o 0
500if lt op 15
501ret enc_a o w
502end
503if lt op 30
504ret enc_b o w
505end
506if lt op 46
507ret enc_c o w
508end
509if lt op 60
510ret enc_d o w
511end
512ret enc_e o w
513end
514
515note the 16 bytes of instruction i, each word little-endian
516fn store code edit uchar 16384 i ulong w view ulong 4 to void
517for k ulong 4
518x = get w k
519q = add mul i 16 mul k 4
520for j ulong 4
521put code add q j uchar and shr x mul j 8 255
522end
523end
524end
525
526fn bw_enc_sm121 ins view ulong 8192 phys view ulong 288 n ulong pad ulong code edit uchar 16384 to ulong
527o = new ulong 16 0
528w = new ulong 4 0
529if gt n 1024
530ret 0
531end
532cnt = n
533if ne pad 0
534cnt = mul div add n 7 8 8
535if lt cnt 32
536cnt = 32
537end
538end
539for i ulong cnt
540if lt i n
541opnds ins i phys edit o
542if ne enc1 view o edit w 0
543ret 0
544end
545else
546put4 edit w NOPW 0 0 NOPC
547end
548store code i view w
549end
550ret mul cnt 16
551end