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.h

354 lines · 23510 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_H
57#define BW_WARP_SIM_H
58
59#include "omega_blackwell_codegen.h"
60#include "bw_mufu_ftz.h" /* MUFU EX2 / RCP / RSQ: the chip's flush of subnormal inputs and results (omega #462) */
61#include <math.h>
62#include <stdint.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66
67#define WS_WARP 32
68#define WS_MAX_THREADS 256
69#define WS_MAX_WARPS (WS_MAX_THREADS / WS_WARP)
70#define WS_MAX_FRAGS 64
71#define WS_NBAR BW_RECONV_MAX_BAR
72#define WS_MAX_RANGES 8
73
74enum {
75 WS_OK = 0, WS_E_SHFL_SPLIT, WS_E_SHFL_EXITED, WS_E_BAR_SPLIT, WS_E_BSYNC_UNARMED, WS_E_BSSY_REARM,
76 WS_E_JOIN_MISMATCH, WS_E_NONMEMBER, WS_E_STUCK, WS_E_NEVER_JOINED, WS_E_SELF_BRANCH, WS_E_PC,
77 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
78};
79static const char *ws_err_name(int e) {
80 static const char *const n[] = { "OK", "SHFL_SPLIT", "SHFL_EXITED", "BAR_SPLIT", "BSYNC_UNARMED", "BSSY_REARM",
81 "JOIN_MISMATCH", "NONMEMBER", "STUCK", "NEVER_JOINED", "SELF_BRANCH", "PC", "OP", "SHIFT", "MEM", "SMEM",
82 "FRAGS", "BAR_TIMEOUT", "STEPS", "IMADHI_RC" };
83 return (e >= 0 && e < (int)(sizeof n / sizeof n[0])) ? n[e] : "?";
84}
85
86typedef struct { uint32_t r[256]; uint8_t p0; } WsLane;
87typedef struct { int pc; uint32_t mask; int parked; /* waiting at a BAR.SYNC */ } WsFrag;
88typedef struct { int valid; int target; uint32_t members, arrived; } WsBar;
89typedef struct {
90 WsLane lane[WS_WARP];
91 WsFrag frag[WS_MAX_FRAGS];
92 int nfrag;
93 WsBar bar[WS_NBAR];
94 uint32_t exited;
95} WsWarp;
96typedef struct { uintptr_t lo, hi; } WsRange;
97
98typedef struct {
99 const BlackwellIRProgram *p;
100 uint32_t cbank[256]; /* constant bank 0 words (byte offset / 4) */
101 uint32_t threads; /* per CTA, a multiple of 32, <= WS_MAX_THREADS */
102 uint8_t *smem; /* per-CTA shared memory buffer (poisoned at CTA start) */
103 uint32_t smem_bytes;
104 WsRange ranges[WS_MAX_RANGES]; int nranges; /* when > 0, every global access must fall in one */
105 int order; /* 0 = lowest pc first, 1 = highest pc first */
106 uint32_t warp_sched; /* warp visit order per barrier phase: 0 ascending, 1 descending, else a seed for a fresh shuffle each phase
107 * (the chip gives no order between warps; port of omega#490 bw_warp_sim_k.h) */
108 int trace; /* print every issue of warp 0 of CTA (0,0) */
109 /* results */
110 int err; int err_pc; char msg[200];
111 uint64_t warp_issues; /* fragment-instruction issues (the chip's cost unit for divergence) */
112 uint64_t lane_issues; /* lanes active over those issues */
113 uint64_t splits; /* branches that split a fragment */
114 uint64_t bar_split_arrivals; /* BAR.SYNC reached by a fragment that is not the whole live warp */
115 int max_frags, peak_bars;
116} WsSim;
117
118static void ws_fail(WsSim *s, int err, int pc, const char *what) {
119 if (s->err) return;
120 s->err = err; s->err_pc = pc;
121 snprintf(s->msg, sizeof s->msg, "%s at pc %d: %s", ws_err_name(err), pc, what ? what : "");
122}
123
124static 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]; }
125static 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); }
126static 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; }
127static 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); }
128/* sub-register operands (quad/pair lanes, BlackwellIRInsn.*_subreg) */
129static 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]; }
130static 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; }
131static float ws_u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; }
132static uint32_t ws_f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
133static uint32_t ws_lut3(uint32_t lut, uint32_t a, uint32_t b, uint32_t c) {
134 uint32_t r = 0;
135 for (int i = 0; i < 8; i++) if (lut >> i & 1) r |= ((i & 4) ? a : ~a) & ((i & 2) ? b : ~b) & ((i & 1) ? c : ~c);
136 return r;
137}
138static int ws_mem_ok(WsSim *s, uint64_t a, unsigned bytes, int pc) {
139 if (s->nranges == 0) return 1;
140 for (int i = 0; i < s->nranges; i++) if (a >= s->ranges[i].lo && a + bytes <= s->ranges[i].hi) return 1;
141 ws_fail(s, WS_E_MEM, pc, "global access outside the declared buffers"); return 0;
142}
143static uint32_t *ws_smem(WsSim *s, uint32_t a, int pc) {
144 if ((a & 3) || a + 4 > s->smem_bytes) { ws_fail(s, WS_E_SMEM, pc, "shared access past the declared size"); return NULL; }
145 return (uint32_t *)(s->smem + a);
146}
147static int ws_popc(uint32_t m) { return __builtin_popcount(m); }
148
149/* One lane, one non-control instruction. */
150static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, uint32_t tid, uint32_t cx, uint32_t cy) {
151 const BlackwellIRProgram *p = s->p;
152 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);
153 switch (in->op) {
154 case BW_IR_NOP: break;
155 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;
156 case BW_IR_LDC: ws_wrs(l, in->dst_vreg, in->dst_subreg, s->cbank[(in->imm / 4) & 0xff], p); break;
157 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;
158 case BW_IR_LDCU64: case BW_IR_LDCU: break; /* memory descriptor: not modelled */
159 case BW_IR_MOV_IMM: ws_wrs(l, in->dst_vreg, in->dst_subreg, in->imm, p); break;
160 case BW_IR_MOV_RZ: ws_wrs(l, in->dst_vreg, in->dst_subreg, 0, p); break;
161 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;
162 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;
163 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
164 * oracle evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016, not chip-probed); an odd Rc has no evidence: refused */
165 uint64_t c64 = 0;
166 int rc = in->src3_vreg >= 0 ? p->regalloc.vreg_to_phys[in->src3_vreg] : -1;
167 if (rc >= 0) {
168 rc = (rc + in->src3_subreg) & 0xff;
169 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; }
170 c64 = (uint64_t)l->r[rc] | ((uint64_t)l->r[rc + 1] << 32);
171 }
172 ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b + c64) >> 32), p); break; }
173 case BW_IR_IADD3: ws_wrs(l, in->dst_vreg, in->dst_subreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */
174 case BW_IR_SHF_R: ws_wrs(l, in->dst_vreg, in->dst_subreg, a >> (in->imm & 31), p); break;
175 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;
176 case BW_IR_LOP3_AND: ws_wrs(l, in->dst_vreg, in->dst_subreg, a & in->imm, p); break;
177 case BW_IR_LOP3_XOR: ws_wrs(l, in->dst_vreg, in->dst_subreg, a ^ b, p); break;
178 case BW_IR_LOP3_LUT: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_lut3(in->imm & 0xff, a, b, c), p); break;
179 case BW_IR_ISETP_GE_U32: l->p0 = a >= b; break;
180 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; }
181 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; }
182 case BW_IR_STG_E: { 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; }
183 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)) 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; }
184 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; }
185 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; }
186 case BW_IR_STS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; *m = b; break; }
187 case BW_IR_FADD: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) + ws_u2f(b)), p); break;
188 case BW_IR_FSUB: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) - ws_u2f(b)), p); break;
189 case BW_IR_FMUL: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) * ws_u2f(b)), p); break;
190 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;
191 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;
192 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;
193 case BW_IR_MUFU_EX2: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_ex2(ws_u2f(a))), p); break;
194 case BW_IR_MUFU_RCP: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_rcp(ws_u2f(a))), p); break;
195 case BW_IR_MUFU_RSQ: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_rsq(ws_u2f(a))), p); break;
196 default: ws_fail(s, WS_E_OP, pc, "opcode not modelled"); return;
197 }
198}
199
200static int ws_pick(const WsSim *s, const WsWarp *w) {
201 int best = -1;
202 for (int i = 0; i < w->nfrag; i++) {
203 if (w->frag[i].mask == 0 || w->frag[i].parked) continue;
204 if (best < 0) { best = i; continue; }
205 if (s->order == 0 ? w->frag[i].pc < w->frag[best].pc : w->frag[i].pc > w->frag[best].pc) best = i;
206 }
207 return best;
208}
209static void ws_drop_frag(WsWarp *w, int i) { w->frag[i] = w->frag[--w->nfrag]; }
210static int ws_add_frag(WsSim *s, WsWarp *w, int pc, uint32_t mask) {
211 if (mask == 0) return 0;
212 if (w->nfrag >= WS_MAX_FRAGS) { ws_fail(s, WS_E_FRAGS, pc, "too many fragments"); return -1; }
213 w->frag[w->nfrag].pc = pc; w->frag[w->nfrag].mask = mask; w->frag[w->nfrag].parked = 0; w->nfrag++;
214 if (w->nfrag > s->max_frags) s->max_frags = w->nfrag;
215 return 0;
216}
217
218/* Runs one warp until it has no runnable fragment (finished, parked at BAR.SYNC, or stuck).
219 * Returns 1 when parked at BAR.SYNC, 0 when finished, -1 on error. */
220static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t cy, int trace) {
221 const BlackwellIRProgram *p = s->p;
222 const uint32_t live_all = 0xffffffffu;
223 for (uint64_t guard = 0;; guard++) {
224 if (s->err) return -1;
225 if (guard > 2000000000ull) { ws_fail(s, WS_E_STEPS, -1, "step budget"); return -1; }
226 int fi = ws_pick(s, w);
227 if (fi < 0) {
228 /* nothing runnable: parked at the CTA barrier, finished, or lanes waiting at a BSYNC
229 * that can never complete */
230 for (int i = 0; i < w->nfrag; i++) if (w->frag[i].parked) return 1;
231 for (int b = 0; b < WS_NBAR; b++) if (w->bar[b].valid) {
232 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; }
233 ws_fail(s, WS_E_NEVER_JOINED, w->bar[b].target - 1, "warp finished with a barrier still armed"); return -1;
234 }
235 return 0;
236 }
237 WsFrag *f = &w->frag[fi];
238 int pc = f->pc;
239 if (pc < 0 || (size_t)pc >= p->count) { ws_fail(s, WS_E_PC, pc, "pc outside the program"); return -1; }
240 const BlackwellIRInsn *in = &p->insns[pc];
241 uint32_t mask = f->mask;
242 s->warp_issues++; s->lane_issues += (uint64_t)ws_popc(mask);
243 if (trace) printf("W%u pc=%d op=%d mask=%08x frags=%d\n", warp, pc, (int)in->op, mask, w->nfrag);
244 uint32_t live = live_all & ~w->exited;
245 uint32_t pmask = 0;
246 for (int L = 0; L < WS_WARP; L++) if ((mask >> L & 1) && w->lane[L].p0) pmask |= 1u << L;
247 uint32_t cond = in->predicate_p0 ? (in->predicate_not ? (mask & ~pmask) : (mask & pmask)) : mask;
248 switch (in->op) {
249 case BW_IR_EXIT: {
250 w->exited |= cond;
251 f->mask = mask & ~cond;
252 if (f->mask == 0) ws_drop_frag(w, fi); else f->pc = pc + 1;
253 break;
254 }
255 case BW_IR_BRA: {
256 int32_t delta = (int32_t)in->imm;
257 uint32_t taken = cond, fall = mask & ~cond;
258 if (taken && delta == 0) { ws_fail(s, WS_E_SELF_BRANCH, pc, "branch to itself"); return -1; }
259 if (taken && fall) s->splits++;
260 if (taken == 0) { f->pc = pc + 1; break; }
261 if (fall == 0) { f->pc = pc + delta; break; }
262 f->mask = fall; f->pc = pc + 1;
263 if (ws_add_frag(s, w, pc + delta, taken) < 0) return -1;
264 break;
265 }
266 case BW_IR_BSSY: {
267 WsBar *b = &w->bar[in->bar_reg & (WS_NBAR - 1)];
268 if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; }
269 if (b->valid) { ws_fail(s, WS_E_BSSY_REARM, pc, "BSSY on an armed barrier"); return -1; }
270 b->valid = 1; b->target = pc + (int32_t)in->imm; b->members = cond; b->arrived = 0;
271 int armed = 0; for (int k = 0; k < WS_NBAR; k++) armed += w->bar[k].valid;
272 if (armed > s->peak_bars) s->peak_bars = armed;
273 f->pc = pc + 1;
274 break;
275 }
276 case BW_IR_BSYNC: {
277 if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; }
278 WsBar *b = &w->bar[in->bar_reg];
279 if (!b->valid) { ws_fail(s, WS_E_BSYNC_UNARMED, pc, "BSYNC without a BSSY"); return -1; }
280 if (cond & ~b->members) { ws_fail(s, WS_E_NONMEMBER, pc, "a lane at BSYNC did not execute the BSSY"); return -1; }
281 b->arrived |= cond;
282 uint32_t rest = mask & ~cond; /* a predicated-off lane walks on */
283 if (rest) { f->mask = rest; f->pc = pc + 1; } else ws_drop_frag(w, fi);
284 if (b->arrived == (b->members & ~w->exited)) {
285 if (b->target != pc + 1) { ws_fail(s, WS_E_JOIN_MISMATCH, pc, "BSSY named a different join point"); return -1; }
286 uint32_t joined = b->arrived; b->valid = 0; b->arrived = 0; b->members = 0;
287 if (ws_add_frag(s, w, pc + 1, joined) < 0) return -1;
288 }
289 break;
290 }
291 case BW_IR_SHFL_DOWN: {
292 if (w->exited) { ws_fail(s, WS_E_SHFL_EXITED, pc, "SHFL on a warp with exited lanes"); return -1; }
293 if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "SHFL on a split warp"); return -1; }
294 uint32_t src[WS_WARP];
295 for (int L = 0; L < WS_WARP; L++) src[L] = ws_rd(&w->lane[L], in->src1_vreg, p);
296 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); }
297 f->pc = pc + 1;
298 break;
299 }
300 case BW_IR_BAR_SYNC: {
301 if (mask != live) s->bar_split_arrivals++;
302 f->parked = 1; /* released by ws_run when every live lane of the CTA has arrived */
303 break;
304 }
305 default: {
306 for (int L = 0; L < WS_WARP; L++) if (mask >> L & 1) {
307 ws_lane_exec(s, &w->lane[L], in, pc, warp * WS_WARP + (uint32_t)L, cx, cy);
308 if (s->err) return -1;
309 }
310 f->pc = pc + 1;
311 break;
312 }
313 }
314 }
315}
316
317/* Runs every CTA of the grid. Returns s->err (0 = clean). */
318static int ws_run(WsSim *s, uint32_t grid_x, uint32_t grid_y) {
319 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;
320 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; }
321 static WsWarp warps[WS_MAX_WARPS];
322 uint32_t nw = s->threads / WS_WARP;
323 for (uint32_t cy = 0; cy < grid_y && !s->err; cy++) for (uint32_t cx = 0; cx < grid_x && !s->err; cx++) {
324 memset(warps, 0, sizeof(WsWarp) * nw);
325 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); }
326 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; }
327 int trace = s->trace && cx == 0 && cy == 0;
328 /* Barrier phases: every warp runs until all of its fragments are parked at BAR.SYNC or it
329 * has finished (exited lanes do not hold the barrier up: INFERRED, see WS_E_BAR_SPLIT).
330 * Then every parked fragment is released and the next phase starts. */
331 uint32_t rng = s->warp_sched;
332 for (int phase = 0; ; phase++) {
333 int parked = 0;
334 /* visit order: a slot or barrier mistake that the ascending order hides must show under the reversed or a shuffled one */
335 uint32_t vis[WS_MAX_WARPS];
336 for (uint32_t i = 0; i < nw; i++) vis[i] = s->warp_sched == 1 ? nw - 1u - i : i;
337 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; }
338 for (uint32_t vi = 0; vi < nw && !s->err; vi++) {
339 const uint32_t w = vis[vi];
340 if (warps[w].nfrag == 0) continue;
341 int r = ws_run_warp(s, &warps[w], w, cx, cy, trace && w == 0);
342 if (r < 0) break;
343 if (r == 1) parked++;
344 }
345 if (s->err || parked == 0) break; /* error, or every warp finished */
346 for (uint32_t w = 0; w < nw; w++) for (int i = 0; i < warps[w].nfrag; i++)
347 if (warps[w].frag[i].parked) { warps[w].frag[i].parked = 0; warps[w].frag[i].pc++; }
348 if (phase > 100000) { ws_fail(s, WS_E_BAR_TIMEOUT, -1, "barrier phases"); break; }
349 }
350 }
351 return s->err;
352}
353
354#endif /* BW_WARP_SIM_H */