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_k.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_k.h` 397 lines · 27830 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); 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 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) 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 } WsWarp; 102 typedef struct { uintptr_t lo, hi; } WsRange; 103 104 typedef 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 uint32_t quantum; /* 0 preserves ws_run; nonzero yields warp after this many issues (return 2) */ 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_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) */ 140 static uint32_t ws_f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } 141 static uint32_t ws_lut3(uint32_t lut, uint32_t a, uint32_t b, uint32_t c) { 142 uint32_t r = 0; 143 for (int i = 0; i < 8; i++) if (lut >> i & 1) r |= ((i & 4) ? a : ~a) & ((i & 2) ? b : ~b) & ((i & 1) ? c : ~c); 144 return r; 145 } 146 static int ws_mem_ok(WsSim *s, uint64_t a, unsigned bytes, int pc) { 147 if (s->nranges == 0) return 1; 148 for (int i = 0; i < s->nranges; i++) if (a >= s->ranges[i].lo && a + bytes <= s->ranges[i].hi) return 1; 149 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); } 150 ws_fail(s, WS_E_MEM, pc, "global access outside the declared buffers"); return 0; 151 } 152 static uint32_t *ws_smem(WsSim *s, uint32_t a, int pc) { 153 if ((a & 3) || a + 4 > s->smem_bytes) { ws_fail(s, WS_E_SMEM, pc, "shared access past the declared size"); return NULL; } 154 return (uint32_t *)(s->smem + a); 155 } 156 static int ws_popc(uint32_t m) { return __builtin_popcount(m); } 157 158 /* One lane, one non-control instruction. */ 159 static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, uint32_t tid, uint32_t cx, uint32_t cy) { 160 const BlackwellIRProgram *p = s->p; 161 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); 162 switch (in->op) { 163 case BW_IR_NOP: break; 164 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; 165 case BW_IR_LDC: ws_wrs(l, in->dst_vreg, in->dst_subreg, s->cbank[(in->imm / 4) & 0xff], p); break; 166 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; 167 case BW_IR_LDCU64: case BW_IR_LDCU: break; /* memory descriptor: not modelled */ 168 case BW_IR_MOV_IMM: ws_wrs(l, in->dst_vreg, in->dst_subreg, in->imm, p); break; 169 case BW_IR_MOV_RZ: ws_wrs(l, in->dst_vreg, in->dst_subreg, 0, p); break; 170 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; 171 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; 172 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 173 * oracle evidence/IMADHI-RC-ORACLE-nvcc13.0-`20261016`, not chip-probed); an odd Rc has no evidence: refused */ 174 uint64_t c64 = 0; 175 int rc = in->src3_vreg >= 0 ? p->regalloc.vreg_to_phys[in->src3_vreg] : -1; 176 if (rc >= 0) { 177 rc = (rc + in->src3_subreg) & 0xff; 178 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; } 179 c64 = (uint64_t)l->r[rc] | ((uint64_t)l->r[rc + 1] << 32); 180 } 181 ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b + c64) >> 32), p); break; } 182 case BW_IR_IADD3: ws_wrs(l, in->dst_vreg, in->dst_subreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ 183 case BW_IR_SHF_R: ws_wrs(l, in->dst_vreg, in->dst_subreg, a >> (in->imm & 31), p); break; 184 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; 185 case BW_IR_LOP3_AND: ws_wrs(l, in->dst_vreg, in->dst_subreg, a & in->imm, p); break; 186 case BW_IR_LOP3_XOR: ws_wrs(l, in->dst_vreg, in->dst_subreg, a ^ b, p); break; 187 case BW_IR_LOP3_LUT: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_lut3(in->imm & 0xff, a, b, c), p); break; 188 case BW_IR_ISETP_GE_U32: l->p0 = a >= b; break; 189 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; } 190 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; } 191 /* 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) */ 192 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; } 193 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; } 194 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; } 195 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; } 196 case BW_IR_STS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; *m = b; break; } 197 case BW_IR_FADD: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) + ws_u2f(b)), p); break; 198 case BW_IR_FSUB: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) - ws_u2f(b)), p); break; 199 case BW_IR_FMUL: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) * ws_u2f(b)), p); break; 200 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 */ 201 case BW_IR_I2FP_U32: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u((float)a), p); break; 202 case BW_IR_F2I_U32: { 203 float fa = ws_u2f(a); 204 uint32_t r = (fa != fa || fa <= 0.0f) ? 0u : fa >= `4294967296`.0f ? 0xffffffffu : (uint32_t)fa; 205 ws_wrs(l, in->dst_vreg, in->dst_subreg, r, p); 206 break; 207 } 208 /* The simulator uses the host half conversion, independently checked against 209 * the integer E1 references by gpu_dnn_half_test.c, including all half bits. */ 210 case BW_IR_F16_TO_F32: { 211 uint16_t raw=(uint16_t)a;_Float16 h;memcpy(&h,&raw,sizeof h); 212 ws_wrs(l,in->dst_vreg,in->dst_subreg,ws_f2u((float)h),p);break; 213 } 214 case BW_IR_F32_TO_F16: { 215 _Float16 h=(_Float16)ws_u2f(a);uint16_t raw;memcpy(&raw,&h,sizeof raw); 216 ws_wrs(l,in->dst_vreg,in->dst_subreg,raw,p);break; 217 } 218 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; 219 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) */ 220 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; 221 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; 222 case BW_IR_MUFU_EX2: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_ex2(ws_u2f(a))), p); break; 223 case BW_IR_MUFU_RCP: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_rcp(ws_u2f(a))), p); break; 224 case BW_IR_MUFU_RSQ: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_rsq(ws_u2f(a))), p); break; 225 case BW_IR_MUFU_LG2: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(log2f(ws_u2f(a))), p); break; 226 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; } 227 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; } 228 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; } 229 case BW_IR_ATOMG_EXCH_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); memcpy((void *)(uintptr_t)ad, &b, 4); ws_wrs(l, in->dst_vreg, in->dst_subreg, old, p); break; } 230 case BW_IR_MEMBAR_SC_SYS: break; 231 case BW_IR_MEMBAR_ALL_SYS: break; 232 default: ws_fail(s, WS_E_OP, pc, "opcode not modelled"); return; 233 } 234 } 235 236 static int ws_pick(const WsSim *s, const WsWarp *w) { 237 int best = -1; 238 for (int i = 0; i < w->nfrag; i++) { 239 if (w->frag[i].mask == 0 || w->frag[i].parked) continue; 240 if (best < 0) { best = i; continue; } 241 if (s->order == 0 ? w->frag[i].pc < w->frag[best].pc : w->frag[i].pc > w->frag[best].pc) best = i; 242 } 243 return best; 244 } 245 static void ws_drop_frag(WsWarp *w, int i) { w->frag[i] = w->frag[--w->nfrag]; } 246 static int ws_add_frag(WsSim *s, WsWarp *w, int pc, uint32_t mask) { 247 if (mask == 0) return 0; 248 if (w->nfrag >= WS_MAX_FRAGS) { ws_fail(s, WS_E_FRAGS, pc, "too many fragments"); return -1; } 249 w->frag[w->nfrag].pc = pc; w->frag[w->nfrag].mask = mask; w->frag[w->nfrag].parked = 0; w->nfrag++; 250 if (w->nfrag > s->max_frags) s->max_frags = w->nfrag; 251 return 0; 252 } 253 254 /* Runs one warp until it has no runnable fragment (finished, parked at BAR.SYNC, or stuck). 255 * Returns 1 when parked at BAR.SYNC, 0 when finished, -1 on error. */ 256 static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t cy, int trace) { 257 const BlackwellIRProgram *p = s->p; 258 const uint32_t live_all = 0xffffffffu; 259 for (uint64_t guard = 0;; guard++) { 260 if (s->quantum && guard >= s->quantum) return 2; 261 if (s->err) return -1; 262 if (guard > (s->step_budget ? s->step_budget : 2000000000ull)) { ws_fail(s, WS_E_STEPS, -1, "step budget"); return -1; } 263 int fi = ws_pick(s, w); 264 if (fi < 0) { 265 /* nothing runnable: parked at the CTA barrier, finished, or lanes waiting at a BSYNC 266 * that can never complete */ 267 for (int i = 0; i < w->nfrag; i++) if (w->frag[i].parked) return 1; 268 for (int b = 0; b < WS_NBAR; b++) if (w->bar[b].valid) { 269 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; } 270 ws_fail(s, WS_E_NEVER_JOINED, w->bar[b].target - 1, "warp finished with a barrier still armed"); return -1; 271 } 272 return 0; 273 } 274 WsFrag *f = &w->frag[fi]; 275 int pc = f->pc; 276 if (pc < 0 || (size_t)pc >= p->count) { ws_fail(s, WS_E_PC, pc, "pc outside the program"); return -1; } 277 const BlackwellIRInsn *in = &p->insns[pc]; 278 uint32_t mask = f->mask; 279 s->warp_issues++; s->lane_issues += (uint64_t)ws_popc(mask); 280 if (trace) printf("W%u pc=%d op=%d mask=%08x frags=%d\n", warp, pc, (int)in->op, mask, w->nfrag); 281 uint32_t live = live_all & ~w->exited; 282 uint32_t pmask = 0; 283 for (int L = 0; L < WS_WARP; L++) if ((mask >> L & 1) && w->lane[L].p0) pmask |= 1u << L; 284 uint32_t cond = in->predicate_p0 ? (in->predicate_not ? (mask & ~pmask) : (mask & pmask)) : mask; 285 switch (in->op) { 286 case BW_IR_EXIT: { 287 w->exited |= cond; 288 f->mask = mask & ~cond; 289 if (f->mask == 0) ws_drop_frag(w, fi); else f->pc = pc + 1; 290 break; 291 } 292 case BW_IR_BRA: { 293 int32_t delta = (int32_t)in->imm; 294 uint32_t taken = cond, fall = mask & ~cond; 295 if (taken && delta == 0) { ws_fail(s, WS_E_SELF_BRANCH, pc, "branch to itself"); return -1; } 296 if (taken && fall) s->splits++; 297 if (taken == 0) { f->pc = pc + 1; break; } 298 if (fall == 0) { f->pc = pc + delta; break; } 299 f->mask = fall; f->pc = pc + 1; 300 if (ws_add_frag(s, w, pc + delta, taken) < 0) return -1; 301 break; 302 } 303 case BW_IR_BSSY: { 304 WsBar *b = &w->bar[in->bar_reg & (WS_NBAR - 1)]; 305 if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } 306 if (b->valid) { ws_fail(s, WS_E_BSSY_REARM, pc, "BSSY on an armed barrier"); return -1; } 307 b->valid = 1; b->target = pc + (int32_t)in->imm; b->members = cond; b->arrived = 0; 308 int armed = 0; for (int k = 0; k < WS_NBAR; k++) armed += w->bar[k].valid; 309 if (armed > s->peak_bars) s->peak_bars = armed; 310 f->pc = pc + 1; 311 break; 312 } 313 case BW_IR_BSYNC: { 314 if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } 315 WsBar *b = &w->bar[in->bar_reg]; 316 if (!b->valid) { ws_fail(s, WS_E_BSYNC_UNARMED, pc, "BSYNC without a BSSY"); return -1; } 317 if (cond & ~b->members) { ws_fail(s, WS_E_NONMEMBER, pc, "a lane at BSYNC did not execute the BSSY"); return -1; } 318 b->arrived |= cond; 319 uint32_t rest = mask & ~cond; /* a predicated-off lane walks on */ 320 if (rest) { f->mask = rest; f->pc = pc + 1; } else ws_drop_frag(w, fi); 321 if (b->arrived == (b->members & ~w->exited)) { 322 if (b->target != pc + 1) { ws_fail(s, WS_E_JOIN_MISMATCH, pc, "BSSY named a different join point"); return -1; } 323 uint32_t joined = b->arrived; b->valid = 0; b->arrived = 0; b->members = 0; 324 if (ws_add_frag(s, w, pc + 1, joined) < 0) return -1; 325 } 326 break; 327 } 328 case BW_IR_SHFL_DOWN: case BW_IR_SHFL_BFLY: { 329 if (!cond) { f->pc = pc + 1; break; } 330 if (w->exited) { ws_fail(s, WS_E_SHFL_EXITED, pc, "SHFL on a warp with exited lanes"); return -1; } 331 if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "SHFL on a split warp"); return -1; } 332 uint32_t src[WS_WARP]; 333 for (int L = 0; L < WS_WARP; L++) src[L] = ws_rd(&w->lane[L], in->src1_vreg, p); 334 /* Guard predicates select destination lanes only. Snapshot every source lane 335 * before writes: a guard-inactive source still supplies its register value. 336 * Divergent fragments/exited source lanes retain the structural refusals above. */ 337 if (in->op == BW_IR_SHFL_BFLY) { for (int L = 0; L < WS_WARP; L++) if (cond >> L & 1) ws_wr(&w->lane[L], in->dst_vreg, src[L ^ (int)(in->imm & 31)], p); f->pc = pc + 1; break; } 338 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); } 339 f->pc = pc + 1; 340 break; 341 } 342 case BW_IR_BAR_SYNC: { 343 if (mask != live) s->bar_split_arrivals++; 344 f->parked = 1; /* released by ws_run when every live lane of the CTA has arrived */ 345 break; 346 } 347 default: { 348 for (int L = 0; L < WS_WARP; L++) if (cond >> L & 1) { 349 ws_lane_exec(s, &w->lane[L], in, pc, warp * WS_WARP + (uint32_t)L, cx, cy); 350 if (s->err) return -1; 351 } 352 f->pc = pc + 1; 353 break; 354 } 355 } 356 } 357 } 358 359 /* Runs every CTA of the grid. Returns s->err (0 = clean). */ 360 static int ws_run(WsSim *s, uint32_t grid_x, uint32_t grid_y) { 361 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; 362 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; } 363 static WsWarp warps[WS_MAX_WARPS]; 364 uint32_t nw = s->threads / WS_WARP; 365 for (uint32_t cy = 0; cy < grid_y && !s->err; cy++) for (uint32_t cx = 0; cx < grid_x && !s->err; cx++) { 366 memset(warps, 0, sizeof(WsWarp) * nw); 367 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); } 368 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; } 369 int trace = s->trace && cx == 0 && cy == 0; 370 /* Barrier phases: every warp runs until all of its fragments are parked at BAR.SYNC or it 371 * has finished (exited lanes do not hold the barrier up: INFERRED, see WS_E_BAR_SPLIT). 372 * Then every parked fragment is released and the next phase starts. */ 373 uint32_t rng = s->warp_sched; 374 for (int phase = 0; ; phase++) { 375 int parked = 0; 376 /* visit order: the chip gives no order between warps, so a slot or barrier mistake that the fixed order hides 377 * must show under the reversed or a shuffled one */ 378 uint32_t vis[WS_MAX_WARPS]; 379 for (uint32_t i = 0; i < nw; i++) vis[i] = s->warp_sched == 1 ? nw - 1u - i : i; 380 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; } 381 for (uint32_t vi = 0; vi < nw && !s->err; vi++) { 382 const uint32_t w = vis[vi]; 383 if (warps[w].nfrag == 0) continue; 384 int r = ws_run_warp(s, &warps[w], w, cx, cy, trace && w == 0); 385 if (r < 0) break; 386 if (r == 1) parked++; 387 } 388 if (s->err || parked == 0) break; /* error, or every warp finished */ 389 for (uint32_t w = 0; w < nw; w++) for (int i = 0; i < warps[w].nfrag; i++) 390 if (warps[w].frag[i].parked) { warps[w].frag[i].parked = 0; warps[w].frag[i].pc++; } 391 if (phase > 100000) { ws_fail(s, WS_E_BAR_TIMEOUT, -1, "barrier phases"); break; } 392 } 393 } 394 return s->err; 395 } 396 397 #endif /* BW_WARP_SIM_K_H */