interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / evidence / OWN30B-REQUAL-MAIN-710d438c-20261015 / gates / reconv / GPU-RECONV-710d438c / gpu_reconv_test.c

472 lines · 32836 bytes Raw Blame History
1/* gpu_reconv_test: structured warp reconvergence (omega #308) on Omega's own Blackwell codegen.
2 *
3 * ./gpu_reconv_test --host-only encoder golden words, region bookkeeping, codegen refusals,
4 * nvdisasm listing (when /usr/local/cuda/bin/nvdisasm exists),
5 * every probe kernel through the SIMT warp simulator against
6 * the host oracle under both fragment orders, the mutation
7 * controls, scoreboard and control-edge walks. No device.
8 * ./gpu_reconv_test --out receipt.json chip gate: host part first (a kernel the simulator rejects
9 * never gets chip time), then every probe on the chip against
10 * the oracle, bit-exact, then each math mutant must be caught.
11 * --observe-dropped-join chip only, optional: also launch the dropped-BSYNC mutant of
12 * one probe and record what the chip did (an observation, not
13 * a gate; the simulator rejects that kernel first).
14 *
15 * Probes (src/omega_gpu_elementwise_api.c, gen_rc_*): R3 diamond, R4 loop break, R5 nested,
16 * R6 lanes past the end with STS/BAR/LDS + SHFL, and the 16-deep barrier-register bound.
17 * Oracle: rc_oracle below; every comparison is exact (u32). Launcher chunking (64 CTAs x 128
18 * threads per launch, P0 = words in the chunk) is part of the oracle, so a clamp to the wrong
19 * chunk end shows up. */
20#include "omega_gpu_elementwise_api.h"
21#include "omega_blackwell_codegen.h"
22#include "omega_bw_reconv.h"
23#include "omega_gpu_session.h"
24#include "bw_warp_sim.h"
25#include <inttypes.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
31static int g_checks, g_failed;
32#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
33
34static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
35
36typedef struct { OmegaGpuEwOp op; const char *name; const char *reg; } Probe;
37static const Probe PROBES[] = {
38 { OMEGA_GPU_EW_RC_DIAMOND, "rc_diamond", "R3" },
39 { OMEGA_GPU_EW_RC_LOOPBREAK, "rc_loopbreak", "R4" },
40 { OMEGA_GPU_EW_RC_NESTED, "rc_nested", "R5" },
41 { OMEGA_GPU_EW_RC_EXIT_BAR, "rc_exit_bar", "R6" },
42 { OMEGA_GPU_EW_RC_DEPTH16, "rc_depth16", "B0..B15" },
43};
44#define NPROBES (sizeof PROBES / sizeof PROBES[0])
45/* 8191..8193 straddled the 64-CTA chunk, 131071..131073 the 1024-CTA chunk (cut L, 2026-10-12) */
46static const uint32_t NS[] = { 1, 5, 31, 32, 33, 63, 64, 65, 96, 127, 128, 129, 200, 1000, 4096, 8191, 8192, 8193, 20000, 131071, 131072, 131073 };
47#define MAXN 131073u
48#define NNS (sizeof NS / sizeof NS[0])
49#define CHUNK (OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS)
50
51/* ----------------------------------------------------------------- oracle */
52static uint32_t count_loop(uint32_t x, uint32_t y, int math, uint32_t *v_out) {
53 uint32_t v = x & 255u, st = (y & 15u) + 1u, cnt = 0, bound = math ? 255u : 256u;
54 while (v < bound) { v += st; cnt++; }
55 *v_out = v; return cnt;
56}
57static uint32_t rc_r(OmegaGpuEwOp op, int math, uint32_t x, uint32_t y, uint32_t partner_x) {
58 uint32_t v, cnt;
59 switch (op) {
60 case OMEGA_GPU_EW_RC_DIAMOND:
61 if (x >= y) { uint32_t t = 3u * x + y, u = t ^ x, r = 5u * u + 1u; r += r >> 3; return math ? r + 1u : r; }
62 return x + 1u;
63 case OMEGA_GPU_EW_RC_LOOPBREAK: cnt = count_loop(x, y, math, &v); return cnt * 256u + v;
64 case OMEGA_GPU_EW_RC_NESTED: if (x & 1u) return 7u; cnt = count_loop(x, y, math, &v); return cnt * 256u + v + 100u;
65 case OMEGA_GPU_EW_RC_EXIT_BAR: return 3u * x + (partner_x | 1u);
66 case OMEGA_GPU_EW_RC_DEPTH16: return x + (x < y ? (uint32_t)BW_RECONV_MAX_BAR : 0u);
67 default: return 0xdeadbeefu;
68 }
69}
70static void rc_oracle(OmegaGpuEwOp op, int math, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out) {
71 for (uint32_t i0 = 0; i0 < n; i0 += CHUNK) {
72 uint32_t ni = n - i0 < CHUNK ? n - i0 : CHUNK;
73 uint32_t threads = ((ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS) * OMEGA_GPU_EW_THREADS;
74 uint32_t *r = malloc((size_t)threads * 4);
75 for (uint32_t t = 0; t < threads; t++) {
76 uint32_t idx = t < ni ? t : ni - 1, pt = t ^ 1u, pidx = pt < ni ? pt : ni - 1;
77 r[t] = rc_r(op, math, a[i0 + idx], b[i0 + idx], a[i0 + pidx]);
78 }
79 for (uint32_t i = 0; i < ni; i++) { uint32_t nb = (i & 31u) == 31u ? i : i + 1u; out[i0 + i] = r[i] + 4099u * r[nb]; }
80 free(r);
81 }
82}
83/* An LCG's low bits cycle quickly (bit 0 has period 2), and the probes key on low bits
84 * (x & 255, y & 15, x & 1), so each draw goes through a mixing step. */
85static uint32_t mix(uint32_t r) { r ^= r >> 16; r *= 0x45d9f3bu; r ^= r >> 16; r *= 0x45d9f3bu; r ^= r >> 16; return r; }
86static void fill(uint32_t *a, uint32_t *b, uint32_t n, uint32_t seed) {
87 for (uint32_t i = 0; i < n; i++) { a[i] = mix(lcg(&seed)); b[i] = mix(lcg(&seed)); }
88}
89static size_t diff_count(const uint32_t *got, const uint32_t *want, uint32_t n, uint32_t *first) {
90 size_t bad = 0; *first = n;
91 for (uint32_t i = 0; i < n; i++) if (got[i] != want[i]) { if (!bad) *first = i; bad++; }
92 return bad;
93}
94
95/* --------------------------------------------------------- simulator driver */
96static uint8_t g_smem[1024];
97/* Mirrors run_1d: 64 CTAs x 128 threads per launch, P0 = words in the chunk. Returns the
98 * simulator's error code (0 clean); the output words are written to out. */
99static int ws_probe(const BlackwellIRProgram *p, uint32_t n, const uint32_t *a, const uint32_t *b, uint32_t *out, int order, WsSim *stats) {
100 for (uint32_t i = 0; i < n; i++) out[i] = 0xffbadbadu;
101 uint64_t wi = 0, li = 0, sp = 0; int mf = 0, pb = 0;
102 for (uint32_t i0 = 0; i0 < n; i0 += CHUNK) {
103 uint32_t ni = n - i0 < CHUNK ? n - i0 : CHUNK;
104 WsSim s; memset(&s, 0, sizeof s);
105 s.p = p; s.threads = OMEGA_GPU_EW_THREADS; s.smem = g_smem; s.smem_bytes = sizeof g_smem; s.order = order;
106 uint64_t aa = (uintptr_t)(a + i0), ba = (uintptr_t)(b + i0), ca = (uintptr_t)(out + i0);
107 s.cbank[0x380 / 4] = (uint32_t)aa; s.cbank[0x380 / 4 + 1] = (uint32_t)(aa >> 32);
108 s.cbank[0x388 / 4] = (uint32_t)ba; s.cbank[0x388 / 4 + 1] = (uint32_t)(ba >> 32);
109 s.cbank[0x390 / 4] = (uint32_t)ca; s.cbank[0x390 / 4 + 1] = (uint32_t)(ca >> 32);
110 s.cbank[0x398 / 4] = ni;
111 s.ranges[0] = (WsRange){ (uintptr_t)aa, (uintptr_t)aa + (size_t)ni * 4 };
112 s.ranges[1] = (WsRange){ (uintptr_t)ba, (uintptr_t)ba + (size_t)ni * 4 };
113 s.ranges[2] = (WsRange){ (uintptr_t)ca, (uintptr_t)ca + (size_t)ni * 4 };
114 s.nranges = 3;
115 int err = ws_run(&s, (ni + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS, 1);
116 wi += s.warp_issues; li += s.lane_issues; sp += s.splits;
117 if (s.max_frags > mf) mf = s.max_frags;
118 if (s.peak_bars > pb) pb = s.peak_bars;
119 if (err) { if (stats) *stats = s; return err; }
120 }
121 if (stats) { memset(stats, 0, sizeof *stats); stats->warp_issues = wi; stats->lane_issues = li; stats->splits = sp; stats->max_frags = mf; stats->peak_bars = pb; }
122 return 0;
123}
124static int codegen(OmegaGpuEwOp op, int mutant, BlackwellIRProgram *p, OmegaBlackwellKernel *k) {
125 memset(k, 0, sizeof *k);
126 return omega_gpu_elementwise_codegen_ir(op, mutant, p, k);
127}
128static int is_reconv_err(int e) {
129 return e == WS_E_SHFL_SPLIT || e == WS_E_BAR_SPLIT || e == WS_E_STUCK || e == WS_E_NEVER_JOINED || e == WS_E_JOIN_MISMATCH ||
130 e == WS_E_BSYNC_UNARMED || e == WS_E_BSSY_REARM || e == WS_E_NONMEMBER || e == WS_E_SHFL_EXITED;
131}
132
133/* --------------------------------------------------------- static walks */
134/* Scoreboard walk (as bench/prime_race/tests/gb10_sieve_host_test.c sb_hazards): a destination
135 * written under write scoreboard 0 is in flight until a wait mask that includes it. */
136static int sb_hazards(const BlackwellIRProgram *p) {
137 int inflight[64], n = 0, bad = 0;
138 for (size_t i = 0; i < p->count; i++) {
139 const BlackwellIRInsn *in = &p->insns[i];
140 uint32_t c = in->control;
141 if ((c >> 20) & 1u) n = 0;
142 const int src[3] = { in->src1_vreg, in->src2_vreg, in->src3_vreg };
143 for (int s = 0; s < 3; s++) for (int k = 0; src[s] >= 0 && k < n; k++) if (inflight[k] == src[s]) bad++;
144 if (c != 0 && ((c >> 14) & 7u) == 0 && in->dst_vreg >= 0 && n < 64) inflight[n++] = in->dst_vreg;
145 }
146 return bad;
147}
148/* Control-edge walk: the straight-line scoreboard reasoning above holds only if nothing is in
149 * flight across a branch, barrier op or exit. Every BRA / EXIT / BSSY / BSYNC must wait on every
150 * scoreboard set since the last wait. Returns the number of control ops that do not. */
151static int edge_waits(const BlackwellIRProgram *p) {
152 uint32_t pending = 0; int bad = 0;
153 for (size_t i = 0; i < p->count; i++) {
154 const BlackwellIRInsn *in = &p->insns[i];
155 uint32_t c = in->control, wait = (c >> 20) & 63u, wb = (c >> 14) & 7u, rb = (c >> 17) & 7u;
156 pending &= ~wait;
157 if (in->op == BW_IR_BRA || in->op == BW_IR_EXIT || in->op == BW_IR_BSSY || in->op == BW_IR_BSYNC) { if (pending) bad++; }
158 if (wb != 7u) pending |= 1u << wb;
159 if (rb != 7u) pending |= 1u << rb;
160 }
161 return bad;
162}
163static int count_op(const BlackwellIRProgram *p, BlackwellIROpcode op, int bar) {
164 int n = 0;
165 for (size_t i = 0; i < p->count; i++) if (p->insns[i].op == op && (bar < 0 || p->insns[i].bar_reg == bar)) n++;
166 return n;
167}
168static int find_op(const BlackwellIRProgram *p, BlackwellIROpcode op, int nth) {
169 for (size_t i = 0; i < p->count; i++) if (p->insns[i].op == op && nth-- == 0) return (int)i;
170 return -1;
171}
172
173/* --------------------------------------------------------- nvdisasm listing */
174static int nvdisasm_bytes(const uint8_t *code, size_t bytes, const char *name, const char *const *must, int n_must) {
175 const char *nv = "/usr/local/cuda/bin/nvdisasm";
176 if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check skipped for %s\n", name); return 0; }
177 char bin[96], lst[96], cmd[320];
178 snprintf(bin, sizeof bin, "/tmp/omega_rc_%s_%d.bin", name, (int)getpid());
179 snprintf(lst, sizeof lst, "/tmp/omega_rc_%s_%d.sass", name, (int)getpid());
180 FILE *f = fopen(bin, "wb"); if (!f) return -1;
181 fwrite(code, 1, bytes, f); fclose(f);
182 snprintf(cmd, sizeof cmd, "%s -b SM121 %s > %s 2>&1", nv, bin, lst);
183 int rc = system(cmd);
184 FILE *l = fopen(lst, "r"); if (!l) return -1;
185 char line[512]; int seen[16] = {0}, bad = 0, insns = 0;
186 while (fgets(line, sizeof line, l)) {
187 if (!strstr(line, "/*0")) continue;
188 insns++;
189 if (strstr(line, "?") || strstr(line, "error")) bad++;
190 for (int i = 0; i < n_must && i < 16; i++) if (strstr(line, must[i])) seen[i] = 1;
191 }
192 fclose(l);
193 int miss = 0;
194 for (int i = 0; i < n_must && i < 16; i++) if (!seen[i]) { printf(" %s: nvdisasm listing lacks %s\n", name, must[i]); miss++; }
195 printf("nvdisasm %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst);
196 unlink(bin);
197 return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1;
198}
199static int nvdisasm_kernel(OmegaGpuEwOp op, int mutant, const char *name, const char *const *must, int n_must) {
200 BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k;
201 if (!p || codegen(op, mutant, p, &k) != OMEGA_GPU_EW_OK) { free(p); return -1; }
202 int rc = nvdisasm_bytes(k.code, k.code_size, name, must, n_must);
203 free(k.code); free(p);
204 return rc;
205}
206
207/* --------------------------------------------------------- host part */
208typedef struct { const char *name; uint64_t warp_issues, lane_issues, splits; int max_frags, peak_bars; } Model;
209static Model g_model[NPROBES * 2]; static int g_nmodel;
210
211static int host_part(void) {
212 int before = g_failed;
213 /* 1. encoder golden words (nvcc 13.0.88 -arch=sm_121 oracle, docs/gpu-reconvergence-308.md) */
214 int fx = omega_blackwell_verify_codegen_fixtures_reconv();
215 CHECK(fx == 0, "encoder golden words BSSY / BSYNC and refusals (rc %d)", fx);
216
217 /* 2. region bookkeeping on a hand-built program */
218 {
219 BlackwellIRProgram *p = calloc(1, sizeof *p); BwRegions r; memset(&r, 0, sizeof r);
220 omega_bw_ir_init(p);
221 BlackwellIRInsn in; memset(&in, 0, sizeof in); in.dst_vreg = in.src1_vreg = in.src2_vreg = in.src3_vreg = -1;
222 in.op = BW_IR_BSSY; in.bar_reg = 0; in.imm = 2; omega_bw_ir_append(p, &in); /* 0 */
223 CHECK(bw_regions_next_bar(&r) == 0 && bw_regions_begin(&r, 0) == 0, "region begin returns B0");
224 in.op = BW_IR_BRA; in.imm = 1; in.predicate_p0 = true; omega_bw_ir_append(p, &in); /* 1 */
225 CHECK(bw_regions_exit(&r, 1) == 0, "region exit recorded");
226 in.op = BW_IR_NOP; in.predicate_p0 = false; omega_bw_ir_append(p, &in); /* 2 */
227 in.op = BW_IR_BSYNC; in.bar_reg = 0; omega_bw_ir_append(p, &in); /* 3 */
228 CHECK(bw_regions_join(&r, p, 3) == 0, "region join returns B0");
229 CHECK(p->insns[0].imm == 4 && p->insns[1].imm == 2, "join patched BSSY to the insn after BSYNC (%u) and the exit to the BSYNC (%u)", p->insns[0].imm, p->insns[1].imm);
230 CHECK(bw_regions_join(&r, p, 3) == -1, "join with no open region refused");
231 CHECK(bw_regions_exit(&r, 1) == -1, "exit with no open region refused");
232 /* exits table full */
233 memset(&r, 0, sizeof r); bw_regions_begin(&r, 0);
234 int ok = 1; for (int i = 0; i < BW_RECONV_MAX_EXITS; i++) ok &= bw_regions_exit(&r, 1) == 0;
235 CHECK(ok && bw_regions_exit(&r, 1) == -1, "exit table bound (%d) fails closed", BW_RECONV_MAX_EXITS);
236 /* join index inconsistent: BSYNC before BSSY, wrong barrier */
237 memset(&r, 0, sizeof r); bw_regions_begin(&r, 3);
238 CHECK(bw_regions_join(&r, p, 0) == -1, "join with BSSY after BSYNC refused");
239 memset(&r, 0, sizeof r); bw_regions_begin(&r, 0); p->insns[3].bar_reg = 1;
240 CHECK(bw_regions_join(&r, p, 3) == -1, "join with the wrong barrier register refused");
241 p->insns[3].bar_reg = 0;
242 /* depth bound */
243 memset(&r, 0, sizeof r); ok = 1;
244 for (int d = 0; d < BW_RECONV_MAX_DEPTH; d++) ok &= bw_regions_next_bar(&r) == d && bw_regions_begin(&r, d) == d;
245 CHECK(ok && bw_regions_next_bar(&r) == -1 && bw_regions_begin(&r, 99) == -1, "nesting bound %d: the next begin fails closed", BW_RECONV_MAX_DEPTH);
246 free(p);
247 }
248
249 /* 3. codegen: production probes build, refusals refuse, mutants differ */
250 static const struct { OmegaGpuEwOp op; const char *name; } refuse[] = {
251 { OMEGA_GPU_EW_RC_REFUSE_DEPTH17, "17 nested regions" }, { OMEGA_GPU_EW_RC_REFUSE_EXIT_SHFL, "EXIT then BAR/SHFL" },
252 { OMEGA_GPU_EW_RC_REFUSE_EXIT_IN_REGION, "EXIT inside a region" }, { OMEGA_GPU_EW_RC_REFUSE_BAR_IN_REGION, "BAR.SYNC inside a region" },
253 { OMEGA_GPU_EW_RC_REFUSE_SHFL_IN_REGION, "SHFL inside a region" }, { OMEGA_GPU_EW_RC_REFUSE_UNCLOSED, "region never joined" },
254 { OMEGA_GPU_EW_RC_REFUSE_JOIN_ONLY, "BSYNC with no BSSY" },
255 };
256 for (size_t i = 0; i < sizeof refuse / sizeof refuse[0]; i++) {
257 BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k;
258 int rc = codegen(refuse[i].op, 0, p, &k);
259 CHECK(rc == OMEGA_GPU_EW_CODEGEN_FAIL, "%s refused at codegen (rc %s)", refuse[i].name, omega_gpu_elementwise_rc_name(rc));
260 if (rc == OMEGA_GPU_EW_OK) free(k.code);
261 free(p);
262 }
263 for (size_t i = 0; i < NPROBES; i++) {
264 BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k;
265 int rc = codegen(PROBES[i].op, 0, p, &k);
266 CHECK(rc == OMEGA_GPU_EW_OK && k.gpr_count <= 64, "%s builds (rc %s, %zu insns, %u gprs)", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), k.insn_count, k.gpr_count);
267 if (rc != OMEGA_GPU_EW_OK) { free(p); continue; }
268 int nb = count_op(p, BW_IR_BSSY, -1), ns = count_op(p, BW_IR_BSYNC, -1);
269 int want = PROBES[i].op == OMEGA_GPU_EW_RC_NESTED ? 2 : PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16 ? BW_RECONV_MAX_BAR : 1;
270 CHECK(nb == want && ns == want, "%s has %d BSSY and %d BSYNC (want %d)", PROBES[i].name, nb, ns, want);
271 if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) {
272 int each = 1; for (int b = 0; b < BW_RECONV_MAX_BAR; b++) each &= count_op(p, BW_IR_BSSY, b) == 1 && count_op(p, BW_IR_BSYNC, b) == 1;
273 CHECK(each, "depth16 uses every barrier register B0..B15 exactly once");
274 }
275 CHECK(sb_hazards(p) == 0, "%s scoreboard walk clean", PROBES[i].name);
276 CHECK(edge_waits(p) == 0, "%s every control op waits on everything in flight", PROBES[i].name);
277 printf("codegen %s: %zu insns, %u gprs, %d regions\n", PROBES[i].name, p->count, k.gpr_count, nb);
278 uint8_t d0[32]; memcpy(d0, k.code_digest, 32); free(k.code);
279 for (int m = 1; m <= 3; m++) {
280 BlackwellIRProgram *mp = calloc(1, sizeof *mp); OmegaBlackwellKernel mk;
281 int mrc = codegen(PROBES[i].op, m, mp, &mk);
282 int same_ok = m == 1 && PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16; /* depth16 has no math mutant */
283 CHECK(mrc == OMEGA_GPU_EW_OK && ((memcmp(d0, mk.code_digest, 32) != 0) != same_ok), "%s mutant %d builds and %s (rc %s)", PROBES[i].name, m, same_ok ? "equals production" : "differs", omega_gpu_elementwise_rc_name(mrc));
284 if (mrc == OMEGA_GPU_EW_OK) {
285 if (m == 2) CHECK(count_op(mp, BW_IR_BSYNC, -1) == 0 && count_op(mp, BW_IR_BSSY, -1) == nb, "%s mutant 2 has no BSYNC", PROBES[i].name);
286 if (m == 3) CHECK(count_op(mp, BW_IR_BSYNC, -1) == ns, "%s mutant 3 keeps its BSYNCs", PROBES[i].name);
287 free(mk.code);
288 }
289 free(mp);
290 }
291 free(p);
292 }
293 /* the four regioned sieves build too (their sweep runs in gb10_sieve_host_test) */
294 for (int op = OMEGA_GPU_EW_PRIME_SIEVE_DIV1; op <= OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2; op++) {
295 BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k;
296 int rc = codegen((OmegaGpuEwOp)op, 0, p, &k);
297 int staged = op == OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1 || op == OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2;
298 int nb = rc == OMEGA_GPU_EW_OK ? count_op(p, BW_IR_BSSY, -1) : -1, want = (op >= OMEGA_GPU_EW_PRIME_SIEVE_DIV2 ? 2 : 1) + staged;
299 CHECK(rc == OMEGA_GPU_EW_OK && k.gpr_count <= 64 && nb == want && edge_waits(p) == 0, "sieve op %d builds with %d regions (rc %s, %u gprs)", op, nb, omega_gpu_elementwise_rc_name(rc), k.gpr_count);
300 if (rc == OMEGA_GPU_EW_OK) free(k.code);
301 free(p);
302 }
303
304 /* 4. nvdisasm: the words decode as BSSY/BSYNC on the real barrier registers, and the predicated
305 * and B15 forms the encoder can produce decode too (built by hand: encode_one) */
306 {
307 static const char *const m0[] = { "BSSY", "BSYNC", "B0", "SHFL.DOWN", "@P0 BRA" };
308 CHECK(nvdisasm_kernel(OMEGA_GPU_EW_RC_DIAMOND, 0, "diamond", m0, 5) == 0, "diamond listing decodes with BSSY/BSYNC B0");
309 static const char *const m1[] = { "BSSY", "BSYNC", "B15", "B7" };
310 CHECK(nvdisasm_kernel(OMEGA_GPU_EW_RC_DEPTH16, 0, "depth16", m1, 4) == 0, "depth16 listing decodes with B15");
311 static const char *const m2[] = { "BSSY", "B1", "BSYNC" };
312 CHECK(nvdisasm_kernel(OMEGA_GPU_EW_PRIME_SIEVE_DIV2, 0, "sieve_div2", m2, 3) == 0, "nested sieve listing decodes with B1");
313 uint8_t code[64]; uint32_t w[4]; BlackwellIRInsn in; memset(&in, 0, sizeof in); in.dst_vreg = in.src1_vreg = in.src2_vreg = in.src3_vreg = -1;
314 in.op = BW_IR_BSSY; in.bar_reg = 3; in.imm = 2; in.predicate_p0 = true; omega_blackwell_encode_one(&in, w); memcpy(code, w, 16);
315 in.op = BW_IR_BSYNC; in.bar_reg = 3; in.predicate_p0 = true; in.predicate_not = true; omega_blackwell_encode_one(&in, w); memcpy(code + 16, w, 16);
316 in.op = BW_IR_BSYNC; in.bar_reg = 15; in.predicate_p0 = false; in.predicate_not = false; omega_blackwell_encode_one(&in, w); memcpy(code + 32, w, 16);
317 in.op = BW_IR_EXIT; omega_blackwell_encode_one(&in, w); memcpy(code + 48, w, 16);
318 static const char *const m3[] = { "@P0 BSSY", "B3", "@!P0 BSYNC", "BSYNC.RECONVERGENT B15" };
319 CHECK(nvdisasm_bytes(code, 64, "forms", m3, 4) == 0, "predicated BSSY/BSYNC and B15 decode");
320 }
321
322 /* 5. simulator: every probe, every n, both fragment orders, exact against the oracle */
323 uint32_t *a = malloc(MAXN * 4), *b = malloc(MAXN * 4), *got = malloc(MAXN * 4), *want = malloc(MAXN * 4);
324 for (size_t i = 0; i < NPROBES; i++) {
325 BlackwellIRProgram *p = calloc(1, sizeof *p); OmegaBlackwellKernel k;
326 if (codegen(PROBES[i].op, 0, p, &k) != OMEGA_GPU_EW_OK) { free(p); continue; }
327 int fails = 0; uint64_t cases = 0;
328 for (size_t j = 0; j < NNS; j++) for (int order = 0; order < 2; order++) {
329 uint32_t n = NS[j]; fill(a, b, n, 0x308u + n * 7u + (uint32_t)order);
330 rc_oracle(PROBES[i].op, 0, n, a, b, want);
331 WsSim st; int err = ws_probe(p, n, a, b, got, order, &st);
332 uint32_t first; size_t bad = diff_count(got, want, n, &first);
333 cases++;
334 if (err || bad) { fails++; printf(" %s n=%u order=%d: sim %s, %zu words differ (first %u: got %08x want %08x)\n", PROBES[i].name, n, order, err ? st.msg : "clean", bad, first, first < n ? got[first] : 0, first < n ? want[first] : 0); }
335 if (n == 4096) { Model *m = &g_model[g_nmodel++]; m->name = PROBES[i].name; m->warp_issues = st.warp_issues; m->lane_issues = st.lane_issues; m->splits = st.splits; m->max_frags = st.max_frags; m->peak_bars = st.peak_bars; }
336 }
337 CHECK(fails == 0, "%s simulator vs oracle: %d of %" PRIu64 " cases differ", PROBES[i].name, fails, cases);
338 /* math mutant: simulator clean, result wrong (the oracle sees it) */
339 BlackwellIRProgram *mp = calloc(1, sizeof *mp); OmegaBlackwellKernel mk;
340 if (codegen(PROBES[i].op, 1, mp, &mk) == OMEGA_GPU_EW_OK) {
341 uint32_t n = 1000; fill(a, b, n, 0x1001u); rc_oracle(PROBES[i].op, 0, n, a, b, want);
342 WsSim st; int err = ws_probe(mp, n, a, b, got, 0, &st); uint32_t first; size_t bad = diff_count(got, want, n, &first);
343 if (PROBES[i].op == OMEGA_GPU_EW_RC_EXIT_BAR) CHECK(err == WS_E_SHFL_EXITED || err == WS_E_BAR_SPLIT, "R6 early-exit kernel: simulator reports SHFL_EXITED (got %s)", st.msg);
344 else if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) CHECK(err == 0 && bad == 0, "depth16 'mutant 1' is the production kernel: clean and exact (err %d bad %zu)", err, bad);
345 else CHECK(err == 0 && bad > 0, "%s math mutant: simulator clean, %zu words differ (err %d)", PROBES[i].name, bad, err);
346 free(mk.code);
347 }
348 free(mp);
349 /* dropped join and corrupted target: the simulator must reject both, in both orders */
350 for (int m = 2; m <= 3; m++) for (int order = 0; order < 2; order++) {
351 BlackwellIRProgram *xp = calloc(1, sizeof *xp); OmegaBlackwellKernel xk;
352 if (codegen(PROBES[i].op, m, xp, &xk) == OMEGA_GPU_EW_OK) {
353 uint32_t n = 1000; fill(a, b, n, 0x2002u + (uint32_t)m);
354 WsSim st; int err = ws_probe(xp, n, a, b, got, order, &st);
355 CHECK(is_reconv_err(err), "%s mutant %d (%s) order %d rejected by the simulator: %s", PROBES[i].name, m, m == 2 ? "BSYNC dropped" : "BSSY target corrupted", order, err ? st.msg : "NOT CAUGHT");
356 if (m == 3) CHECK(err == WS_E_JOIN_MISMATCH || err == WS_E_STUCK, "%s corrupted target shows as JOIN_MISMATCH or STUCK (%s)", PROBES[i].name, ws_err_name(err));
357 free(xk.code);
358 }
359 free(xp);
360 }
361 free(k.code); free(p);
362 }
363 /* 6. hand mutations of the production diamond (controls on the simulator itself, independent of
364 * the emitter's mutant hooks): exit jumping past the BSYNC, BSYNC on the wrong barrier, a BSSY
365 * on an armed barrier. And the unmodified program as the positive control. */
366 {
367 BlackwellIRProgram *p = calloc(1, sizeof *p), *q = calloc(1, sizeof *q); OmegaBlackwellKernel k;
368 if (codegen(OMEGA_GPU_EW_RC_DIAMOND, 0, p, &k) == OMEGA_GPU_EW_OK) {
369 uint32_t n = 500; fill(a, b, n, 0x3003u); WsSim st;
370 int bssy = find_op(p, BW_IR_BSSY, 0), bsync = find_op(p, BW_IR_BSYNC, 0), ex = -1;
371 for (int i = bssy + 1; i < bsync; i++) if (p->insns[i].op == BW_IR_BRA && !p->insns[i].predicate_p0) ex = i;
372 CHECK(bssy >= 0 && bsync > bssy && ex > bssy, "diamond shape: BSSY %d, exit %d, BSYNC %d", bssy, ex, bsync);
373 CHECK(ws_probe(p, n, a, b, got, 0, &st) == 0, "positive control: untouched diamond runs clean");
374 *q = *p; q->insns[ex].imm += 1; /* exit lands after the BSYNC */
375 int e1 = ws_probe(q, n, a, b, got, 0, &st); CHECK(is_reconv_err(e1), "exit past the BSYNC rejected: %s", st.msg);
376 *q = *p; q->insns[bsync].bar_reg = 1; /* BSYNC on a barrier nobody armed */
377 int e2 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e2 == WS_E_BSYNC_UNARMED, "BSYNC on an unarmed barrier rejected: %s", st.msg);
378 *q = *p; q->insns[bsync].op = BW_IR_NOP; /* join dropped by hand */
379 int e3 = ws_probe(q, n, a, b, got, 0, &st); CHECK(is_reconv_err(e3), "join replaced by NOP rejected: %s", st.msg);
380 *q = *p; q->insns[bssy].imm += 1; /* names the wrong join */
381 int e4 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e4 == WS_E_JOIN_MISMATCH, "wrong join target rejected: %s", st.msg);
382 *q = *p; q->insns[bssy + 1] = p->insns[bssy]; /* second BSSY on the armed barrier (overwrites the ISETP) */
383 int e5 = ws_probe(q, n, a, b, got, 0, &st); CHECK(e5 == WS_E_BSSY_REARM, "BSSY on an armed barrier rejected: %s", st.msg);
384 free(k.code);
385 }
386 free(p); free(q);
387 }
388 free(a); free(b); free(got); free(want);
389 for (int i = 0; i < g_nmodel; i++) printf("model %s n=4096: warp_issues=%" PRIu64 " lane_util=%.3f splits=%" PRIu64 " max_frags=%d peak_bars=%d\n", g_model[i].name, g_model[i].warp_issues, g_model[i].warp_issues ? (double)g_model[i].lane_issues / (32.0 * (double)g_model[i].warp_issues) : 0.0, g_model[i].splits, g_model[i].max_frags, g_model[i].peak_bars);
390 return g_failed == before ? 0 : -1;
391}
392
393/* --------------------------------------------------------- chip part */
394static int chip(const char *out_path, int observe) {
395 FILE *out = out_path ? fopen(out_path, "w") : NULL;
396 if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; }
397 if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_RECONV_V1\",\"issue\":308,\"comparison\":\"exact u32\",\"cases\":[");
398 int first = 1;
399 uint32_t *a = malloc(MAXN * 4), *b = malloc(MAXN * 4), *got = malloc(MAXN * 4), *want = malloc(MAXN * 4);
400 for (size_t i = 0; i < NPROBES; i++) {
401 for (size_t j = 0; j < NNS; j++) {
402 uint32_t n = NS[j]; fill(a, b, n, 0x5050u + n);
403 rc_oracle(PROBES[i].op, 0, n, a, b, want);
404 OmegaGpuEwInfo info; memset(&info, 0, sizeof info);
405 int rc = omega_gpu_reconv_probe_u32(PROBES[i].op, n, a, b, got, &info);
406 uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n;
407 int pass = rc == OMEGA_GPU_EW_OK && bad == 0;
408 CHECK(pass, "%s n=%u chip rc=%s violations=%zu err=\"%s\"", PROBES[i].name, n, omega_gpu_elementwise_rc_name(rc), bad, omega_gpu_elementwise_last_error());
409 if (out) fprintf(out, "%s{\"op\":\"%s\",\"regression\":\"%s\",\"n\":%u,\"rc\":\"%s\",\"violations\":%zu,\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 ",\"chip_calls\":%u,\"unwritten_words\":%u,\"cache_hit\":%s}",
410 first ? "" : ",", PROBES[i].name, PROBES[i].reg, n, omega_gpu_elementwise_rc_name(rc), bad, pass ? "true" : "false", info.elapsed_ns, info.chip_calls, info.unwritten_words, info.kernel_cache_hit ? "true" : "false");
411 first = 0;
412 if (!pass) { printf("STOP: %s failed on the chip at n=%u; no further launches of this probe\n", PROBES[i].name, n); break; }
413 }
414 }
415 if (out) fprintf(out, "],\"mutants\":[");
416 first = 1;
417 for (size_t i = 0; i < NPROBES; i++) {
418 if (PROBES[i].op == OMEGA_GPU_EW_RC_DEPTH16) continue; /* no math mutant */
419 uint32_t n = 1000; fill(a, b, n, 0x6060u); rc_oracle(PROBES[i].op, 0, n, a, b, want);
420 omega_gpu_elementwise_test_set_mutant((int)PROBES[i].op);
421 /* R6's math mutant is the early-exit kernel: the emitter refuses it on the launch path
422 * (the bypass exists only for codegen_ir), so the catch is CODEGEN_FAIL: no undefined
423 * SHFL ever reaches the chip. */
424 OmegaGpuEwInfo info; memset(&info, 0, sizeof info);
425 int rc = omega_gpu_reconv_probe_u32(PROBES[i].op, n, a, b, got, &info);
426 uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n;
427 int caught = rc != OMEGA_GPU_EW_OK || bad > 0;
428 omega_gpu_elementwise_test_set_mutant(0);
429 CHECK(caught, "%s math mutant caught (rc %s, violations %zu)", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), bad);
430 if (PROBES[i].op == OMEGA_GPU_EW_RC_EXIT_BAR) CHECK(rc == OMEGA_GPU_EW_CODEGEN_FAIL, "R6 early-exit form refused at codegen on the launch path (rc %s)", omega_gpu_elementwise_rc_name(rc));
431 if (out) fprintf(out, "%s{\"op\":\"%s\",\"mutant\":1,\"rc\":\"%s\",\"violations\":%zu,\"caught\":%s}", first ? "" : ",", PROBES[i].name, omega_gpu_elementwise_rc_name(rc), bad, caught ? "true" : "false");
432 first = 0;
433 }
434 if (out) fprintf(out, "],\"observations\":[");
435 if (observe) {
436 /* The dropped-join kernel (rejected by the simulator above) on the chip once, small n:
437 * recorded as an observation of what the hardware does without the BSYNC. Not a gate. */
438 uint32_t n = 1000; fill(a, b, n, 0x7070u); rc_oracle(OMEGA_GPU_EW_RC_DIAMOND, 0, n, a, b, want);
439 omega_gpu_elementwise_test_set_mutant_level(OMEGA_GPU_EW_RC_DIAMOND, 2);
440 OmegaGpuEwInfo info; memset(&info, 0, sizeof info);
441 int rc = omega_gpu_reconv_probe_u32(OMEGA_GPU_EW_RC_DIAMOND, n, a, b, got, &info);
442 uint32_t fi; size_t bad = rc == OMEGA_GPU_EW_OK ? diff_count(got, want, n, &fi) : n;
443 omega_gpu_elementwise_test_set_mutant(0);
444 printf("observation: diamond with the BSYNC dropped, n=%u: rc=%s, %zu of %u words differ from the oracle (first %u)\n", n, omega_gpu_elementwise_rc_name(rc), bad, n, fi);
445 if (out) fprintf(out, "{\"op\":\"rc_diamond\",\"mutant\":2,\"what\":\"BSYNC dropped\",\"n\":%u,\"rc\":\"%s\",\"violations\":%zu,\"first_bad\":%u,\"gate\":false}", n, omega_gpu_elementwise_rc_name(rc), bad, fi);
446 }
447 CHECK(omega_gpu_session_open_count() == 1, "device opened once for the battery (opens=%u)", omega_gpu_session_open_count());
448 if (out) {
449 fprintf(out, "],\"sim_model_n4096\":[");
450 for (int i = 0; i < g_nmodel; i++) fprintf(out, "%s{\"op\":\"%s\",\"order\":%d,\"warp_issues\":%" PRIu64 ",\"lane_issues\":%" PRIu64 ",\"splits\":%" PRIu64 ",\"max_frags\":%d,\"peak_bars\":%d}", i ? "," : "", g_model[i].name, i & 1, g_model[i].warp_issues, g_model[i].lane_issues, g_model[i].splits, g_model[i].max_frags, g_model[i].peak_bars);
451 fprintf(out, "],\"device_opens\":%u,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", omega_gpu_session_open_count(), g_checks, g_failed, g_failed == 0 ? "OMEGA_GPU_RECONV_PASS" : "OMEGA_GPU_RECONV_FAIL");
452 fclose(out);
453 }
454 free(a); free(b); free(got); free(want);
455 return 0;
456}
457
458int main(int argc, char **argv) {
459 const char *out_path = NULL; int host = 0, observe = 0;
460 for (int i = 1; i < argc; i++) {
461 if (strcmp(argv[i], "--host-only") == 0) host = 1;
462 else if (strcmp(argv[i], "--observe-dropped-join") == 0) observe = 1;
463 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i];
464 }
465 int hrc = host_part();
466 if (!host) {
467 if (hrc != 0) { printf("host part failed: no chip launches\nFAIL: %d checks, %d failed\n", g_checks, g_failed); return 1; }
468 if (chip(out_path, observe) != 0) return 2;
469 }
470 printf("%s: %d checks, %d failed\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed);
471 return g_failed ? 1 : 0;
472}