interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tests / bw_hazard_test.c

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