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/gpu_latmeas_test.c # 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 / gpu_latmeas_test.c` 2066 lines · 84544 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* gpu_latmeas_test: dependent-chain latency kernels for GB10 (sm_121). 3 * 4 * The chip path is tools/latmeas/run_spark.sh. It encodes every kernel before 5 * taking the shared lock, then launches them. This file does not invent a 6 * measured latency: --self-test and --emit never open a device, and a launch 7 * writes only numbers the chip returned. 8 * 9 * Clock. NAK reads the shader clock with CS2R on SM >= 50, not S2R 10 * (nak/from_nir.rs, SM >= 50 branch on NAK_SV_CLOCK). nak_private.h names 11 * NAK_SV_CLOCK_LO = 0x50 and NAK_SV_CLOCK_HI = 0x51. sm70_encode.rs encodes 12 * OpCS2R as opcode 0x805, destination at bits 16..24, index at bits 72..80, 13 * and bit 80 set for a 64-bit read. A 32-bit CS2R is Dualalu (fixed) in 14 * sm120_instr_latencies.rs. The encoder has S2R and not CS2R, so each clock 15 * site is emitted as S2R immediate 0x50 and the low 12 bits are rewritten to 16 * 0x805. The index byte is left as 0x50. Two such sites bracket the chain. 17 * Mesa commit `01bab9302a6bf0a082c7ba8c49d74427cf10b0cf`. 18 * 19 * What is compared to NAK. Fixed-latency producers do not scoreboard a GPR 20 * result (src/omega_numeric.c): the stall field is the wait. Each chain is 21 * launched once per stall. The result has to match the host oracle (or, for 22 * HMMA, the stall-36 result of the same kernel). The reported stall is the 23 * smallest S such that S and every larger S matched on all runs. Cycles are 24 * the 32-bit clock delta around that same chain, median and max-min over the 25 * runs. An empty loop with the same trip count is the overhead control. 26 * 27 * The two CS2R reads bracket the loop, not one instruction. Between them sit 28 * the producer, its consumer, the live-value advance, the sentinel move, and 29 * the loop-back (increment, compare, branch). A programmed stall of 1 is only 30 * the producer's field. The predicate chain puts that stall on the ISETP that 31 * feeds @P0 BRA: a shorter stall leaves the previous P0, which was set true, 32 * so the poison move runs and the clocks do not cover the whole trip count. 33 * 34 * The loop increment stalls 15 before ISETP. Stall 1 there is shorter than a 35 * fixed-latency add, so the compare samples the old counter and the body runs 36 * one extra time. sm121dis of the previous overhead kernel showed that IADD 37 * (control 0x000fc200, stall 1) immediately before ISETP, with @!P0 BRA back 38 * to the IADD. The overhead word is that counter. The host simulator below 39 * applies the stall counts and rejects that shape. 40 * 41 * One warp, 32 threads, uniform control flow inside the timed region. Every 42 * lane stores eight words. Mesa commit and the NAK line citations live in 43 * tools/latmeas/diff_nak.py. 44 * 45 * Opening the device on a GB10 needs the driver's SM version 0xa04 admitted 46 * (draft PR #251, or the same change). select_classes is not modified here. 47 * 48 * Each fixed-latency case writes a destination that was armed to SENTINEL and 49 * does not read that destination. The next instruction reads it. A stale read 50 * therefore adds SENTINEL, which the host oracle rejects. Back-to-back 51 * `r = r + 1` can stay correct at stall 1 when the new value is forwarded, so 52 * that shape cannot show a too-short stall. HMMA keeps the shape that resolved 53 * on the chip: d starts at 0, HMMA writes it, FADD reads d0. 54 */ 55 #include "omega_blackwell_codegen.h" 56 #include "omega_gpu_session.h" 57 #include "omega_sm121_disasm.h" 58 #include 59 #include 60 #include 61 #include 62 #include 63 #include 64 #include 65 #include 66 #include 67 #include 68 69 #define NOREG (-1) 70 #define SB_NONE 7u 71 #define SB_GPR 0u 72 #define SB_RD 1u 73 #define CLOCK_LO 0x50u /* NAK_SV_CLOCK_LO, nak_private.h */ 74 #define CS2R_OPC 0x805u 75 #define S2R_OPC 0x919u 76 #define POISON 0xA5A5A5A5u 77 #define PRED_POISON 0xDEADBEEFu 78 #define SWEEP_MAX 36 79 #define CODE_CAP 8192u 80 81 enum { 82 RUNS = 11, 83 N_FIXED = 32, 84 HMMA_ITERS = 1, 85 SWEEP_FIXED = 15, 86 SWEEP_HMMA = 36 87 }; 88 #define SENTINEL 0x7f800000u /* +inf bits: not equal to 1..N or to f32 1..N */ 89 #define LANE_N 32 90 #define LANE_WORDS 8 91 #define LANE_STRIDE 32u /* LANE_WORDS * 4: t0, t1, r0, r1, r2, r3, tid, unused */ 92 _Static_assert(RUNS % 2 == 1, "median uses the middle of an odd run count"); 93 _Static_assert(SWEEP_HMMA <= SWEEP_MAX, "sweep fits the sample array"); 94 95 static const char *MESA_COMMIT = "`01bab9302a6bf0a082c7ba8c49d74427cf10b0cf`"; 96 97 typedef struct { 98 const char *id; 99 unsigned iters; 100 unsigned unroll; 101 unsigned sweep; 102 unsigned ops; 103 int href; /* 1: oracle is the result at sweep, not a host formula */ 104 } CaseDef; 105 106 static const CaseDef CASES[] = { 107 {"alu_alu", N_FIXED, 1, SWEEP_FIXED, N_FIXED, 0}, 108 {"fma_fma", N_FIXED, 1, SWEEP_FIXED, N_FIXED, 0}, 109 {"mixed", N_FIXED, 1, SWEEP_FIXED, N_FIXED * 2u, 0}, 110 {"pred", N_FIXED, 1, SWEEP_FIXED, N_FIXED, 0}, 111 {"fmnmx_alu", N_FIXED, 1, SWEEP_FIXED, N_FIXED, 0}, 112 {"imad_wide_lo", N_FIXED, 1, SWEEP_FIXED, N_FIXED, 0}, 113 {"hmma_reader", HMMA_ITERS, 1, SWEEP_HMMA, HMMA_ITERS, 0}, 114 }; 115 #define NCASES ((int)(sizeof CASES / sizeof CASES[0])) 116 117 static BlackwellIRProgram g_prog; 118 119 typedef struct { 120 BlackwellIRProgram *p; 121 int desc; 122 uint32_t pending; 123 int err; 124 } B; 125 126 static uint32_t cw(unsigned stall, unsigned wbar, unsigned rbar, unsigned wait, int yield) { 127 uint32_t v = (stall << 9) | (wbar << 14) | (rbar << 17) | (wait << 20); 128 if (yield) v |= 0x2000u; 129 return v; 130 } 131 132 static int V(B *b) { 133 int v = omega_bw_ir_alloc_vreg(b->p); 134 if (v < 0) b->err = -1; 135 return v; 136 } 137 static int V64(B *b) { 138 int v = omega_bw_ir_alloc_vreg64(b->p); 139 if (v < 0) b->err = -1; 140 return v; 141 } 142 static int V128(B *b) { 143 int v = omega_bw_ir_alloc_vreg128(b->p); 144 if (v < 0) b->err = -1; 145 return v; 146 } 147 148 static int add(B *b, BlackwellIROpcode op, int dst, int s1, int s2, int s3, uint32_t imm, 149 uint32_t control, int p0, int pnot, int dst_sub, int s2_sub) { 150 BlackwellIRInsn in; 151 memset(&in, 0, sizeof in); 152 in.op = op; 153 in.dst_vreg = dst; 154 in.src1_vreg = s1; 155 in.src2_vreg = s2; 156 in.src3_vreg = s3; 157 in.ureg = NOREG; 158 in.imm = imm; 159 in.control = control; 160 in.predicate_p0 = p0; 161 in.predicate_not = pnot; 162 in.dst_subreg = (uint8_t)dst_sub; 163 in.src2_subreg = (uint8_t)s2_sub; 164 if (op == BW_IR_LDCU64) in.is_uniform = 1; 165 if (op == BW_IR_STG_E) in.ureg = b->desc; 166 if (omega_bw_ir_append(b->p, &in) < 0) b->err = -1; 167 return (int)b->p->count - 1; 168 } 169 170 static void movi(B *b, int dst, uint32_t imm, int sub) { 171 uint32_t wait = b->pending; 172 add(b, BW_IR_MOV_IMM, dst, NOREG, NOREG, NOREG, imm, cw(6, SB_NONE, SB_NONE, wait, 0), 0, 0, sub, 0); 173 b->pending = 0; 174 } 175 static void movz(B *b, int dst, int sub) { 176 uint32_t wait = b->pending; 177 add(b, BW_IR_MOV_RZ, dst, NOREG, NOREG, NOREG, 0, cw(6, SB_NONE, SB_NONE, wait, 0), 0, 0, sub, 0); 178 b->pending = 0; 179 } 180 static void iadd(B *b, int dst, int a, int c, unsigned stall, int dst_sub, int src1_sub, int src2_sub) { 181 uint32_t wait = b->pending; 182 int at = add(b, BW_IR_IADD3, dst, a, c, NOREG, 0, cw(stall, SB_NONE, SB_NONE, wait, 0), 0, 0, dst_sub, 0); 183 if (at >= 0 && src1_sub) b->p->insns[at].src1_subreg = (uint8_t)src1_sub; 184 if (at >= 0 && src2_sub) b->p->insns[at].src2_subreg = (uint8_t)src2_sub; 185 b->pending = 0; 186 } 187 static void ffma(B *b, int dst, int a, int c, int d, unsigned stall) { 188 uint32_t wait = b->pending; 189 add(b, BW_IR_FFMA, dst, a, c, d, 0, cw(stall, SB_NONE, SB_NONE, wait, 0), 0, 0, 0, 0); 190 b->pending = 0; 191 } 192 static void fadd(B *b, int dst, int a, int asub, int c, unsigned stall) { 193 uint32_t wait = b->pending; 194 int at = add(b, BW_IR_FADD, dst, a, c, NOREG, 0, cw(stall, SB_NONE, SB_NONE, wait, 0), 0, 0, 0, 0); 195 if (at >= 0 && asub) b->p->insns[at].src1_subreg = (uint8_t)asub; 196 b->pending = 0; 197 } 198 static void fmnmx(B *b, int dst, int a, int c, unsigned stall) { 199 uint32_t wait = b->pending; 200 add(b, BW_IR_FMNMX_MIN, dst, a, c, NOREG, 0, cw(stall, SB_NONE, SB_NONE, wait, 0), 0, 0, 0, 0); 201 b->pending = 0; 202 } 203 static void isetp_ge(B *b, int a, int c, unsigned stall) { 204 uint32_t wait = b->pending; 205 add(b, BW_IR_ISETP_GE_U32, NOREG, a, c, NOREG, 0, cw(stall, SB_NONE, SB_NONE, wait, 0), 0, 0, 0, 0); 206 b->pending = 0; 207 } 208 static int bra(B *b, int p0, int pnot, unsigned stall) { 209 uint32_t wait = b->pending; 210 int at = add(b, BW_IR_BRA, NOREG, NOREG, NOREG, NOREG, 0, cw(stall, SB_NONE, SB_NONE, wait, 0), p0, pnot, 0, 0); 211 b->pending = 0; 212 return at; 213 } 214 static void patch_bra(B *b, int at, int target) { 215 if (at < 0 || target < 0 || (size_t)at >= b->p->count) { 216 b->err = -1; 217 return; 218 } 219 b->p->insns[at].imm = (uint32_t)(int32_t)(target - at); 220 } 221 static void nop_stall(B *b, unsigned stall) { 222 add(b, BW_IR_NOP, NOREG, NOREG, NOREG, NOREG, 0, cw(stall, SB_NONE, SB_NONE, 0, 0), 0, 0, 0, 0); 223 } 224 /* Producer stall is at most 15. Extra cycles are NOP stalls so the consumer 225 * issues `gap` cycles after the producer. */ 226 static void gap_nops(B *b, unsigned gap) { 227 while (gap) { 228 unsigned s = gap > 15u ? 15u : gap; 229 nop_stall(b, s); 230 gap -= s; 231 } 232 } 233 static unsigned stall_head(unsigned gap) { return gap > 15u ? 15u : gap; } 234 235 static void clock_read(B *b, int dst) { 236 uint32_t wait = b->pending; 237 /* Stall 15, no write barrier: the 32-bit CS2R is fixed-latency, and the 238 * timed body uses other registers. Rewritten to CS2R after encoding. */ 239 add(b, BW_IR_S2R, dst, NOREG, NOREG, NOREG, CLOCK_LO, cw(15, SB_NONE, SB_NONE, wait, 0), 0, 0, 0, 0); 240 b->pending = 0; 241 } 242 243 static void pin_live(BlackwellIRProgram *p) { 244 int end = (int)p->count - 1; 245 if (end < 0) return; 246 for (int v = 0; v < p->regalloc.num_vregs; v++) { 247 OmegaLiveInterval *iv = &p->regalloc.intervals[v]; 248 if (iv->first_def >= 0 && iv->last_use < end) iv->last_use = end; 249 } 250 } 251 252 static uint32_t fbits(float f) { 253 uint32_t u; 254 memcpy(&u, &f, 4); 255 return u; 256 } 257 258 /* HMMA.16816.F32.BF16: each of the four f32 accumulators is a dot of k=16. 259 * This kernel writes bf16 1.0 into every A and B half and 0 into C, so each 260 * accumulator is 16 products of 1*1. The same value on every lane, because 261 * every lane's fragments are initialized alike. */ 262 static uint32_t hmma_16816_ones(void) { 263 float acc = 0.f; 264 for (int k = 0; k < 16; k++) acc += 1.f * 1.f; 265 return fbits(acc); 266 } 267 268 /* stale=1: the consumer sees SENTINEL instead of the value just written. 269 * The two results differ for every fixed-latency case. HMMA has no closed form. */ 270 static int eval_case(const CaseDef *c, int stale, uint32_t *result, uint32_t *aux) { 271 if (!c || c->href) return -1; 272 unsigned n = c->iters; 273 *aux = 1; 274 *result = 0; 275 if (!strcmp(c->id, "alu_alu") || !strcmp(c->id, "imad_wide_lo")) { 276 uint32_t acc = 0, live = 0; 277 for (unsigned i = 0; i < n; i++) { 278 uint32_t produced = live + 1u; 279 acc += stale ? SENTINEL : produced; 280 live += 1u; 281 } 282 *result = acc; 283 return 0; 284 } 285 if (!strcmp(c->id, "fma_fma")) { 286 float acc = 0.f, live = 0.f, inf = 0.f; 287 memcpy(&inf, &(uint32_t){SENTINEL}, 4); 288 for (unsigned i = 0; i < n; i++) { 289 float produced = live + 1.f; 290 float seen = stale ? inf : produced; 291 acc = acc + seen; 292 live = produced; 293 } 294 *result = fbits(acc); 295 return 0; 296 } 297 if (!strcmp(c->id, "fmnmx_alu")) { 298 uint32_t acc = 0; 299 float cur = 1.f; 300 for (unsigned i = 0; i < n; i++) { 301 acc += stale ? SENTINEL : fbits(cur); 302 cur += 1.f; 303 } 304 *result = acc; 305 return 0; 306 } 307 if (!strcmp(c->id, "mixed")) { 308 uint32_t acc = 0; 309 float live = 0.f; 310 for (unsigned i = 0; i < n; i++) { 311 float produced = live + 1.f; 312 acc += stale ? SENTINEL : fbits(produced); 313 live = produced; 314 } 315 uint32_t acc2 = 0; 316 float inf = 0.f; 317 memcpy(&inf, &(uint32_t){SENTINEL}, 4); 318 live = 1.f; 319 for (unsigned i = 0; i < n; i++) { 320 /* FFMA(0, 0, x) preserves a normal f32 and yields +inf for SENTINEL. */ 321 float out = stale ? inf : live; 322 acc2 += fbits(out); 323 live += 1.f; 324 } 325 *result = acc; 326 *aux = acc2; 327 return 0; 328 } 329 if (!strcmp(c->id, "pred")) { 330 *result = stale ? PRED_POISON : n; 331 return 0; 332 } 333 if (!strcmp(c->id, "hmma_reader")) { 334 uint32_t e = hmma_16816_ones(); 335 *result = stale ? 0u : e; 336 *aux = stale ? 0u : e; 337 return 0; 338 } 339 return -1; 340 } 341 342 static int expect_abs(const CaseDef *c, uint32_t *result, uint32_t *aux) { 343 return eval_case(c, 0, result, aux); 344 } 345 346 static void prologue(B *b, int *ptr) { 347 b->desc = omega_bw_ir_alloc_uvreg64(b->p); 348 if (b->desc < 0) b->err = -1; 349 *ptr = V64(b); 350 uint32_t wait = b->pending; 351 add(b, BW_IR_LDCU64, b->desc, NOREG, NOREG, NOREG, 0x358, cw(4, 2, SB_NONE, wait, 1), 0, 0, 0, 0); 352 b->pending = 1u << 2; 353 wait = b->pending; 354 add(b, BW_IR_LDC64, *ptr, NOREG, NOREG, NOREG, 0x380, cw(4, SB_GPR, SB_NONE, wait, 1), 0, 0, 0, 0); 355 b->pending = 1u << SB_GPR; 356 } 357 358 /* Every lane stores eight words at ptr + tid * LANE_STRIDE. There is no 359 * lane-0 filter: a lane that did not run would leave POISON in its slot. */ 360 static void store_lanes(B *b, int ptr, int t0, int t1, int r0, int r0s, int r1, int r1s, int r2, int r2s, int r3, 361 int r3s) { 362 int tid = V(b); 363 int addr = V64(b); 364 /* Stall 15, and a scoreboard the IMAD waits on. The address uses tid. 365 * A stall of 4 here is shorter than a fixed-latency S2R in the host 366 * simulator, which would then scale every lane by 0. */ 367 uint32_t wait = b->pending; 368 add(b, BW_IR_S2R, tid, NOREG, NOREG, NOREG, BW_SR_TID_X, cw(15, SB_GPR, SB_NONE, wait, 1), 0, 0, 0, 0); 369 b->pending = 1u << SB_GPR; 370 wait = b->pending; 371 add(b, BW_IR_IMAD_WIDE, addr, tid, NOREG, ptr, LANE_STRIDE, cw(6, SB_NONE, SB_NONE, wait, 0), 0, 0, 0, 0); 372 b->pending = 0; 373 uint32_t st = cw(4, SB_NONE, SB_RD, 0, 1); 374 add(b, BW_IR_STG_E, NOREG, addr, t0, NOREG, 0, st, 0, 0, 0, 0); 375 add(b, BW_IR_STG_E, NOREG, addr, t1, NOREG, 4, st, 0, 0, 0, 0); 376 add(b, BW_IR_STG_E, NOREG, addr, r0, NOREG, 8, st, 0, 0, 0, r0s); 377 add(b, BW_IR_STG_E, NOREG, addr, r1, NOREG, 12, st, 0, 0, 0, r1s); 378 add(b, BW_IR_STG_E, NOREG, addr, r2, NOREG, 16, st, 0, 0, 0, r2s); 379 add(b, BW_IR_STG_E, NOREG, addr, r3, NOREG, 20, st, 0, 0, 0, r3s); 380 add(b, BW_IR_STG_E, NOREG, addr, tid, NOREG, 24, st, 0, 0, 0, 0); 381 add(b, BW_IR_EXIT, NOREG, NOREG, NOREG, NOREG, 0, cw(5, SB_NONE, SB_NONE, 0, 0), 0, 0, 0, 0); 382 add(b, BW_IR_BRA, NOREG, NOREG, NOREG, NOREG, 0, 0x000fc000u, 0, 0, 0, 0); 383 } 384 385 static void loop_ctl(B *b, int i, int nreg, int one, int *loop_back) { 386 /* 15, not 1. The compare reads i. Stall 1 is shorter than a fixed-latency 387 * IADD, so ISETP samples the previous i and the body runs one extra time: 388 * the overhead word is then not the trip count, and every exact oracle 389 * fails, including a stall at the top of the sweep. 15 is past every fixed 390 * GPR latency this kit measures. The stall under test stays on the producer. */ 391 iadd(b, i, i, one, 15, 0, 0, 0); 392 isetp_ge(b, i, nreg, 14); /* P0 = (i >= n) */ 393 *loop_back = bra(b, 1, 1, 5); /* @!P0 BRA loop */ 394 } 395 396 /* Producer writes dst, which was armed to SENTINEL and which the producer does 397 * not read. The very next instruction reads dst. A stale read adds SENTINEL. */ 398 static void body(B *b, const CaseDef *c, unsigned stall, int live, int livef, int one, int zero, int fone, int acc, 399 int acc2, int dst, int dstf, int seen, int rpair, int tpair, int *poison_br) { 400 unsigned head = stall_head(stall); 401 unsigned rest = stall - head; 402 if (!strcmp(c->id, "alu_alu")) { 403 iadd(b, dst, live, one, head, 0, 0, 0); 404 gap_nops(b, rest); 405 iadd(b, acc, acc, dst, 15, 0, 0, 0); 406 iadd(b, live, live, one, 15, 0, 0, 0); 407 movi(b, dst, SENTINEL, 0); 408 } else if (!strcmp(c->id, "fma_fma")) { 409 ffma(b, dst, fone, fone, livef, head); /* dst = livef + 1 */ 410 gap_nops(b, rest); 411 fadd(b, acc, acc, 0, dst, 15); /* FMA consumer, immediate */ 412 fadd(b, livef, livef, 0, fone, 15); 413 movi(b, dst, SENTINEL, 0); 414 } else if (!strcmp(c->id, "mixed")) { 415 ffma(b, dstf, fone, fone, livef, head); 416 gap_nops(b, rest); 417 iadd(b, acc, acc, dstf, 15, 0, 0, 0); /* FMA -> ALU */ 418 fadd(b, livef, livef, 0, fone, 15); 419 movi(b, dstf, SENTINEL, 0); 420 iadd(b, dst, live, zero, head, 0, 0, 0); /* ALU copy of a normal f32 bit pattern */ 421 gap_nops(b, rest); 422 ffma(b, seen, zero, zero, dst, 15); /* ALU -> FMA, immediate */ 423 iadd(b, acc2, acc2, seen, 15, 0, 0, 0); 424 fadd(b, live, live, 0, fone, 15); 425 movi(b, dst, SENTINEL, 0); 426 } else if (!strcmp(c->id, "pred")) { 427 /* P0 is true from the previous setup. This ISETP clears it. The next 428 * instruction is @P0 BRA poison, so a stale predicate takes the poison. */ 429 isetp_ge(b, one, zero, 15); 430 iadd(b, live, live, one, 15, 0, 0, 0); 431 isetp_ge(b, zero, live, head); 432 gap_nops(b, rest); 433 *poison_br = bra(b, 1, 0, 1); 434 } else if (!strcmp(c->id, "fmnmx_alu")) { 435 fmnmx(b, dst, livef, livef, head); 436 gap_nops(b, rest); 437 iadd(b, acc, acc, dst, 15, 0, 0, 0); 438 fadd(b, livef, livef, 0, fone, 15); 439 movi(b, dst, SENTINEL, 0); 440 } else if (!strcmp(c->id, "imad_wide_lo")) { 441 uint32_t wait = b->pending; 442 add(b, BW_IR_IMAD_WIDE, tpair, one, NOREG, rpair, 1, cw(head, SB_NONE, SB_NONE, wait, 0), 0, 0, 0, 0); 443 b->pending = 0; 444 gap_nops(b, rest); 445 iadd(b, acc, acc, tpair, 15, 0, 0, 0); 446 iadd(b, rpair, rpair, one, 15, 0, 0, 0); 447 movi(b, tpair, SENTINEL, 0); 448 movi(b, tpair, SENTINEL, 1); 449 } else { 450 b->err = -1; 451 } 452 } 453 454 static int build(const CaseDef *c, unsigned stall, unsigned ovh_iters, int overhead, uint8_t *code, size_t cap, 455 size_t *out_len, uint32_t *gpr) { 456 if (!overhead && !c) return -1; 457 if (!overhead && (stall < 1u || stall > c->sweep)) return -1; 458 memset(&g_prog, 0, sizeof g_prog); 459 omega_bw_ir_init(&g_prog); 460 B b; 461 memset(&b, 0, sizeof b); 462 b.p = &g_prog; 463 b.desc = NOREG; 464 int ptr = NOREG; 465 prologue(&b, &ptr); 466 int one = V(&b), zero = V(&b), fone = V(&b), i = V(&b), nreg = V(&b); 467 int r = V(&b), acc = V(&b), acc2 = V(&b), cur = V(&b), tmp = V(&b), dsti = V(&b), seen = V(&b); 468 int t0 = V(&b), t1 = V(&b), aux = V(&b); 469 int rpair = NOREG, tpair = NOREG, dquad = NOREG, aquad = NOREG, bpair = NOREG, cquad = NOREG; 470 int h0 = NOREG, h1 = NOREG, h2 = NOREG, h3 = NOREG; 471 unsigned iters = overhead ? ovh_iters : c->iters; 472 movi(&b, one, 1, 0); 473 movz(&b, zero, 0); 474 movi(&b, fone, 0x3f800000u, 0); 475 movi(&b, nreg, iters, 0); 476 movz(&b, i, 0); 477 movz(&b, r, 0); 478 movz(&b, acc, 0); 479 movz(&b, acc2, 0); 480 movz(&b, cur, 0); 481 movi(&b, aux, 1, 0); 482 if (!overhead && c && !c->href && strcmp(c->id, "pred") != 0 && strcmp(c->id, "imad_wide_lo") != 0) 483 movi(&b, dsti, SENTINEL, 0); 484 if (!overhead && !strcmp(c->id, "fmnmx_alu")) movi(&b, cur, 0x3f800000u, 0); 485 if (!overhead && !strcmp(c->id, "mixed")) { 486 movi(&b, r, 0x3f800000u, 0); /* ALU source: f32 1.0 bits, a normal number */ 487 movi(&b, tmp, SENTINEL, 0); /* FMA destination, separate from dsti */ 488 } 489 if (!overhead && !strcmp(c->id, "imad_wide_lo")) { 490 rpair = V64(&b); 491 tpair = V64(&b); 492 movz(&b, rpair, 0); 493 movz(&b, rpair, 1); 494 movi(&b, tpair, SENTINEL, 0); 495 movi(&b, tpair, SENTINEL, 1); 496 } 497 if (!overhead && !strcmp(c->id, "hmma_reader")) { 498 aquad = V128(&b); 499 bpair = V64(&b); 500 cquad = V128(&b); 501 dquad = V128(&b); 502 for (int s = 0; s < 4; s++) movi(&b, aquad, 0x3f803f80u, s); 503 for (int s = 0; s < 2; s++) movi(&b, bpair, 0x3f803f80u, s); 504 for (int s = 0; s < 4; s++) movz(&b, cquad, s); 505 for (int s = 0; s < 4; s++) movz(&b, dquad, s); 506 movi(&b, acc, 0xffffffffu, 0); 507 } 508 clock_read(&b, t0); 509 int loop = (int)g_prog.count; 510 int poison_br = -1; 511 int back = -1; 512 if (!overhead && !strcmp(c->id, "hmma_reader")) { 513 unsigned head = stall_head(stall); 514 unsigned rest = stall - head; 515 uint32_t wait = b.pending; 516 add(&b, BW_IR_HMMA_BF16, dquad, aquad, bpair, cquad, 0, cw(head, SB_NONE, SB_NONE, wait, 0), 0, 0, 0, 0); 517 b.pending = 0; 518 gap_nops(&b, rest); 519 /* Four consumers, one per destination register. Stall 1 between them 520 * is the minimum issue gap: d1 is sampled one cycle after d0, d2 two, 521 * d3 three. The programmed stall is the gap before d0. */ 522 h0 = V(&b); 523 h1 = V(&b); 524 h2 = V(&b); 525 h3 = V(&b); 526 fadd(&b, h0, dquad, 0, zero, 1); 527 fadd(&b, h1, dquad, 1, zero, 1); 528 fadd(&b, h2, dquad, 2, zero, 1); 529 fadd(&b, h3, dquad, 3, zero, 1); 530 if (c->iters > 1) { 531 /* HMMA_ITERS is 1: a repeated identical HMMA would make a stale 532 * read equal the previous correct write. */ 533 b.err = -1; 534 } 535 } else if (!overhead) { 536 body(&b, c, stall, r, cur, one, zero, fone, acc, acc2, dsti, tmp, seen, rpair, tpair, &poison_br); 537 } 538 if (!(c && !overhead && c->iters == 1 && !strcmp(c->id, "hmma_reader"))) { 539 loop_ctl(&b, i, nreg, one, &back); 540 patch_bra(&b, back, loop); 541 } 542 int join_br = -1; 543 if (poison_br >= 0) join_br = bra(&b, 0, 0, 5); 544 if (poison_br >= 0) { 545 int poison = (int)g_prog.count; 546 movi(&b, r, PRED_POISON, 0); 547 patch_bra(&b, poison_br, poison); 548 patch_bra(&b, join_br, (int)g_prog.count); 549 } 550 clock_read(&b, t1); 551 int result = r; 552 int rsub = 0; 553 int auxv = aux; 554 int asub = 0; 555 if (!overhead && c && !c->href && strcmp(c->id, "pred") != 0) result = acc; 556 if (!overhead && !strcmp(c->id, "mixed")) auxv = acc2; 557 int r2 = zero, r3 = zero, r2s = 0, r3s = 0; 558 if (!overhead && !strcmp(c->id, "hmma_reader")) { 559 result = h0; 560 rsub = 0; 561 auxv = h1; 562 asub = 0; 563 r2 = h2; 564 r3 = h3; 565 } 566 if (overhead) result = i; 567 store_lanes(&b, ptr, t0, t1, result, rsub, auxv, asub, r2, r2s, r3, r3s); 568 if (b.err) return -1; 569 pin_live(&g_prog); 570 if (omega_bw_regalloc_solve(&g_prog) != 0) return -1; 571 if (cap < omega_bw_program_code_bytes(&g_prog)) return -1; 572 if (omega_bw_encode_program(&g_prog, code, cap, out_len) != 0) return -1; 573 int clocks = 0; 574 for (size_t at = 0; at + 16 <= *out_len && at / 16 < g_prog.count; at += 16) { 575 uint32_t w0, w2; 576 memcpy(&w0, code + at, 4); 577 memcpy(&w2, code + at + 8, 4); 578 if ((w0 & 0xfffu) == S2R_OPC && ((w2 >> 8) & 0xffu) == CLOCK_LO) { 579 w0 = (w0 & ~0xfffu) | CS2R_OPC; 580 memcpy(code + at, &w0, 4); 581 clocks++; 582 } 583 } 584 if (clocks != 2) return -1; 585 if (gpr) *gpr = omega_bw_gpr_declared(g_prog.regalloc.peak_gpr_usage); 586 return 0; 587 } 588 589 static uint32_t rd32(const uint8_t *p) { 590 uint32_t w; 591 memcpy(&w, p, 4); 592 return w; 593 } 594 static unsigned stall_of(const uint8_t *insn) { return (rd32(insn + 12) >> 9) & 15u; } 595 static unsigned opc_of(const uint8_t *insn) { return rd32(insn) & 0xfffu; } 596 597 static int count_opc_stall(const uint8_t *code, size_t ninsn, unsigned opc, unsigned stall) { 598 int n = 0; 599 for (size_t i = 0; i < ninsn; i++) { 600 const uint8_t *ins = code + i * 16; 601 if (opc_of(ins) == opc && stall_of(ins) == stall) n++; 602 } 603 return n; 604 } 605 static unsigned reg_dst(const uint8_t *ins) { return (rd32(ins) >> 16) & 0xffu; } 606 static unsigned reg_a(const uint8_t *ins) { return (rd32(ins) >> 24) & 0xffu; } 607 static unsigned reg_b(const uint8_t *ins) { return rd32(ins + 4) & 0xffu; } 608 static unsigned reg_c(const uint8_t *ins) { return rd32(ins + 8) & 0xffu; } 609 static int bare_issue(const uint8_t *ins) { 610 unsigned w3 = rd32(ins + 12); 611 return ((w3 >> 13) & 1u) == 0u && ((w3 >> 14) & 7u) == 7u && ((w3 >> 17) & 7u) == 7u && ((w3 >> 20) & 63u) == 0u; 612 } 613 static int reads_reg(const uint8_t *ins, unsigned r) { 614 unsigned opc = opc_of(ins); 615 if (opc == 0x223u) return reg_c(ins) == r; 616 if (opc == 0x221u || opc == 0x210u) return reg_a(ins) == r || reg_b(ins) == r; 617 return 0; 618 } 619 static int bra_index(const uint8_t *ins, int index) { 620 uint32_t w0 = rd32(ins), w1 = rd32(ins + 4); 621 int32_t low = (int32_t)((w0 >> 18) & 0x3fu); 622 int32_t high = (int32_t)w1 >> 2; 623 int32_t rel = (high << 6) | low; 624 return index + rel + 1; 625 } 626 627 /* Stall-1 shape: the consumer is the next instruction, it reads the producer's 628 * destination, and the producer carries no scoreboard and no yield. A stale 629 * read of the armed SENTINEL is then visible to the oracle. */ 630 static int check_raw_window(const char *id, const uint8_t *code, size_t ninsn, unsigned stall) { 631 int failed = 0, producers = 0; 632 #define W(cond, ...) \ 633 do { \ 634 if (!(cond)) { \ 635 failed++; \ 636 printf("FAIL: %s: ", id); \ 637 printf(__VA_ARGS__); \ 638 printf("\n"); \ 639 } \ 640 } while (0) 641 for (size_t i = 0; i + 1 < ninsn; i++) { 642 const uint8_t *ins = code + i * 16; 643 const uint8_t *nxt = code + (i + 1) * 16; 644 unsigned opc = opc_of(ins); 645 if (stall_of(ins) != stall) continue; 646 int take = 0; 647 if (!strcmp(id, "alu_alu") && opc == 0x210u && reg_dst(ins) != reg_a(ins)) take = 1; 648 if (!strcmp(id, "fma_fma") && opc == 0x223u && reg_dst(ins) != reg_c(ins)) take = 1; 649 if (!strcmp(id, "mixed") && opc == 0x223u && reg_dst(ins) != reg_c(ins)) take = 1; 650 if (!strcmp(id, "mixed") && opc == 0x210u && reg_dst(ins) != reg_a(ins)) take = 1; 651 if (!strcmp(id, "fmnmx_alu") && opc == 0x209u && reg_dst(ins) != reg_a(ins)) take = 1; 652 if (!strcmp(id, "imad_wide_lo") && opc == 0x825u) take = 1; 653 if (!strcmp(id, "hmma_reader") && opc == 0x23cu) take = 1; 654 if (!strcmp(id, "pred") && opc == 0x20cu) take = 1; 655 if (!take) continue; 656 producers++; 657 W(bare_issue(ins), "producer %zu is scoreboarded or yields (w3 0x%08x)", i, rd32(ins + 12)); 658 if (!strcmp(id, "pred")) { 659 W(opc_of(nxt) == 0x947u, "predicate consumer is not BRA"); 660 W(((rd32(nxt) >> 12) & 0xfu) == 0u, "poison branch is not @P0"); 661 int tgt = bra_index(nxt, (int)i + 1); 662 W(tgt >= 0 && (size_t)tgt < ninsn, "poison target %d", tgt); 663 if (tgt >= 0 && (size_t)tgt < ninsn) { 664 const uint8_t *mv = code + (size_t)tgt * 16; 665 W(opc_of(mv) == 0x802u && rd32(mv + 4) == PRED_POISON, "poison mov missing"); 666 } 667 } else { 668 W(reads_reg(nxt, reg_dst(ins)), "consumer does not read producer dest R%u", reg_dst(ins)); 669 } 670 } 671 W(producers == (!strcmp(id, "mixed") ? 2 : 1), "producer count %d", producers); 672 #undef W 673 return failed; 674 } 675 676 /* Host latency simulator. The shared warp simulator (tests/bw_warp_sim.h) writes 677 * a register and the next instruction already sees it, so it cannot catch a 678 * stall that is too short. This one issues the IR in order and keeps each GPR 679 * write hidden for SIM_GPR_LAT cycles and each P0 write hidden for 680 * SIM_PRED_LAT cycles. Those are the NAK floors (ALU/FMA raw 4 plus one, 681 * predicate raw 13 plus one), used here as the model's assumption. A read at 682 * the issue cycle sees a write whose visible cycle is at or before that. The 683 * stall field is how many cycles pass before the next issue. */ 684 #define SIM_GPR_LAT 5u 685 #define SIM_PRED_LAT 14u 686 #define SIM_LOG 4096 687 688 float fmaf(float, float, float); 689 float fminf(float, float); 690 691 typedef struct { 692 int is_pred; 693 int reg; 694 uint32_t issue, visible, value; 695 } SimWr; 696 697 typedef struct { 698 SimWr wr[SIM_LOG]; 699 int nwr; 700 uint32_t now; 701 int pc; 702 int stores; 703 const char *err; 704 } LatSim; 705 706 static uint32_t lat_u2f_bits(float f) { 707 uint32_t u; 708 memcpy(&u, &f, 4); 709 return u; 710 } 711 static float lat_f(uint32_t u) { 712 float f; 713 memcpy(&f, &u, 4); 714 return f; 715 } 716 717 static int lat_phys(int v, unsigned sub, int *reg) { 718 if (v < 0) { 719 *reg = -1; 720 return 0; 721 } 722 if (v >= g_prog.regalloc.num_vregs) return -1; 723 int ph = g_prog.regalloc.vreg_to_phys[v]; 724 if (ph < 0) return -1; 725 *reg = (ph + (int)sub) & 0xff; 726 return 0; 727 } 728 729 static uint32_t lat_gpr(const LatSim *s, int reg) { 730 uint32_t val = 0, issue = 0; 731 int have = 0; 732 if (reg < 0) return 0; 733 for (int i = 0; i < s->nwr; i++) { 734 if (s->wr[i].is_pred || s->wr[i].reg != reg) continue; 735 if (s->wr[i].visible > s->now) continue; 736 if (!have || s->wr[i].issue >= issue) { 737 have = 1; 738 issue = s->wr[i].issue; 739 val = s->wr[i].value; 740 } 741 } 742 return val; 743 } 744 745 static int lat_p0(const LatSim *s) { 746 uint32_t issue = 0, val = 0; 747 int have = 0; 748 for (int i = 0; i < s->nwr; i++) { 749 if (!s->wr[i].is_pred) continue; 750 if (s->wr[i].visible > s->now) continue; 751 if (!have || s->wr[i].issue >= issue) { 752 have = 1; 753 issue = s->wr[i].issue; 754 val = s->wr[i].value; 755 } 756 } 757 return val != 0; 758 } 759 760 static int lat_post(LatSim *s, int is_pred, int reg, uint32_t lat, uint32_t value) { 761 if (reg < 0 && !is_pred) return 0; 762 if (s->nwr >= SIM_LOG) { 763 s->err = "write log full"; 764 return -1; 765 } 766 s->wr[s->nwr].is_pred = is_pred; 767 s->wr[s->nwr].reg = reg; 768 s->wr[s->nwr].issue = s->now; 769 s->wr[s->nwr].visible = s->now + lat; 770 s->wr[s->nwr].value = value; 771 s->nwr++; 772 return 0; 773 } 774 775 static int lat_rd(const LatSim *s, int v, unsigned sub, uint32_t *out) { 776 int reg = 0; 777 if (lat_phys(v, sub, ®) != 0) return -1; 778 *out = lat_gpr(s, reg); 779 return 0; 780 } 781 782 static int lat_wr(LatSim *s, int v, unsigned sub, uint32_t value, uint32_t lat) { 783 int reg = 0; 784 if (lat_phys(v, sub, ®) != 0) return -1; 785 return lat_post(s, 0, reg, lat, value); 786 } 787 788 /* One lane. mem[] is the whole warp's output, poisoned by the caller. 789 * Returns 0 when this lane stored LANE_WORDS-1 words (the last slot is unused). */ 790 static int lat_sim_lane(uint32_t tid, uint32_t *mem) { 791 LatSim s; 792 memset(&s, 0, sizeof s); 793 for (int steps = 0; steps < 100000; steps++) { 794 if (s.pc < 0 || (size_t)s.pc >= g_prog.count) { 795 s.err = "pc outside the program"; 796 break; 797 } 798 const BlackwellIRInsn *in = &g_prog.insns[s.pc]; 799 unsigned stall = (in->control >> 9) & 15u; 800 int next = s.pc + 1; 801 uint32_t a = 0, b = 0, c = 0; 802 if (lat_rd(&s, in->src1_vreg, in->src1_subreg, &a) || lat_rd(&s, in->src2_vreg, in->src2_subreg, &b) || 803 lat_rd(&s, in->src3_vreg, in->src3_subreg, &c)) { 804 s.err = "vreg has no physical register"; 805 break; 806 } 807 switch (in->op) { 808 case BW_IR_EXIT: 809 if (s.stores != LANE_WORDS - 1) { 810 s.err = "lane did not store its seven words"; 811 break; 812 } 813 return 0; 814 case BW_IR_BRA: { 815 int take = 1; 816 if (in->predicate_p0) take = in->predicate_not ? !lat_p0(&s) : lat_p0(&s); 817 if (take && (int32_t)in->imm == 0) { 818 s.err = "branch to itself"; 819 break; 820 } 821 if (take) next = s.pc + (int32_t)in->imm; 822 break; 823 } 824 case BW_IR_NOP: 825 case BW_IR_LDCU64: 826 case BW_IR_LDCU: 827 break; 828 case BW_IR_LDC64: 829 if (lat_wr(&s, in->dst_vreg, 0, 0, SIM_GPR_LAT) || lat_wr(&s, in->dst_vreg, 1, 0, SIM_GPR_LAT)) 830 s.err = "write log full"; 831 break; 832 case BW_IR_MOV_IMM: 833 if (lat_wr(&s, in->dst_vreg, in->dst_subreg, in->imm, SIM_GPR_LAT)) s.err = "write log full"; 834 break; 835 case BW_IR_MOV_RZ: 836 if (lat_wr(&s, in->dst_vreg, in->dst_subreg, 0, SIM_GPR_LAT)) s.err = "write log full"; 837 break; 838 case BW_IR_S2R: { 839 uint32_t v = 0; 840 if (in->imm == BW_SR_TID_X) v = tid; 841 else if (in->imm == CLOCK_LO) v = s.now; 842 if (lat_wr(&s, in->dst_vreg, in->dst_subreg, v, SIM_GPR_LAT)) s.err = "write log full"; 843 break; 844 } 845 case BW_IR_HMMA_BF16: { 846 int ones = 1; 847 for (unsigned sub = 0; sub < 4 && ones; sub++) { 848 uint32_t u = 0; 849 if (lat_rd(&s, in->src1_vreg, sub, &u) || u != 0x3f803f80u) ones = 0; 850 } 851 for (unsigned sub = 0; sub < 2 && ones; sub++) { 852 uint32_t u = 0; 853 if (lat_rd(&s, in->src2_vreg, sub, &u) || u != 0x3f803f80u) ones = 0; 854 } 855 for (unsigned sub = 0; sub < 4 && ones; sub++) { 856 uint32_t u = 0; 857 if (lat_rd(&s, in->src3_vreg, sub, &u) || u != 0) ones = 0; 858 } 859 if (!ones) { 860 s.err = "hmma inputs are not the ones matrix"; 861 break; 862 } 863 uint32_t y = hmma_16816_ones(); 864 for (unsigned sub = 0; sub < 4; sub++) 865 if (lat_wr(&s, in->dst_vreg, sub, y, SIM_GPR_LAT)) s.err = "write log full"; 866 break; 867 } 868 case BW_IR_IADD3: 869 if (lat_wr(&s, in->dst_vreg, in->dst_subreg, a + b, SIM_GPR_LAT)) s.err = "write log full"; 870 break; 871 case BW_IR_ISETP_GE_U32: 872 if (lat_post(&s, 1, 0, SIM_PRED_LAT, a >= b ? 1u : 0u)) s.err = "write log full"; 873 break; 874 case BW_IR_FFMA: 875 if (lat_wr(&s, in->dst_vreg, in->dst_subreg, lat_u2f_bits(fmaf(lat_f(a), lat_f(b), lat_f(c))), SIM_GPR_LAT)) 876 s.err = "write log full"; 877 break; 878 case BW_IR_FADD: 879 if (lat_wr(&s, in->dst_vreg, in->dst_subreg, lat_u2f_bits(lat_f(a) + lat_f(b)), SIM_GPR_LAT)) 880 s.err = "write log full"; 881 break; 882 case BW_IR_FMNMX_MIN: 883 if (lat_wr(&s, in->dst_vreg, in->dst_subreg, lat_u2f_bits(fminf(lat_f(a), lat_f(b))), SIM_GPR_LAT)) 884 s.err = "write log full"; 885 break; 886 case BW_IR_IMAD_WIDE: { 887 /* Immediate form: Rd:Rd+1 = Ra * imm + Rc+1:Rc. src2 is NOREG. */ 888 uint32_t hi = 0; 889 if (lat_rd(&s, in->src3_vreg, 1, &hi)) { 890 s.err = "vreg has no physical register"; 891 break; 892 } 893 uint64_t prod = (uint64_t)a * in->imm + (((uint64_t)hi << 32) | c); 894 if (lat_wr(&s, in->dst_vreg, 0, (uint32_t)prod, SIM_GPR_LAT) || 895 lat_wr(&s, in->dst_vreg, 1, (uint32_t)(prod >> 32), SIM_GPR_LAT)) 896 s.err = "write log full"; 897 break; 898 } 899 case BW_IR_STG_E: { 900 uint32_t lo = 0, hi = 0; 901 if (lat_rd(&s, in->src1_vreg, 0, &lo) || lat_rd(&s, in->src1_vreg, 1, &hi)) { 902 s.err = "vreg has no physical register"; 903 break; 904 } 905 uint64_t ad = (uint64_t)lo | ((uint64_t)hi << 32); 906 uint64_t at = ad + (uint32_t)in->imm; 907 if ((at & 3u) || at / 4u >= (uint64_t)LANE_N * LANE_WORDS) { 908 s.err = "store address is outside the lane buffer"; 909 break; 910 } 911 mem[at / 4u] = b; 912 s.stores++; 913 break; 914 } 915 default: 916 s.err = "opcode not modelled"; 917 break; 918 } 919 if (s.err) break; 920 s.now += stall ? stall : 1u; 921 s.pc = next; 922 } 923 if (s.err) fprintf(stderr, "lat sim: %s at pc %d\n", s.err, s.pc); 924 else fprintf(stderr, "lat sim: step budget at pc %d\n", s.pc); 925 return -1; 926 } 927 928 /* The loop-back sm121dis must show: IADD (dest == src A) stall 15, ISETP stall 929 * 14, then @!P0 BRA back to the body. The body is at or before that IADD 930 * (overhead has no body, so the branch lands on the IADD). HMMA has no loop. */ 931 static int check_loop_sass(const char *id, const uint8_t *code, size_t ninsn, int expect_loop) { 932 int found = 0, failed = 0; 933 for (size_t i = 0; i + 2 < ninsn; i++) { 934 const uint8_t *a = code + i * 16; 935 const uint8_t *bp = code + (i + 1) * 16; 936 const uint8_t *c = code + (i + 2) * 16; 937 if (opc_of(a) != 0x210u || opc_of(bp) != 0x20cu || opc_of(c) != 0x947u) continue; 938 if (reg_dst(a) != reg_a(a)) continue; 939 if (((rd32(c) >> 12) & 0xfu) != 8u) continue; 940 found++; 941 if (stall_of(a) != 15u) { 942 failed++; 943 printf("FAIL: %s loop increment stall %u, want 15\n", id, stall_of(a)); 944 } 945 if (stall_of(bp) != 14u) { 946 failed++; 947 printf("FAIL: %s loop compare stall %u, want 14\n", id, stall_of(bp)); 948 } 949 int tgt = bra_index(c, (int)i + 2); 950 if (tgt < 0 || tgt > (int)i) { 951 failed++; 952 printf("FAIL: %s loop branch targets %d, increment is at %zu\n", id, tgt, i); 953 } 954 } 955 if (found != expect_loop) { 956 failed++; 957 printf("FAIL: %s loop-back count %d, want %d\n", id, found, expect_loop); 958 } 959 return failed; 960 } 961 962 static uint32_t g_sim_mem[LANE_N * LANE_WORDS]; 963 964 static int lat_sim(uint32_t lane0[LANE_WORDS]) { 965 for (int i = 0; i < LANE_N * LANE_WORDS; i++) g_sim_mem[i] = POISON; 966 for (uint32_t tid = 0; tid < LANE_N; tid++) 967 if (lat_sim_lane(tid, g_sim_mem) != 0) return -1; 968 if (lane0) memcpy(lane0, g_sim_mem, sizeof(uint32_t) * LANE_WORDS); 969 return 0; 970 } 971 972 /* four=1: words 2..5 are the same value (HMMA d0..d3). Otherwise words 4 and 5 stay 0. */ 973 static int sim_lanes_match(uint32_t result, uint32_t aux, int four) { 974 for (int lane = 0; lane < LANE_N; lane++) { 975 const uint32_t *w = g_sim_mem + (size_t)lane * LANE_WORDS; 976 if (w[6] != (uint32_t)lane || w[2] != result || w[3] != aux) return 0; 977 if (four) { 978 if (w[4] != result || w[5] != result) return 0; 979 } else if (w[4] != 0 || w[5] != 0) { 980 return 0; 981 } 982 } 983 return 1; 984 } 985 986 /* Seven stores at +0,+4,...,+24, addressed by IMAD.WIDE Rd, tid, 32, ptr. */ 987 static int check_lane_stores(const char *id, const uint8_t *code, size_t ninsn) { 988 int stg = 0, wide = 0, failed = 0; 989 int saw_off[7] = {0, 0, 0, 0, 0, 0, 0}; 990 for (size_t i = 0; i < ninsn; i++) { 991 const uint8_t *ins = code + i * 16; 992 if (opc_of(ins) == 0x825u && rd32(ins + 4) == LANE_STRIDE) wide++; 993 if (opc_of(ins) == 0x986u) { 994 stg++; 995 uint32_t off = (rd32(ins + 4) >> 8) & 0xffffffu; 996 if (off % 4u == 0 && off / 4u < 7u) saw_off[off / 4u] = 1; 997 } 998 } 999 if (wide != 1) { 1000 failed++; 1001 printf("FAIL: %s tid-scaled address count %d\n", id, wide); 1002 } 1003 if (stg != 7) { 1004 failed++; 1005 printf("FAIL: %s store count %d, want 7\n", id, stg); 1006 } 1007 for (int k = 0; k < 7; k++) 1008 if (!saw_off[k]) { 1009 failed++; 1010 printf("FAIL: %s missing store +%d\n", id, k * 4); 1011 } 1012 return failed; 1013 } 1014 1015 static int loop_iadd_index(void) { 1016 int found = -1; 1017 for (size_t i = 0; i + 2 < g_prog.count; i++) { 1018 const BlackwellIRInsn *a = &g_prog.insns[i]; 1019 const BlackwellIRInsn *b = &g_prog.insns[i + 1]; 1020 const BlackwellIRInsn *c = &g_prog.insns[i + 2]; 1021 if (a->op != BW_IR_IADD3 || b->op != BW_IR_ISETP_GE_U32 || c->op != BW_IR_BRA) continue; 1022 if (a->dst_vreg != a->src1_vreg) continue; 1023 if (!c->predicate_p0 || !c->predicate_not) continue; 1024 found = (int)i; 1025 } 1026 return found; 1027 } 1028 1029 static int sim_self_test(void) { 1030 int failed = 0; 1031 uint8_t code[CODE_CAP]; 1032 size_t len = 0; 1033 uint32_t gpr = 0; 1034 uint32_t out[LANE_WORDS]; 1035 #define SCHECK(cond, ...) \ 1036 do { \ 1037 if (!(cond)) { \ 1038 failed++; \ 1039 printf("FAIL: "); \ 1040 printf(__VA_ARGS__); \ 1041 printf("\n"); \ 1042 } \ 1043 } while (0) 1044 1045 SCHECK(build(NULL, 0, N_FIXED, 1, code, sizeof code, &len, &gpr) == 0, "overhead encode"); 1046 SCHECK(check_loop_sass("ovh32", code, g_prog.count, 1) == 0, "overhead sass"); 1047 SCHECK(lat_sim(out) == 0, "overhead sim ran"); 1048 SCHECK(out[2] == N_FIXED && out[3] == 1u, "overhead loop count is %u (aux 0x%x), want %u", out[2], out[3], N_FIXED); 1049 SCHECK(sim_lanes_match(N_FIXED, 1u, 0), "overhead count on every lane"); 1050 { 1051 int at = loop_iadd_index(); 1052 SCHECK(at >= 0, "overhead loop increment is in the IR"); 1053 if (at >= 0) { 1054 uint32_t saved = g_prog.insns[at].control; 1055 g_prog.insns[at].control = (saved & ~(15u << 9)) | (1u << 9); 1056 uint32_t bad[LANE_WORDS] = {0}; 1057 SCHECK(lat_sim(bad) == 0, "short-stall overhead sim ran"); 1058 SCHECK(bad[2] != N_FIXED, "stall 1 on the increment must not store the trip count (got %u)", bad[2]); 1059 g_prog.insns[at].control = saved; 1060 } 1061 } 1062 SCHECK(build(NULL, 0, 1, 1, code, sizeof code, &len, &gpr) == 0, "one-trip overhead encode"); 1063 SCHECK(lat_sim(out) == 0 && out[2] == 1u && sim_lanes_match(1u, 1u, 0), "one-trip overhead stored %u", out[2]); 1064 1065 for (int ci = 0; ci < NCASES; ci++) { 1066 int hmma = !strcmp(CASES[ci].id, "hmma_reader"); 1067 uint32_t fresh = 0, fr_aux = 0, stale = 0, st_aux = 0; 1068 SCHECK(eval_case(&CASES[ci], 0, &fresh, &fr_aux) == 0, "%s fresh", CASES[ci].id); 1069 SCHECK(eval_case(&CASES[ci], 1, &stale, &st_aux) == 0, "%s stale", CASES[ci].id); 1070 SCHECK(build(&CASES[ci], CASES[ci].sweep, 0, 0, code, sizeof code, &len, &gpr) == 0, "%s stall %u encode", 1071 CASES[ci].id, CASES[ci].sweep); 1072 SCHECK(check_loop_sass(CASES[ci].id, code, g_prog.count, hmma ? 0 : 1) == 0, "%s loop sass", CASES[ci].id); 1073 SCHECK(lat_sim(out) == 0, "%s stall %u sim", CASES[ci].id, CASES[ci].sweep); 1074 SCHECK(out[2] == fresh && out[3] == fr_aux, "%s stall %u sim result 0x%x aux 0x%x, fresh 0x%x aux 0x%x", 1075 CASES[ci].id, CASES[ci].sweep, out[2], out[3], fresh, fr_aux); 1076 SCHECK(sim_lanes_match(fresh, fr_aux, hmma), "%s stall %u every lane", CASES[ci].id, CASES[ci].sweep); 1077 SCHECK(build(&CASES[ci], 1, 0, 0, code, sizeof code, &len, &gpr) == 0, "%s stall 1 encode", CASES[ci].id); 1078 SCHECK(lat_sim(out) == 0, "%s stall 1 sim", CASES[ci].id); 1079 SCHECK(out[2] == stale && out[3] == st_aux, "%s stall 1 sim result 0x%x aux 0x%x, stale 0x%x aux 0x%x", 1080 CASES[ci].id, out[2], out[3], stale, st_aux); 1081 SCHECK(sim_lanes_match(stale, st_aux, hmma), "%s stall 1 every lane", CASES[ci].id); 1082 } 1083 #undef SCHECK 1084 return failed; 1085 } 1086 1087 static int lane_field_self_test(void); 1088 1089 static int self_test(void) { 1090 int failed = 0; 1091 uint8_t code[CODE_CAP]; 1092 size_t len = 0; 1093 uint32_t gpr = 0; 1094 #define CHECK(cond, ...) \ 1095 do { \ 1096 if (!(cond)) { \ 1097 failed++; \ 1098 printf("FAIL: "); \ 1099 printf(__VA_ARGS__); \ 1100 printf("\n"); \ 1101 } \ 1102 } while (0) 1103 CHECK(build(&CASES[0], 0, 0, 0, code, sizeof code, &len, &gpr) != 0, "stall 0 must be refused"); 1104 CHECK(build(&CASES[0], 99, 0, 0, code, sizeof code, &len, &gpr) != 0, "stall past the sweep must be refused"); 1105 for (int ci = 0; ci < NCASES; ci++) { 1106 if (CASES[ci].href) continue; 1107 uint32_t good = 0, bad = 0, ga = 0, ba = 0; 1108 CHECK(eval_case(&CASES[ci], 0, &good, &ga) == 0, "%s fresh oracle", CASES[ci].id); 1109 CHECK(eval_case(&CASES[ci], 1, &bad, &ba) == 0, "%s stale oracle", CASES[ci].id); 1110 CHECK(good != bad || ga != ba, "%s stale read is distinguishable (fresh 0x%x stale 0x%x)", CASES[ci].id, good, bad); 1111 } 1112 { 1113 uint32_t good = 0, aux = 0; 1114 CHECK(eval_case(&CASES[0], 0, &good, &aux) == 0 && good == (N_FIXED * (N_FIXED + 1u)) / 2u, "alu sum"); 1115 CHECK(eval_case(&CASES[1], 0, &good, &aux) == 0 && good == fbits((float)(N_FIXED * (N_FIXED + 1u)) / 2u), "fma sum"); 1116 } 1117 { 1118 uint32_t er = 0, ea = 0; 1119 float fifteen = 0.f; 1120 for (int k = 0; k < 15; k++) fifteen += 1.f; 1121 CHECK(hmma_16816_ones() == fbits(16.f), "hmma ones dot is 16.0f (got 0x%x)", hmma_16816_ones()); 1122 CHECK(fbits(fifteen) != hmma_16816_ones(), "a 15-term dot must not match k=16"); 1123 CHECK(expect_abs(&CASES[6], &er, &ea) == 0 && er == hmma_16816_ones() && ea == er, "hmma oracle"); 1124 } 1125 1126 for (int ci = 0; ci < NCASES; ci++) { 1127 unsigned stall = 1u; 1128 len = 0; 1129 int rc = build(&CASES[ci], stall, 0, 0, code, sizeof code, &len, &gpr); 1130 CHECK(rc == 0, "%s encode rc %d", CASES[ci].id, rc); 1131 if (rc != 0) continue; 1132 int clocks = 0, s2r_tid = 0; 1133 size_t ninsn = g_prog.count; 1134 for (size_t i = 0; i < ninsn; i++) { 1135 unsigned opc = opc_of(code + i * 16); 1136 unsigned idx = (rd32(code + i * 16 + 8) >> 8) & 0xffu; 1137 if (opc == CS2R_OPC && idx == CLOCK_LO) clocks++; 1138 if (opc == S2R_OPC && idx == BW_SR_TID_X) s2r_tid++; 1139 } 1140 CHECK(clocks == 2, "%s clock sites %d", CASES[ci].id, clocks); 1141 CHECK(s2r_tid == 1, "%s tid read %d", CASES[ci].id, s2r_tid); 1142 failed += check_lane_stores(CASES[ci].id, code, ninsn); 1143 char *text = NULL; 1144 size_t tlen = 0; 1145 FILE *mf = open_memstream(&text, &tlen); 1146 int unk = mf ? omega_sm121_listing(code, len, mf) : -1; 1147 if (mf) fclose(mf); 1148 CHECK(unk == 2, "%s disasm unknowns %d (the two CS2R sites)", CASES[ci].id, unk); 1149 failed += check_raw_window(CASES[ci].id, code, ninsn, stall); 1150 if (!strcmp(CASES[ci].id, "hmma_reader")) { 1151 int quads = 0; 1152 for (size_t i = 0; i + 4 < ninsn; i++) { 1153 if (opc_of(code + i * 16) != 0x23cu || stall_of(code + i * 16) != 1u) continue; 1154 unsigned d = reg_dst(code + i * 16); 1155 int ok = 1; 1156 for (int k = 0; k < 4; k++) { 1157 const uint8_t *f = code + (i + 1 + (size_t)k) * 16; 1158 if (opc_of(f) != 0x221u || reg_a(f) != d + (unsigned)k) ok = 0; 1159 } 1160 if (ok) quads++; 1161 } 1162 CHECK(quads == 1, "hmma stall 1 is followed by FADD of d0,d1,d2,d3"); 1163 len = 0; 1164 rc = build(&CASES[ci], 29u, 0, 0, code, sizeof code, &len, &gpr); 1165 CHECK(rc == 0, "hmma stall 29 encode"); 1166 ninsn = g_prog.count; 1167 CHECK(count_opc_stall(code, ninsn, 0x23c, 15) == 1, "hmma stall 15"); 1168 CHECK(count_opc_stall(code, ninsn, 0x918, 14) == 1, "hmma padding nop stall 14 (15+14=29)"); 1169 int saw = 0; 1170 for (size_t i = 0; i + 2 < ninsn; i++) { 1171 if (opc_of(code + i * 16) == 0x23c && stall_of(code + i * 16) == 15 && 1172 opc_of(code + (i + 1) * 16) == 0x918 && stall_of(code + (i + 1) * 16) == 14 && 1173 opc_of(code + (i + 2) * 16) == 0x221) { 1174 unsigned dst = (rd32(code + i * 16) >> 16) & 0xffu; 1175 unsigned src = (rd32(code + (i + 2) * 16) >> 24) & 0xffu; 1176 CHECK(src == dst, "FADD reads HMMA d0 after the 29-cycle gap"); 1177 saw = 1; 1178 } 1179 } 1180 CHECK(saw, "hmma, nop, fadd sequence"); 1181 /* stall 19 is HMMA stall 15 plus one NOP of stall 4: the gap the chip 1182 * can fail without faulting. The self-test only checks the shape. */ 1183 len = 0; 1184 CHECK(build(&CASES[ci], 19u, 0, 0, code, sizeof code, &len, &gpr) == 0, "hmma stall 19 encodes"); 1185 CHECK(count_opc_stall(code, g_prog.count, 0x23c, 15) == 1, "hmma 19 head"); 1186 CHECK(count_opc_stall(code, g_prog.count, 0x918, 4) == 1, "hmma 19 tail nop"); 1187 } 1188 for (size_t i = 0; i < ninsn; i++) { 1189 if (opc_of(code + i * 16) == CS2R_OPC) { 1190 unsigned guard = (rd32(code + i * 16) >> 12) & 0xfu; 1191 CHECK(guard == 7, "%s CS2R guard %u", CASES[ci].id, guard); 1192 } 1193 } 1194 free(text); 1195 } 1196 len = 0; 1197 CHECK(build(NULL, 0, 64, 1, code, sizeof code, &len, &gpr) == 0, "overhead kernel"); 1198 failed += sim_self_test(); 1199 failed += lane_field_self_test(); 1200 1201 int pass[RUNS][SWEEP_MAX]; 1202 memset(pass, 0, sizeof pass); 1203 for (int r = 0; r < RUNS; r++) 1204 for (int s = 5; s <= 15; s++) pass[r][s - 1] = 1; 1205 /* declared below */ 1206 #undef CHECK 1207 printf("gpu_latmeas_test --self-test %s (%d checks failed)\n", failed ? "FAIL" : "PASS", failed); 1208 return failed ? 1 : 0; 1209 } 1210 1211 /* The suffix helper is tested from main after self_test's static copy. */ 1212 static int required_stall(const int pass[][SWEEP_MAX], int runs, int sweep) { 1213 for (int s = 1; s <= sweep; s++) { 1214 int ok = 1; 1215 for (int t = s; t <= sweep && ok; t++) 1216 for (int r = 0; r < runs && ok; r++) 1217 if (!pass[r][t - 1]) ok = 0; 1218 if (ok) return s; 1219 } 1220 return -1; 1221 } 1222 /* 1 means every stall matched, which is not a measured threshold. -1 means no suffix did. */ 1223 static int reported_threshold(const int pass[][SWEEP_MAX], int runs, int sweep) { 1224 int start = required_stall(pass, runs, sweep); 1225 return start > 1 ? start : -1; 1226 } 1227 1228 static int self_test_suffix(void) { 1229 int failed = 0; 1230 int pass[RUNS][SWEEP_MAX]; 1231 memset(pass, 0, sizeof pass); 1232 for (int r = 0; r < RUNS; r++) 1233 for (int s = 5; s <= 15; s++) pass[r][s - 1] = 1; 1234 if (required_stall(pass, RUNS, 15) != 5) { 1235 printf("FAIL: suffix threshold want 5\n"); 1236 failed++; 1237 } 1238 pass[3][4] = 0; /* run 3 fails stall 5; 6..15 still pass */ 1239 if (required_stall(pass, RUNS, 15) != 6) { 1240 printf("FAIL: one-run hole must move the threshold to 6\n"); 1241 failed++; 1242 } 1243 memset(pass, 0, sizeof pass); 1244 if (required_stall(pass, RUNS, 15) != -1) { 1245 printf("FAIL: empty sweep must not invent a stall\n"); 1246 failed++; 1247 } 1248 for (int r = 0; r < RUNS; r++) 1249 for (int s = 1; s <= 15; s++) pass[r][s - 1] = 1; 1250 if (reported_threshold(pass, RUNS, 15) != -1) { 1251 printf("FAIL: a sweep that passes at stall 1 is not a measured threshold\n"); 1252 failed++; 1253 } 1254 return failed; 1255 } 1256 1257 static int plan_launches(void) { 1258 int n = 0; 1259 int seen64 = 0, seen32 = 0, seen1 = 0; 1260 for (int i = 0; i < NCASES; i++) { 1261 n += RUNS * (int)CASES[i].sweep; 1262 if (CASES[i].iters == 64) seen64 = 1; 1263 if (CASES[i].iters == 32) seen32 = 1; 1264 if (CASES[i].iters == 1) seen1 = 1; 1265 } 1266 n += RUNS * (seen64 + seen32 + seen1); 1267 return n; 1268 } 1269 1270 static void print_plan(void) { 1271 int n = plan_launches(); 1272 unsigned upper_s = (unsigned)((n * 50 + 999) / 1000); 1273 printf("latmeas plan: %d cases, %d runs, sweep 1..15 (HMMA 1..36), %d launches\n", NCASES, RUNS, n); 1274 printf("estimated chip window: %u s upper bound (%d launches; 50 ms each covers a cold shader invalidate). Usually shorter.\n", 1275 upper_s, n); 1276 printf("The runner wraps only the launch with: flock /tmp/aien-gb10.lock timeout 600\n"); 1277 printf("One warp, 32 threads. Clock: CS2R idx 0x50. Mesa %s\n", MESA_COMMIT); 1278 } 1279 1280 static int mkdir_p(const char *dir); 1281 1282 /* fopen("w") creates the file mode 0666. These outputs are not secrets, and they are not world-writable. */ 1283 static FILE *fopen_0644(const char *path, const char *mode) { 1284 int fd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0644); 1285 if (fd < 0) return NULL; 1286 FILE *f = fdopen(fd, mode); 1287 if (!f) close(fd); 1288 return f; 1289 } 1290 1291 /* The manifest name is chosen from the case table, not copied out of the file. 1292 * The scanned token is only compared. The name and stall written here come from 1293 * that table, so the path passed to fopen is not the fscanf text. */ 1294 static int canonical_kernel(const char *scanned, unsigned stall, char name[64], unsigned *stall_out) { 1295 if (!scanned || !name || !stall_out) return 0; 1296 name[0] = 0; 1297 for (int i = 0; i < NCASES; i++) { 1298 if (strcmp(scanned, CASES[i].id) != 0) continue; 1299 for (unsigned s = 1; s <= CASES[i].sweep; s++) { 1300 if (s != stall) continue; 1301 snprintf(name, 64, "%s", CASES[i].id); 1302 *stall_out = s; 1303 return 1; 1304 } 1305 return 0; 1306 } 1307 for (int i = 0; i < NCASES; i++) { 1308 char ovh[32]; 1309 snprintf(ovh, sizeof ovh, "ovh%u", CASES[i].iters); 1310 if (strcmp(scanned, ovh) != 0 || stall != 0) continue; 1311 snprintf(name, 64, "ovh%u", CASES[i].iters); 1312 *stall_out = 0; 1313 return 1; 1314 } 1315 return 0; 1316 } 1317 1318 static int emit_kernels(const char *dir) { 1319 if (mkdir_p(dir) != 0) return -1; 1320 char path[512], manp[512]; 1321 snprintf(manp, sizeof manp, "%s/manifest.txt", dir); 1322 FILE *man = fopen_0644(manp, "w"); 1323 if (!man) return -1; 1324 uint8_t code[CODE_CAP]; 1325 for (int ci = 0; ci < NCASES; ci++) { 1326 uint32_t expv = 0, auxv = 1; 1327 const char *mode = CASES[ci].href ? "ref" : "abs"; 1328 if (!CASES[ci].href && expect_abs(&CASES[ci], &expv, &auxv) != 0) { 1329 fclose(man); 1330 return -1; 1331 } 1332 for (unsigned s = 1; s <= CASES[ci].sweep; s++) { 1333 size_t len = 0; 1334 uint32_t gpr = 0; 1335 if (build(&CASES[ci], s, 0, 0, code, sizeof code, &len, &gpr) != 0) { 1336 fprintf(stderr, "emit %s stall %u failed\n", CASES[ci].id, s); 1337 fclose(man); 1338 return -1; 1339 } 1340 snprintf(path, sizeof path, "%s/%s_%u.bin", dir, CASES[ci].id, s); 1341 FILE *f = fopen_0644(path, "wb"); 1342 if (!f || fwrite(code, 1, len, f) != len) { 1343 if (f) fclose(f); 1344 fclose(man); 1345 return -1; 1346 } 1347 fclose(f); 1348 fprintf(man, "%s %u %s %u %u %u %u %u %zu\n", CASES[ci].id, s, mode, expv, auxv, CASES[ci].iters, 1349 CASES[ci].ops, gpr, len); 1350 } 1351 } 1352 unsigned ovh_iters[8]; 1353 int novh = 0; 1354 for (int ci = 0; ci < NCASES; ci++) { 1355 int seen = 0; 1356 for (int j = 0; j < novh; j++) 1357 if (ovh_iters[j] == CASES[ci].iters) seen = 1; 1358 if (!seen && novh < 8) ovh_iters[novh++] = CASES[ci].iters; 1359 } 1360 for (int k = 0; k < novh; k++) { 1361 size_t len = 0; 1362 uint32_t gpr = 0; 1363 if (build(NULL, 0, ovh_iters[k], 1, code, sizeof code, &len, &gpr) != 0) { 1364 fclose(man); 1365 return -1; 1366 } 1367 snprintf(path, sizeof path, "%s/ovh%u_0.bin", dir, ovh_iters[k]); 1368 FILE *f = fopen_0644(path, "wb"); 1369 if (!f || fwrite(code, 1, len, f) != len) { 1370 if (f) fclose(f); 1371 fclose(man); 1372 return -1; 1373 } 1374 fclose(f); 1375 fprintf(man, "ovh%u 0 abs %u 1 %u 0 %u %zu\n", ovh_iters[k], ovh_iters[k], ovh_iters[k], gpr, len); 1376 } 1377 if (fclose(man) != 0) return -1; 1378 printf("emitted kernels in %s (%d launches when run)\n", dir, plan_launches()); 1379 return 0; 1380 } 1381 1382 static int mkdir_p(const char *dir) { 1383 if (!dir || !dir[0]) return -1; 1384 if (mkdir(dir, 0755) == 0 || errno == EEXIST) return 0; 1385 return -1; 1386 } 1387 1388 static int cmp_u64(const void *a, const void *b) { 1389 uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; 1390 return (x > y) - (x < y); 1391 } 1392 static uint64_t median_spread(uint64_t *v, int n, uint64_t *spread) { 1393 if (n <= 0) { 1394 *spread = 0; 1395 return 0; 1396 } 1397 qsort(v, (size_t)n, sizeof *v, cmp_u64); 1398 *spread = v[n - 1] - v[0]; 1399 return v[n / 2]; 1400 } 1401 1402 typedef struct { 1403 char id[64]; 1404 unsigned stall, iters, ops, gpr; 1405 char mode[8]; 1406 uint32_t expect, aux; 1407 size_t nbytes; 1408 uint8_t *code; 1409 } Kern; 1410 1411 static Kern *load_manifest(const char *dir, int *nout) { 1412 char manp[512]; 1413 snprintf(manp, sizeof manp, "%s/manifest.txt", dir); 1414 FILE *man = fopen(manp, "r"); 1415 if (!man) return NULL; 1416 int cap = 256, n = 0; 1417 Kern *k = calloc((size_t)cap, sizeof *k); 1418 if (!k) { 1419 fclose(man); 1420 return NULL; 1421 } 1422 while (1) { 1423 Kern t; 1424 memset(&t, 0, sizeof t); 1425 unsigned stall, expv, auxv, iters, ops, gpr; 1426 size_t nbytes = 0; 1427 int got = fscanf(man, "%63s %u %7s %u %u %u %u %u %zu\n", t.id, &stall, t.mode, &expv, &auxv, &iters, &ops, 1428 &gpr, &nbytes); 1429 if (got != 9) break; 1430 if (n == cap) { 1431 cap *= 2; 1432 Kern *g = realloc(k, (size_t)cap * sizeof *k); 1433 if (!g) { 1434 free(k); 1435 fclose(man); 1436 return NULL; 1437 } 1438 k = g; 1439 } 1440 t.stall = stall; 1441 t.expect = expv; 1442 t.aux = auxv; 1443 t.iters = iters; 1444 t.ops = ops; 1445 t.gpr = gpr; 1446 t.nbytes = nbytes; 1447 char name[64]; 1448 unsigned stall_used = 0; 1449 if (!canonical_kernel(t.id, stall, name, &stall_used) || nbytes == 0 || nbytes > CODE_CAP) { 1450 free(k); 1451 fclose(man); 1452 return NULL; 1453 } 1454 snprintf(t.id, sizeof t.id, "%s", name); 1455 t.stall = stall_used; 1456 char path[512]; 1457 snprintf(path, sizeof path, "%s/%s_%u.bin", dir, name, stall_used); 1458 FILE *f = fopen(path, "rb"); 1459 t.code = malloc(CODE_CAP); 1460 if (!f || !t.code || fread(t.code, 1, nbytes, f) != nbytes) { 1461 if (f) fclose(f); 1462 free(t.code); 1463 free(k); 1464 fclose(man); 1465 return NULL; 1466 } 1467 fclose(f); 1468 k[n++] = t; 1469 } 1470 fclose(man); 1471 *nout = n; 1472 return k; 1473 } 1474 1475 static Kern *find_kern(Kern *k, int n, const char *id, unsigned stall) { 1476 for (int i = 0; i < n; i++) 1477 if (!strcmp(k[i].id, id) && k[i].stall == stall) return &k[i]; 1478 return NULL; 1479 } 1480 1481 static void read_driver(char *dst, size_t n) { 1482 dst[0] = 0; 1483 FILE *f = fopen("/proc/driver/nvidia/version", "r"); 1484 if (!f) return; 1485 if (!fgets(dst, (int)n, f)) dst[0] = 0; 1486 fclose(f); 1487 size_t L = strlen(dst); 1488 while (L && (dst[L - 1] == '\n' || dst[L - 1] == '\r')) dst[--L] = 0; 1489 } 1490 1491 /* mem holds LANE_N * LANE_WORDS words. Cycles come from lane 0's two clocks. 1492 * Returns -3 when a lane's id slot is not that lane (the store address is wrong). */ 1493 static int launch_kern(const Kern *k, OmegaGpuMem *code, OmegaGpuMem *buf, uint32_t *mem, uint64_t *cycles) { 1494 if (k->nbytes > code->size) return -1; 1495 memset(code->cpu, 0, code->size); 1496 memcpy(code->cpu, k->code, k->nbytes); 1497 uint32_t *w = buf->cpu; 1498 for (int i = 0; i < LANE_N * LANE_WORDS; i++) w[i] = POISON; 1499 if (omega_gpu_session_sync_to_gpu(code) || omega_gpu_session_sync_to_gpu(buf)) return -1; 1500 uint32_t args[2] = {(uint32_t)buf->va, (uint32_t)(buf->va >> 32)}; 1501 OmegaGpuLaunch L = {.code_va = code->va, 1502 .gpr_count = k->gpr, 1503 .threads_x = 32, 1504 .threads_y = 1, 1505 .grid_x = 1, 1506 .grid_y = 1, 1507 .num_elements = 4, 1508 .args = args, 1509 .n_args = 2, 1510 .timeout_ms = 3000, 1511 .spin_us = 200000, 1512 .target_sm = 121}; 1513 if (omega_gpu_session_launch(&L, NULL, NULL) != 0) return -1; 1514 if (omega_gpu_session_sync_to_cpu(buf) != 0) return -1; 1515 memcpy(mem, buf->cpu, sizeof(uint32_t) * LANE_N * LANE_WORDS); 1516 for (int lane = 0; lane < LANE_N; lane++) 1517 if (mem[(size_t)lane * LANE_WORDS + 6] != (uint32_t)lane) return -3; 1518 uint32_t t0 = mem[0], t1 = mem[1]; 1519 if (t0 == POISON && t1 == POISON) return -2; 1520 *cycles = t1 >= t0 ? (uint64_t)t1 - t0 : (1ull << 32) + t1 - t0; 1521 return 0; 1522 } 1523 1524 /* Host oracle for the four stored result words (lane record words 2..5). 1525 * HMMA: d0..d3, each the 16-term ones dot. Other cases: result, aux, 0, 0. */ 1526 static void oracle_words(const CaseDef *c, uint32_t o[4]) { 1527 uint32_t r = 0, a = 0; 1528 o[0] = o[1] = o[2] = o[3] = 0; 1529 if (!c || expect_abs(c, &r, &a) != 0) return; 1530 int four = !strcmp(c->id, "hmma_reader"); 1531 o[0] = r; 1532 o[1] = a; 1533 o[2] = four ? r : 0; 1534 o[3] = four ? r : 0; 1535 } 1536 1537 typedef struct { 1538 int run; /* -1 when this stall never mismatched */ 1539 int lane; 1540 int word; /* 2..5, the lane-record index */ 1541 uint32_t got, want; 1542 } LaneMiss; 1543 1544 /* How many lanes match o[0..3] in words 2..5. The first failure, if any, is written to *miss (lane stays -1 when every lane matches). */ 1545 static int lanes_report(const uint32_t *mem, const uint32_t o[4], LaneMiss *miss) { 1546 int n = 0; 1547 miss->run = -1; 1548 miss->lane = -1; 1549 miss->word = 0; 1550 miss->got = miss->want = 0; 1551 for (int lane = 0; lane < LANE_N; lane++) { 1552 const uint32_t *w = mem + (size_t)lane * LANE_WORDS; 1553 int bad = 0; 1554 for (int k = 0; k < 4; k++) { 1555 if (w[2 + k] == o[k]) continue; 1556 if (miss->lane < 0) { 1557 miss->lane = lane; 1558 miss->word = 2 + k; 1559 miss->got = w[2 + k]; 1560 miss->want = o[k]; 1561 } 1562 bad = 1; 1563 } 1564 if (!bad) n++; 1565 } 1566 return n; 1567 } 1568 1569 /* out[0..3] count lanes whose words 2, 3, 4 and 5 differ from the oracle. */ 1570 static void word_fail_counts(const uint32_t *mem, const uint32_t o[4], uint8_t out[4]) { 1571 out[0] = out[1] = out[2] = out[3] = 0; 1572 for (int lane = 0; lane < LANE_N; lane++) { 1573 const uint32_t *w = mem + (size_t)lane * LANE_WORDS; 1574 for (int k = 0; k < 4; k++) 1575 if (w[2 + k] != o[k] && out[k] < 255) out[k]++; 1576 } 1577 } 1578 1579 static uint32_t lane_fail_bits(const uint32_t *mem, const uint32_t o[4]) { 1580 uint32_t mask = 0; 1581 for (int lane = 0; lane < LANE_N; lane++) { 1582 const uint32_t *w = mem + (size_t)lane * LANE_WORDS; 1583 for (int k = 0; k < 4; k++) 1584 if (w[2 + k] != o[k]) { 1585 mask |= 1u << lane; 1586 break; 1587 } 1588 } 1589 return mask; 1590 } 1591 1592 static int mem_match(const uint32_t *mem, uint32_t result, uint32_t aux, int four) { 1593 uint32_t o[4] = {result, aux, four ? result : 0u, four ? result : 0u}; 1594 LaneMiss m; 1595 return lanes_report(mem, o, &m) == LANE_N; 1596 } 1597 1598 static void fprint_lane_fields(FILE *jf, const uint32_t o[4], const uint8_t *matched_min, const uint32_t *fail_mask, 1599 const uint8_t *word_max, const LaneMiss *miss, int sweep, int have) { 1600 fprintf(jf, " \"oracle\": [%u, %u, %u, %u],\n", o[0], o[1], o[2], o[3]); 1601 fprintf(jf, " \"reference_result\": %u,\n", o[0]); 1602 fprintf(jf, " \"lanes_matched_min\": ["); 1603 for (int s = 0; s < sweep; s++) { 1604 if (!have || matched_min[s] == 0xff) 1605 fprintf(jf, "%snull", s ? ", " : ""); 1606 else 1607 fprintf(jf, "%s%u", s ? ", " : "", matched_min[s]); 1608 } 1609 fprintf(jf, "],\n \"lane_fail_mask\": ["); 1610 for (int s = 0; s < sweep; s++) { 1611 if (!have || matched_min[s] == 0xff) 1612 fprintf(jf, "%snull", s ? ", " : ""); 1613 else 1614 fprintf(jf, "%s\"0x%08x\"", s ? ", " : "", fail_mask[s]); 1615 } 1616 fprintf(jf, "],\n \"word_mismatch_max\": ["); 1617 for (int s = 0; s < sweep; s++) { 1618 const uint8_t *w = word_max + (size_t)s * 4; 1619 if (!have || matched_min[s] == 0xff) 1620 fprintf(jf, "%snull", s ? ", " : ""); 1621 else 1622 fprintf(jf, "%s[%u, %u, %u, %u]", s ? ", " : "", w[0], w[1], w[2], w[3]); 1623 } 1624 fprintf(jf, "],\n \"first_mismatch\": ["); 1625 for (int s = 0; s < sweep; s++) { 1626 if (!have || matched_min[s] == 0xff || miss[s].lane < 0) 1627 fprintf(jf, "%snull", s ? ", " : ""); 1628 else 1629 fprintf(jf, 1630 "%s{\"run\": %d, \"lane\": %d, \"word\": %d, \"got\": %u, \"want\": %u}", 1631 s ? ", " : "", miss[s].run, miss[s].lane, miss[s].word, miss[s].got, miss[s].want); 1632 } 1633 fprintf(jf, "],\n"); 1634 } 1635 1636 static int lane_field_self_test(void) { 1637 int failed = 0; 1638 #define LCHECK(cond, ...) \ 1639 do { \ 1640 if (!(cond)) { \ 1641 failed++; \ 1642 printf("FAIL: "); \ 1643 printf(__VA_ARGS__); \ 1644 printf("\n"); \ 1645 } \ 1646 } while (0) 1647 uint32_t o[4]; 1648 oracle_words(&CASES[6], o); 1649 LCHECK(o[0] == hmma_16816_ones() && o[1] == o[0] && o[2] == o[0] && o[3] == o[0], 1650 "hmma oracle is four destination words"); 1651 oracle_words(&CASES[0], o); 1652 LCHECK(o[0] == (N_FIXED * (N_FIXED + 1u)) / 2u && o[1] == 1u && o[2] == 0u && o[3] == 0u, "alu oracle words"); 1653 1654 uint32_t mem[LANE_N * LANE_WORDS]; 1655 memset(mem, 0, sizeof mem); 1656 for (int lane = 0; lane < LANE_N; lane++) { 1657 mem[(size_t)lane * LANE_WORDS + 2] = o[0]; 1658 mem[(size_t)lane * LANE_WORDS + 3] = o[1]; 1659 mem[(size_t)lane * LANE_WORDS + 4] = o[2]; 1660 mem[(size_t)lane * LANE_WORDS + 5] = o[3]; 1661 mem[(size_t)lane * LANE_WORDS + 6] = (uint32_t)lane; 1662 } 1663 LaneMiss m; 1664 LCHECK(lanes_report(mem, o, &m) == LANE_N && m.lane < 0, "all lanes match the oracle"); 1665 mem[7u * LANE_WORDS + 4] = 0x11111111u; 1666 LCHECK(lanes_report(mem, o, &m) == LANE_N - 1 && m.lane == 7 && m.word == 4 && m.got == 0x11111111u && m.want == o[2], 1667 "a planted word-4 mismatch names lane 7"); 1668 LCHECK(lane_fail_bits(mem, o) == (1u << 7), "fail mask is lane 7"); 1669 1670 uint8_t wc[4]; 1671 word_fail_counts(mem, o, wc); 1672 LCHECK(wc[0] == 0 && wc[1] == 0 && wc[2] == 1 && wc[3] == 0, "only word 4 mismatches"); 1673 uint8_t mins[2] = {31, 32}; 1674 uint32_t masks[2] = {1u << 7, 0}; 1675 uint8_t words[2][4] = {{0, 0, 1, 0}, {0, 0, 0, 0}}; 1676 LaneMiss ms[2]; 1677 ms[0] = m; 1678 ms[0].run = 3; 1679 ms[1].lane = -1; 1680 char *text = NULL; 1681 size_t tlen = 0; 1682 FILE *mf = open_memstream(&text, &tlen); 1683 if (mf) { 1684 fprint_lane_fields(mf, o, mins, masks, (const uint8_t *)words, ms, 2, 1); 1685 fclose(mf); 1686 } 1687 LCHECK(text && strstr(text, "\"oracle\": [") && strstr(text, "\"lanes_matched_min\": [31, 32]") && 1688 strstr(text, "\"lane_fail_mask\"") && strstr(text, "\"lane\": 7") && strstr(text, "\"word\": 4") && 1689 strstr(text, "\"run\": 3") && strstr(text, "\"word_mismatch_max\": [[0, 0, 1, 0], [0, 0, 0, 0]]") && 1690 strstr(text, ", null]"), 1691 "json lane fields: %s", text ? text : "(none)"); 1692 free(text); 1693 1694 uint8_t code[CODE_CAP]; 1695 size_t len = 0; 1696 uint32_t gpr = 0, lane0[LANE_WORDS]; 1697 LCHECK(build(&CASES[0], CASES[0].sweep, 0, 0, code, sizeof code, &len, &gpr) == 0, "alu top encode"); 1698 LCHECK(lat_sim(lane0) == 0, "alu top sim"); 1699 oracle_words(&CASES[0], o); 1700 LCHECK(lanes_report(g_sim_mem, o, &m) == LANE_N && m.lane < 0, "sim stall 15 matches on every lane"); 1701 LCHECK(build(&CASES[0], 1, 0, 0, code, sizeof code, &len, &gpr) == 0, "alu stall 1 encode"); 1702 LCHECK(lat_sim(lane0) == 0, "alu stall 1 sim"); 1703 LCHECK(lanes_report(g_sim_mem, o, &m) == 0 && m.lane == 0 && m.word == 2, "sim stall 1 fails word 2 on lane 0"); 1704 oracle_words(&CASES[6], o); 1705 LCHECK(build(&CASES[6], 1, 0, 0, code, sizeof code, &len, &gpr) == 0, "hmma stall 1 encode"); 1706 LCHECK(lat_sim(lane0) == 0, "hmma stall 1 sim"); 1707 LCHECK(lanes_report(g_sim_mem, o, &m) == 0 && m.word >= 2 && m.word <= 5, "hmma stall 1 fails a destination word"); 1708 char tmpl[] = "/tmp/latmeas-load-XXXXXX"; 1709 char *dump = mkdtemp(tmpl); 1710 LCHECK(dump != NULL, "load-check directory"); 1711 if (dump) { 1712 int nk = 0; 1713 LCHECK(emit_kernels(dump) == 0, "emit for the load check"); 1714 Kern *kk = load_manifest(dump, &nk); 1715 int files = 0, ns = 0; 1716 unsigned seen[8]; 1717 for (int i = 0; i < NCASES; i++) { 1718 files += (int)CASES[i].sweep; 1719 int have = 0; 1720 for (int j = 0; j < ns; j++) 1721 if (seen[j] == CASES[i].iters) have = 1; 1722 if (!have && ns < 8) seen[ns++] = CASES[i].iters; 1723 } 1724 files += ns; 1725 LCHECK(kk && nk == files, "manifest load count %d want %d", nk, files); 1726 LCHECK(kk && find_kern(kk, nk, "alu_alu", 1) && find_kern(kk, nk, "hmma_reader", 20) && find_kern(kk, nk, "ovh32", 0), 1727 "loaded kernel names"); 1728 if (kk) { 1729 for (int i = 0; i < nk; i++) free(kk[i].code); 1730 free(kk); 1731 } 1732 DIR *dd = opendir(dump); 1733 if (dd) { 1734 struct dirent *de; 1735 while ((de = readdir(dd)) != NULL) { 1736 if (de->d_name[0] == '.') continue; 1737 char p[768]; 1738 snprintf(p, sizeof p, "%s/%s", dump, de->d_name); 1739 unlink(p); 1740 } 1741 closedir(dd); 1742 } 1743 rmdir(dump); 1744 } 1745 #undef LCHECK 1746 return failed; 1747 } 1748 1749 static void json_escape(const char *s, char *dst, size_t n) { 1750 size_t j = 0; 1751 if (!s) s = ""; 1752 for (size_t i = 0; s[i] && j + 2 < n; i++) { 1753 unsigned char c = (unsigned char)s[i]; 1754 if (c == '"' || c == '\\') { 1755 if (j + 3 >= n) break; 1756 dst[j++] = '\\'; 1757 } 1758 if (c < 32) c = ' '; 1759 dst[j++] = (char)c; 1760 } 1761 dst[j] = 0; 1762 } 1763 1764 static int run_launch(const char *kdir, const char *out_path) { 1765 int nk = 0; 1766 Kern *kerns = load_manifest(kdir, &nk); 1767 if (!kerns || nk < 1) { 1768 fprintf(stderr, "no kernels in %s (run --emit first, outside the lock)\n", kdir); 1769 return 2; 1770 } 1771 print_plan(); 1772 char driver[256]; 1773 read_driver(driver, sizeof driver); 1774 struct utsname ut; 1775 if (uname(&ut) != 0) memset(&ut, 0, sizeof ut); 1776 1777 int pass[NCASES][RUNS][SWEEP_MAX]; 1778 uint64_t cyc[NCASES][RUNS][SWEEP_MAX]; 1779 uint64_t ovh_cyc[8][RUNS]; 1780 uint32_t run0_res[NCASES][SWEEP_MAX]; 1781 uint8_t lane_min[NCASES][SWEEP_MAX]; 1782 uint32_t fail_mask[NCASES][SWEEP_MAX]; 1783 uint8_t word_max[NCASES][SWEEP_MAX][4]; 1784 LaneMiss first_miss[NCASES][SWEEP_MAX]; 1785 memset(pass, 0, sizeof pass); 1786 memset(cyc, 0, sizeof cyc); 1787 memset(ovh_cyc, 0, sizeof ovh_cyc); 1788 memset(run0_res, 0, sizeof run0_res); 1789 memset(lane_min, 0xff, sizeof lane_min); 1790 memset(fail_mask, 0, sizeof fail_mask); 1791 memset(word_max, 0, sizeof word_max); 1792 for (int ci = 0; ci < NCASES; ci++) 1793 for (int s = 0; s < SWEEP_MAX; s++) first_miss[ci][s].lane = -1; 1794 char notes[NCASES][384]; 1795 memset(notes, 0, sizeof notes); 1796 unsigned ovh_list[8]; 1797 int novh = 0; 1798 for (int ci = 0; ci < NCASES && novh < 8; ci++) { 1799 int seen = 0; 1800 for (int j = 0; j < novh; j++) 1801 if (ovh_list[j] == CASES[ci].iters) seen = 1; 1802 if (!seen) ovh_list[novh++] = CASES[ci].iters; 1803 } 1804 int device_error = 0; 1805 const char *err = ""; 1806 1807 omega_gpu_session_lock(); 1808 if (!omega_gpu_session_open()) { 1809 err = omega_gpu_session_last_error(); 1810 fprintf(stderr, "GPU not available (%s)\n", err); 1811 device_error = 1; 1812 goto write_json; 1813 } 1814 if (omega_gpu_session_target_sm() != 121) { 1815 err = "opened device is not sm_121"; 1816 fprintf(stderr, "%s (%u)\n", err, omega_gpu_session_target_sm()); 1817 device_error = 1; 1818 goto write_json; 1819 } 1820 OmegaGpuMem code = {0}, buf = {0}; 1821 if (omega_gpu_session_alloc_code(CODE_CAP, &code) != 0 || omega_gpu_session_alloc(4096, &buf) != 0) { 1822 err = omega_gpu_session_last_error(); 1823 device_error = 1; 1824 goto write_json; 1825 } 1826 1827 for (int oi = 0; oi < novh && !device_error; oi++) { 1828 char id[16]; 1829 snprintf(id, sizeof id, "ovh%u", ovh_list[oi]); 1830 Kern *k = find_kern(kerns, nk, id, 0); 1831 if (!k) { 1832 err = "missing overhead kernel"; 1833 device_error = 1; 1834 break; 1835 } 1836 for (int r = 0; r < RUNS; r++) { 1837 uint32_t mem[LANE_N * LANE_WORDS]; 1838 uint64_t cy = 0; 1839 int rc = launch_kern(k, &code, &buf, mem, &cy); 1840 printf("overhead %s run %d/%d rc %d\n", id, r + 1, RUNS, rc); 1841 fflush(stdout); 1842 if (rc != 0) { 1843 err = rc == -3 ? "lane id was not stored" : omega_gpu_session_last_error(); 1844 device_error = 1; 1845 break; 1846 } 1847 ovh_cyc[oi][r] = cy; 1848 if (!mem_match(mem, ovh_list[oi], 1u, 0)) err = "overhead loop count did not match"; 1849 } 1850 } 1851 1852 for (int ci = 0; ci < NCASES && !device_error; ci++) { 1853 const CaseDef *c = &CASES[ci]; 1854 for (int r = 0; r < RUNS && !device_error; r++) { 1855 printf("case %s run %d/%d\n", c->id, r + 1, RUNS); 1856 fflush(stdout); 1857 for (unsigned s = 1; s <= c->sweep && !device_error; s++) { 1858 Kern *k = find_kern(kerns, nk, c->id, s); 1859 uint32_t mem[LANE_N * LANE_WORDS]; 1860 uint64_t cy = 0; 1861 int rc = !k ? -1 : launch_kern(k, &code, &buf, mem, &cy); 1862 if (rc != 0) { 1863 err = rc == -3 ? "lane id was not stored" : (k ? omega_gpu_session_last_error() : "missing kernel"); 1864 device_error = 1; 1865 break; 1866 } 1867 cyc[ci][r][s - 1] = cy; 1868 uint32_t oracle[4]; 1869 oracle_words(c, oracle); 1870 LaneMiss one; 1871 int nmatch = lanes_report(mem, oracle, &one); 1872 if (lane_min[ci][s - 1] == 0xff || nmatch < lane_min[ci][s - 1]) lane_min[ci][s - 1] = (uint8_t)nmatch; 1873 fail_mask[ci][s - 1] |= lane_fail_bits(mem, oracle); 1874 { 1875 uint8_t wc[4]; 1876 word_fail_counts(mem, oracle, wc); 1877 for (int k = 0; k < 4; k++) 1878 if (wc[k] > word_max[ci][s - 1][k]) word_max[ci][s - 1][k] = wc[k]; 1879 } 1880 if (nmatch < LANE_N && first_miss[ci][s - 1].lane < 0) { 1881 one.run = r; 1882 first_miss[ci][s - 1] = one; 1883 } 1884 pass[ci][r][s - 1] = nmatch == LANE_N; 1885 if (r == 0) run0_res[ci][s - 1] = mem[2]; 1886 } 1887 } 1888 } 1889 1890 omega_gpu_session_free(&code); 1891 omega_gpu_session_free(&buf); 1892 1893 write_json:; 1894 int locked = 1; 1895 char tmp[512]; 1896 snprintf(tmp, sizeof tmp, "%s.tmp", out_path); 1897 FILE *jf = fopen_0644(tmp, "w"); 1898 if (!jf) { 1899 if (locked) { 1900 omega_gpu_session_unlock(); 1901 omega_gpu_session_close(); 1902 } 1903 free(kerns); 1904 return 1; 1905 } 1906 char err_esc[512], drv_esc[512], mach_esc[128]; 1907 json_escape(err, err_esc, sizeof err_esc); 1908 json_escape(driver, drv_esc, sizeof drv_esc); 1909 json_escape(ut.machine, mach_esc, sizeof mach_esc); 1910 fprintf(jf, 1911 "{\n \"kind\": \"cudapp-latmeas\",\n \"synthetic\": false,\n \"measured\": %s,\n \"mesa_commit\": \"%s\",\n" 1912 " \"driver\": \"%s\",\n \"machine\": \"%s\",\n \"runs\": %d,\n \"threads\": 32,\n \"warps\": 1,\n" 1913 " \"clock\": \"CS2R idx 0x50 (NAK_SV_CLOCK_LO)\",\n \"launches\": %d,\n \"error\": \"%s\",\n \"cases\": {\n", 1914 device_error ? "false" : "true", MESA_COMMIT, drv_esc, mach_esc, RUNS, plan_launches(), err_esc); 1915 for (int ci = 0; ci < NCASES; ci++) { 1916 const CaseDef *c = &CASES[ci]; 1917 int req = device_error ? -1 : reported_threshold(pass[ci], RUNS, (int)c->sweep); 1918 int spread_s = 0; 1919 int smin = 1000, smax = -1, nthr = 0; 1920 if (!device_error) { 1921 for (int r = 0; r < RUNS; r++) { 1922 int one[1][SWEEP_MAX]; 1923 memcpy(one[0], pass[ci][r], sizeof one[0]); 1924 int row = reported_threshold(one, 1, (int)c->sweep); 1925 if (row > 1) { 1926 if (row < smin) smin = row; 1927 if (row > smax) smax = row; 1928 nthr++; 1929 } 1930 } 1931 if (nthr == RUNS) spread_s = smax - smin; 1932 } 1933 if (!device_error && req < 0 && notes[ci][0] == 0) { 1934 int any_fail = 0; 1935 for (unsigned s = 1; s <= c->sweep && !any_fail; s++) 1936 for (int r = 0; r < RUNS; r++) 1937 if (!pass[ci][r][s - 1]) any_fail = 1; 1938 snprintf(notes[ci], sizeof notes[ci], 1939 any_fail ? "no stall was correct through the top of the sweep" 1940 : "every stall matched the oracle; threshold is at or below 1"); 1941 } 1942 uint64_t samples[RUNS]; 1943 uint64_t cmed = 0, cspread = 0; 1944 int have_cyc = 0; 1945 if (req > 0) { 1946 for (int r = 0; r < RUNS; r++) samples[r] = cyc[ci][r][req - 1]; 1947 uint64_t copy[RUNS]; 1948 memcpy(copy, samples, sizeof copy); 1949 cmed = median_spread(copy, RUNS, &cspread); 1950 have_cyc = 1; 1951 for (int r = 0; r < RUNS; r++) 1952 if (samples[r] == 0) have_cyc = 0; 1953 } 1954 int oi = 0; 1955 for (int k = 0; k < novh; k++) 1956 if (ovh_list[k] == c->iters) oi = k; 1957 uint64_t oc[RUNS]; 1958 memcpy(oc, ovh_cyc[oi], sizeof oc); 1959 uint64_t ospread = 0; 1960 uint64_t omed = median_spread(oc, RUNS, &ospread); 1961 (void)ospread; 1962 uint64_t s1[RUNS], s1copy[RUNS], s1med = 0, s1spread = 0; 1963 int have_s1 = !device_error; 1964 for (int r = 0; r < RUNS; r++) { 1965 s1[r] = cyc[ci][r][0]; 1966 if (s1[r] == 0) have_s1 = 0; 1967 } 1968 if (have_s1) { 1969 memcpy(s1copy, s1, sizeof s1); 1970 s1med = median_spread(s1copy, RUNS, &s1spread); 1971 } 1972 char note_esc[512]; 1973 json_escape(notes[ci], note_esc, sizeof note_esc); 1974 fprintf(jf, " \"%s\": {\n", c->id); 1975 if (req > 0 && !device_error) 1976 fprintf(jf, " \"stall_required\": %d,\n", req); 1977 else 1978 fprintf(jf, " \"stall_required\": null,\n"); 1979 fprintf(jf, " \"stall_spread\": %d,\n", req > 0 && !device_error ? spread_s : 0); 1980 if (have_cyc && !device_error) { 1981 fprintf(jf, " \"cycle_median\": %llu,\n \"cycle_spread\": %llu,\n", (unsigned long long)cmed, 1982 (unsigned long long)cspread); 1983 } else { 1984 fprintf(jf, " \"cycle_median\": null,\n \"cycle_spread\": null,\n"); 1985 } 1986 if (have_s1) { 1987 fprintf(jf, " \"cycle_median_stall1\": %llu,\n \"cycle_spread_stall1\": %llu,\n", 1988 (unsigned long long)s1med, (unsigned long long)s1spread); 1989 } else { 1990 fprintf(jf, " \"cycle_median_stall1\": null,\n \"cycle_spread_stall1\": null,\n"); 1991 } 1992 if (device_error) 1993 fprintf(jf, " \"overhead_cycle_median\": null,\n"); 1994 else 1995 fprintf(jf, " \"overhead_cycle_median\": %llu,\n", (unsigned long long)omed); 1996 { 1997 uint32_t oracle[4]; 1998 oracle_words(c, oracle); 1999 fprint_lane_fields(jf, oracle, lane_min[ci], fail_mask[ci], (const uint8_t *)word_max[ci], first_miss[ci], 2000 (int)c->sweep, !device_error); 2001 } 2002 fprintf(jf, " \"run0_result\": ["); 2003 for (unsigned s = 1; s <= c->sweep; s++) 2004 fprintf(jf, "%s%u", s == 1 ? "" : ", ", run0_res[ci][s - 1]); 2005 fprintf(jf, "],\n"); 2006 fprintf(jf, " \"iters\": %u,\n \"ops\": %u,\n", c->iters, c->ops); 2007 fprintf(jf, " \"note\": \"%s\"\n }%s\n", note_esc, ci + 1 == NCASES ? "" : ","); 2008 } 2009 fprintf(jf, " }\n}\n"); 2010 if (fclose(jf) != 0) { 2011 omega_gpu_session_unlock(); 2012 omega_gpu_session_close(); 2013 free(kerns); 2014 return 1; 2015 } 2016 if (rename(tmp, out_path) != 0) { 2017 omega_gpu_session_unlock(); 2018 omega_gpu_session_close(); 2019 free(kerns); 2020 return 1; 2021 } 2022 omega_gpu_session_unlock(); 2023 omega_gpu_session_close(); 2024 for (int i = 0; i < nk; i++) free(kerns[i].code); 2025 free(kerns); 2026 printf("wrote %s\n", out_path); 2027 return device_error ? 1 : 0; 2028 } 2029 2030 static void usage(void) { 2031 fprintf(stderr, 2032 "usage:\n" 2033 " gpu_latmeas_test --self-test\n" 2034 " gpu_latmeas_test --print-plan\n" 2035 " gpu_latmeas_test --emit DIR\n" 2036 " gpu_latmeas_test --launch DIR --out raw.json\n"); 2037 } 2038 2039 int main(int argc, char **argv) { 2040 const char *emit = NULL, *launch = NULL, *out = NULL; 2041 int self = 0, plan = 0; 2042 for (int i = 1; i < argc; i++) { 2043 if (!strcmp(argv[i], "--self-test")) self = 1; 2044 else if (!strcmp(argv[i], "--print-plan")) plan = 1; 2045 else if (!strcmp(argv[i], "--emit") && i + 1 < argc) emit = argv[++i]; 2046 else if (!strcmp(argv[i], "--launch") && i + 1 < argc) launch = argv[++i]; 2047 else if (!strcmp(argv[i], "--out") && i + 1 < argc) out = argv[++i]; 2048 else { 2049 usage(); 2050 return 2; 2051 } 2052 } 2053 if (self) { 2054 int f = self_test(); 2055 f += self_test_suffix(); 2056 return f ? 1 : 0; 2057 } 2058 if (plan && !emit && !launch) { 2059 print_plan(); 2060 return 0; 2061 } 2062 if (emit && !launch) return emit_kernels(emit) == 0 ? 0 : 1; 2063 if (launch && out) return run_launch(launch, out); 2064 usage(); 2065 return 2; 2066 }