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/tests/bw_hazard_test.c # 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 / tests / bw_hazard_test.c` 688 lines ยท 50485 bytes Raw Blame History 1 /* bw_hazard_test.c: the static scoreboard hazard checker (src/omega_bw_hazard.c, omega-mv#554) run over every kernel 2 * Omega generates, plus its own proofs. Host only, no device. 3 * 4 * 1. decoder vs encoder: every IR op encoded with known registers decodes to exactly those registers; 5 * 2. red receipt: the pre-#553 head_dim 256 attend kernel (the bytes that faulted on the GB10) fails with WAR on 6 * R22/R23 at IR 88/89; the same kernel after the encoder fix passes; the fixed kernel with the one lost wait 7 * cleared fails again; 8 * 3. negative controls: small encoded programs with one hazard each are caught, the same programs with the wait 9 * in place pass, and an undecodable word is "not checked", never clean; 10 * 4. sweep: every generator over its accepted shapes (production kernels, mutant 0), encoded and patched as the 11 * launchers do; every finding is printed and must be on the triaged list below. 12 * 13 * --disasm: additionally cross-checks the decoder against Omega's own sm_121 disassembler (src/omega_sm121_disasm.c, 14 * in process) on every distinct kernel: the base registers each instruction names must be the ones the decoder found. 15 * The disassembler is checked word for word against nvdisasm by test-sm121dis; this test never calls nvdisasm (bar C6 16 * of docs/OMEGA.md: every check that called nvdisasm calls ours). */ 17 #include "omega_bw_hazard.h" 18 #include "omega_sm121_disasm.h" 19 #include "omega_blackwell_codegen.h" 20 #include "omega_blackwell_codegen_moe.h" 21 #include "omega_blackwell_codegen_moe2.h" 22 #include "omega_blackwell_codegen_bf16.h" 23 #include "omega_blackwell_codegen_router.h" 24 #include "omega_blackwell_codegen_attn.h" 25 #include "omega_blackwell_codegen_gemm.h" 26 #include "omega_blackwell_codegen_tern.h" 27 #include "omega_blackwell_codegen_tc.h" 28 #include "omega_blackwell_codegen_tc2.h" 29 #include "omega_blackwell_codegen_qg.h" 30 #include "omega_blackwell_codegen_qmma.h" 31 #include "omega_blackwell_codegen_pf.h" 32 #include "omega_blackwell_codegen_pfhost.h" 33 #include "omega_blackwell_codegen_dec.h" 34 #include "omega_blackwell_codegen_pattn.h" 35 #include "omega_gpu_attention_api.h" 36 #include "omega_gpu_elementwise_api.h" 37 #include "omega_gpu_prefill_attn_api.h" 38 #include 39 #include 40 #include 41 #include 42 #include 43 44 static int g_checks, g_failed, g_disasm; 45 static const char *g_dump_dir; /* --dump DIR: write each distinct kernel with findings as DIR/-.bin (triage) */ 46 #define CHECK(c, ...) do { g_checks++; if (!(c)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) 47 48 static BlackwellIRProgram g_prog; 49 static uint8_t g_code[OMEGA_BW_CODE_CAP_MAX + 4096]; 50 static OmegaBwHazardReport g_rep; 51 52 #define CW(stall, wbar, rbar, wait) \ 53 (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) 54 55 /* ------------------------------------------------------------------------------------------- 1. decoder vs encoder */ 56 typedef struct { int w[3]; int r[6]; } Ops; /* entries: reg | n << 8 | uniform << 12, 0 terminates */ 57 #define G(reg, n) ((reg) | (n) << 8) 58 #define U(reg, n) ((reg) | (n) << 8 | 1 << 12) 59 60 static int has(const OmegaBwOperand *o, unsigned cnt, int want) { 61 for (unsigned i = 0; i < cnt; i++) 62 if (o[i].reg == (want & 0xff) && o[i].n == ((want >> 8) & 0xf) && o[i].uniform == ((want >> 12) & 1)) return 1; 63 return 0; 64 } 65 static int count(const int *a, int max) { int n = 0; while (n < max && a[n]) n++; return n; } 66 67 static void test_decoder_vs_encoder(void) { 68 /* registers: dst 8, src1 12, src2 16, src3 20, uniform 6 (encode_one maps vreg n to Rn / URn) */ 69 static const struct { BlackwellIROpcode op; int src2, src3, ureg; Ops e; } t[] = { 70 { BW_IR_NOP, -1, -1, -1, { {0}, {0} } }, 71 { BW_IR_MOV_RZ, -1, -1, -1, { {G(8,1)}, {0} } }, 72 { BW_IR_MOV_IMM, -1, -1, -1, { {G(8,1)}, {0} } }, 73 { BW_IR_S2R, -1, -1, -1, { {G(8,1)}, {0} } }, 74 { BW_IR_LDC, -1, -1, -1, { {G(8,1)}, {0} } }, 75 { BW_IR_LDC64, -1, -1, -1, { {G(8,2)}, {0} } }, 76 { BW_IR_IMAD, 16, 20, -1, { {G(8,1)}, {G(12,1), G(16,1), G(20,1)} } }, 77 { BW_IR_IMAD, -1, 20, 6, { {G(8,1)}, {G(12,1), U(6,1), G(20,1)} } }, 78 { BW_IR_IMAD, -1, 20, -1, { {G(8,1)}, {G(12,1), G(20,1)} } }, 79 { BW_IR_IMAD_WIDE, -1, 20, -1, { {G(8,2)}, {G(12,1), G(20,2)} } }, 80 { BW_IR_ISETP_GE, -1, -1, -1, { {0}, {G(12,1)} } }, 81 { BW_IR_ISETP_GE, -1, -1, 6, { {0}, {G(12,1), U(6,1)} } }, 82 { BW_IR_LDG_E, -1, -1, 6, { {G(8,1)}, {G(12,2), U(6,1)} } }, 83 { BW_IR_STG_E, 16, -1, 6, { {0}, {G(12,2), G(16,1), U(6,1)} } }, 84 { BW_IR_STG_EF, 16, -1, 6, { {0}, {G(12,2), G(16,1), U(6,1)} } }, 85 { BW_IR_IADD3, 16, -1, -1, { {G(8,1)}, {G(12,1), G(16,1)} } }, 86 { BW_IR_HMMA_F16, 16, 20, -1, { {G(8,4)}, {G(12,4), G(16,2), G(20,4)} } }, 87 { BW_IR_HMMA_BF16, 16, 20, -1, { {G(8,4)}, {G(12,4), G(16,2), G(20,4)} } }, 88 { BW_IR_SHF_R, -1, -1, -1, { {G(8,1)}, {G(12,1)} } }, 89 { BW_IR_LOP3_AND, -1, -1, -1, { {G(8,1)}, {G(12,1)} } }, 90 { BW_IR_LDG_E_U16, -1, -1, 6, { {G(8,1)}, {G(12,2), U(6,1)} } }, 91 { BW_IR_LDG_STRONG_SYS, -1, -1, 6, { {G(8,1)}, {G(12,2), U(6,1)} } }, 92 { BW_IR_LDG_MMIO, -1, -1, 6, { {G(8,1)}, {G(12,2), U(6,1)} } }, 93 { BW_IR_STG_STRONG_SYS, 16, -1, 6, { {0}, {G(12,2), G(16,1), U(6,1)} } }, 94 { BW_IR_MEMBAR_ALL_SYS, -1, -1, -1, { {0}, {0} } }, 95 { BW_IR_MEMBAR_SC_SYS, -1, -1, -1, { {0}, {0} } }, 96 { BW_IR_CCTL_IVALL, -1, -1, -1, { {0}, {0} } }, 97 { BW_IR_ATOMG_ADD_STRONG_SYS, 16, -1, 6, { {G(8,1)}, {G(12,2), G(16,1), U(6,1)} } }, 98 { BW_IR_ATOMG_EXCH_STRONG_SYS, 16, -1, 6, { {G(8,1)}, {G(12,2), G(16,1), U(6,1)} } }, 99 { BW_IR_ISETP_GE_U32, 16, -1, -1, { {0}, {G(12,1), G(16,1)} } }, 100 { BW_IR_LOP3_XOR, 16, -1, -1, { {G(8,1)}, {G(12,1), G(16,1)} } }, 101 { BW_IR_FADD, 16, -1, -1, { {G(8,1)}, {G(12,1), G(16,1)} } }, 102 { BW_IR_FSUB, 16, -1, -1, { {G(8,1)}, {G(12,1), G(16,1)} } }, 103 { BW_IR_FMUL, 16, -1, -1, { {G(8,1)}, {G(12,1), G(16,1)} } }, 104 { BW_IR_FFMA, 16, 20, -1, { {G(8,1)}, {G(12,1), G(16,1), G(20,1)} } }, 105 { BW_IR_FSETP, 16, -1, -1, { {0}, {G(12,1), G(16,1)} } }, 106 { BW_IR_FSEL, 16, -1, -1, { {G(8,1)}, {G(12,1), G(16,1)} } }, 107 { BW_IR_FMNMX_MIN, 16, -1, -1, { {G(8,1)}, {G(12,1), G(16,1)} } }, 108 { BW_IR_FMNMX_MAX, 16, -1, -1, { {G(8,1)}, {G(12,1), G(16,1)} } }, 109 { BW_IR_I2FP, -1, -1, -1, { {G(8,1)}, {G(12,1)} } }, 110 { BW_IR_F2I, -1, -1, -1, { {G(8,1)}, {G(12,1)} } }, 111 { BW_IR_MUFU_RCP, -1, -1, -1, { {G(8,1)}, {G(12,1)} } }, 112 { BW_IR_MUFU_RSQ, -1, -1, -1, { {G(8,1)}, {G(12,1)} } }, 113 { BW_IR_SHFL_DOWN, -1, -1, -1, { {G(8,1)}, {G(12,1)} } }, 114 { BW_IR_LDS, -1, -1, -1, { {G(8,1)}, {G(12,1)} } }, 115 { BW_IR_STS, 16, -1, -1, { {0}, {G(12,1), G(16,1)} } }, 116 { BW_IR_EXIT, -1, -1, -1, { {0}, {0} } }, 117 { BW_IR_MUFU_EX2, -1, -1, -1, { {G(8,1)}, {G(12,1)} } }, 118 { BW_IR_BAR_SYNC, -1, -1, -1, { {0}, {0} } }, 119 { BW_IR_LDS32, -1, -1, -1, { {G(8,1)}, {G(12,1)} } }, 120 { BW_IR_STS32, 16, -1, -1, { {0}, {G(12,1), G(16,1)} } }, 121 { BW_IR_LOP3_LUT, 16, 20, -1, { {G(8,1)}, {G(12,1), G(16,1), G(20,1)} } }, 122 { BW_IR_SHF_L_U32, 16, 20, -1, { {G(8,1)}, {G(12,1), G(16,1), G(20,1)} } }, 123 { BW_IR_IMAD_HI_U32, 16, 20, -1, { {G(8,1)}, {G(12,1), G(16,1), G(20,1)} } }, 124 { BW_IR_LDG_E_64, -1, -1, 6, { {G(8,2)}, {G(12,2), U(6,1)} } }, 125 { BW_IR_LDG_E_128, -1, -1, 6, { {G(8,4)}, {G(12,2), U(6,1)} } }, 126 { BW_IR_LDS128, -1, -1, -1, { {G(8,4)}, {G(12,1)} } }, 127 }; 128 int bad = 0; 129 for (unsigned i = 0; i < sizeof t / sizeof *t; i++) { 130 BlackwellIRInsn in; memset(&in, 0, sizeof in); 131 in.op = t[i].op; in.dst_vreg = 8; in.src1_vreg = 12; in.src2_vreg = t[i].src2; in.src3_vreg = t[i].src3; in.ureg = t[i].ureg; 132 in.control = CW(3, 2, 5, 0x29) | 0x2000u; 133 /* IMAD and ISETP_GE take the uniform-operand form only with is_uniform set */ 134 if ((t[i].op == BW_IR_IMAD || t[i].op == BW_IR_ISETP_GE) && t[i].ureg >= 0) in.is_uniform = true; 135 uint32_t w[4]; uint8_t b[16]; OmegaBwInsn d; 136 if (omega_blackwell_encode_one(&in, w) != 0) { CHECK(0, "encode op %d", (int)t[i].op); bad++; continue; } 137 for (int k = 0; k < 4; k++) { b[4*k] = (uint8_t)w[k]; b[4*k+1] = (uint8_t)(w[k] >> 8); b[4*k+2] = (uint8_t)(w[k] >> 16); b[4*k+3] = (uint8_t)(w[k] >> 24); } 138 if (omega_bw_decode(b, &d) != 0) { CHECK(0, "decode of op %d (word0 %08x) refused", (int)t[i].op, w[0]); bad++; continue; } 139 int nw = count(t[i].e.w, 3), nr = count(t[i].e.r, 6), ok = d.nw == nw && d.nr == nr; 140 for (int k = 0; k < nw && ok; k++) ok = has(d.wr, d.nw, t[i].e.w[k]); 141 for (int k = 0; k < nr && ok; k++) ok = has(d.rd, d.nr, t[i].e.r[k]); 142 if (!ok) { bad++; CHECK(0, "op %d (%s): decoded %u writes %u reads, expected %d / %d", (int)t[i].op, d.name ? d.name : "?", d.nw, d.nr, nw, nr); } 143 } 144 { /* uniform loads write the uniform file */ 145 BlackwellIROpcode u[2] = { BW_IR_LDCU, BW_IR_LDCU64 }; 146 for (int i = 0; i < 2; i++) { 147 BlackwellIRInsn in; memset(&in, 0, sizeof in); in.op = u[i]; in.dst_vreg = 6; in.src1_vreg = in.src2_vreg = in.src3_vreg = in.ureg = -1; in.is_uniform = true; 148 uint32_t w[4]; uint8_t b[16]; OmegaBwInsn d; 149 CHECK(omega_blackwell_encode_one(&in, w) == 0, "encode LDCU"); 150 memcpy(b, w, 16); 151 CHECK(omega_bw_decode(b, &d) == 0 && d.nw == 1 && d.wr[0].uniform && d.wr[0].reg == 6 && d.wr[0].n == (i ? 2 : 1) && d.nr == 0 && d.var, 152 "LDCU%s decodes to a uniform write of UR6", i ? ".64" : ""); 153 } 154 } 155 { /* the control word: every field at a distinct value (NOP keeps insn->control on every encoder version) */ 156 BlackwellIRInsn in; memset(&in, 0, sizeof in); in.op = BW_IR_NOP; in.dst_vreg = in.src1_vreg = in.src2_vreg = in.src3_vreg = in.ureg = -1; 157 in.control = CW(3, 2, 5, 0x29) | 0x2000u; 158 uint32_t w[4]; OmegaBwInsn d; CHECK(omega_blackwell_encode_one(&in, w) == 0, "encode NOP"); 159 CHECK(omega_bw_decode((const uint8_t *)w, &d) == 0 && d.stall == 3 && d.yield == 1 && d.wbar == 2 && d.rbar == 5 && d.wait == 0x29, 160 "control fields stall %u yield %u wbar %u rbar %u wait 0x%x", d.stall, d.yield, d.wbar, d.rbar, d.wait); 161 } 162 { /* branch targets, both signs, both predicate senses */ 163 int32_t deltas[] = { -300, -64, -63, -5, -1, 0, 1, 2, 63, 64, 65, 1000 }; 164 for (unsigned i = 0; i < sizeof deltas / sizeof *deltas; i++) 165 for (int p = 0; p < 3; p++) { 166 BlackwellIRInsn in; memset(&in, 0, sizeof in); in.op = BW_IR_BRA; in.imm = (uint32_t)deltas[i]; 167 in.dst_vreg = in.src1_vreg = in.src2_vreg = in.src3_vreg = in.ureg = -1; in.predicate_p0 = p > 0; in.predicate_not = p == 2; 168 uint32_t w[4]; OmegaBwInsn d; 169 CHECK(omega_blackwell_encode_one(&in, w) == 0 && omega_bw_decode((const uint8_t *)w, &d) == 0 && d.flow == OMEGA_BW_FLOW_BRA && 170 d.target == deltas[i] && d.guard == (p == 0 ? 7u : p == 1 ? 0u : 8u), "BRA delta %d pred %d decodes to %d guard %u", deltas[i], p, d.target, d.guard); 171 } 172 } 173 printf("decoder vs encoder: %zu forms, %d mismatched\n", sizeof t / sizeof *t, bad); 174 } 175 176 /* -------------------------------------------------------------------------------------------- fixtures and helpers */ 177 static size_t load(const char *dir, const char *name, uint8_t *buf, size_t cap) { 178 char path[512]; snprintf(path, sizeof path, "%s/%s", dir, name); 179 FILE *f = fopen(path, "rb"); if (!f) return 0; 180 size_t n = fread(buf, 1, cap, f); fclose(f); return n; 181 } 182 static int finding(const OmegaBwHazardReport *r, uint32_t pc, uint8_t kind, uint8_t reg) { 183 for (size_t i = 0; i < r->count && i < OMEGA_BW_HAZARD_MAX; i++) 184 if (r->f[i].pc == pc && r->f[i].kind == kind && r->f[i].reg == reg && !r->f[i].uniform) return 1; 185 return 0; 186 } 187 static void print_findings(const char *tag, const OmegaBwHazardReport *r) { 188 for (size_t i = 0; i < r->count && i < OMEGA_BW_HAZARD_MAX; i++) 189 printf(" %s: IR %u %s %sR%u (release mask 0x%x, earliest producer/reader IR %u)\n", tag, r->f[i].pc, omega_bw_hazard_kind(r->f[i].kind), 190 r->f[i].uniform ? "U" : "", r->f[i].reg, r->f[i].release, r->f[i].src_pc); 191 } 192 static void set_wait(uint8_t *code, uint32_t pc, unsigned wait) { 193 uint8_t *p = code + (size_t)pc * 16u + 12u; uint32_t w3 = (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; 194 w3 = (w3 & ~(0x3fu << 20)) | (wait & 0x3fu) << 20; 195 p[0] = (uint8_t)w3; p[1] = (uint8_t)(w3 >> 8); p[2] = (uint8_t)(w3 >> 16); p[3] = (uint8_t)(w3 >> 24); 196 } 197 198 /* ---------------------------------------------------------------------------------------------- 2. red receipt */ 199 static void test_red_receipt(const char *dir) { 200 static uint8_t a[1 << 16]; 201 size_t n = load(dir, "pattn256_attend_`ad5d650b`.bin", a, sizeof a); 202 CHECK(n == 10112, "fixture pattn256_attend_`ad5d650b`.bin (%zu bytes)", n); 203 if (n == 10112) { 204 int rc = omega_bw_hazard_check(a, n, &g_rep); 205 CHECK(rc == 0 && g_rep.count == 2 && finding(&g_rep, 88, OMEGA_BW_HZ_WAR, 22) && finding(&g_rep, 89, OMEGA_BW_HZ_WAR, 23) && 206 g_rep.f[0].release == 0x3 && g_rep.f[1].release == 0x3, 207 "RED: pre-#553 attend must fail with WAR R22/R23 at IR 88/89 (rc %d, %zu findings)", rc, g_rep.count); 208 printf("red receipt, pre-#553 attend (the kernel that faulted on the GB10): %zu findings\n", g_rep.count); 209 print_findings("pre-#553 attend", &g_rep); 210 } 211 n = load(dir, "pattn256_attend_`e3a26848`.bin", a, sizeof a); 212 CHECK(n == 10112, "fixture pattn256_attend_`e3a26848`.bin (%zu bytes)", n); 213 if (n == 10112) { 214 int rc = omega_bw_hazard_check(a, n, &g_rep); 215 CHECK(rc == 0 && g_rep.count == 0, "GREEN: the fixed attend must pass (rc %d, %zu findings)", rc, g_rep.count); 216 printf("fixed attend (`e3a26848`): rc %d, %zu findings, %zu of %zu instructions reachable\n", rc, g_rep.count, g_rep.reachable, g_rep.insns); 217 /* the one wait the old encoder lost: the IADD3 at IR 55 waits on the Q loads' barriers 0x3 */ 218 OmegaBwInsn d; CHECK(omega_bw_decode(a + 55 * 16, &d) == 0 && d.op == 0x210 && d.wait == 0x3, "IR 55 is the IADD3 waiting 0x3"); 219 set_wait(a, 55, 0x10); 220 rc = omega_bw_hazard_check(a, n, &g_rep); 221 CHECK(rc == 0 && g_rep.count == 2 && finding(&g_rep, 88, OMEGA_BW_HZ_WAR, 22) && finding(&g_rep, 89, OMEGA_BW_HZ_WAR, 23), 222 "MUTANT: the fixed attend with IR 55 waiting 0x10 must fail again (%zu findings)", g_rep.count); 223 } 224 n = load(dir, "pattn256_prep_`ad5d650b`.bin", a, sizeof a); 225 CHECK(n == 5376, "fixture pattn256_prep_`ad5d650b`.bin"); 226 if (n == 5376) { 227 int rc = omega_bw_hazard_check(a, n, &g_rep); 228 CHECK(rc == 0 && g_rep.count == 1 && finding(&g_rep, 35, OMEGA_BW_HZ_RAW, 20) && g_rep.f[0].release == 0x1, 229 "pre-#553 prep: RAW R20 at IR 35 (S2R at IR 14 on barrier 0; the IADD3 at IR 15 waited on 4 only), %zu findings", g_rep.count); 230 print_findings("pre-#553 prep", &g_rep); 231 } 232 n = load(dir, "pattn256_prep_`e3a26848`.bin", a, sizeof a); 233 CHECK(n == 5376 && omega_bw_hazard_check(a, n, &g_rep) == 0 && g_rep.count == 0, "fixed prep passes (%zu findings)", g_rep.count); 234 } 235 236 /* -------------------------------------------------------------------------------------------- 3. negative controls */ 237 static size_t enc(const BlackwellIRInsn *ins, size_t n, uint8_t *out) { 238 for (size_t i = 0; i < n; i++) { uint32_t w[4]; if (omega_blackwell_encode_one(&ins[i], w) != 0) return 0; memcpy(out + 16 * i, w, 16); } 239 return 16 * n; 240 } 241 static BlackwellIRInsn mk(BlackwellIROpcode op, int d, int a, int b, int c, int u, uint32_t control) { 242 BlackwellIRInsn in; memset(&in, 0, sizeof in); 243 in.op = op; in.dst_vreg = d; in.src1_vreg = a; in.src2_vreg = b; in.src3_vreg = c; in.ureg = u; in.control = control; return in; 244 } 245 /* hand-encoded words (forms the encoder never writes, e.g. a forged control word) */ 246 static void raw16(uint8_t *b, uint32_t w0, uint32_t w1, uint32_t w2, uint32_t w3) { uint32_t w[4] = { w0, w1, w2, w3 }; memcpy(b, w, 16); } 247 static uint32_t bra16(int rel) { return 0x947u | ((uint32_t)(rel - 1) & 0x3fu) << 18; } /* BRA, target = this + rel (small rel > 0) */ 248 static void test_controls(void) { 249 uint8_t c[16 * 16]; size_t n; 250 const uint32_t FIX = CW(6, 7, 7, 0), LD = CW(4, 0, 1, 0) | 0x2000u, ST = CW(4, 7, 1, 0) | 0x2000u, ENDW = CW(5, 7, 7, 0); 251 /* RAW: a load's result read with no wait / with the wait / with a wait on an unrelated barrier (the #545 pattern) */ 252 BlackwellIRInsn raw[] = { mk(BW_IR_LDG_E, 4, 2, -1, -1, 4, LD), mk(BW_IR_FADD, 6, 4, 4, -1, -1, FIX), mk(BW_IR_EXIT, -1, -1, -1, -1, -1, ENDW) }; 253 n = enc(raw, 3, c); 254 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 1 && finding(&g_rep, 1, OMEGA_BW_HZ_RAW, 4), "control RAW caught"); 255 raw[1].control = CW(6, 7, 7, 0x1); n = enc(raw, 3, c); 256 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 0, "control RAW with the wait passes"); 257 raw[1].control = CW(6, 7, 7, 0x10); n = enc(raw, 3, c); 258 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 1 && finding(&g_rep, 1, OMEGA_BW_HZ_RAW, 4), "control RAW behind a wait on barrier 4 caught"); 259 raw[1].control = CW(6, 7, 7, 0x2); n = enc(raw, 3, c); /* waiting on the read barrier does not cover the result */ 260 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 1 && finding(&g_rep, 1, OMEGA_BW_HZ_RAW, 4), "control RAW behind the read barrier only caught"); 261 /* WAR: a store's data register overwritten before the store's read barrier */ 262 BlackwellIRInsn war[] = { mk(BW_IR_STG_E, -1, 2, 4, -1, 4, ST), mk(BW_IR_MOV_RZ, 4, -1, -1, -1, -1, FIX), mk(BW_IR_EXIT, -1, -1, -1, -1, -1, ENDW) }; 263 n = enc(war, 3, c); 264 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 1 && finding(&g_rep, 1, OMEGA_BW_HZ_WAR, 4), "control WAR (store data) caught"); 265 war[1].dst_vreg = 3; n = enc(war, 3, c); /* the store's address pair is R2:R3 */ 266 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 1 && finding(&g_rep, 1, OMEGA_BW_HZ_WAR, 3), "control WAR (address high half) caught"); 267 war[1].control = CW(6, 7, 7, 0x2); n = enc(war, 3, c); 268 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 0, "control WAR with the wait passes"); 269 war[1].dst_vreg = 5; war[1].control = FIX; n = enc(war, 3, c); 270 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 0, "control: a register the store does not read passes"); 271 /* WAR released by the load's completion: waiting on the write barrier also frees the address */ 272 BlackwellIRInsn warc[] = { mk(BW_IR_LDG_E, 4, 2, -1, -1, 4, LD), mk(BW_IR_MOV_RZ, 2, -1, -1, -1, -1, CW(6, 7, 7, 0x1)), mk(BW_IR_EXIT, -1, -1, -1, -1, -1, ENDW) }; 273 n = enc(warc, 3, c); 274 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 0, "control: address overwrite after the load completed passes"); 275 warc[1].control = FIX; n = enc(warc, 3, c); 276 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 1 && finding(&g_rep, 1, OMEGA_BW_HZ_WAR, 2), "control WAR (load address) caught"); 277 /* WAW: a pending load's destination overwritten */ 278 BlackwellIRInsn waw[] = { mk(BW_IR_LDG_E, 4, 2, -1, -1, 4, LD), mk(BW_IR_MOV_RZ, 4, -1, -1, -1, -1, CW(6, 7, 7, 0x2)), mk(BW_IR_EXIT, -1, -1, -1, -1, -1, ENDW) }; 279 n = enc(waw, 3, c); 280 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 1 && finding(&g_rep, 1, OMEGA_BW_HZ_WAW, 4), "control WAW caught"); 281 /* a variable-latency result with no write barrier at all: nothing can release it */ 282 BlackwellIRInsn nob[] = { mk(BW_IR_S2R, 4, -1, -1, -1, -1, CW(4, 7, 7, 0)), mk(BW_IR_FADD, 6, 4, 4, -1, -1, CW(6, 7, 7, 0x3f)), mk(BW_IR_EXIT, -1, -1, -1, -1, -1, ENDW) }; 283 nob[0].imm = BW_SR_TID_X; n = enc(nob, 3, c); 284 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 1 && finding(&g_rep, 1, OMEGA_BW_HZ_RAW, 4) && g_rep.f[0].release == 0, 285 "control: S2R with no write barrier caught even behind a wait on every barrier"); 286 /* a hazard carried round a loop: the read at the top sees the load issued at the bottom of the previous trip */ 287 BlackwellIRInsn loop[] = { mk(BW_IR_MOV_RZ, 4, -1, -1, -1, -1, FIX), mk(BW_IR_FADD, 6, 4, 4, -1, -1, FIX), mk(BW_IR_LDG_E, 4, 2, -1, -1, 4, LD), 288 mk(BW_IR_BRA, -1, -1, -1, -1, -1, CW(5, 7, 7, 0)), mk(BW_IR_EXIT, -1, -1, -1, -1, -1, ENDW) }; 289 loop[3].imm = (uint32_t)-2; loop[3].predicate_p0 = true; 290 n = enc(loop, 5, c); 291 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && finding(&g_rep, 1, OMEGA_BW_HZ_RAW, 4) && finding(&g_rep, 0, OMEGA_BW_HZ_WAW, 4) == 0, 292 "control: RAW across the loop back edge caught (%zu findings)", g_rep.count); 293 loop[3].control = CW(5, 7, 7, 0x1); n = enc(loop, 5, c); 294 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 0, "control: the loop with the wait on the branch passes (%zu findings)", g_rep.count); 295 /* the path the hazard is on is not the straight line: an unpredicated branch skips the wait */ 296 BlackwellIRInsn skip[] = { mk(BW_IR_LDG_E, 4, 2, -1, -1, 4, LD), mk(BW_IR_BRA, -1, -1, -1, -1, -1, CW(5, 7, 7, 0)), mk(BW_IR_NOP, -1, -1, -1, -1, -1, CW(1, 7, 7, 0x1)), 297 mk(BW_IR_FADD, 6, 4, 4, -1, -1, FIX), mk(BW_IR_EXIT, -1, -1, -1, -1, -1, ENDW) }; 298 skip[1].imm = 2; skip[1].predicate_p0 = true; n = enc(skip, 5, c); 299 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 1 && finding(&g_rep, 3, OMEGA_BW_HZ_RAW, 4), "control: RAW on the branch-taken path caught"); 300 skip[1].predicate_p0 = false; n = enc(skip, 5, c); 301 CHECK(n && omega_bw_hazard_check(c, n, &g_rep) == 0 && g_rep.count == 1 && g_rep.reachable == 4, "control: unreachable wait does not count (%zu reachable)", g_rep.reachable); 302 /* undecodable word: not checked, never clean */ 303 n = enc(raw, 3, c); c[16] = 0x55; c[17] = 0x7f; 304 CHECK(omega_bw_hazard_check(c, n, &g_rep) == -1 && g_rep.bad_pc == 1, "control: an unknown opcode is refused (bad_pc %lld)", (long long)g_rep.bad_pc); 305 /* branch out of range */ 306 BlackwellIRInsn far[] = { mk(BW_IR_BRA, -1, -1, -1, -1, -1, CW(5, 7, 7, 0)) }; far[0].imm = 5; n = enc(far, 1, c); 307 CHECK(omega_bw_hazard_check(c, n, &g_rep) == -1 && g_rep.bad_pc == 0, "control: a branch out of the buffer is refused"); 308 CHECK(omega_bw_hazard_check(c, 15, &g_rep) == -1 && omega_bw_hazard_check(NULL, 16, &g_rep) == -1, "control: bad length / NULL refused"); 309 /* barrier 6 does not exist on sm_121 (0..5, 7 = none) and would shift by 64 in the walk: a forged control word is 310 * refused (the kernel is reported as not checked), on either barrier field (DRA-606) */ 311 { 312 static const unsigned bars[3][2] = { { 6, 6 }, { 6, 7 }, { 7, 6 } }; 313 for (int b = 0; b < 3; b++) { 314 uint8_t u[3 * 16]; OmegaBwInsn d; 315 raw16(u + 0, 0x919u | 7u << 12 | 1u << 16, 0, 0, CW(0, bars[b][0], bars[b][1], 0)); /* S2R R1 */ 316 raw16(u + 16, 0x202u | 7u << 12 | 2u << 16, 1, 0, CW(6, 7, 7, 0)); /* MOV R2, R1 */ 317 raw16(u + 32, 0x94du | 7u << 12, 0, 0, CW(5, 7, 7, 0)); /* EXIT */ 318 CHECK(omega_bw_decode(u, &d) == -1, "control: decode refuses wbar %u rbar %u", bars[b][0], bars[b][1]); 319 CHECK(omega_bw_hazard_check(u, sizeof u, &g_rep) == -1 && g_rep.bad_pc == 0, 320 "control: a kernel with wbar %u rbar %u is not checked (bad_pc %lld)", bars[b][0], bars[b][1], (long long)g_rep.bad_pc); 321 } 322 } 323 /* src_pc is the smallest producer pc merged on any path, whatever order the worklist visits the two successors in. 324 * Layout A: the taken path (visited first) holds the larger producer pc; layout B: the smaller. Both report the 325 * smaller (before DRA-606 layout A reported IR 3, the producer visited first). bw_real_demo.c, Math Wizard. */ 326 { 327 uint8_t k[7 * 16]; 328 const uint32_t S2R1 = 0x919u | 7u << 12 | 1u << 16, MOV21 = 0x202u | 7u << 12 | 2u << 16, EX = 0x94du | 7u << 12; 329 raw16(k + 0, bra16(3) | 0u << 12, 0, 0, CW(5, 7, 7, 0)); /* 0: @P0 BRA -> 3 */ 330 raw16(k + 16, S2R1, 0, 0, CW(1, 0, 7, 0)); /* 1: S2R R1, wbar 0 */ 331 raw16(k + 32, bra16(2) | 7u << 12, 0, 0, CW(5, 7, 7, 0)); /* 2: BRA -> 4 */ 332 raw16(k + 48, S2R1, 0, 0, CW(1, 0, 7, 0)); /* 3: S2R R1, wbar 0 */ 333 raw16(k + 64, MOV21, 1, 0, CW(6, 7, 7, 0)); /* 4: MOV R2, R1 (no wait) */ 334 raw16(k + 80, EX, 0, 0, CW(5, 7, 7, 0)); /* 5: EXIT */ 335 int rc = omega_bw_hazard_check(k, 6 * 16, &g_rep); 336 CHECK(rc == 0 && g_rep.count == 1 && finding(&g_rep, 4, OMEGA_BW_HZ_RAW, 1) && g_rep.f[0].src_pc == 1, 337 "control: diamond A RAW R1 at IR 4 names producer IR 1 (rc %d, %zu findings, src_pc %u)", rc, g_rep.count, g_rep.count ? g_rep.f[0].src_pc : 0u); 338 raw16(k + 0, bra16(2) | 0u << 12, 0, 0, CW(5, 7, 7, 0)); /* 0: @P0 BRA -> 2 */ 339 raw16(k + 16, bra16(3) | 7u << 12, 0, 0, CW(5, 7, 7, 0)); /* 1: BRA -> 4 */ 340 raw16(k + 32, S2R1, 0, 0, CW(1, 0, 7, 0)); /* 2: S2R R1, wbar 0 */ 341 raw16(k + 48, bra16(2) | 7u << 12, 0, 0, CW(5, 7, 7, 0)); /* 3: BRA -> 5 */ 342 raw16(k + 64, S2R1, 0, 0, CW(1, 0, 7, 0)); /* 4: S2R R1, wbar 0 */ 343 raw16(k + 80, MOV21, 1, 0, CW(6, 7, 7, 0)); /* 5: MOV R2, R1 (no wait) */ 344 raw16(k + 96, EX, 0, 0, CW(5, 7, 7, 0)); /* 6: EXIT */ 345 rc = omega_bw_hazard_check(k, 7 * 16, &g_rep); 346 CHECK(rc == 0 && g_rep.count == 1 && finding(&g_rep, 5, OMEGA_BW_HZ_RAW, 1) && g_rep.f[0].src_pc == 2, 347 "control: diamond B RAW R1 at IR 5 names producer IR 2 (rc %d, %zu findings, src_pc %u)", rc, g_rep.count, g_rep.count ? g_rep.f[0].src_pc : 0u); 348 } 349 } 350 351 /* ------------------------------------------------------------------------------------------------------- 4. sweep */ 352 typedef struct { const char *family; char variant[96]; uint64_t hash; size_t findings; } Seen; 353 #define MAX_SEEN 8192 354 static Seen g_seen[MAX_SEEN]; 355 static size_t g_nseen, g_variants, g_unchecked, g_findings_total; 356 357 /* Triaged findings on main `a8ffd2b9` (2026-10-19), each read in the bytes (instruction, barrier, every wait between). 358 * Every finding the sweep prints must be listed here; a listed entry no longer seen prints a NOTE (fixed: remove it). 359 * The rule behind A and B is the one ptxas follows (offline oracle, nvcc 13.0 -arch=sm_121: an LDG whose address 360 * register is overwritten while in flight gets a read barrier and the overwriting IADD.64 waits on it; the first 361 * reader of an LDCU.64 descriptor waits on the LDCU's write barrier) and the one the chip taught us twice (#545; the 362 * fp8 GEMV hang in omega_bw_em.h, evidence OWN30B-FP8-GEMV-`c4006391`). None of A..C is observed failing on the chip: 363 * they are contract violations that timing has hidden so far. 364 * A the LDCU.64 descriptor UR4 (write barrier 1) is read by the first LDG with no wait on barrier 1 in between 365 * (matmul generators in omega_blackwell_codegen.c, default control words). Not caused by the #553 encoder bug. 366 * B matmul_tensor: the LDG / STG address pair R16:R17 is rewritten by the next IMAD.WIDE before the op read it: 367 * the loads only have write barrier 2 and nothing waits on it; the STGs carry no barrier at all (release 0). 368 * C prime sieve kernels: a second LDG / LDS writes its destination into the first load's address register while 369 * the first load (write barrier 0, no read barrier) is in flight; the pattern of the fp8 GEMV chip hang. 370 * D (gone) RMSNORM: round the loop the IMAD.WIDE / FMUL rewrite registers the STG (read barrier 1) still read; the 371 * IADD3s scheduled to wait on barrier 1 carried the fixed word 3 (wait 0x10) of the #553 encoder bug. PR #553 372 * (main `b11bd1f6`) fixed the encoder and the sweep no longer sees them, so their three entries are removed. */ 373 #define ANY_PC UINT32_MAX 374 typedef struct { const char *family; uint32_t pc; uint8_t kind; uint8_t reg; uint8_t uniform; char cls; int seen; } Known; 375 static Known g_known[] = { 376 { "matmul_i32", ANY_PC, OMEGA_BW_HZ_RAW, 4, 1, 'A', 0 }, 377 { "matmul_tensor", ANY_PC, OMEGA_BW_HZ_RAW, 4, 1, 'A', 0 }, 378 { "matmul_tensor_loop", ANY_PC, OMEGA_BW_HZ_RAW, 4, 1, 'A', 0 }, 379 { "matmul_frag", ANY_PC, OMEGA_BW_HZ_RAW, 4, 1, 'A', 0 }, 380 { "matmul_frag_block", ANY_PC, OMEGA_BW_HZ_RAW, 4, 1, 'A', 0 }, 381 { "matmul_tensor", ANY_PC, OMEGA_BW_HZ_WAR, 16, 0, 'B', 0 }, 382 { "matmul_tensor", ANY_PC, OMEGA_BW_HZ_WAR, 17, 0, 'B', 0 }, 383 { "elementwise", 25, OMEGA_BW_HZ_WAR, 22, 0, 'C', 0 }, /* PRIME_SIEVE */ 384 { "elementwise", 26, OMEGA_BW_HZ_WAR, 23, 0, 'C', 0 }, 385 { "elementwise", 39, OMEGA_BW_HZ_WAR, 21, 0, 'C', 0 }, /* PRIME_SIEVE_SHARED */ 386 { "elementwise", 25, OMEGA_BW_HZ_WAR, 20, 0, 'C', 0 }, /* PRIME_SIEVE_DIV1 */ 387 { "elementwise", 26, OMEGA_BW_HZ_WAR, 21, 0, 'C', 0 }, 388 { "elementwise", 41, OMEGA_BW_HZ_WAR, 19, 0, 'C', 0 }, /* PRIME_SIEVE_SHARED_DIV1 */ 389 { "elementwise", 26, OMEGA_BW_HZ_WAR, 20, 0, 'C', 0 }, /* PRIME_SIEVE_DIV2 */ 390 { "elementwise", 27, OMEGA_BW_HZ_WAR, 21, 0, 'C', 0 }, 391 { "elementwise", 42, OMEGA_BW_HZ_WAR, 19, 0, 'C', 0 }, /* PRIME_SIEVE_SHARED_DIV2 */ 392 { NULL, 0, 0, 0, 0, 0, 0 } 393 }; 394 395 static uint64_t fnv(const uint8_t *p, size_t n) { uint64_t h = 1469598103934665603ull; for (size_t i = 0; i < n; i++) { h ^= p[i]; h *= 1099511628211ull; } return h; } 396 397 static void disasm_cross_check(const char *family, const char *variant, const uint8_t *code, size_t len); 398 399 static void sweep_bytes(const char *family, const char *variant, const uint8_t *code, size_t len) { 400 g_variants++; 401 uint64_t h = fnv(code, len); 402 for (size_t i = 0; i < g_nseen; i++) if (g_seen[i].hash == h) return; /* identical bytes already checked */ 403 int rc = omega_bw_hazard_check(code, len, &g_rep); 404 if (g_nseen < MAX_SEEN) { Seen *s = &g_seen[g_nseen++]; s->family = family; snprintf(s->variant, sizeof s->variant, "%s", variant); s->hash = h; s->findings = g_rep.count; } 405 if (rc != 0) { 406 g_unchecked++; 407 OmegaBwInsn d; long long pc = g_rep.bad_pc; uint32_t w0 = 0; 408 if (pc >= 0) memcpy(&w0, code + pc * 16, 4); 409 CHECK(0, "%s %s: not checked (instruction %lld, word 0 %08x, decode %d)", family, variant, pc, w0, pc >= 0 ? omega_bw_decode(code + pc * 16, &d) : -9); 410 return; 411 } 412 for (size_t i = 0; i < g_rep.count && i < OMEGA_BW_HAZARD_MAX; i++) { 413 const OmegaBwHazard *f = &g_rep.f[i]; int known = 0; 414 for (size_t k = 0; g_known[k].family; k++) 415 if (!strcmp(g_known[k].family, family) && (g_known[k].pc == ANY_PC || g_known[k].pc == f->pc) && g_known[k].kind == f->kind && 416 g_known[k].reg == f->reg && g_known[k].uniform == f->uniform) { g_known[k].seen = 1; known = g_known[k].cls; } 417 printf("FINDING %s %s: IR %u %s %sR%u release 0x%x (earliest producer/reader IR %u) [%s %c]\n", family, variant, f->pc, omega_bw_hazard_kind(f->kind), 418 f->uniform ? "U" : "", f->reg, f->release, f->src_pc, known ? "triaged" : "UNTRIAGED", known ? known : '-'); 419 CHECK(known, "%s %s: untriaged %s at IR %u", family, variant, omega_bw_hazard_kind(f->kind), f->pc); 420 } 421 g_findings_total += g_rep.count; 422 if (g_dump_dir && g_rep.count) { 423 char path[512]; snprintf(path, sizeof path, "%s/%s-%zu.bin", g_dump_dir, family, g_nseen - 1); 424 /* 0644, not fopen's 0666 & ~umask: a dump is never world-writable */ 425 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); 426 FILE *f = fd >= 0 ? fdopen(fd, "wb") : NULL; 427 if (fd >= 0 && !f) close(fd); 428 if (f) { fwrite(code, 1, len, f); fclose(f); printf("DUMP %s = %s %s\n", path, family, variant); } 429 } 430 if (g_disasm) disasm_cross_check(family, variant, code, len); 431 } 432 433 /* encode + the launcher's patch step (SHFL.BFLY and F2FP.BF16 are native encoder words since training R5; only QMMA is patched) */ 434 enum { P_NONE = 0, P_QMMA = 1 }; 435 static void sweep_prog(const char *family, const char *variant, int patch) { 436 size_t len = 0; 437 if (omega_bw_encode_program(&g_prog, g_code, sizeof g_code, &len) != 0) { CHECK(0, "%s %s: encode failed", family, variant); return; } 438 if ((patch & P_QMMA) && omega_qmma_patch_code(&g_prog, g_code, len) < 0) { CHECK(0, "%s %s: qmma patch failed", family, variant); return; } 439 sweep_bytes(family, variant, g_code, len); 440 } 441 #define BUILD(call) (memset(&g_prog, 0, sizeof g_prog), (call) == 0) 442 443 static void sweep(void) { 444 char v[96]; 445 /* fp8 GEMV (MoE experts, attention projections): k values of the live models, every buildable lane / rows / warps / depth */ 446 { unsigned ks[] = { 256, 768, 2048, 4096, 8192 }; 447 for (unsigned a = 0; a < 5; a++) for (unsigned lb = 8; lb <= 16; lb += 8) for (unsigned rows = 1; rows <= 8; rows *= 2) 448 for (unsigned w = 1; w <= 32; w *= 2) for (unsigned dp = 0; dp <= 8; dp += dp ? dp : 1) { 449 OmegaFp8GemvSpec s = { .k = ks[a], .lane_bytes = lb, .rows = rows, .warps = w, .depth = dp }; 450 snprintf(v, sizeof v, "k%u lane%u rows%u warps%u depth%u", ks[a], lb, rows, w, dp); 451 if (BUILD(omega_blackwell_codegen_fp8_gemv_prog(&s, &g_prog))) sweep_prog("fp8_gemv", v, P_NONE); 452 if (BUILD(omega_blackwell_codegen_fp8_gemv2_prog(&s, &g_prog))) sweep_prog("fp8_gemv2", v, P_NONE); 453 } } 454 /* bf16 GEMV (lm_head) */ 455 { unsigned ks[] = { 256, 2048, 4096, 5120 }; 456 for (unsigned a = 0; a < 4; a++) for (unsigned rows = 1; rows <= 4; rows *= 2) for (unsigned w = 1; w <= 32; w *= 2) for (unsigned dp = 1; dp <= 16; dp *= 2) { 457 OmegaBf16GemvSpec s = { .k = ks[a], .rows = rows, .warps = w, .depth = dp }; 458 snprintf(v, sizeof v, "k%u rows%u warps%u depth%u", ks[a], rows, w, dp); 459 if (BUILD(omega_blackwell_codegen_bf16_gemv_prog(&s, &g_prog))) sweep_prog("bf16_gemv", v, P_NONE); 460 } } 461 /* fp8 grouped GEMM */ 462 { unsigned ks[] = { 768, 2048, 4096 }; 463 for (unsigned a = 0; a < 3; a++) for (unsigned lb = 8; lb <= 16; lb += 8) for (unsigned mt = 1; mt <= 16; mt *= 2) for (unsigned w = 1; w <= 32; w *= 2) { 464 OmegaFp8GemmSpec s = { .k = ks[a], .lane_bytes = lb, .mt = mt, .warps = w }; 465 snprintf(v, sizeof v, "k%u lane%u mt%u warps%u", ks[a], lb, mt, w); 466 if (BUILD(omega_blackwell_codegen_fp8gemm_prog(&s, &g_prog))) sweep_prog("fp8gemm", v, P_NONE); 467 } } 468 /* ternary GEMV */ 469 { unsigned ks[] = { 256, 2048, 4096, 8192, 16384 }; 470 for (unsigned a = 0; a < 5; a++) for (unsigned lay = 0; lay <= 1; lay++) for (unsigned rows = 1; rows <= 4; rows *= 2) for (unsigned w = 1; w <= 32; w *= 2) 471 for (unsigned dp = 1; dp <= 16; dp *= 2) for (unsigned epi = 0; epi <= 1; epi++) { 472 OmegaTernGemvSpec s = { ks[a], lay, rows, w, dp, epi, 0 }; 473 snprintf(v, sizeof v, "k%u layout%u rows%u warps%u depth%u epi%u", ks[a], lay, rows, w, dp, epi); 474 if (BUILD(omega_blackwell_codegen_tern_gemv_prog(&s, &g_prog))) sweep_prog("tern_gemv", v, P_NONE); 475 } } 476 /* router, decode attention */ 477 for (unsigned w = 8; w <= 32; w *= 2) for (unsigned dp = 1; dp <= 8; dp *= 2) { 478 OmegaRouterSpec s = { .warps = w, .depth = dp }; snprintf(v, sizeof v, "warps%u depth%u", w, dp); 479 if (BUILD(omega_blackwell_codegen_router_prog(&s, &g_prog))) sweep_prog("router", v, P_NONE); 480 } 481 for (unsigned dp = 1; dp <= 4; dp++) { 482 OmegaDecodeAttnSpec s = { dp, 0 }; snprintf(v, sizeof v, "depth%u", dp); 483 if (BUILD(omega_blackwell_codegen_decode_attn_prog(&s, &g_prog))) sweep_prog("decode_attn", v, P_NONE); 484 } 485 /* decode glue kernels (command list / decode graph) */ 486 { struct { OmegaDecKind k; uint32_t mode, stride; } d[] = { 487 { OMEGA_DEC_AXPY, 0, 256 }, { OMEGA_DEC_AXPY, 0, 512 }, { OMEGA_DEC_AXPY, 0, 2048 }, { OMEGA_DEC_AXPY, 0, 12288 }, 488 { OMEGA_DEC_SWIGLU, 0, 0 }, { OMEGA_DEC_ARGMAX, 0, 0 }, { OMEGA_DEC_ARGMAX, 1, 0 } }; 489 for (unsigned i = 0; i < sizeof d / sizeof *d; i++) { 490 OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = d[i].k; s.mode = d[i].mode; s.stride = d[i].stride; 491 snprintf(v, sizeof v, "kind%d mode%u stride%u", (int)d[i].k, d[i].mode, d[i].stride); 492 if (BUILD(omega_blackwell_codegen_dec_prog(&s, &g_prog))) sweep_prog("dec", v, P_NONE); 493 } 494 for (uint32_t mode = 0; mode < 8; mode++) { 495 OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_RES; s.mode = mode; 496 snprintf(v, sizeof v, "RES mode%u", mode); 497 if (BUILD(omega_blackwell_codegen_dec_prog(&s, &g_prog))) sweep_prog("dec", v, P_NONE); 498 } } 499 /* tensor-core GEMM and probes */ 500 { unsigned ks[] = { 128, 768, 2048, 4096, 16384 }; 501 for (unsigned a = 0; a < 5; a++) for (unsigned rt = 1; rt <= 4; rt *= 2) for (unsigned ct = 1; ct <= 4; ct *= 2) for (unsigned w = 1; w <= 8; w *= 2) { 502 OmegaTcGemmSpec s = { ks[a], rt, ct, w, 0 }; snprintf(v, sizeof v, "k%u rt%u ct%u warps%u", ks[a], rt, ct, w); 503 if (BUILD(omega_blackwell_codegen_tcgemm_prog(&s, &g_prog))) sweep_prog("tcgemm", v, P_NONE); 504 } } 505 if (BUILD(omega_blackwell_codegen_tc_probe_prog(0, &g_prog))) sweep_prog("tc_probe", "mutant0", P_NONE); 506 for (unsigned ch = 1; ch <= 16; ch *= 2) for (unsigned st = 1; st <= 4; st++) { 507 snprintf(v, sizeof v, "iters5 chains%u stall%u threads64", ch, st); 508 if (BUILD(omega_blackwell_codegen_tc_peak_prog(5, ch, st, 64, &g_prog))) sweep_prog("tc_peak", v, P_NONE); 509 snprintf(v, sizeof v, "iters64 chains%u stall%u threads128", ch, st); 510 if (BUILD(omega_blackwell_codegen_qmma_peak_prog(64, ch, st, 128, &g_prog))) sweep_prog("qmma_peak", v, P_QMMA); 511 } 512 for (unsigned g = 1; g <= 8; g *= 2) for (unsigned t = 32; t <= 512; t *= 2) { 513 snprintf(v, sizeof v, "iters16 groups%u threads%u", g, t); 514 if (BUILD(omega_blackwell_codegen_tc_bw_prog(16, g, t, &g_prog))) sweep_prog("tc_bw", v, P_NONE); 515 } 516 for (unsigned nops = 0; nops <= 8; nops += 4) { 517 snprintf(v, sizeof v, "nops%u", nops); 518 if (BUILD(omega_blackwell_codegen_qmma_probe_prog(0, nops, &g_prog))) sweep_prog("qmma_probe", v, P_QMMA); 519 } 520 if (BUILD(omega_blackwell_codegen_f2e4_probe_prog(&g_prog))) sweep_prog("f2e4_probe", "-", P_QMMA); 521 /* prefill module kernels (omega_gpu_prefill.c applies the QMMA patch to every one of them) */ 522 { unsigned ks[] = { 128, 768, 2048, 4096, 16384 }; 523 for (unsigned a = 0; a < 5; a++) for (unsigned rt = 2; rt <= 8; rt *= 2) for (unsigned w = 4; w <= 16; w *= 2) for (unsigned aq = 1; aq <= 2; aq++) 524 for (unsigned epi = 0; epi <= 1; epi++) for (unsigned dir = 0; dir <= 1; dir++) { 525 OmegaTc2Spec s = { ks[a], rt, 2, w, aq, epi, dir, 0, 0 }; 526 s.n = 4u * w * 8u * 2u; if (epi) s.n *= 2u; 527 snprintf(v, sizeof v, "k%u rt%u warps%u aqn%u epi%u direct%u n%u", ks[a], rt, w, aq, epi, dir, s.n); 528 if (BUILD(omega_blackwell_codegen_tc2_prog(&s, &g_prog))) sweep_prog("tc2", v, P_QMMA); 529 } 530 for (unsigned a = 0; a < 5; a++) for (unsigned w = 8; w <= 16; w *= 2) for (unsigned epi = 0; epi <= 1; epi++) for (unsigned dir = 0; dir <= 1; dir++) 531 for (unsigned wt = 0; wt <= 120; wt += 60) { 532 OmegaQgSpec s = { ks[a], 4, w, epi, dir, 512, wt, 0 }; 533 snprintf(v, sizeof v, "k%u warps%u epi%u direct%u n512 wait%u", ks[a], w, epi, dir, wt); 534 if (BUILD(omega_blackwell_codegen_qgemm_prog(&s, &g_prog))) sweep_prog("qgemm", v, P_QMMA); 535 } 536 for (unsigned a = 0; a < 4; a++) for (unsigned it = 0; it <= 2; it++) { 537 OmegaPqSpec s = { .k = ks[a], .in_type = it }; snprintf(v, sizeof v, "k%u in_type%u", ks[a], it); 538 if (BUILD(omega_blackwell_codegen_pq_prog(&s, &g_prog))) sweep_prog("pq", v, P_QMMA); 539 } } 540 { unsigned ks[] = { 768, 2048, 4096 }, es[] = { 8, 64, 128 }, ts[] = { 1, 16, 128, 1024 }; 541 for (unsigned a = 0; a < 3; a++) for (unsigned b = 0; b < 3; b++) for (unsigned c = 0; c < 4; c++) { 542 OmegaRlSpec s = { ks[a], es[b], ts[c], 0 }; snprintf(v, sizeof v, "k%u e%u t%u", ks[a], es[b], ts[c]); 543 if (BUILD(omega_blackwell_codegen_rl_prog(&s, &g_prog))) sweep_prog("rl", v, P_QMMA); 544 } 545 unsigned hs[] = { 768, 2048, 4096 }; 546 for (unsigned a = 0; a < 3; a++) for (unsigned tk = 1; tk <= 8; tk *= 2) { 547 OmegaMcSpec s = { hs[a], tk, 0 }; snprintf(v, sizeof v, "h%u top_k%u", hs[a], tk); 548 if (BUILD(omega_blackwell_codegen_mc_prog(&s, &g_prog))) sweep_prog("mc", v, P_QMMA); 549 } } 550 if (BUILD(omega_blackwell_codegen_pf_copy_prog(&g_prog))) sweep_prog("pf_copy", "-", P_QMMA); 551 /* prefill attention: the API's own host encode (build + encode) */ 552 for (unsigned w = 4; w <= 8; w *= 2) for (unsigned md = 0; md <= 2; md++) for (unsigned mir = 0; mir <= 1; mir++) { 553 OmegaPrefillAttnSpec s = { w, 0, md, mir }; 554 static uint8_t pa[OMEGA_BW_CODE_CAP_MAX + 4096]; uint32_t gp = 0; 555 size_t len = omega_gpu_prefill_attn_encode(&s, pa, sizeof pa, &gp); 556 snprintf(v, sizeof v, "warps%u mode%u mirror%u", w, md, mir); 557 if (len) sweep_bytes("prefill_attn", v, pa, len); 558 } 559 /* gqa / paged attention (FB-1 cut 5): the API's codegen */ 560 for (int f32 = 0; f32 <= 1; f32++) for (uint32_t lb = 0; lb <= 8; lb++) for (uint32_t lg = 0; lg <= 4; lg++) for (uint32_t hd = 64; hd <= 128; hd *= 2) { 561 OmegaBlackwellKernel k; memset(&k, 0, sizeof k); 562 if (omega_gpu_attention_codegen_hd(f32 != 0, lb, lg, hd, &k) != 0 || !k.code) { free(k.code); continue; } 563 snprintf(v, sizeof v, "kv_f32 %d log2_block %u log2_gqa %u hd %u", f32, lb, lg, hd); 564 sweep_bytes("attention", v, k.code, k.code_size); 565 free(k.code); 566 } 567 /* every elementwise op: the kernels, the reconvergence probes and the MUFU probes */ 568 for (int op = OMEGA_GPU_EW_EX2; op < OMEGA_GPU_EW_OP_END; op++) { 569 OmegaBlackwellKernel k; memset(&k, 0, sizeof k); 570 if (omega_gpu_elementwise_codegen_ir((OmegaGpuEwOp)op, 0, &g_prog, &k) != 0 || !k.code) { free(k.code); continue; } 571 snprintf(v, sizeof v, "op%d", op); 572 sweep_bytes("elementwise", v, k.code, k.code_size); 573 free(k.code); 574 } 575 /* matmul: integer, single-tile tensor, looped tensor, fragment, blocked fragment */ 576 { OmegaMatMulSpec s; 577 uint32_t i32[][3] = { {16,16,16}, {32,16,64}, {16,32,16}, {64,32,64} }; 578 for (unsigned i = 0; i < 4; i++) if (omega_matmul_spec_init(&s, i32[i][0], i32[i][1], i32[i][2], OMEGA_MATMUL_PRECISION_INT32) == 0) { 579 snprintf(v, sizeof v, "m%u k%u n%u", i32[i][0], i32[i][1], i32[i][2]); 580 if (BUILD(omega_blackwell_codegen_matmul_i32_prog(&s, &g_prog))) sweep_prog("matmul_i32", v, P_NONE); 581 } 582 for (int pr = 0; pr < 2; pr++) { 583 OmegaMatMulPrecision p = pr ? OMEGA_MATMUL_PRECISION_FP16 : OMEGA_MATMUL_PRECISION_BF16; 584 if (omega_matmul_spec_init(&s, 16, 16, 8, p) == 0 && BUILD(omega_blackwell_codegen_matmul_tensor_prog(&s, &g_prog))) sweep_prog("matmul_tensor", pr ? "fp16" : "bf16", P_NONE); 585 uint32_t kk[] = { 16, 64, 2048 }, nn[] = { 8, 64, 2048 }; 586 for (unsigned a = 0; a < 3; a++) for (unsigned b = 0; b < 3; b++) for (uint32_t gx = 1; gx <= 8; gx *= 2) { 587 if (omega_matmul_spec_init(&s, 16, kk[a], nn[b], p) != 0) continue; 588 snprintf(v, sizeof v, "%s k%u n%u grid_x%u", pr ? "fp16" : "bf16", kk[a], nn[b], gx); 589 if (BUILD(omega_blackwell_codegen_matmul_tensor_loop_prog(&s, gx, 0, &g_prog))) sweep_prog("matmul_tensor_loop", v, P_NONE); 590 } 591 } 592 unsigned kps[] = { 768, 2048, 4096 }, nps[] = { 8, 128, 2048 }; 593 for (unsigned a = 0; a < 3; a++) for (unsigned b = 0; b < 3; b++) { 594 if (omega_matmul_spec_init(&s, 16, kps[a], nps[b], OMEGA_MATMUL_PRECISION_BF16) != 0) continue; 595 for (uint32_t un = 1; un <= 4; un *= 2) { 596 snprintf(v, sizeof v, "k%u n%u unroll%u", kps[a], nps[b], un); 597 if (BUILD(omega_blackwell_codegen_matmul_frag_prog(&s, 48, un, 0, &g_prog))) sweep_prog("matmul_frag", v, P_NONE); 598 } 599 for (unsigned rt = 1; rt <= 8; rt *= 2) for (unsigned ct = 1; ct <= 4; ct *= 2) for (uint32_t un = 1; un <= 2; un++) { 600 if (rt * ct < 2) continue; 601 snprintf(v, sizeof v, "k%u n%u rt%u ct%u unroll%u", kps[a], nps[b], rt, ct, un); 602 if (BUILD(omega_blackwell_codegen_matmul_frag_block_prog(&s, 1, un, rt, ct, 0, &g_prog))) sweep_prog("matmul_frag_block", v, P_NONE); 603 } 604 } } 605 /* the command list's no-op kernel: EXIT; BRA self */ 606 { omega_bw_ir_init(&g_prog); 607 BlackwellIRInsn ex = mk(BW_IR_EXIT, -1, -1, -1, -1, -1, 0), self = mk(BW_IR_BRA, -1, -1, -1, -1, -1, 0x000fc000u); 608 if (omega_bw_ir_append(&g_prog, &ex) >= 0 && omega_bw_ir_append(&g_prog, &self) >= 0 && omega_bw_regalloc_solve(&g_prog) == 0) sweep_prog("cmdlist_nop", "-", P_NONE); } 609 } 610 611 /* ----------------------------------------------------------------------------------- disassembler cross-check (C6) */ 612 static size_t g_dis_insns, g_dis_bad, g_dis_kernels; 613 static int decoded_has(const OmegaBwInsn *d, int reg, int uniform) { 614 for (unsigned j = 0; j < d->nr; j++) if (d->rd[j].uniform == uniform && d->rd[j].reg == reg) return 1; 615 for (unsigned j = 0; j < d->nw; j++) if (d->wr[j].uniform == uniform && d->wr[j].reg == reg) return 1; 616 return 0; 617 } 618 static void disasm_cross_check(const char *family, const char *variant, const uint8_t *code, size_t len) { 619 size_t seen = 0; g_dis_kernels++; 620 for (size_t pc = 0; pc * 16u + 16u <= len; pc++) { 621 const uint8_t *w = code + pc * 16u; 622 OmegaBwInsn d; if (omega_bw_decode(w, &d) != 0) continue; /* unreachable padding words are not decoded either */ 623 seen++; g_dis_insns++; 624 uint64_t lo = 0, hi = 0; 625 for (int i = 7; i >= 0; i--) { lo = lo << 8 | w[i]; hi = hi << 8 | w[8 + i]; } 626 char t[256]; 627 int rc = omega_sm121_disasm(lo, hi, pc * 16u, t, sizeof t); 628 if (rc != 0) { g_dis_bad++; if (g_dis_bad <= 20) printf("DISASM UNKNOWN %s %s IR %zu: decoder %s, disassembler rc %d lo %016llx hi %016llx\n", family, variant, pc, d.name, rc, (unsigned long long)lo, (unsigned long long)hi); continue; } 629 /* every register token the disassembler prints must be a base register the decoder has, and vice versa */ 630 int nvr[16], nvu[16], nn = 0, nu = 0; 631 for (char *q = t; *q; q++) { 632 int uni = 0; char *r = q; 633 if (r[0] == 'U' && r[1] == 'R') { uni = 1; r++; } else if (q > t && (q[-1] == 'U' || q[-1] == 'S' || q[-1] == '_')) continue; 634 if (r[0] != 'R' || r[1] < '0' || r[1] > '9') continue; 635 if (q > t && ((q[-1] >= 'A' && q[-1] <= 'Z') || (q[-1] >= 'a' && q[-1] <= 'z') || q[-1] == '_')) continue; 636 int reg = atoi(r + 1); 637 if (uni) { if (nu < 16) nvu[nu++] = reg; } else { if (nn < 16) nvr[nn++] = reg; } 638 q = r + 1; 639 } 640 int ok = 1; 641 for (int i = 0; i < nn && ok; i++) ok = decoded_has(&d, nvr[i], 0); 642 for (int i = 0; i < nu && ok; i++) ok = decoded_has(&d, nvu[i], 1); 643 for (unsigned j = 0; j < d.nr + d.nw && ok; j++) { 644 const OmegaBwOperand *o = j < d.nr ? &d.rd[j] : &d.wr[j - d.nr]; int f2 = 0; 645 for (int i = 0; i < (o->uniform ? nu : nn); i++) if ((o->uniform ? nvu[i] : nvr[i]) == o->reg) f2 = 1; 646 ok = f2; 647 } 648 if (!ok) { g_dis_bad++; if (g_dis_bad <= 20) printf("DISASM MISMATCH %s %s IR %zu: '%s' vs decoder %s (%u reads, %u writes)\n", family, variant, pc, t, d.name, d.nr, d.nw); } 649 } 650 CHECK(seen > 0, "%s %s: disassembled %zu instructions", family, variant, seen); 651 } 652 653 int main(int argc, char **argv) { 654 const char *dir = "tests/fixtures/bw_hazard"; 655 for (int i = 1; i < argc; i++) { 656 if (!strcmp(argv[i], "--disasm")) g_disasm = 1; 657 else if (!strcmp(argv[i], "--fixtures") && i + 1 < argc) dir = argv[++i]; 658 else if (!strcmp(argv[i], "--dump") && i + 1 < argc) g_dump_dir = argv[++i]; 659 } 660 test_decoder_vs_encoder(); 661 test_red_receipt(dir); 662 test_controls(); 663 sweep(); 664 size_t clean = 0; 665 printf("sweep per generator (distinct kernels, clean, findings):\n"); 666 for (size_t i = 0; i < g_nseen; i++) { 667 clean += g_seen[i].findings == 0; 668 int first = 1; 669 for (size_t j = 0; j < i && first; j++) if (!strcmp(g_seen[j].family, g_seen[i].family)) first = 0; 670 if (!first) continue; 671 size_t fk = 0, fc = 0, fv = 0; 672 for (size_t j = i; j < g_nseen; j++) 673 if (!strcmp(g_seen[j].family, g_seen[i].family)) { fk++; fc += g_seen[j].findings == 0; fv += g_seen[j].findings; } 674 printf(" %-20s %4zu distinct, %4zu clean, %4zu findings\n", g_seen[i].family, fk, fc, fv); 675 } 676 for (size_t k = 0; g_known[k].family; k++) 677 if (!g_known[k].seen) printf("NOTE: triaged finding %s IR %d %s %sR%u (class %c) no longer seen\n", g_known[k].family, g_known[k].pc == ANY_PC ? -1 : (int)g_known[k].pc, 678 omega_bw_hazard_kind(g_known[k].kind), g_known[k].uniform ? "U" : "", g_known[k].reg, g_known[k].cls); 679 printf("sweep: %zu variants built, %zu distinct kernels, %zu clean, %zu with findings, %zu findings, %zu not checked\n", 680 g_variants, g_nseen, clean, g_nseen - clean - g_unchecked, g_findings_total, g_unchecked); 681 CHECK(g_nseen >= 50, "the sweep built only %zu distinct kernels", g_nseen); 682 if (g_disasm) { 683 printf("disassembler cross-check (sm121dis, in process): %zu kernels, %zu instructions, %zu mismatched\n", g_dis_kernels, g_dis_insns, g_dis_bad); 684 CHECK(g_dis_bad == 0 && g_dis_insns > 0, "disassembler cross-check"); 685 } 686 printf("bw_hazard_test: %d checks, %d failed: %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); 687 return g_failed ? 1 : 0; 688 }