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 / bench / prime_race / tests / gb10_sieve_host_test.c

821 lines · 49863 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* Host tests for the native GB10 prime sieve (no device is opened).
18 *
19 * gb10_sieve_host_test golden words of the three new ops, codegen budget, sm121dis
20 * listing + every branch target, then the allocated IR program
21 * run by a host IR simulator over the limits sweep against an
22 * independent Eratosthenes oracle; the mutant must be caught
23 * gb10_sieve_host_test --big also 1e7 through the simulator (exercises the magic correction)
24 * gb10_sieve_host_test --limits print the DESIGN limits sweep, one per line (chip script input)
25 * gb10_sieve_host_test --oracle LIMIT PATH [K]
26 * write the canonical bitmap of LIMIT (K copies, default 1)
27 *
28 * Simulator: executes the post-allocation IR (physical registers, so the loop liveness
29 * fix of the register allocator is under test), one thread at a time (the kernel has no
30 * barrier or shuffle), host pointers as addresses with a bounds check on every access.
31 * It models the ISA as the IR documents it; it is not chip evidence.
32 */
33#include "prime_race_impl.h"
34#include "omega_blackwell_codegen.h"
35#include "omega_gpu_elementwise_api.h"
36#include "omega_sm121_disasm.h"
37#include "bw_warp_sim.h" /* SIMT warp simulator (omega #308): the regioned sieves run through it */
38#include <inttypes.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
43
44static int g_checks, g_failed;
45#define CHECK(c, ...) do { g_checks++; if (!(c)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
46
47/* ------------------------------------------------------------- oracle */
48/* canonical words for limit: bit k set iff 2k+1 prime (plain Eratosthenes on odd numbers) */
49static uint64_t *oracle_bitmap(uint64_t limit) {
50 uint64_t odd = pr_odd_count(limit);
51 size_t nw = pr_words64(limit);
52 uint64_t *w = calloc(nw ? nw : 1, 8);
53 uint8_t *comp = calloc(odd ? odd : 1, 1);
54 if (!w || !comp) { free(w); free(comp); return NULL; }
55 if (odd) comp[0] = 1; /* 1 */
56 for (uint64_t i = 1; i < odd; i++) {
57 uint64_t p = 2 * i + 1;
58 if (comp[i]) continue;
59 if (p * p > 2 * odd) continue;
60 for (uint64_t c = p * p; (c - 1) / 2 < odd; c += 2 * p) comp[(c - 1) / 2] = 1;
61 }
62 for (uint64_t k = 0; k < odd; k++) if (!comp[k]) w[k / 64] |= 1ull << (k % 64);
63 free(comp);
64 return w;
65}
66
67static uint64_t count_primes(const uint64_t *w, uint64_t limit) {
68 uint64_t n = limit >= 2;
69 for (size_t i = 0; i < pr_words64(limit); i++) n += (uint64_t)__builtin_popcountll(w[i]);
70 return n;
71}
72
73/* ------------------------------------------------------------- limits sweep */
74static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y; }
75static size_t sweep_limits(uint64_t *out, size_t cap, int full) {
76 static const uint64_t fixed[] = { 0, 1, 2, 3, 4, 5, 8, 9, 15, 25, 31, 32, 33, 34, 49, 63, 64, 65, 66, 95, 96, 97,
77 121, 127, 128, 129, 130, 169, 255, 256, 257, 258, 289, 361, 961, 1023, 1024, 1025, 1999, 2000, 2001, 4095, 4096, 4097 };
78 /* words of 32 odd numbers: limit 64k has exactly 32k odd numbers. k = 128 is one CTA of
79 * words, k = 8192 one full grid-stride round (64 CTAs x 128 threads). */
80 static const uint64_t ks[] = { 1, 2, 3, 4, 127, 128, 129, 256, 8191, 8192, 8193, 16384 };
81 size_t n = 0;
82#define ADD(x) do { if (n < cap) out[n++] = (x); } while (0)
83 for (size_t i = 0; i < sizeof fixed / sizeof fixed[0]; i++) ADD(fixed[i]);
84 for (size_t i = 0; i < sizeof ks / sizeof ks[0]; i++) { ADD(64 * ks[i] - 1); ADD(64 * ks[i]); ADD(64 * ks[i] + 1); }
85 uint64_t pmax = full ? 997 : 113;
86 for (uint64_t p = 3; p <= pmax; p += 2) {
87 int prime = 1;
88 for (uint64_t d = 3; d * d <= p; d += 2) if (p % d == 0) { prime = 0; break; }
89 if (prime) { ADD(p * p - 1); ADD(p * p); ADD(p * p + 1); }
90 }
91 if (!full) { ADD(994009 - 1); ADD(994009); ADD(994009 + 1); } /* 997^2 */
92 for (uint64_t l = 999983; l <= 1000003; l++) if (full || l == 999983 || l == 1000000 || l == 1000003) ADD(l);
93#undef ADD
94 qsort(out, n, sizeof *out, cmp_u64);
95 size_t m = 0;
96 for (size_t i = 0; i < n; i++) if (m == 0 || out[i] != out[m - 1]) out[m++] = out[i];
97 return m;
98}
99
100/* ------------------------------------------------------------- simulator */
101typedef struct { uint32_t r[256]; int pc, p0, done; } Thr;
102static uint32_t g_cb[256]; /* constant bank 0 words 0..255 (byte offset / 4) */
103typedef struct { uintptr_t lo, hi; } Range;
104static Range g_ranges[2];
105static int g_sim_err;
106
107static uint32_t rd(const Thr *t, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; return t->r[p->regalloc.vreg_to_phys[v] & 0xff]; }
108static uint64_t rd64(const Thr *t, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v] & 0xff; return (uint64_t)t->r[ph] | ((uint64_t)t->r[ph + 1] << 32); }
109static void wr(Thr *t, int v, uint32_t x, const BlackwellIRProgram *p) { if (v >= 0) t->r[p->regalloc.vreg_to_phys[v] & 0xff] = x; }
110static void wr64(Thr *t, int v, uint64_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v] & 0xff; t->r[ph] = (uint32_t)x; t->r[ph + 1] = (uint32_t)(x >> 32); }
111static uint32_t *mem_at(uint64_t a) {
112 for (int i = 0; i < 2; i++) if (a >= g_ranges[i].lo && a + 4 <= g_ranges[i].hi && (a & 3) == 0) return (uint32_t *)(uintptr_t)a;
113 g_sim_err = 3; return NULL;
114}
115static uint32_t lut3(uint32_t lut, uint32_t a, uint32_t b, uint32_t c) {
116 uint32_t r = 0;
117 for (int i = 0; i < 8; i++) if (lut >> i & 1) r |= ((i & 4) ? a : ~a) & ((i & 2) ? b : ~b) & ((i & 1) ? c : ~c);
118 return r;
119}
120
121static uint64_t g_steps;
122/* Shared memory of the CTA being simulated (campaign C3). g_smem_bytes is what the launch
123 * declares; an access past it is an error. Race check: a word written in barrier phase k may
124 * not be read by a different thread in the same phase (no BAR_SYNC between them). */
125#define SIM_SMEM_WORDS 4096
126static uint32_t g_smem[SIM_SMEM_WORDS], g_smem_wphase[SIM_SMEM_WORDS], g_smem_wtid[SIM_SMEM_WORDS];
127static uint32_t g_smem_bytes, g_phase, g_smem_short; /* g_smem_short: negative control only */
128static uint64_t g_races;
129static uint32_t *smem_at(uint32_t a, uint32_t tid, int write) {
130 if ((a & 3) || a + 4 > g_smem_bytes || a / 4 >= SIM_SMEM_WORDS) { g_sim_err = 5; return NULL; }
131 uint32_t w = a / 4;
132 if (write) { g_smem_wphase[w] = g_phase + 1; g_smem_wtid[w] = tid; }
133 else if (g_smem_wphase[w] == g_phase + 1 && g_smem_wtid[w] != tid) { g_races++; g_sim_err = 6; return NULL; }
134 return &g_smem[w];
135}
136/* Runs one thread until EXIT (t->done = 1) or a BAR_SYNC (returns with pc past it). */
137static void sim_thread(const BlackwellIRProgram *p, Thr *tp, uint32_t tid, uint32_t cta) {
138 Thr t = *tp;
139 for (uint64_t guard = 0; guard < 400000000ull; guard++) {
140 if (t.pc < 0 || (size_t)t.pc >= p->count) { g_sim_err = 1; return; }
141 const BlackwellIRInsn *in = &p->insns[t.pc];
142 uint32_t a = rd(&t, in->src1_vreg, p), b = rd(&t, in->src2_vreg, p), c = rd(&t, in->src3_vreg, p);
143 g_steps++;
144 switch (in->op) {
145 case BW_IR_EXIT: if (!in->predicate_p0 || t.p0) { t.done = 1; *tp = t; return; } break;
146 case BW_IR_BRA: {
147 int take = !in->predicate_p0 || (in->predicate_not ? !t.p0 : t.p0);
148 if (take) { if ((int32_t)in->imm == 0) { g_sim_err = 2; return; } t.pc += (int32_t)in->imm; continue; }
149 break; }
150 case BW_IR_S2R: wr(&t, in->dst_vreg, in->imm == BW_SR_TID_X ? tid : in->imm == BW_SR_CTAID_X ? cta : 0, p); break;
151 case BW_IR_LDC: wr(&t, in->dst_vreg, g_cb[(in->imm / 4) & 0xff], p); break;
152 case BW_IR_LDC64: wr64(&t, in->dst_vreg, (uint64_t)g_cb[(in->imm / 4) & 0xff] | ((uint64_t)g_cb[(in->imm / 4 + 1) & 0xff] << 32), p); break;
153 case BW_IR_LDCU64: break; /* memory descriptor: not modelled */
154 case BW_IR_MOV_IMM: wr(&t, in->dst_vreg, in->imm, p); break;
155 case BW_IR_MOV_RZ: wr(&t, in->dst_vreg, 0, p); break;
156 case BW_IR_IMAD: wr(&t, in->dst_vreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break;
157 case BW_IR_IMAD_WIDE: wr64(&t, in->dst_vreg, (uint64_t)a * in->imm + rd64(&t, in->src3_vreg, p), p); break;
158 case BW_IR_IMAD_HI_U32: { /* hi32(a * b + Rc+1:Rc): a register Rc is a 64-bit pair (omega #492, nvcc oracle); odd Rc: err 9 */
159 uint64_t c64 = 0;
160 if (in->src3_vreg >= 0 && p->regalloc.vreg_to_phys[in->src3_vreg] >= 0) { if (p->regalloc.vreg_to_phys[in->src3_vreg] & 1) { g_sim_err = 9; return; } c64 = rd64(&t, in->src3_vreg, p); }
161 wr(&t, in->dst_vreg, (uint32_t)(((uint64_t)a * b + c64) >> 32), p); break; }
162 case BW_IR_IADD3: wr(&t, in->dst_vreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */
163 case BW_IR_SHF_R: wr(&t, in->dst_vreg, a >> (in->imm & 31), p); break;
164 case BW_IR_SHF_L_U32: if (b >= 32) { g_sim_err = 8; return; } wr(&t, in->dst_vreg, a << b, p); break;
165 case BW_IR_LOP3_XOR: wr(&t, in->dst_vreg, a ^ b, p); break;
166 case BW_IR_LOP3_LUT: wr(&t, in->dst_vreg, lut3(in->imm & 0xff, a, b, c), p); break;
167 case BW_IR_ISETP_GE_U32: t.p0 = a >= b; break;
168 case BW_IR_LDG_E: { uint32_t *m = mem_at(rd64(&t, in->src1_vreg, p)); if (!m) return; wr(&t, in->dst_vreg, *m, p); break; }
169 case BW_IR_STG_E: { uint32_t *m = mem_at(rd64(&t, in->src1_vreg, p)); if (!m) return; *m = b; break; }
170 case BW_IR_LDS32: { uint32_t *m = smem_at(a, tid, 0); if (!m) return; wr(&t, in->dst_vreg, *m, p); break; }
171 case BW_IR_STS32: { uint32_t *m = smem_at(a, tid, 1); if (!m) return; *m = b; break; }
172 case BW_IR_BAR_SYNC: t.pc++; *tp = t; return;
173 default: g_sim_err = 100 + (int)in->op; return;
174 }
175 t.pc++;
176 }
177 g_sim_err = 4;
178}
179
180/* One CTA: every thread runs to its next BAR_SYNC or EXIT, then the next barrier phase starts.
181 * Exited threads do not hold up a barrier (they are no longer counted). Shared memory starts
182 * poisoned, so a read of a word nobody staged gives a wrong answer the oracle sees. */
183static void sim_cta(const BlackwellIRProgram *p, uint32_t cta) {
184 static Thr th[OMEGA_GPU_EW_THREADS];
185 memset(th, 0, sizeof th);
186 for (int i = 0; i < SIM_SMEM_WORDS; i++) { g_smem[i] = 0xfeedfaceu; g_smem_wphase[i] = 0; }
187 for (g_phase = 0; !g_sim_err; g_phase++) {
188 int live = 0;
189 for (uint32_t tid = 0; tid < OMEGA_GPU_EW_THREADS && !g_sim_err; tid++)
190 if (!th[tid].done) { sim_thread(p, &th[tid], tid, cta); live += !th[tid].done; }
191 if (!live) return;
192 if (g_phase > 1000) { g_sim_err = 7; return; }
193 }
194}
195
196/* omega #308: when g_ws_order >= 0, sim_sieve runs the CTAs through the SIMT warp simulator
197 * (tests/bw_warp_sim.h: lane masks, divergence, BSSY/BSYNC) in that fragment order instead of
198 * the per-thread simulator above; a simulator error lands in g_sim_err as 200 + its code. */
199static int g_ws_order = -1;
200static WsSim g_ws_last;
201static uint8_t g_ws_smem[65536];
202static void ws_sieve_run(const BlackwellIRProgram *p, uint32_t ctas) {
203 WsSim s; memset(&s, 0, sizeof s);
204 s.p = p; s.threads = OMEGA_GPU_EW_THREADS; s.order = g_ws_order;
205 memcpy(s.cbank, g_cb, sizeof s.cbank);
206 s.smem = g_ws_smem; s.smem_bytes = g_smem_bytes > sizeof g_ws_smem ? (uint32_t)sizeof g_ws_smem : g_smem_bytes;
207 s.ranges[0] = (WsRange){ g_ranges[0].lo, g_ranges[0].hi }; s.ranges[1] = (WsRange){ g_ranges[1].lo, g_ranges[1].hi }; s.nranges = 2;
208 int err = ws_run(&s, ctas, 1);
209 g_ws_last = s;
210 g_steps += s.warp_issues;
211 if (err) g_sim_err = 200 + err;
212}
213
214/* Mirrors gb10_native.c's pass on the host: same table, same argument words; `budget`
215 * is the CTA cap (PR_GB10_CTA_BUDGET, default OMEGA_GPU_EW_MAX_CTAS). */
216static int sim_sieve(const BlackwellIRProgram *p, uint64_t limit, uint32_t budget, int staged, uint64_t *words) {
217 uint64_t odd = pr_odd_count(limit);
218 uint32_t nwords = (uint32_t)((odd + 31) / 32);
219 uint32_t blocks = (nwords + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS;
220 uint32_t ctas = blocks == 0 ? 1 : (blocks > budget ? budget : blocks);
221 uint32_t stride = ctas * OMEGA_GPU_EW_THREADS, lastw = nwords ? nwords - 1 : 0;
222 uint32_t valid = nwords ? (uint32_t)(odd - 32ull * lastw) : 32;
223 uint32_t tailinv = ~(valid >= 32 ? 0xffffffffu : ((1u << valid) - 1u));
224 uint32_t root = 0; while ((uint64_t)(root + 1) * (root + 1) <= limit) root++;
225 size_t cap = root / 2 + 2;
226 uint32_t *tab = calloc(cap * 3, 4), *out = calloc(nwords ? nwords : 1, 4);
227 if (!tab || !out) { free(tab); free(out); return -1; }
228 uint32_t n = 0;
229 for (uint32_t q = 3; q <= root; q += 2) {
230 int prime = 1;
231 for (uint32_t d = 3; d * d <= q; d += 2) if (q % d == 0) { prime = 0; break; }
232 if (!prime) continue;
233 tab[3 * n] = q; tab[3 * n + 1] = (uint32_t)(((1ull << 32) + q - 1) / q); tab[3 * n + 2] = (uint32_t)(((uint64_t)q * q - 1) / 2); n++;
234 }
235 tab[3 * n] = 1; tab[3 * n + 1] = 0; tab[3 * n + 2] = 0xffffffffu;
236 for (uint32_t i = 0; i < nwords; i++) out[i] = 0xffbadbadu;
237 memset(g_cb, 0, sizeof g_cb);
238 uint64_t ta = (uintptr_t)tab, oa = (uintptr_t)out;
239 g_cb[0x380 / 4] = (uint32_t)ta; g_cb[0x380 / 4 + 1] = (uint32_t)(ta >> 32);
240 g_cb[0x390 / 4] = (uint32_t)oa; g_cb[0x390 / 4 + 1] = (uint32_t)(oa >> 32);
241 g_cb[0x398 / 4] = nwords; g_cb[0x39c / 4] = stride; g_cb[0x3a0 / 4] = lastw; g_cb[0x3a4 / 4] = tailinv;
242 g_cb[0x3a8 / 4] = (n + 1) * 3; /* P4: table words (staged kernel) */
243 g_smem_bytes = staged ? (n + 1) * 12 - g_smem_short : 0; /* what gb10_native.c declares */
244 g_ranges[0] = (Range){ (uintptr_t)tab, (uintptr_t)tab + cap * 12 };
245 g_ranges[1] = (Range){ (uintptr_t)out, (uintptr_t)out + (size_t)nwords * 4 };
246 g_sim_err = 0;
247 if (g_ws_order >= 0) ws_sieve_run(p, ctas);
248 else for (uint32_t cta = 0; cta < ctas && !g_sim_err; cta++) sim_cta(p, cta);
249 size_t nw64 = pr_words64(limit);
250 memset(words, 0, nw64 * 8);
251 memcpy(words, out, (size_t)nwords * 4);
252 free(tab); free(out);
253 return g_sim_err ? -1 : 0;
254}
255
256/* ------------------------------------------------------------- scoreboard hazards */
257/* Campaign C2a: loads may now be in flight together, so check the waits the emitter wrote.
258 * Walking the IR in order, a destination of an instruction that sets write scoreboard 0
259 * (control bits 14..16, the emitter's SB_GPR) stays in flight until an instruction whose wait
260 * mask (bits 20..25) includes scoreboard 0; reading it earlier is a hazard. Straight-line walk:
261 * every branch the emitter writes waits on what is pending, so no value crosses one in flight. */
262static int sb_hazards(const BlackwellIRProgram *p, int verbose) {
263 int inflight[64], n = 0, bad = 0;
264 for (size_t i = 0; i < p->count; i++) {
265 const BlackwellIRInsn *in = &p->insns[i];
266 uint32_t c = in->control;
267 if ((c >> 20) & 1u) n = 0;
268 const int src[3] = { in->src1_vreg, in->src2_vreg, in->src3_vreg };
269 for (int s = 0; s < 3; s++)
270 for (int k = 0; src[s] >= 0 && k < n; k++)
271 if (inflight[k] == src[s]) { bad++; if (verbose) printf(" hazard: insn %zu reads vreg %d still in flight\n", i, src[s]); }
272 if (c != 0 && ((c >> 14) & 7u) == 0 && in->dst_vreg >= 0 && n < 64) inflight[n++] = in->dst_vreg;
273 }
274 return bad;
275}
276
277/* Fixed RAW checks use ENCODED delays, allocated registers and both successors
278 * of conditional branches, including back-edges. No credit is taken for extra
279 * scoreboard/memory delays. This covers the scalar ALU/FMA and predicate classes
280 * changed by the emitter; wide producers and variable latency remain the job
281 * of the existing schedule and scoreboard checker.
282 * Independent reference: NAK sm100/reg_raw.csv (reader rows, writer columns)
283 * plus sm120_instr_latencies.rs's +1. ALU/dual-ALU -> ALU/dual-ALU and FMA ->
284 * FMA are 5, mixed and address consumers at most 6 for these producers.
285 * ISETP -> guard is 14 via sm70.rs paw_latency, NOT the 5-cycle ISETP operand
286 * predicate latency. WAW/WAR for these scalar classes are <= 2, below our floor.
287 */
288static int latency_class(BlackwellIROpcode op) {
289 switch (op) {
290 case BW_IR_ISETP_GE_U32: case BW_IR_ISETP_GE: case BW_IR_FSETP: return 3;
291 case BW_IR_IMAD: case BW_IR_IMAD_HI_U32:
292 case BW_IR_FADD: case BW_IR_FSUB: case BW_IR_FMUL: case BW_IR_FFMA: return 2;
293 case BW_IR_MOV_IMM: case BW_IR_MOV_RZ: case BW_IR_IADD3:
294 case BW_IR_LOP3_LUT: case BW_IR_LOP3_XOR:
295 case BW_IR_SHF_R: case BW_IR_SHF_L_U32: return 1;
296 default: return 0;
297 }
298}
299
300static uint32_t encoded_control(const OmegaBlackwellKernel *k, size_t i) {
301 uint32_t c;
302 memcpy(&c, k->code + i * 16 + 12, sizeof c);
303 return c;
304}
305
306static int reg_contains(const BlackwellIRProgram *p, int v, unsigned sub, int phys) {
307 if (v < 0) return 0;
308 int first = p->regalloc.vreg_to_phys[v] + (int)sub;
309 /* Conservatively count the rest of a bundle as read/written. The changed
310 * producers are scalar; this includes both halves of address consumers. */
311 return phys >= first && phys < p->regalloc.vreg_to_phys[v] +
312 (int)p->regalloc.intervals[v].bundle_size;
313}
314
315static int relax_successors(const BlackwellIRProgram *p, size_t i, unsigned a,
316 unsigned *age) {
317 const BlackwellIRInsn *in = &p->insns[i];
318 int next[2], n = 0, changed = 0;
319 if (in->op == BW_IR_BRA) next[n++] = (int)i + (int32_t)in->imm;
320 if ((in->op != BW_IR_BRA && in->op != BW_IR_EXIT) || in->predicate_p0)
321 next[n++] = (int)i + 1;
322 for (int s = 0; s < n; s++) {
323 if (next[s] < 0 || (size_t)next[s] >= p->count) return -1;
324 if (a < age[next[s]]) { age[next[s]] = a; changed = 1; }
325 }
326 return changed;
327}
328
329static int fixed_hazards(const BlackwellIRProgram *p, const OmegaBlackwellKernel *k) {
330 /* The encoder pads short programs to 32 instructions. */
331 if (p->count > BW_MAX_IR_INSNS || k->code_size < p->count * 16) return 1;
332 int bad = 0;
333 for (size_t w = 0; w < p->count; w++) {
334 const BlackwellIRInsn *writer = &p->insns[w];
335 int cls = latency_class(writer->op), pred = cls == 3;
336 if (!cls || (!pred && (writer->dst_vreg < 0 || writer->is_uniform))) continue;
337 int phys = pred ? -1 : p->regalloc.vreg_to_phys[writer->dst_vreg] + writer->dst_subreg;
338 unsigned age[BW_MAX_IR_INSNS], bound = pred ? 14 : 6;
339 for (size_t i = 0; i < p->count; i++) age[i] = 255;
340 unsigned delay = (encoded_control(k, w) >> 9) & 15u;
341 if (delay == 0) { bad++; continue; } /* zero-delay/co-issue is outside this model */
342 if (relax_successors(p, w, delay, age) < 0) return 1;
343 int changed;
344 do {
345 changed = 0;
346 for (size_t i = 0; i < p->count; i++) {
347 if (age[i] >= bound) continue;
348 const BlackwellIRInsn *in = &p->insns[i];
349 int kills = pred ? latency_class(in->op) == 3 :
350 (!in->is_uniform && reg_contains(p, in->dst_vreg, in->dst_subreg, phys));
351 if (kills && !in->predicate_p0) continue;
352 unsigned d = (encoded_control(k, i) >> 9) & 15u;
353 int r = relax_successors(p, i, age[i] + d, age);
354 if (r < 0) return 1;
355 changed |= r;
356 }
357 } while (changed);
358 for (size_t i = 0; i < p->count; i++) {
359 if (age[i] >= bound) continue;
360 const BlackwellIRInsn *in = &p->insns[i];
361 int reads = pred ? (in->predicate_p0 || in->op == BW_IR_FSEL) :
362 (reg_contains(p, in->src1_vreg, in->src1_subreg, phys) ||
363 reg_contains(p, in->src2_vreg, in->src2_subreg, phys) ||
364 reg_contains(p, in->src3_vreg, in->src3_subreg, phys));
365 int reader_cls = latency_class(in->op);
366 unsigned need = pred ? 14 :
367 ((cls == 2 && reader_cls == 2) ||
368 (cls == 1 && (reader_cls == 1 || reader_cls == 3)) ? 5 : 6);
369 if (reads && age[i] < need) bad++;
370 }
371 }
372 return bad;
373}
374
375static void fixed_schedule_check(const BlackwellIRProgram *p, const OmegaBlackwellKernel *k) {
376 int hz = fixed_hazards(p, k);
377 CHECK(hz == 0, "encoded fixed RAW hazards: %d", hz);
378 OmegaBlackwellKernel bad = *k;
379 bad.code = malloc(k->code_size);
380 CHECK(bad.code != NULL, "fixed schedule negative-control allocation");
381 if (!bad.code) return;
382 int shortened_alu = 0, shortened_pred = 0, alu_hz = 0, pred_hz = 0;
383 for (size_t i = 0; i < p->count; i++) {
384 uint32_t c = encoded_control(k, i);
385 int cls = latency_class(p->insns[i].op);
386 if ((!shortened_alu && cls > 0 && cls < 3 && ((c >> 9) & 15u) == 5) ||
387 (!shortened_pred && cls == 3)) {
388 memcpy(bad.code, k->code, k->code_size);
389 uint32_t short_c = (c & ~(15u << 9)) | ((cls == 3 ? 13u : 4u) << 9);
390 memcpy(bad.code + i * 16 + 12, &short_c, sizeof short_c);
391 int h = fixed_hazards(p, &bad);
392 if (h > hz) {
393 if (cls == 3) { shortened_pred = 1; pred_hz = h; }
394 else { shortened_alu = 1; alu_hz = h; }
395 }
396 }
397 }
398 CHECK(shortened_alu && shortened_pred, "fixed RAW checker missed a short ALU/predicate delay");
399 printf("fixed RAW: %d hazards; negative controls ALU=4: %d, predicate=13: %d\n", hz, alu_hz, pred_hz);
400 free(bad.code);
401}
402
403/* ------------------------------------------------------------- sm121dis */
404/* every word decodes, the new ops appear, and every BRA lands where the IR says */
405static const char *const MUST_GLOBAL[] = { "LOP3.LUT", "SHF.L.U32", "IMAD.HI.U32", "@P0 BRA", "@!P0 BRA", "@P0 EXIT", "STG.E", "LDG.E" };
406static const char *const MUST_STAGED[] = { "LDS", "STS", "BAR.SYNC", "LDG.E", "STG.E", "IMAD.HI.U32", "@P0 BRA", "@!P0 BRA" };
407static void sm121dis_check(const OmegaBlackwellKernel *k, const BlackwellIRProgram *p, const char *const *must, const char *tag) {
408 char lst[64];
409 snprintf(lst, sizeof lst, "/tmp/pr_gb10_sieve_%d.sass.XXXXXX", (int)getpid());
410 int fd = mkstemp(lst); /* private, unique listing; read back the same descriptor */
411 FILE *f = fd >= 0 ? fdopen(fd, "w+") : NULL;
412 if (!f) { if (fd >= 0) close(fd); CHECK(0, "cannot write %s", lst); return; }
413 int rc = omega_sm121_listing(k->code, k->code_size, f);
414 CHECK(rc == 0, "sm121dis rc %d", rc);
415 if (fflush(f) != 0 || fseek(f, 0, SEEK_SET) != 0) { fclose(f); CHECK(0, "cannot read %s", lst); return; }
416 FILE *l = f;
417 int seen[8] = {0}, insns = 0, bad = 0, bra_ok = 0, bra_bad = 0;
418 char line[512];
419 while (fgets(line, sizeof line, l)) {
420 char *s = strstr(line, "/*");
421 if (!s) continue;
422 unsigned addr;
423 if (sscanf(s, "/*%x*/", &addr) != 1 || s[6] != '*') continue;
424 insns++;
425 if (strchr(line, '?') || strstr(line, "error")) bad++;
426 for (int i = 0; i < 8; i++) if (strstr(line, must[i])) seen[i] = 1;
427 char *br = strstr(line, "BRA ");
428 if (br && addr / 16 < p->count) {
429 const BlackwellIRInsn *in = &p->insns[addr / 16];
430 unsigned long target = strtoul(br + 4, NULL, 16);
431 unsigned long want = (unsigned long)((int)(addr / 16) + (int32_t)in->imm) * 16;
432 if (in->op == BW_IR_BRA && target == want) bra_ok++;
433 else { bra_bad++; printf(" branch at 0x%04x: sm121dis target 0x%lx, IR target 0x%lx\n", addr, target, want); }
434 }
435 }
436 CHECK(!ferror(l), "read listing %s", lst);
437 CHECK(fclose(l) == 0, "close listing %s", lst);
438 for (int i = 0; i < 8; i++) CHECK(seen[i], "sm121dis listing lacks %s (%s)", must[i], lst);
439 CHECK(bad == 0, "%d undecodable words (%s)", bad, lst);
440 CHECK(bra_bad == 0 && bra_ok >= 6, "branch targets: %d match, %d differ", bra_ok, bra_bad);
441 printf("sm121dis (%s): %d words, %d undecodable, %d branch targets checked (%s)\n", tag, insns, bad, bra_ok, lst);
442 if (g_failed == 0) unlink(lst);
443}
444
445/* ------------------------------------------------------------- magic model */
446/* The kernel's offset step as plain C: counts how often q = hi32(d * ceil(2^32/p)) is one
447 * too big (r wraps) over the workload, so the --big run is known to exercise that case, and
448 * checks the kernel's j = (p - r) - p*((r-1)>>31) against (p - d % p) % p on every pair. */
449static void magic_model(uint64_t limit) {
450 uint64_t odd = pr_odd_count(limit), nwords = (odd + 31) / 32, over = 0, wrong = 0, pairs = 0;
451 uint32_t root = 0; while ((uint64_t)(root + 1) * (root + 1) <= limit) root++;
452 for (uint32_t p = 3; p <= root; p += 2) {
453 int prime = 1;
454 for (uint32_t d = 3; d * d <= p; d += 2) if (p % d == 0) { prime = 0; break; }
455 if (!prime) continue;
456 uint32_t magic = (uint32_t)(((1ull << 32) + p - 1) / p), k0 = (uint32_t)(((uint64_t)p * p - 1) / 2);
457 for (uint64_t w = 0; w < nwords; w++) {
458 uint32_t base = (uint32_t)(32 * w);
459 if (k0 >= base + 32 || k0 > base) continue;
460 uint32_t d = base - k0, q = (uint32_t)(((uint64_t)d * magic) >> 32), r = d - q * p;
461 pairs++;
462 if (r >> 31) over++;
463 uint32_t j = p - r;
464 j -= p * ((r - 1) >> 31); /* the kernel's single fix */
465 if (j != (p - d % p) % p) wrong++;
466 }
467 }
468 printf("magic model limit %" PRIu64 ": %" PRIu64 " word/prime pairs, %" PRIu64 " needed the correction, %" PRIu64 " wrong\n", limit, pairs, over, wrong);
469 CHECK(wrong == 0, "magic division wrong on %" PRIu64 " pairs", wrong);
470 if (limit >= 10000000ull) CHECK(over > 0, "the 1e7 workload never needs the correction (test would not exercise it)");
471}
472
473static int write_oracle(uint64_t limit, const char *path, unsigned k) {
474 uint64_t *w = oracle_bitmap(limit);
475 FILE *f = fopen(path, "wb");
476 if (!w || !f) { free(w); if (f) fclose(f); return 2; }
477 size_t nw = pr_words64(limit);
478 for (unsigned i = 0; i < k; i++) if (nw && fwrite(w, 8, nw, f) != nw) { fclose(f); free(w); return 2; }
479 free(w);
480 return fclose(f) == 0 ? 0 : 2;
481}
482
483int main(int argc, char **argv) {
484 int big = 0;
485 if (argc >= 2 && strcmp(argv[1], "--limits") == 0) {
486 static uint64_t l[4096];
487 size_t n = sweep_limits(l, 4096, 1);
488 for (size_t i = 0; i < n; i++) printf("%" PRIu64 "\n", l[i]);
489 return 0;
490 }
491 if (argc >= 4 && strcmp(argv[1], "--oracle") == 0)
492 return write_oracle(strtoull(argv[2], NULL, 10), argv[3], argc >= 5 ? (unsigned)atoi(argv[4]) : 1u);
493 if (argc >= 2 && strcmp(argv[1], "--big") == 0) big = 1;
494
495 /* 1. golden words */
496 int fx = omega_blackwell_verify_codegen_fixtures_intops();
497 CHECK(fx == 0, "golden words LOP3_LUT / SHF_L_U32 / IMAD_HI_U32 (rc %d)", fx);
498 /* omega #492: this per-thread simulator reads a register Rc of IMAD.HI.U32 as the pair Rc+1:Rc
499 * (nvcc oracle evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016) and refuses an odd Rc (err 9). */
500 {
501 static BlackwellIRProgram ip;
502 memset(&ip, 0, sizeof ip);
503 ip.insns[0] = (BlackwellIRInsn){ .op = BW_IR_IMAD_HI_U32, .dst_vreg = 3, .src1_vreg = 0, .src2_vreg = 1, .src3_vreg = 2, .ureg = -1 };
504 ip.insns[1] = (BlackwellIRInsn){ .op = BW_IR_EXIT, .dst_vreg = -1, .src1_vreg = -1, .src2_vreg = -1, .src3_vreg = -1, .ureg = -1 };
505 ip.count = 2; ip.regalloc.num_vregs = 4;
506 const int ph[4] = { 4, 5, 6, 8 };
507 for (int v = 0; v < 4; v++) ip.regalloc.vreg_to_phys[v] = ph[v];
508 Thr t; memset(&t, 0, sizeof t);
509 t.r[4] = 0xdeadbeefu; t.r[5] = 0x10000u; t.r[6] = 0x56780000u; t.r[7] = 0x3f803f80u;
510 const uint32_t want = (uint32_t)(((uint64_t)0xdeadbeefu * 0x10000u + (((uint64_t)0x3f803f80u << 32) | 0x56780000u)) >> 32);
511 g_sim_err = 0; sim_thread(&ip, &t, 0, 0);
512 CHECK(g_sim_err == 0 && t.done && t.r[8] == want, "IMAD.HI.U32 even Rc: err %d out 0x%08x want pair 0x%08x", g_sim_err, t.r[8], want);
513 ip.regalloc.vreg_to_phys[2] = 7; memset(&t, 0, sizeof t);
514 g_sim_err = 0; sim_thread(&ip, &t, 0, 0);
515 CHECK(g_sim_err == 9, "IMAD.HI.U32 odd Rc not refused (err %d)", g_sim_err);
516 g_sim_err = 0;
517 }
518
519 /* The scheduling pass is shared by every elementwise family. This is
520 * codegen only: no public launcher or device session is called. */
521 for (int op = OMEGA_GPU_EW_EX2; op <= OMEGA_GPU_EW_PRIME_SIEVE_SHARED; op++) {
522 for (int mutant = 0; mutant <= 1; mutant++) {
523 BlackwellIRProgram *p = calloc(1, sizeof *p);
524 OmegaBlackwellKernel code = {0};
525 int rc = p ? omega_gpu_elementwise_codegen_ir((OmegaGpuEwOp)op, mutant, p, &code) : -1;
526 CHECK(rc == OMEGA_GPU_EW_OK, "schedule codegen op %d mutant %d rc %d", op, mutant, rc);
527 if (rc == OMEGA_GPU_EW_OK) {
528 CHECK(sb_hazards(p, 0) == 0, "schedule scoreboard op %d mutant %d", op, mutant);
529 CHECK(fixed_hazards(p, &code) == 0, "schedule fixed RAW op %d mutant %d", op, mutant);
530 }
531 free(code.code); free(p);
532 }
533 }
534
535 /* 2. oracle self-check against pi(x) */
536 static const struct { uint64_t x, pi; } pis[] = { {10, 4}, {100, 25}, {1000, 168}, {10000, 1229}, {100000, 9592}, {1000000, 78498} };
537 for (size_t i = 0; i < sizeof pis / sizeof pis[0]; i++) {
538 uint64_t *w = oracle_bitmap(pis[i].x);
539 CHECK(w && count_primes(w, pis[i].x) == pis[i].pi, "oracle pi(%" PRIu64 ")", pis[i].x);
540 free(w);
541 }
542
543 /* 3. codegen */
544 BlackwellIRProgram *prog = calloc(1, sizeof *prog), *mprog = calloc(1, sizeof *mprog);
545 OmegaBlackwellKernel k, mk;
546 memset(&k, 0, sizeof k); memset(&mk, 0, sizeof mk);
547 int rc = omega_gpu_elementwise_codegen_ir(OMEGA_GPU_EW_PRIME_SIEVE, 0, prog, &k);
548 int mrc = omega_gpu_elementwise_codegen_ir(OMEGA_GPU_EW_PRIME_SIEVE, 1, mprog, &mk);
549 CHECK(rc == OMEGA_GPU_EW_OK && mrc == OMEGA_GPU_EW_OK, "codegen rc %d mutant rc %d", rc, mrc);
550 if (rc != OMEGA_GPU_EW_OK || mrc != OMEGA_GPU_EW_OK) { printf("VERDICT FAIL (codegen)\n"); return 1; }
551 printf("kernel: %zu IR insns, %zu encoded words, %u GPRs, sha256 ", prog->count, k.insn_count, k.gpr_count);
552 for (int i = 0; i < 32; i++) printf("%02x", k.code_digest[i]);
553 printf("\n");
554 CHECK(k.gpr_count <= 64, "GPR budget %u > 64", k.gpr_count);
555 CHECK(memcmp(k.code_digest, mk.code_digest, 32) != 0, "mutant kernel differs from production");
556 sm121dis_check(&k, prog, MUST_GLOBAL, "global table");
557 fixed_schedule_check(prog, &k);
558 {
559 int hz = sb_hazards(prog, 1);
560 CHECK(hz == 0, "scoreboard hazards in the sieve kernel: %d", hz);
561 /* negative control: drop the first wait that follows a group of loads; must be caught */
562 BlackwellIRProgram *bad = malloc(sizeof *bad);
563 int dropped = 0;
564 if (bad) {
565 memcpy(bad, prog, sizeof *bad);
566 for (size_t i = 1; i < bad->count && !dropped; i++)
567 if (bad->insns[i - 1].op == BW_IR_LDG_E && ((bad->insns[i].control >> 20) & 1u)) {
568 bad->insns[i].control &= ~(1u << 20); dropped = 1;
569 }
570 }
571 int bhz = bad && dropped ? sb_hazards(bad, 0) : 0;
572 CHECK(dropped && bhz > 0, "hazard check missed a dropped wait (dropped %d, found %d)", dropped, bhz);
573 printf("scoreboard: %d hazards; negative control (one wait dropped) found %d\n", hz, bhz);
574 free(bad);
575 }
576
577 /* 4. simulator sweep */
578 static uint64_t lims[4096];
579 size_t nl = sweep_limits(lims, 4096, 0);
580 int sim_fail = 0; uint64_t first_bad = 0;
581 for (size_t i = 0; i < nl + (size_t)big; i++) {
582 uint64_t L = i < nl ? lims[i] : 10000000ull;
583 uint64_t *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8);
584 if (!want || !got) { CHECK(0, "alloc"); break; }
585 int src = sim_sieve(prog, L, OMEGA_GPU_EW_MAX_CTAS, 0, got);
586 size_t nw = pr_words64(L), bad = nw;
587 for (size_t j = 0; j < nw; j++) if (got[j] != want[j]) { bad = j; break; }
588 if (src != 0 || bad != nw) {
589 if (!sim_fail++) first_bad = L;
590 printf(" sim limit %" PRIu64 ": sim rc %d err %d, first differing u64 word %zu (got %016" PRIx64 " want %016" PRIx64 ")\n",
591 L, src, g_sim_err, bad, bad < nw ? got[bad] : 0, bad < nw ? want[bad] : 0);
592 }
593 if (L == 1000000) CHECK(count_primes(got, L) == 78498, "sim pi(1e6) = %" PRIu64, count_primes(got, L));
594 free(want); free(got);
595 }
596 CHECK(sim_fail == 0, "simulator differs from the oracle at %d limits (first %" PRIu64 ")", sim_fail, first_bad);
597 printf("simulator: %zu limits%s, %d differ, %" PRIu64 " IR steps\n", nl + (size_t)big, big ? " (+1e7)" : "", sim_fail, g_steps);
598
599 /* 4b. other CTA budgets (campaign C1): each budget at its own grid-stride boundaries
600 * (limit 64 * budget * 128 +-1: the word count crosses one full round of threads) and at 1e6. */
601 {
602 static const uint32_t budgets[] = { 1, 2, 128, 256 };
603 int bfail = 0, bn = 0;
604 for (size_t b = 0; b < sizeof budgets / sizeof budgets[0]; b++) {
605 uint64_t edge = 64ull * budgets[b] * OMEGA_GPU_EW_THREADS;
606 const uint64_t ls[] = { edge - 1, edge, edge + 1, 1000000 };
607 for (size_t i = 0; i < 4; i++, bn++) {
608 uint64_t L = ls[i], *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8);
609 if (!want || !got) { CHECK(0, "alloc"); free(want); free(got); break; }
610 int src = sim_sieve(prog, L, budgets[b], 0, got);
611 if (src != 0 || memcmp(got, want, pr_words64(L) * 8) != 0) {
612 bfail++;
613 printf(" budget %u limit %" PRIu64 ": sim rc %d err %d, differs\n", budgets[b], L, src, g_sim_err);
614 }
615 free(want); free(got);
616 }
617 }
618 CHECK(bfail == 0, "CTA budget sweep: %d of %d differ", bfail, bn);
619 printf("budget sweep: %d cases (budgets 1, 2, 128, 256), %d differ\n", bn, bfail);
620 }
621
622 /* 5. mutant must be caught */
623 {
624 uint64_t L = 100000, *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8);
625 int src = want && got ? sim_sieve(mprog, L, OMEGA_GPU_EW_MAX_CTAS, 0, got) : -1;
626 CHECK(src == 0 && want && memcmp(got, want, pr_words64(L) * 8) != 0, "mutant kernel not caught at 1e5 (sim rc %d)", src);
627 free(want); free(got);
628 }
629
630 /* 5b. staged kernel (campaign C3): table copied to shared memory, then LDS. Same sweeps as the
631 * global kernel, the hazard walk, the mutant, and two negative controls on the new machinery:
632 * the barrier removed (the race check must fire) and too little shared memory declared. */
633 {
634 BlackwellIRProgram *sp = calloc(1, sizeof *sp), *smp = calloc(1, sizeof *smp), *nobar = malloc(sizeof *nobar);
635 OmegaBlackwellKernel sk, smk;
636 memset(&sk, 0, sizeof sk); memset(&smk, 0, sizeof smk);
637 int src1 = sp ? omega_gpu_elementwise_codegen_ir(OMEGA_GPU_EW_PRIME_SIEVE_SHARED, 0, sp, &sk) : -1;
638 int src2 = smp ? omega_gpu_elementwise_codegen_ir(OMEGA_GPU_EW_PRIME_SIEVE_SHARED, 1, smp, &smk) : -1;
639 CHECK(src1 == OMEGA_GPU_EW_OK && src2 == OMEGA_GPU_EW_OK && nobar, "staged codegen rc %d mutant rc %d", src1, src2);
640 if (src1 == OMEGA_GPU_EW_OK && src2 == OMEGA_GPU_EW_OK && nobar) {
641 printf("staged kernel: %zu IR insns, %u GPRs, sha256 ", sp->count, sk.gpr_count);
642 for (int i = 0; i < 32; i++) printf("%02x", sk.code_digest[i]);
643 printf("\n");
644 CHECK(sk.gpr_count <= 64, "staged GPR budget %u > 64", sk.gpr_count);
645 sm121dis_check(&sk, sp, MUST_STAGED, "staged");
646 fixed_schedule_check(sp, &sk);
647 int hz = sb_hazards(sp, 1);
648 CHECK(hz == 0, "scoreboard hazards in the staged kernel: %d", hz);
649 int sfail = 0, sn = 0;
650 for (size_t i = 0; i < nl; i++, sn++) {
651 uint64_t L = lims[i], *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8);
652 if (!want || !got) { CHECK(0, "alloc"); free(want); free(got); break; }
653 int r = sim_sieve(sp, L, OMEGA_GPU_EW_MAX_CTAS, 1, got);
654 if (r != 0 || memcmp(got, want, pr_words64(L) * 8) != 0) {
655 if (!sfail++) printf(" staged limit %" PRIu64 ": sim rc %d err %d, differs\n", L, r, g_sim_err);
656 }
657 if (L == 1000000) CHECK(count_primes(got, L) == 78498, "staged pi(1e6) = %" PRIu64, count_primes(got, L));
658 free(want); free(got);
659 }
660 static const uint32_t budgets[] = { 1, 2, 128, 256 };
661 for (size_t b = 0; b < sizeof budgets / sizeof budgets[0]; b++) {
662 uint64_t edge = 64ull * budgets[b] * OMEGA_GPU_EW_THREADS;
663 const uint64_t ls[] = { edge - 1, edge, edge + 1 };
664 for (size_t i = 0; i < 3; i++, sn++) {
665 uint64_t L = ls[i], *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8);
666 if (!want || !got) { CHECK(0, "alloc"); free(want); free(got); break; }
667 int r = sim_sieve(sp, L, budgets[b], 1, got);
668 if (r != 0 || memcmp(got, want, pr_words64(L) * 8) != 0) {
669 if (!sfail++) printf(" staged budget %u limit %" PRIu64 ": sim rc %d err %d\n", budgets[b], L, r, g_sim_err);
670 }
671 free(want); free(got);
672 }
673 }
674 CHECK(sfail == 0, "staged kernel differs from the oracle in %d of %d cases", sfail, sn);
675 printf("staged simulator: %d cases, %d differ, races seen %" PRIu64 "\n", sn, sfail, g_races);
676 CHECK(g_races == 0, "staged kernel: %" PRIu64 " shared-memory races", g_races);
677
678 uint64_t L = 100000, *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8);
679 int r = want && got ? sim_sieve(smp, L, OMEGA_GPU_EW_MAX_CTAS, 1, got) : -1;
680 CHECK(r == 0 && want && memcmp(got, want, pr_words64(L) * 8) != 0, "staged mutant not caught at 1e5 (sim rc %d)", r);
681
682 /* negative control 1: BAR_SYNC turned into a no-op -> a thread reads a word another
683 * thread staged in the same phase; the race check must stop the run */
684 memcpy(nobar, sp, sizeof *nobar);
685 int bars = 0;
686 for (size_t i = 0; i < nobar->count; i++)
687 if (nobar->insns[i].op == BW_IR_BAR_SYNC) { nobar->insns[i].op = BW_IR_MOV_RZ; nobar->insns[i].dst_vreg = -1; bars++; }
688 uint64_t races0 = g_races;
689 r = got ? sim_sieve(nobar, L, OMEGA_GPU_EW_MAX_CTAS, 1, got) : -1;
690 CHECK(bars == 1 && r != 0 && g_sim_err == 6 && g_races > races0,
691 "barrier removed: race not caught (bars %d, rc %d, err %d)", bars, r, g_sim_err);
692 printf("negative control, barrier removed: rc %d err %d (6 = race)\n", r, g_sim_err);
693
694 /* negative control 2: declare 12 bytes (one entry) too little shared memory; the
695 * staging copy must run off the end and stop */
696 g_smem_short = 12;
697 r = got ? sim_sieve(sp, L, OMEGA_GPU_EW_MAX_CTAS, 1, got) : -1;
698 g_smem_short = 0;
699 CHECK(r != 0 && g_sim_err == 5, "short shared declaration not caught (rc %d, err %d)", r, g_sim_err);
700 printf("negative control, shared memory one entry short: rc %d err %d (5 = out of bounds)\n", r, g_sim_err);
701 free(want); free(got);
702 }
703 free(sk.code); free(smk.code); free(sp); free(smp); free(nobar);
704 }
705
706 /* 6. structured reconvergence (omega #308, regression R1): the sieve with its lane-dependent
707 * loops put back inside BSSY/BSYNC regions. DIV1 = the 888a011 mark loop in a region, DIV2 =
708 * the prime loop in an outer region too (nested). Each variant: codegen, hazard walks,
709 * listing, the oracle sweep through the warp simulator in both fragment orders, the math
710 * mutant caught, and the dropped-join / corrupted-target mutants rejected by the simulator.
711 * Then the warp-issue model of the C5 kernel against the regioned ones (a host model, not
712 * a measurement: the chip number comes from gb10_native PR_GB10_MARK=divergent). */
713 {
714 static const struct { OmegaGpuEwOp op; int staged; const char *name; int regions; } dv[] = {
715 { OMEGA_GPU_EW_PRIME_SIEVE_DIV1, 0, "div1", 1 }, { OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV1, 1, "shared_div1", 2 },
716 { OMEGA_GPU_EW_PRIME_SIEVE_DIV2, 0, "div2", 2 }, { OMEGA_GPU_EW_PRIME_SIEVE_SHARED_DIV2, 1, "shared_div2", 3 },
717 }; /* the staged variants also put the staging loop (lane-dependent trip count) in a region */
718 static const char *const MUST_DIV[] = { "BSSY", "BSYNC", "IMAD.HI.U32", "@P0 BRA", "@!P0 BRA", "@P0 EXIT", "STG.E", "LDG.E" };
719 /* positive control for the adapter: the production (C5) kernel through the warp simulator */
720 {
721 uint64_t L = 1000000, *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8);
722 g_ws_order = 0;
723 int r = want && got ? sim_sieve(prog, L, OMEGA_GPU_EW_MAX_CTAS, 0, got) : -1;
724 g_ws_order = -1;
725 CHECK(r == 0 && want && memcmp(got, want, pr_words64(L) * 8) == 0, "C5 kernel through the warp simulator at 1e6 (rc %d err %d)", r, g_sim_err);
726 printf("warp-sim model C5 (global) 1e6: warp_issues=%" PRIu64 " lane_util=%.3f splits=%" PRIu64 " max_frags=%d\n",
727 g_ws_last.warp_issues, g_ws_last.warp_issues ? (double)g_ws_last.lane_issues / (32.0 * (double)g_ws_last.warp_issues) : 0.0, g_ws_last.splits, g_ws_last.max_frags);
728 /* the production staged kernel too: its staging loop splits the warp before BAR.SYNC
729 * (allowed on the chip since Volta; counted here, and the fragments stay split) */
730 BlackwellIRProgram *cp = calloc(1, sizeof *cp); OmegaBlackwellKernel ck; memset(&ck, 0, sizeof ck);
731 if (cp && omega_gpu_elementwise_codegen_ir(OMEGA_GPU_EW_PRIME_SIEVE_SHARED, 0, cp, &ck) == OMEGA_GPU_EW_OK) {
732 want = oracle_bitmap(L); got = calloc(pr_words64(L) + 1, 8);
733 g_ws_order = 0;
734 r = want && got ? sim_sieve(cp, L, OMEGA_GPU_EW_MAX_CTAS, 1, got) : -1;
735 g_ws_order = -1;
736 CHECK(r == 0 && want && memcmp(got, want, pr_words64(L) * 8) == 0, "C5 staged kernel through the warp simulator at 1e6 (rc %d err %d)", r, g_sim_err);
737 printf("warp-sim model C5 (staged) 1e6: warp_issues=%" PRIu64 " lane_util=%.3f splits=%" PRIu64 " max_frags=%d bar_split_arrivals=%" PRIu64 "\n",
738 g_ws_last.warp_issues, g_ws_last.warp_issues ? (double)g_ws_last.lane_issues / (32.0 * (double)g_ws_last.warp_issues) : 0.0, g_ws_last.splits, g_ws_last.max_frags, g_ws_last.bar_split_arrivals);
739 free(ck.code);
740 } else CHECK(0, "C5 staged codegen for the warp-sim control");
741 free(cp);
742 free(want); free(got);
743 }
744 for (size_t v = 0; v < sizeof dv / sizeof dv[0]; v++) {
745 BlackwellIRProgram *dp = calloc(1, sizeof *dp);
746 OmegaBlackwellKernel dk; memset(&dk, 0, sizeof dk);
747 int drc = dp ? omega_gpu_elementwise_codegen_ir(dv[v].op, 0, dp, &dk) : -1;
748 CHECK(drc == OMEGA_GPU_EW_OK && dk.gpr_count <= 64, "%s codegen rc %d gprs %u", dv[v].name, drc, dk.gpr_count);
749 if (drc != OMEGA_GPU_EW_OK) { free(dp); continue; }
750 int nbssy = 0, nbsync = 0;
751 for (size_t i = 0; i < dp->count; i++) { nbssy += dp->insns[i].op == BW_IR_BSSY; nbsync += dp->insns[i].op == BW_IR_BSYNC; }
752 CHECK(nbssy == dv[v].regions && nbsync == dv[v].regions, "%s has %d BSSY / %d BSYNC (want %d)", dv[v].name, nbssy, nbsync, dv[v].regions);
753 printf("%s kernel: %zu IR insns, %u GPRs, %d regions, sha256 ", dv[v].name, dp->count, dk.gpr_count, nbssy);
754 for (int i = 0; i < 32; i++) printf("%02x", dk.code_digest[i]);
755 printf("\n");
756 CHECK(sb_hazards(dp, 1) == 0, "%s scoreboard hazards", dv[v].name);
757 CHECK(fixed_hazards(dp, &dk) == 0, "%s encoded fixed RAW hazards", dv[v].name);
758 if (!dv[v].staged) sm121dis_check(&dk, dp, MUST_DIV, dv[v].name);
759 int dfail = 0, dn = 0;
760 for (int order = 0; order < 2; order++) for (size_t i = 0; i < nl; i++, dn++) {
761 uint64_t L = lims[i], *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8);
762 if (!want || !got) { CHECK(0, "alloc"); free(want); free(got); break; }
763 g_ws_order = order;
764 int r = sim_sieve(dp, L, OMEGA_GPU_EW_MAX_CTAS, dv[v].staged, got);
765 g_ws_order = -1;
766 if (r != 0 || memcmp(got, want, pr_words64(L) * 8) != 0) {
767 if (!dfail++) printf(" %s order %d limit %" PRIu64 ": sim rc %d err %d (%s), differs\n", dv[v].name, order, L, r, g_sim_err, g_sim_err >= 200 ? ws_err_name(g_sim_err - 200) : "-");
768 }
769 if (L == 1000000 && order == 0) {
770 CHECK(count_primes(got, L) == 78498, "%s pi(1e6) = %" PRIu64, dv[v].name, count_primes(got, L));
771 printf("warp-sim model %s 1e6: warp_issues=%" PRIu64 " lane_util=%.3f splits=%" PRIu64 " max_frags=%d peak_bars=%d bar_split_arrivals=%" PRIu64 "\n", dv[v].name,
772 g_ws_last.warp_issues, g_ws_last.warp_issues ? (double)g_ws_last.lane_issues / (32.0 * (double)g_ws_last.warp_issues) : 0.0, g_ws_last.splits, g_ws_last.max_frags, g_ws_last.peak_bars, g_ws_last.bar_split_arrivals);
773 CHECK(g_ws_last.bar_split_arrivals == 0, "%s reaches BAR.SYNC as a whole warp", dv[v].name);
774 }
775 free(want); free(got);
776 }
777 static const uint32_t budgets[] = { 1, 2, 128, 256 };
778 for (size_t b = 0; b < sizeof budgets / sizeof budgets[0]; b++) {
779 uint64_t edge = 64ull * budgets[b] * OMEGA_GPU_EW_THREADS;
780 const uint64_t ls[] = { edge - 1, edge, edge + 1 };
781 for (size_t i = 0; i < 3; i++, dn++) {
782 uint64_t L = ls[i], *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8);
783 if (!want || !got) { CHECK(0, "alloc"); free(want); free(got); break; }
784 g_ws_order = (int)(i & 1);
785 int r = sim_sieve(dp, L, budgets[b], dv[v].staged, got);
786 g_ws_order = -1;
787 if (r != 0 || memcmp(got, want, pr_words64(L) * 8) != 0) { if (!dfail++) printf(" %s budget %u limit %" PRIu64 ": sim rc %d err %d\n", dv[v].name, budgets[b], L, r, g_sim_err); }
788 free(want); free(got);
789 }
790 }
791 CHECK(dfail == 0, "%s differs from the oracle in %d of %d warp-sim cases", dv[v].name, dfail, dn);
792 printf("%s warp simulator: %d cases (both orders), %d differ\n", dv[v].name, dn, dfail);
793 /* mutants: 1 math (caught by the oracle), 2 join dropped and 3 target corrupted (rejected by the simulator) */
794 for (int m = 1; m <= 3; m++) {
795 BlackwellIRProgram *mp2 = calloc(1, sizeof *mp2); OmegaBlackwellKernel mk2; memset(&mk2, 0, sizeof mk2);
796 int mrc = mp2 ? omega_gpu_elementwise_codegen_ir(dv[v].op, m, mp2, &mk2) : -1;
797 CHECK(mrc == OMEGA_GPU_EW_OK && memcmp(mk2.code_digest, dk.code_digest, 32) != 0, "%s mutant %d builds and differs (rc %d)", dv[v].name, m, mrc);
798 if (mrc == OMEGA_GPU_EW_OK) {
799 uint64_t L = 100000, *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8);
800 g_ws_order = 0;
801 int r = want && got ? sim_sieve(mp2, L, OMEGA_GPU_EW_MAX_CTAS, dv[v].staged, got) : -1;
802 g_ws_order = -1;
803 if (m == 1) CHECK(r == 0 && want && memcmp(got, want, pr_words64(L) * 8) != 0, "%s math mutant not caught at 1e5 (rc %d err %d)", dv[v].name, r, g_sim_err);
804 else CHECK(r != 0 && g_sim_err >= 200, "%s mutant %d (%s) not rejected by the warp simulator (rc %d err %d)", dv[v].name, m, m == 2 ? "BSYNC dropped" : "BSSY target corrupted", r, g_sim_err);
805 if (m > 1) printf("%s mutant %d: warp simulator says %s\n", dv[v].name, m, g_sim_err >= 200 ? ws_err_name(g_sim_err - 200) : "nothing");
806 free(want); free(got); free(mk2.code);
807 }
808 free(mp2);
809 }
810 free(dk.code); free(dp);
811 }
812 }
813
814 /* 7. division model */
815 magic_model(1000000);
816 if (big) magic_model(10000000);
817
818 free(k.code); free(mk.code); free(prog); free(mprog);
819 printf("checks=%d failed=%d\nVERDICT %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS");
820 return g_failed ? 1 : 0;
821}