From 106b06e345d1520de692cdb7f2a47af43ebc91a6 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 14:16:38 -0500 Subject: [PATCH 01/13] bench/prime_race: CTA budget setting for the GB10 native sieve (campaign C1) PR_GB10_CTA_BUDGET=N (1..4096) lifts or lowers the launch cap; unset keeps the proven I42 envelope (64). Malformed values fail setup before the GPU is opened. Detail JSON reports cta_budget. Host simulator now sweeps budgets 1, 2, 128, 256 at each budget's grid-stride boundary and at 1e6 (0 differ). Co-Authored-By: Claude Opus 5.5 --- bench/prime_race/gb10_native.c | 25 +++++++++++--- bench/prime_race/tests/gb10_sieve_host_test.c | 34 ++++++++++++++++--- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/bench/prime_race/gb10_native.c b/bench/prime_race/gb10_native.c index a54484f78..e07ad5e8b 100644 --- a/bench/prime_race/gb10_native.c +++ b/bench/prime_race/gb10_native.c @@ -14,6 +14,10 @@ * table, one launch (<= OMEGA_GPU_EW_MAX_CTAS CTAs, grid-stride over words), wait for * both markers. Persistent buffers => faithful=no. * + * CTA budget (optimization campaign C1, 2026-10-05): PR_GB10_CTA_BUDGET=N (1..4096) in the + * environment lifts or lowers the launch cap; unset keeps OMEGA_GPU_EW_MAX_CTAS (the proven + * I42 envelope). Any other value fails setup. The budget used is reported in the detail JSON. + * * Labels (handoff 2026-10-05 DESIGN): algorithm=other, faithful=no, bits=1, * environment linux-hosted-gb10-native. Build with -DPR_GB10_MUTANT=1 only for the chip * test's negative control (the r == 0 fix is dropped; the oracle must catch it). @@ -37,10 +41,21 @@ #define GB_WAIT_MS 600000ull /* same marker wait as the elementwise launcher (omega#198) */ #define GB_POISON 0xffbadbadu #define GB_MAX_LIMIT 0xffffffffull /* kernel range: base, k0 < 2^31 */ +#define GB_BUDGET_MAX 4096u + +/* PR_GB10_CTA_BUDGET or the I42 default; 0 on a malformed or out-of-range value. */ +static uint32_t gb_cta_budget(void) { + const char *v = getenv("PR_GB10_CTA_BUDGET"); + if (!v) return GB_MAX_CTAS; + char *end = NULL; + unsigned long n = strtoul(v, &end, 10); + if (end == v || *end != 0 || v[0] == '-' || n == 0 || n > GB_BUDGET_MAX) return 0; + return (uint32_t)n; +} typedef struct { uint64_t limit, odd; - uint32_t nwords, ctas, stride, lastw, tailinv; + uint32_t nwords, ctas, budget, stride, lastw, tailinv; uint32_t root; /* isqrt(limit) */ uint32_t nprimes; /* odd primes in the table of the last pass (sentinel excluded) */ size_t table_cap; /* entries, sentinel included */ @@ -80,8 +95,10 @@ static int gb_setup(void **state, uint64_t limit) { s->limit = limit; s->odd = pr_odd_count(limit); s->nwords = (uint32_t)((s->odd + 31) / 32); + s->budget = gb_cta_budget(); + if (s->budget == 0) { fprintf(stderr, "gb10-native: PR_GB10_CTA_BUDGET must be 1..%u\n", GB_BUDGET_MAX); return -1; } uint32_t blocks = (s->nwords + GB_THREADS - 1) / GB_THREADS; - s->ctas = blocks == 0 ? 1 : (blocks > GB_MAX_CTAS ? GB_MAX_CTAS : blocks); + s->ctas = blocks == 0 ? 1 : (blocks > s->budget ? s->budget : blocks); s->stride = s->ctas * GB_THREADS; s->lastw = s->nwords ? s->nwords - 1 : 0; uint32_t valid = s->nwords ? (uint32_t)(s->odd - 32ull * s->lastw) : 32; /* 1..32 */ @@ -171,8 +188,8 @@ static uint64_t gb_threads(void *state) { gb_state *s = state; return s ? s->str static int gb_detail(void *state, FILE *f) { gb_state *s = state; if (!s) return -1; - fprintf(f, "{\"grid\": %u, \"block\": %u, \"words\": %u, \"nprimes\": %u, \"kernel_code_sha256\": \"", - s->ctas, GB_THREADS, s->nwords, s->nprimes); + fprintf(f, "{\"grid\": %u, \"cta_budget\": %u, \"block\": %u, \"words\": %u, \"nprimes\": %u, \"kernel_code_sha256\": \"", + s->ctas, s->budget, GB_THREADS, s->nwords, s->nprimes); for (int i = 0; i < 32; i++) fprintf(f, "%02x", s->k.code_digest[i]); fprintf(f, "\", \"kernel_insns\": %zu, \"kernel_gpr\": %u, \"mutant\": %d, \"launches\": %" PRIu64 ", \"diag_kernel_ns_total\": %" PRIu64 diff --git a/bench/prime_race/tests/gb10_sieve_host_test.c b/bench/prime_race/tests/gb10_sieve_host_test.c index 4973d579a..850a9e370 100644 --- a/bench/prime_race/tests/gb10_sieve_host_test.c +++ b/bench/prime_race/tests/gb10_sieve_host_test.c @@ -138,12 +138,13 @@ static void sim_thread(const BlackwellIRProgram *p, uint32_t tid, uint32_t cta) g_sim_err = 4; } -/* Mirrors gb10_native.c's pass on the host: same table, same argument words. */ -static int sim_sieve(const BlackwellIRProgram *p, uint64_t limit, uint64_t *words) { +/* Mirrors gb10_native.c's pass on the host: same table, same argument words; `budget` + * is the CTA cap (PR_GB10_CTA_BUDGET, default OMEGA_GPU_EW_MAX_CTAS). */ +static int sim_sieve(const BlackwellIRProgram *p, uint64_t limit, uint32_t budget, uint64_t *words) { uint64_t odd = pr_odd_count(limit); uint32_t nwords = (uint32_t)((odd + 31) / 32); uint32_t blocks = (nwords + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS; - uint32_t ctas = blocks == 0 ? 1 : (blocks > OMEGA_GPU_EW_MAX_CTAS ? OMEGA_GPU_EW_MAX_CTAS : blocks); + uint32_t ctas = blocks == 0 ? 1 : (blocks > budget ? budget : blocks); uint32_t stride = ctas * OMEGA_GPU_EW_THREADS, lastw = nwords ? nwords - 1 : 0; uint32_t valid = nwords ? (uint32_t)(odd - 32ull * lastw) : 32; uint32_t tailinv = ~(valid >= 32 ? 0xffffffffu : ((1u << valid) - 1u)); @@ -307,7 +308,7 @@ int main(int argc, char **argv) { uint64_t L = i < nl ? lims[i] : 10000000ull; uint64_t *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); if (!want || !got) { CHECK(0, "alloc"); break; } - int src = sim_sieve(prog, L, got); + int src = sim_sieve(prog, L, OMEGA_GPU_EW_MAX_CTAS, got); size_t nw = pr_words64(L), bad = nw; for (size_t j = 0; j < nw; j++) if (got[j] != want[j]) { bad = j; break; } if (src != 0 || bad != nw) { @@ -321,10 +322,33 @@ int main(int argc, char **argv) { CHECK(sim_fail == 0, "simulator differs from the oracle at %d limits (first %" PRIu64 ")", sim_fail, first_bad); printf("simulator: %zu limits%s, %d differ, %" PRIu64 " IR steps\n", nl + (size_t)big, big ? " (+1e7)" : "", sim_fail, g_steps); + /* 4b. other CTA budgets (campaign C1): each budget at its own grid-stride boundaries + * (limit 64 * budget * 128 +-1: the word count crosses one full round of threads) and at 1e6. */ + { + static const uint32_t budgets[] = { 1, 2, 128, 256 }; + int bfail = 0, bn = 0; + for (size_t b = 0; b < sizeof budgets / sizeof budgets[0]; b++) { + uint64_t edge = 64ull * budgets[b] * OMEGA_GPU_EW_THREADS; + const uint64_t ls[] = { edge - 1, edge, edge + 1, 1000000 }; + for (size_t i = 0; i < 4; i++, bn++) { + uint64_t L = ls[i], *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); + if (!want || !got) { CHECK(0, "alloc"); free(want); free(got); break; } + int src = sim_sieve(prog, L, budgets[b], got); + if (src != 0 || memcmp(got, want, pr_words64(L) * 8) != 0) { + bfail++; + printf(" budget %u limit %" PRIu64 ": sim rc %d err %d, differs\n", budgets[b], L, src, g_sim_err); + } + free(want); free(got); + } + } + CHECK(bfail == 0, "CTA budget sweep: %d of %d differ", bfail, bn); + printf("budget sweep: %d cases (budgets 1, 2, 128, 256), %d differ\n", bn, bfail); + } + /* 5. mutant must be caught */ { uint64_t L = 100000, *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); - int src = want && got ? sim_sieve(mprog, L, got) : -1; + int src = want && got ? sim_sieve(mprog, L, OMEGA_GPU_EW_MAX_CTAS, got) : -1; CHECK(src == 0 && want && memcmp(got, want, pr_words64(L) * 8) != 0, "mutant kernel not caught at 1e5 (sim rc %d)", src); free(want); free(got); } From 41fd1c9ec1a34093b40e2922ddc0deceac7fea30 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 14:22:56 -0500 Subject: [PATCH 02/13] omega_gpu_elementwise: ldg_group issues independent loads together (campaign C2a) The emitter made every instruction wait on the previous variable-latency result, so the sieve paid three serial memory round trips per prime (k0, then p, then magic). ldg_group issues n independent loads back to back on the counting scoreboard and the next consumer waits once. The prime sieve now loads its whole table entry in one round trip (the sentinel is a full entry, so this stays in bounds). Host test: new scoreboard hazard walk (0 hazards; negative control with one wait dropped finds 6); simulator and CTA-budget sweeps unchanged (0 differ). Co-Authored-By: Claude Opus 5.5 --- bench/prime_race/tests/gb10_sieve_host_test.c | 39 +++++++++++++++++++ src/omega_gpu_elementwise_api.c | 29 +++++++++++--- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/bench/prime_race/tests/gb10_sieve_host_test.c b/bench/prime_race/tests/gb10_sieve_host_test.c index 850a9e370..577d1677d 100644 --- a/bench/prime_race/tests/gb10_sieve_host_test.c +++ b/bench/prime_race/tests/gb10_sieve_host_test.c @@ -178,6 +178,27 @@ static int sim_sieve(const BlackwellIRProgram *p, uint64_t limit, uint32_t budge return g_sim_err ? -1 : 0; } +/* ------------------------------------------------------------- scoreboard hazards */ +/* Campaign C2a: loads may now be in flight together, so check the waits the emitter wrote. + * Walking the IR in order, a destination of an instruction that sets write scoreboard 0 + * (control bits 14..16, the emitter's SB_GPR) stays in flight until an instruction whose wait + * mask (bits 20..25) includes scoreboard 0; reading it earlier is a hazard. Straight-line walk: + * every branch the emitter writes waits on what is pending, so no value crosses one in flight. */ +static int sb_hazards(const BlackwellIRProgram *p, int verbose) { + int inflight[64], n = 0, bad = 0; + for (size_t i = 0; i < p->count; i++) { + const BlackwellIRInsn *in = &p->insns[i]; + uint32_t c = in->control; + if ((c >> 20) & 1u) n = 0; + const int src[3] = { in->src1_vreg, in->src2_vreg, in->src3_vreg }; + for (int s = 0; s < 3; s++) + for (int k = 0; src[s] >= 0 && k < n; k++) + if (inflight[k] == src[s]) { bad++; if (verbose) printf(" hazard: insn %zu reads vreg %d still in flight\n", i, src[s]); } + if (c != 0 && ((c >> 14) & 7u) == 0 && in->dst_vreg >= 0 && n < 64) inflight[n++] = in->dst_vreg; + } + return bad; +} + /* ------------------------------------------------------------- nvdisasm */ /* every word decodes, the new ops appear, and every BRA lands where the IR says */ static void nvdisasm_check(const OmegaBlackwellKernel *k, const BlackwellIRProgram *p) { @@ -299,6 +320,24 @@ int main(int argc, char **argv) { CHECK(k.gpr_count <= 64, "GPR budget %u > 64", k.gpr_count); CHECK(memcmp(k.code_digest, mk.code_digest, 32) != 0, "mutant kernel differs from production"); nvdisasm_check(&k, prog); + { + int hz = sb_hazards(prog, 1); + CHECK(hz == 0, "scoreboard hazards in the sieve kernel: %d", hz); + /* negative control: drop the first wait that follows a group of loads; must be caught */ + BlackwellIRProgram *bad = malloc(sizeof *bad); + int dropped = 0; + if (bad) { + memcpy(bad, prog, sizeof *bad); + for (size_t i = 1; i < bad->count && !dropped; i++) + if (bad->insns[i - 1].op == BW_IR_LDG_E && ((bad->insns[i].control >> 20) & 1u)) { + bad->insns[i].control &= ~(1u << 20); dropped = 1; + } + } + int bhz = bad && dropped ? sb_hazards(bad, 0) : 0; + CHECK(dropped && bhz > 0, "hazard check missed a dropped wait (dropped %d, found %d)", dropped, bhz); + printf("scoreboard: %d hazards; negative control (one wait dropped) found %d\n", hz, bhz); + free(bad); + } /* 4. simulator sweep */ static uint64_t lims[4096]; diff --git a/src/omega_gpu_elementwise_api.c b/src/omega_gpu_elementwise_api.c index 7b48b0a39..c83a1b9df 100644 --- a/src/omega_gpu_elementwise_api.c +++ b/src/omega_gpu_elementwise_api.c @@ -95,6 +95,22 @@ static void lop3xor(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR /* dst64 = idx * 4 + ptr64 (byte address of element idx) */ static void addr4(Em *e, int dst64, int idx, int ptr64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = idx; i.imm = 4; i.src3_vreg = ptr64; emit(e, i, K_FIXED); } static void ldg(Em *e, int dst, int addr64, int udesc) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = dst; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, K_VAR); } +/* Independent global loads issued back to back (campaign C2a, 2026-10-05). The first load + * waits on whatever is pending; the others wait on nothing, so all n are in flight together. + * Every load sets SB_GPR (a counting scoreboard), so the next instruction waits once for all + * of them instead of once per load. Fails the build if a load address is a destination of the + * group or a destination repeats (the later loads would read an operand still in flight). */ +static void ldg_group(Em *e, int n, const int *dst, const int *addr64, int udesc) { + for (int i = 0; i < n; i++) + for (int k = 0; k < n; k++) + if (addr64[i] == dst[k] || (k != i && dst[i] == dst[k])) { e->err = -1; return; } + for (int i = 0; i < n; i++) { + BlackwellIRInsn in = I0(BW_IR_LDG_E); in.dst_vreg = dst[i]; in.src1_vreg = addr64[i]; in.ureg = udesc; + if (i == 0) { emit(e, in, K_VAR); continue; } + in.control = CW(4, SB_GPR, SB_NONE, 0) | CW_YIELD; /* nonzero, so the encoder keeps it */ + if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; + } +} static void stg(Em *e, int addr64, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.ureg = udesc; emit(e, i, K_STORE); } static void fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } static void fsub(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FSUB); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } @@ -351,7 +367,7 @@ static void gen_prime_sieve(Em *e, int mutant) { isetp_ge_u32(e, w, nwords); exit_if_p0(e); - int base = V(e), basep32 = V(e), acc = V(e), ptr = V64(e), a64 = V64(e); + int base = V(e), basep32 = V(e), acc = V(e), ptr = V64(e), a64 = V64(e), a64m = V64(e); int p = V(e), magic = V(e), k0 = V(e), negp = V(e), d = V(e), neg = V(e), nd = V(e), dpos = V(e), jadd = V(e); int q = V(e), r = V(e), j = V(e), t = V(e), z = V(e), bit = V(e); int e1 = V(e), m = V(e), word = V(e), oaddr = V64(e); @@ -365,13 +381,16 @@ static void gen_prime_sieve(Em *e, int mutant) { loop_begin(e); /* prime loop */ int lp = here(e); + /* one memory round trip per prime: all three entry words load together (C2a). The + * sentinel entry is a whole entry, so loading p and magic before the k0 test is in bounds. */ widei(e, a64, one, 8, ptr); - ldg(e, k0, a64, pr.udesc); + widei(e, a64m, one, 4, ptr); + { + const int dsts[3] = { k0, p, magic }, adrs[3] = { a64, ptr, a64m }; + ldg_group(e, 3, dsts, adrs, pr.udesc); + } isetp_ge_u32(e, k0, basep32); int brk = bra_fwd_if_p0(e); - ldg(e, p, ptr, pr.udesc); - widei(e, a64, one, 4, ptr); - ldg(e, magic, a64, pr.udesc); imadi(e, negp, p, 0xffffffffu, NOREG); /* -p */ imadi(e, d, k0, 0xffffffffu, base); /* base - k0 */ shr_i(e, neg, d, 31); From fa609ba062307c01a980dbe19588d326a31e2493 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 14:24:07 -0500 Subject: [PATCH 03/13] mk/prime_race: binaries carry +dirty and rebuild when the commit label changes Both prime race binaries were labelled with the commit before the one their code came from (built moments before committing; make saw no source change after). The label now gets +dirty when src, bench/prime_race or mk have uncommitted changes, and a stamp file that changes with the label is a dependency, so a rebuild after a commit refreshes the label (same fix as aien-yardsticks a8b24a1). Co-Authored-By: Claude Opus 5.5 --- mk/prime_race.mk | 13 ++++++++++--- mk/prime_race_gb10.mk | 17 ++++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/mk/prime_race.mk b/mk/prime_race.mk index e3072a4f7..5f82b3d06 100644 --- a/mk/prime_race.mk +++ b/mk/prime_race.mk @@ -6,19 +6,26 @@ # prime-race-host-test trial-division oracle test of cpu_base + protocol formatting/parsing ifndef PRIME_RACE_MK PRIME_RACE_MK := 1 -.PHONY: prime-race-cpu prime-race-host-test +.PHONY: prime-race-cpu prime-race-host-test PRIME_RACE_CPU_FORCE +PRIME_RACE_CPU_FORCE: PR_DIR = bench/prime_race PR_BUILD = $(PR_DIR)/build PR_WARN = -std=gnu11 -Wall -Wextra -Werror -D_GNU_SOURCE PR_OPT = -O3 -mcpu=native -PR_SOURCE_COMMIT_VAL := $(shell git rev-parse HEAD 2>/dev/null || echo unknown) +# +dirty when the code that goes into the binary has uncommitted changes; the stamp changes with +# the label, so a binary never carries a stale commit. +PR_SOURCE_COMMIT_VAL := $(shell git rev-parse HEAD 2>/dev/null || echo unknown)$(shell git status --porcelain -- bench/prime_race mk 2>/dev/null | head -c1 | sed 's/.\+/+dirty/') +PR_LABEL_STAMP = $(PR_BUILD)/.pr_source_label PR_DEFS = -DPR_SOURCE_COMMIT='"$(PR_SOURCE_COMMIT_VAL)"' -DPR_CC='"$(CC)"' -DPR_CFLAGS='"$(PR_WARN) $(PR_OPT)"' PR_HDRS = $(PR_DIR)/prime_race_impl.h -$(PR_BUILD)/cpu_base: $(PR_DIR)/cpu_base.c $(PR_DIR)/prime_race_impl.c $(PR_HDRS) +$(PR_BUILD)/cpu_base: $(PR_DIR)/cpu_base.c $(PR_DIR)/prime_race_impl.c $(PR_HDRS) $(PR_LABEL_STAMP) @mkdir -p $(PR_BUILD) $(CC) $(PR_WARN) $(PR_OPT) $(PR_DEFS) -o $@ $(PR_DIR)/cpu_base.c $(PR_DIR)/prime_race_impl.c -lm prime-race-cpu: $(PR_BUILD)/cpu_base +$(PR_LABEL_STAMP): PRIME_RACE_CPU_FORCE + @mkdir -p $(PR_BUILD) + @echo '$(PR_SOURCE_COMMIT_VAL)' > $@.tmp; cmp -s $@.tmp $@ && rm $@.tmp || mv $@.tmp $@ $(PR_BUILD)/prime_race_host_test: $(PR_DIR)/tests/prime_race_host_test.c $(PR_DIR)/cpu_base.c $(PR_DIR)/prime_race_impl.c $(PR_HDRS) @mkdir -p $(PR_BUILD) diff --git a/mk/prime_race_gb10.mk b/mk/prime_race_gb10.mk index 0c78729f6..eeec33600 100644 --- a/mk/prime_race_gb10.mk +++ b/mk/prime_race_gb10.mk @@ -1,3 +1,7 @@ +# The label carries +dirty when the code that goes into the binary has uncommitted changes, and +# the stamp below changes whenever the label does, so a binary can never carry a stale commit. +GB_SRC_COMMIT := $(shell git rev-parse HEAD 2>/dev/null || echo unknown)$(shell git status --porcelain -- src bench/prime_race mk 2>/dev/null | grep -v ' bench/prime_race/build/' | head -c1 | sed 's/.\+/+dirty/') +GB_LABEL_STAMP = $(GB_BUILD)/.gb_source_label # AIEN Prime Drag Race: native GB10 implementation (bench/prime_race/gb10_native.c) on Omega's # own Blackwell codegen + session (libomega_gpu.a, no CUDA). Picked up by `-include mk/*.mk`; # not part of `all` or `test`. Needs PHYSICS_DIR at the physics.lock commit (libomega_gpu). @@ -10,14 +14,18 @@ # chip launches need GB10_CHIP_RUN=1 and the GPU lock) ifndef PRIME_RACE_GB10_MK PRIME_RACE_GB10_MK := 1 -.PHONY: prime-race-gb10 prime-race-gb10-host-test prime-race-gb10-launch-cost +.PHONY: prime-race-gb10 prime-race-gb10-host-test prime-race-gb10-launch-cost PRIME_RACE_FORCE +PRIME_RACE_FORCE: GB_DIR = bench/prime_race GB_BUILD = $(GB_DIR)/build GB_OPT = -O3 -mcpu=native -GB_SRC_COMMIT := $(shell git rev-parse HEAD 2>/dev/null || echo unknown) +# The label carries +dirty when code that goes into the binary has uncommitted changes, and the +# stamp below changes whenever the label does, so a binary never carries a stale commit. +GB_SRC_COMMIT := $(shell git rev-parse HEAD 2>/dev/null || echo unknown)$(shell git status --porcelain -- src bench/prime_race mk 2>/dev/null | head -c1 | sed 's/.\+/+dirty/') +GB_LABEL_STAMP = $(GB_BUILD)/.gb_source_label GB_DEFS = -DPR_SOURCE_COMMIT='"$(GB_SRC_COMMIT)"' -DPR_CC='"$(CC)"' \ -DPR_CFLAGS='"$(GB_OPT) -I$(GB_DIR) + omega CFLAGS; libomega_gpu.a built with omega CFLAGS"' -GB_DEPS = $(GB_DIR)/gb10_native.c $(GB_DIR)/prime_race_impl.c $(GB_DIR)/prime_race_impl.h \ +GB_DEPS = $(GB_DIR)/gb10_native.c $(GB_LABEL_STAMP) $(GB_DIR)/prime_race_impl.c $(GB_DIR)/prime_race_impl.h \ src/omega_gpu_elementwise_api.h src/omega_gpu_session.h $(OUT_DIR)/libomega_gpu.a # Same pattern as tools/chip_run.sh REFUSAL:cuda GB_NM_CHECK = if nm -u $@ | grep -Eiq 'cuda|cuInit|cuLaunch|nvrtc|cublas'; then echo "CUDA symbols in $@"; rm -f $@; exit 1; fi @@ -31,6 +39,9 @@ $(GB_BUILD)/gb10_native_mutant: $(GB_DEPS) $(CC) $(CFLAGS) $(GB_OPT) -I$(GB_DIR) $(GB_DEFS) -DPR_GB10_MUTANT=1 -o $@ $(GB_DIR)/gb10_native.c $(GB_DIR)/prime_race_impl.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm @$(GB_NM_CHECK) prime-race-gb10: $(GB_BUILD)/gb10_native $(GB_BUILD)/gb10_native_mutant +$(GB_LABEL_STAMP): PRIME_RACE_FORCE + @mkdir -p $(GB_BUILD) + @echo '$(GB_SRC_COMMIT)' > $@.tmp; cmp -s $@.tmp $@ && rm $@.tmp || mv $@.tmp $@ $(GB_BUILD)/gb10_sieve_host_test: $(GB_DIR)/tests/gb10_sieve_host_test.c $(GB_DIR)/prime_race_impl.h src/omega_blackwell_codegen.h src/omega_gpu_elementwise_api.h $(OUT_DIR)/libomega_gpu.a @mkdir -p $(GB_BUILD) From 8190badb0afb509a84798166334dd65e0da18c7a Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 14:33:43 -0500 Subject: [PATCH 04/13] mk/prime_race_gb10: drop the duplicate label block at the top of the file An editing slip in 0dc96dc left a second copy of GB_SRC_COMMIT/GB_LABEL_STAMP above the header comment. The later definition already won, so behavior is unchanged. Co-Authored-By: Claude Opus 5.5 --- mk/prime_race_gb10.mk | 4 ---- 1 file changed, 4 deletions(-) diff --git a/mk/prime_race_gb10.mk b/mk/prime_race_gb10.mk index eeec33600..fe94c8c65 100644 --- a/mk/prime_race_gb10.mk +++ b/mk/prime_race_gb10.mk @@ -1,7 +1,3 @@ -# The label carries +dirty when the code that goes into the binary has uncommitted changes, and -# the stamp below changes whenever the label does, so a binary can never carry a stale commit. -GB_SRC_COMMIT := $(shell git rev-parse HEAD 2>/dev/null || echo unknown)$(shell git status --porcelain -- src bench/prime_race mk 2>/dev/null | grep -v ' bench/prime_race/build/' | head -c1 | sed 's/.\+/+dirty/') -GB_LABEL_STAMP = $(GB_BUILD)/.gb_source_label # AIEN Prime Drag Race: native GB10 implementation (bench/prime_race/gb10_native.c) on Omega's # own Blackwell codegen + session (libomega_gpu.a, no CUDA). Picked up by `-include mk/*.mk`; # not part of `all` or `test`. Needs PHYSICS_DIR at the physics.lock commit (libomega_gpu). From 1c4209b3d0e04ded2c04bee716a91d085a483be1 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 14:33:43 -0500 Subject: [PATCH 05/13] omega_gpu: shared-memory staging of the sieve prime table (campaign C3) Launcher (general): OmegaGpuLaunch.shared_bytes sets the QMD shared memory size (SHARED_MEMORY_SIZE_SHIFTED7, clcec0qmd.h MW(1162:1152)); 0 keeps the old 1024-byte default, so every existing caller builds the same QMD word. omega_blackwell_build_qmd1 refuses more than 16384 bytes (target SM config 9 is not documented in bytes; 16 KB stays well inside it). Kernel (sieve-specific): OMEGA_GPU_EW_PRIME_SIEVE_SHARED copies the {k0, p, magic} table into shared memory once per CTA (strided by the 128 threads, then BAR.SYNC), and the prime loop reads it with three grouped LDS instead of three uncached LDG. The default PRIME_SIEVE kernel is byte-identical (sha256 22a27c6c...). gb10_native picks the staged kernel when the table fits (limit <= 127667400) and the global one above that; the detail JSON says which. Host test: the IR simulator now models shared memory, barriers and cross-thread races (resumable threads, per-phase write tags, poisoned fill). The staged kernel matches the oracle on 167 cases (all sweeps + CTA budgets 1/2/128/256 at their edges), 0 races, 0 scoreboard hazards, nvdisasm decodes LDS/STS/BAR.SYNC with all 8 branch targets right. Negative controls: barrier removed -> race caught; table one entry short -> out-of-bounds caught. 48 checks PASS. Co-Authored-By: Claude Opus 5.5 --- bench/prime_race/gb10_native.c | 35 ++++- bench/prime_race/tests/gb10_sieve_host_test.c | 136 ++++++++++++++++-- src/omega_blackwell_qmd.c | 7 +- src/omega_blackwell_qmd.h | 5 + src/omega_gpu_elementwise_api.c | 60 ++++++-- src/omega_gpu_elementwise_api.h | 3 +- src/omega_gpu_session.c | 3 +- src/omega_gpu_session.h | 1 + 8 files changed, 216 insertions(+), 34 deletions(-) diff --git a/bench/prime_race/gb10_native.c b/bench/prime_race/gb10_native.c index e07ad5e8b..5a4dd2bc0 100644 --- a/bench/prime_race/gb10_native.c +++ b/bench/prime_race/gb10_native.c @@ -42,6 +42,7 @@ #define GB_POISON 0xffbadbadu #define GB_MAX_LIMIT 0xffffffffull /* kernel range: base, k0 < 2^31 */ #define GB_BUDGET_MAX 4096u +#define GB_ARGS 11u /* a, b, c (64-bit each) then P0..P4 */ /* PR_GB10_CTA_BUDGET or the I42 default; 0 on a malformed or out-of-range value. */ static uint32_t gb_cta_budget(void) { @@ -59,6 +60,8 @@ typedef struct { uint32_t root; /* isqrt(limit) */ uint32_t nprimes; /* odd primes in the table of the last pass (sentinel excluded) */ size_t table_cap; /* entries, sentinel included */ + uint32_t entries; /* table entries for this limit, sentinel included (fixed by the limit) */ + uint32_t shared_bytes; /* 0: global-table kernel; else the staged kernel's shared memory per CTA */ OmegaBlackwellKernel k; NvrmMem code, out, table; uint64_t diag_ns, launches; @@ -75,6 +78,22 @@ static uint32_t isqrt_u64(uint64_t n) { return (uint32_t)r; } +/* odd primes <= root (the table the pass builds, without the sentinel) */ +static uint32_t gb_count_odd_primes(uint32_t root) { + uint32_t n = 0, half = root / 2 + 1; + uint8_t *flags = calloc(half, 1); + if (!flags) return 0xfffffffeu; /* makes the per-pass check fail closed */ + for (uint32_t i = 1; i < half; i++) { + uint32_t p = 2 * i + 1; + if (p > root) break; + if (flags[i]) continue; + for (uint64_t c = (uint64_t)p * p; c <= root; c += 2ull * p) flags[c / 2] = 1; + n++; + } + free(flags); + return n; +} + static void gb_release(gb_state *s) { if (s->locked) { omega_gpu_session_free(&s->table); @@ -109,8 +128,12 @@ static int gb_setup(void **state, uint64_t limit) { fprintf(stderr, "gb10-native: isqrt(%" PRIu64 ") = %u is wrong\n", limit, s->root); return -1; /* fail closed */ } s->table_cap = (size_t)s->root / 2 + 2; /* every odd number <= root, plus the sentinel */ + s->entries = gb_count_odd_primes(s->root) + 1; + /* campaign C3: stage the table in shared memory whenever it fits the QMD bound */ + if ((uint64_t)s->entries * 12 <= OMEGA_BW_QMD_MAX_SHARED_BYTES) s->shared_bytes = s->entries * 12; + OmegaGpuEwOp op = s->shared_bytes ? OMEGA_GPU_EW_PRIME_SIEVE_SHARED : OMEGA_GPU_EW_PRIME_SIEVE; - if (omega_gpu_elementwise_codegen_ir(OMEGA_GPU_EW_PRIME_SIEVE, PR_GB10_MUTANT, NULL, &s->k) != OMEGA_GPU_EW_OK) { + if (omega_gpu_elementwise_codegen_ir(op, PR_GB10_MUTANT, NULL, &s->k) != OMEGA_GPU_EW_OK) { fprintf(stderr, "gb10-native: kernel codegen failed\n"); return -1; } omega_gpu_session_lock(); @@ -152,16 +175,18 @@ static int gb_pass(void *state, uint64_t limit) { free(flags); tab[3 * n + 0] = 1; tab[3 * n + 1] = 0; tab[3 * n + 2] = 0xffffffffu; /* sentinel: ends every scan */ s->nprimes = n; + if (n + 1 != s->entries) { fprintf(stderr, "gb10-native: %u table entries, setup counted %u\n", n + 1, s->entries); return -1; } __asm__ volatile("dsb sy" ::: "memory"); - uint32_t args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS] = {0}; + uint32_t args[GB_ARGS] = {0}; args[0] = (uint32_t)s->table.va; args[1] = (uint32_t)(s->table.va >> 32); args[4] = (uint32_t)s->out.va; args[5] = (uint32_t)(s->out.va >> 32); args[6] = s->nwords; args[7] = s->stride; args[8] = s->lastw; args[9] = s->tailinv; + args[10] = s->entries * 3; /* P4: table words, read by the staged kernel only */ OmegaGpuLaunch L = { .code_va = s->code.va, .gpr_count = GB_GPR, .threads_x = GB_THREADS, .threads_y = 1, .grid_x = s->ctas, .grid_y = 1, .num_elements = s->nwords ? s->nwords : 1, - .args = args, .n_args = OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, .timeout_ms = GB_WAIT_MS, + .args = args, .n_args = GB_ARGS, .timeout_ms = GB_WAIT_MS, .shared_bytes = s->shared_bytes, }; uint64_t ns = 0; uint32_t marker = 0; if (omega_gpu_session_launch(&L, &ns, &marker) != 0) { @@ -188,8 +213,8 @@ static uint64_t gb_threads(void *state) { gb_state *s = state; return s ? s->str static int gb_detail(void *state, FILE *f) { gb_state *s = state; if (!s) return -1; - fprintf(f, "{\"grid\": %u, \"cta_budget\": %u, \"block\": %u, \"words\": %u, \"nprimes\": %u, \"kernel_code_sha256\": \"", - s->ctas, s->budget, GB_THREADS, s->nwords, s->nprimes); + fprintf(f, "{\"grid\": %u, \"cta_budget\": %u, \"block\": %u, \"words\": %u, \"nprimes\": %u, \"table\": \"%s\", \"shared_bytes\": %u, \"kernel_code_sha256\": \"", + s->ctas, s->budget, GB_THREADS, s->nwords, s->nprimes, s->shared_bytes ? "shared" : "global", s->shared_bytes); for (int i = 0; i < 32; i++) fprintf(f, "%02x", s->k.code_digest[i]); fprintf(f, "\", \"kernel_insns\": %zu, \"kernel_gpr\": %u, \"mutant\": %d, \"launches\": %" PRIu64 ", \"diag_kernel_ns_total\": %" PRIu64 diff --git a/bench/prime_race/tests/gb10_sieve_host_test.c b/bench/prime_race/tests/gb10_sieve_host_test.c index 577d1677d..1be5d3b39 100644 --- a/bench/prime_race/tests/gb10_sieve_host_test.c +++ b/bench/prime_race/tests/gb10_sieve_host_test.c @@ -101,15 +101,30 @@ static uint32_t lut3(uint32_t lut, uint32_t a, uint32_t b, uint32_t c) { } static uint64_t g_steps; -static void sim_thread(const BlackwellIRProgram *p, uint32_t tid, uint32_t cta) { - Thr t; memset(&t, 0, sizeof t); +/* Shared memory of the CTA being simulated (campaign C3). g_smem_bytes is what the launch + * declares; an access past it is an error. Race check: a word written in barrier phase k may + * not be read by a different thread in the same phase (no BAR_SYNC between them). */ +#define SIM_SMEM_WORDS 4096 +static uint32_t g_smem[SIM_SMEM_WORDS], g_smem_wphase[SIM_SMEM_WORDS], g_smem_wtid[SIM_SMEM_WORDS]; +static uint32_t g_smem_bytes, g_phase, g_smem_short; /* g_smem_short: negative control only */ +static uint64_t g_races; +static uint32_t *smem_at(uint32_t a, uint32_t tid, int write) { + if ((a & 3) || a + 4 > g_smem_bytes || a / 4 >= SIM_SMEM_WORDS) { g_sim_err = 5; return NULL; } + uint32_t w = a / 4; + if (write) { g_smem_wphase[w] = g_phase + 1; g_smem_wtid[w] = tid; } + else if (g_smem_wphase[w] == g_phase + 1 && g_smem_wtid[w] != tid) { g_races++; g_sim_err = 6; return NULL; } + return &g_smem[w]; +} +/* Runs one thread until EXIT (t->done = 1) or a BAR_SYNC (returns with pc past it). */ +static void sim_thread(const BlackwellIRProgram *p, Thr *tp, uint32_t tid, uint32_t cta) { + Thr t = *tp; for (uint64_t guard = 0; guard < 400000000ull; guard++) { if (t.pc < 0 || (size_t)t.pc >= p->count) { g_sim_err = 1; return; } const BlackwellIRInsn *in = &p->insns[t.pc]; uint32_t a = rd(&t, in->src1_vreg, p), b = rd(&t, in->src2_vreg, p), c = rd(&t, in->src3_vreg, p); g_steps++; switch (in->op) { - case BW_IR_EXIT: if (!in->predicate_p0 || t.p0) return; break; + case BW_IR_EXIT: if (!in->predicate_p0 || t.p0) { t.done = 1; *tp = t; return; } break; case BW_IR_BRA: { int take = !in->predicate_p0 || (in->predicate_not ? !t.p0 : t.p0); if (take) { if ((int32_t)in->imm == 0) { g_sim_err = 2; return; } t.pc += (int32_t)in->imm; continue; } @@ -131,6 +146,9 @@ static void sim_thread(const BlackwellIRProgram *p, uint32_t tid, uint32_t cta) case BW_IR_ISETP_GE_U32: t.p0 = a >= b; break; 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; } case BW_IR_STG_E: { uint32_t *m = mem_at(rd64(&t, in->src1_vreg, p)); if (!m) return; *m = b; break; } + case BW_IR_LDS32: { uint32_t *m = smem_at(a, tid, 0); if (!m) return; wr(&t, in->dst_vreg, *m, p); break; } + case BW_IR_STS32: { uint32_t *m = smem_at(a, tid, 1); if (!m) return; *m = b; break; } + case BW_IR_BAR_SYNC: t.pc++; *tp = t; return; default: g_sim_err = 100 + (int)in->op; return; } t.pc++; @@ -138,9 +156,25 @@ static void sim_thread(const BlackwellIRProgram *p, uint32_t tid, uint32_t cta) g_sim_err = 4; } +/* One CTA: every thread runs to its next BAR_SYNC or EXIT, then the next barrier phase starts. + * Exited threads do not hold up a barrier (they are no longer counted). Shared memory starts + * poisoned, so a read of a word nobody staged gives a wrong answer the oracle sees. */ +static void sim_cta(const BlackwellIRProgram *p, uint32_t cta) { + static Thr th[OMEGA_GPU_EW_THREADS]; + memset(th, 0, sizeof th); + for (int i = 0; i < SIM_SMEM_WORDS; i++) { g_smem[i] = 0xfeedfaceu; g_smem_wphase[i] = 0; } + for (g_phase = 0; !g_sim_err; g_phase++) { + int live = 0; + for (uint32_t tid = 0; tid < OMEGA_GPU_EW_THREADS && !g_sim_err; tid++) + if (!th[tid].done) { sim_thread(p, &th[tid], tid, cta); live += !th[tid].done; } + if (!live) return; + if (g_phase > 1000) { g_sim_err = 7; return; } + } +} + /* Mirrors gb10_native.c's pass on the host: same table, same argument words; `budget` * is the CTA cap (PR_GB10_CTA_BUDGET, default OMEGA_GPU_EW_MAX_CTAS). */ -static int sim_sieve(const BlackwellIRProgram *p, uint64_t limit, uint32_t budget, uint64_t *words) { +static int sim_sieve(const BlackwellIRProgram *p, uint64_t limit, uint32_t budget, int staged, uint64_t *words) { uint64_t odd = pr_odd_count(limit); uint32_t nwords = (uint32_t)((odd + 31) / 32); uint32_t blocks = (nwords + OMEGA_GPU_EW_THREADS - 1) / OMEGA_GPU_EW_THREADS; @@ -166,11 +200,13 @@ static int sim_sieve(const BlackwellIRProgram *p, uint64_t limit, uint32_t budge g_cb[0x380 / 4] = (uint32_t)ta; g_cb[0x380 / 4 + 1] = (uint32_t)(ta >> 32); g_cb[0x390 / 4] = (uint32_t)oa; g_cb[0x390 / 4 + 1] = (uint32_t)(oa >> 32); g_cb[0x398 / 4] = nwords; g_cb[0x39c / 4] = stride; g_cb[0x3a0 / 4] = lastw; g_cb[0x3a4 / 4] = tailinv; + g_cb[0x3a8 / 4] = (n + 1) * 3; /* P4: table words (staged kernel) */ + g_smem_bytes = staged ? (n + 1) * 12 - g_smem_short : 0; /* what gb10_native.c declares */ g_ranges[0] = (Range){ (uintptr_t)tab, (uintptr_t)tab + cap * 12 }; g_ranges[1] = (Range){ (uintptr_t)out, (uintptr_t)out + (size_t)nwords * 4 }; g_sim_err = 0; for (uint32_t cta = 0; cta < ctas && !g_sim_err; cta++) - for (uint32_t tid = 0; tid < OMEGA_GPU_EW_THREADS && !g_sim_err; tid++) sim_thread(p, tid, cta); + sim_cta(p, cta); size_t nw64 = pr_words64(limit); memset(words, 0, nw64 * 8); memcpy(words, out, (size_t)nwords * 4); @@ -201,7 +237,9 @@ static int sb_hazards(const BlackwellIRProgram *p, int verbose) { /* ------------------------------------------------------------- nvdisasm */ /* every word decodes, the new ops appear, and every BRA lands where the IR says */ -static void nvdisasm_check(const OmegaBlackwellKernel *k, const BlackwellIRProgram *p) { +static const char *const MUST_GLOBAL[] = { "LOP3.LUT", "SHF.L.U32", "IMAD.HI.U32", "@P0 BRA", "@!P0 BRA", "@P0 EXIT", "STG.E", "LDG.E" }; +static const char *const MUST_STAGED[] = { "LDS", "STS", "BAR.SYNC", "LDG.E", "STG.E", "IMAD.HI.U32", "@P0 BRA", "@!P0 BRA" }; +static void nvdisasm_check(const OmegaBlackwellKernel *k, const BlackwellIRProgram *p, const char *const *must, const char *tag) { const char *nv = "/usr/local/cuda/bin/nvdisasm"; if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check SKIPPED\n"); return; } char bin[64], lst[64], cmd[256]; @@ -216,7 +254,6 @@ static void nvdisasm_check(const OmegaBlackwellKernel *k, const BlackwellIRProgr CHECK(rc == 0, "nvdisasm rc %d", rc); FILE *l = fopen(lst, "r"); if (!l) { CHECK(0, "cannot read %s", lst); return; } - static const char *must[] = { "LOP3.LUT", "SHF.L.U32", "IMAD.HI.U32", "@P0 BRA", "@!P0 BRA", "@P0 EXIT", "STG.E", "LDG.E" }; int seen[8] = {0}, insns = 0, bad = 0, bra_ok = 0, bra_bad = 0; char line[512]; while (fgets(line, sizeof line, l)) { @@ -240,7 +277,7 @@ static void nvdisasm_check(const OmegaBlackwellKernel *k, const BlackwellIRProgr for (int i = 0; i < 8; i++) CHECK(seen[i], "nvdisasm listing lacks %s (%s)", must[i], lst); CHECK(bad == 0, "%d undecodable words (%s)", bad, lst); CHECK(bra_bad == 0 && bra_ok >= 6, "branch targets: %d match, %d differ", bra_ok, bra_bad); - printf("nvdisasm: %d words, %d undecodable, %d branch targets checked (%s)\n", insns, bad, bra_ok, lst); + printf("nvdisasm (%s): %d words, %d undecodable, %d branch targets checked (%s)\n", tag, insns, bad, bra_ok, lst); if (g_failed == 0) unlink(lst); } @@ -319,7 +356,7 @@ int main(int argc, char **argv) { printf("\n"); CHECK(k.gpr_count <= 64, "GPR budget %u > 64", k.gpr_count); CHECK(memcmp(k.code_digest, mk.code_digest, 32) != 0, "mutant kernel differs from production"); - nvdisasm_check(&k, prog); + nvdisasm_check(&k, prog, MUST_GLOBAL, "global table"); { int hz = sb_hazards(prog, 1); CHECK(hz == 0, "scoreboard hazards in the sieve kernel: %d", hz); @@ -347,7 +384,7 @@ int main(int argc, char **argv) { uint64_t L = i < nl ? lims[i] : 10000000ull; uint64_t *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); if (!want || !got) { CHECK(0, "alloc"); break; } - int src = sim_sieve(prog, L, OMEGA_GPU_EW_MAX_CTAS, got); + int src = sim_sieve(prog, L, OMEGA_GPU_EW_MAX_CTAS, 0, got); size_t nw = pr_words64(L), bad = nw; for (size_t j = 0; j < nw; j++) if (got[j] != want[j]) { bad = j; break; } if (src != 0 || bad != nw) { @@ -372,7 +409,7 @@ int main(int argc, char **argv) { for (size_t i = 0; i < 4; i++, bn++) { uint64_t L = ls[i], *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); if (!want || !got) { CHECK(0, "alloc"); free(want); free(got); break; } - int src = sim_sieve(prog, L, budgets[b], got); + int src = sim_sieve(prog, L, budgets[b], 0, got); if (src != 0 || memcmp(got, want, pr_words64(L) * 8) != 0) { bfail++; printf(" budget %u limit %" PRIu64 ": sim rc %d err %d, differs\n", budgets[b], L, src, g_sim_err); @@ -387,11 +424,86 @@ int main(int argc, char **argv) { /* 5. mutant must be caught */ { uint64_t L = 100000, *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); - int src = want && got ? sim_sieve(mprog, L, OMEGA_GPU_EW_MAX_CTAS, got) : -1; + int src = want && got ? sim_sieve(mprog, L, OMEGA_GPU_EW_MAX_CTAS, 0, got) : -1; CHECK(src == 0 && want && memcmp(got, want, pr_words64(L) * 8) != 0, "mutant kernel not caught at 1e5 (sim rc %d)", src); free(want); free(got); } + /* 5b. staged kernel (campaign C3): table copied to shared memory, then LDS. Same sweeps as the + * global kernel, the hazard walk, the mutant, and two negative controls on the new machinery: + * the barrier removed (the race check must fire) and too little shared memory declared. */ + { + BlackwellIRProgram *sp = calloc(1, sizeof *sp), *smp = calloc(1, sizeof *smp), *nobar = malloc(sizeof *nobar); + OmegaBlackwellKernel sk, smk; + memset(&sk, 0, sizeof sk); memset(&smk, 0, sizeof smk); + int src1 = sp ? omega_gpu_elementwise_codegen_ir(OMEGA_GPU_EW_PRIME_SIEVE_SHARED, 0, sp, &sk) : -1; + int src2 = smp ? omega_gpu_elementwise_codegen_ir(OMEGA_GPU_EW_PRIME_SIEVE_SHARED, 1, smp, &smk) : -1; + CHECK(src1 == OMEGA_GPU_EW_OK && src2 == OMEGA_GPU_EW_OK && nobar, "staged codegen rc %d mutant rc %d", src1, src2); + if (src1 == OMEGA_GPU_EW_OK && src2 == OMEGA_GPU_EW_OK && nobar) { + printf("staged kernel: %zu IR insns, %u GPRs, sha256 ", sp->count, sk.gpr_count); + for (int i = 0; i < 32; i++) printf("%02x", sk.code_digest[i]); + printf("\n"); + CHECK(sk.gpr_count <= 64, "staged GPR budget %u > 64", sk.gpr_count); + nvdisasm_check(&sk, sp, MUST_STAGED, "staged"); + int hz = sb_hazards(sp, 1); + CHECK(hz == 0, "scoreboard hazards in the staged kernel: %d", hz); + int sfail = 0, sn = 0; + for (size_t i = 0; i < nl; i++, sn++) { + uint64_t L = lims[i], *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); + if (!want || !got) { CHECK(0, "alloc"); free(want); free(got); break; } + int r = sim_sieve(sp, L, OMEGA_GPU_EW_MAX_CTAS, 1, got); + if (r != 0 || memcmp(got, want, pr_words64(L) * 8) != 0) { + if (!sfail++) printf(" staged limit %" PRIu64 ": sim rc %d err %d, differs\n", L, r, g_sim_err); + } + if (L == 1000000) CHECK(count_primes(got, L) == 78498, "staged pi(1e6) = %" PRIu64, count_primes(got, L)); + free(want); free(got); + } + static const uint32_t budgets[] = { 1, 2, 128, 256 }; + for (size_t b = 0; b < sizeof budgets / sizeof budgets[0]; b++) { + uint64_t edge = 64ull * budgets[b] * OMEGA_GPU_EW_THREADS; + const uint64_t ls[] = { edge - 1, edge, edge + 1 }; + for (size_t i = 0; i < 3; i++, sn++) { + uint64_t L = ls[i], *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); + if (!want || !got) { CHECK(0, "alloc"); free(want); free(got); break; } + int r = sim_sieve(sp, L, budgets[b], 1, got); + if (r != 0 || memcmp(got, want, pr_words64(L) * 8) != 0) { + if (!sfail++) printf(" staged budget %u limit %" PRIu64 ": sim rc %d err %d\n", budgets[b], L, r, g_sim_err); + } + free(want); free(got); + } + } + CHECK(sfail == 0, "staged kernel differs from the oracle in %d of %d cases", sfail, sn); + printf("staged simulator: %d cases, %d differ, races seen %" PRIu64 "\n", sn, sfail, g_races); + CHECK(g_races == 0, "staged kernel: %" PRIu64 " shared-memory races", g_races); + + uint64_t L = 100000, *want = oracle_bitmap(L), *got = calloc(pr_words64(L) + 1, 8); + int r = want && got ? sim_sieve(smp, L, OMEGA_GPU_EW_MAX_CTAS, 1, got) : -1; + CHECK(r == 0 && want && memcmp(got, want, pr_words64(L) * 8) != 0, "staged mutant not caught at 1e5 (sim rc %d)", r); + + /* negative control 1: BAR_SYNC turned into a no-op -> a thread reads a word another + * thread staged in the same phase; the race check must stop the run */ + memcpy(nobar, sp, sizeof *nobar); + int bars = 0; + for (size_t i = 0; i < nobar->count; i++) + if (nobar->insns[i].op == BW_IR_BAR_SYNC) { nobar->insns[i].op = BW_IR_MOV_RZ; nobar->insns[i].dst_vreg = -1; bars++; } + uint64_t races0 = g_races; + r = got ? sim_sieve(nobar, L, OMEGA_GPU_EW_MAX_CTAS, 1, got) : -1; + CHECK(bars == 1 && r != 0 && g_sim_err == 6 && g_races > races0, + "barrier removed: race not caught (bars %d, rc %d, err %d)", bars, r, g_sim_err); + printf("negative control, barrier removed: rc %d err %d (6 = race)\n", r, g_sim_err); + + /* negative control 2: declare 12 bytes (one entry) too little shared memory; the + * staging copy must run off the end and stop */ + g_smem_short = 12; + r = got ? sim_sieve(sp, L, OMEGA_GPU_EW_MAX_CTAS, 1, got) : -1; + g_smem_short = 0; + CHECK(r != 0 && g_sim_err == 5, "short shared declaration not caught (rc %d, err %d)", r, g_sim_err); + printf("negative control, shared memory one entry short: rc %d err %d (5 = out of bounds)\n", r, g_sim_err); + free(want); free(got); + } + free(sk.code); free(smk.code); free(sp); free(smp); free(nobar); + } + /* 6. division model */ magic_model(1000000); if (big) magic_model(10000000); diff --git a/src/omega_blackwell_qmd.c b/src/omega_blackwell_qmd.c index 43bac827e..417c51c6b 100644 --- a/src/omega_blackwell_qmd.c +++ b/src/omega_blackwell_qmd.c @@ -111,8 +111,11 @@ int omega_blackwell_build_qmd1(uint32_t *qmd1_words, const OmegaBlackwellQmdConf if (gpr < 16) gpr = 16; qmd1_words[35] = (tz & 0xff) | ((gpr & 0x1ff) << 8) | (1u << 17); - /* Word 36: Shared memory config (8 shifted 7 = 1024 bytes) */ - qmd1_words[36] = 0x04b44808; + /* Word 36: SHARED_MEMORY_SIZE_SHIFTED7 in bits 10:0 (default 8 = 1024 bytes), then the + * MIN/MAX/TARGET_SM_CONFIG_SHARED_MEM_SIZE fields (9, 26, 9) kept as they were. */ + if (cfg->shared_bytes > OMEGA_BW_QMD_MAX_SHARED_BYTES) return -1; + uint32_t smem7 = cfg->shared_bytes ? (cfg->shared_bytes + 127u) / 128u : 8u; + qmd1_words[36] = (0x04b44808u & ~0x7ffu) | smem7; /* Word 37: SHADER_LOCAL_MEMORY_HIGH_SIZE_SHIFTED4 = 0 (SKEDCHECK05 compliance) */ qmd1_words[37] = 0x00000000; diff --git a/src/omega_blackwell_qmd.h b/src/omega_blackwell_qmd.h index 5d2809c13..f1c7018f0 100644 --- a/src/omega_blackwell_qmd.h +++ b/src/omega_blackwell_qmd.h @@ -33,7 +33,12 @@ typedef struct { uint32_t grid_x; uint32_t grid_y; uint32_t gpr_count; + /* Shared memory per CTA in bytes (clcec0qmd.h SHARED_MEMORY_SIZE_SHIFTED7, 128-byte units). + * 0 keeps the long-standing 1024. At most OMEGA_BW_QMD_MAX_SHARED_BYTES, well inside the + * TARGET_SM_CONFIG this QMD declares (word 36). */ + uint32_t shared_bytes; } OmegaBlackwellQmdConfig; +#define OMEGA_BW_QMD_MAX_SHARED_BYTES 16384u /* Build QMD 0: GRID_NULL null-barrier grid with dependence counter 2 */ int omega_blackwell_build_qmd0(uint32_t *qmd0_words, uint64_t qmd0_va, uint64_t qmd1_va); diff --git a/src/omega_gpu_elementwise_api.c b/src/omega_gpu_elementwise_api.c index c83a1b9df..34dd61fa1 100644 --- a/src/omega_gpu_elementwise_api.c +++ b/src/omega_gpu_elementwise_api.c @@ -95,17 +95,19 @@ static void lop3xor(Em *e, int dst, int a, int b) { BlackwellIRInsn i = I0(BW_IR /* dst64 = idx * 4 + ptr64 (byte address of element idx) */ static void addr4(Em *e, int dst64, int idx, int ptr64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = dst64; i.src1_vreg = idx; i.imm = 4; i.src3_vreg = ptr64; emit(e, i, K_FIXED); } static void ldg(Em *e, int dst, int addr64, int udesc) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = dst; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, K_VAR); } -/* Independent global loads issued back to back (campaign C2a, 2026-10-05). The first load +/* Independent loads (LDG_E from global, or LDS32 from shared memory with ureg NOREG) issued + * back to back (campaign C2a, 2026-10-05). The first load * waits on whatever is pending; the others wait on nothing, so all n are in flight together. * Every load sets SB_GPR (a counting scoreboard), so the next instruction waits once for all * of them instead of once per load. Fails the build if a load address is a destination of the * group or a destination repeats (the later loads would read an operand still in flight). */ -static void ldg_group(Em *e, int n, const int *dst, const int *addr64, int udesc) { +static void ld_group(Em *e, BlackwellIROpcode op, int n, const int *dst, const int *addr, int ureg) { for (int i = 0; i < n; i++) for (int k = 0; k < n; k++) - if (addr64[i] == dst[k] || (k != i && dst[i] == dst[k])) { e->err = -1; return; } + if (addr[i] == dst[k] || (k != i && dst[i] == dst[k])) { e->err = -1; return; } for (int i = 0; i < n; i++) { - BlackwellIRInsn in = I0(BW_IR_LDG_E); in.dst_vreg = dst[i]; in.src1_vreg = addr64[i]; in.ureg = udesc; + if (op != BW_IR_LDG_E && op != BW_IR_LDS32) { e->err = -1; return; } + BlackwellIRInsn in = I0(op); in.dst_vreg = dst[i]; in.src1_vreg = addr[i]; in.ureg = ureg; if (i == 0) { emit(e, in, K_VAR); continue; } in.control = CW(4, SB_GPR, SB_NONE, 0) | CW_YIELD; /* nonzero, so the encoder keeps it */ if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; @@ -161,6 +163,7 @@ static void loop_end(Em *e) { #define ARG_P1 0x39cu #define ARG_P2 0x3a0u #define ARG_P3 0x3a4u +#define ARG_P4 0x3a8u #define DESC_OFF 0x358u typedef struct { int udesc, pa, pb, pc, tid, cta; } Pro; @@ -356,18 +359,47 @@ static void bra_always(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i * j += jadd; while j < 32 { acc |= 1 << j; j += p } * then acc |= (w == 0) (the number 1), word = ~(acc | (w == last ? ~tailmask : 0)). * Branch-free except the loop branches. Mutant: drops the j fix (j = p - r unreduced), so words where - * base - k0 is a multiple of p (or q overshot) mark the wrong bits. */ -static void gen_prime_sieve(Em *e, int mutant) { + * base - k0 is a multiple of p (or q overshot) mark the wrong bits. + * + * staged = 1 (OMEGA_GPU_EW_PRIME_SIEVE_SHARED, campaign C3): P4 = table words, sentinel included + * (3 per entry). The CTA first copies the table into shared memory at offset 0 (thread t copies + * words t, t + 128, ...), BAR_SYNC, then the prime loop reads shared memory (LDS) instead of + * uncached global memory. Every thread reaches the barrier: the w >= nwords exit comes after + * it. The launch must declare at least 4 * P4 bytes of shared memory. */ +static void gen_prime_sieve(Em *e, int mutant, int staged) { Pro pr = prologue(e); int nwords = V(e), stride = V(e), lastw = V(e), tailinv = V(e); int one = V(e), c32 = V(e), cm1 = V(e), w = V(e); ldc32(e, nwords, ARG_P0); ldc32(e, stride, ARG_P1); ldc32(e, lastw, ARG_P2); ldc32(e, tailinv, ARG_P3); movi(e, one, 1); movi(e, c32, 32); movi(e, cm1, 0xffffffffu); + int c4 = -1, c8 = -1, c12 = -1; + if (staged) { + int twords = V(e), i = V(e), ga = V64(e), val = V(e), sa = V(e); + c4 = V(e); c8 = V(e); c12 = V(e); + ldc32(e, twords, ARG_P4); + movi(e, c4, 4); movi(e, c8, 8); movi(e, c12, 12); + imadi(e, i, pr.tid, 1, NOREG); + loop_begin(e); /* staging loop */ + int ls = here(e); + isetp_ge_u32(e, i, twords); + int sdone = bra_fwd_if_p0(e); + addr4(e, ga, i, pr.pa); + ldg(e, val, ga, pr.udesc); + imadi(e, sa, i, 4, NOREG); + sts32(e, sa, val); + imadi(e, i, one, OMEGA_GPU_EW_THREADS, i); + bra_always(e, ls); + loop_end(e); + bra_patch_here(e, sdone); + bar_sync(e); + } imadi(e, w, pr.cta, OMEGA_GPU_EW_THREADS, pr.tid); isetp_ge_u32(e, w, nwords); exit_if_p0(e); - int base = V(e), basep32 = V(e), acc = V(e), ptr = V64(e), a64 = V64(e), a64m = V64(e); + /* ptr, a64, a64m: global addresses, or (staged) 32-bit shared offsets */ + int base = V(e), basep32 = V(e), acc = V(e); + int ptr = staged ? V(e) : V64(e), a64 = staged ? V(e) : V64(e), a64m = staged ? V(e) : V64(e); int p = V(e), magic = V(e), k0 = V(e), negp = V(e), d = V(e), neg = V(e), nd = V(e), dpos = V(e), jadd = V(e); int q = V(e), r = V(e), j = V(e), t = V(e), z = V(e), bit = V(e); int e1 = V(e), m = V(e), word = V(e), oaddr = V64(e); @@ -377,17 +409,18 @@ static void gen_prime_sieve(Em *e, int mutant) { imadi(e, base, w, 32, NOREG); iadd3(e, basep32, base, c32); movrz(e, acc); - widei(e, ptr, one, 0, pr.pa); + if (staged) movrz(e, ptr); else widei(e, ptr, one, 0, pr.pa); loop_begin(e); /* prime loop */ int lp = here(e); /* one memory round trip per prime: all three entry words load together (C2a). The * sentinel entry is a whole entry, so loading p and magic before the k0 test is in bounds. */ - widei(e, a64, one, 8, ptr); - widei(e, a64m, one, 4, ptr); + if (staged) { iadd3(e, a64, ptr, c8); iadd3(e, a64m, ptr, c4); } + else { widei(e, a64, one, 8, ptr); widei(e, a64m, one, 4, ptr); } { const int dsts[3] = { k0, p, magic }, adrs[3] = { a64, ptr, a64m }; - ldg_group(e, 3, dsts, adrs, pr.udesc); + if (staged) ld_group(e, BW_IR_LDS32, 3, dsts, adrs, NOREG); + else ld_group(e, BW_IR_LDG_E, 3, dsts, adrs, pr.udesc); } isetp_ge_u32(e, k0, basep32); int brk = bra_fwd_if_p0(e); @@ -418,7 +451,7 @@ static void gen_prime_sieve(Em *e, int mutant) { loop_end(e); bra_patch_here(e, done); - widei(e, ptr, one, 12, ptr); + if (staged) iadd3(e, ptr, ptr, c12); else widei(e, ptr, one, 12, ptr); bra_always(e, lp); loop_end(e); bra_patch_here(e, brk); @@ -473,7 +506,8 @@ static int build_kernel(OmegaGpuEwOp op, int mutant, OmegaBlackwellKernel *kerne case OMEGA_GPU_EW_RMSNORM: gen_rmsnorm(&e, mutant); break; case OMEGA_GPU_EW_ROPE: gen_rope(&e, mutant); break; case OMEGA_GPU_EW_SWIGLU: gen_swiglu(&e, mutant); break; - case OMEGA_GPU_EW_PRIME_SIEVE: gen_prime_sieve(&e, mutant); break; + case OMEGA_GPU_EW_PRIME_SIEVE: gen_prime_sieve(&e, mutant, 0); break; + case OMEGA_GPU_EW_PRIME_SIEVE_SHARED: gen_prime_sieve(&e, mutant, 1); break; default: e.err = -1; break; } int rc = OMEGA_GPU_EW_CODEGEN_FAIL; diff --git a/src/omega_gpu_elementwise_api.h b/src/omega_gpu_elementwise_api.h index cf41877ec..3d168fbd2 100644 --- a/src/omega_gpu_elementwise_api.h +++ b/src/omega_gpu_elementwise_api.h @@ -60,9 +60,10 @@ typedef enum { OMEGA_GPU_EW_RMSNORM, OMEGA_GPU_EW_ROPE, OMEGA_GPU_EW_SWIGLU, - OMEGA_GPU_EW_PRIME_SIEVE /* prime race (2026-10-05): word-parallel odd-only sieve, codegen only + OMEGA_GPU_EW_PRIME_SIEVE, /* prime race (2026-10-05): word-parallel odd-only sieve, codegen only * here; launched by bench/prime_race/gb10_native.c (argument words in * gen_prime_sieve's comment) */ + OMEGA_GPU_EW_PRIME_SIEVE_SHARED /* the same sieve with the table staged in shared memory (C3) */ } OmegaGpuEwOp; /* Shape rules (checked, refused with BAD_ARGS / TOO_LARGE): diff --git a/src/omega_gpu_session.c b/src/omega_gpu_session.c index c66d99385..d1bd1b396 100644 --- a/src/omega_gpu_session.c +++ b/src/omega_gpu_session.c @@ -148,10 +148,11 @@ int omega_gpu_session_launch(const OmegaGpuLaunch *L, uint64_t *elapsed_ns, uint .qmd0_va = qmd0_va, .qmd1_va = qmd1_va, .num_elements = L->num_elements, .threads_per_block = L->threads_x, .grid_width = L->grid_x * L->grid_y, .threads_x = L->threads_x, .threads_y = threads_y, .grid_x = L->grid_x, .grid_y = L->grid_y, .gpr_count = gpr, + .shared_bytes = L->shared_bytes, }; uint32_t qmd0_words[OMEGA_BW_QMD_WORDS], qmd1_words[OMEGA_BW_QMD_WORDS]; omega_blackwell_build_qmd0(qmd0_words, qmd0_va, qmd1_va); - omega_blackwell_build_qmd1(qmd1_words, &cfg); + if (omega_blackwell_build_qmd1(qmd1_words, &cfg) != 0) { omega_gpu_session_set_error("qmd build (shared_bytes)"); return -1; } if (omega_blackwell_verify_qmd_invariants(qmd1_words) != 0) { omega_gpu_session_set_error("qmd invariants"); return -1; } memcpy(g_s.qmd.cpu, qmd0_words, sizeof qmd0_words); memcpy((uint8_t *)g_s.qmd.cpu + 0x1000, qmd1_words, sizeof qmd1_words); diff --git a/src/omega_gpu_session.h b/src/omega_gpu_session.h index bcd5c06e4..964948237 100644 --- a/src/omega_gpu_session.h +++ b/src/omega_gpu_session.h @@ -71,6 +71,7 @@ typedef struct { const uint32_t *args; /* n_args words written at cbank + 0x380 */ uint32_t n_args; /* <= OMEGA_GPU_SESSION_MAX_ARGS */ uint64_t timeout_ms; /* marker wait; past it the process latches */ + uint32_t shared_bytes; /* shared memory per CTA; 0 = the QMD default (1024) */ } OmegaGpuLaunch; #define OMEGA_GPU_SESSION_MAX_ARGS 32u From 379239b7f2631fd171f9667fb16b5c789c8a6860 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 14:51:21 -0500 Subject: [PATCH 06/13] omega_gpu_session: optional spin window before the marker sleep-poll (campaign C4) OmegaGpuLaunch.spin_us (0..1000000) re-reads the uncached completion marker back to back, with the ARM yield hint, for up to that long before falling back to m16_native_wait_marker's 50 us sleep-poll with the full timeout, so a stuck launch still latches exactly as before. 0 (every existing caller) keeps the old wait. Why: the sleep-poll sees a finished launch only after the current 50 us sleep, which Linux timer slack stretches further; the empty-launch floor measured ~104 us. gb10_native reads PR_GB10_SPIN_US (bad values fail setup) and reports spin_us in the detail JSON; gb10_launch_cost takes the same setting for the floor diagnostic. Kernels unchanged (sha256 22a27c6c global, 418f1295 staged); host test 48 checks PASS. Co-Authored-By: Claude Opus 5.5 --- bench/prime_race/gb10_native.c | 23 ++++++++++++++++++++--- bench/prime_race/tests/gb10_launch_cost.c | 6 ++++-- src/omega_gpu_session.c | 22 +++++++++++++++++++--- src/omega_gpu_session.h | 3 +++ 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/bench/prime_race/gb10_native.c b/bench/prime_race/gb10_native.c index 5a4dd2bc0..1c2ef30ed 100644 --- a/bench/prime_race/gb10_native.c +++ b/bench/prime_race/gb10_native.c @@ -18,6 +18,10 @@ * environment lifts or lowers the launch cap; unset keeps OMEGA_GPU_EW_MAX_CTAS (the proven * I42 envelope). Any other value fails setup. The budget used is reported in the detail JSON. * + * Marker wait (campaign C4): PR_GB10_SPIN_US=N (0..1000000) polls the completion marker without + * sleeping for up to N us before the session's usual 50 us sleep-poll; unset or 0 keeps the old + * wait. Any other value fails setup. Reported in the detail JSON as spin_us. + * * Labels (handoff 2026-10-05 DESIGN): algorithm=other, faithful=no, bits=1, * environment linux-hosted-gb10-native. Build with -DPR_GB10_MUTANT=1 only for the chip * test's negative control (the r == 0 fix is dropped; the oracle must catch it). @@ -44,6 +48,16 @@ #define GB_BUDGET_MAX 4096u #define GB_ARGS 11u /* a, b, c (64-bit each) then P0..P4 */ +/* PR_GB10_SPIN_US or 0; UINT32_MAX on a malformed or out-of-range value. */ +static uint32_t gb_spin_us(void) { + const char *v = getenv("PR_GB10_SPIN_US"); + if (!v) return 0; + char *end = NULL; + unsigned long n = strtoul(v, &end, 10); + if (end == v || *end != 0 || v[0] == '-' || n > OMEGA_GPU_SESSION_MAX_SPIN_US) return UINT32_MAX; + return (uint32_t)n; +} + /* PR_GB10_CTA_BUDGET or the I42 default; 0 on a malformed or out-of-range value. */ static uint32_t gb_cta_budget(void) { const char *v = getenv("PR_GB10_CTA_BUDGET"); @@ -57,6 +71,7 @@ static uint32_t gb_cta_budget(void) { typedef struct { uint64_t limit, odd; uint32_t nwords, ctas, budget, stride, lastw, tailinv; + uint32_t spin_us; /* marker wait spin window (campaign C4) */ uint32_t root; /* isqrt(limit) */ uint32_t nprimes; /* odd primes in the table of the last pass (sentinel excluded) */ size_t table_cap; /* entries, sentinel included */ @@ -116,6 +131,8 @@ static int gb_setup(void **state, uint64_t limit) { s->nwords = (uint32_t)((s->odd + 31) / 32); s->budget = gb_cta_budget(); if (s->budget == 0) { fprintf(stderr, "gb10-native: PR_GB10_CTA_BUDGET must be 1..%u\n", GB_BUDGET_MAX); return -1; } + s->spin_us = gb_spin_us(); + if (s->spin_us == UINT32_MAX) { fprintf(stderr, "gb10-native: PR_GB10_SPIN_US must be 0..%u\n", OMEGA_GPU_SESSION_MAX_SPIN_US); return -1; } uint32_t blocks = (s->nwords + GB_THREADS - 1) / GB_THREADS; s->ctas = blocks == 0 ? 1 : (blocks > s->budget ? s->budget : blocks); s->stride = s->ctas * GB_THREADS; @@ -186,7 +203,7 @@ static int gb_pass(void *state, uint64_t limit) { OmegaGpuLaunch L = { .code_va = s->code.va, .gpr_count = GB_GPR, .threads_x = GB_THREADS, .threads_y = 1, .grid_x = s->ctas, .grid_y = 1, .num_elements = s->nwords ? s->nwords : 1, - .args = args, .n_args = GB_ARGS, .timeout_ms = GB_WAIT_MS, .shared_bytes = s->shared_bytes, + .args = args, .n_args = GB_ARGS, .timeout_ms = GB_WAIT_MS, .shared_bytes = s->shared_bytes, .spin_us = s->spin_us, }; uint64_t ns = 0; uint32_t marker = 0; if (omega_gpu_session_launch(&L, &ns, &marker) != 0) { @@ -213,8 +230,8 @@ static uint64_t gb_threads(void *state) { gb_state *s = state; return s ? s->str static int gb_detail(void *state, FILE *f) { gb_state *s = state; if (!s) return -1; - fprintf(f, "{\"grid\": %u, \"cta_budget\": %u, \"block\": %u, \"words\": %u, \"nprimes\": %u, \"table\": \"%s\", \"shared_bytes\": %u, \"kernel_code_sha256\": \"", - s->ctas, s->budget, GB_THREADS, s->nwords, s->nprimes, s->shared_bytes ? "shared" : "global", s->shared_bytes); + fprintf(f, "{\"grid\": %u, \"cta_budget\": %u, \"block\": %u, \"words\": %u, \"nprimes\": %u, \"table\": \"%s\", \"shared_bytes\": %u, \"spin_us\": %u, \"kernel_code_sha256\": \"", + s->ctas, s->budget, GB_THREADS, s->nwords, s->nprimes, s->shared_bytes ? "shared" : "global", s->shared_bytes, s->spin_us); for (int i = 0; i < 32; i++) fprintf(f, "%02x", s->k.code_digest[i]); fprintf(f, "\", \"kernel_insns\": %zu, \"kernel_gpr\": %u, \"mutant\": %d, \"launches\": %" PRIu64 ", \"diag_kernel_ns_total\": %" PRIu64 diff --git a/bench/prime_race/tests/gb10_launch_cost.c b/bench/prime_race/tests/gb10_launch_cost.c index 018900862..75a28af64 100644 --- a/bench/prime_race/tests/gb10_launch_cost.c +++ b/bench/prime_race/tests/gb10_launch_cost.c @@ -100,6 +100,8 @@ int main(int argc, char **argv) { .code_va = code.va, .gpr_count = 64, .threads_x = block, .threads_y = 1, .grid_x = grid, .grid_y = 1, .num_elements = nwords ? nwords : 1, .args = args, .n_args = OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, .timeout_ms = 600000, }; + /* PR_GB10_SPIN_US: marker wait spin window, as in gb10_native.c (campaign C4) */ + if (getenv("PR_GB10_SPIN_US")) L.spin_us = (uint32_t)strtoul(getenv("PR_GB10_SPIN_US"), NULL, 10); static uint64_t dev[MAXN], wall[MAXN]; for (int i = 0; i < n; i++) { uint64_t ns = 0; uint32_t marker = 0; /* elapsed_ns is added to, so reset per launch */ @@ -109,8 +111,8 @@ int main(int argc, char **argv) { } long bits = 0; if (sieve) { uint32_t *o = out.cpu; for (uint32_t i = 0; i < nwords; i++) bits += __builtin_popcount(o[i]); } - printf("DIAGNOSTIC %s launches=%d grid=%u block=%u insns=%zu limit=%" PRIu64 " words=%u base_primes=%u odd_primes_found=%ld\n", - argv[1], n, grid, block, k.insn_count, limit, nwords, np, bits); + printf("DIAGNOSTIC %s launches=%d grid=%u block=%u insns=%zu limit=%" PRIu64 " words=%u base_primes=%u odd_primes_found=%ld spin_us=%u\n", + argv[1], n, grid, block, k.insn_count, limit, nwords, np, bits, L.spin_us); stats("submit->marker2", dev, n); stats("host wall per launch", wall, n); omega_gpu_session_free(&tab); omega_gpu_session_free(&out); omega_gpu_session_free(&code); diff --git a/src/omega_gpu_session.c b/src/omega_gpu_session.c index d1bd1b396..29207d021 100644 --- a/src/omega_gpu_session.c +++ b/src/omega_gpu_session.c @@ -36,6 +36,21 @@ static uint64_t now_ns(void) { clock_gettime(CLOCK_MONOTONIC, &t); return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; } + +/* Marker wait. With spin_us, the host first re-reads the uncached marker word back to back (with + * the ARM yield hint) so a launch that finishes inside the window is seen within about one read, + * instead of after a 50 us sleep that Linux timer slack stretches further. Past the window it is + * the same m16 sleep-poll as before, with the full timeout, so a stuck launch still latches. */ +static int session_wait(volatile uint32_t *m, uint32_t want, uint64_t timeout_ms, uint32_t spin_us) { + if (spin_us) { + const uint64_t end = now_ns() + (uint64_t)spin_us * 1000u; + do { + if ((int32_t)(*m - want) >= 0) return 0; + __asm__ volatile("yield" ::: "memory"); + } while (now_ns() < end); + } + return m16_native_wait_marker(m, want, timeout_ms); +} static size_t page_up(size_t b) { b = (b + PAGE - 1) & ~(PAGE - 1); return b < PAGE ? PAGE : b; } const char *omega_gpu_session_last_error(void) { return g_err; } @@ -132,7 +147,8 @@ static uint32_t round_up(uint32_t v, uint32_t q) { return (v + q - 1) / q * q; } int omega_gpu_session_launch(const OmegaGpuLaunch *L, uint64_t *elapsed_ns, uint32_t *marker_out) { if (!g_open || g_blocked) { omega_gpu_session_set_error(g_blocked ? "process latched" : "launch without open device"); return -1; } - if (!L || !L->args || L->n_args == 0 || L->n_args > OMEGA_GPU_SESSION_MAX_ARGS || L->threads_x == 0 || L->grid_x == 0 || L->grid_y == 0) { + if (!L || !L->args || L->n_args == 0 || L->n_args > OMEGA_GPU_SESSION_MAX_ARGS || L->threads_x == 0 || L->grid_x == 0 || L->grid_y == 0 || + L->spin_us > OMEGA_GPU_SESSION_MAX_SPIN_US) { omega_gpu_session_set_error("launch arguments"); return -1; } const uint32_t threads_y = L->threads_y ? L->threads_y : 1; @@ -207,8 +223,8 @@ int omega_gpu_session_launch(const OmegaGpuLaunch *L, uint64_t *elapsed_ns, uint uint64_t t0 = now_ns(); if (m16_native_submit_methods(&g_s.ctx, pb, n) != 0) { fail_rm("submit_methods", &g_s.ctx.rm); return -1; } - if (m16_native_wait_marker(hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, L->timeout_ms) != 0 || - m16_native_wait_marker(hmarker2, OMEGA_BW_MARKER2_PAYLOAD, L->timeout_ms) != 0) { + if (session_wait(hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, L->timeout_ms, L->spin_us) != 0 || + session_wait(hmarker2, OMEGA_BW_MARKER2_PAYLOAD, L->timeout_ms, L->spin_us) != 0) { g_blocked = true; /* uncertain completion: keep everything, refuse every later call */ omega_gpu_session_set_error("marker wait timed out (latched)"); return -1; diff --git a/src/omega_gpu_session.h b/src/omega_gpu_session.h index 964948237..b48faf52f 100644 --- a/src/omega_gpu_session.h +++ b/src/omega_gpu_session.h @@ -72,7 +72,10 @@ typedef struct { uint32_t n_args; /* <= OMEGA_GPU_SESSION_MAX_ARGS */ uint64_t timeout_ms; /* marker wait; past it the process latches */ uint32_t shared_bytes; /* shared memory per CTA; 0 = the QMD default (1024) */ + uint32_t spin_us; /* poll the marker without sleeping for up to this long, then the usual + 50 us sleep-poll until timeout_ms; 0 = sleep-poll only (old behavior) */ } OmegaGpuLaunch; +#define OMEGA_GPU_SESSION_MAX_SPIN_US 1000000u #define OMEGA_GPU_SESSION_MAX_ARGS 32u /* Take the session lock. Never fails; does not touch the device. */ From 47cd91841eb32054db30667707e8772d0c02b3a8 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 15:11:58 -0500 Subject: [PATCH 07/13] bench/prime_race: crumbs recompiled after campaign C1-C4 Co-Authored-By: Claude Opus 5.5 --- bench/prime_race/.crumb | 8 ++++---- mk/.crumb | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/bench/prime_race/.crumb b/bench/prime_race/.crumb index 06b88e751..d1edd4a4d 100644 --- a/bench/prime_race/.crumb +++ b/bench/prime_race/.crumb @@ -3,14 +3,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-05T18:58:51Z", + "compiled_at": "2026-10-05T20:11:58Z", "compiler": "crumb-compile/1", - "digest": "sha256:e6b80bc9b9e80320eb4e25bd113c090767e5d28c2313a306f71baddf3ca856e0", + "digest": "sha256:14f1cef401ae1e5e3144cedc90eb98b5a5c2dc512a3851fa13d105814a20eeeb", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 10, - "generated_at_commit": "dc7a3d7a8c1997a0821c098fb862107d3fd54b9d", - "source_tree": "sha256:51cf8e4ff02a60a62394389bff2fe8d91cb2c936c5d2f5f418f7d91e28a10064" + "generated_at_commit": "c884b376ee4d9fa56af4078b3ed47c5b5a9af959", + "source_tree": "sha256:3cd6005d765c29438f4792bfa00e7dd8acb8479aad8b760cb2aca9d7b1315c73" } }, "invariants": [ diff --git a/mk/.crumb b/mk/.crumb index f8a8a27ac..4543a8b24 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-05T18:58:51Z", + "compiled_at": "2026-10-05T20:11:58Z", "compiler": "crumb-compile/1", - "digest": "sha256:bf8aa4246cf9b4a9d21e0a57856079a75bced3f53adefae2584b428f8afbd07a", + "digest": "sha256:243c8e51a2a001d63e898e6853e3ef522a65a92d6ad7344e74f5d54c8e575cb9", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 58, - "generated_at_commit": "dc7a3d7a8c1997a0821c098fb862107d3fd54b9d", - "source_tree": "sha256:8c46fbfd97a0d6112b4dc8e6564fb8742e4f71bf52d550b66761fb3ef4168f8e" + "generated_at_commit": "c884b376ee4d9fa56af4078b3ed47c5b5a9af959", + "source_tree": "sha256:e78d5187aea843e7d5c695959e2e8aa7bed183b7b2d2b36c50bbb12b4c31b987" }, "owner_lane": "", "provenance": { From e10490a05f16bde28a3f239a04128627276b16ef Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 15:24:18 -0500 Subject: [PATCH 08/13] omega_gpu_elementwise: NAK-derived fixed-latency scheduling + fixed-RAW hazard checker (campaign C2b) Patch by GPT-6 Astra (Codex), host-validated by the orchestrator. General: applies to every elementwise family. - Predicate guard delay 13 -> 14 (Mesa NAK sm120 latency table, pred_raw Dualalu 13 + 1 padding; 13 worked on the chip so far, 14 is the reference-derived safe value). - schedule_fixed_pairs: adjacent ALU/ALU or FMA/FMA pairs 6 -> 5 cycles (NAK reg_raw bound incl. padding); mixed classes, IMAD.WIDE producers and IADD3 (fixed encoding) unchanged. - Host test: fixed-RAW checker over encoded control words, allocated registers, branches and predicate guards, for all seven families and both mutants. Negative controls: ALU delay 4 and predicate delay 13 are each caught. 96 checks PASS. Static model (Astra): ~2 us per launch at most; a flat chip result supports "fixed stalls are not the bottleneck". Co-Authored-By: Claude Opus 5.5 --- bench/prime_race/tests/gb10_sieve_host_test.c | 144 ++++++++++++++++++ src/omega_gpu_elementwise_api.c | 46 +++++- 2 files changed, 187 insertions(+), 3 deletions(-) diff --git a/bench/prime_race/tests/gb10_sieve_host_test.c b/bench/prime_race/tests/gb10_sieve_host_test.c index 1be5d3b39..cf82a6007 100644 --- a/bench/prime_race/tests/gb10_sieve_host_test.c +++ b/bench/prime_race/tests/gb10_sieve_host_test.c @@ -235,6 +235,132 @@ static int sb_hazards(const BlackwellIRProgram *p, int verbose) { return bad; } +/* Fixed RAW checks use ENCODED delays, allocated registers and both successors + * of conditional branches, including back-edges. No credit is taken for extra + * scoreboard/memory delays. This covers the scalar ALU/FMA and predicate classes + * changed by the emitter; wide producers and variable latency remain the job + * of the existing schedule and scoreboard checker. + * Independent reference: NAK sm100/reg_raw.csv (reader rows, writer columns) + * plus sm120_instr_latencies.rs's +1. ALU/dual-ALU -> ALU/dual-ALU and FMA -> + * FMA are 5, mixed and address consumers at most 6 for these producers. + * ISETP -> guard is 14 via sm70.rs paw_latency, NOT the 5-cycle ISETP operand + * predicate latency. WAW/WAR for these scalar classes are <= 2, below our floor. + */ +static int latency_class(BlackwellIROpcode op) { + switch (op) { + case BW_IR_ISETP_GE_U32: case BW_IR_ISETP_GE: case BW_IR_FSETP: return 3; + case BW_IR_IMAD: case BW_IR_IMAD_HI_U32: + case BW_IR_FADD: case BW_IR_FSUB: case BW_IR_FMUL: case BW_IR_FFMA: return 2; + case BW_IR_MOV_IMM: case BW_IR_MOV_RZ: case BW_IR_IADD3: + case BW_IR_LOP3_LUT: case BW_IR_LOP3_XOR: + case BW_IR_SHF_R: case BW_IR_SHF_L_U32: return 1; + default: return 0; + } +} + +static uint32_t encoded_control(const OmegaBlackwellKernel *k, size_t i) { + uint32_t c; + memcpy(&c, k->code + i * 16 + 12, sizeof c); + return c; +} + +static int reg_contains(const BlackwellIRProgram *p, int v, unsigned sub, int phys) { + if (v < 0) return 0; + int first = p->regalloc.vreg_to_phys[v] + (int)sub; + /* Conservatively count the rest of a bundle as read/written. The changed + * producers are scalar; this includes both halves of address consumers. */ + return phys >= first && phys < p->regalloc.vreg_to_phys[v] + + (int)p->regalloc.intervals[v].bundle_size; +} + +static int relax_successors(const BlackwellIRProgram *p, size_t i, unsigned a, + unsigned *age) { + const BlackwellIRInsn *in = &p->insns[i]; + int next[2], n = 0, changed = 0; + if (in->op == BW_IR_BRA) next[n++] = (int)i + (int32_t)in->imm; + if ((in->op != BW_IR_BRA && in->op != BW_IR_EXIT) || in->predicate_p0) + next[n++] = (int)i + 1; + for (int s = 0; s < n; s++) { + if (next[s] < 0 || (size_t)next[s] >= p->count) return -1; + if (a < age[next[s]]) { age[next[s]] = a; changed = 1; } + } + return changed; +} + +static int fixed_hazards(const BlackwellIRProgram *p, const OmegaBlackwellKernel *k) { + /* The encoder pads short programs to 32 instructions. */ + if (p->count > BW_MAX_IR_INSNS || k->code_size < p->count * 16) return 1; + int bad = 0; + for (size_t w = 0; w < p->count; w++) { + const BlackwellIRInsn *writer = &p->insns[w]; + int cls = latency_class(writer->op), pred = cls == 3; + if (!cls || (!pred && (writer->dst_vreg < 0 || writer->is_uniform))) continue; + int phys = pred ? -1 : p->regalloc.vreg_to_phys[writer->dst_vreg] + writer->dst_subreg; + unsigned age[BW_MAX_IR_INSNS], bound = pred ? 14 : 6; + for (size_t i = 0; i < p->count; i++) age[i] = 255; + unsigned delay = (encoded_control(k, w) >> 9) & 15u; + if (delay == 0) { bad++; continue; } /* zero-delay/co-issue is outside this model */ + if (relax_successors(p, w, delay, age) < 0) return 1; + int changed; + do { + changed = 0; + for (size_t i = 0; i < p->count; i++) { + if (age[i] >= bound) continue; + const BlackwellIRInsn *in = &p->insns[i]; + int kills = pred ? latency_class(in->op) == 3 : + (!in->is_uniform && reg_contains(p, in->dst_vreg, in->dst_subreg, phys)); + if (kills && !in->predicate_p0) continue; + unsigned d = (encoded_control(k, i) >> 9) & 15u; + int r = relax_successors(p, i, age[i] + d, age); + if (r < 0) return 1; + changed |= r; + } + } while (changed); + for (size_t i = 0; i < p->count; i++) { + if (age[i] >= bound) continue; + const BlackwellIRInsn *in = &p->insns[i]; + int reads = pred ? (in->predicate_p0 || in->op == BW_IR_FSEL) : + (reg_contains(p, in->src1_vreg, in->src1_subreg, phys) || + reg_contains(p, in->src2_vreg, in->src2_subreg, phys) || + reg_contains(p, in->src3_vreg, in->src3_subreg, phys)); + int reader_cls = latency_class(in->op); + unsigned need = pred ? 14 : + ((cls == 2 && reader_cls == 2) || + (cls == 1 && (reader_cls == 1 || reader_cls == 3)) ? 5 : 6); + if (reads && age[i] < need) bad++; + } + } + return bad; +} + +static void fixed_schedule_check(const BlackwellIRProgram *p, const OmegaBlackwellKernel *k) { + int hz = fixed_hazards(p, k); + CHECK(hz == 0, "encoded fixed RAW hazards: %d", hz); + OmegaBlackwellKernel bad = *k; + bad.code = malloc(k->code_size); + CHECK(bad.code != NULL, "fixed schedule negative-control allocation"); + if (!bad.code) return; + int shortened_alu = 0, shortened_pred = 0, alu_hz = 0, pred_hz = 0; + for (size_t i = 0; i < p->count; i++) { + uint32_t c = encoded_control(k, i); + int cls = latency_class(p->insns[i].op); + if ((!shortened_alu && cls > 0 && cls < 3 && ((c >> 9) & 15u) == 5) || + (!shortened_pred && cls == 3)) { + memcpy(bad.code, k->code, k->code_size); + uint32_t short_c = (c & ~(15u << 9)) | ((cls == 3 ? 13u : 4u) << 9); + memcpy(bad.code + i * 16 + 12, &short_c, sizeof short_c); + int h = fixed_hazards(p, &bad); + if (h > hz) { + if (cls == 3) { shortened_pred = 1; pred_hz = h; } + else { shortened_alu = 1; alu_hz = h; } + } + } + } + CHECK(shortened_alu && shortened_pred, "fixed RAW checker missed a short ALU/predicate delay"); + printf("fixed RAW: %d hazards; negative controls ALU=4: %d, predicate=13: %d\n", hz, alu_hz, pred_hz); + free(bad.code); +} + /* ------------------------------------------------------------- nvdisasm */ /* every word decodes, the new ops appear, and every BRA lands where the IR says */ static const char *const MUST_GLOBAL[] = { "LOP3.LUT", "SHF.L.U32", "IMAD.HI.U32", "@P0 BRA", "@!P0 BRA", "@P0 EXIT", "STG.E", "LDG.E" }; @@ -335,6 +461,22 @@ int main(int argc, char **argv) { int fx = omega_blackwell_verify_codegen_fixtures_intops(); CHECK(fx == 0, "golden words LOP3_LUT / SHF_L_U32 / IMAD_HI_U32 (rc %d)", fx); + /* The scheduling pass is shared by every elementwise family. This is + * codegen only: no public launcher or device session is called. */ + for (int op = OMEGA_GPU_EW_EX2; op <= OMEGA_GPU_EW_PRIME_SIEVE_SHARED; op++) { + for (int mutant = 0; mutant <= 1; mutant++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); + OmegaBlackwellKernel code = {0}; + int rc = p ? omega_gpu_elementwise_codegen_ir((OmegaGpuEwOp)op, mutant, p, &code) : -1; + CHECK(rc == OMEGA_GPU_EW_OK, "schedule codegen op %d mutant %d rc %d", op, mutant, rc); + if (rc == OMEGA_GPU_EW_OK) { + CHECK(sb_hazards(p, 0) == 0, "schedule scoreboard op %d mutant %d", op, mutant); + CHECK(fixed_hazards(p, &code) == 0, "schedule fixed RAW op %d mutant %d", op, mutant); + } + free(code.code); free(p); + } + } + /* 2. oracle self-check against pi(x) */ static const struct { uint64_t x, pi; } pis[] = { {10, 4}, {100, 25}, {1000, 168}, {10000, 1229}, {100000, 9592}, {1000000, 78498} }; for (size_t i = 0; i < sizeof pis / sizeof pis[0]; i++) { @@ -357,6 +499,7 @@ int main(int argc, char **argv) { CHECK(k.gpr_count <= 64, "GPR budget %u > 64", k.gpr_count); CHECK(memcmp(k.code_digest, mk.code_digest, 32) != 0, "mutant kernel differs from production"); nvdisasm_check(&k, prog, MUST_GLOBAL, "global table"); + fixed_schedule_check(prog, &k); { int hz = sb_hazards(prog, 1); CHECK(hz == 0, "scoreboard hazards in the sieve kernel: %d", hz); @@ -445,6 +588,7 @@ int main(int argc, char **argv) { printf("\n"); CHECK(sk.gpr_count <= 64, "staged GPR budget %u > 64", sk.gpr_count); nvdisasm_check(&sk, sp, MUST_STAGED, "staged"); + fixed_schedule_check(sp, &sk); int hz = sb_hazards(sp, 1); CHECK(hz == 0, "scoreboard hazards in the staged kernel: %d", hz); int sfail = 0, sn = 0; diff --git a/src/omega_gpu_elementwise_api.c b/src/omega_gpu_elementwise_api.c index 34dd61fa1..ad807c1bc 100644 --- a/src/omega_gpu_elementwise_api.c +++ b/src/omega_gpu_elementwise_api.c @@ -11,11 +11,11 @@ * a scoreboard; the producer stalls >= 2 and the consumer waits on that SB; * - a fixed-latency result (FADD FMUL FFMA IMAD IADD3 MOV LOP3 SHF) has no * scoreboard: the producer stalls >= 5 before its consumer (we use 6); - * - a predicate producer (ISETP) stalls 13 before @P0 use (the codegen default); + * - a predicate producer (ISETP) stalls 14 before @P0 use (NAK SM120 padding); * - stores (STG, STS) set a read barrier so the next instruction cannot overwrite * the source register early. * The emitter below serialises: every instruction waits on everything still - * pending. Slow and safe; speed is a later cut. + * pending. A final local pass tightens supported adjacent ALU pairs to 5 cycles. */ #include "omega_gpu_elementwise_api.h" #include "omega_blackwell_codegen.h" @@ -55,7 +55,7 @@ static void emit(Em *e, BlackwellIRInsn in, int kind) { uint32_t wait = e->pending; switch (kind) { case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; - case K_PRED: in.control = CW(13, SB_NONE, SB_NONE, wait); e->pending = 0; break; + case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; case K_VAR: in.control = CW(4, SB_GPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_GPR; break; case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; @@ -495,6 +495,45 @@ static int check_loop_invariant(const Em *e) { #define EW_GPR_BUDGET 64u /* the QMD declares 64 registers, as the matmul launcher does */ +/* Mesa NAK sm120_instr_latencies.rs + sm100/reg_raw.csv: ALU/dual-ALU pairs + * and FMA/FMA pairs need 4 + 1 cycles; mixed pairs need 5 + 1. CSV rows are + * READERS, columns WRITERS (see lat_rs_gen.py), not the reverse. IMAD.WIDE + * has operand-specific latencies and is deliberately excluded here. + * + * Retain at least 5 cycles after every fixed producer. Thus the next reader + * uses the pair-specific bound and later readers are >= 9 cycles away (the + * next instruction in a tightened pair is itself fixed). No instruction, + * branch target, wait, barrier assignment or register allocation is changed. + * IADD3's encoder still supplies its historical control word; do not pretend + * to schedule that producer here. Its existing 5-cycle delay is unchanged. + * + * Predicate-to-guard uses NAK paw_latency -> raw(write, None): reader + * RedirectedFp64, writer Dualalu = 13, plus SM120's 1 = 14, in emit above. + * NVIDIA's public ISA docs are silent about SASS control-word semantics. + */ +static int fixed_pair_class(BlackwellIROpcode op) { + switch (op) { + case BW_IR_MOV_RZ: case BW_IR_MOV_IMM: case BW_IR_IADD3: + case BW_IR_LOP3_XOR: case BW_IR_LOP3_LUT: + case BW_IR_SHF_R: case BW_IR_SHF_L_U32: return 1; + case BW_IR_IMAD: case BW_IR_IMAD_HI_U32: + case BW_IR_FADD: case BW_IR_FSUB: case BW_IR_FMUL: case BW_IR_FFMA: return 2; + default: return 0; + } +} + +static void schedule_fixed_pairs(BlackwellIRProgram *p) { + for (size_t i = 0; i + 1 < p->count; i++) { + BlackwellIRInsn *a = &p->insns[i]; + const BlackwellIRInsn *b = &p->insns[i + 1]; + int cls = fixed_pair_class(a->op); + if (!cls || cls != fixed_pair_class(b->op) || a->op == BW_IR_IADD3 || + a->predicate_p0 || b->predicate_p0 || ((a->control >> 9) & 15u) != 6) + continue; + a->control = (a->control & ~(15u << 9)) | (5u << 9); + } +} + static int build_kernel(OmegaGpuEwOp op, int mutant, OmegaBlackwellKernel *kernel, BlackwellIRProgram *prog_out) { BlackwellIRProgram *prog = calloc(1, sizeof *prog); if (!prog) return OMEGA_GPU_EW_CODEGEN_FAIL; @@ -510,6 +549,7 @@ static int build_kernel(OmegaGpuEwOp op, int mutant, OmegaBlackwellKernel *kerne case OMEGA_GPU_EW_PRIME_SIEVE_SHARED: gen_prime_sieve(&e, mutant, 1); break; default: e.err = -1; break; } + schedule_fixed_pairs(prog); int rc = OMEGA_GPU_EW_CODEGEN_FAIL; int ra_rc = (e.err == 0) ? omega_bw_regalloc_solve(prog) : -1; int loop_rc = (ra_rc == 0) ? check_loop_invariant(&e) : -1; From 7f367009aea4d2bee164128bc6d48b8bfe156d6f Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 15:25:32 -0500 Subject: [PATCH 09/13] omega_gpu_elementwise: sieve mark loop with a warp-uniform trip count (campaign C5) The mark loop ran "while j < 32", a lane-dependent branch with no reconvergence point, so the 32 lanes of a warp could split there and stay split. GPT-6 Astra's cycle model put the fixed-stall critical path at ~30-60 us per launch against 821 us measured, and a fully serialized-lanes extreme near the measured times. Now every active lane runs ceil(32/p) steps (the counter depends only on p, the same for all active lanes) and bits past the word are masked out. Sieve-specific; the lesson (reconvergence) is general. Host test 96 checks PASS (oracle sweeps, CTA budgets, staged + global, fixed-RAW and scoreboard hazards 0, mutant caught); test-gpu-elementwise 24 PASS. Co-Authored-By: Claude Opus 5.5 --- src/omega_gpu_elementwise_api.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/omega_gpu_elementwise_api.c b/src/omega_gpu_elementwise_api.c index ad807c1bc..83e746f0c 100644 --- a/src/omega_gpu_elementwise_api.c +++ b/src/omega_gpu_elementwise_api.c @@ -403,6 +403,8 @@ static void gen_prime_sieve(Em *e, int mutant, int staged) { int p = V(e), magic = V(e), k0 = V(e), negp = V(e), d = V(e), neg = V(e), nd = V(e), dpos = V(e), jadd = V(e); int q = V(e), r = V(e), j = V(e), t = V(e), z = V(e), bit = V(e); int e1 = V(e), m = V(e), word = V(e), oaddr = V64(e); + int tt = V(e), jj = V(e), sh = V(e), c31 = V(e), cm32 = V(e); + movi(e, c31, 31); movi(e, cm32, 0xffffffe0u); loop_begin(e); /* word loop */ int lw = here(e); @@ -440,13 +442,23 @@ static void gen_prime_sieve(Em *e, int mutant, int staged) { } iadd3(e, j, j, jadd); - loop_begin(e); /* mark loop */ + /* mark loop (campaign C5): the trip count depends only on p, so every active lane runs the + * same ceil(32/p) steps and the warp never splits here (all active lanes are on the same + * prime). Bits past this word are masked out instead of branched around. The old loop ran + * "while j < 32", a lane-dependent branch with no reconvergence point. */ + movrz(e, tt); + loop_begin(e); int li = here(e); - isetp_ge_u32(e, j, c32); + isetp_ge_u32(e, tt, c32); int done = bra_fwd_if_p0(e); - shl_r(e, bit, one, j); - lop3lut(e, acc, acc, bit, NOREG, 0xfc); /* acc | bit */ - iadd3(e, j, j, p); + iadd3(e, jj, j, tt); + lop3lut(e, sh, jj, c31, NOREG, 0xc0); /* jj & 31 */ + shl_r(e, bit, one, sh); + iadd3(e, t, jj, cm32); + shr_i(e, z, t, 31); /* jj < 32 (j <= p + 31, far below 2^31) */ + imadi(e, m, z, 0xffffffffu, NOREG); /* all ones when jj < 32, else 0 */ + lop3lut(e, acc, acc, bit, m, 0xf8); /* acc | (bit & m) */ + iadd3(e, tt, tt, p); bra_always(e, li); loop_end(e); bra_patch_here(e, done); From 10db0382b8ce622edeb1bbf67e6920eb2591291e Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 15:25:33 -0500 Subject: [PATCH 10/13] bench/prime_race: crumbs recompiled after C2b + C5 Co-Authored-By: Claude Opus 5.5 --- bench/prime_race/.crumb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bench/prime_race/.crumb b/bench/prime_race/.crumb index d1edd4a4d..2573d2ec0 100644 --- a/bench/prime_race/.crumb +++ b/bench/prime_race/.crumb @@ -3,14 +3,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-05T20:11:58Z", + "compiled_at": "2026-10-05T20:25:32Z", "compiler": "crumb-compile/1", - "digest": "sha256:14f1cef401ae1e5e3144cedc90eb98b5a5c2dc512a3851fa13d105814a20eeeb", + "digest": "sha256:ce4d9054f2ba0aec6cbf8fb6653db817c52a514e43bfd77a6849f4b96d6a5ce6", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 10, - "generated_at_commit": "c884b376ee4d9fa56af4078b3ed47c5b5a9af959", - "source_tree": "sha256:3cd6005d765c29438f4792bfa00e7dd8acb8479aad8b760cb2aca9d7b1315c73" + "generated_at_commit": "77d5f2e5b732ea5ee86249c7a7281ddaa97769c0", + "source_tree": "sha256:e09bd5921627d2000041c62478684f291facd0c9b6ed87635058887541da80f2" } }, "invariants": [ From 357eed8f6ab851b12121508fb79bef9c85ffe006 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 16:43:25 -0500 Subject: [PATCH 11/13] bench/prime_race: PR_GB10_TABLE=auto|global switch for the staged-table A/B (campaign C6) Default unchanged (auto: shared-memory table when it fits). global never stages the table. Any other value fails setup before the chip is opened. Co-Authored-By: Claude Opus 5.5 --- bench/prime_race/gb10_native.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/bench/prime_race/gb10_native.c b/bench/prime_race/gb10_native.c index 1c2ef30ed..b735e046d 100644 --- a/bench/prime_race/gb10_native.c +++ b/bench/prime_race/gb10_native.c @@ -68,10 +68,20 @@ static uint32_t gb_cta_budget(void) { return (uint32_t)n; } +/* PR_GB10_TABLE: "auto" or unset (shared-memory table when it fits), "global" (never staged); + * -1 on any other value. The A/B switch for the staged table (campaign C6). */ +static int gb_table_global(void) { + const char *v = getenv("PR_GB10_TABLE"); + if (!v || strcmp(v, "auto") == 0) return 0; + if (strcmp(v, "global") == 0) return 1; + return -1; +} + typedef struct { uint64_t limit, odd; uint32_t nwords, ctas, budget, stride, lastw, tailinv; uint32_t spin_us; /* marker wait spin window (campaign C4) */ + int table_global; /* PR_GB10_TABLE=global: never stage the table (campaign C6) */ uint32_t root; /* isqrt(limit) */ uint32_t nprimes; /* odd primes in the table of the last pass (sentinel excluded) */ size_t table_cap; /* entries, sentinel included */ @@ -133,6 +143,8 @@ static int gb_setup(void **state, uint64_t limit) { if (s->budget == 0) { fprintf(stderr, "gb10-native: PR_GB10_CTA_BUDGET must be 1..%u\n", GB_BUDGET_MAX); return -1; } s->spin_us = gb_spin_us(); if (s->spin_us == UINT32_MAX) { fprintf(stderr, "gb10-native: PR_GB10_SPIN_US must be 0..%u\n", OMEGA_GPU_SESSION_MAX_SPIN_US); return -1; } + s->table_global = gb_table_global(); + if (s->table_global < 0) { fprintf(stderr, "gb10-native: PR_GB10_TABLE must be auto or global\n"); return -1; } uint32_t blocks = (s->nwords + GB_THREADS - 1) / GB_THREADS; s->ctas = blocks == 0 ? 1 : (blocks > s->budget ? s->budget : blocks); s->stride = s->ctas * GB_THREADS; @@ -147,7 +159,7 @@ static int gb_setup(void **state, uint64_t limit) { s->table_cap = (size_t)s->root / 2 + 2; /* every odd number <= root, plus the sentinel */ s->entries = gb_count_odd_primes(s->root) + 1; /* campaign C3: stage the table in shared memory whenever it fits the QMD bound */ - if ((uint64_t)s->entries * 12 <= OMEGA_BW_QMD_MAX_SHARED_BYTES) s->shared_bytes = s->entries * 12; + if (!s->table_global && (uint64_t)s->entries * 12 <= OMEGA_BW_QMD_MAX_SHARED_BYTES) s->shared_bytes = s->entries * 12; OmegaGpuEwOp op = s->shared_bytes ? OMEGA_GPU_EW_PRIME_SIEVE_SHARED : OMEGA_GPU_EW_PRIME_SIEVE; if (omega_gpu_elementwise_codegen_ir(op, PR_GB10_MUTANT, NULL, &s->k) != OMEGA_GPU_EW_OK) { From df9f40a0435a70d680f6b49297815e53e3497ea9 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 16:43:28 -0500 Subject: [PATCH 12/13] bench/prime_race: crumbs recompiled after the table switch Co-Authored-By: Claude Opus 5.5 --- bench/prime_race/.crumb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bench/prime_race/.crumb b/bench/prime_race/.crumb index 2573d2ec0..6673818e8 100644 --- a/bench/prime_race/.crumb +++ b/bench/prime_race/.crumb @@ -3,14 +3,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-05T20:25:32Z", + "compiled_at": "2026-10-05T21:43:28Z", "compiler": "crumb-compile/1", - "digest": "sha256:ce4d9054f2ba0aec6cbf8fb6653db817c52a514e43bfd77a6849f4b96d6a5ce6", + "digest": "sha256:e48fd63d20aa45947aecd81b6481d5edfce2fdd5f88f5e1f70ae18026a19e679", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 10, - "generated_at_commit": "77d5f2e5b732ea5ee86249c7a7281ddaa97769c0", - "source_tree": "sha256:e09bd5921627d2000041c62478684f291facd0c9b6ed87635058887541da80f2" + "generated_at_commit": "7b961a5590f1087971321490a2655757d72cd31e", + "source_tree": "sha256:819b99c3125bfc48608d2177983d34efa8c81aab38b7a3cc57c218fbdfe37c5a" } }, "invariants": [ From 4d243a42d6f1a0b54912ea70e6c3430910f0f69e Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Mon, 5 Oct 2026 20:43:55 -0500 Subject: [PATCH 13/13] crumbs: compile after rebase on main Co-Authored-By: Claude Opus 5.5 --- .crumb | 18 +++++++++--------- src/.crumb | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.crumb b/.crumb index a0d37e6d1..e245c5a26 100644 --- a/.crumb +++ b/.crumb @@ -63,8 +63,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "bf8aa4246cf9b4a9d21e0a57856079a75bced3f53adefae2584b428f8afbd07a", - "last_commit": "48776cf", + "digest": "243c8e51a2a001d63e898e6853e3ef522a65a92d6ad7344e74f5d54c8e575cb9", + "last_commit": "8190bad", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -87,8 +87,8 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "c4d0989f06c0bcfd0bd58edabdf147c0894d0882fa12ed69c55c0440302f46dd", - "last_commit": "0501183", + "digest": "6d49877f41b071c4f28e00bb47293fd6c483791242e7a7fc54460a5e0f6ae3ca", + "last_commit": "7f36700", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, @@ -105,15 +105,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:c17c99ce3ffcc7e46c9b1a73693858ddfbc3f9ee7698a1a0094a93e6d55464b4", - "compiled_at": "2026-10-06T01:31:06Z", + "children_root": "sha256:2c5c4c1dd77a1bf73e662f3cb0c5d707b294bbdc63f5821947f4bc62d8ebaa17", + "compiled_at": "2026-10-06T01:43:54Z", "compiler": "crumb-compile/1", - "digest": "sha256:4fb72698e3eaab51a105949921e75146a9ca8461a497c2c8af38c256942899f5", + "digest": "sha256:77c5261e0f8d0b6a3cfee57862bced9d40b8c2002774fcbbc332821556e6f121", "evidence_files": 3304, "evidence_root": "sha256:9594e3e2a05603747e60ab08376dcf98a6857a89e43720f33126d5b68e41f4d1", "files": 5127, - "generated_at_commit": "39f93e5e6dbb405fcbaa276f4fc41fd9700d683d", - "source_tree": "sha256:e29691d0c0196d99a8951ffccf0e69a27caf4d1c9bb158ce90ef35725d4847c0" + "generated_at_commit": "df9f40a0435a70d680f6b49297815e53e3497ea9", + "source_tree": "sha256:7a3871434c18631c02fc551dce23a09b61df1fdfc8852b4fff3b3154a9f80c38" }, "owner_lane": "", "provenance": { diff --git a/src/.crumb b/src/.crumb index a0b0d90b0..ab6aec041 100644 --- a/src/.crumb +++ b/src/.crumb @@ -170,14 +170,14 @@ } ], "children_root": "sha256:920218d42490379078986bd65f6d50e8ef9064df20da67bfc0ca50ea0de4b4c0", - "compiled_at": "2026-10-06T01:31:06Z", + "compiled_at": "2026-10-06T01:43:54Z", "compiler": "crumb-compile/1", - "digest": "sha256:c4d0989f06c0bcfd0bd58edabdf147c0894d0882fa12ed69c55c0440302f46dd", + "digest": "sha256:6d49877f41b071c4f28e00bb47293fd6c483791242e7a7fc54460a5e0f6ae3ca", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 408, - "generated_at_commit": "39f93e5e6dbb405fcbaa276f4fc41fd9700d683d", - "source_tree": "sha256:dcee4fbb0262400359e7fa3054b55b6d26b98108bf59e0b8353713c1a1be9813" + "generated_at_commit": "df9f40a0435a70d680f6b49297815e53e3497ea9", + "source_tree": "sha256:f65cd26a7304076fc024ceb08e6fdc1f909b2b730bd432b85183879bd747eaae" }, "owner_lane": "", "provenance": {