Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/gpu/spirv/emit.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / gpu / spirv / emit.om` 891 lines ยท 12969 bytes Raw Blame History 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 vmul c[gid] = a[gid] * b[gid] for gid < n 15 note vsub c[gid] = a[gid] - b[gid] for gid < n 16 note vmax c[gid] = a[gid] > b[gid] ? a[gid] : b[gid] for gid < n (OpFOrdGreaterThan, OpSelect) 17 note fsum16 o[gid] = (((x[16g] + x[16g+1]) + x[16g+2]) ... + x[16g+15]) frozen left-to-right order 18 note gemm16 one work-item per element of a 16x16 f32 tile, 4096 tiles (n = `1048576`): 19 note gid = tile * 256 + row * 16 + col 20 note C = 0; for k in 0..15 left to right: C = C + (A[row,k] * B[k,col]) 21 note each * and + is its own op. No OpFma. 22 note Vulkan decorates every mul and add with NoContraction (42). spirv-val --target-env opencl2.2 23 note rejects that decoration (requires Capability Shader, which OpenCL forbids), so the OpenCL 24 note module uses ExecutionMode ContractionOff (31) instead. spirv-val --target-env vulkan1.1 25 note rejects ContractionOff (requires Capability Kernel, which Vulkan does not grant). 26 note A and B are row-major 16x16 inside the tile (256 floats per tile, same length as C). 27 note rmsnorm one work-item per row of 16 (train/openaccel-lab/workloads/rmsnorm/omega.om, frozen): 28 note ss = (((x0*x0 + x1*x1) + x2*x2) ... + x15*x15) 29 note yk = (xk * (1 / sqrt(ss * (1/16) + eps))) * wk eps = binary32(0.000001) = 0x358637BD 30 note sqrt then div, not rsqrt and not fma. 1/16 is 0.0625 and is exact. 31 note cl arguments: vadd, vmul, vsub, vmax, gemm16 (a, b, c, uint n), fsum16 (x, o, uint n), 32 note rmsnorm (x, w, y, uint nrows). w is 16 floats, shared by every row. 33 note vk bindings set 0: vadd, vmul, vsub, vmax, gemm16 a=0 b=1 c=2, fsum16 x=0 o=1, rmsnorm x=0 w=1 y=2; 34 note n = OpArrayLength of the output buffer (rmsnorm divides that length by 16); 35 note LocalSize 64 1 1. 36 note 37 note Usage: emit vadd|vmul|vsub|vmax|fsum16|rmsnorm|gemm16 cl|vk [mutant] 38 note mutant plants a wrong kernel for negative controls: vadd uses OpFSub, vmul and vsub use OpFAdd, 39 note vmax selects the other operand (min), fsum16, rmsnorm and gemm16 add right-to-left. 40 note Exit: 0 module written; 2 usage; 3 stdout write failed. Traps exit 100 + code. 41 om2 42 43 text tuse "usage: emit vadd|vmul|vsub|vmax|fsum16|rmsnorm|gemm16 cl|vk [mutant]\n" 44 text tio "emit: stdout write failed\n" 45 text tvadd "vadd" 46 text tfsum "fsum16" 47 text tvmul "vmul" 48 text tvsub "vsub" 49 text tvmax "vmax" 50 text trms "rmsnorm" 51 text tgemm "gemm16" 52 text tocls "OpenCL.std" 53 text tglsl "GLSL.std.450" 54 text tcl "cl" 55 text tvk "vk" 56 text tmut "mutant" 57 58 const IVOID uint 1 59 const IU32 uint 2 60 const IU64 uint 3 61 const IF32 uint 4 62 const IBOOL uint 5 63 const IV3 uint 6 64 const IPIN uint 7 65 const IPF uint 8 66 const IFN uint 9 67 const IGID uint 10 68 const IMAIN uint 11 69 const IRTA uint 12 70 const IST uint 13 71 const IPST uint 14 72 const IB0 uint 15 73 const IB1 uint 16 74 const IB2 uint 17 75 const IN uint 18 76 const IK0 uint 20 77 const IK16 uint 36 78 const IEXT uint 19 79 const IONE uint 37 80 const IEPS uint 38 81 const IINV uint 39 82 note gemm16 ids: 256 and f32 0, then 16 mul results and 16 add results. Each is NoContraction. 83 const I256 uint 74 84 const IZERO uint 75 85 const IMUL0 uint 80 86 const IADD0 uint 96 87 const IDYN uint 112 88 89 global gw uint 8192 90 global gob uchar 65536 91 global garg uchar 16 92 note gst: 0 word fill, 1 next id, 2 env (0 cl, 1 vk), 3 kernel (0 vadd, 1 fsum16, 2 vmul, 3 vsub, 4 vmax, 5 rmsnorm, 6 gemm16), 4 mutant 93 global gst ulong 8 94 95 fn usage to void 96 n = write 2 tuse 0 len tuse 97 exit 2 98 end 99 100 fn w v uint to void 101 f = get gst 0 102 put gw f v 103 put gst 0 add f 1 104 end 105 106 fn op c uint n uint to void 107 w or shl n 16 c 108 end 109 110 fn nid to uint 111 i = get gst 1 112 put gst 1 add i 1 113 ret uint i 114 end 115 116 fn isvk to bool 117 ret eq get gst 2 1 118 end 119 120 note Literal string: bytes little-endian in words, a zero byte always ends it, zero padded. 121 fn strwc t view uchar * to uint 122 ret uint add div len t 4 1 123 end 124 125 fn wstr t view uchar * to void 126 nw = ulong strwc t 127 for i ulong nw 128 v = uint 0 129 for k ulong 4 130 j = add mul i 4 k 131 if lt j len t 132 v = or v shl uint get t j mul uint k 8 133 end 134 end 135 w v 136 end 137 end 138 139 fn argis i ulong t view uchar * to bool 140 n = argread i garg 0 len garg 141 if ne n long len t 142 ret false 143 end 144 for k ulong len t 145 if ne get garg k get t k 146 ret false 147 end 148 end 149 ret true 150 end 151 152 fn kname to void 153 if eq get gst 3 0 154 wstr tvadd 155 ret 156 end 157 if eq get gst 3 2 158 wstr tvmul 159 ret 160 end 161 if eq get gst 3 3 162 wstr tvsub 163 ret 164 end 165 if eq get gst 3 4 166 wstr tvmax 167 ret 168 end 169 if eq get gst 3 5 170 wstr trms 171 ret 172 end 173 if eq get gst 3 6 174 wstr tgemm 175 ret 176 end 177 wstr tfsum 178 end 179 180 fn knamewc to uint 181 if eq get gst 3 0 182 ret strwc tvadd 183 end 184 if eq get gst 3 2 185 ret strwc tvmul 186 end 187 if eq get gst 3 3 188 ret strwc tvsub 189 end 190 if eq get gst 3 4 191 ret strwc tvmax 192 end 193 if eq get gst 3 5 194 ret strwc trms 195 end 196 if eq get gst 3 6 197 ret strwc tgemm 198 end 199 ret strwc tfsum 200 end 201 202 fn nbuf to uint 203 if eq get gst 3 1 204 ret 2 205 end 206 ret 3 207 end 208 209 note Buffer i (0-based) as an id: cl parameters and vk variables share IB0..IB2. 210 fn bufid i uint to uint 211 ret add IB0 i 212 end 213 214 fn outbuf to uint 215 ret bufid sub nbuf 1 216 end 217 218 fn extimp to void 219 if ne get gst 3 5 220 ret 221 end 222 if isvk 223 op 11 add 2 strwc tglsl 224 w IEXT 225 wstr tglsl 226 ret 227 end 228 op 11 add 2 strwc tocls 229 w IEXT 230 wstr tocls 231 end 232 233 fn header to void 234 if isvk 235 op 17 2 236 w 1 237 extimp 238 op 14 3 239 w 0 240 w 1 241 op 15 add 4 knamewc 242 w 5 243 w IMAIN 244 kname 245 w IGID 246 op 16 6 247 w IMAIN 248 w 17 249 w 64 250 w 1 251 w 1 252 ret 253 end 254 op 17 2 255 w 4 256 op 17 2 257 w 6 258 op 17 2 259 w 11 260 extimp 261 op 14 3 262 w 2 263 w 2 264 op 15 add 4 knamewc 265 w 6 266 w IMAIN 267 kname 268 w IGID 269 nocon 270 end 271 272 note Execution mode 31 is ContractionOff, OpenCL only. Vulkan rejects it (needs Kernel). 273 note Applied to rmsnorm (5) and gemm16 (6) so OpenCL runtimes do not contract mul+add into FMA. 274 fn nocon to void 275 if isvk 276 ret 277 end 278 if and ne get gst 3 5 ne get gst 3 6 279 ret 280 end 281 op 16 3 282 w IMAIN 283 w 31 284 end 285 286 fn decor to void 287 op 71 4 288 w IGID 289 w 11 290 w 28 291 if not isvk 292 op 71 3 293 w IGID 294 w 22 295 end 296 if isvk 297 op 71 4 298 w IRTA 299 w 6 300 w 4 301 op 72 5 302 w IST 303 w 0 304 w 35 305 w 0 306 op 71 3 307 w IST 308 w 2 309 for i uint nbuf 310 op 71 4 311 w bufid i 312 w 34 313 w 0 314 op 71 4 315 w bufid i 316 w 33 317 w i 318 end 319 end 320 if ne get gst 3 6 321 ret 322 end 323 if not isvk 324 ret 325 end 326 note Decoration 42 is NoContraction. Vulkan only: OpenCL forbids it without Shader. 327 for i uint 32 328 op 71 3 329 w add IMUL0 i 330 w 42 331 end 332 end 333 334 fn types to void 335 op 19 2 336 w IVOID 337 op 21 4 338 w IU32 339 w 32 340 w 0 341 op 22 3 342 w IF32 343 w 32 344 op 20 2 345 w IBOOL 346 if isvk 347 op 23 4 348 w IV3 349 w IU32 350 w 3 351 op 32 4 352 w IPIN 353 w 1 354 w IV3 355 op 29 3 356 w IRTA 357 w IF32 358 op 30 3 359 w IST 360 w IRTA 361 op 32 4 362 w IPST 363 w 12 364 w IST 365 op 32 4 366 w IPF 367 w 12 368 w IF32 369 op 33 3 370 w IFN 371 w IVOID 372 ret 373 end 374 op 21 4 375 w IU64 376 w 64 377 w 0 378 op 23 4 379 w IV3 380 w IU64 381 w 3 382 op 32 4 383 w IPIN 384 w 1 385 w IV3 386 op 32 4 387 w IPF 388 w 5 389 w IF32 390 op 33 add 4 nbuf 391 w IFN 392 w IVOID 393 for i uint nbuf 394 w IPF 395 end 396 w IU32 397 end 398 399 note Index constants k = 0..16 at ids IK0 + k: ulong for cl (two literal words), uint for vk. 400 note rmsnorm also emits 1, eps and 1/16 at IONE, IEPS, IINV. 401 fn consts to void 402 for k uint 17 403 if isvk 404 op 43 4 405 w IU32 406 w add IK0 k 407 w k 408 end 409 if not isvk 410 op 43 5 411 w IU64 412 w add IK0 k 413 w k 414 w 0 415 end 416 end 417 if eq get gst 3 6 418 if isvk 419 op 43 4 420 w IU32 421 w I256 422 w 256 423 end 424 if not isvk 425 op 43 5 426 w IU64 427 w I256 428 w 256 429 w 0 430 end 431 op 43 4 432 w IF32 433 w IZERO 434 w 0 435 ret 436 end 437 if ne get gst 3 5 438 ret 439 end 440 op 43 4 441 w IF32 442 w IONE 443 w 0x3f800000 444 op 43 4 445 w IF32 446 w IEPS 447 w 0x358637bd 448 op 43 4 449 w IF32 450 w IINV 451 w 0x3d800000 452 end 453 454 fn vars to void 455 if isvk 456 for i uint nbuf 457 op 59 4 458 w IPST 459 w bufid i 460 w 12 461 end 462 end 463 op 59 4 464 w IPIN 465 w IGID 466 w 1 467 end 468 469 fn idxty to uint 470 if isvk 471 ret IU32 472 end 473 ret IU64 474 end 475 476 fn eptr b uint x uint to uint 477 p = nid 478 if isvk 479 op 65 6 480 w IPF 481 w p 482 w b 483 w IK0 484 w x 485 ret p 486 end 487 op 70 5 488 w IPF 489 w p 490 w b 491 w x 492 ret p 493 end 494 495 fn ld p uint to uint 496 r = nid 497 if isvk 498 op 61 4 499 w IF32 500 w r 501 w p 502 ret r 503 end 504 op 61 6 505 w IF32 506 w r 507 w p 508 w 2 509 w 4 510 ret r 511 end 512 513 fn st p uint v uint to void 514 if isvk 515 op 62 3 516 w p 517 w v 518 ret 519 end 520 op 62 5 521 w p 522 w v 523 w 2 524 w 4 525 end 526 527 fn farith c uint a uint b uint to uint 528 r = nid 529 op c 5 530 w IF32 531 w r 532 w a 533 w b 534 ret r 535 end 536 537 fn farithid c uint a uint b uint r uint to uint 538 op c 5 539 w IF32 540 w r 541 w a 542 w b 543 ret r 544 end 545 546 fn iarith c uint a uint b uint to uint 547 r = nid 548 op c 5 549 w idxty 550 w r 551 w a 552 w b 553 ret r 554 end 555 556 note Elementwise binary kernels: vadd FAdd 129, vmul FMul 133, vsub FSub 131; mutants swap the opcode. 557 fn binop to uint 558 c = uint 129 559 if eq get gst 3 2 560 c = 133 561 end 562 if eq get gst 3 3 563 c = 131 564 end 565 if eq get gst 4 1 566 c = 129 567 if eq get gst 3 0 568 c = 131 569 end 570 end 571 ret c 572 end 573 574 fn bvmax a uint b uint to uint 575 c = nid 576 op 186 5 577 w IBOOL 578 w c 579 w a 580 w b 581 r = nid 582 op 169 6 583 w IF32 584 w r 585 w c 586 if eq get gst 4 1 587 w b 588 w a 589 ret r 590 end 591 w a 592 w b 593 ret r 594 end 595 596 fn bvadd x uint to void 597 a = ld eptr IB0 x 598 b = ld eptr IB1 x 599 if eq get gst 3 4 600 st eptr IB2 x bvmax a b 601 ret 602 end 603 s = farith binop a b 604 st eptr IB2 x s 605 end 606 607 fn fsel i uint to uint 608 if eq get gst 4 1 609 ret sub 15 i 610 end 611 ret i 612 end 613 614 fn fld bs uint k uint to uint 615 x = bs 616 if ne k 0 617 x = iarith 128 bs add IK0 k 618 end 619 ret ld eptr IB0 x 620 end 621 622 fn sqrtv a uint to uint 623 r = nid 624 op 12 6 625 w IF32 626 w r 627 w IEXT 628 if isvk 629 w 31 630 end 631 if not isvk 632 w 61 633 end 634 w a 635 ret r 636 end 637 638 fn fldat bs uint k uint to uint 639 x = bs 640 if ne k 0 641 x = iarith 128 bs add IK0 k 642 end 643 ret ld eptr IB0 x 644 end 645 646 note rmsnorm: left-to-right sum of squares, then (x * (1 / sqrt(ss * 1/16 + eps))) * w. 647 note The mutant reverses only the sum, the same way fsum16 does. 648 fn brms x uint to void 649 bs = iarith 132 x IK16 650 z = fld bs fsel 0 651 acc = farith 133 z z 652 for i uint 15 653 v = fld bs fsel add i 1 654 acc = farith 129 acc farith 133 v v 655 end 656 acc = farith 133 acc IINV 657 acc = farith 129 acc IEPS 658 acc = farith 136 IONE sqrtv acc 659 for i uint 16 660 xv = fldat bs i 661 p = farith 133 xv acc 662 yv = farith 133 p ld eptr IB1 add IK0 i 663 idx = bs 664 if ne i 0 665 idx = iarith 128 bs add IK0 i 666 end 667 st eptr IB2 idx yv 668 end 669 end 670 671 fn bfsum x uint to void 672 bs = iarith 132 x IK16 673 acc = fld bs fsel 0 674 for i uint 15 675 v = fld bs fsel add i 1 676 acc = farith 129 acc v 677 end 678 st eptr IB1 x acc 679 end 680 681 note gemm16: C = 0; for k left to right, C = C + (A[row,k] * B[k,col]). 682 note Mul is OpFMul 133 and add is OpFAdd 129, each with its own result id (NoContraction). 683 note The mutant walks k from 15 down to 0. No OpFma. 684 fn gacc rowb uint j uint base uint s uint acc uint to uint 685 q = fsel s 686 q = add IK0 q 687 a = iarith 128 rowb q 688 a = iarith 128 base a 689 b = iarith 132 q IK16 690 b = iarith 128 b j 691 b = iarith 128 base b 692 a = ld eptr IB0 a 693 b = ld eptr IB1 b 694 q = farithid 133 a b add IMUL0 s 695 ret farithid 129 acc q add IADD0 s 696 end 697 698 fn bgemm x uint to void 699 p = iarith 134 x I256 700 q = iarith 137 x I256 701 i = iarith 134 q IK16 702 j = iarith 137 q IK16 703 rowb = iarith 132 i IK16 704 base = iarith 132 p I256 705 acc = IZERO 706 for s uint 16 707 acc = gacc rowb j base s acc 708 end 709 st eptr IB2 x acc 710 end 711 712 fn count x uint to uint 713 n = nid 714 if isvk 715 op 68 5 716 w IU32 717 w n 718 w outbuf 719 w 0 720 if ne get gst 3 5 721 ret n 722 end 723 d = nid 724 op 194 5 725 w IU32 726 w d 727 w n 728 w add IK0 4 729 ret d 730 end 731 op 113 4 732 w IU64 733 w n 734 w IN 735 ret n 736 end 737 738 fn body to void 739 op 54 5 740 w IVOID 741 w IMAIN 742 w 0 743 w IFN 744 if not isvk 745 for i uint nbuf 746 op 55 3 747 w IPF 748 w bufid i 749 end 750 op 55 3 751 w IU32 752 w IN 753 end 754 op 248 2 755 w nid 756 v = nid 757 op 61 4 758 w IV3 759 w v 760 w IGID 761 x = nid 762 op 81 5 763 w idxty 764 w x 765 w v 766 w 0 767 n = count x 768 c = nid 769 op 176 5 770 w IBOOL 771 w c 772 w x 773 w n 774 tl = nid 775 ml = nid 776 op 247 3 777 w ml 778 w 0 779 op 250 4 780 w c 781 w tl 782 w ml 783 op 248 2 784 w tl 785 k = get gst 3 786 if eq k 1 787 bfsum x 788 end 789 if eq k 5 790 brms x 791 end 792 if eq k 6 793 bgemm x 794 end 795 if ne k 1 796 if ne k 5 797 if ne k 6 798 bvadd x 799 end 800 end 801 end 802 op 249 2 803 w ml 804 op 248 2 805 w ml 806 op 253 1 807 op 56 1 808 end 809 810 fn ob o ulong v uint to void 811 for k ulong 4 812 put gob add o k uchar and shr v mul uint k 8 255 813 end 814 end 815 816 fn flush to void 817 nw = get gst 0 818 ob 0 0x07230203 819 if isvk 820 ob 4 0x00010300 821 end 822 if not isvk 823 ob 4 0x00010000 824 end 825 ob 8 0 826 ob 12 uint get gst 1 827 ob 16 0 828 for i ulong nw 829 ob add 20 mul i 4 get gw i 830 end 831 tot = add 20 mul nw 4 832 r = write 1 gob 0 tot 833 if ne r long tot 834 n = write 2 tio 0 len tio 835 exit 3 836 end 837 end 838 839 fn main to int 840 ac = argcnt 841 if or lt ac 3 gt ac 4 842 usage 843 end 844 put gst 3 9 845 if argis 1 tvadd 846 put gst 3 0 847 end 848 if argis 1 tfsum 849 put gst 3 1 850 end 851 if argis 1 tvmul 852 put gst 3 2 853 end 854 if argis 1 tvsub 855 put gst 3 3 856 end 857 if argis 1 tvmax 858 put gst 3 4 859 end 860 if argis 1 trms 861 put gst 3 5 862 end 863 if argis 1 tgemm 864 put gst 3 6 865 end 866 put gst 2 9 867 if argis 2 tcl 868 put gst 2 0 869 end 870 if argis 2 tvk 871 put gst 2 1 872 end 873 if or eq get gst 3 9 eq get gst 2 9 874 usage 875 end 876 if eq ac 4 877 if not argis 3 tmut 878 usage 879 end 880 put gst 4 1 881 end 882 put gst 1 ulong IDYN 883 header 884 decor 885 types 886 consts 887 vars 888 body 889 flush 890 ret 0 891 end