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
omega-mv / seed / kflow / kpack.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/kflow/kpack.om: kflow's package stage. Wraps raw SASS text in a loadable CUDA cubin (ELF64, |
| 3 | note EM_CUDA 190), in seeded Omega (program mode, Linux AArch64). The layout is a port of CUDA++'s |
| 4 | note omega_cubin_pack (aien-dev/CUDApp src/omega_cubin.c at 853e814d): the same ten sections, the same |
| 5 | note .nv.info attributes, the same five program headers, made per architecture: |
| 6 | note sm_121: e_flags 0x6007902, cuinfo note arch 121, with CUDA++'s 36-byte .nv.compat |
| 7 | note sm_80: e_flags 0x6005004, cuinfo note arch 80, no .nv.compat (ptxas 13.0 writes none for sm_80) |
| 8 | note |
| 9 | note Run: kpack ARCH NAME BIN OUT REGS SHARED P... |
| 10 | note ARCH 80 or 121; NAME the kernel symbol ([A-Za-z_][A-Za-z0-9_]*, at most 63 bytes); BIN raw SASS |
| 11 | note (a nonzero multiple of 16 bytes, at most 64 KiB); OUT the cubin to write; REGS registers per thread |
| 12 | note (at most 255); SHARED reserved shared bytes (multiple of 4); then one word per parameter in order: |
| 13 | note Select ARCH 121-tk for a driver-loadable CUDA 13 image with linked toolkit metadata. |
| 14 | note p8 a pointer, or 1 2 4 8 a value of that many bytes. Parameters are laid out naturally aligned from |
| 15 | note constant bank offset 0x380 (CUDA's ABI), and the constant bank ends at the end of the last one. |
| 16 | note Exit 0 written, 1 refused arguments or input, 2 write failed. The packer does not decode SASS: the |
| 17 | note register count is the caller's claim, and EIATTR_EXIT_INSTR_OFFSETS is left out as in CUDA++. |
| 18 | om2 |
| 19 | text msg "kpack: usage: kpack ARCH NAME BIN OUT REGS SHARED P...\nkpack: bad argument\nkpack: bin unreadable, empty, too large or not a multiple of 16 bytes\nkpack: cannot write output\nkpack: wrote bytes=\n sections=\n cbank=\n" |
| 20 | text sn ".shstrtab|.strtab|.symtab|.text.|.nv.constant0.|.nv.info|.nv.compat|.nv.info.|.nv.shared.reserved.0|.note.nv.cuinfo|.note.nv.tkinfo|" |
| 21 | text tkname "121-tk" |
| 22 | text producer "Omega kpack" |
| 23 | text target "sm_121a" |
| 24 | global ob uchar 196608 |
| 25 | global tx uchar 65536 |
| 26 | global ab uchar 256 |
| 27 | global fnb uchar 64 |
| 28 | global dig uchar 32 |
| 29 | global po ulong 32 |
| 30 | global ps ulong 32 |
| 31 | global pp ulong 32 |
| 32 | global so ulong 12 |
| 33 | global sz ulong 12 |
| 34 | global nm ulong 12 |
| 35 | global sv ulong 16 |
| 36 | note sv: 0 arch, 1 cursor, 2 name length, 3 parameter count, 4 text bytes, 5 cbank size, 6 regs, |
| 37 | note 7 shared bytes, 8 section count (with the null one) |
| 38 | fn say id ulong to void |
| 39 | k = ulong 1 |
| 40 | s = ulong 0 |
| 41 | for i ulong len msg |
| 42 | if eq get msg i 10 |
| 43 | if eq k id |
| 44 | e = i |
| 45 | if lt id 5 |
| 46 | e = add i 1 |
| 47 | end |
| 48 | r = write 1 msg s sub e s |
| 49 | ret |
| 50 | end |
| 51 | k = add k 1 |
| 52 | s = add i 1 |
| 53 | end |
| 54 | end |
| 55 | exit 99 |
| 56 | end |
| 57 | fn num v ulong to void |
| 58 | x = v |
| 59 | n = ulong 0 |
| 60 | while ge x 10 bound 64 |
| 61 | put dig n uchar add 48 rem x 10 |
| 62 | x = div x 10 |
| 63 | n = add n 1 |
| 64 | end |
| 65 | put dig n uchar add 48 x |
| 66 | n = add n 1 |
| 67 | for i ulong n |
| 68 | r = write 1 dig sub sub n i 1 1 |
| 69 | end |
| 70 | end |
| 71 | note ---- little-endian stores at an absolute offset, and at the cursor ---- |
| 72 | fn b1 o ulong v ulong to void |
| 73 | put ob o uchar and v 255 |
| 74 | end |
| 75 | fn b2 o ulong v ulong to void |
| 76 | b1 o v |
| 77 | b1 add o 1 shr v 8 |
| 78 | end |
| 79 | fn b4 o ulong v ulong to void |
| 80 | b2 o v |
| 81 | b2 add o 2 shr v 16 |
| 82 | end |
| 83 | fn b8 o ulong v ulong to void |
| 84 | b4 o v |
| 85 | b4 add o 4 shr v 32 |
| 86 | end |
| 87 | fn cur to ulong |
| 88 | ret get sv 1 |
| 89 | end |
| 90 | fn adv n ulong to void |
| 91 | put sv 1 add get sv 1 n |
| 92 | end |
| 93 | fn e1 v ulong to void |
| 94 | b1 cur v |
| 95 | adv 1 |
| 96 | end |
| 97 | fn e4 v ulong to void |
| 98 | b4 cur v |
| 99 | adv 4 |
| 100 | end |
| 101 | fn alignc a ulong to void |
| 102 | while ne rem cur a 0 bound 256 |
| 103 | adv 1 |
| 104 | end |
| 105 | end |
| 106 | note ---- .nv.info attributes: format 4 (id, length, payload), 3 (id, u16), 2 (id, 0) ---- |
| 107 | fn at4 id ulong n ulong to void |
| 108 | e1 4 |
| 109 | e1 id |
| 110 | e1 and n 255 |
| 111 | e1 shr n 8 |
| 112 | end |
| 113 | fn at3 id ulong v ulong to void |
| 114 | e1 3 |
| 115 | e1 id |
| 116 | e1 and v 255 |
| 117 | e1 shr v 8 |
| 118 | end |
| 119 | fn ename to void |
| 120 | for i ulong get sv 2 |
| 121 | e1 ulong get fnb i |
| 122 | end |
| 123 | end |
| 124 | note section-name number k (0..9) at the cursor: the prefix, the kernel name for 3 4 7, then 0 |
| 125 | fn sname k ulong to void |
| 126 | j = ulong 0 |
| 127 | for i ulong len sn |
| 128 | c = get sn i |
| 129 | if eq c 124 |
| 130 | if eq j k |
| 131 | if or or eq k 3 eq k 4 eq k 7 |
| 132 | ename |
| 133 | end |
| 134 | e1 0 |
| 135 | ret |
| 136 | end |
| 137 | j = add j 1 |
| 138 | else |
| 139 | if eq j k |
| 140 | e1 ulong c |
| 141 | end |
| 142 | end |
| 143 | end |
| 144 | end |
| 145 | fn hascompat to bool |
| 146 | ret eq get sv 0 121 |
| 147 | end |
| 148 | fn shstr to void |
| 149 | e1 0 |
| 150 | for k ulong add 10 get sv 9 |
| 151 | if or ne k 6 hascompat |
| 152 | put nm k sub cur get so 0 |
| 153 | sname k |
| 154 | end |
| 155 | end |
| 156 | end |
| 157 | fn symtab to void |
| 158 | b = cur |
| 159 | for i ulong 96 |
| 160 | e1 0 |
| 161 | end |
| 162 | b1 add b 28 3 |
| 163 | b2 add b 30 4 |
| 164 | b4 add b 48 1 |
| 165 | b1 add b 52 0x12 |
| 166 | b1 add b 53 0x10 |
| 167 | b2 add b 54 4 |
| 168 | b8 add b 64 get sv 4 |
| 169 | b1 add b 76 3 |
| 170 | b2 add b 78 5 |
| 171 | end |
| 172 | fn ginfo to void |
| 173 | at4 0x2f 8 |
| 174 | e4 2 |
| 175 | e4 get sv 6 |
| 176 | at4 0x11 8 |
| 177 | e4 2 |
| 178 | e4 0 |
| 179 | at4 0x12 8 |
| 180 | e4 2 |
| 181 | e4 0 |
| 182 | end |
| 183 | fn compat to void |
| 184 | e4 0x00000902 |
| 185 | e4 0x00020202 |
| 186 | e4 0x00050502 |
| 187 | e4 0x01010703 |
| 188 | e4 0x00000302 |
| 189 | e4 0x00010602 |
| 190 | e4 0x00080b04 |
| 191 | e4 0 |
| 192 | e4 0 |
| 193 | end |
| 194 | fn finfo to void |
| 195 | at4 0x37 4 |
| 196 | e4 130 |
| 197 | np = get sv 3 |
| 198 | for i ulong np |
| 199 | at4 0x17 12 |
| 200 | e4 0 |
| 201 | e4 or i shl sub get po i 0x380 16 |
| 202 | t = ulong 0x0011f000 |
| 203 | if ne get sv 9 0 |
| 204 | t = or 0x0001f000 shl get ps i 18 |
| 205 | end |
| 206 | if ne get pp i 0 |
| 207 | t = ulong 0x0021f500 |
| 208 | end |
| 209 | e4 t |
| 210 | end |
| 211 | e = sub get sv 5 0x380 |
| 212 | at3 0x19 e |
| 213 | at4 0x0a 8 |
| 214 | e4 3 |
| 215 | e4 or 0x380 shl e 16 |
| 216 | at3 0x50 0 |
| 217 | at3 0x1b get sv 6 |
| 218 | at3 0x5f 0x0101 |
| 219 | e1 2 |
| 220 | e1 0x4a |
| 221 | e1 0 |
| 222 | e1 0 |
| 223 | at4 0x36 4 |
| 224 | e4 0 |
| 225 | end |
| 226 | note one section body, by section-name number k |
| 227 | fn body k ulong to void |
| 228 | if eq k 0 |
| 229 | shstr |
| 230 | end |
| 231 | if eq k 1 |
| 232 | e1 0 |
| 233 | ename |
| 234 | e1 0 |
| 235 | end |
| 236 | if eq k 2 |
| 237 | symtab |
| 238 | end |
| 239 | if eq k 3 |
| 240 | for j ulong get sv 4 |
| 241 | e1 ulong get tx j |
| 242 | end |
| 243 | end |
| 244 | if eq k 4 |
| 245 | for j ulong get sv 5 |
| 246 | e1 0 |
| 247 | end |
| 248 | end |
| 249 | if eq k 5 |
| 250 | ginfo |
| 251 | end |
| 252 | if eq k 6 |
| 253 | compat |
| 254 | end |
| 255 | if eq k 7 |
| 256 | finfo |
| 257 | end |
| 258 | if eq k 8 |
| 259 | adv get sv 7 |
| 260 | end |
| 261 | if eq k 9 |
| 262 | noteb |
| 263 | end |
| 264 | if eq k 10 |
| 265 | toolnote |
| 266 | end |
| 267 | end |
| 268 | fn toolnote to void |
| 269 | note ELF toolkit descriptor v2 with producer, ABI release, build and target string offsets. |
| 270 | e4 12 |
| 271 | e4 64 |
| 272 | e4 2000 |
| 273 | e4 0x4449564e |
| 274 | e4 0x43204149 |
| 275 | e4 0x0070726f |
| 276 | e4 2 |
| 277 | e4 0 |
| 278 | e4 1 |
| 279 | e4 13 |
| 280 | e4 18 |
| 281 | e4 25 |
| 282 | e1 0 |
| 283 | for i ulong len producer |
| 284 | e1 ulong get producer i |
| 285 | end |
| 286 | e1 0 |
| 287 | e4 0x302e3331 |
| 288 | e1 0 |
| 289 | e4 0x64656573 |
| 290 | e1 101 |
| 291 | e1 100 |
| 292 | e1 0 |
| 293 | for i ulong len target |
| 294 | e1 ulong get target i |
| 295 | end |
| 296 | e1 0 |
| 297 | for i ulong 7 |
| 298 | e1 0 |
| 299 | end |
| 300 | end |
| 301 | fn noteb to void |
| 302 | e4 12 |
| 303 | e4 8 |
| 304 | e4 0x3e8 |
| 305 | e4 0x4449564e |
| 306 | e4 0x43204149 |
| 307 | e4 0x0070726f |
| 308 | e1 2 |
| 309 | e1 0 |
| 310 | e1 and get sv 0 255 |
| 311 | e1 0 |
| 312 | e4 130 |
| 313 | end |
| 314 | note section attributes for name number k: type, flags, link, info, align, entsize |
| 315 | fn sty k ulong to ulong |
| 316 | if eq k 2 |
| 317 | ret 2 |
| 318 | end |
| 319 | if or eq k 3 eq k 4 |
| 320 | ret 1 |
| 321 | end |
| 322 | if or eq k 5 eq k 7 |
| 323 | ret 0x70000000 |
| 324 | end |
| 325 | if eq k 6 |
| 326 | ret 0x70000086 |
| 327 | end |
| 328 | if eq k 8 |
| 329 | ret 8 |
| 330 | end |
| 331 | if ge k 9 |
| 332 | ret 7 |
| 333 | end |
| 334 | ret 3 |
| 335 | end |
| 336 | fn sfl k ulong to ulong |
| 337 | if eq k 3 |
| 338 | ret 6 |
| 339 | end |
| 340 | if eq k 4 |
| 341 | ret 0x42 |
| 342 | end |
| 343 | if eq k 7 |
| 344 | ret 0x40 |
| 345 | end |
| 346 | if eq k 8 |
| 347 | ret 3 |
| 348 | end |
| 349 | if eq k 10 |
| 350 | ret 0x1000000 |
| 351 | end |
| 352 | if eq k 9 |
| 353 | ret 0x1000040 |
| 354 | end |
| 355 | ret 0 |
| 356 | end |
| 357 | fn sal k ulong to ulong |
| 358 | if eq k 2 |
| 359 | ret 8 |
| 360 | end |
| 361 | if eq k 3 |
| 362 | ret 128 |
| 363 | end |
| 364 | if eq k 4 |
| 365 | ret 16 |
| 366 | end |
| 367 | if or or eq k 5 eq k 6 or eq k 7 ge k 9 |
| 368 | ret 4 |
| 369 | end |
| 370 | ret 1 |
| 371 | end |
| 372 | fn slk k ulong to ulong |
| 373 | if and eq k 9 ne get sv 9 0 |
| 374 | ret 11 |
| 375 | end |
| 376 | if eq k 2 |
| 377 | ret 2 |
| 378 | end |
| 379 | if or or eq k 3 eq k 4 or eq k 5 eq k 7 |
| 380 | ret 3 |
| 381 | end |
| 382 | ret 0 |
| 383 | end |
| 384 | fn sin k ulong to ulong |
| 385 | if eq k 2 |
| 386 | ret 2 |
| 387 | end |
| 388 | if eq k 3 |
| 389 | ret 2 |
| 390 | end |
| 391 | if or eq k 4 eq k 7 |
| 392 | ret 4 |
| 393 | end |
| 394 | if eq k 9 |
| 395 | ret 2 |
| 396 | end |
| 397 | ret 0 |
| 398 | end |
| 399 | fn shdr p ulong k ulong to void |
| 400 | b4 p get nm k |
| 401 | b4 add p 4 sty k |
| 402 | b8 add p 8 sfl k |
| 403 | b8 add p 24 get so k |
| 404 | b8 add p 32 get sz k |
| 405 | b4 add p 40 slk k |
| 406 | b4 add p 44 sin k |
| 407 | b8 add p 48 sal k |
| 408 | if eq k 2 |
| 409 | b8 add p 56 24 |
| 410 | end |
| 411 | end |
| 412 | fn phdr p ulong t ulong f ulong o ulong fs ulong ms ulong to void |
| 413 | b4 p t |
| 414 | b4 add p 4 f |
| 415 | b8 add p 8 o |
| 416 | b8 add p 32 fs |
| 417 | b8 add p 40 ms |
| 418 | b8 add p 48 8 |
| 419 | end |
| 420 | note ---- arguments ---- |
| 421 | fn argdec i ulong to ulong |
| 422 | r = argread i ab 0 20 |
| 423 | if lt r 1 |
| 424 | ret 0xffffffffffffffff |
| 425 | end |
| 426 | v = ulong 0 |
| 427 | for k ulong ulong r |
| 428 | c = get ab k |
| 429 | if or lt c 48 gt c 57 |
| 430 | ret 0xffffffffffffffff |
| 431 | end |
| 432 | v = add mul v 10 ulong sub c 48 |
| 433 | end |
| 434 | ret v |
| 435 | end |
| 436 | fn namearg to bool |
| 437 | r = argread 2 fnb 0 64 |
| 438 | if or lt r 1 gt r 63 |
| 439 | ret false |
| 440 | end |
| 441 | n = ulong r |
| 442 | for k ulong n |
| 443 | c = get fnb k |
| 444 | d = and ge c 48 le c 57 |
| 445 | a = or and ge c 65 le c 90 and ge c 97 le c 122 |
| 446 | if not or or a eq c 95 and d gt k 0 |
| 447 | ret false |
| 448 | end |
| 449 | end |
| 450 | put sv 2 n |
| 451 | ret true |
| 452 | end |
| 453 | fn params to bool |
| 454 | np = sub ulong argcnt 7 |
| 455 | if gt np 32 |
| 456 | ret false |
| 457 | end |
| 458 | e = ulong 0x380 |
| 459 | for i ulong np |
| 460 | r = argread add i 7 ab 0 4 |
| 461 | p = ulong 0 |
| 462 | s = ulong 0 |
| 463 | if and eq r 2 eq get ab 0 112 |
| 464 | if eq get ab 1 56 |
| 465 | p = ulong 1 |
| 466 | s = ulong 8 |
| 467 | end |
| 468 | end |
| 469 | if eq r 1 |
| 470 | s = ulong sub get ab 0 48 |
| 471 | end |
| 472 | if not or or eq s 1 eq s 2 or eq s 4 eq s 8 |
| 473 | ret false |
| 474 | end |
| 475 | while ne rem e s 0 bound 8 |
| 476 | e = add e 1 |
| 477 | end |
| 478 | put po i e |
| 479 | put ps i s |
| 480 | put pp i p |
| 481 | e = add e s |
| 482 | end |
| 483 | put sv 3 np |
| 484 | put sv 5 e |
| 485 | ret true |
| 486 | end |
| 487 | fn loadbin to bool |
| 488 | r = argread 3 ab 0 255 |
| 489 | if lt r 1 |
| 490 | ret false |
| 491 | end |
| 492 | fd = fopen ab 0 ulong r 524288 0 |
| 493 | if lt fd 0 |
| 494 | ret false |
| 495 | end |
| 496 | n = ulong 0 |
| 497 | for k ulong 4097 |
| 498 | x = read int fd tx n sub len tx n |
| 499 | if le x 0 |
| 500 | break |
| 501 | end |
| 502 | n = add n ulong x |
| 503 | if eq n len tx |
| 504 | break |
| 505 | end |
| 506 | end |
| 507 | y = read int fd ab 0 1 |
| 508 | c = fclose int fd |
| 509 | put sv 4 n |
| 510 | ret and and gt n 0 eq rem n 16 0 eq y 0 |
| 511 | end |
| 512 | fn place to void |
| 513 | put sv 1 64 |
| 514 | i = ulong 1 |
| 515 | for k ulong add 10 get sv 9 |
| 516 | if or ne k 6 hascompat |
| 517 | alignc sal k |
| 518 | put so k cur |
| 519 | body k |
| 520 | put sz k sub cur get so k |
| 521 | i = add i 1 |
| 522 | end |
| 523 | end |
| 524 | put sv 8 i |
| 525 | end |
| 526 | fn headers to ulong |
| 527 | alignc 8 |
| 528 | sh = cur |
| 529 | n = get sv 8 |
| 530 | i = ulong 1 |
| 531 | for k ulong add 10 get sv 9 |
| 532 | if or ne k 6 hascompat |
| 533 | shdr add sh mul i 64 k |
| 534 | i = add i 1 |
| 535 | end |
| 536 | end |
| 537 | ph = add sh mul n 64 |
| 538 | phdr ph 6 4 ph 280 280 |
| 539 | phdr add ph 56 1 4 ph 280 280 |
| 540 | phdr add ph 112 1 5 get so 3 get sz 3 get sz 3 |
| 541 | phdr add ph 168 1 6 get so 8 0 get sz 8 |
| 542 | phdr add ph 224 1 4 get so 4 get sz 4 get sz 4 |
| 543 | b4 0 0x464c457f |
| 544 | b1 4 2 |
| 545 | b1 5 1 |
| 546 | b1 6 1 |
| 547 | b1 7 0x41 |
| 548 | b1 8 8 |
| 549 | b2 16 2 |
| 550 | b2 18 190 |
| 551 | b4 20 1 |
| 552 | b8 32 ph |
| 553 | b8 40 sh |
| 554 | f = ulong 0x6005004 |
| 555 | if hascompat |
| 556 | f = ulong 0x6007902 |
| 557 | end |
| 558 | b4 48 f |
| 559 | b2 52 64 |
| 560 | b2 54 56 |
| 561 | b2 56 5 |
| 562 | b2 58 64 |
| 563 | b2 60 n |
| 564 | b2 62 1 |
| 565 | ret add ph 280 |
| 566 | end |
| 567 | fn architecture to ulong |
| 568 | a = argdec 1 |
| 569 | r = argread 1 ab 0 20 |
| 570 | if eq r long len tkname |
| 571 | match = true |
| 572 | for j ulong len tkname |
| 573 | if ne get ab j get tkname j |
| 574 | match = false |
| 575 | end |
| 576 | end |
| 577 | if match |
| 578 | a = ulong 121 |
| 579 | put sv 9 1 |
| 580 | end |
| 581 | end |
| 582 | ret a |
| 583 | end |
| 584 | fn main to int |
| 585 | if lt argcnt 7 |
| 586 | say 1 |
| 587 | ret 1 |
| 588 | end |
| 589 | a = architecture |
| 590 | g = argdec 5 |
| 591 | h = argdec 6 |
| 592 | if or or and ne a 80 ne a 121 gt g 255 or gt h 0xfffff ne rem h 4 0 |
| 593 | say 2 |
| 594 | ret 1 |
| 595 | end |
| 596 | if not namearg |
| 597 | say 2 |
| 598 | ret 1 |
| 599 | end |
| 600 | if not params |
| 601 | say 2 |
| 602 | ret 1 |
| 603 | end |
| 604 | if not loadbin |
| 605 | say 3 |
| 606 | ret 1 |
| 607 | end |
| 608 | put sv 0 a |
| 609 | put sv 6 g |
| 610 | put sv 7 h |
| 611 | place |
| 612 | t = headers |
| 613 | r = argread 4 ab 0 255 |
| 614 | if lt r 1 |
| 615 | say 4 |
| 616 | ret 2 |
| 617 | end |
| 618 | fd = fopen ab 0 ulong r 524865 420 |
| 619 | if lt fd 0 |
| 620 | say 4 |
| 621 | ret 2 |
| 622 | end |
| 623 | w = write int fd ob 0 t |
| 624 | c = fclose int fd |
| 625 | if or ne w long t ne c 0 |
| 626 | say 4 |
| 627 | ret 2 |
| 628 | end |
| 629 | say 5 |
| 630 | num t |
| 631 | say 6 |
| 632 | num get sv 8 |
| 633 | say 7 |
| 634 | num get sv 5 |
| 635 | put dig 0 10 |
| 636 | r = write 1 dig 0 1 |
| 637 | ret 0 |
| 638 | end |