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/CUDApp/blob/main/tests/regalloc_om/hazard_diff.c # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / regalloc_om / hazard_diff.c` 279 lines · 13188 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* hazard_diff.c: om/codegen/bw_hazard.om (src/omega_bw_hazard.c default build) against the C oracle 3 * (CUDAPP_MIGRATION_PENDING_C_HAZARD, symbols renamed legacy_*). The corpus partitions every branch of the C decoder 4 * and walk: every 12-bit opcode with every size field and both address/descriptor bits, the RZ / register 254 5 * boundaries, every barrier 0..7 (6 is the shift-by-64 case), every wait mask, guarded EXIT/BRA, branch targets in 6 * range, at both ends and out of range (including rel = INT32_MAX), undecodable words, the 256-finding cap and the 7 * committed GB10 fixture kernels with their waits dropped one at a time. */ 8 #include "omega_bw_hazard.h" 9 10 #include 11 #include 12 #include 13 #include 14 15 int legacy_omega_bw_decode(const uint8_t b[16], OmegaBwInsn *d); 16 int legacy_omega_bw_hazard_check(const uint8_t *code, size_t len, OmegaBwHazardReport *r); 17 18 static uint64_t g_s = 0x9e3779b97f4a7c15ull; 19 static uint64_t rnd(void) { 20 uint64_t z = (g_s += 0x9e3779b97f4a7c15ull); 21 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; 22 z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; 23 return z ^ (z >> 31); 24 } 25 26 static const uint16_t k_ops[] = {0x918, 0xb1d, 0x992, 0x98f, 0x94d, 0x947, 0x945, 0x941, 0x202, 0x802, 0x919, 0xb82, 27 0x7ac, 0x224, 0x824, 0xc24, 0x825, 0x227, 0x20c, 0x80c, 0xc0c, 0x210, 0x23c, 0x27a, 28 0x819, 0x219, 0x812, 0x212, 0x221, 0x220, 0x223, 0x20b, 0x208, 0x209, 0x23e, 0x245, 29 0x305, 0x308, 0xf89, 0x981, 0x986, 0x9a8, 0x984, 0x388, 0x988}; 30 #define NOPS (sizeof k_ops / sizeof k_ops[0]) 31 static const unsigned k_regs[] = {0, 1, 2, 3, 4, 5, 6, 7, 253, 254, 255}; 32 33 static long g_decodes, g_dec_refused, g_checks, g_chk_refused, g_findings, g_capped, g_fail; 34 35 static void put32(uint8_t *p, uint32_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); } 36 37 static int same_insn(const OmegaBwInsn *a, const OmegaBwInsn *b) { 38 if (a->op != b->op || a->guard != b->guard || a->stall != b->stall || a->yield != b->yield || a->wbar != b->wbar || 39 a->rbar != b->rbar || a->wait != b->wait || a->var != b->var || a->late != b->late || a->nr != b->nr || 40 a->nw != b->nw || a->flow != b->flow || a->target != b->target) 41 return 0; 42 for (unsigned j = 0; j < 5; ++j) 43 if (a->rd[j].reg != b->rd[j].reg || a->rd[j].n != b->rd[j].n || a->rd[j].uniform != b->rd[j].uniform) return 0; 44 if (a->wr[0].reg != b->wr[0].reg || a->wr[0].n != b->wr[0].n || a->wr[0].uniform != b->wr[0].uniform) return 0; 45 if (!a->name || !b->name) return a->name == b->name; 46 return strcmp(a->name, b->name) == 0; 47 } 48 49 static void decode_one(const uint8_t b[16]) { 50 OmegaBwInsn x, y; 51 memset(&x, 0xa5, sizeof x); 52 memset(&y, 0x5a, sizeof y); 53 int rx = legacy_omega_bw_decode(b, &x), ry = omega_bw_decode(b, &y); 54 g_decodes++; 55 if (rx) g_dec_refused++; 56 if (rx != ry || !same_insn(&x, &y)) { 57 if (g_fail++ < 10) { 58 uint32_t w[4]; 59 memcpy(w, b, 16); 60 printf("FAIL decode %08x %08x %08x %08x rc %d/%d op %x/%x nr %u/%u name %s/%s\n", w[0], w[1], w[2], w[3], 61 rx, ry, x.op, y.op, x.nr, y.nr, x.name ? x.name : "-", y.name ? y.name : "-"); 62 } 63 } 64 } 65 66 static OmegaBwHazardReport g_ra, g_rb; 67 static double g_t_c, g_t_om; 68 static double now(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (double)t.tv_sec + 1e-9 * (double)t.tv_nsec; } 69 70 static void check_one(const uint8_t *code, size_t len, int null_report) { 71 double t0 = now(); 72 int ra = legacy_omega_bw_hazard_check(code, len, null_report ? NULL : &g_ra); 73 double t1 = now(); 74 int rb = omega_bw_hazard_check(code, len, null_report ? NULL : &g_rb); 75 g_t_c += t1 - t0; 76 g_t_om += now() - t1; 77 g_checks++; 78 if (ra) g_chk_refused++; 79 int ok = ra == rb; 80 if (ok && !null_report) { 81 ok = g_ra.insns == g_rb.insns && g_ra.reachable == g_rb.reachable && g_ra.count == g_rb.count && 82 g_ra.bad_pc == g_rb.bad_pc; 83 for (size_t i = 0; ok && i < g_ra.count && i < OMEGA_BW_HAZARD_MAX; ++i) 84 ok = memcmp(&g_ra.f[i], &g_rb.f[i], sizeof g_ra.f[i]) == 0; 85 g_findings += (long)g_ra.count; 86 if (g_ra.count > OMEGA_BW_HAZARD_MAX) g_capped++; 87 } 88 if (!ok && g_fail++ < 10) 89 printf("FAIL check len %zu rc %d/%d insns %zu/%zu reach %zu/%zu count %zu/%zu bad %lld/%lld\n", len, ra, rb, 90 g_ra.insns, g_rb.insns, g_ra.reachable, g_rb.reachable, g_ra.count, g_rb.count, 91 (long long)g_ra.bad_pc, (long long)g_rb.bad_pc); 92 } 93 94 /* one random instruction; rel_span bounds the branch targets of a BRA */ 95 static void gen_insn(uint8_t *b, size_t pc, size_t n, int allow_bad) { 96 uint32_t op = k_ops[rnd() % NOPS]; 97 if (allow_bad && rnd() % 97 == 0) op = (uint32_t)(rnd() & 0xfffu); 98 uint32_t guard = rnd() % 4 ? 7u : (uint32_t)(rnd() & 15u); 99 uint32_t D = k_regs[rnd() % 11], A = k_regs[rnd() % 11], B = k_regs[rnd() % 11], C = k_regs[rnd() % 11]; 100 uint32_t size = rnd() % 8 ? (uint32_t)(rnd() % 7) : (uint32_t)(rnd() & 7u); 101 if (!allow_bad && size == 7) size = 6; 102 /* Mandatory size-class witness: hz_size opcode, size=4, live write. 103 * same_insn compares nr/nw and every rd.n/wr.n, not just the return code. */ 104 if (pc == 0) { op = 0x981u; size = 4; D = 1; } 105 uint32_t w0 = op | guard << 12 | D << 16 | A << 24; 106 uint32_t w1 = B | (uint32_t)(rnd() & 0xffffff00u); 107 uint32_t w2 = C | size << 9 | (rnd() & 1 ? 0x100u : 0) | (rnd() & 1 ? 0x08000000u : 0) | (uint32_t)(rnd() & 0xf0000000u); 108 uint32_t wbar = (uint32_t)(rnd() % 3 ? 7 : rnd() & 7), rbar = (uint32_t)(rnd() % 3 ? 7 : rnd() & 7); 109 uint32_t wait = (uint32_t)(rnd() % 3 ? 0 : rnd() & 63); 110 uint32_t w3 = (uint32_t)(rnd() & 15) << 9 | (uint32_t)(rnd() & 1) << 13 | wbar << 14 | rbar << 17 | wait << 20 | 111 (uint32_t)(rnd() & 0xfc000000u); 112 if (op == 0x947) { 113 int64_t lo = -(int64_t)pc - 2, hi = (int64_t)(n - pc) + 1; 114 int64_t t = lo + (int64_t)(rnd() % (uint64_t)(hi - lo + 1)); 115 int32_t rel = (int32_t)(t - 1); 116 if (rnd() % 61 == 0) rel = rnd() & 1 ? `2147483647` : (-`2147483647` - 1); 117 w0 = (w0 & ~(0x3fu << 18)) | ((uint32_t)rel & 63u) << 18; 118 w1 = (uint32_t)((rel >> 6) * 4); 119 } 120 put32(b, w0); put32(b + 4, w1); put32(b + 8, w2); put32(b + 12, w3); 121 } 122 123 static uint8_t *load(const char *path, size_t *len) { 124 FILE *f = fopen(path, "rb"); 125 if (!f) return NULL; 126 fseek(f, 0, SEEK_END); 127 long n = ftell(f); 128 fseek(f, 0, SEEK_SET); 129 uint8_t *p = malloc((size_t)n); 130 if (p && fread(p, 1, (size_t)n, f) != (size_t)n) { free(p); p = NULL; } 131 fclose(f); 132 *len = (size_t)n; 133 return p; 134 } 135 136 int main(void) { 137 uint8_t b[16]; 138 memset(b, 0, sizeof b); 139 put32(b, 0x981u | 7u << 12 | 1u << 16 | 2u << 24); 140 put32(b + 8, 4u << 9); 141 decode_one(b); 142 /* decode: every opcode x size field x bit 8 x bit 27, registers at the RZ boundary */ 143 for (uint32_t op = 0; op < 4096; ++op) 144 for (uint32_t s = 0; s < 8; ++s) 145 for (uint32_t bits = 0; bits < 4; ++bits) { 146 unsigned r = k_regs[(op + s + bits) % 11]; 147 uint32_t w0 = op | (uint32_t)(rnd() & 15) << 12 | r << 16 | k_regs[(op * 3 + s) % 11] << 24; 148 uint32_t w2 = k_regs[(op * 7 + bits) % 11] | s << 9 | (bits & 1 ? 0x100u : 0) | (bits & 2 ? 0x08000000u : 0); 149 put32(b, w0); put32(b + 4, k_regs[(op * 5 + s) % 11] | (uint32_t)(rnd() & 0xffffff00u)); 150 put32(b + 8, w2); put32(b + 12, (uint32_t)rnd()); 151 decode_one(b); 152 } 153 /* decode: random words over the known opcodes, every register value, BRA rel at both int32 ends */ 154 for (long i = 0; i < 300000; ++i) { 155 uint64_t x = rnd(), y = rnd(); 156 memcpy(b, &x, 8); memcpy(b + 8, &y, 8); 157 uint32_t w0; 158 memcpy(&w0, b, 4); 159 w0 = (w0 & ~0xfffu) | k_ops[rnd() % NOPS]; 160 memcpy(b, &w0, 4); 161 decode_one(b); 162 } 163 { 164 static const uint32_t k_bra[][2] = {{0x947u | 0x3fu << 18, 0x7fffffffu & ~3u}, {0x947u, 0x80000000u}, 165 {0x947u | 0x3fu << 18, 0xfffffffcu}, {0x947u, 0}}; 166 for (unsigned i = 0; i < 4; ++i) { 167 memset(b, 0, 16); 168 put32(b, k_bra[i][0]); put32(b + 4, k_bra[i][1]); 169 decode_one(b); 170 } 171 } 172 { 173 OmegaBwInsn d; 174 if (legacy_omega_bw_decode(NULL, &d) != omega_bw_decode(NULL, &d) || omega_bw_decode(b, NULL) != -1) { 175 g_fail++; 176 printf("FAIL decode NULL\n"); 177 } 178 g_decodes += 2; 179 } 180 181 /* check: refusals */ 182 static uint8_t prog[16 * 6000]; 183 memset(prog, 0, sizeof prog); 184 check_one(prog, 16, 1); 185 check_one(NULL, 16, 0); 186 check_one(prog, 0, 0); 187 check_one(prog, 1, 0); 188 check_one(prog, 15, 0); 189 check_one(prog, 17, 0); 190 191 /* check: random programs over every branch of the walk */ 192 for (long it = 0; it < 40000; ++it) { 193 size_t n = 1 + (size_t)(rnd() % (it % 10 == 0 ? 120 : 24)); 194 int allow_bad = rnd() % 4 == 0; 195 for (size_t pc = 0; pc < n; ++pc) gen_insn(prog + 16 * pc, pc, n, allow_bad); 196 if (rnd() % 2) { /* a final EXIT keeps most programs walkable */ 197 uint8_t *e = prog + 16 * (n - 1); 198 memset(e, 0, 16); 199 put32(e, 0x94du | 7u << 12); 200 put32(e + 12, 7u << 14 | 7u << 17); 201 } 202 check_one(prog, 16 * n, 0); 203 } 204 /* the 256-finding cap: 300 LDG writes of R0..R?, then 300 unwaited readers */ 205 { 206 size_t n = 0; 207 for (unsigned i = 0; i < 300; ++i, ++n) { 208 uint8_t *p = prog + 16 * n; 209 memset(p, 0, 16); 210 put32(p, 0x981u | 7u << 12 | (i % 250) << 16 | 254u << 24); 211 put32(p + 8, 0x100u); 212 put32(p + 12, (uint32_t)(i % 6) << 14 | 7u << 17); 213 } 214 for (unsigned i = 0; i < 300; ++i, ++n) { 215 uint8_t *p = prog + 16 * n; 216 memset(p, 0, 16); 217 put32(p, 0x210u | 7u << 12 | 253u << 16 | (i % 250) << 24); 218 put32(p + 4, (i * 7) % 250); 219 put32(p + 8, (i * 11) % 250); 220 put32(p + 12, 7u << 14 | 7u << 17); 221 } 222 memset(prog + 16 * n, 0, 16); 223 put32(prog + 16 * n, 0x94du | 7u << 12); 224 put32(prog + 16 * n + 12, 7u << 14 | 7u << 17); 225 check_one(prog, 16 * (n + 1), 0); 226 } 227 /* the portable unit's fixed code view: 2048 instructions agree with the oracle; 2049 is the documented 228 * divergence (the oracle has no cap; the successor refuses with insns set and bad_pc -1) */ 229 for (size_t n = 2047; n <= 2049; ++n) { 230 for (size_t pc = 0; pc < n; ++pc) gen_insn(prog + 16 * pc, pc, n, 0); 231 memset(prog + 16 * (n - 1), 0, 16); 232 put32(prog + 16 * (n - 1), 0x94du | 7u << 12); 233 put32(prog + 16 * (n - 1) + 12, 7u << 14 | 7u << 17); 234 if (n <= 2048) { 235 check_one(prog, 16 * n, 0); 236 continue; 237 } 238 int ra = legacy_omega_bw_hazard_check(prog, 16 * n, &g_ra); 239 int rb = omega_bw_hazard_check(prog, 16 * n, &g_rb); 240 g_checks++; 241 if (ra != 0 || rb != -1 || g_rb.insns != n || g_rb.bad_pc != -1 || g_rb.count != 0) { 242 g_fail++; 243 printf("FAIL length cap n %zu rc %d/%d\n", n, ra, rb); 244 } 245 } 246 /* the committed GB10 fixtures, as is and with every wait dropped, one at a time (first 400) */ 247 static const char *k_fix[] = {"pattn256_attend_`ad5d650b`.bin", "pattn256_attend_`e3a26848`.bin", 248 "pattn256_prep_`ad5d650b`.bin", "pattn256_prep_`e3a26848`.bin"}; 249 long fixtures = 0; 250 for (unsigned f = 0; f < 4; ++f) { 251 char path[256]; 252 snprintf(path, sizeof path, "tests/fixtures/bw_hazard/%s", k_fix[f]); 253 size_t len = 0; 254 uint8_t *code = load(path, &len); 255 if (!code) { g_fail++; printf("FAIL fixture %s missing\n", path); continue; } 256 check_one(code, len, 0); 257 fixtures++; 258 unsigned dropped = 0; 259 for (size_t pc = 0; pc < len / 16 && dropped < 400; ++pc) { 260 uint32_t w3 = (uint32_t)code[16 * pc + 12] | (uint32_t)code[16 * pc + 13] << 8 | 261 (uint32_t)code[16 * pc + 14] << 16 | (uint32_t)code[16 * pc + 15] << 24; 262 if (!(w3 >> 20 & 63u)) continue; 263 put32(code + 16 * pc + 12, w3 & ~(63u << 20)); 264 check_one(code, len, 0); 265 put32(code + 16 * pc + 12, w3); 266 dropped++; 267 fixtures++; 268 } 269 free(code); 270 } 271 if (g_fail) { 272 printf("FAIL hazard differential failures=%ld\n", g_fail); 273 return 1; 274 } 275 printf("PASS hazard decode differential cases=%ld refused=%ld\n", g_decodes, g_dec_refused); 276 printf("PASS hazard check differential cases=%ld refused=%ld findings=%ld capped=%ld fixtures=%ld c_s=%.3f omega_s=%.3f\n", 277 g_checks, g_chk_refused, g_findings, g_capped, fixtures, g_t_c, g_t_om); 278 return 0; 279 }