interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / om / encoder / bw_enc_sm121.om

578 lines · 13290 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 Two copies stay two files for two reasons only. seed/ng/cudapp.lock hashes
9note gpu/nvidia/om/encoder/bw_enc_sm121.om, and that file encodes ops 64..79, which this file refuses.
10note This file is what om/units.sh and mk/om-encoder.mk link. Op 11 and op 21 were bugs here.
11note Op 11 dropped imm, so nvdisasm read LDG.E R4, desc[UR4][R8.64]; the signed 24-bit offset is now in
12note word1, and imm 64 reads LDG.E R4, desc[UR4][R8.64+0x40]. Op 21's word2 was 202250496 (0x0c0e1900),
13note which nvdisasm reads as LDG.E.EF; it is now 203364608 (0x0c1f1900), LDG.E.MMIO.GPU. The C oracle's
14note LDG_MMIO case moved with it. om/encoder/guard-gate.sh exits nonzero while any word on ops 0..63 differs.
15note
16note Input (bw_om_encode in src/omega_blackwell_codegen.c packs it; 8 ulong per instruction, at most 1024 instructions):
17note 0 op (the BlackwellIROpcode number) 1 dst_vreg 2 src1_vreg 3 src2_vreg 4 src3_vreg
18note 5 ureg (the five vreg fields are C int, passed as 64-bit two's complement: negative = none)
19note 6 imm or control shl 32
20note 7 is_uniform or predicate_p0 shl 1 or predicate_not shl 2 or bar_reg shl 8
21note or dst_subreg shl 16 or src1_subreg shl 24 or src2_subreg shl 32 or src3_subreg shl 40
22note phys: vreg_to_phys 0..255 then uvreg_to_phys at 256..287 (C int, two's complement).
23note pad 1: omega_bw_encode_program (pad to the multiple of 8, at least 32); pad 0: exactly n instructions.
24note Result: the bytes written, or 0 when an instruction is refused (unknown op, an offset outside its
25note field, a BSSY delta below 2, a barrier register above 15) or n is above 1024.
26note Every constant is the decimal form of the C encoder's hex word (conversions by Wolfram,
27note evidence/CUDAPP-OM-ENCODER/wolfram-constants.txt).
28
29const NEG ulong 9223372036854775808
30const NOPW ulong 31000
31const NOPC ulong 1032192
32
33fn put4 w edit ulong 4 a ulong b ulong c ulong d ulong to void
34put w 0 a
35put w 1 b
36put w 2 c
37put w 3 d
38end
39
40note the physical register of one operand field: phys + subreg, or dflt when the vreg is negative
41fn preg phys view ulong 288 v ulong sb ulong dflt ulong to ulong
42if ge v NEG
43ret dflt
44end
45ret add get phys v sb
46end
47
48note 1 when a vreg field is given (not negative), else 0
49fn pres v ulong to ulong
50if ge v NEG
51ret 0
52end
53ret 1
54end
55
56fn opnds ins view ulong 8192 i ulong phys view ulong 288 o edit ulong 16 to void
57b = mul i 8
58f = get ins add b 7
59put o 0 get ins b
60put o 1 preg phys get ins add b 1 and shr f 16 255 0
61put o 2 preg phys get ins add b 2 and shr f 24 255 0
62put o 3 preg phys get ins add b 3 and shr f 32 255 0
63put o 4 preg phys get ins add b 4 and shr f 40 255 255
64u = get ins add b 5
65put o 5 0
66if lt u NEG
67put o 5 get phys add u 256
68end
69put o 14 pres u
70x = get ins add b 6
71put o 6 and x 4294967295
72put o 7 shr x 32
73put o 8 and f 1
74put o 9 and shr f 1 1
75put o 10 and shr f 2 1
76put o 11 and shr f 8 255
77put o 12 pres get ins add b 2
78put o 13 pres get ins add b 3
79d = get ins add b 1
80put o 15 pres d
81if and eq and f 1 1 lt d NEG
82put o 1 get phys add d 256
83end
84end
85
86note the control word: the instruction's own, else the op's default
87fn ctl o view ulong 16 dflt ulong to ulong
88c = get o 7
89if ne c 0
90ret c
91end
92ret dflt
93end
94
95note low 8 bits of operand slot k
96fn rb o view ulong 16 k ulong to ulong
97ret and get o k 255
98end
99
100note slot k when its vreg (presence slot h) is given, else RZ
101fn rz o view ulong 16 k ulong h ulong to ulong
102if ne get o h 0
103ret rb o k
104end
105ret 255
106end
107
108fn wd o view ulong 16 base ulong to ulong
109ret or base shl rb o 1 16
110end
111
112fn wa o view ulong 16 base ulong to ulong
113ret or base shl rb o 2 24
114end
115
116fn wda o view ulong 16 base ulong to ulong
117ret or wd o base shl rb o 2 24
118end
119
120note the 4-bit guard predicate field: 7 always, 0 @P0, 8 @!P0
121fn pred o view ulong 16 to ulong
122if eq get o 9 0
123ret 7
124end
125if eq get o 10 0
126ret 0
127end
128ret 8
129end
130
131note a signed 24-bit byte offset (imm read as int32) fits
132fn off24 imm ulong to bool
133ret or lt imm 8388608 ge imm 4286578688
134end
135
136note ops 0..14: moves, constants, integer multiply-add, compares and global memory
137fn enc_a o view ulong 16 w edit ulong 4 to ulong
138op = get o 0
139imm = get o 6
140if eq op 0
141put4 w NOPW 0 0 ctl o NOPC
142ret 0
143end
144if eq op 1
145put4 w wd o 29186 255 3840 ctl o 1040896
146ret 0
147end
148if eq op 2
149put4 w wd o 30722 imm 3840 ctl o 1040896
150ret 0
151end
152if eq op 3
153put4 w wd o 31001 0 shl and imm 255 8 ctl o 926208
154ret 0
155end
156if eq op 4
157put4 w wd o 4278221698 and shl imm 6 16776960 2048 ctl o 1040896
158ret 0
159end
160if eq op 5
161put4 w wd o 4278221698 and shl imm 6 16776960 2560 ctl o 926208
162ret 0
163end
164if eq op 6
165put4 w wd o 4278220716 and shl imm 5 16776960 134219776 ctl o 926208
166ret 0
167end
168if eq op 7
169put4 w wd o 4278220716 and shl imm 5 16776960 134220288 ctl o 945664
170ret 0
171end
172if eq op 8
173if ne get o 13 0
174put4 w wda o 29220 rb o 3 or 126747136 rb o 4 ctl o 2083328
175ret 0
176end
177if and ne get o 8 0 ne get o 14 0
178put4 w wda o 31780 rb o 5 or 260964864 rb o 4 ctl o 2083328
179ret 0
180end
181put4 w wda o 30756 imm or 126747136 rb o 4 ctl o 2083328
182ret 0
183end
184if eq op 9
185put4 w wda o 30757 imm or 126746624 rb o 4 ctl o 2083840
186ret 0
187end
188if eq op 10
189if and ne get o 8 0 ne get o 14 0
190put4 w wda o 31756 rb o 5 200302704 ctl o 3136000
191ret 0
192end
193put4 w wda o 30732 imm 66085488 ctl o 2087424
194ret 0
195end
196if eq op 11
197if not off24 imm
198ret 1
199end
200put4 w wda o 31105 or rb o 5 shl and imm 16777215 8 203299072 ctl o 991744
201ret 0
202end
203if eq op 12
204if not off24 imm
205ret 1
206end
207put4 w wa o 31110 or rb o 3 shl and imm 16777215 8 or 202381568 rb o 5 ctl o 1040896
208ret 0
209end
210if eq op 13
211put4 w wa o 31110 rb o 3 or 202447104 rb o 5 ctl o 1040896
212ret 0
213end
214if eq op 14
215put4 w wda o 29200 rb o 3 134209791 ctl o 17811968
216ret 0
217end
218ret 1
219end
220
221note ops 15..29: tensor core, shifts, wide and strong loads, stores, fences, atomics
222fn enc_b o view ulong 16 w edit ulong 4 to ulong
223op = get o 0
224imm = get o 6
225if or eq op 15 eq op 16
226p = ulong 0
227if eq op 16
228p = 262144
229end
230put4 w wda o 29244 rb o 3 or or rb o 4 6144 p ctl o 1040896
231ret 0
232end
233if eq op 17
234put4 w or wd o 30745 4278190080 imm or 71168 rb o 2 ctl o 2083328
235ret 0
236end
237if eq op 18
238put4 w wda o 30738 imm 126796031 ctl o 2083328
239ret 0
240end
241if eq op 19
242put4 w wda o 31105 rb o 5 203298048 ctl o 991744
243ret 0
244end
245if eq op 20
246put4 w wda o 31105 rb o 5 203380992 ctl o 991744
247ret 0
248end
249if eq op 21
250put4 w wda o 31105 rb o 5 203364608 ctl o 991744
251ret 0
252end
253if eq op 22
254a = wa o 31110
255if ne get o 9 0
256a = or wa o 2438 shl pred o 12
257end
258put4 w a rb o 3 or 202463488 rb o 5 ctl o 1040896
259ret 0
260end
261if eq op 23
262put4 w 31122 0 45056 ctl o 1043456
263ret 0
264end
265if eq op 24
266put4 w 31122 0 12288 ctl o 1043456
267ret 0
268end
269if eq op 25
270put4 w 4278221199 0 33554432 ctl o 1042432
271ret 0
272end
273if eq op 26
274put4 w wda o 31144 or rb o 3 2147483648 or 136270080 rb o 5 ctl o 3284480
275ret 0
276end
277if eq op 27
278put4 w wda o 31144 or rb o 3 2147483648 or 203378944 rb o 5 ctl o 3284480
279ret 0
280end
281if eq op 28
282put4 w wa o 29196 rb o 3 66084976 ctl o 2087424
283ret 0
284end
285if eq op 29
286put4 w wda o 29202 rb o 3 126762239 ctl o 2083328
287ret 0
288end
289ret 1
290end
291
292note ops 30..45: floating point, special functions, shuffles, shared memory, exit
293fn enc_c o view ulong 16 w edit ulong 4 to ulong
294op = get o 0
295imm = get o 6
296if eq op 30
297put4 w wda o 29217 rb o 3 0 ctl o 5227520
298ret 0
299end
300if eq op 31
301put4 w wda o 29217 or rb o 3 2147483648 0 ctl o 5227520
302ret 0
303end
304if eq op 32
305put4 w wda o 29216 rb o 3 4194304 ctl o 5227520
306ret 0
307end
308if eq op 33
309put4 w wda o 29219 rb o 3 rb o 4 ctl o 5227520
310ret 0
311end
312if eq op 34
313c = ulong 66084864
314if ne imm 0
315c = imm
316end
317a = or or 29195 shl and get o 1 7 16 shl rb o 2 24
318put4 w a rb o 3 c ctl o 5227520
319ret 0
320end
321if eq op 35
322put4 w wda o 29192 rb o 3 and get o 4 7 ctl o 1034752
323ret 0
324end
325if eq op 36
326put4 w wda o 29193 rb o 3 58720256 ctl o 5227520
327ret 0
328end
329if eq op 37
330put4 w wda o 29193 rb o 3 125829120 ctl o 5227520
331ret 0
332end
333if eq op 38
334put4 w wd o 29253 rb o 2 2102272 ctl o 5235200
335ret 0
336end
337if eq op 39
338put4 w wd o 29445 rb o 2 2158848 ctl o 5120512
339ret 0
340end
341if eq op 40
342put4 w wd o 29448 rb o 2 4096 ctl o 926720
343ret 0
344end
345if eq op 41
346put4 w wd o 29448 rb o 2 5120 ctl o 926720
347ret 0
348end
349if eq op 42
350put4 w wda o 32649 or 134225664 shl and imm 31 21 917504 ctl o 926720
351ret 0
352end
353if eq op 43
354put4 w wda o 31108 0 and imm 65535 ctl o 1040896
355ret 0
356end
357if eq op 44
358put4 w wa o 29576 rb o 3 and imm 65535 ctl o 1040896
359ret 0
360end
361if eq op 45
362a = ulong 31053
363if ne get o 9 0
364a = 2381
365end
366put4 w a 0 58720256 ctl o 1042944
367ret 0
368end
369ret 1
370end
371
372note op 46: BRA. rel = imm - 1 as int32; low 6 bits at w0 18..23, rel arithmetic-shifted by 6 at w1 bit 2
373fn enc_bra o view ulong 16 w edit ulong 4 to ulong
374imm = get o 6
375rel = and sub imm 1 4294967295
376s = shr rel 6
377c = ulong 58720256
378if ge rel 2147483648
379s = or s 4227858432
380c = 58982399
381end
382d = get o 7
383if eq d 0
384d = 1042944
385if and eq imm 0 eq get o 9 0
386d = NOPC
387end
388end
389a = or or 2375 shl pred o 12 shl and rel 63 18
390put4 w a and shl s 2 4294967295 c d
391ret 0
392end
393
394note ops 46..59: branches, reconvergence, barriers, wide shared loads, three-source integer ops
395fn enc_d o view ulong 16 w edit ulong 4 to ulong
396op = get o 0
397imm = get o 6
398bar = get o 11
399if eq op 46
400ret enc_bra o w
401end
402if eq op 47
403put4 w wd o 29448 rb o 2 2048 ctl o 926720
404ret 0
405end
406if eq op 48
407put4 w 31517 0 65536 ctl o 1043456
408ret 0
409end
410if eq op 49
411put4 w wda o 31108 255 134219776 ctl o 927744
412ret 0
413end
414if eq op 50
415put4 w wa o 31112 rb o 3 134220031 ctl o 1040896
416ret 0
417end
418if eq op 51
419a = or or 29202 shl rb o 1 16 shl rz o 2 12 24
420put4 w a rz o 3 13 or or 126746624 shl and imm 255 8 rb o 4 ctl o 2083328
421ret 0
422end
423if eq op 52
424a = or or 29209 shl rb o 1 16 shl rz o 2 12 24
425put4 w a rz o 3 13 or 1536 rb o 4 ctl o 2083328
426ret 0
427end
428if eq op 53
429a = or or 29223 shl rb o 1 16 shl rz o 2 12 24
430put4 w a rz o 3 13 or 126746624 rb o 4 ctl o 2083328
431ret 0
432end
433if eq op 54
434if or or lt imm 2 ge imm 2147483648 ge bar 16
435ret 1
436end
437a = or or 2373 shl pred o 12 shl bar 16
438put4 w a and mul sub imm 1 16 4294967295 58720768 ctl o 1040896
439ret 0
440end
441if eq op 55
442if ge bar 16
443ret 1
444end
445a = or or 2369 shl pred o 12 shl bar 16
446put4 w a 0 58720768 ctl o 1042944
447ret 0
448end
449if eq op 56
450if not off24 imm
451ret 1
452end
453put4 w wda o 31105 or rb o 5 shl and imm 16777215 8 203299584 ctl o 991744
454ret 0
455end
456if eq op 57
457if not off24 imm
458ret 1
459end
460put4 w wda o 31105 or rb o 5 shl and imm 16777215 8 203300096 ctl o 991744
461ret 0
462end
463if eq op 58
464if ge imm 8388608
465ret 1
466end
467put4 w wda o 31108 shl and imm 16777215 8 3072 ctl o 1042432
468ret 0
469end
470if eq op 59
471put4 w wd o 29448 rb o 2 3072 ctl o 926720
472ret 0
473end
474ret 1
475end
476
477note ops 60..63 (Training R5): SHFL.BFLY (SHFL.DOWN's words with w1 bit 26), F2FP.BF16.F32.PACK_AB,
478note ATOMG.E.ADD.F32.FTZ.RN.STRONG.SYS and .GPU (no dst vreg encodes RZ, presence slot 15).
479note Constants by Wolfram: FromDigits of 0c001f00 7f89 000e0000 000e2400 723e 10ff 004fc400 79a3 80000000
480note 0c1f5300 0c1ef300 00321e00 base 16 = 201334528 32649 917504 926720 29246 4351 5227520 31139 2147483648
481note 203379456 203354880 3284480.
482fn enc_e o view ulong 16 w edit ulong 4 to ulong
483op = get o 0
484if eq op 60
485put4 w wda o 32649 or 201334528 shl and get o 6 31 21 917504 ctl o 926720
486ret 0
487end
488if eq op 61
489put4 w wda o 29246 rb o 3 4351 ctl o 5227520
490ret 0
491end
492if or eq op 62 eq op 63
493s = ulong 203379456
494if eq op 63
495s = 203354880
496end
497a = or or 31139 shl rz o 1 15 16 shl rb o 2 24
498put4 w a or rb o 3 2147483648 or s rb o 5 ctl o 3284480
499ret 0
500end
501ret 1
502end
503
504fn encraw o view ulong 16 w edit ulong 4 to ulong
505op = get o 0
506if lt op 15
507ret enc_a o w
508end
509if lt op 30
510ret enc_b o w
511end
512if lt op 46
513ret enc_c o w
514end
515if lt op 60
516ret enc_d o w
517end
518ret enc_e o w
519end
520
521note LDCU and LDCU.64: the guard bits name a uniform predicate, so a P0 guard is refused (DRA-599).
522fn isuld op ulong to bool
523ret or eq op 6 eq op 7
524end
525
526note After the op's own words, a predicated instruction gets pred o in w0 bits 12..15.
527note Unpredicated words already carry 7 there, so they are left as the C oracle wrote them.
528note 61440 is the guard mask 0xf000.
529fn enc1 o view ulong 16 w edit ulong 4 to ulong
530r = encraw o w
531if or ne r 0 eq get o 9 0
532ret r
533end
534if isuld get o 0
535ret 1
536end
537x = get w 0
538put w 0 or sub x and x 61440 shl pred o 12
539ret 0
540end
541
542note the 16 bytes of instruction i, each word little-endian
543fn store code edit uchar 16384 i ulong w view ulong 4 to void
544for k ulong 4
545x = get w k
546q = add mul i 16 mul k 4
547for j ulong 4
548put code add q j uchar and shr x mul j 8 255
549end
550end
551end
552
553fn bw_enc_sm121 ins view ulong 8192 phys view ulong 288 n ulong pad ulong code edit uchar 16384 to ulong
554o = new ulong 16 0
555w = new ulong 4 0
556if gt n 1024
557ret 0
558end
559cnt = n
560if ne pad 0
561cnt = mul div add n 7 8 8
562if lt cnt 32
563cnt = 32
564end
565end
566for i ulong cnt
567if lt i n
568opnds ins i phys edit o
569if ne enc1 view o edit w 0
570ret 0
571end
572else
573put4 edit w NOPW 0 0 NOPC
574end
575store code i view w
576end
577ret mul cnt 16
578end