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/run.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 / run.om` 796 lines ยท 12529 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 2 note run.om: pure seeded-Omega SPIR-V compute binary executor (replaces Vulkan/lavapipe). 3 note Decodes and executes SPIR-V 1.3 Vulkan Shader (GLCompute) and SPIR-V 1.0 OpenCL Kernel 4 note binary modules directly in seeded Omega with exact IEEE binary32 arithmetic (0 ulp, no FMA). 5 note 6 note Usage: 7 note run MODULE.spv N_WORK N_OUT OUT.bin IN0.bin [IN1.bin] 8 note MODULE.spv SPIR-V binary file (little-endian 32-bit words, magic 0x07230203) 9 note N_WORK number of work-items (gid = 0 .. N_WORK - 1) 10 note N_OUT number of f32 elements in OUT.bin 11 note OUT.bin output binary file (written as N_OUT little-endian binary32 values) 12 note IN0.bin first input binary file (little-endian binary32 values) 13 note IN1.bin optional second input binary file 14 note 15 note Exit: 0 ok; 2 usage; 3 I/O error; 4 invalid or unsupported SPIR-V module. Traps exit 100 + code. 16 om2 17 18 text tuse "usage: run MODULE.spv N_WORK N_OUT OUT.bin IN0.bin [IN1.bin]\n" 19 text tio "run: I/O error or size limit exceeded\n" 20 text tbad "run: invalid or unsupported SPIR-V module\n" 21 text tocls "OpenCL.std" 22 text tglsl "GLSL.std.450" 23 24 const MAXBUF ulong `1048576` 25 const MAXIDS ulong 1024 26 const MAXINST ulong 1024 27 const POISON uint `4290838510` 28 29 global gspv uchar 65536 30 global gw uint 16384 31 global gio uchar `4194304` 32 global gbuf0 uint `1048576` 33 global gbuf1 uint `1048576` 34 global gbuf2 uint `1048576` 35 global gblen ulong 4 36 global gval ulong 1024 37 global gbind uint 1024 38 global glabel uint 1024 39 global giop uint 1024 40 global gia1 uint 1024 41 global gia2 uint 1024 42 global gia3 uint 1024 43 global gia4 uint 1024 44 global gpath uchar 4096 45 global garg uchar 64 46 note gst: 47 note 0 nwords, 1 id_bound, 2 exec_model (5 vk, 6 cl), 3 entry_fn, 48 note 4 gid_var, 5 ext_id, 6 ext_kind (1 glsl, 2 ocl), 7 nin_files (1 or 2), 49 note 8 n_work, 9 n_out, 10 n_inst, 11 n_ptr_params, 12 n_param_id, 13 in_entry, 50 note 14 out_slot (1 if 1 input, 2 if 2 inputs), 15 ptr_f32_tid 51 global gst ulong 16 52 53 fn fail code int msg view uchar * to void 54 n = write 2 msg 0 len msg 55 exit code 56 end 57 58 fn parseu ai ulong to ulong 59 n = argread ai garg 0 len garg 60 if or le n 0 gt n 20 61 fail 2 tuse 62 end 63 v = ulong 0 64 for i ulong ulong n 65 c = get garg i 66 if or lt c 48 gt c 57 67 fail 2 tuse 68 end 69 v = add mul v 10 ulong sub c 48 70 end 71 ret v 72 end 73 74 fn spvu32 i ulong to uint 75 o = mul i 4 76 v = uint 0 77 for k ulong 4 78 v = or v shl uint get gspv add o k mul uint k 8 79 end 80 ret v 81 end 82 83 fn giou32 i ulong to uint 84 o = mul i 4 85 v = uint 0 86 for k ulong 4 87 v = or v shl uint get gio add o k mul uint k 8 88 end 89 ret v 90 end 91 92 fn putgio32 i ulong v uint to void 93 o = mul i 4 94 for k ulong 4 95 put gio add o k uchar and shr v mul uint k 8 255 96 end 97 end 98 99 fn readspv ai ulong to void 100 n = argread ai gpath 0 len gpath 101 if le n 0 102 fail 2 tuse 103 end 104 fd = fopen gpath 0 ulong n 0 0 105 if lt fd 0 106 fail 3 tio 107 end 108 sz = fseek int fd 0 2 109 if or lt sz 20 gt sz 65536 110 c = fclose int fd 111 fail 4 tbad 112 end 113 if ne rem ulong sz 4 0 114 c = fclose int fd 115 fail 4 tbad 116 end 117 r = pread int fd gspv 0 ulong sz 0 118 c = fclose int fd 119 if ne r sz 120 fail 3 tio 121 end 122 nw = div ulong sz 4 123 put gst 0 nw 124 for i ulong nw 125 put gw i spvu32 i 126 end 127 end 128 129 fn loadbuf ai ulong slot ulong to void 130 n = argread ai gpath 0 len gpath 131 if le n 0 132 fail 2 tuse 133 end 134 fd = fopen gpath 0 ulong n 0 0 135 if lt fd 0 136 fail 3 tio 137 end 138 sz = fseek int fd 0 2 139 if or lt sz 0 gt sz `4194304` 140 c = fclose int fd 141 fail 3 tio 142 end 143 if ne rem ulong sz 4 0 144 c = fclose int fd 145 fail 3 tio 146 end 147 r = pread int fd gio 0 ulong sz 0 148 c = fclose int fd 149 if ne r sz 150 fail 3 tio 151 end 152 nelem = div ulong sz 4 153 put gblen slot nelem 154 pos = ulong 0 155 while lt pos nelem bound 260 156 step = sub nelem pos 157 if gt step 4096 158 step = 4096 159 end 160 for l ulong step 161 idx = add pos l 162 v = giou32 idx 163 if eq slot 0 164 put gbuf0 idx v 165 end 166 if eq slot 1 167 put gbuf1 idx v 168 end 169 end 170 pos = add pos step 171 end 172 end 173 174 fn saveout ai ulong nelem ulong to void 175 n = argread ai gpath 0 len gpath 176 if le n 0 177 fail 2 tuse 178 end 179 pos = ulong 0 180 while lt pos nelem bound 260 181 step = sub nelem pos 182 if gt step 4096 183 step = 4096 184 end 185 for l ulong step 186 idx = add pos l 187 putgio32 idx get gbuf2 idx 188 end 189 pos = add pos step 190 end 191 nb = mul nelem 4 192 fd = fopen gpath 0 ulong n 577 420 193 if lt fd 0 194 fail 3 tio 195 end 196 r = write int fd gio 0 nb 197 c = fclose int fd 198 if ne r long nb 199 fail 3 tio 200 end 201 end 202 203 fn initout nelem ulong to void 204 put gblen get gst 14 nelem 205 pos = ulong 0 206 while lt pos nelem bound 260 207 step = sub nelem pos 208 if gt step 4096 209 step = 4096 210 end 211 for l ulong step 212 put gbuf2 add pos l POISON 213 end 214 pos = add pos step 215 end 216 end 217 218 fn wbyte woff ulong i ulong to uchar 219 w = get gw add woff div i 4 220 sh = mul uint rem i 4 8 221 ret uchar and shr w sh 255 222 end 223 224 fn stris woff ulong wc ulong t view uchar * to bool 225 for i ulong len t 226 if ge div i 4 wc 227 ret false 228 end 229 if ne wbyte woff i get t i 230 ret false 231 end 232 end 233 if ge div len t 4 wc 234 ret false 235 end 236 ret eq wbyte woff len t 0 237 end 238 239 fn chk_id id uint to void 240 if ge ulong id MAXIDS 241 fail 4 tbad 242 end 243 end 244 245 fn addinst op uint a1 uint a2 uint a3 uint a4 uint to void 246 k = get gst 10 247 if ge k MAXINST 248 fail 4 tbad 249 end 250 put giop k op 251 put gia1 k a1 252 put gia2 k a2 253 put gia3 k a3 254 put gia4 k a4 255 put gst 10 add k 1 256 end 257 258 fn parse_meta op uint p ulong wc ulong to void 259 if eq op 11 260 if lt wc 3 261 fail 4 tbad 262 end 263 eid = get gw add p 1 264 chk_id eid 265 if stris add p 2 sub wc 2 tglsl 266 put gst 5 ulong eid 267 put gst 6 1 268 end 269 if stris add p 2 sub wc 2 tocls 270 put gst 5 ulong eid 271 put gst 6 2 272 end 273 ret 274 end 275 if eq op 15 276 if lt wc 4 277 fail 4 tbad 278 end 279 put gst 2 ulong get gw add p 1 280 put gst 3 ulong get gw add p 2 281 ret 282 end 283 if eq op 71 284 if lt wc 3 285 fail 4 tbad 286 end 287 tid = get gw add p 1 288 dec = get gw add p 2 289 chk_id tid 290 if and eq dec 11 ge wc 4 291 if eq get gw add p 3 28 292 put gst 4 ulong tid 293 end 294 end 295 if and eq dec 33 ge wc 4 296 slot = get gw add p 3 297 if gt slot 2 298 fail 4 tbad 299 end 300 put gbind ulong tid add slot 1 301 end 302 ret 303 end 304 if eq op 32 305 if lt wc 4 306 fail 4 tbad 307 end 308 tid = get gw add p 1 309 sc = get gw add p 2 310 if eq sc 5 311 put gst 15 ulong tid 312 end 313 ret 314 end 315 if eq op 43 316 if lt wc 4 317 fail 4 tbad 318 end 319 rid = get gw add p 2 320 chk_id rid 321 v = ulong get gw add p 3 322 if ge wc 5 323 v = or v shl ulong get gw add p 4 32 324 end 325 put gval ulong rid v 326 ret 327 end 328 end 329 330 fn parse_body op uint p ulong wc ulong to void 331 if eq op 55 332 if lt wc 3 333 fail 4 tbad 334 end 335 ty = get gw add p 1 336 pid = get gw add p 2 337 chk_id pid 338 if eq ulong ty get gst 15 339 ps = get gst 11 340 if gt ps 2 341 fail 4 tbad 342 end 343 put gbind ulong pid uint add ps 1 344 put gst 11 add ps 1 345 ret 346 end 347 put gst 12 ulong pid 348 put gval ulong pid get gst 8 349 ret 350 end 351 if eq op 248 352 if lt wc 2 353 fail 4 tbad 354 end 355 lid = get gw add p 1 356 chk_id lid 357 put glabel ulong lid uint add get gst 10 1 358 ret 359 end 360 if eq op 247 361 ret 362 end 363 if eq op 249 364 if lt wc 2 365 fail 4 tbad 366 end 367 addinst op get gw add p 1 0 0 0 368 ret 369 end 370 if eq op 250 371 if lt wc 4 372 fail 4 tbad 373 end 374 addinst op get gw add p 1 get gw add p 2 get gw add p 3 0 375 ret 376 end 377 if eq op 253 378 addinst op 0 0 0 0 379 ret 380 end 381 if eq op 61 382 if lt wc 4 383 fail 4 tbad 384 end 385 rid = get gw add p 2 386 chk_id rid 387 addinst op rid get gw add p 3 0 0 388 ret 389 end 390 if eq op 62 391 if lt wc 3 392 fail 4 tbad 393 end 394 addinst op get gw add p 1 get gw add p 2 0 0 395 ret 396 end 397 if eq op 81 398 if lt wc 5 399 fail 4 tbad 400 end 401 rid = get gw add p 2 402 chk_id rid 403 addinst op rid get gw add p 3 get gw add p 4 0 404 ret 405 end 406 if eq op 68 407 if lt wc 5 408 fail 4 tbad 409 end 410 rid = get gw add p 2 411 chk_id rid 412 addinst op rid get gw add p 3 0 0 413 ret 414 end 415 if eq op 113 416 if lt wc 4 417 fail 4 tbad 418 end 419 rid = get gw add p 2 420 chk_id rid 421 addinst op rid get gw add p 3 0 0 422 ret 423 end 424 if eq op 65 425 if lt wc 6 426 fail 4 tbad 427 end 428 rid = get gw add p 2 429 chk_id rid 430 addinst op rid get gw add p 3 get gw add p 5 0 431 ret 432 end 433 if eq op 70 434 if lt wc 5 435 fail 4 tbad 436 end 437 rid = get gw add p 2 438 chk_id rid 439 addinst op rid get gw add p 3 get gw add p 4 0 440 ret 441 end 442 if eq op 12 443 if lt wc 6 444 fail 4 tbad 445 end 446 rid = get gw add p 2 447 chk_id rid 448 addinst op rid get gw add p 3 get gw add p 4 get gw add p 5 449 ret 450 end 451 if eq op 169 452 if lt wc 6 453 fail 4 tbad 454 end 455 rid = get gw add p 2 456 chk_id rid 457 addinst op rid get gw add p 3 get gw add p 4 get gw add p 5 458 ret 459 end 460 parse_body_bin op p wc 461 end 462 463 fn is_binop op uint to bool 464 if eq op 128 465 ret true 466 end 467 if eq op 129 468 ret true 469 end 470 if eq op 131 471 ret true 472 end 473 if eq op 132 474 ret true 475 end 476 if eq op 133 477 ret true 478 end 479 if eq op 134 480 ret true 481 end 482 if eq op 136 483 ret true 484 end 485 if eq op 137 486 ret true 487 end 488 if eq op 176 489 ret true 490 end 491 if eq op 186 492 ret true 493 end 494 ret eq op 194 495 end 496 497 fn parse_body_bin op uint p ulong wc ulong to void 498 if not is_binop op 499 fail 4 tbad 500 end 501 if lt wc 5 502 fail 4 tbad 503 end 504 rid = get gw add p 2 505 chk_id rid 506 addinst op rid get gw add p 3 get gw add p 4 0 507 end 508 509 fn decode to void 510 nw = get gst 0 511 if lt nw 5 512 fail 4 tbad 513 end 514 if ne get gw 0 0x07230203 515 fail 4 tbad 516 end 517 bnd = ulong get gw 3 518 if or eq bnd 0 gt bnd MAXIDS 519 fail 4 tbad 520 end 521 put gst 1 bnd 522 p = ulong 5 523 while lt p nw bound 16384 524 hd = get gw p 525 wc = ulong shr hd 16 526 op = and hd 65535 527 if or eq wc 0 gt add p wc nw 528 fail 4 tbad 529 end 530 if eq op 54 531 if lt wc 5 532 fail 4 tbad 533 end 534 fid = ulong get gw add p 2 535 if eq fid get gst 3 536 put gst 13 1 537 end 538 end 539 if eq op 56 540 put gst 13 0 541 end 542 if and eq get gst 13 1 and ne op 54 ne op 56 543 parse_body op p wc 544 end 545 if eq get gst 13 0 546 parse_meta op p wc 547 end 548 p = add p wc 549 end 550 if eq get gst 10 0 551 fail 4 tbad 552 end 553 end 554 555 fn bufget slot ulong idx ulong to uint 556 if ge idx get gblen slot 557 fail 4 tbad 558 end 559 if eq slot 0 560 ret get gbuf0 idx 561 end 562 if eq slot 1 563 if eq get gst 14 1 564 ret get gbuf2 idx 565 end 566 ret get gbuf1 idx 567 end 568 if eq slot 2 569 ret get gbuf2 idx 570 end 571 fail 4 tbad 572 ret 0 573 end 574 575 fn bufput slot ulong idx ulong v uint to void 576 if ge idx get gblen slot 577 fail 4 tbad 578 end 579 if eq slot get gst 14 580 put gbuf2 idx v 581 ret 582 end 583 fail 4 tbad 584 end 585 586 fn exec_alu op uint a1 ulong a2 ulong a3 ulong a4 ulong to void 587 if eq op 128 588 put gval a1 add get gval a2 get gval a3 589 ret 590 end 591 if eq op 132 592 put gval a1 mul get gval a2 get gval a3 593 ret 594 end 595 if eq op 134 596 put gval a1 div get gval a2 get gval a3 597 ret 598 end 599 if eq op 137 600 put gval a1 rem get gval a2 get gval a3 601 ret 602 end 603 if eq op 194 604 put gval a1 shr get gval a2 get gval a3 605 ret 606 end 607 if eq op 176 608 if lt get gval a2 get gval a3 609 put gval a1 1 610 ret 611 end 612 put gval a1 0 613 ret 614 end 615 if eq op 129 616 fa = bitcast f32 uint get gval a2 617 fb = bitcast f32 uint get gval a3 618 put gval a1 ulong bitcast uint add fa fb 619 ret 620 end 621 if eq op 131 622 fa = bitcast f32 uint get gval a2 623 fb = bitcast f32 uint get gval a3 624 put gval a1 ulong bitcast uint sub fa fb 625 ret 626 end 627 if eq op 133 628 fa = bitcast f32 uint get gval a2 629 fb = bitcast f32 uint get gval a3 630 put gval a1 ulong bitcast uint mul fa fb 631 ret 632 end 633 if eq op 136 634 fa = bitcast f32 uint get gval a2 635 fb = bitcast f32 uint get gval a3 636 put gval a1 ulong bitcast uint div fa fb 637 ret 638 end 639 if eq op 186 640 fa = bitcast f32 uint get gval a2 641 fb = bitcast f32 uint get gval a3 642 if gt fa fb 643 put gval a1 1 644 ret 645 end 646 put gval a1 0 647 ret 648 end 649 if eq op 169 650 if ne get gval a2 0 651 put gval a1 get gval a3 652 ret 653 end 654 put gval a1 get gval a4 655 ret 656 end 657 if eq op 12 658 if ne a2 get gst 5 659 fail 4 tbad 660 end 661 ok = false 662 if and eq get gst 6 1 eq a3 31 663 ok = true 664 end 665 if and eq get gst 6 2 eq a3 61 666 ok = true 667 end 668 if not ok 669 fail 4 tbad 670 end 671 fa = bitcast f32 uint get gval a4 672 put gval a1 ulong bitcast uint fsqrt fa 673 ret 674 end 675 fail 4 tbad 676 end 677 678 fn exec_step op uint a1 ulong a2 ulong a3 ulong a4 ulong to void 679 if eq op 61 680 pv = get gval a2 681 put gval a1 ulong bufget shr pv 32 and pv `4294967295` 682 ret 683 end 684 if eq op 62 685 pv = get gval a1 686 bufput shr pv 32 and pv `4294967295` uint get gval a2 687 ret 688 end 689 if eq op 81 690 if eq a3 0 691 put gval a1 get gval a2 692 else 693 put gval a1 0 694 end 695 ret 696 end 697 if eq op 68 698 b = get gbind a2 699 if eq b 0 700 fail 4 tbad 701 end 702 put gval a1 get gblen ulong sub b 1 703 ret 704 end 705 if eq op 113 706 put gval a1 and get gval a2 `4294967295` 707 ret 708 end 709 if or eq op 65 eq op 70 710 b = get gbind a2 711 if eq b 0 712 fail 4 tbad 713 end 714 put gval a1 or shl ulong sub b 1 32 and get gval a3 `4294967295` 715 ret 716 end 717 exec_alu op a1 a2 a3 a4 718 end 719 720 fn jump_pc lbl ulong to ulong 721 t = get glabel lbl 722 if eq t 0 723 fail 4 tbad 724 end 725 ret ulong sub t 1 726 end 727 728 fn run_one gid ulong to void 729 ninst = get gst 10 730 gvar = get gst 4 731 pc = ulong 0 732 while lt pc ninst bound 4096 733 op = get giop pc 734 a1 = ulong get gia1 pc 735 a2 = ulong get gia2 pc 736 if eq op 253 737 ret 738 end 739 if eq op 249 740 pc = jump_pc a1 741 continue 742 end 743 if eq op 250 744 if ne get gval a1 0 745 pc = jump_pc a2 746 else 747 pc = jump_pc ulong get gia3 pc 748 end 749 continue 750 end 751 if and eq op 61 eq a2 gvar 752 put gval a1 gid 753 pc = add pc 1 754 continue 755 end 756 exec_step op a1 a2 ulong get gia3 pc ulong get gia4 pc 757 pc = add pc 1 758 end 759 end 760 761 fn main to int 762 ac = argcnt 763 if and ne ac 6 ne ac 7 764 fail 2 tuse 765 end 766 nin = ulong sub ac 5 767 put gst 7 nin 768 put gst 14 nin 769 nw = parseu 2 770 no = parseu 3 771 if or gt nw MAXBUF gt no MAXBUF 772 fail 3 tio 773 end 774 put gst 8 nw 775 put gst 9 no 776 readspv 1 777 loadbuf 5 0 778 if eq nin 2 779 loadbuf 6 1 780 end 781 initout no 782 decode 783 pos = ulong 0 784 while lt pos nw bound 260 785 step = sub nw pos 786 if gt step 4096 787 step = 4096 788 end 789 for l ulong step 790 run_one add pos l 791 end 792 pos = add pos step 793 end 794 saveout 4 no 795 ret 0 796 end