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 / tests / bw_warp_sim_k.h

373 lines · 26169 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/* bw_warp_sim.h: host SIMT simulator for the Blackwell IR (omega #308).
18 *
19 * Models what the earlier per-thread simulators could not: a warp of 32 lanes with an
20 * active mask, divergence at predicated branches, structured reconvergence through the
21 * barrier registers (BSSY arms Bn for the lanes executing it and names the join; BSYNC
22 * parks arriving lanes until every armed lane that has not exited is there, then the
23 * warp continues as one), nested regions, and lane participation in SHFL and BAR.SYNC.
24 *
25 * A warp is a set of fragments (pc, lane mask). A predicated branch whose lanes disagree
26 * splits the fragment; the scheduler then runs one fragment at a time in a chosen order
27 * (lowest pc first or highest pc first; tests run both), which is the freedom the chip's
28 * scheduler also has. Fragments only merge at a BSYNC. Errors (all fatal, first one wins):
29 * WS_E_SHFL_SPLIT a SHFL reached by a fragment that is not the whole live warp
30 * (the chip reads inactive lanes: undefined; attention 2026-10-04)
31 * WS_E_SHFL_EXITED a SHFL on a warp some of whose lanes already exited (undefined too)
32 * WS_E_BAR_SPLIT (defined, never raised) a BAR.SYNC reached by a split warp is allowed:
33 * since Volta the CTA barrier counts arriving threads, not warps (CUDA
34 * programming guide, independent thread scheduling; INFERRED for sm_121,
35 * not chip-verified by us). The simulator parks each fragment, releases
36 * them together, counts the event in bar_split_arrivals, and leaves the
37 * fragments split: a barrier is not a reconvergence point, so a SHFL after
38 * it still fails with SHFL_SPLIT unless a BSYNC rejoined the warp.
39 * WS_E_BSYNC_UNARMED BSYNC on a barrier no BSSY armed
40 * WS_E_BSSY_REARM BSSY on a barrier still armed (lanes may still be heading to its join)
41 * WS_E_JOIN_MISMATCH BSYNC at an address other than the one the BSSY named minus one
42 * WS_E_NONMEMBER a lane reached BSYNC Bn without having executed its BSSY
43 * WS_E_STUCK lanes parked at a BSYNC that can never complete (a join was dropped,
44 * an exit jumped past it, or a target was corrupted)
45 * WS_E_NEVER_JOINED the warp finished with a barrier still armed
46 * WS_E_SELF_BRANCH, WS_E_PC, WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS
47 * WS_E_IMADHI_RC IMAD.HI.U32 with an odd register Rc. An even Rc is modelled as the 64-bit pair
48 * Rc+1:Rc the chip reads (omega #492, nvcc oracle, chip probe pending); odd has no evidence
49 * Every error names the pc. Memory: global addresses are host pointers (optionally range
50 * checked); shared memory is a per-CTA buffer poisoned at CTA start so a read of a word
51 * nobody wrote shows up in the oracle comparison.
52 *
53 * Per-lane ALU semantics are those of the two earlier simulators (tests/gpu_attention_test.c,
54 * bench/prime_race/tests/gb10_sieve_host_test.c). MUFU EX2 / RCP / RSQ are the correctly
55 * rounded IEEE value with the chip's subnormal flush (bw_mufu_ftz.h); the chip's are approximate. LDCU64 (memory descriptor) is not modelled. */
56#ifndef BW_WARP_SIM_K_H
57#define BW_WARP_SIM_K_H
58/* bw_warp_sim_k.h (OWN-30B cut 8, 2026-10-13): tests/bw_warp_sim.h plus the system-scope opcodes the decode attention
59 * kernel uses (ATOMG.ADD, LDG/STG.STRONG.SYS, MEMBAR): a derived copy so the 8B lane's file stays untouched.
60 * The simulator runs CTAs one after another, so an atomic counter sees every earlier CTA's add. */
61
62#include "omega_blackwell_codegen.h"
63#include "bw_mufu_ftz.h" /* MUFU EX2 / RCP / RSQ: the chip's flush of subnormal inputs and results (omega #462) */
64#include "omega_blackwell_codegen_tc.h"
65#include <math.h>
66#include <stdint.h>
67#include <stdio.h>
68#include <stdlib.h>
69#include <string.h>
70
71#define WS_WARP 32
72#ifndef WS_MAX_THREADS
73#define WS_MAX_THREADS 256
74#endif
75#define WS_MAX_WARPS (WS_MAX_THREADS / WS_WARP)
76#define WS_MAX_FRAGS 64
77#define WS_NBAR BW_RECONV_MAX_BAR
78#define WS_MAX_RANGES 8
79
80enum {
81 WS_OK = 0, WS_E_SHFL_SPLIT, WS_E_SHFL_EXITED, WS_E_BAR_SPLIT, WS_E_BSYNC_UNARMED, WS_E_BSSY_REARM,
82 WS_E_JOIN_MISMATCH, WS_E_NONMEMBER, WS_E_STUCK, WS_E_NEVER_JOINED, WS_E_SELF_BRANCH, WS_E_PC,
83 WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS, WS_E_IMADHI_RC
84};
85static const char *ws_err_name(int e) {
86 static const char *const n[] = { "OK", "SHFL_SPLIT", "SHFL_EXITED", "BAR_SPLIT", "BSYNC_UNARMED", "BSSY_REARM",
87 "JOIN_MISMATCH", "NONMEMBER", "STUCK", "NEVER_JOINED", "SELF_BRANCH", "PC", "OP", "SHIFT", "MEM", "SMEM",
88 "FRAGS", "BAR_TIMEOUT", "STEPS", "IMADHI_RC" };
89 return (e >= 0 && e < (int)(sizeof n / sizeof n[0])) ? n[e] : "?";
90}
91
92typedef struct { uint32_t r[256]; uint8_t p0; } WsLane;
93typedef struct { int pc; uint32_t mask; int parked; /* waiting at a BAR.SYNC */ } WsFrag;
94typedef struct { int valid; int target; uint32_t members, arrived; } WsBar;
95typedef struct {
96 WsLane lane[WS_WARP];
97 WsFrag frag[WS_MAX_FRAGS];
98 int nfrag;
99 WsBar bar[WS_NBAR];
100 uint32_t exited;
101} WsWarp;
102typedef struct { uintptr_t lo, hi; } WsRange;
103
104typedef struct {
105 const BlackwellIRProgram *p;
106 uint32_t cbank[256]; /* constant bank 0 words (byte offset / 4) */
107 uint32_t threads; /* per CTA, a multiple of 32, <= WS_MAX_THREADS */
108 uint8_t *smem; /* per-CTA shared memory buffer (poisoned at CTA start) */
109 uint32_t smem_bytes;
110 WsRange ranges[WS_MAX_RANGES]; int nranges; /* when > 0, every global access must fall in one */
111 int order; /* 0 = lowest pc first, 1 = highest pc first */
112 uint32_t warp_sched; /* warp visit order per barrier phase: 0 ascending, 1 descending, else a seed for a fresh shuffle each phase */
113 int trace; /* print every issue of warp 0 of CTA (0,0) */
114 uint64_t step_budget; /* scheduling steps per warp before "step budget" (0 = 2e9); tests of a dropped loop guard set it low */
115 /* results */
116 int err; int err_pc; char msg[200];
117 uint64_t warp_issues; /* fragment-instruction issues (the chip's cost unit for divergence) */
118 uint64_t lane_issues; /* lanes active over those issues */
119 uint64_t splits; /* branches that split a fragment */
120 uint64_t bar_split_arrivals; /* BAR.SYNC reached by a fragment that is not the whole live warp */
121 int max_frags, peak_bars;
122} WsSim;
123
124static void ws_fail(WsSim *s, int err, int pc, const char *what) {
125 if (s->err) return;
126 s->err = err; s->err_pc = pc;
127 snprintf(s->msg, sizeof s->msg, "%s at pc %d: %s", ws_err_name(err), pc, what ? what : "");
128}
129
130static uint32_t ws_rd(const WsLane *l, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v]; return ph < 0 ? 0 : l->r[ph & 0xff]; }
131static uint64_t ws_rd64(const WsLane *l, int v, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v] & 0xff; return (uint64_t)l->r[ph] | ((uint64_t)l->r[(ph + 1) & 0xff] << 32); }
132static void ws_wr(WsLane *l, int v, uint32_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v]; if (ph >= 0) l->r[ph & 0xff] = x; }
133static void ws_wr64(WsLane *l, int v, uint64_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v] & 0xff; l->r[ph] = (uint32_t)x; l->r[(ph + 1) & 0xff] = (uint32_t)(x >> 32); }
134/* sub-register operands (quad/pair lanes, BlackwellIRInsn.*_subreg) */
135static uint32_t ws_rds(const WsLane *l, int v, unsigned sub, const BlackwellIRProgram *p) { if (v < 0) return 0; int ph = p->regalloc.vreg_to_phys[v]; return ph < 0 ? 0 : l->r[(ph + sub) & 0xff]; }
136static void ws_wrs(WsLane *l, int v, unsigned sub, uint32_t x, const BlackwellIRProgram *p) { if (v < 0) return; int ph = p->regalloc.vreg_to_phys[v]; if (ph >= 0) l->r[(ph + sub) & 0xff] = x; }
137static float ws_u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; }
138static uint32_t ws_k_bf16_rn(uint32_t u) { if ((u & 0x7fffffffu) > 0x7f800000u) return 0x7fc0u; return (u + 0x7fffu + ((u >> 16) & 1u)) >> 16; } /* f32 bits -> bf16 bits, round to nearest even (as bw_warp_sim_tc.h) */
139static uint32_t ws_f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
140static uint32_t ws_lut3(uint32_t lut, uint32_t a, uint32_t b, uint32_t c) {
141 uint32_t r = 0;
142 for (int i = 0; i < 8; i++) if (lut >> i & 1) r |= ((i & 4) ? a : ~a) & ((i & 2) ? b : ~b) & ((i & 1) ? c : ~c);
143 return r;
144}
145static int ws_mem_ok(WsSim *s, uint64_t a, unsigned bytes, int pc) {
146 if (s->nranges == 0) return 1;
147 for (int i = 0; i < s->nranges; i++) if (a >= s->ranges[i].lo && a + bytes <= s->ranges[i].hi) return 1;
148 if (getenv("WS_DBG")) { fprintf(stderr, "bad addr %llx +%u pc %d\n", (unsigned long long)a, bytes, pc); for (int i = 0; i < s->nranges; i++) fprintf(stderr, " range %d %llx..%llx\n", i, (unsigned long long)s->ranges[i].lo, (unsigned long long)s->ranges[i].hi); }
149 ws_fail(s, WS_E_MEM, pc, "global access outside the declared buffers"); return 0;
150}
151static uint32_t *ws_smem(WsSim *s, uint32_t a, int pc) {
152 if ((a & 3) || a + 4 > s->smem_bytes) { ws_fail(s, WS_E_SMEM, pc, "shared access past the declared size"); return NULL; }
153 return (uint32_t *)(s->smem + a);
154}
155static int ws_popc(uint32_t m) { return __builtin_popcount(m); }
156
157/* One lane, one non-control instruction. */
158static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, uint32_t tid, uint32_t cx, uint32_t cy) {
159 const BlackwellIRProgram *p = s->p;
160 uint32_t a = ws_rds(l, in->src1_vreg, in->src1_subreg, p), b = ws_rds(l, in->src2_vreg, in->src2_subreg, p), c = ws_rds(l, in->src3_vreg, in->src3_subreg, p);
161 switch (in->op) {
162 case BW_IR_NOP: break;
163 case BW_IR_S2R: ws_wrs(l, in->dst_vreg, in->dst_subreg, in->imm == BW_SR_TID_X ? tid : in->imm == BW_SR_CTAID_X ? cx : in->imm == BW_SR_CTAID_Y ? cy : 0, p); break;
164 case BW_IR_LDC: ws_wrs(l, in->dst_vreg, in->dst_subreg, s->cbank[(in->imm / 4) & 0xff], p); break;
165 case BW_IR_LDC64: ws_wr64(l, in->dst_vreg, (uint64_t)s->cbank[(in->imm / 4) & 0xff] | ((uint64_t)s->cbank[(in->imm / 4 + 1) & 0xff] << 32), p); break;
166 case BW_IR_LDCU64: case BW_IR_LDCU: break; /* memory descriptor: not modelled */
167 case BW_IR_MOV_IMM: ws_wrs(l, in->dst_vreg, in->dst_subreg, in->imm, p); break;
168 case BW_IR_MOV_RZ: ws_wrs(l, in->dst_vreg, in->dst_subreg, 0, p); break;
169 case BW_IR_IMAD: ws_wrs(l, in->dst_vreg, in->dst_subreg, in->src2_vreg >= 0 ? a * b + c : a * in->imm + c, p); break;
170 case BW_IR_IMAD_WIDE: ws_wr64(l, in->dst_vreg, (uint64_t)a * in->imm + ws_rd64(l, in->src3_vreg, p), p); break;
171 case BW_IR_IMAD_HI_U32: { /* Rd = hi32(Ra * Rb + Rc+1:Rc). A register Rc is a 64-bit pair, low word Rc (omega #492, nvcc 13.0
172 * oracle evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016, not chip-probed); an odd Rc has no evidence: refused */
173 uint64_t c64 = 0;
174 int rc = in->src3_vreg >= 0 ? p->regalloc.vreg_to_phys[in->src3_vreg] : -1;
175 if (rc >= 0) {
176 rc = (rc + in->src3_subreg) & 0xff;
177 if (rc & 1) { ws_fail(s, WS_E_IMADHI_RC, pc, "IMAD.HI.U32 with an odd register Rc (the chip reads a 64-bit pair; nvcc emits only even bases, odd not chip-probed): use RZ or an even pair"); return; }
178 c64 = (uint64_t)l->r[rc] | ((uint64_t)l->r[rc + 1] << 32);
179 }
180 ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b + c64) >> 32), p); break; }
181 case BW_IR_IADD3: ws_wrs(l, in->dst_vreg, in->dst_subreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */
182 case BW_IR_SHF_R: ws_wrs(l, in->dst_vreg, in->dst_subreg, a >> (in->imm & 31), p); break;
183 case BW_IR_SHF_L_U32: if (b >= 32) { ws_fail(s, WS_E_SHIFT, pc, "SHF.L.U32 amount >= 32 (not chip-verified)"); return; } ws_wrs(l, in->dst_vreg, in->dst_subreg, a << b, p); break;
184 case BW_IR_LOP3_AND: ws_wrs(l, in->dst_vreg, in->dst_subreg, a & in->imm, p); break;
185 case BW_IR_LOP3_XOR: ws_wrs(l, in->dst_vreg, in->dst_subreg, a ^ b, p); break;
186 case BW_IR_LOP3_LUT: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_lut3(in->imm & 0xff, a, b, c), p); break;
187 case BW_IR_ISETP_GE_U32: l->p0 = a >= b; break;
188 case BW_IR_LDG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p) + (uint64_t)(int64_t)(int32_t)in->imm; if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t x; memcpy(&x, (const void *)(uintptr_t)ad, 4); ws_wrs(l, in->dst_vreg, in->dst_subreg, x, p); break; }
189 case BW_IR_LDG_E_U16: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 2, pc)) return; uint16_t x; memcpy(&x, (const void *)(uintptr_t)ad, 2); ws_wrs(l, in->dst_vreg, in->dst_subreg, x, p); break; }
190 /* STG.E takes the signed 24-bit byte offset the encoder writes (nvdisasm 13.0 -b SM121 decodes "STG.E desc[UR4][R4.64+0x1e0c], R9", omega#542) */
191 case BW_IR_STG_E: { uint64_t ad = ws_rd64(l, in->src1_vreg, p) + (uint64_t)(int64_t)(int32_t)in->imm; if (!ws_mem_ok(s, ad, 4, pc)) return; memcpy((void *)(uintptr_t)ad, &b, 4); break; }
192 case BW_IR_LDG_E_64: case BW_IR_LDG_E_128: { unsigned n = in->op == BW_IR_LDG_E_64 ? 8u : 16u; uint64_t ad = ws_rd64(l, in->src1_vreg, p) + (uint64_t)(int64_t)(int32_t)in->imm; if (!ws_mem_ok(s, ad, n, pc)) { if (getenv("WS_DBG")) fprintf(stderr, " tid %u cx %u cy %u\n", tid, cx, cy); return; } if (ad & (n - 1)) { ws_fail(s, WS_E_MEM, pc, "wide global load not naturally aligned"); return; } uint32_t x[4]; memcpy(x, (const void *)(uintptr_t)ad, n); for (unsigned i = 0; i < n / 4; i++) ws_wrs(l, in->dst_vreg, i, x[i], p); break; }
193 case BW_IR_LDS128: { uint32_t ad = a + in->imm; if (ad & 15u) { ws_fail(s, WS_E_SMEM, pc, "LDS.128 address not 16-byte aligned"); return; } for (unsigned i = 0; i < 4; i++) { uint32_t *m = ws_smem(s, ad + 4u * i, pc); if (!m) return; ws_wrs(l, in->dst_vreg, i, *m, p); } break; }
194 case BW_IR_LDS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; ws_wrs(l, in->dst_vreg, in->dst_subreg, *m, p); break; }
195 case BW_IR_STS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; *m = b; break; }
196 case BW_IR_FADD: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) + ws_u2f(b)), p); break;
197 case BW_IR_FSUB: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) - ws_u2f(b)), p); break;
198 case BW_IR_FMUL: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) * ws_u2f(b)), p); break;
199 case BW_IR_I2FP: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u((float)(int32_t)a), p); break; /* I2FP.F32.S32, round to nearest */
200 case BW_IR_FFMA: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(fmaf(ws_u2f(a), ws_u2f(b), ws_u2f(c))), p); break;
201 case BW_IR_F2FP_BF16_PACK: ws_wrs(l, in->dst_vreg, in->dst_subreg, (ws_k_bf16_rn(a) << 16) | ws_k_bf16_rn(b), p); break; /* F2FP.BF16.F32.PACK_AB (L313 bf16 state) */
202 case BW_IR_FMNMX_MAX: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(fmaxf(ws_u2f(a), ws_u2f(b))), p); break;
203 case BW_IR_FMNMX_MIN: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(fminf(ws_u2f(a), ws_u2f(b))), p); break;
204 case BW_IR_MUFU_EX2: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_ex2(ws_u2f(a))), p); break;
205 case BW_IR_MUFU_RCP: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_rcp(ws_u2f(a))), p); break;
206 case BW_IR_MUFU_RSQ: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_rsq(ws_u2f(a))), p); break;
207 case BW_IR_MUFU_LG2: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(log2f(ws_u2f(a))), p); break;
208 case BW_IR_LDG_STRONG_SYS: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t x; memcpy(&x, (const void *)(uintptr_t)ad, 4); ws_wrs(l, in->dst_vreg, in->dst_subreg, x, p); break; }
209 case BW_IR_STG_STRONG_SYS: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; memcpy((void *)(uintptr_t)ad, &b, 4); break; }
210 case BW_IR_ATOMG_ADD_STRONG_SYS: { uint64_t ad = ws_rd64(l, in->src1_vreg, p); if (!ws_mem_ok(s, ad, 4, pc)) return; uint32_t old; memcpy(&old, (const void *)(uintptr_t)ad, 4); uint32_t nw = old + b; memcpy((void *)(uintptr_t)ad, &nw, 4); ws_wrs(l, in->dst_vreg, in->dst_subreg, old, p); break; }
211 case BW_IR_MEMBAR_ALL_SYS: break;
212 default: ws_fail(s, WS_E_OP, pc, "opcode not modelled"); return;
213 }
214}
215
216static int ws_pick(const WsSim *s, const WsWarp *w) {
217 int best = -1;
218 for (int i = 0; i < w->nfrag; i++) {
219 if (w->frag[i].mask == 0 || w->frag[i].parked) continue;
220 if (best < 0) { best = i; continue; }
221 if (s->order == 0 ? w->frag[i].pc < w->frag[best].pc : w->frag[i].pc > w->frag[best].pc) best = i;
222 }
223 return best;
224}
225static void ws_drop_frag(WsWarp *w, int i) { w->frag[i] = w->frag[--w->nfrag]; }
226static int ws_add_frag(WsSim *s, WsWarp *w, int pc, uint32_t mask) {
227 if (mask == 0) return 0;
228 if (w->nfrag >= WS_MAX_FRAGS) { ws_fail(s, WS_E_FRAGS, pc, "too many fragments"); return -1; }
229 w->frag[w->nfrag].pc = pc; w->frag[w->nfrag].mask = mask; w->frag[w->nfrag].parked = 0; w->nfrag++;
230 if (w->nfrag > s->max_frags) s->max_frags = w->nfrag;
231 return 0;
232}
233
234/* Runs one warp until it has no runnable fragment (finished, parked at BAR.SYNC, or stuck).
235 * Returns 1 when parked at BAR.SYNC, 0 when finished, -1 on error. */
236static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t cy, int trace) {
237 const BlackwellIRProgram *p = s->p;
238 const uint32_t live_all = 0xffffffffu;
239 for (uint64_t guard = 0;; guard++) {
240 if (s->err) return -1;
241 if (guard > (s->step_budget ? s->step_budget : 2000000000ull)) { ws_fail(s, WS_E_STEPS, -1, "step budget"); return -1; }
242 int fi = ws_pick(s, w);
243 if (fi < 0) {
244 /* nothing runnable: parked at the CTA barrier, finished, or lanes waiting at a BSYNC
245 * that can never complete */
246 for (int i = 0; i < w->nfrag; i++) if (w->frag[i].parked) return 1;
247 for (int b = 0; b < WS_NBAR; b++) if (w->bar[b].valid) {
248 if (w->bar[b].arrived) { ws_fail(s, WS_E_STUCK, w->bar[b].target - 1, "lanes parked at BSYNC, the rest never arrive"); return -1; }
249 ws_fail(s, WS_E_NEVER_JOINED, w->bar[b].target - 1, "warp finished with a barrier still armed"); return -1;
250 }
251 return 0;
252 }
253 WsFrag *f = &w->frag[fi];
254 int pc = f->pc;
255 if (pc < 0 || (size_t)pc >= p->count) { ws_fail(s, WS_E_PC, pc, "pc outside the program"); return -1; }
256 const BlackwellIRInsn *in = &p->insns[pc];
257 uint32_t mask = f->mask;
258 s->warp_issues++; s->lane_issues += (uint64_t)ws_popc(mask);
259 if (trace) printf("W%u pc=%d op=%d mask=%08x frags=%d\n", warp, pc, (int)in->op, mask, w->nfrag);
260 uint32_t live = live_all & ~w->exited;
261 uint32_t pmask = 0;
262 for (int L = 0; L < WS_WARP; L++) if ((mask >> L & 1) && w->lane[L].p0) pmask |= 1u << L;
263 uint32_t cond = in->predicate_p0 ? (in->predicate_not ? (mask & ~pmask) : (mask & pmask)) : mask;
264 switch (in->op) {
265 case BW_IR_EXIT: {
266 w->exited |= cond;
267 f->mask = mask & ~cond;
268 if (f->mask == 0) ws_drop_frag(w, fi); else f->pc = pc + 1;
269 break;
270 }
271 case BW_IR_BRA: {
272 int32_t delta = (int32_t)in->imm;
273 uint32_t taken = cond, fall = mask & ~cond;
274 if (taken && delta == 0) { ws_fail(s, WS_E_SELF_BRANCH, pc, "branch to itself"); return -1; }
275 if (taken && fall) s->splits++;
276 if (taken == 0) { f->pc = pc + 1; break; }
277 if (fall == 0) { f->pc = pc + delta; break; }
278 f->mask = fall; f->pc = pc + 1;
279 if (ws_add_frag(s, w, pc + delta, taken) < 0) return -1;
280 break;
281 }
282 case BW_IR_BSSY: {
283 WsBar *b = &w->bar[in->bar_reg & (WS_NBAR - 1)];
284 if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; }
285 if (b->valid) { ws_fail(s, WS_E_BSSY_REARM, pc, "BSSY on an armed barrier"); return -1; }
286 b->valid = 1; b->target = pc + (int32_t)in->imm; b->members = cond; b->arrived = 0;
287 int armed = 0; for (int k = 0; k < WS_NBAR; k++) armed += w->bar[k].valid;
288 if (armed > s->peak_bars) s->peak_bars = armed;
289 f->pc = pc + 1;
290 break;
291 }
292 case BW_IR_BSYNC: {
293 if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; }
294 WsBar *b = &w->bar[in->bar_reg];
295 if (!b->valid) { ws_fail(s, WS_E_BSYNC_UNARMED, pc, "BSYNC without a BSSY"); return -1; }
296 if (cond & ~b->members) { ws_fail(s, WS_E_NONMEMBER, pc, "a lane at BSYNC did not execute the BSSY"); return -1; }
297 b->arrived |= cond;
298 uint32_t rest = mask & ~cond; /* a predicated-off lane walks on */
299 if (rest) { f->mask = rest; f->pc = pc + 1; } else ws_drop_frag(w, fi);
300 if (b->arrived == (b->members & ~w->exited)) {
301 if (b->target != pc + 1) { ws_fail(s, WS_E_JOIN_MISMATCH, pc, "BSSY named a different join point"); return -1; }
302 uint32_t joined = b->arrived; b->valid = 0; b->arrived = 0; b->members = 0;
303 if (ws_add_frag(s, w, pc + 1, joined) < 0) return -1;
304 }
305 break;
306 }
307 case BW_IR_SHFL_DOWN: case BW_IR_SHFL_BFLY: {
308 if (w->exited) { ws_fail(s, WS_E_SHFL_EXITED, pc, "SHFL on a warp with exited lanes"); return -1; }
309 if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "SHFL on a split warp"); return -1; }
310 uint32_t src[WS_WARP];
311 for (int L = 0; L < WS_WARP; L++) src[L] = ws_rd(&w->lane[L], in->src1_vreg, p);
312 /* SHFL.BFLY: lane L reads L ^ lane */
313 if (in->op == BW_IR_SHFL_BFLY) { for (int L = 0; L < WS_WARP; L++) ws_wr(&w->lane[L], in->dst_vreg, src[L ^ (int)(in->imm & 31)], p); f->pc = pc + 1; break; }
314 for (int L = 0; L < WS_WARP; L++) { int from = L + (int)(in->imm & 31); ws_wr(&w->lane[L], in->dst_vreg, from < WS_WARP ? src[from] : src[L], p); }
315 f->pc = pc + 1;
316 break;
317 }
318 case BW_IR_BAR_SYNC: {
319 if (mask != live) s->bar_split_arrivals++;
320 f->parked = 1; /* released by ws_run when every live lane of the CTA has arrived */
321 break;
322 }
323 default: {
324 for (int L = 0; L < WS_WARP; L++) if (mask >> L & 1) {
325 ws_lane_exec(s, &w->lane[L], in, pc, warp * WS_WARP + (uint32_t)L, cx, cy);
326 if (s->err) return -1;
327 }
328 f->pc = pc + 1;
329 break;
330 }
331 }
332 }
333}
334
335/* Runs every CTA of the grid. Returns s->err (0 = clean). */
336static int ws_run(WsSim *s, uint32_t grid_x, uint32_t grid_y) {
337 s->err = 0; s->msg[0] = 0; s->warp_issues = s->lane_issues = s->splits = s->bar_split_arrivals = 0; s->max_frags = 0; s->peak_bars = 0;
338 if (!s->p || s->threads == 0 || s->threads % WS_WARP || s->threads > WS_MAX_THREADS) { ws_fail(s, WS_E_OP, -1, "bad simulator configuration"); return s->err; }
339 static WsWarp warps[WS_MAX_WARPS];
340 uint32_t nw = s->threads / WS_WARP;
341 for (uint32_t cy = 0; cy < grid_y && !s->err; cy++) for (uint32_t cx = 0; cx < grid_x && !s->err; cx++) {
342 memset(warps, 0, sizeof(WsWarp) * nw);
343 if (s->smem && s->smem_bytes) for (uint32_t i = 0; i + 4 <= s->smem_bytes; i += 4) { uint32_t poison = 0xfeedfaceu; memcpy(s->smem + i, &poison, 4); }
344 for (uint32_t w = 0; w < nw; w++) { warps[w].nfrag = 1; warps[w].frag[0].pc = 0; warps[w].frag[0].mask = 0xffffffffu; warps[w].frag[0].parked = 0; }
345 int trace = s->trace && cx == 0 && cy == 0;
346 /* Barrier phases: every warp runs until all of its fragments are parked at BAR.SYNC or it
347 * has finished (exited lanes do not hold the barrier up: INFERRED, see WS_E_BAR_SPLIT).
348 * Then every parked fragment is released and the next phase starts. */
349 uint32_t rng = s->warp_sched;
350 for (int phase = 0; ; phase++) {
351 int parked = 0;
352 /* visit order: the chip gives no order between warps, so a slot or barrier mistake that the fixed order hides
353 * must show under the reversed or a shuffled one */
354 uint32_t vis[WS_MAX_WARPS];
355 for (uint32_t i = 0; i < nw; i++) vis[i] = s->warp_sched == 1 ? nw - 1u - i : i;
356 if (s->warp_sched > 1) for (uint32_t i = nw - 1u; i > 0; i--) { rng = rng * 1664525u + 1013904223u; uint32_t j = (rng >> 8) % (i + 1u); uint32_t t = vis[i]; vis[i] = vis[j]; vis[j] = t; }
357 for (uint32_t vi = 0; vi < nw && !s->err; vi++) {
358 const uint32_t w = vis[vi];
359 if (warps[w].nfrag == 0) continue;
360 int r = ws_run_warp(s, &warps[w], w, cx, cy, trace && w == 0);
361 if (r < 0) break;
362 if (r == 1) parked++;
363 }
364 if (s->err || parked == 0) break; /* error, or every warp finished */
365 for (uint32_t w = 0; w < nw; w++) for (int i = 0; i < warps[w].nfrag; i++)
366 if (warps[w].frag[i].parked) { warps[w].frag[i].parked = 0; warps[w].frag[i].pc++; }
367 if (phase > 100000) { ws_fail(s, WS_E_BAR_TIMEOUT, -1, "barrier phases"); break; }
368 }
369 }
370 return s->err;
371}
372
373#endif /* BW_WARP_SIM_K_H */