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 / om / encoder / guard_pred_test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note guard_pred_test.om: gate for the guard predicate of every sm_121 op this encoder emits (DRA-599). |
| 3 | note Concatenate after om/encoder/bw_enc_sm121.om; om/encoder/guard-gate.sh compiles it with the pinned omc. |
| 4 | note Input word 7 bit 1 (predicate_p0) and bit 2 (predicate_not) ask for @P0 / @!P0; the guard is w0 bits 12..15: |
| 5 | note 7 always (PT), 0 @P0, 8 @!P0. Every op 0..63 is encoded three times with R4 <- R8, R12, R16, UR4 (imm 2 for |
| 6 | note BRA and BSSY, else 0): unpredicated, @P0 and @!P0. The @P0 / @!P0 words must carry guard 0 / 8 and equal the |
| 7 | note unpredicated words in every other bit. LDCU and LDCU.64 (ops 6, 7) are uniform-datapath instructions: their |
| 8 | note guard bits name UP0..UPT, so @P0 cannot be encoded there and both forms must be refused. |
| 9 | note Program rows: the omega-mv #1044 seed/oscgpu bool and trap shapes (gpuir.om bw_bool, bw_trap; the C twin is |
| 10 | note src/omega_osc_gpu.c Bool, Trap) go through the program entry bw_enc_sm121 with word 7 as the C packer writes |
| 11 | note it: MOV R4, 0 / @P0 MOV R4, 1 / @!P0 STG.E desc[UR4][R8.64], R12 / @!P0 MOV R5, 1 / @!P0 MOV R4, 0. Their |
| 12 | note guards must be 7, 0, 8, 8, 8. |
| 13 | note No arguments: print one line per failing form (op, form 1 @P0 / 2 @!P0, guard read; 99 refused, 98 accepted |
| 14 | note but must refuse, 97 another bit changed; program rows as op 100 + row), then the failure count; exit 0 when |
| 15 | note none, else 1. |
| 16 | note One argument: write 133 instructions (2128 bytes) on stdout for the ORACLE: the @P0 and @!P0 forms of the 62 |
| 17 | note P-guarded ops in op order, then the unpredicated LDCU and LDCU.64 words with their guard bits set to 0 and 8 |
| 18 | note (an oracle probe, not encoder output), then the five program rows. |
| 19 | note Two arguments: read the ORACLE's text on stdin; instruction k must start with @P0 / @!P0 (k < 124, even / odd), |
| 20 | note then @UP0 / @!UP0 (124..127), then no guard, @P0, @!P0, @!P0, @!P0 (128..132). Exit 0, 4 when a guard or the |
| 21 | note instruction count is wrong, 70 read error, 71 text too long. |
| 22 | note ORACLE (independent, not first-party, never shipped): NVIDIA nvdisasm 13.0.85 -b SM121 -hex. |
| 23 | note Constants: 61440 = 0xf000 (guard mask), 124 = 2 * 62, 128 = 124 + 4, 133 = 128 + 5, 2128 = 133 * 16. |
| 24 | text gptok "@P0 @!P0 @UP0 @!UP0 " |
| 25 | text gpm1 "FAIL op " |
| 26 | text gpm2 " form " |
| 27 | text gpm3 " guard " |
| 28 | text gpm4 "guard predicate failures: " |
| 29 | text gpm5 "oracle instructions: " |
| 30 | text gpm6 " guard mismatches: " |
| 31 | text gprow "row guards:" |
| 32 | text gps " " |
| 33 | text gpnl "\n" |
| 34 | global gpbuf uchar 65536 |
| 35 | const GPNONE ulong 18446744073709551615 |
| 36 | |
| 37 | note uniform-datapath ops: the guard bits name a uniform predicate |
| 38 | fn gp_uni op ulong to bool |
| 39 | ret or eq op 6 eq op 7 |
| 40 | end |
| 41 | |
| 42 | note encode op with form f (0 none, 1 @P0, 2 @!P0) into w; enc1's result (0 accepted) |
| 43 | fn gp_enc op ulong f ulong w edit ulong 4 to ulong |
| 44 | o = new ulong 16 0 |
| 45 | put o 0 op |
| 46 | put o 1 4 |
| 47 | put o 2 8 |
| 48 | put o 3 12 |
| 49 | put o 4 16 |
| 50 | put o 5 4 |
| 51 | if or eq op 46 eq op 54 |
| 52 | put o 6 2 |
| 53 | end |
| 54 | put o 12 1 |
| 55 | put o 13 1 |
| 56 | put o 14 1 |
| 57 | put o 15 1 |
| 58 | if ne f 0 |
| 59 | put o 9 1 |
| 60 | end |
| 61 | if eq f 2 |
| 62 | put o 10 1 |
| 63 | end |
| 64 | ret enc1 view o w |
| 65 | end |
| 66 | |
| 67 | fn gp_dec x ulong to void |
| 68 | b = new uchar 20 0 |
| 69 | i = ulong 20 |
| 70 | v = x |
| 71 | c = true |
| 72 | while c bound 20 |
| 73 | i = sub i 1 |
| 74 | put b i uchar add 48 rem v 10 |
| 75 | v = div v 10 |
| 76 | if eq v 0 |
| 77 | c = false |
| 78 | end |
| 79 | end |
| 80 | k = write 1 b i sub 20 i |
| 81 | end |
| 82 | |
| 83 | fn gp_fail op ulong f ulong g ulong to void |
| 84 | k = write 1 gpm1 0 len gpm1 |
| 85 | gp_dec op |
| 86 | k = write 1 gpm2 0 len gpm2 |
| 87 | gp_dec f |
| 88 | k = write 1 gpm3 0 len gpm3 |
| 89 | gp_dec g |
| 90 | k = write 1 gpnl 0 1 |
| 91 | end |
| 92 | |
| 93 | note the guard field of w0 |
| 94 | fn gp_g w view ulong 4 to ulong |
| 95 | ret and shr get w 0 12 15 |
| 96 | end |
| 97 | |
| 98 | note equal outside the guard field |
| 99 | fn gp_same u view ulong 4 p view ulong 4 to bool |
| 100 | if ne or get u 0 61440 or get p 0 61440 |
| 101 | ret false |
| 102 | end |
| 103 | for k ulong 3 |
| 104 | if ne get u add k 1 get p add k 1 |
| 105 | ret false |
| 106 | end |
| 107 | end |
| 108 | ret true |
| 109 | end |
| 110 | |
| 111 | note the failing forms of op (0..2) |
| 112 | fn gp_row op ulong to ulong |
| 113 | u = new ulong 4 0 |
| 114 | p = new ulong 4 0 |
| 115 | if ne gp_enc op 0 edit u 0 |
| 116 | gp_fail op 0 99 |
| 117 | ret 1 |
| 118 | end |
| 119 | bad = ulong 0 |
| 120 | for f ulong 2 |
| 121 | fm = add f 1 |
| 122 | r = gp_enc op fm edit p |
| 123 | if gp_uni op |
| 124 | if eq r 0 |
| 125 | gp_fail op fm 98 |
| 126 | bad = add bad 1 |
| 127 | end |
| 128 | else |
| 129 | if ne r 0 |
| 130 | gp_fail op fm 99 |
| 131 | bad = add bad 1 |
| 132 | else |
| 133 | g = gp_g view p |
| 134 | if ne g mul f 8 |
| 135 | gp_fail op fm g |
| 136 | bad = add bad 1 |
| 137 | else |
| 138 | if not gp_same view u view p |
| 139 | gp_fail op fm 97 |
| 140 | bad = add bad 1 |
| 141 | end |
| 142 | end |
| 143 | end |
| 144 | end |
| 145 | end |
| 146 | ret bad |
| 147 | end |
| 148 | |
| 149 | note program row r: op, dst, src1, src2, imm, word 7 (2 @P0, 6 @!P0), and the guard it must get |
| 150 | fn gp_pop r ulong to ulong |
| 151 | if eq r 2 |
| 152 | ret 12 |
| 153 | end |
| 154 | ret 2 |
| 155 | end |
| 156 | fn gp_pdst r ulong to ulong |
| 157 | if eq r 2 |
| 158 | ret GPNONE |
| 159 | end |
| 160 | if eq r 3 |
| 161 | ret 5 |
| 162 | end |
| 163 | ret 4 |
| 164 | end |
| 165 | fn gp_pimm r ulong to ulong |
| 166 | if or eq r 1 eq r 3 |
| 167 | ret 1 |
| 168 | end |
| 169 | ret 0 |
| 170 | end |
| 171 | fn gp_pfl r ulong to ulong |
| 172 | if eq r 0 |
| 173 | ret 0 |
| 174 | end |
| 175 | if eq r 1 |
| 176 | ret 2 |
| 177 | end |
| 178 | ret 6 |
| 179 | end |
| 180 | fn gp_pg r ulong to ulong |
| 181 | if eq r 0 |
| 182 | ret 7 |
| 183 | end |
| 184 | if eq r 1 |
| 185 | ret 0 |
| 186 | end |
| 187 | ret 8 |
| 188 | end |
| 189 | |
| 190 | note the five #1044 rows through bw_enc_sm121 into code; the byte count (80 when accepted) |
| 191 | fn gp_penc code edit uchar 16384 to ulong |
| 192 | ins = new ulong 8192 0 |
| 193 | phys = new ulong 288 0 |
| 194 | for v ulong 256 |
| 195 | put phys v v |
| 196 | end |
| 197 | for u ulong 32 |
| 198 | put phys add 256 u u |
| 199 | end |
| 200 | for r ulong 5 |
| 201 | b = mul r 8 |
| 202 | put ins b gp_pop r |
| 203 | put ins add b 1 gp_pdst r |
| 204 | put ins add b 2 GPNONE |
| 205 | put ins add b 3 GPNONE |
| 206 | put ins add b 4 GPNONE |
| 207 | put ins add b 5 GPNONE |
| 208 | put ins add b 6 gp_pimm r |
| 209 | put ins add b 7 gp_pfl r |
| 210 | if eq r 2 |
| 211 | put ins add b 2 8 |
| 212 | put ins add b 3 12 |
| 213 | put ins add b 5 4 |
| 214 | end |
| 215 | end |
| 216 | ret bw_enc_sm121 view ins view phys 5 0 code |
| 217 | end |
| 218 | |
| 219 | fn gp_prog to ulong |
| 220 | code = new uchar 16384 0 |
| 221 | if ne gp_penc edit code 80 |
| 222 | gp_fail 100 0 99 |
| 223 | ret 1 |
| 224 | end |
| 225 | k = write 1 gprow 0 len gprow |
| 226 | for r ulong 5 |
| 227 | k = write 1 gps 0 1 |
| 228 | g = and shr get code add mul r 16 1 4 15 |
| 229 | gp_dec ulong g |
| 230 | end |
| 231 | k = write 1 gpnl 0 1 |
| 232 | bad = ulong 0 |
| 233 | for r ulong 5 |
| 234 | g = and shr get code add mul r 16 1 4 15 |
| 235 | if ne ulong g gp_pg r |
| 236 | gp_fail add 100 r gp_pfl r ulong g |
| 237 | bad = add bad 1 |
| 238 | end |
| 239 | end |
| 240 | ret bad |
| 241 | end |
| 242 | |
| 243 | fn gp_rows to int |
| 244 | bad = ulong 0 |
| 245 | for op ulong 64 |
| 246 | bad = add bad gp_row op |
| 247 | end |
| 248 | bad = add bad gp_prog |
| 249 | k = write 1 gpm4 0 len gpm4 |
| 250 | gp_dec bad |
| 251 | k = write 1 gpnl 0 1 |
| 252 | if ne bad 0 |
| 253 | ret 1 |
| 254 | end |
| 255 | ret 0 |
| 256 | end |
| 257 | |
| 258 | fn gp_bin to int |
| 259 | w = new ulong 4 0 |
| 260 | code = new uchar 16384 0 |
| 261 | pc = new uchar 16384 0 |
| 262 | i = ulong 0 |
| 263 | for op ulong 64 |
| 264 | if not gp_uni op |
| 265 | for f ulong 2 |
| 266 | if ne gp_enc op add f 1 edit w 0 |
| 267 | ret int add 10 op |
| 268 | end |
| 269 | store edit code i view w |
| 270 | i = add i 1 |
| 271 | end |
| 272 | end |
| 273 | end |
| 274 | for op ulong 64 |
| 275 | if gp_uni op |
| 276 | if ne gp_enc op 0 edit w 0 |
| 277 | ret int add 10 op |
| 278 | end |
| 279 | x = get w 0 |
| 280 | for f ulong 2 |
| 281 | put w 0 or sub x and x 61440 shl mul f 8 12 |
| 282 | store edit code i view w |
| 283 | i = add i 1 |
| 284 | end |
| 285 | end |
| 286 | end |
| 287 | if ne gp_penc edit pc 80 |
| 288 | ret 9 |
| 289 | end |
| 290 | for q ulong 80 |
| 291 | put code add mul i 16 q get pc q |
| 292 | end |
| 293 | n = add mul i 16 80 |
| 294 | off = ulong 0 |
| 295 | while lt off n bound 4096 |
| 296 | k = write 1 code off sub n off |
| 297 | if le k 0 |
| 298 | ret 3 |
| 299 | end |
| 300 | off = add off ulong k |
| 301 | end |
| 302 | ret 0 |
| 303 | end |
| 304 | |
| 305 | fn gp_ts t ulong to ulong |
| 306 | if eq t 0 |
| 307 | ret 0 |
| 308 | end |
| 309 | if eq t 1 |
| 310 | ret 4 |
| 311 | end |
| 312 | if eq t 2 |
| 313 | ret 9 |
| 314 | end |
| 315 | ret 14 |
| 316 | end |
| 317 | |
| 318 | note does token t of gptok start at gpbuf[j]? Token 4 is no guard (the mnemonic is not an @ word). |
| 319 | fn gp_tok j ulong n ulong t ulong to bool |
| 320 | if eq t 4 |
| 321 | if ge j n |
| 322 | ret false |
| 323 | end |
| 324 | ret ne get gpbuf j 64 |
| 325 | end |
| 326 | s = gp_ts t |
| 327 | e = gp_ts add t 1 |
| 328 | if eq t 3 |
| 329 | e = 20 |
| 330 | end |
| 331 | if gt add j sub e s n |
| 332 | ret false |
| 333 | end |
| 334 | for q ulong sub e s |
| 335 | if ne get gpbuf add j q get gptok add s q |
| 336 | ret false |
| 337 | end |
| 338 | end |
| 339 | ret true |
| 340 | end |
| 341 | |
| 342 | note an address marker "/*" then a hex digit at gpbuf[i]; the caller keeps i + 2 below n |
| 343 | fn gp_mark i ulong to bool |
| 344 | if ne get gpbuf i 47 |
| 345 | ret false |
| 346 | end |
| 347 | if ne get gpbuf add i 1 42 |
| 348 | ret false |
| 349 | end |
| 350 | c = get gpbuf add i 2 |
| 351 | if and ge c 48 le c 57 |
| 352 | ret true |
| 353 | end |
| 354 | ret and ge c 97 le c 102 |
| 355 | end |
| 356 | |
| 357 | note the index after the marker's "*/" and the blanks that follow |
| 358 | fn gp_after i ulong n ulong to ulong |
| 359 | j = add i 2 |
| 360 | while lt add j 1 n bound 65536 |
| 361 | if eq get gpbuf j 42 |
| 362 | if eq get gpbuf add j 1 47 |
| 363 | j = add j 2 |
| 364 | break |
| 365 | end |
| 366 | end |
| 367 | j = add j 1 |
| 368 | end |
| 369 | while lt j n bound 65536 |
| 370 | c = get gpbuf j |
| 371 | if and ne c 32 ne c 9 |
| 372 | break |
| 373 | end |
| 374 | j = add j 1 |
| 375 | end |
| 376 | ret j |
| 377 | end |
| 378 | |
| 379 | note the token instruction k must carry |
| 380 | fn gp_want k ulong to ulong |
| 381 | if lt k 124 |
| 382 | ret rem k 2 |
| 383 | end |
| 384 | if lt k 128 |
| 385 | ret add 2 rem k 2 |
| 386 | end |
| 387 | if eq k 128 |
| 388 | ret 4 |
| 389 | end |
| 390 | if eq k 129 |
| 391 | ret 0 |
| 392 | end |
| 393 | ret 1 |
| 394 | end |
| 395 | |
| 396 | fn gp_check to int |
| 397 | n = ulong 0 |
| 398 | while lt n 65536 bound 65536 |
| 399 | r = read 0 gpbuf n sub 65536 n |
| 400 | if lt r 0 |
| 401 | ret 70 |
| 402 | end |
| 403 | if eq r 0 |
| 404 | break |
| 405 | end |
| 406 | n = add n ulong r |
| 407 | end |
| 408 | if eq n 65536 |
| 409 | ret 71 |
| 410 | end |
| 411 | k = ulong 0 |
| 412 | bad = ulong 0 |
| 413 | i = ulong 0 |
| 414 | while lt add i 2 n bound 65536 |
| 415 | if gp_mark i |
| 416 | j = gp_after i n |
| 417 | if not gp_tok j n gp_want k |
| 418 | bad = add bad 1 |
| 419 | end |
| 420 | k = add k 1 |
| 421 | i = j |
| 422 | else |
| 423 | i = add i 1 |
| 424 | end |
| 425 | end |
| 426 | w = write 1 gpm5 0 len gpm5 |
| 427 | gp_dec k |
| 428 | w = write 1 gpm6 0 len gpm6 |
| 429 | gp_dec bad |
| 430 | w = write 1 gpnl 0 1 |
| 431 | if or ne k 133 ne bad 0 |
| 432 | ret 4 |
| 433 | end |
| 434 | ret 0 |
| 435 | end |
| 436 | |
| 437 | fn main to int |
| 438 | if gt argcnt 3 |
| 439 | ret 64 |
| 440 | end |
| 441 | if eq argcnt 3 |
| 442 | ret gp_check |
| 443 | end |
| 444 | if eq argcnt 2 |
| 445 | ret gp_bin |
| 446 | end |
| 447 | ret gp_rows |
| 448 | end |