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