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/CUDApp/blob/main/tests/bw_warp_sim.h # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / bw_warp_sim.h` 447 lines · 29125 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 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); LG2 is the host log2f; the chip's are 56 * approximate. I2FP.F32.S32 rounds to nearest even, as the host conversion does. STG.E adds its 57 * signed 24-bit byte offset (encoder w[1] bits 8..31, nvdisasm "[Ra.64+imm]"). A CTA of threads that 58 * is not a multiple of 32 ends in a partial warp whose missing lanes are never present (they do not 59 * count as live for SHFL or BSYNC). LDCU64 (memory descriptor) is not modelled. */ 60 #ifndef BW_WARP_SIM_H 61 #define BW_WARP_SIM_H 62 63 #include "omega_blackwell_codegen.h" 64 #include "bw_mufu_ftz.h" /* MUFU EX2 / RCP / RSQ: the chip's flush of subnormal inputs and results (omega #462) */ 65 #include 66 #include 67 #include 68 #include 69 #include 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 - 1) / WS_WARP) 76 #define WS_MAX_FRAGS 64 77 #define WS_NBAR BW_RECONV_MAX_BAR 78 #define WS_MAX_RANGES 8 79 80 enum { 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 }; 85 static 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 92 typedef struct { uint32_t r[256]; uint8_t p0; } WsLane; 93 typedef struct { int pc; uint32_t mask; int parked; /* waiting at a BAR.SYNC */ } WsFrag; 94 typedef struct { int valid; int target; uint32_t members, arrived; } WsBar; 95 typedef 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 uint32_t present; /* lanes that exist: all 32, or fewer in the last warp of a partial CTA */ 102 } WsWarp; 103 typedef struct { uintptr_t lo, hi; } WsRange; 104 105 typedef struct { 106 const BlackwellIRProgram *p; 107 uint32_t cbank[256]; /* constant bank 0 words (byte offset / 4) */ 108 uint32_t threads; /* per CTA, 1..WS_MAX_THREADS (a partial last warp when not a multiple of 32) */ 109 uint8_t *smem; /* per-CTA shared memory buffer (poisoned at CTA start) */ 110 uint32_t smem_bytes; 111 WsRange ranges[WS_MAX_RANGES]; int nranges; /* when > 0, every global access must fall in one */ 112 int order; /* 0 = lowest pc first, 1 = highest pc first */ 113 uint32_t warp_sched; /* warp visit order per barrier phase: 0 ascending, 1 descending, else a seed for a fresh shuffle each phase 114 * (the chip gives no order between warps; port of omega#490 bw_warp_sim_k.h) */ 115 int trace; /* print every issue of warp 0 of CTA (0,0) */ 116 /* results */ 117 int err; int err_pc; char msg[200]; 118 uint64_t warp_issues; /* fragment-instruction issues (the chip's cost unit for divergence) */ 119 uint64_t lane_issues; /* lanes active over those issues */ 120 uint64_t splits; /* branches that split a fragment */ 121 uint64_t bar_split_arrivals; /* BAR.SYNC reached by a fragment that is not the whole live warp */ 122 int max_frags, peak_bars; 123 } WsSim; 124 125 static void ws_fail(WsSim *s, int err, int pc, const char *what) { 126 if (s->err) return; 127 s->err = err; s->err_pc = pc; 128 snprintf(s->msg, sizeof s->msg, "%s at pc %d: %s", ws_err_name(err), pc, what ? what : ""); 129 } 130 131 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]; } 132 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); } 133 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; } 134 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); } 135 /* sub-register operands (quad/pair lanes, BlackwellIRInsn.*_subreg) */ 136 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]; } 137 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; } 138 static float ws_u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } 139 static uint32_t ws_f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } 140 static double ws_drd(const WsLane *l, int v, unsigned sub, const BlackwellIRProgram *p) { 141 uint64_t u=(uint64_t)ws_rds(l,v,sub,p)|((uint64_t)ws_rds(l,v,sub+1,p)<<32); 142 double d;memcpy(&d,&u,sizeof d);return d; 143 } 144 static void ws_dwr(WsLane *l,int v,unsigned sub,double d,const BlackwellIRProgram *p) { 145 uint64_t u;memcpy(&u,&d,sizeof u);ws_wrs(l,v,sub,(uint32_t)u,p);ws_wrs(l,v,sub+1,(uint32_t)(u>>32),p); 146 } 147 /* PTX ISA rcp.approx.ftz.f64 / rsqrt.approx.ftz.f64 specifies a 1.11.20 148 * high-word input/output, ignored low input and sign-preserving flush. SASS 149 * MUFU.*64H writes ONLY that high word; the caller clears the low word. 150 * https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#floating-point-instructions-rcp-approx-ftz-f64 151 * https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#floating-point-instructions-rsqrt-approx-ftz-f64 152 * NVIDIA publishes no normal-input error bound there. This simulator models 153 * high-word truncation of a host double reciprocal/rsqrt, not the undisclosed 154 * hardware approximation table. Its special values and word effects are exact. */ 155 static uint32_t ws_mufu64h(uint32_t hi,int rsqrt) { 156 unsigned exp=(hi>>20)&2047u;uint32_t sign=hi&0x80000000u; 157 if(!exp)return sign|0x7ff00000u; 158 if(exp==2047u){ 159 if(hi&0xfffffu)return 0x7fffffffu; 160 return rsqrt&&sign?0x7fffffffu:sign; 161 } 162 if(rsqrt&&sign)return 0x7fffffffu; 163 uint64_t u=(uint64_t)hi<<32;double d;memcpy(&d,&u,sizeof d); 164 d=rsqrt?1.0/sqrt(d):1.0/d;memcpy(&u,&d,sizeof u); 165 uint32_t out=(uint32_t)(u>>32); 166 return (out&0x7ff00000u)?out:(out&0x80000000u); 167 } 168 static uint32_t ws_lut3(uint32_t lut, uint32_t a, uint32_t b, uint32_t c) { 169 uint32_t r = 0; 170 for (int i = 0; i < 8; i++) if (lut >> i & 1) r |= ((i & 4) ? a : ~a) & ((i & 2) ? b : ~b) & ((i & 1) ? c : ~c); 171 return r; 172 } 173 static int ws_mem_ok(WsSim *s, uint64_t a, unsigned bytes, int pc) { 174 if (s->nranges == 0) return 1; 175 for (int i = 0; i < s->nranges; i++) if (a >= s->ranges[i].lo && a + bytes <= s->ranges[i].hi) return 1; 176 ws_fail(s, WS_E_MEM, pc, "global access outside the declared buffers"); return 0; 177 } 178 static uint32_t *ws_smem(WsSim *s, uint32_t a, int pc) { 179 if ((a & 3) || a + 4 > s->smem_bytes) { ws_fail(s, WS_E_SMEM, pc, "shared access past the declared size"); return NULL; } 180 return (uint32_t *)(s->smem + a); 181 } 182 static int ws_popc(uint32_t m) { return __builtin_popcount(m); } 183 184 /* One lane, one non-control instruction. */ 185 static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, uint32_t tid, uint32_t cx, uint32_t cy) { 186 const BlackwellIRProgram *p = s->p; 187 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); 188 switch (in->op) { 189 case BW_IR_NOP: break; 190 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; 191 case BW_IR_LDC: ws_wrs(l, in->dst_vreg, in->dst_subreg, s->cbank[(in->imm / 4) & 0xff], p); break; 192 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; 193 case BW_IR_LDCU64: case BW_IR_LDCU: break; /* memory descriptor: not modelled */ 194 case BW_IR_MOV_IMM: ws_wrs(l, in->dst_vreg, in->dst_subreg, in->imm, p); break; 195 case BW_IR_MOV_RZ: ws_wrs(l, in->dst_vreg, in->dst_subreg, 0, p); break; 196 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; 197 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; 198 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 199 * oracle evidence/IMADHI-RC-ORACLE-nvcc13.0-`20261016`, not chip-probed); an odd Rc has no evidence: refused */ 200 uint64_t c64 = 0; 201 int rc = in->src3_vreg >= 0 ? p->regalloc.vreg_to_phys[in->src3_vreg] : -1; 202 if (rc >= 0) { 203 rc = (rc + in->src3_subreg) & 0xff; 204 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; } 205 c64 = (uint64_t)l->r[rc] | ((uint64_t)l->r[rc + 1] << 32); 206 } 207 ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b + c64) >> 32), p); break; } 208 case BW_IR_IADD3: ws_wrs(l, in->dst_vreg, in->dst_subreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ 209 case BW_IR_SHF_R: ws_wrs(l, in->dst_vreg, in->dst_subreg, a >> (in->imm & 31), p); break; 210 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; 211 case BW_IR_LOP3_AND: ws_wrs(l, in->dst_vreg, in->dst_subreg, a & in->imm, p); break; 212 case BW_IR_LOP3_XOR: ws_wrs(l, in->dst_vreg, in->dst_subreg, a ^ b, p); break; 213 case BW_IR_LOP3_LUT: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_lut3(in->imm & 0xff, a, b, c), p); break; 214 case BW_IR_ISETP_GE_U32: l->p0 = a >= b; break; 215 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; } 216 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; } 217 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; } 218 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; } 219 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; } 220 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; } 221 case BW_IR_STS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; *m = b; break; } 222 case BW_IR_FADD: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) + ws_u2f(b)), p); break; 223 case BW_IR_FSUB: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) - ws_u2f(b)), p); break; 224 case BW_IR_FMUL: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) * ws_u2f(b)), p); break; 225 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; 226 case BW_IR_DADD: case BW_IR_DMUL: case BW_IR_DFMA: { 227 double da=ws_drd(l,in->src1_vreg,in->src1_subreg,p),db=ws_drd(l,in->src2_vreg,in->src2_subreg,p); 228 double dc=ws_drd(l,in->src3_vreg,in->src3_subreg,p); 229 double d=in->op==BW_IR_DADD?da+db:in->op==BW_IR_DMUL?da*db:fma(da,db,dc); 230 ws_dwr(l,in->dst_vreg,in->dst_subreg,d,p);break; 231 } 232 case BW_IR_DSETP: { 233 if(in->dst_vreg>=0){ws_fail(s,WS_E_OP,pc,"DSETP into a predicate other than P0");return;} 234 double da=ws_drd(l,in->src1_vreg,in->src1_subreg,p),db=ws_drd(l,in->src2_vreg,in->src2_subreg,p); 235 unsigned mask=((in->imm?in->imm:0x03f06000u)>>12)&15u; 236 unsigned rel=(isnan(da)||isnan(db))?8u:dadb?4u:2u; 237 l->p0=!!(mask&rel);break; 238 } 239 case BW_IR_MUFU_RCP64H: case BW_IR_MUFU_RSQ64H: 240 ws_wrs(l,in->dst_vreg,in->dst_subreg,ws_mufu64h(a,in->op==BW_IR_MUFU_RSQ64H),p);break; 241 case BW_IR_F64_F32: case BW_IR_F64_S32: case BW_IR_F64_U32: { 242 double d=in->op==BW_IR_F64_F32?(double)ws_u2f(a):in->op==BW_IR_F64_S32?(double)(int32_t)a:(double)a; 243 ws_dwr(l,in->dst_vreg,in->dst_subreg,d,p);break; 244 } 245 case BW_IR_F32_F64: 246 ws_wrs(l,in->dst_vreg,in->dst_subreg,ws_f2u((float)ws_drd(l,in->src1_vreg,in->src1_subreg,p)),p);break; 247 case BW_IR_S32_F64: { 248 double d=ws_drd(l,in->src1_vreg,in->src1_subreg,p); 249 /* PTX cvt: f64 NaN -> MININT; out-of-range results saturate. */ 250 int32_t r=isnan(d)?INT32_MIN:d>=INT32_MAX?INT32_MAX:d<=INT32_MIN?INT32_MIN:(int32_t)d; 251 ws_wrs(l,in->dst_vreg,in->dst_subreg,(uint32_t)r,p);break; 252 } 253 case BW_IR_I2FP: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u((float)(int32_t)a), p); break; 254 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; 255 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; 256 case BW_IR_MUFU_EX2: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_ex2(ws_u2f(a))), p); break; 257 case BW_IR_MUFU_RCP: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_rcp(ws_u2f(a))), p); break; 258 case BW_IR_MUFU_RSQ: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_rsq(ws_u2f(a))), p); break; 259 case BW_IR_MUFU_LG2: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(log2f(ws_u2f(a))), p); break; 260 case BW_IR_FSETP: { 261 if (in->dst_vreg >= 0) { ws_fail(s, WS_E_OP, pc, "FSETP into a predicate other than P0"); return; } 262 unsigned code = ((in->imm ? in->imm : 0x03f06000u) >> 12) & 0xfu; 263 float fa = ws_u2f(a), fb = ws_u2f(b); 264 unsigned o = (fa != fa || fb != fb) ? 8u : fa < fb ? 1u : fa > fb ? 4u : 2u; 265 l->p0 = (code & o) != 0; 266 break; 267 } 268 case BW_IR_I2FP_U32: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u((float)a), p); break; 269 case BW_IR_F2I_U32: { 270 float fa = ws_u2f(a); 271 uint32_t r = (fa != fa || fa <= 0.0f) ? 0u : fa >= `4294967296`.0f ? 0xffffffffu : (uint32_t)fa; 272 ws_wrs(l, in->dst_vreg, in->dst_subreg, r, p); 273 break; 274 } 275 /* Independent integer E1 references check these host half conversions. */ 276 case BW_IR_F16_TO_F32: { 277 uint16_t raw=(uint16_t)a;_Float16 h;memcpy(&h,&raw,sizeof h); 278 ws_wrs(l,in->dst_vreg,in->dst_subreg,ws_f2u((float)h),p);break; 279 } 280 case BW_IR_F32_TO_F16: { 281 _Float16 h=(_Float16)ws_u2f(a);uint16_t raw;memcpy(&raw,&h,sizeof raw); 282 ws_wrs(l,in->dst_vreg,in->dst_subreg,raw,p);break; 283 } 284 default: ws_fail(s, WS_E_OP, pc, "opcode not modelled"); return; 285 } 286 } 287 288 static int ws_pick(const WsSim *s, const WsWarp *w) { 289 int best = -1; 290 for (int i = 0; i < w->nfrag; i++) { 291 if (w->frag[i].mask == 0 || w->frag[i].parked) continue; 292 if (best < 0) { best = i; continue; } 293 if (s->order == 0 ? w->frag[i].pc < w->frag[best].pc : w->frag[i].pc > w->frag[best].pc) best = i; 294 } 295 return best; 296 } 297 static void ws_drop_frag(WsWarp *w, int i) { w->frag[i] = w->frag[--w->nfrag]; } 298 static int ws_add_frag(WsSim *s, WsWarp *w, int pc, uint32_t mask) { 299 if (mask == 0) return 0; 300 if (w->nfrag >= WS_MAX_FRAGS) { ws_fail(s, WS_E_FRAGS, pc, "too many fragments"); return -1; } 301 w->frag[w->nfrag].pc = pc; w->frag[w->nfrag].mask = mask; w->frag[w->nfrag].parked = 0; w->nfrag++; 302 if (w->nfrag > s->max_frags) s->max_frags = w->nfrag; 303 return 0; 304 } 305 306 /* Runs one warp until it has no runnable fragment (finished, parked at BAR.SYNC, or stuck). 307 * Returns 1 when parked at BAR.SYNC, 0 when finished, -1 on error. */ 308 static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t cy, int trace) { 309 const BlackwellIRProgram *p = s->p; 310 const uint32_t live_all = w->present; 311 for (uint64_t guard = 0;; guard++) { 312 if (s->err) return -1; 313 if (guard > 2000000000ull) { ws_fail(s, WS_E_STEPS, -1, "step budget"); return -1; } 314 int fi = ws_pick(s, w); 315 if (fi < 0) { 316 /* nothing runnable: parked at the CTA barrier, finished, or lanes waiting at a BSYNC 317 * that can never complete */ 318 for (int i = 0; i < w->nfrag; i++) if (w->frag[i].parked) return 1; 319 for (int b = 0; b < WS_NBAR; b++) if (w->bar[b].valid) { 320 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; } 321 ws_fail(s, WS_E_NEVER_JOINED, w->bar[b].target - 1, "warp finished with a barrier still armed"); return -1; 322 } 323 return 0; 324 } 325 WsFrag *f = &w->frag[fi]; 326 int pc = f->pc; 327 if (pc < 0 || (size_t)pc >= p->count) { ws_fail(s, WS_E_PC, pc, "pc outside the program"); return -1; } 328 const BlackwellIRInsn *in = &p->insns[pc]; 329 uint32_t mask = f->mask; 330 s->warp_issues++; s->lane_issues += (uint64_t)ws_popc(mask); 331 if (trace) printf("W%u pc=%d op=%d mask=%08x frags=%d\n", warp, pc, (int)in->op, mask, w->nfrag); 332 uint32_t live = live_all & ~w->exited; 333 uint32_t pmask = 0; 334 for (int L = 0; L < WS_WARP; L++) if ((mask >> L & 1) && w->lane[L].p0) pmask |= 1u << L; 335 uint32_t cond = in->predicate_p0 ? (in->predicate_not ? (mask & ~pmask) : (mask & pmask)) : mask; 336 switch (in->op) { 337 case BW_IR_EXIT: { 338 w->exited |= cond; 339 f->mask = mask & ~cond; 340 if (f->mask == 0) ws_drop_frag(w, fi); else f->pc = pc + 1; 341 break; 342 } 343 case BW_IR_BRA: { 344 int32_t delta = (int32_t)in->imm; 345 uint32_t taken = cond, fall = mask & ~cond; 346 if (taken && delta == 0) { ws_fail(s, WS_E_SELF_BRANCH, pc, "branch to itself"); return -1; } 347 if (taken && fall) s->splits++; 348 if (taken == 0) { f->pc = pc + 1; break; } 349 if (fall == 0) { f->pc = pc + delta; break; } 350 f->mask = fall; f->pc = pc + 1; 351 if (ws_add_frag(s, w, pc + delta, taken) < 0) return -1; 352 break; 353 } 354 case BW_IR_BSSY: { 355 WsBar *b = &w->bar[in->bar_reg & (WS_NBAR - 1)]; 356 if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } 357 if (b->valid) { ws_fail(s, WS_E_BSSY_REARM, pc, "BSSY on an armed barrier"); return -1; } 358 b->valid = 1; b->target = pc + (int32_t)in->imm; b->members = cond; b->arrived = 0; 359 int armed = 0; for (int k = 0; k < WS_NBAR; k++) armed += w->bar[k].valid; 360 if (armed > s->peak_bars) s->peak_bars = armed; 361 f->pc = pc + 1; 362 break; 363 } 364 case BW_IR_BSYNC: { 365 if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } 366 WsBar *b = &w->bar[in->bar_reg]; 367 if (!b->valid) { ws_fail(s, WS_E_BSYNC_UNARMED, pc, "BSYNC without a BSSY"); return -1; } 368 if (cond & ~b->members) { ws_fail(s, WS_E_NONMEMBER, pc, "a lane at BSYNC did not execute the BSSY"); return -1; } 369 b->arrived |= cond; 370 uint32_t rest = mask & ~cond; /* a predicated-off lane walks on */ 371 if (rest) { f->mask = rest; f->pc = pc + 1; } else ws_drop_frag(w, fi); 372 if (b->arrived == (b->members & ~w->exited)) { 373 if (b->target != pc + 1) { ws_fail(s, WS_E_JOIN_MISMATCH, pc, "BSSY named a different join point"); return -1; } 374 uint32_t joined = b->arrived; b->valid = 0; b->arrived = 0; b->members = 0; 375 if (ws_add_frag(s, w, pc + 1, joined) < 0) return -1; 376 } 377 break; 378 } 379 case BW_IR_SHFL_DOWN: { 380 /* Guard predicates suppress destination execution, not source 381 * register availability: guarded-off lanes still supply SHFL data. 382 * An entirely guarded-off SHFL performs no structural checks. */ 383 if (!cond) { f->pc = pc + 1; break; } 384 if (w->exited) { ws_fail(s, WS_E_SHFL_EXITED, pc, "SHFL on a warp with exited lanes"); return -1; } 385 if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "SHFL on a split warp"); return -1; } 386 uint32_t src[WS_WARP]; 387 for (int L = 0; L < WS_WARP; L++) src[L] = ws_rd(&w->lane[L], in->src1_vreg, p); 388 for (int L = 0; L < WS_WARP; L++) if (cond >> L & 1) { int from = L + (int)(in->imm & 31); ws_wr(&w->lane[L], in->dst_vreg, from < WS_WARP ? src[from] : src[L], p); } 389 f->pc = pc + 1; 390 break; 391 } 392 case BW_IR_BAR_SYNC: { 393 if (mask != live) s->bar_split_arrivals++; 394 f->parked = 1; /* released by ws_run when every live lane of the CTA has arrived */ 395 break; 396 } 397 default: { 398 for (int L = 0; L < WS_WARP; L++) if (cond >> L & 1) { 399 ws_lane_exec(s, &w->lane[L], in, pc, warp * WS_WARP + (uint32_t)L, cx, cy); 400 if (s->err) return -1; 401 } 402 f->pc = pc + 1; 403 break; 404 } 405 } 406 } 407 } 408 409 /* Runs every CTA of the grid. Returns s->err (0 = clean). */ 410 static int ws_run(WsSim *s, uint32_t grid_x, uint32_t grid_y) { 411 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; 412 if (!s->p || s->threads == 0 || s->threads > WS_MAX_THREADS) { ws_fail(s, WS_E_OP, -1, "bad simulator configuration"); return s->err; } 413 static WsWarp warps[WS_MAX_WARPS]; 414 uint32_t nw = (s->threads + WS_WARP - 1) / WS_WARP; 415 uint32_t tail = s->threads % WS_WARP ? (1u << (s->threads % WS_WARP)) - 1u : 0xffffffffu; 416 for (uint32_t cy = 0; cy < grid_y && !s->err; cy++) for (uint32_t cx = 0; cx < grid_x && !s->err; cx++) { 417 memset(warps, 0, sizeof(WsWarp) * nw); 418 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); } 419 for (uint32_t w = 0; w < nw; w++) { warps[w].present = w + 1 == nw ? tail : 0xffffffffu; warps[w].nfrag = 1; warps[w].frag[0].pc = 0; warps[w].frag[0].mask = warps[w].present; warps[w].frag[0].parked = 0; } 420 int trace = s->trace && cx == 0 && cy == 0; 421 /* Barrier phases: every warp runs until all of its fragments are parked at BAR.SYNC or it 422 * has finished (exited lanes do not hold the barrier up: INFERRED, see WS_E_BAR_SPLIT). 423 * Then every parked fragment is released and the next phase starts. */ 424 uint32_t rng = s->warp_sched; 425 for (int phase = 0; ; phase++) { 426 int parked = 0; 427 /* visit order: a slot or barrier mistake that the ascending order hides must show under the reversed or a shuffled one */ 428 uint32_t vis[WS_MAX_WARPS]; 429 for (uint32_t i = 0; i < nw; i++) vis[i] = s->warp_sched == 1 ? nw - 1u - i : i; 430 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; } 431 for (uint32_t vi = 0; vi < nw && !s->err; vi++) { 432 const uint32_t w = vis[vi]; 433 if (warps[w].nfrag == 0) continue; 434 int r = ws_run_warp(s, &warps[w], w, cx, cy, trace && w == 0); 435 if (r < 0) break; 436 if (r == 1) parked++; 437 } 438 if (s->err || parked == 0) break; /* error, or every warp finished */ 439 for (uint32_t w = 0; w < nw; w++) for (int i = 0; i < warps[w].nfrag; i++) 440 if (warps[w].frag[i].parked) { warps[w].frag[i].parked = 0; warps[w].frag[i].pc++; } 441 if (phase > 100000) { ws_fail(s, WS_E_BAR_TIMEOUT, -1, "barrier phases"); break; } 442 } 443 } 444 return s->err; 445 } 446 447 #endif /* BW_WARP_SIM_H */