Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/evidence/OWN30B-REQUAL-MAIN-710d438c-20261015/gates/reconv/GPU-RECONV-710d438c/bw_warp_sim.h # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / evidence / OWN30B-REQUAL-MAIN-710d438c-20261015 / gates / reconv / GPU-RECONV-710d438c / bw_warp_sim.h` 318 lines ยท 20836 bytes Raw Blame History 1 /* bw_warp_sim.h: host SIMT simulator for the Blackwell IR (omega #308). 2 * 3 * Models what the earlier per-thread simulators could not: a warp of 32 lanes with an 4 * active mask, divergence at predicated branches, structured reconvergence through the 5 * barrier registers (BSSY arms Bn for the lanes executing it and names the join; BSYNC 6 * parks arriving lanes until every armed lane that has not exited is there, then the 7 * warp continues as one), nested regions, and lane participation in SHFL and BAR.SYNC. 8 * 9 * A warp is a set of fragments (pc, lane mask). A predicated branch whose lanes disagree 10 * splits the fragment; the scheduler then runs one fragment at a time in a chosen order 11 * (lowest pc first or highest pc first; tests run both), which is the freedom the chip's 12 * scheduler also has. Fragments only merge at a BSYNC. Errors (all fatal, first one wins): 13 * WS_E_SHFL_SPLIT a SHFL reached by a fragment that is not the whole live warp 14 * (the chip reads inactive lanes: undefined; attention 2026-10-04) 15 * WS_E_SHFL_EXITED a SHFL on a warp some of whose lanes already exited (undefined too) 16 * WS_E_BAR_SPLIT (defined, never raised) a BAR.SYNC reached by a split warp is allowed: 17 * since Volta the CTA barrier counts arriving threads, not warps (CUDA 18 * programming guide, independent thread scheduling; INFERRED for sm_121, 19 * not chip-verified by us). The simulator parks each fragment, releases 20 * them together, counts the event in bar_split_arrivals, and leaves the 21 * fragments split: a barrier is not a reconvergence point, so a SHFL after 22 * it still fails with SHFL_SPLIT unless a BSYNC rejoined the warp. 23 * WS_E_BSYNC_UNARMED BSYNC on a barrier no BSSY armed 24 * WS_E_BSSY_REARM BSSY on a barrier still armed (lanes may still be heading to its join) 25 * WS_E_JOIN_MISMATCH BSYNC at an address other than the one the BSSY named minus one 26 * WS_E_NONMEMBER a lane reached BSYNC Bn without having executed its BSSY 27 * WS_E_STUCK lanes parked at a BSYNC that can never complete (a join was dropped, 28 * an exit jumped past it, or a target was corrupted) 29 * WS_E_NEVER_JOINED the warp finished with a barrier still armed 30 * WS_E_SELF_BRANCH, WS_E_PC, WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS 31 * Every error names the pc. Memory: global addresses are host pointers (optionally range 32 * checked); shared memory is a per-CTA buffer poisoned at CTA start so a read of a word 33 * nobody wrote shows up in the oracle comparison. 34 * 35 * Per-lane ALU semantics are those of the two earlier simulators (tests/gpu_attention_test.c, 36 * bench/prime_race/tests/gb10_sieve_host_test.c). EX2 and RCP are exact here; the chip's 37 * are approximate. LDCU64 (memory descriptor) is not modelled. */ 38 #ifndef BW_WARP_SIM_H 39 #define BW_WARP_SIM_H 40 41 #include "omega_blackwell_codegen.h" 42 #include 43 #include 44 #include 45 #include 46 #include 47 48 #define WS_WARP 32 49 #define WS_MAX_THREADS 256 50 #define WS_MAX_WARPS (WS_MAX_THREADS / WS_WARP) 51 #define WS_MAX_FRAGS 64 52 #define WS_NBAR BW_RECONV_MAX_BAR 53 #define WS_MAX_RANGES 8 54 55 enum { 56 WS_OK = 0, WS_E_SHFL_SPLIT, WS_E_SHFL_EXITED, WS_E_BAR_SPLIT, WS_E_BSYNC_UNARMED, WS_E_BSSY_REARM, 57 WS_E_JOIN_MISMATCH, WS_E_NONMEMBER, WS_E_STUCK, WS_E_NEVER_JOINED, WS_E_SELF_BRANCH, WS_E_PC, 58 WS_E_OP, WS_E_SHIFT, WS_E_MEM, WS_E_SMEM, WS_E_FRAGS, WS_E_BAR_TIMEOUT, WS_E_STEPS 59 }; 60 static const char *ws_err_name(int e) { 61 static const char *const n[] = { "OK", "SHFL_SPLIT", "SHFL_EXITED", "BAR_SPLIT", "BSYNC_UNARMED", "BSSY_REARM", 62 "JOIN_MISMATCH", "NONMEMBER", "STUCK", "NEVER_JOINED", "SELF_BRANCH", "PC", "OP", "SHIFT", "MEM", "SMEM", 63 "FRAGS", "BAR_TIMEOUT", "STEPS" }; 64 return (e >= 0 && e < (int)(sizeof n / sizeof n[0])) ? n[e] : "?"; 65 } 66 67 typedef struct { uint32_t r[256]; uint8_t p0; } WsLane; 68 typedef struct { int pc; uint32_t mask; int parked; /* waiting at a BAR.SYNC */ } WsFrag; 69 typedef struct { int valid; int target; uint32_t members, arrived; } WsBar; 70 typedef struct { 71 WsLane lane[WS_WARP]; 72 WsFrag frag[WS_MAX_FRAGS]; 73 int nfrag; 74 WsBar bar[WS_NBAR]; 75 uint32_t exited; 76 } WsWarp; 77 typedef struct { uintptr_t lo, hi; } WsRange; 78 79 typedef struct { 80 const BlackwellIRProgram *p; 81 uint32_t cbank[256]; /* constant bank 0 words (byte offset / 4) */ 82 uint32_t threads; /* per CTA, a multiple of 32, <= WS_MAX_THREADS */ 83 uint8_t *smem; /* per-CTA shared memory buffer (poisoned at CTA start) */ 84 uint32_t smem_bytes; 85 WsRange ranges[WS_MAX_RANGES]; int nranges; /* when > 0, every global access must fall in one */ 86 int order; /* 0 = lowest pc first, 1 = highest pc first */ 87 int trace; /* print every issue of warp 0 of CTA (0,0) */ 88 /* results */ 89 int err; int err_pc; char msg[200]; 90 uint64_t warp_issues; /* fragment-instruction issues (the chip's cost unit for divergence) */ 91 uint64_t lane_issues; /* lanes active over those issues */ 92 uint64_t splits; /* branches that split a fragment */ 93 uint64_t bar_split_arrivals; /* BAR.SYNC reached by a fragment that is not the whole live warp */ 94 int max_frags, peak_bars; 95 } WsSim; 96 97 static void ws_fail(WsSim *s, int err, int pc, const char *what) { 98 if (s->err) return; 99 s->err = err; s->err_pc = pc; 100 snprintf(s->msg, sizeof s->msg, "%s at pc %d: %s", ws_err_name(err), pc, what ? what : ""); 101 } 102 103 static 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]; } 104 static 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); } 105 static 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; } 106 static 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); } 107 /* sub-register operands (quad/pair lanes, BlackwellIRInsn.*_subreg) */ 108 static 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]; } 109 static 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; } 110 static float ws_u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } 111 static uint32_t ws_f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } 112 static uint32_t ws_lut3(uint32_t lut, uint32_t a, uint32_t b, uint32_t c) { 113 uint32_t r = 0; 114 for (int i = 0; i < 8; i++) if (lut >> i & 1) r |= ((i & 4) ? a : ~a) & ((i & 2) ? b : ~b) & ((i & 1) ? c : ~c); 115 return r; 116 } 117 static int ws_mem_ok(WsSim *s, uint64_t a, unsigned bytes, int pc) { 118 if (s->nranges == 0) return 1; 119 for (int i = 0; i < s->nranges; i++) if (a >= s->ranges[i].lo && a + bytes <= s->ranges[i].hi) return 1; 120 ws_fail(s, WS_E_MEM, pc, "global access outside the declared buffers"); return 0; 121 } 122 static uint32_t *ws_smem(WsSim *s, uint32_t a, int pc) { 123 if ((a & 3) || a + 4 > s->smem_bytes) { ws_fail(s, WS_E_SMEM, pc, "shared access past the declared size"); return NULL; } 124 return (uint32_t *)(s->smem + a); 125 } 126 static int ws_popc(uint32_t m) { return __builtin_popcount(m); } 127 128 /* One lane, one non-control instruction. */ 129 static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, uint32_t tid, uint32_t cx, uint32_t cy) { 130 const BlackwellIRProgram *p = s->p; 131 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); 132 switch (in->op) { 133 case BW_IR_NOP: break; 134 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; 135 case BW_IR_LDC: ws_wrs(l, in->dst_vreg, in->dst_subreg, s->cbank[(in->imm / 4) & 0xff], p); break; 136 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; 137 case BW_IR_LDCU64: case BW_IR_LDCU: break; /* memory descriptor: not modelled */ 138 case BW_IR_MOV_IMM: ws_wrs(l, in->dst_vreg, in->dst_subreg, in->imm, p); break; 139 case BW_IR_MOV_RZ: ws_wrs(l, in->dst_vreg, in->dst_subreg, 0, p); break; 140 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; 141 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; 142 case BW_IR_IMAD_HI_U32: ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b) >> 32) + c, p); break; 143 case BW_IR_IADD3: ws_wrs(l, in->dst_vreg, in->dst_subreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ 144 case BW_IR_SHF_R: ws_wrs(l, in->dst_vreg, in->dst_subreg, a >> (in->imm & 31), p); break; 145 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; 146 case BW_IR_LOP3_AND: ws_wrs(l, in->dst_vreg, in->dst_subreg, a & in->imm, p); break; 147 case BW_IR_LOP3_XOR: ws_wrs(l, in->dst_vreg, in->dst_subreg, a ^ b, p); break; 148 case BW_IR_LOP3_LUT: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_lut3(in->imm & 0xff, a, b, c), p); break; 149 case BW_IR_ISETP_GE_U32: l->p0 = a >= b; break; 150 case BW_IR_LDG_E: { 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; } 151 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; } 152 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; } 153 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; } 154 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; } 155 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; } 156 case BW_IR_STS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; *m = b; break; } 157 case BW_IR_FADD: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) + ws_u2f(b)), p); break; 158 case BW_IR_FSUB: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) - ws_u2f(b)), p); break; 159 case BW_IR_FMUL: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) * ws_u2f(b)), p); break; 160 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; 161 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; 162 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; 163 case BW_IR_MUFU_EX2: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(exp2f(ws_u2f(a))), p); break; 164 case BW_IR_MUFU_RCP: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(1.0f / ws_u2f(a)), p); break; 165 case BW_IR_MUFU_RSQ: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(1.0f / sqrtf(ws_u2f(a))), p); break; 166 default: ws_fail(s, WS_E_OP, pc, "opcode not modelled"); return; 167 } 168 } 169 170 static int ws_pick(const WsSim *s, const WsWarp *w) { 171 int best = -1; 172 for (int i = 0; i < w->nfrag; i++) { 173 if (w->frag[i].mask == 0 || w->frag[i].parked) continue; 174 if (best < 0) { best = i; continue; } 175 if (s->order == 0 ? w->frag[i].pc < w->frag[best].pc : w->frag[i].pc > w->frag[best].pc) best = i; 176 } 177 return best; 178 } 179 static void ws_drop_frag(WsWarp *w, int i) { w->frag[i] = w->frag[--w->nfrag]; } 180 static int ws_add_frag(WsSim *s, WsWarp *w, int pc, uint32_t mask) { 181 if (mask == 0) return 0; 182 if (w->nfrag >= WS_MAX_FRAGS) { ws_fail(s, WS_E_FRAGS, pc, "too many fragments"); return -1; } 183 w->frag[w->nfrag].pc = pc; w->frag[w->nfrag].mask = mask; w->frag[w->nfrag].parked = 0; w->nfrag++; 184 if (w->nfrag > s->max_frags) s->max_frags = w->nfrag; 185 return 0; 186 } 187 188 /* Runs one warp until it has no runnable fragment (finished, parked at BAR.SYNC, or stuck). 189 * Returns 1 when parked at BAR.SYNC, 0 when finished, -1 on error. */ 190 static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t cy, int trace) { 191 const BlackwellIRProgram *p = s->p; 192 const uint32_t live_all = 0xffffffffu; 193 for (uint64_t guard = 0;; guard++) { 194 if (s->err) return -1; 195 if (guard > 2000000000ull) { ws_fail(s, WS_E_STEPS, -1, "step budget"); return -1; } 196 int fi = ws_pick(s, w); 197 if (fi < 0) { 198 /* nothing runnable: parked at the CTA barrier, finished, or lanes waiting at a BSYNC 199 * that can never complete */ 200 for (int i = 0; i < w->nfrag; i++) if (w->frag[i].parked) return 1; 201 for (int b = 0; b < WS_NBAR; b++) if (w->bar[b].valid) { 202 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; } 203 ws_fail(s, WS_E_NEVER_JOINED, w->bar[b].target - 1, "warp finished with a barrier still armed"); return -1; 204 } 205 return 0; 206 } 207 WsFrag *f = &w->frag[fi]; 208 int pc = f->pc; 209 if (pc < 0 || (size_t)pc >= p->count) { ws_fail(s, WS_E_PC, pc, "pc outside the program"); return -1; } 210 const BlackwellIRInsn *in = &p->insns[pc]; 211 uint32_t mask = f->mask; 212 s->warp_issues++; s->lane_issues += (uint64_t)ws_popc(mask); 213 if (trace) printf("W%u pc=%d op=%d mask=%08x frags=%d\n", warp, pc, (int)in->op, mask, w->nfrag); 214 uint32_t live = live_all & ~w->exited; 215 uint32_t pmask = 0; 216 for (int L = 0; L < WS_WARP; L++) if ((mask >> L & 1) && w->lane[L].p0) pmask |= 1u << L; 217 uint32_t cond = in->predicate_p0 ? (in->predicate_not ? (mask & ~pmask) : (mask & pmask)) : mask; 218 switch (in->op) { 219 case BW_IR_EXIT: { 220 w->exited |= cond; 221 f->mask = mask & ~cond; 222 if (f->mask == 0) ws_drop_frag(w, fi); else f->pc = pc + 1; 223 break; 224 } 225 case BW_IR_BRA: { 226 int32_t delta = (int32_t)in->imm; 227 uint32_t taken = cond, fall = mask & ~cond; 228 if (taken && delta == 0) { ws_fail(s, WS_E_SELF_BRANCH, pc, "branch to itself"); return -1; } 229 if (taken && fall) s->splits++; 230 if (taken == 0) { f->pc = pc + 1; break; } 231 if (fall == 0) { f->pc = pc + delta; break; } 232 f->mask = fall; f->pc = pc + 1; 233 if (ws_add_frag(s, w, pc + delta, taken) < 0) return -1; 234 break; 235 } 236 case BW_IR_BSSY: { 237 WsBar *b = &w->bar[in->bar_reg & (WS_NBAR - 1)]; 238 if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } 239 if (b->valid) { ws_fail(s, WS_E_BSSY_REARM, pc, "BSSY on an armed barrier"); return -1; } 240 b->valid = 1; b->target = pc + (int32_t)in->imm; b->members = cond; b->arrived = 0; 241 int armed = 0; for (int k = 0; k < WS_NBAR; k++) armed += w->bar[k].valid; 242 if (armed > s->peak_bars) s->peak_bars = armed; 243 f->pc = pc + 1; 244 break; 245 } 246 case BW_IR_BSYNC: { 247 if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } 248 WsBar *b = &w->bar[in->bar_reg]; 249 if (!b->valid) { ws_fail(s, WS_E_BSYNC_UNARMED, pc, "BSYNC without a BSSY"); return -1; } 250 if (cond & ~b->members) { ws_fail(s, WS_E_NONMEMBER, pc, "a lane at BSYNC did not execute the BSSY"); return -1; } 251 b->arrived |= cond; 252 uint32_t rest = mask & ~cond; /* a predicated-off lane walks on */ 253 if (rest) { f->mask = rest; f->pc = pc + 1; } else ws_drop_frag(w, fi); 254 if (b->arrived == (b->members & ~w->exited)) { 255 if (b->target != pc + 1) { ws_fail(s, WS_E_JOIN_MISMATCH, pc, "BSSY named a different join point"); return -1; } 256 uint32_t joined = b->arrived; b->valid = 0; b->arrived = 0; b->members = 0; 257 if (ws_add_frag(s, w, pc + 1, joined) < 0) return -1; 258 } 259 break; 260 } 261 case BW_IR_SHFL_DOWN: { 262 if (w->exited) { ws_fail(s, WS_E_SHFL_EXITED, pc, "SHFL on a warp with exited lanes"); return -1; } 263 if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "SHFL on a split warp"); return -1; } 264 uint32_t src[WS_WARP]; 265 for (int L = 0; L < WS_WARP; L++) src[L] = ws_rd(&w->lane[L], in->src1_vreg, p); 266 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); } 267 f->pc = pc + 1; 268 break; 269 } 270 case BW_IR_BAR_SYNC: { 271 if (mask != live) s->bar_split_arrivals++; 272 f->parked = 1; /* released by ws_run when every live lane of the CTA has arrived */ 273 break; 274 } 275 default: { 276 for (int L = 0; L < WS_WARP; L++) if (mask >> L & 1) { 277 ws_lane_exec(s, &w->lane[L], in, pc, warp * WS_WARP + (uint32_t)L, cx, cy); 278 if (s->err) return -1; 279 } 280 f->pc = pc + 1; 281 break; 282 } 283 } 284 } 285 } 286 287 /* Runs every CTA of the grid. Returns s->err (0 = clean). */ 288 static int ws_run(WsSim *s, uint32_t grid_x, uint32_t grid_y) { 289 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; 290 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; } 291 static WsWarp warps[WS_MAX_WARPS]; 292 uint32_t nw = s->threads / WS_WARP; 293 for (uint32_t cy = 0; cy < grid_y && !s->err; cy++) for (uint32_t cx = 0; cx < grid_x && !s->err; cx++) { 294 memset(warps, 0, sizeof(WsWarp) * nw); 295 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); } 296 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; } 297 int trace = s->trace && cx == 0 && cy == 0; 298 /* Barrier phases: every warp runs until all of its fragments are parked at BAR.SYNC or it 299 * has finished (exited lanes do not hold the barrier up: INFERRED, see WS_E_BAR_SPLIT). 300 * Then every parked fragment is released and the next phase starts. */ 301 for (int phase = 0; ; phase++) { 302 int parked = 0; 303 for (uint32_t w = 0; w < nw && !s->err; w++) { 304 if (warps[w].nfrag == 0) continue; 305 int r = ws_run_warp(s, &warps[w], w, cx, cy, trace && w == 0); 306 if (r < 0) break; 307 if (r == 1) parked++; 308 } 309 if (s->err || parked == 0) break; /* error, or every warp finished */ 310 for (uint32_t w = 0; w < nw; w++) for (int i = 0; i < warps[w].nfrag; i++) 311 if (warps[w].frag[i].parked) { warps[w].frag[i].parked = 0; warps[w].frag[i].pc++; } 312 if (phase > 100000) { ws_fail(s, WS_E_BAR_TIMEOUT, -1, "barrier phases"); break; } 313 } 314 } 315 return s->err; 316 } 317 318 #endif /* BW_WARP_SIM_H */