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