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 / resident / resident.om

470 lines · 10601 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note Resident descriptor-driven persistent kernel. Only thread zero per CTA works.
3const NONE ulong 65535
4const ALL ulong 18446744073709551615
5const LO32 ulong 4294967295
6const YIELD ulong 8192
7const NOPC ulong 1032192
8const KFIX ulong 0
9const KPRED ulong 1
10const KVAR ulong 2
11const KUVAR ulong 3
12const KSTORE ulong 4
13const KSYNC ulong 5
14const KEXIT ulong 6
15const KBRA ulong 7
16const KRAW ulong 8
17const MOVI ulong 2
18const S2R ulong 3
19const LDC64 ulong 5
20const LDCU64 ulong 7
21const IMAD ulong 8
22const IMADW ulong 9
23const IADD3 ulong 14
24const ISGEU ulong 28
25const FADD ulong 30
26const FMUL ulong 32
27const EXIT ulong 45
28const BRA ulong 46
29
30fn cw st ulong wb ulong rb ulong wt ulong to ulong
31ret or or shl st 9 shl wb 14 or shl rb 17 shl wt 20
32end
33
34fn fld r ulong to ulong
35if eq r NONE
36ret ALL
37end
38ret and r 1023
39end
40
41fn rsub r ulong to ulong
42if eq r NONE
43ret 0
44end
45ret shr r 10
46end
47
48fn r4 d ulong a ulong b ulong c ulong to ulong
49ret or or d shl a 16 or shl b 32 shl c 48
50end
51
52fn okk op ulong k ulong fl ulong ur ulong to ulong
53ret or or op shl k 8 or shl fl 16 shl ur 40
54end
55
56fn sb v ulong s ulong to ulong
57ret or v shl s 10
58end
59
60fn p2 lo ulong hi ulong to ulong
61ret or lo shl hi 16
62end
63
64const PW1 ulong 4294967296
65const PW2 ulong 8589934592
66const PW4 ulong 17179869184
67
68note control word of one record kind: returns control | next pending wait mask << 32
69fn kc k ulong fl ulong pend ulong imm ulong to ulong
70if eq k KPRED
71ret cw 14 7 7 pend
72end
73if eq k KVAR
74ret or or cw 4 0 7 pend YIELD PW1
75end
76if eq k KUVAR
77ret or or cw 4 2 7 pend YIELD PW4
78end
79if eq k KSTORE
80ret or or cw 4 7 1 pend YIELD PW2
81end
82if or eq k KEXIT eq k KBRA
83if ne and fl 2 0
84ret or cw 5 7 7 pend shl pend 32
85end
86ret cw 5 7 7 pend
87end
88if eq k KRAW
89ret or shr imm 32 shl pend 32
90end
91ret cw 6 7 7 pend
92end
93
94note operand fields 1..4 and their subregisters (record word 7 bits 16..47)
95fn wops ins edit ulong 8192 b ulong rg ulong to void
96d = and rg 65535
97s1 = and shr rg 16 65535
98s2 = and shr rg 32 65535
99s3 = shr rg 48
100put ins add b 1 fld d
101put ins add b 2 fld s1
102put ins add b 3 fld s2
103put ins add b 4 fld s3
104f1 = or shl rsub d 16 shl rsub s1 24
105f2 = or shl rsub s2 32 shl rsub s3 40
106put ins add b 7 or f1 f2
107end
108
109note record words 5..7: ureg, imm | control << 32, flags (x = control | pending << 32)
110fn wtail ins edit ulong 8192 b ulong ok ulong imm ulong x ulong to void
111ur = and shr ok 40 65535
112uv = ALL
113if ne ur NONE
114uv = ur
115end
116put ins add b 5 uv
117c = and x LO32
118put ins add b 6 or and imm LO32 shl c 32
119w7 = get ins add b 7
120put ins add b 7 or w7 and shr ok 16 7
121end
122
123note one record: ok = op | kind << 8 | flags << 16 | ureg << 40; rg = r4 operands; imm low 32 bits
124note (a raw record carries its control word in imm bits 32..63)
125fn emit ins edit ulong 8192 m edit ulong 512 ok ulong rg ulong imm ulong to void
126n = get m 0
127if ge n 1024
128put m 4 1
129ret
130end
131k = and shr ok 8 255
132if gt k KRAW
133put m 4 2
134ret
135end
136x = kc k and shr ok 16 255 get m 3 imm
137b = mul n 8
138put ins b and ok 255
139wops ins b rg
140wtail ins b ok imm x
141put m 0 add n 1
142put m 3 shr x 32
143end
144
145fn nv m edit ulong 512 kind ulong to ulong
146v = get m 1
147if ge v 256
148put m 4 3
149ret 0
150end
151put m add 64 v kind
152put m 1 add v 1
153ret v
154end
155
156fn nu m edit ulong 512 kind ulong to ulong
157u = get m 2
158if ge u 32
159put m 4 3
160ret 0
161end
162put m add 320 u kind
163put m 2 add u 1
164ret u
165end
166
167note ---- instruction helpers
168
169fn ef ins edit ulong 8192 m edit ulong 512 op ulong rg ulong imm ulong to void
170emit ins m okk op KFIX 0 NONE rg imm
171end
172
173
174note P0 = a >= b (unsigned)
175fn isge ins edit ulong 8192 m edit ulong 512 a ulong b ulong to void
176emit ins m okk ISGEU KPRED 0 NONE r4 NONE a b NONE 0
177end
178
179fn exitp ins edit ulong 8192 m edit ulong 512 to void
180emit ins m okk EXIT KEXIT 2 NONE r4 NONE NONE NONE NONE 0
181end
182
183fn tail ins edit ulong 8192 m edit ulong 512 to void
184emit ins m okk EXIT KEXIT 0 NONE r4 NONE NONE NONE NONE 0
185emit ins m okk BRA KRAW 0 NONE r4 NONE NONE NONE NONE shl NOPC 32
186end
187
188
189note @P0 BRA forward; returns the record index for patch
190fn bfwd ins edit ulong 8192 m edit ulong 512 to ulong
191at = get m 0
192emit ins m okk BRA KBRA 2 NONE r4 NONE NONE NONE NONE 1
193ret at
194end
195
196fn patch ins edit ulong 8192 m edit ulong 512 at ulong to void
197b = add mul at 8 6
198x = get ins b
199put ins b or and x 18446744069414584320 sub get m 0 at
200end
201
202
203note Descriptor: op,a,b,out,rows,cols,0,0; offsets in f32 elements.
204note No input may overlap its output. Descriptors are immutable for the launch.
205fn spanok off ulong count ulong size ulong to bool
206if gt count size
207ret false
208end
209ret le off sub size count
210end
211
212fn disjoint a ulong na ulong b ulong nb ulong to bool
213ret or le add a na b le add b nb a
214end
215
216fn descok d view ulong 512 at ulong size ulong to bool
217op = get d at
218a = get d add at 1
219b = get d add at 2
220out = get d add at 3
221rows = get d add at 4
222cols = get d add at 5
223if or or lt op 1 gt op 5 or eq rows 0 gt rows 32
224ret false
225end
226if or eq cols 0 gt cols 32
227ret false
228end
229if or ne get d add at 6 0 ne get d add at 7 0
230ret false
231end
232na = rows
233nb = rows
234if eq op 1
235na = mul rows cols
236nb = cols
237else
238if eq op 4
239if ne rows 1
240ret false
241end
242na = cols
243else
244if ne cols 1
245ret false
246end
247end
248end
249if or not spanok a na size not spanok out rows size
250ret false
251end
252if not disjoint a na out rows
253ret false
254end
255if le op 3
256if or not spanok b nb size not disjoint b nb out rows
257ret false
258end
259end
260ret true
261end
262
263fn resident_chk d view ulong 512 n ulong size ulong ctas ulong to ulong
264if or or eq n 0 gt n 64 or eq size 0 gt size 65536
265ret 1
266end
267if or eq ctas 0 gt ctas 2
268ret 1
269end
270for i ulong n
271if not descok d mul i 8 size
272ret 1
273end
274end
275ret 0
276end
277
278note Fixed vreg plan. 0..7 pairs; 8..30 scalars, kept live across all loops.
279fn mov ins edit ulong 8192 m edit ulong 512 d ulong x ulong to void
280ef ins m MOVI r4 d NONE NONE NONE x
281end
282note cp: d = a + vreg 23, which holds zero from the prologue on. A missing second source is encoded
283note as R0, which no instruction of this kernel writes.
284fn cp ins edit ulong 8192 m edit ulong 512 d ulong a ulong to void
285ef ins m IADD3 r4 d a 23 NONE 0
286end
287fn plus ins edit ulong 8192 m edit ulong 512 d ulong a ulong b ulong to void
288ef ins m IADD3 r4 d a b NONE 0
289end
290fn wide ins edit ulong 8192 m edit ulong 512 d ulong a ulong base ulong to void
291ef ins m IMADW r4 d a NONE base 4
292end
293fn load ins edit ulong 8192 m edit ulong 512 d ulong addr ulong off ulong to void
294emit ins m okk 11 KVAR 0 0 r4 d addr NONE NONE off
295end
296fn strong ins edit ulong 8192 m edit ulong 512 d ulong addr ulong to void
297emit ins m okk 20 KVAR 0 0 r4 d addr NONE NONE 0
298end
299fn atomic ins edit ulong 8192 m edit ulong 512 op ulong d ulong addr ulong v ulong to void
300emit ins m okk op KVAR 0 0 r4 d addr v NONE 0
301end
302fn jump ins edit ulong 8192 m edit ulong 512 to ulong
303at = get m 0
304emit ins m okk BRA KBRA 0 NONE r4 NONE NONE NONE NONE 1
305ret at
306end
307fn back ins edit ulong 8192 m edit ulong 512 at ulong flags ulong to void
308emit ins m okk BRA KBRA flags NONE r4 NONE NONE NONE NONE and sub at get m 0 LO32
309end
310fn geimm ins edit ulong 8192 m edit ulong 512 a ulong v ulong to void
311mov ins m 29 v
312isge ins m a 29
313end
314
315fn prologue ins edit ulong 8192 m edit ulong 512 n ulong ctas ulong to void
316for v ulong 31
317kind = ulong 1
318if lt v 8
319kind = 2
320end
321w = nv m kind
322mov ins m w 0
323if lt v 8
324mov ins m sb w 1 0
325end
326end
327u = nu m 2
328emit ins m okk LDCU64 KUVAR 1 NONE r4 u NONE NONE NONE 856
329for v ulong 3
330emit ins m okk LDC64 KVAR 0 NONE r4 v NONE NONE NONE add 896 mul v 8
331end
332mov ins m 22 1
333ef ins m IMADW r4 3 22 NONE 2 4
334mov ins m 24 ctas
335mov ins m 25 n
336note S2R is variable latency on the chip: it takes a write barrier and its reader waits (KVAR), as in
337note om/kgen/ew_emit.om and om/mathlibs/mathlibs_ir.om. Emitted as a fixed-latency op the thread-id test
338note read the zero written above, no thread left, and every lane of both CTAs ran the barrier
339note (GB10, 2026-11-12: marker wait timed out, counter T - 1, generation 2).
340emit ins m okk S2R KVAR 0 NONE r4 8 NONE NONE NONE 33
341geimm ins m 8 1
342exitp ins m
343emit ins m okk S2R KVAR 0 NONE r4 8 NONE NONE NONE 37
344end
345
346note Each CTA owns rows cta, cta+ctas,... . Serial k fixes f32 order.
347fn dotbody ins edit ulong 8192 m edit ulong 512 mat bool to void
348mov ins m 11 0
349mov ins m 18 0
350at = get m 0
351if mat
352ef ins m IMAD r4 21 10 17 11 0
353else
354cp ins m 21 11
355end
356plus ins m 21 21 13
357wide ins m 5 21 1
358strong ins m 19 5
359if mat
360plus ins m 21 14 11
361wide ins m 6 21 1
362strong ins m 20 6
363ef ins m FMUL r4 19 19 20 NONE 0
364end
365ef ins m FADD r4 18 18 19 NONE 0
366plus ins m 11 11 22
367isge ins m 11 17
368back ins m at 6
369end
370
371fn elbody ins edit ulong 8192 m edit ulong 512 op ulong to void
372plus ins m 21 13 10
373wide ins m 5 21 1
374strong ins m 18 5
375if ne op 5
376plus ins m 21 14 10
377wide ins m 6 21 1
378strong ins m 20 6
379alu = FADD
380if eq op 3
381alu = FMUL
382end
383ef ins m alu r4 18 18 20 NONE 0
384end
385end
386
387fn work ins edit ulong 8192 m edit ulong 512 to void
388cp ins m 10 8
389rowtop = get m 0
390isge ins m 10 16
391done = bfwd ins m
392ends = new ulong 5 0
393for op ulong 5
394geimm ins m 12 add op 2
395skip = bfwd ins m
396if eq op 0
397dotbody ins m true
398else
399if eq op 3
400dotbody ins m false
401else
402elbody ins m add op 1
403end
404end
405put ends op jump ins m
406patch ins m skip
407end
408for op ulong 5
409patch ins m get ends op
410end
411plus ins m 21 15 10
412wide ins m 7 21 1
413emit ins m okk 22 KSTORE 0 0 r4 NONE 7 18 NONE 0
414plus ins m 10 10 24
415back ins m rowtop 0
416patch ins m done
417end
418
419note Sense-reversing grid barrier: release stores, count arrivals, last resets
420note count before publishing next generation; strong loads poll generation.
421fn gridbar ins edit ulong 8192 m edit ulong 512 to void
422emit ins m okk 23 KSYNC 0 NONE r4 NONE NONE NONE NONE 0
423atomic ins m 26 27 2 22
424ef ins m IMAD r4 29 24 NONE NONE 1
425mov ins m 30 4294967295
426plus ins m 29 29 30
427isge ins m 27 29
428last = bfwd ins m
429poll = get m 0
430strong ins m 28 3
431isge ins m 28 26
432back ins m poll 6
433finish = jump ins m
434patch ins m last
435atomic ins m 27 27 2 23
436emit ins m okk 23 KSYNC 0 NONE r4 NONE NONE NONE NONE 0
437atomic ins m 27 27 3 26
438patch ins m finish
439emit ins m okk 23 KSYNC 0 NONE r4 NONE NONE NONE NONE 0
440end
441
442fn resident kind ulong n ulong size ulong ctas ulong ins edit ulong 8192 m edit ulong 512 to ulong
443for i ulong 512
444put m i 0
445end
446if or ne kind 0 or eq n 0 gt n 64
447ret 1
448end
449if or or eq size 0 gt size 65536 or eq ctas 0 gt ctas 2
450ret 1
451end
452prologue ins m n ctas
453outer = get m 0
454ef ins m IMADW r4 4 9 NONE 0 32
455for j ulong 6
456load ins m add 12 j 4 mul j 4
457end
458work ins m
459plus ins m 26 9 22
460gridbar ins m
461plus ins m 9 9 22
462isge ins m 9 25
463back ins m outer 6
464note Read every fixed vreg after the loop so the allocator cannot alias branch arms.
465for v ulong 31
466cp ins m v v
467end
468tail ins m
469ret get m 4
470end