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 / backend / spirv / emit.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note emit.om: seeded-Omega SPIR-V kernel builder for the portable backend (docs/ARCHITECTURE.md). |
| 3 | note It writes one SPIR-V module to stdout for a fixed FP32 kernel and one environment: |
| 4 | note cl SPIR-V 1.0, OpenCL Kernel, Physical64, CrossWorkgroup pointer arguments (format SPIRV_CL) |
| 5 | note vk SPIR-V 1.3, Vulkan Shader GLCompute, Logical GLSL450, StorageBuffer bindings (format SPIRV_VK) |
| 6 | note Why a kernel builder and not the IR route: omc IR mode refuses float words (OMEGESE 9.9 at |
| 7 | note omega-mv 369dbc96), so seed/spirv/irspirv.om and vkspirv.om translate integer IR only. This file |
| 8 | note is the FP32 builder until IR v7 (omega-mv#883) carries floats; it is not a compiler. |
| 9 | note Opcode, enum and layout authority: registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html |
| 10 | note Every module is checked by spirv-val and by backend/targets.om before any run. |
| 11 | note |
| 12 | note Kernels (n = element count for vadd, row count for fsum16; gid = global invocation x): |
| 13 | note vadd c[gid] = a[gid] + b[gid] for gid < n (hello_gpu's operation) |
| 14 | note fsum16 o[gid] = (((x[16g] + x[16g+1]) + x[16g+2]) ... + x[16g+15]) frozen left-to-right order |
| 15 | note cl arguments: vadd (a, b, c, uint n), fsum16 (x, o, uint n). |
| 16 | note vk bindings set 0: vadd a=0 b=1 c=2, fsum16 x=0 o=1; n = OpArrayLength of the output buffer; |
| 17 | note LocalSize 64 1 1. |
| 18 | note |
| 19 | note Usage: emit vadd|fsum16 cl|vk [mutant] |
| 20 | note mutant plants a wrong kernel for negative controls: vadd uses OpFSub, fsum16 adds right-to-left. |
| 21 | note Exit: 0 module written; 2 usage; 3 stdout write failed. Traps exit 100 + code. |
| 22 | om2 |
| 23 | |
| 24 | text tuse "usage: emit vadd|fsum16 cl|vk [mutant]\n" |
| 25 | text tio "emit: stdout write failed\n" |
| 26 | text tvadd "vadd" |
| 27 | text tfsum "fsum16" |
| 28 | text tcl "cl" |
| 29 | text tvk "vk" |
| 30 | text tmut "mutant" |
| 31 | |
| 32 | const IVOID uint 1 |
| 33 | const IU32 uint 2 |
| 34 | const IU64 uint 3 |
| 35 | const IF32 uint 4 |
| 36 | const IBOOL uint 5 |
| 37 | const IV3 uint 6 |
| 38 | const IPIN uint 7 |
| 39 | const IPF uint 8 |
| 40 | const IFN uint 9 |
| 41 | const IGID uint 10 |
| 42 | const IMAIN uint 11 |
| 43 | const IRTA uint 12 |
| 44 | const IST uint 13 |
| 45 | const IPST uint 14 |
| 46 | const IB0 uint 15 |
| 47 | const IB1 uint 16 |
| 48 | const IB2 uint 17 |
| 49 | const IN uint 18 |
| 50 | const IK0 uint 20 |
| 51 | const IK16 uint 36 |
| 52 | const IDYN uint 40 |
| 53 | |
| 54 | global gw uint 4096 |
| 55 | global gob uchar 16384 |
| 56 | global garg uchar 16 |
| 57 | note gst: 0 word fill, 1 next id, 2 env (0 cl, 1 vk), 3 kernel (0 vadd, 1 fsum16), 4 mutant |
| 58 | global gst ulong 8 |
| 59 | |
| 60 | fn usage to void |
| 61 | n = write 2 tuse 0 len tuse |
| 62 | exit 2 |
| 63 | end |
| 64 | |
| 65 | fn w v uint to void |
| 66 | f = get gst 0 |
| 67 | put gw f v |
| 68 | put gst 0 add f 1 |
| 69 | end |
| 70 | |
| 71 | fn op c uint n uint to void |
| 72 | w or shl n 16 c |
| 73 | end |
| 74 | |
| 75 | fn nid to uint |
| 76 | i = get gst 1 |
| 77 | put gst 1 add i 1 |
| 78 | ret uint i |
| 79 | end |
| 80 | |
| 81 | fn isvk to bool |
| 82 | ret eq get gst 2 1 |
| 83 | end |
| 84 | |
| 85 | note Literal string: bytes little-endian in words, a zero byte always ends it, zero padded. |
| 86 | fn strwc t view uchar * to uint |
| 87 | ret uint add div len t 4 1 |
| 88 | end |
| 89 | |
| 90 | fn wstr t view uchar * to void |
| 91 | nw = ulong strwc t |
| 92 | for i ulong nw |
| 93 | v = uint 0 |
| 94 | for k ulong 4 |
| 95 | j = add mul i 4 k |
| 96 | if lt j len t |
| 97 | v = or v shl uint get t j mul uint k 8 |
| 98 | end |
| 99 | end |
| 100 | w v |
| 101 | end |
| 102 | end |
| 103 | |
| 104 | fn argis i ulong t view uchar * to bool |
| 105 | n = argread i garg 0 len garg |
| 106 | if ne n long len t |
| 107 | ret false |
| 108 | end |
| 109 | for k ulong len t |
| 110 | if ne get garg k get t k |
| 111 | ret false |
| 112 | end |
| 113 | end |
| 114 | ret true |
| 115 | end |
| 116 | |
| 117 | fn kname to void |
| 118 | if eq get gst 3 0 |
| 119 | wstr tvadd |
| 120 | ret |
| 121 | end |
| 122 | wstr tfsum |
| 123 | end |
| 124 | |
| 125 | fn knamewc to uint |
| 126 | if eq get gst 3 0 |
| 127 | ret strwc tvadd |
| 128 | end |
| 129 | ret strwc tfsum |
| 130 | end |
| 131 | |
| 132 | fn nbuf to uint |
| 133 | if eq get gst 3 0 |
| 134 | ret 3 |
| 135 | end |
| 136 | ret 2 |
| 137 | end |
| 138 | |
| 139 | note Buffer i (0-based) as an id: cl parameters and vk variables share IB0..IB2. |
| 140 | fn bufid i uint to uint |
| 141 | ret add IB0 i |
| 142 | end |
| 143 | |
| 144 | fn outbuf to uint |
| 145 | ret bufid sub nbuf 1 |
| 146 | end |
| 147 | |
| 148 | fn header to void |
| 149 | if isvk |
| 150 | op 17 2 |
| 151 | w 1 |
| 152 | op 14 3 |
| 153 | w 0 |
| 154 | w 1 |
| 155 | op 15 add 4 knamewc |
| 156 | w 5 |
| 157 | w IMAIN |
| 158 | kname |
| 159 | w IGID |
| 160 | op 16 6 |
| 161 | w IMAIN |
| 162 | w 17 |
| 163 | w 64 |
| 164 | w 1 |
| 165 | w 1 |
| 166 | ret |
| 167 | end |
| 168 | op 17 2 |
| 169 | w 4 |
| 170 | op 17 2 |
| 171 | w 6 |
| 172 | op 17 2 |
| 173 | w 11 |
| 174 | op 14 3 |
| 175 | w 2 |
| 176 | w 2 |
| 177 | op 15 add 4 knamewc |
| 178 | w 6 |
| 179 | w IMAIN |
| 180 | kname |
| 181 | w IGID |
| 182 | end |
| 183 | |
| 184 | fn decor to void |
| 185 | op 71 4 |
| 186 | w IGID |
| 187 | w 11 |
| 188 | w 28 |
| 189 | if not isvk |
| 190 | op 71 3 |
| 191 | w IGID |
| 192 | w 22 |
| 193 | ret |
| 194 | end |
| 195 | op 71 4 |
| 196 | w IRTA |
| 197 | w 6 |
| 198 | w 4 |
| 199 | op 72 5 |
| 200 | w IST |
| 201 | w 0 |
| 202 | w 35 |
| 203 | w 0 |
| 204 | op 71 3 |
| 205 | w IST |
| 206 | w 2 |
| 207 | for i uint nbuf |
| 208 | op 71 4 |
| 209 | w bufid i |
| 210 | w 34 |
| 211 | w 0 |
| 212 | op 71 4 |
| 213 | w bufid i |
| 214 | w 33 |
| 215 | w i |
| 216 | end |
| 217 | end |
| 218 | |
| 219 | fn types to void |
| 220 | op 19 2 |
| 221 | w IVOID |
| 222 | op 21 4 |
| 223 | w IU32 |
| 224 | w 32 |
| 225 | w 0 |
| 226 | op 22 3 |
| 227 | w IF32 |
| 228 | w 32 |
| 229 | op 20 2 |
| 230 | w IBOOL |
| 231 | if isvk |
| 232 | op 23 4 |
| 233 | w IV3 |
| 234 | w IU32 |
| 235 | w 3 |
| 236 | op 32 4 |
| 237 | w IPIN |
| 238 | w 1 |
| 239 | w IV3 |
| 240 | op 29 3 |
| 241 | w IRTA |
| 242 | w IF32 |
| 243 | op 30 3 |
| 244 | w IST |
| 245 | w IRTA |
| 246 | op 32 4 |
| 247 | w IPST |
| 248 | w 12 |
| 249 | w IST |
| 250 | op 32 4 |
| 251 | w IPF |
| 252 | w 12 |
| 253 | w IF32 |
| 254 | op 33 3 |
| 255 | w IFN |
| 256 | w IVOID |
| 257 | ret |
| 258 | end |
| 259 | op 21 4 |
| 260 | w IU64 |
| 261 | w 64 |
| 262 | w 0 |
| 263 | op 23 4 |
| 264 | w IV3 |
| 265 | w IU64 |
| 266 | w 3 |
| 267 | op 32 4 |
| 268 | w IPIN |
| 269 | w 1 |
| 270 | w IV3 |
| 271 | op 32 4 |
| 272 | w IPF |
| 273 | w 5 |
| 274 | w IF32 |
| 275 | op 33 add 4 nbuf |
| 276 | w IFN |
| 277 | w IVOID |
| 278 | for i uint nbuf |
| 279 | w IPF |
| 280 | end |
| 281 | w IU32 |
| 282 | end |
| 283 | |
| 284 | note Index constants k = 0..16 at ids IK0 + k: ulong for cl (two literal words), uint for vk. |
| 285 | fn consts to void |
| 286 | for k uint 17 |
| 287 | if isvk |
| 288 | op 43 4 |
| 289 | w IU32 |
| 290 | w add IK0 k |
| 291 | w k |
| 292 | end |
| 293 | if not isvk |
| 294 | op 43 5 |
| 295 | w IU64 |
| 296 | w add IK0 k |
| 297 | w k |
| 298 | w 0 |
| 299 | end |
| 300 | end |
| 301 | end |
| 302 | |
| 303 | fn vars to void |
| 304 | if isvk |
| 305 | for i uint nbuf |
| 306 | op 59 4 |
| 307 | w IPST |
| 308 | w bufid i |
| 309 | w 12 |
| 310 | end |
| 311 | end |
| 312 | op 59 4 |
| 313 | w IPIN |
| 314 | w IGID |
| 315 | w 1 |
| 316 | end |
| 317 | |
| 318 | fn idxty to uint |
| 319 | if isvk |
| 320 | ret IU32 |
| 321 | end |
| 322 | ret IU64 |
| 323 | end |
| 324 | |
| 325 | fn eptr b uint x uint to uint |
| 326 | p = nid |
| 327 | if isvk |
| 328 | op 65 6 |
| 329 | w IPF |
| 330 | w p |
| 331 | w b |
| 332 | w IK0 |
| 333 | w x |
| 334 | ret p |
| 335 | end |
| 336 | op 70 5 |
| 337 | w IPF |
| 338 | w p |
| 339 | w b |
| 340 | w x |
| 341 | ret p |
| 342 | end |
| 343 | |
| 344 | fn ld p uint to uint |
| 345 | r = nid |
| 346 | if isvk |
| 347 | op 61 4 |
| 348 | w IF32 |
| 349 | w r |
| 350 | w p |
| 351 | ret r |
| 352 | end |
| 353 | op 61 6 |
| 354 | w IF32 |
| 355 | w r |
| 356 | w p |
| 357 | w 2 |
| 358 | w 4 |
| 359 | ret r |
| 360 | end |
| 361 | |
| 362 | fn st p uint v uint to void |
| 363 | if isvk |
| 364 | op 62 3 |
| 365 | w p |
| 366 | w v |
| 367 | ret |
| 368 | end |
| 369 | op 62 5 |
| 370 | w p |
| 371 | w v |
| 372 | w 2 |
| 373 | w 4 |
| 374 | end |
| 375 | |
| 376 | fn farith c uint a uint b uint to uint |
| 377 | r = nid |
| 378 | op c 5 |
| 379 | w IF32 |
| 380 | w r |
| 381 | w a |
| 382 | w b |
| 383 | ret r |
| 384 | end |
| 385 | |
| 386 | fn iarith c uint a uint b uint to uint |
| 387 | r = nid |
| 388 | op c 5 |
| 389 | w idxty |
| 390 | w r |
| 391 | w a |
| 392 | w b |
| 393 | ret r |
| 394 | end |
| 395 | |
| 396 | fn bvadd x uint to void |
| 397 | a = ld eptr IB0 x |
| 398 | b = ld eptr IB1 x |
| 399 | c = uint 129 |
| 400 | if eq get gst 4 1 |
| 401 | c = 131 |
| 402 | end |
| 403 | s = farith c a b |
| 404 | st eptr IB2 x s |
| 405 | end |
| 406 | |
| 407 | fn fsel i uint to uint |
| 408 | if eq get gst 4 1 |
| 409 | ret sub 15 i |
| 410 | end |
| 411 | ret i |
| 412 | end |
| 413 | |
| 414 | fn fld bs uint k uint to uint |
| 415 | x = bs |
| 416 | if ne k 0 |
| 417 | x = iarith 128 bs add IK0 k |
| 418 | end |
| 419 | ret ld eptr IB0 x |
| 420 | end |
| 421 | |
| 422 | fn bfsum x uint to void |
| 423 | bs = iarith 132 x IK16 |
| 424 | acc = fld bs fsel 0 |
| 425 | for i uint 15 |
| 426 | v = fld bs fsel add i 1 |
| 427 | acc = farith 129 acc v |
| 428 | end |
| 429 | st eptr IB1 x acc |
| 430 | end |
| 431 | |
| 432 | fn count x uint to uint |
| 433 | n = nid |
| 434 | if isvk |
| 435 | op 68 5 |
| 436 | w IU32 |
| 437 | w n |
| 438 | w outbuf |
| 439 | w 0 |
| 440 | ret n |
| 441 | end |
| 442 | op 113 4 |
| 443 | w IU64 |
| 444 | w n |
| 445 | w IN |
| 446 | ret n |
| 447 | end |
| 448 | |
| 449 | fn body to void |
| 450 | op 54 5 |
| 451 | w IVOID |
| 452 | w IMAIN |
| 453 | w 0 |
| 454 | w IFN |
| 455 | if not isvk |
| 456 | for i uint nbuf |
| 457 | op 55 3 |
| 458 | w IPF |
| 459 | w bufid i |
| 460 | end |
| 461 | op 55 3 |
| 462 | w IU32 |
| 463 | w IN |
| 464 | end |
| 465 | op 248 2 |
| 466 | w nid |
| 467 | v = nid |
| 468 | op 61 4 |
| 469 | w IV3 |
| 470 | w v |
| 471 | w IGID |
| 472 | x = nid |
| 473 | op 81 5 |
| 474 | w idxty |
| 475 | w x |
| 476 | w v |
| 477 | w 0 |
| 478 | n = count x |
| 479 | c = nid |
| 480 | op 176 5 |
| 481 | w IBOOL |
| 482 | w c |
| 483 | w x |
| 484 | w n |
| 485 | tl = nid |
| 486 | ml = nid |
| 487 | op 247 3 |
| 488 | w ml |
| 489 | w 0 |
| 490 | op 250 4 |
| 491 | w c |
| 492 | w tl |
| 493 | w ml |
| 494 | op 248 2 |
| 495 | w tl |
| 496 | if eq get gst 3 0 |
| 497 | bvadd x |
| 498 | end |
| 499 | if eq get gst 3 1 |
| 500 | bfsum x |
| 501 | end |
| 502 | op 249 2 |
| 503 | w ml |
| 504 | op 248 2 |
| 505 | w ml |
| 506 | op 253 1 |
| 507 | op 56 1 |
| 508 | end |
| 509 | |
| 510 | fn ob o ulong v uint to void |
| 511 | for k ulong 4 |
| 512 | put gob add o k uchar and shr v mul uint k 8 255 |
| 513 | end |
| 514 | end |
| 515 | |
| 516 | fn flush to void |
| 517 | nw = get gst 0 |
| 518 | ob 0 0x07230203 |
| 519 | if isvk |
| 520 | ob 4 0x00010300 |
| 521 | end |
| 522 | if not isvk |
| 523 | ob 4 0x00010000 |
| 524 | end |
| 525 | ob 8 0 |
| 526 | ob 12 uint get gst 1 |
| 527 | ob 16 0 |
| 528 | for i ulong nw |
| 529 | ob add 20 mul i 4 get gw i |
| 530 | end |
| 531 | tot = add 20 mul nw 4 |
| 532 | r = write 1 gob 0 tot |
| 533 | if ne r long tot |
| 534 | n = write 2 tio 0 len tio |
| 535 | exit 3 |
| 536 | end |
| 537 | end |
| 538 | |
| 539 | fn main to int |
| 540 | ac = argcnt |
| 541 | if or lt ac 3 gt ac 4 |
| 542 | usage |
| 543 | end |
| 544 | put gst 3 9 |
| 545 | if argis 1 tvadd |
| 546 | put gst 3 0 |
| 547 | end |
| 548 | if argis 1 tfsum |
| 549 | put gst 3 1 |
| 550 | end |
| 551 | put gst 2 9 |
| 552 | if argis 2 tcl |
| 553 | put gst 2 0 |
| 554 | end |
| 555 | if argis 2 tvk |
| 556 | put gst 2 1 |
| 557 | end |
| 558 | if or eq get gst 3 9 eq get gst 2 9 |
| 559 | usage |
| 560 | end |
| 561 | if eq ac 4 |
| 562 | if not argis 3 tmut |
| 563 | usage |
| 564 | end |
| 565 | put gst 4 1 |
| 566 | end |
| 567 | put gst 1 ulong IDYN |
| 568 | header |
| 569 | decor |
| 570 | types |
| 571 | consts |
| 572 | vars |
| 573 | body |
| 574 | flush |
| 575 | ret 0 |
| 576 | end |