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
CUDApp / tests / bw_warp_sim_tc.h
| 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_TC_H |
| 57 | #define BW_WARP_SIM_TC_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 | /* bw_warp_sim_tc.h (OWN-30B T3, 2026-10-13): bw_warp_sim_k.h plus (a) HMMA.16816.F32.BF16 as a warp-collective operation |
| 62 | * computed with the GB10 arithmetic model that matched 3 x 262144 chip outputs bit for bit (tests/bw_hmma_model.h, evidence |
| 63 | * OWN30B-T3-HMMA-MODEL), requiring the whole live warp (like SHFL), and (b) the STG.E immediate byte offset, which the |
| 64 | * encoder emits and bw_warp_sim_k.h ignored. Timing (stall counts, scoreboards) is not modelled. */ |
| 65 | |
| 66 | #include "omega_blackwell_codegen.h" |
| 67 | #include "bw_mufu_ftz.h" /* MUFU EX2 / RCP / RSQ: the chip's flush of subnormal inputs and results (omega #462) */ |
| 68 | #include "bw_hmma_model.h" |
| 69 | #include "omega_blackwell_codegen_tc.h" |
| 70 | #include "omega_blackwell_codegen_qmma.h" |
| 71 | #include "bw_qmma_model.h" |
| 72 | /* QMMA arithmetic used by the simulator (W-L1G): set by the test to the chip-measured model (gpu_qmma_probe_test) */ |
| 73 | static QmmaModel ws_qmma_model = { 32, 26, 0, 2, 0 }; /* chip-exact 262144/262144, evidence OWN30B-L1G-QMMA-PFGEMM-c742aa13 */ |
| 74 | #include <math.h> |
| 75 | #include <stdint.h> |
| 76 | #include <stdio.h> |
| 77 | #include <stdlib.h> |
| 78 | #include <string.h> |
| 79 | |
| 80 | #define WS_WARP 32 |
| 81 | #ifndef WS_MAX_THREADS |
| 82 | #define WS_MAX_THREADS 256 |
| 83 | #endif |
| 84 | #define WS_MAX_WARPS (WS_MAX_THREADS / WS_WARP) |
| 85 | #define WS_MAX_FRAGS 64 |
| 86 | #define WS_NBAR BW_RECONV_MAX_BAR |
| 87 | #define WS_MAX_RANGES 8 |
| 88 | |
| 89 | enum { |
| 90 | WS_OK = 0, WS_E_SHFL_SPLIT, WS_E_SHFL_EXITED, WS_E_BAR_SPLIT, WS_E_BSYNC_UNARMED, WS_E_BSSY_REARM, |
| 91 | WS_E_JOIN_MISMATCH, WS_E_NONMEMBER, WS_E_STUCK, WS_E_NEVER_JOINED, WS_E_SELF_BRANCH, WS_E_PC, |
| 92 | 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 |
| 93 | }; |
| 94 | static const char *ws_err_name(int e) { |
| 95 | static const char *const n[] = { "OK", "SHFL_SPLIT", "SHFL_EXITED", "BAR_SPLIT", "BSYNC_UNARMED", "BSSY_REARM", |
| 96 | "JOIN_MISMATCH", "NONMEMBER", "STUCK", "NEVER_JOINED", "SELF_BRANCH", "PC", "OP", "SHIFT", "MEM", "SMEM", |
| 97 | "FRAGS", "BAR_TIMEOUT", "STEPS", "IMADHI_RC" }; |
| 98 | return (e >= 0 && e < (int)(sizeof n / sizeof n[0])) ? n[e] : "?"; |
| 99 | } |
| 100 | |
| 101 | typedef struct { uint32_t r[256]; uint8_t p0; } WsLane; |
| 102 | typedef struct { int pc; uint32_t mask; int parked; /* waiting at a BAR.SYNC */ } WsFrag; |
| 103 | typedef struct { int valid; int target; uint32_t members, arrived; } WsBar; |
| 104 | typedef struct { |
| 105 | WsLane lane[WS_WARP]; |
| 106 | WsFrag frag[WS_MAX_FRAGS]; |
| 107 | int nfrag; |
| 108 | WsBar bar[WS_NBAR]; |
| 109 | uint32_t exited; |
| 110 | } WsWarp; |
| 111 | typedef struct { uintptr_t lo, hi; } WsRange; |
| 112 | |
| 113 | typedef struct { |
| 114 | const BlackwellIRProgram *p; |
| 115 | uint32_t cbank[256]; /* constant bank 0 words (byte offset / 4) */ |
| 116 | uint32_t threads; /* per CTA, a multiple of 32, <= WS_MAX_THREADS */ |
| 117 | uint8_t *smem; /* per-CTA shared memory buffer (poisoned at CTA start) */ |
| 118 | uint32_t smem_bytes; |
| 119 | WsRange ranges[WS_MAX_RANGES]; int nranges; /* when > 0, every global access must fall in one */ |
| 120 | int order; /* 0 = lowest pc first, 1 = highest pc first */ |
| 121 | int trace; /* print every issue of warp 0 of CTA (0,0) */ |
| 122 | /* results */ |
| 123 | int err; int err_pc; char msg[200]; |
| 124 | uint64_t warp_issues; /* fragment-instruction issues (the chip's cost unit for divergence) */ |
| 125 | uint64_t lane_issues; /* lanes active over those issues */ |
| 126 | uint64_t splits; /* branches that split a fragment */ |
| 127 | uint64_t bar_split_arrivals; /* BAR.SYNC reached by a fragment that is not the whole live warp */ |
| 128 | int max_frags, peak_bars; |
| 129 | } WsSim; |
| 130 | |
| 131 | static void ws_fail(WsSim *s, int err, int pc, const char *what) { |
| 132 | if (s->err) return; |
| 133 | s->err = err; s->err_pc = pc; |
| 134 | snprintf(s->msg, sizeof s->msg, "%s at pc %d: %s", ws_err_name(err), pc, what ? what : ""); |
| 135 | } |
| 136 | |
| 137 | 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]; } |
| 138 | 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); } |
| 139 | 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; } |
| 140 | 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); } |
| 141 | /* sub-register operands (quad/pair lanes, BlackwellIRInsn.*_subreg) */ |
| 142 | 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]; } |
| 143 | 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; } |
| 144 | static float ws_u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } |
| 145 | static uint32_t ws_f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } |
| 146 | /* IEEE fp16 bits -> f32 (exact: every fp16 value, subnormals included, is an f32) */ |
| 147 | static float ws_h2f(uint16_t h) { |
| 148 | const uint32_t s = (uint32_t)(h >> 15) << 31, e = (h >> 10) & 0x1fu, m = h & 0x3ffu; |
| 149 | if (e == 0x1fu) return ws_u2f(s | 0x7f800000u | m << 13); |
| 150 | if (e == 0) { float f = (float)m * 0x1p-24f; return s ? -f : f; } |
| 151 | return ws_u2f(s | (e + 112u) << 23 | m << 13); |
| 152 | } |
| 153 | static uint32_t ws_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 */ |
| 154 | /* MUFU EX2 / RCP / RSQ: the flush rule and its evidence live in bw_mufu_ftz.h (shared by every simulator, omega #462). */ |
| 155 | static uint32_t ws_lut3(uint32_t lut, uint32_t a, uint32_t b, uint32_t c) { |
| 156 | uint32_t r = 0; |
| 157 | for (int i = 0; i < 8; i++) if (lut >> i & 1) r |= ((i & 4) ? a : ~a) & ((i & 2) ? b : ~b) & ((i & 1) ? c : ~c); |
| 158 | return r; |
| 159 | } |
| 160 | static int ws_mem_ok(WsSim *s, uint64_t a, unsigned bytes, int pc) { |
| 161 | if (s->nranges == 0) return 1; |
| 162 | for (int i = 0; i < s->nranges; i++) if (a >= s->ranges[i].lo && a + bytes <= s->ranges[i].hi) return 1; |
| 163 | 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); } |
| 164 | ws_fail(s, WS_E_MEM, pc, "global access outside the declared buffers"); return 0; |
| 165 | } |
| 166 | static uint32_t *ws_smem(WsSim *s, uint32_t a, int pc) { |
| 167 | if ((a & 3) || a + 4 > s->smem_bytes) { ws_fail(s, WS_E_SMEM, pc, "shared access past the declared size"); return NULL; } |
| 168 | return (uint32_t *)(s->smem + a); |
| 169 | } |
| 170 | static int ws_popc(uint32_t m) { return __builtin_popcount(m); } |
| 171 | |
| 172 | /* One lane, one non-control instruction. */ |
| 173 | static void ws_lane_exec(WsSim *s, WsLane *l, const BlackwellIRInsn *in, int pc, uint32_t tid, uint32_t cx, uint32_t cy) { |
| 174 | const BlackwellIRProgram *p = s->p; |
| 175 | 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); |
| 176 | switch (in->op) { |
| 177 | case BW_IR_NOP: break; |
| 178 | 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; |
| 179 | case BW_IR_LDC: ws_wrs(l, in->dst_vreg, in->dst_subreg, s->cbank[(in->imm / 4) & 0xff], p); break; |
| 180 | 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; |
| 181 | case BW_IR_LDCU64: case BW_IR_LDCU: break; /* memory descriptor: not modelled */ |
| 182 | case BW_IR_MOV_IMM: ws_wrs(l, in->dst_vreg, in->dst_subreg, in->imm, p); break; |
| 183 | case BW_IR_MOV_RZ: ws_wrs(l, in->dst_vreg, in->dst_subreg, 0, p); break; |
| 184 | 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; |
| 185 | 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; |
| 186 | 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 |
| 187 | * oracle evidence/IMADHI-RC-ORACLE-nvcc13.0-20261016, not chip-probed); an odd Rc has no evidence: refused */ |
| 188 | uint64_t c64 = 0; |
| 189 | int rc = in->src3_vreg >= 0 ? p->regalloc.vreg_to_phys[in->src3_vreg] : -1; |
| 190 | if (rc >= 0) { |
| 191 | rc = (rc + in->src3_subreg) & 0xff; |
| 192 | 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; } |
| 193 | c64 = (uint64_t)l->r[rc] | ((uint64_t)l->r[rc + 1] << 32); |
| 194 | } |
| 195 | ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)(((uint64_t)a * b + c64) >> 32), p); break; } |
| 196 | case BW_IR_IADD3: ws_wrs(l, in->dst_vreg, in->dst_subreg, a + b, p); break; /* the encoder's IADD3 has Rc = RZ */ |
| 197 | case BW_IR_SHF_R: ws_wrs(l, in->dst_vreg, in->dst_subreg, a >> (in->imm & 31), p); break; |
| 198 | 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; |
| 199 | case BW_IR_LOP3_AND: ws_wrs(l, in->dst_vreg, in->dst_subreg, a & in->imm, p); break; |
| 200 | case BW_IR_LOP3_XOR: ws_wrs(l, in->dst_vreg, in->dst_subreg, a ^ b, p); break; |
| 201 | case BW_IR_LOP3_LUT: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_lut3(in->imm & 0xff, a, b, c), p); break; |
| 202 | case BW_IR_ISETP_GE: l->p0 = (int32_t)a >= (in->src2_vreg >= 0 ? (int32_t)b : (int32_t)in->imm); break; |
| 203 | case BW_IR_ISETP_GE_U32: l->p0 = a >= b; break; |
| 204 | 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; } |
| 205 | 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; } |
| 206 | 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; } |
| 207 | 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; } |
| 208 | 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; } |
| 209 | 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; } |
| 210 | case BW_IR_STS32: { uint32_t *m = ws_smem(s, a, pc); if (!m) return; *m = b; break; } |
| 211 | case BW_IR_FADD: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) + ws_u2f(b)), p); break; |
| 212 | case BW_IR_FSUB: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) - ws_u2f(b)), p); break; |
| 213 | case BW_IR_FMUL: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_u2f(a) * ws_u2f(b)), p); break; |
| 214 | 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; |
| 215 | case BW_IR_F2FP_BF16_PACK: ws_wrs(l, in->dst_vreg, in->dst_subreg, (ws_bf16_rn(a) << 16) | ws_bf16_rn(b), p); break; /* F2FP.BF16.F32.PACK_AB */ |
| 216 | case BW_IR_FMNMX_MAX: |
| 217 | if (in->imm == OMEGA_QMMA_MARK_F2E4) { ws_wrs(l, in->dst_vreg, in->dst_subreg, (uint32_t)qm_e4m3_rne_sat(ws_u2f(a)) << 8 | qm_e4m3_rne_sat(ws_u2f(b)), p); break; } /* F2FP.SATFINITE.E4M3 placeholder (NaN not modelled) */ |
| 218 | ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(fmaxf(ws_u2f(a), ws_u2f(b))), p); break; |
| 219 | 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; |
| 220 | case BW_IR_MUFU_EX2: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_ex2(ws_u2f(a))), p); break; |
| 221 | case BW_IR_MUFU_RCP: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_rcp(ws_u2f(a))), p); break; |
| 222 | case BW_IR_MUFU_RSQ: ws_wrs(l, in->dst_vreg, in->dst_subreg, ws_f2u(ws_mufu_rsq(ws_u2f(a))), p); break; |
| 223 | 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; } |
| 224 | 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; } |
| 225 | 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; } |
| 226 | /* f32 atomic add, flush-to-zero on both inputs and the result (PTX add.rn.ftz.f32); returns the old word */ |
| 227 | case BW_IR_ATOMG_ADD_F32_STRONG_SYS: case BW_IR_ATOMG_ADD_F32_STRONG_GPU: { 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 = ws_f2u(ws_mufu_ftz(ws_mufu_ftz(ws_u2f(old)) + ws_mufu_ftz(ws_u2f(b)))); memcpy((void *)(uintptr_t)ad, &nw, 4); ws_wrs(l, in->dst_vreg, in->dst_subreg, old, p); break; } |
| 228 | case BW_IR_MEMBAR_ALL_SYS: break; |
| 229 | default: ws_fail(s, WS_E_OP, pc, "opcode not modelled"); return; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static int ws_pick(const WsSim *s, const WsWarp *w) { |
| 234 | int best = -1; |
| 235 | for (int i = 0; i < w->nfrag; i++) { |
| 236 | if (w->frag[i].mask == 0 || w->frag[i].parked) continue; |
| 237 | if (best < 0) { best = i; continue; } |
| 238 | if (s->order == 0 ? w->frag[i].pc < w->frag[best].pc : w->frag[i].pc > w->frag[best].pc) best = i; |
| 239 | } |
| 240 | return best; |
| 241 | } |
| 242 | static void ws_drop_frag(WsWarp *w, int i) { w->frag[i] = w->frag[--w->nfrag]; } |
| 243 | static int ws_add_frag(WsSim *s, WsWarp *w, int pc, uint32_t mask) { |
| 244 | if (mask == 0) return 0; |
| 245 | if (w->nfrag >= WS_MAX_FRAGS) { ws_fail(s, WS_E_FRAGS, pc, "too many fragments"); return -1; } |
| 246 | w->frag[w->nfrag].pc = pc; w->frag[w->nfrag].mask = mask; w->frag[w->nfrag].parked = 0; w->nfrag++; |
| 247 | if (w->nfrag > s->max_frags) s->max_frags = w->nfrag; |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /* Runs one warp until it has no runnable fragment (finished, parked at BAR.SYNC, or stuck). |
| 252 | * Returns 1 when parked at BAR.SYNC, 0 when finished, -1 on error. */ |
| 253 | static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t cy, int trace) { |
| 254 | const BlackwellIRProgram *p = s->p; |
| 255 | const uint32_t live_all = 0xffffffffu; |
| 256 | for (uint64_t guard = 0;; guard++) { |
| 257 | if (s->err) return -1; |
| 258 | if (guard > 2000000000ull) { ws_fail(s, WS_E_STEPS, -1, "step budget"); return -1; } |
| 259 | int fi = ws_pick(s, w); |
| 260 | if (fi < 0) { |
| 261 | /* nothing runnable: parked at the CTA barrier, finished, or lanes waiting at a BSYNC |
| 262 | * that can never complete */ |
| 263 | for (int i = 0; i < w->nfrag; i++) if (w->frag[i].parked) return 1; |
| 264 | for (int b = 0; b < WS_NBAR; b++) if (w->bar[b].valid) { |
| 265 | 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; } |
| 266 | ws_fail(s, WS_E_NEVER_JOINED, w->bar[b].target - 1, "warp finished with a barrier still armed"); return -1; |
| 267 | } |
| 268 | return 0; |
| 269 | } |
| 270 | WsFrag *f = &w->frag[fi]; |
| 271 | int pc = f->pc; |
| 272 | if (pc < 0 || (size_t)pc >= p->count) { ws_fail(s, WS_E_PC, pc, "pc outside the program"); return -1; } |
| 273 | const BlackwellIRInsn *in = &p->insns[pc]; |
| 274 | uint32_t mask = f->mask; |
| 275 | s->warp_issues++; s->lane_issues += (uint64_t)ws_popc(mask); |
| 276 | if (trace) printf("W%u pc=%d op=%d mask=%08x frags=%d\n", warp, pc, (int)in->op, mask, w->nfrag); |
| 277 | uint32_t live = live_all & ~w->exited; |
| 278 | uint32_t pmask = 0; |
| 279 | for (int L = 0; L < WS_WARP; L++) if ((mask >> L & 1) && w->lane[L].p0) pmask |= 1u << L; |
| 280 | uint32_t cond = in->predicate_p0 ? (in->predicate_not ? (mask & ~pmask) : (mask & pmask)) : mask; |
| 281 | switch (in->op) { |
| 282 | case BW_IR_EXIT: { |
| 283 | w->exited |= cond; |
| 284 | f->mask = mask & ~cond; |
| 285 | if (f->mask == 0) ws_drop_frag(w, fi); else f->pc = pc + 1; |
| 286 | break; |
| 287 | } |
| 288 | case BW_IR_BRA: { |
| 289 | int32_t delta = (int32_t)in->imm; |
| 290 | uint32_t taken = cond, fall = mask & ~cond; |
| 291 | if (taken && delta == 0) { ws_fail(s, WS_E_SELF_BRANCH, pc, "branch to itself"); return -1; } |
| 292 | if (taken && fall) s->splits++; |
| 293 | if (taken == 0) { f->pc = pc + 1; break; } |
| 294 | if (fall == 0) { f->pc = pc + delta; break; } |
| 295 | f->mask = fall; f->pc = pc + 1; |
| 296 | if (ws_add_frag(s, w, pc + delta, taken) < 0) return -1; |
| 297 | break; |
| 298 | } |
| 299 | case BW_IR_BSSY: { |
| 300 | WsBar *b = &w->bar[in->bar_reg & (WS_NBAR - 1)]; |
| 301 | if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } |
| 302 | if (b->valid) { ws_fail(s, WS_E_BSSY_REARM, pc, "BSSY on an armed barrier"); return -1; } |
| 303 | b->valid = 1; b->target = pc + (int32_t)in->imm; b->members = cond; b->arrived = 0; |
| 304 | int armed = 0; for (int k = 0; k < WS_NBAR; k++) armed += w->bar[k].valid; |
| 305 | if (armed > s->peak_bars) s->peak_bars = armed; |
| 306 | f->pc = pc + 1; |
| 307 | break; |
| 308 | } |
| 309 | case BW_IR_BSYNC: { |
| 310 | if (in->bar_reg >= WS_NBAR) { ws_fail(s, WS_E_OP, pc, "barrier id"); return -1; } |
| 311 | WsBar *b = &w->bar[in->bar_reg]; |
| 312 | if (!b->valid) { ws_fail(s, WS_E_BSYNC_UNARMED, pc, "BSYNC without a BSSY"); return -1; } |
| 313 | if (cond & ~b->members) { ws_fail(s, WS_E_NONMEMBER, pc, "a lane at BSYNC did not execute the BSSY"); return -1; } |
| 314 | b->arrived |= cond; |
| 315 | uint32_t rest = mask & ~cond; /* a predicated-off lane walks on */ |
| 316 | if (rest) { f->mask = rest; f->pc = pc + 1; } else ws_drop_frag(w, fi); |
| 317 | if (b->arrived == (b->members & ~w->exited)) { |
| 318 | if (b->target != pc + 1) { ws_fail(s, WS_E_JOIN_MISMATCH, pc, "BSSY named a different join point"); return -1; } |
| 319 | uint32_t joined = b->arrived; b->valid = 0; b->arrived = 0; b->members = 0; |
| 320 | if (ws_add_frag(s, w, pc + 1, joined) < 0) return -1; |
| 321 | } |
| 322 | break; |
| 323 | } |
| 324 | case BW_IR_SHFL_DOWN: case BW_IR_SHFL_BFLY: { |
| 325 | if (w->exited) { ws_fail(s, WS_E_SHFL_EXITED, pc, "SHFL on a warp with exited lanes"); return -1; } |
| 326 | if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "SHFL on a split warp"); return -1; } |
| 327 | uint32_t src[WS_WARP]; |
| 328 | for (int L = 0; L < WS_WARP; L++) src[L] = ws_rd(&w->lane[L], in->src1_vreg, p); |
| 329 | for (int L = 0; L < WS_WARP; L++) { |
| 330 | int from = in->op == BW_IR_SHFL_BFLY ? (L ^ (int)(in->imm & 31)) : L + (int)(in->imm & 31); |
| 331 | ws_wr(&w->lane[L], in->dst_vreg, from < WS_WARP ? src[from] : src[L], p); |
| 332 | } |
| 333 | f->pc = pc + 1; |
| 334 | break; |
| 335 | } |
| 336 | case BW_IR_HMMA_F16: |
| 337 | case BW_IR_HMMA_BF16: { |
| 338 | if (w->exited) { ws_fail(s, WS_E_SHFL_EXITED, pc, "HMMA on a warp with exited lanes"); return -1; } |
| 339 | if (mask != live) { ws_fail(s, WS_E_SHFL_SPLIT, pc, "HMMA on a split warp"); return -1; } |
| 340 | if (in->imm == OMEGA_QMMA_MARK) { /* QMMA.16832.F32.E4M3.E4M3 (fragment layout: omega_blackwell_codegen_qmma.h) */ |
| 341 | static float QA[16][32], QB[32][8], QC[16][8]; |
| 342 | for (int L = 0; L < WS_WARP; L++) { |
| 343 | int gg = L / 4, tt = L % 4; const WsLane *ln = &w->lane[L]; |
| 344 | for (unsigned q = 0; q < 4; q++) { uint32_t av = ws_rds(ln, in->src1_vreg, in->src1_subreg + q, p); int r = gg + (q & 1u ? 8 : 0), k0 = 4 * tt + (q & 2u ? 16 : 0); |
| 345 | for (int j = 0; j < 4; j++) QA[r][k0 + j] = qm_e4m3((uint8_t)(av >> (8 * j))); } |
| 346 | for (unsigned q = 0; q < 2; q++) { uint32_t bv = ws_rds(ln, in->src2_vreg, in->src2_subreg + q, p); int k0 = 4 * tt + 16 * (int)q; |
| 347 | for (int j = 0; j < 4; j++) QB[k0 + j][gg] = qm_e4m3((uint8_t)(bv >> (8 * j))); } |
| 348 | for (unsigned q = 0; q < 4; q++) QC[gg + (q >= 2 ? 8 : 0)][2 * tt + (int)(q & 1u)] = in->src3_vreg >= 0 ? ws_u2f(ws_rds(ln, in->src3_vreg, in->src3_subreg + q, p)) : 0.0f; |
| 349 | } |
| 350 | for (int L = 0; L < WS_WARP; L++) { |
| 351 | int gg = L / 4, tt = L % 4; |
| 352 | for (unsigned q = 0; q < 4; q++) { int rr = gg + (q >= 2 ? 8 : 0), nn = 2 * tt + (int)(q & 1u); float av[32], bv[32]; |
| 353 | for (int k = 0; k < 32; k++) { av[k] = QA[rr][k]; bv[k] = QB[k][nn]; } |
| 354 | ws_wrs(&w->lane[L], in->dst_vreg, q, ws_f2u(qm_dot32(&ws_qmma_model, av, bv, QC[rr][nn])), p); } |
| 355 | } |
| 356 | f->pc = pc + 1; |
| 357 | break; |
| 358 | } |
| 359 | static const HmmaModel gb10 = { 16, 2, 0, 2, 0 }; |
| 360 | float A[16][16], B[16][8], C[16][8]; |
| 361 | for (int L = 0; L < WS_WARP; L++) { |
| 362 | int gg = L / 4, tt = L % 4; |
| 363 | const WsLane *ln = &w->lane[L]; |
| 364 | uint32_t a4[4], b2[2], c4[4]; |
| 365 | for (unsigned q = 0; q < 4; q++) { a4[q] = ws_rds(ln, in->src1_vreg, in->src1_subreg + q, p); c4[q] = in->src3_vreg >= 0 ? ws_rds(ln, in->src3_vreg, in->src3_subreg + q, p) : 0u; } |
| 366 | for (unsigned q = 0; q < 2; q++) b2[q] = ws_rds(ln, in->src2_vreg, in->src2_subreg + q, p); /* operands may start at a subregister (the encoder adds it) */ |
| 367 | const int ar[4] = { gg, gg + 8, gg, gg + 8 }, ac[4] = { 2 * tt, 2 * tt, 2 * tt + 8, 2 * tt + 8 }; |
| 368 | /* operand halves: bf16 (the high 16 bits of an f32) or IEEE fp16 (HMMA.16816.F32 without .BF16, GemmGeneral) */ |
| 369 | #define WS_HV(h) (in->op == BW_IR_HMMA_F16 ? ws_h2f((uint16_t)(h)) : ws_u2f((uint32_t)(h) << 16)) |
| 370 | for (unsigned q = 0; q < 4; q++) { A[ar[q]][ac[q]] = WS_HV(a4[q] & 0xffffu); A[ar[q]][ac[q] + 1] = WS_HV(a4[q] >> 16); } |
| 371 | for (unsigned q = 0; q < 2; q++) { B[2 * tt + 8 * q][gg] = WS_HV(b2[q] & 0xffffu); B[2 * tt + 8 * q + 1][gg] = WS_HV(b2[q] >> 16); } |
| 372 | #undef WS_HV |
| 373 | C[gg][2 * tt] = ws_u2f(c4[0]); C[gg][2 * tt + 1] = ws_u2f(c4[1]); C[gg + 8][2 * tt] = ws_u2f(c4[2]); C[gg + 8][2 * tt + 1] = ws_u2f(c4[3]); |
| 374 | } |
| 375 | for (int L = 0; L < WS_WARP; L++) { |
| 376 | int gg = L / 4, tt = L % 4; |
| 377 | for (unsigned q = 0; q < 4; q++) { |
| 378 | int rr = gg + (q >= 2 ? 8 : 0), nn = 2 * tt + (int)(q & 1u); |
| 379 | float av[16], bv[16]; for (int k = 0; k < 16; k++) { av[k] = A[rr][k]; bv[k] = B[k][nn]; } |
| 380 | ws_wrs(&w->lane[L], in->dst_vreg, q, ws_f2u(hm_dot16(&gb10, av, bv, C[rr][nn])), p); |
| 381 | } |
| 382 | } |
| 383 | f->pc = pc + 1; |
| 384 | break; |
| 385 | } |
| 386 | case BW_IR_BAR_SYNC: { |
| 387 | if (mask != live) s->bar_split_arrivals++; |
| 388 | f->parked = 1; /* released by ws_run when every live lane of the CTA has arrived */ |
| 389 | break; |
| 390 | } |
| 391 | default: { |
| 392 | for (int L = 0; L < WS_WARP; L++) if (mask >> L & 1) { |
| 393 | ws_lane_exec(s, &w->lane[L], in, pc, warp * WS_WARP + (uint32_t)L, cx, cy); |
| 394 | if (s->err) return -1; |
| 395 | } |
| 396 | f->pc = pc + 1; |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /* Runs every CTA of the grid. Returns s->err (0 = clean). */ |
| 404 | static int ws_run(WsSim *s, uint32_t grid_x, uint32_t grid_y) { |
| 405 | 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; |
| 406 | 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; } |
| 407 | static WsWarp warps[WS_MAX_WARPS]; |
| 408 | uint32_t nw = s->threads / WS_WARP; |
| 409 | for (uint32_t cy = 0; cy < grid_y && !s->err; cy++) for (uint32_t cx = 0; cx < grid_x && !s->err; cx++) { |
| 410 | memset(warps, 0, sizeof(WsWarp) * nw); |
| 411 | 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); } |
| 412 | 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; } |
| 413 | int trace = s->trace && cx == 0 && cy == 0; |
| 414 | /* Barrier phases: every warp runs until all of its fragments are parked at BAR.SYNC or it |
| 415 | * has finished (exited lanes do not hold the barrier up: INFERRED, see WS_E_BAR_SPLIT). |
| 416 | * Then every parked fragment is released and the next phase starts. */ |
| 417 | for (int phase = 0; ; phase++) { |
| 418 | int parked = 0; |
| 419 | for (uint32_t w = 0; w < nw && !s->err; w++) { |
| 420 | if (warps[w].nfrag == 0) continue; |
| 421 | int r = ws_run_warp(s, &warps[w], w, cx, cy, trace && w == 0); |
| 422 | if (r < 0) break; |
| 423 | if (r == 1) parked++; |
| 424 | } |
| 425 | if (s->err || parked == 0) break; /* error, or every warp finished */ |
| 426 | for (uint32_t w = 0; w < nw; w++) for (int i = 0; i < warps[w].nfrag; i++) |
| 427 | if (warps[w].frag[i].parked) { warps[w].frag[i].parked = 0; warps[w].frag[i].pc++; } |
| 428 | if (phase > 100000) { ws_fail(s, WS_E_BAR_TIMEOUT, -1, "barrier phases"); break; } |
| 429 | } |
| 430 | } |
| 431 | return s->err; |
| 432 | } |
| 433 | |
| 434 | #endif /* BW_WARP_SIM_TC_H */ |