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 / src / omega_bw_hazard.h
| 1 | /* omega_bw_hazard.h: static scoreboard hazard checker over encoded sm_121 kernel bytes (omega-mv#554). |
| 2 | * |
| 3 | * The warp simulators run the IR and model no scoreboards, so a dropped wait mask passes every host test and only |
| 4 | * shows on the chip (omega-mv#545: the encoder wrote a fixed word 3 for IADD3, the wait on the Q loads' read barrier |
| 5 | * was lost and a MOV zeroed the address of 16 queued LDG.E.128; Xid 13). This checker reads the bytes the chip runs, |
| 6 | * after every encode and patch step, and walks the control flow with the scoreboard rules: |
| 7 | * |
| 8 | * RAW a reader of a register written by a variable-latency op must have waited on that op's write barrier; |
| 9 | * WAR a writer of a register still being read by a queued load or store must have waited on its read barrier |
| 10 | * (or on its write barrier: an op that has completed has read its sources); |
| 11 | * WAW a writer of a register a variable-latency op has not yet written must have waited on that op's write |
| 12 | * barrier (the late result would land over the new value). |
| 13 | * |
| 14 | * Control word (word 3 of each 16-byte instruction, the same layout as CW() in omega_bw_em.h): stall bits 9..12, |
| 15 | * yield 13, write barrier 14..16, read barrier 17..19 (7 = none), wait mask 20..25. A wait on barrier n releases every |
| 16 | * register any outstanding op on n holds (the barrier is a counter that reaches zero). The analysis is a may-analysis |
| 17 | * over all paths (branches, predicated EXIT/BRA, loops) to a fixed point; a predicated instruction may or may not |
| 18 | * write, and its wait mask is taken as applied (UNKNOWN whether the chip applies the wait of a predicated-off |
| 19 | * instruction; if it does not, this checker can miss a hazard behind such an instruction). |
| 20 | * |
| 21 | * Limits, said out loud: fixed-latency hazards (stall counts too short) are not checked; predicate registers are not |
| 22 | * tracked; HMMA/QMMA are tracked only through the barriers their bytes declare. NVIDIA documents none of the control |
| 23 | * field; the layout is our own decode, cross-checked against nvdisasm on every kernel (tests/bw_hazard_test.c). */ |
| 24 | #ifndef OMEGA_BW_HAZARD_H |
| 25 | #define OMEGA_BW_HAZARD_H |
| 26 | |
| 27 | #include <stddef.h> |
| 28 | #include <stdint.h> |
| 29 | |
| 30 | #define OMEGA_BW_RZ 0xffu /* register 255 is RZ, uniform 255 is URZ: never tracked */ |
| 31 | #define OMEGA_BW_NOBAR 7u |
| 32 | |
| 33 | enum { OMEGA_BW_FLOW_NEXT = 0, OMEGA_BW_FLOW_EXIT, OMEGA_BW_FLOW_BRA }; |
| 34 | |
| 35 | typedef struct { |
| 36 | uint8_t reg; /* first register */ |
| 37 | uint8_t n; /* 1, 2 or 4 consecutive registers */ |
| 38 | uint8_t uniform; /* 1 = uniform register file */ |
| 39 | } OmegaBwOperand; |
| 40 | |
| 41 | typedef struct { |
| 42 | uint16_t op; /* word 0 bits 0..11 */ |
| 43 | uint8_t guard; /* word 0 bits 12..15: 7 = always (PT) */ |
| 44 | uint8_t stall, yield, wbar, rbar, wait; |
| 45 | uint8_t var; /* variable-latency writer: its result needs a write barrier */ |
| 46 | uint8_t late; /* reads its sources late (loads, stores, atomics, constant loads) */ |
| 47 | uint8_t nr, nw; |
| 48 | OmegaBwOperand rd[5], wr[1]; |
| 49 | uint8_t flow; /* OMEGA_BW_FLOW_* */ |
| 50 | int32_t target; /* FLOW_BRA: target instruction index relative to this one */ |
| 51 | const char *name; |
| 52 | } OmegaBwInsn; |
| 53 | |
| 54 | /* Decodes one 16-byte instruction. 0 ok, -1 an opcode or form this decoder does not know (the kernel is then |
| 55 | * reported as not checked, never as clean). */ |
| 56 | int omega_bw_decode(const uint8_t b[16], OmegaBwInsn *d); |
| 57 | |
| 58 | enum { OMEGA_BW_HZ_RAW = 1, OMEGA_BW_HZ_WAR = 2, OMEGA_BW_HZ_WAW = 3 }; |
| 59 | |
| 60 | typedef struct { |
| 61 | uint32_t pc; /* instruction index of the offending reader / writer */ |
| 62 | uint32_t src_pc; /* the last producer / queued reader of the register seen on some path (a hint) */ |
| 63 | uint8_t kind; /* OMEGA_BW_HZ_* */ |
| 64 | uint8_t reg; |
| 65 | uint8_t uniform; |
| 66 | uint8_t release; /* barrier mask whose wait would have released it; 0 = the op declared no barrier at all */ |
| 67 | } OmegaBwHazard; |
| 68 | |
| 69 | #define OMEGA_BW_HAZARD_MAX 256 |
| 70 | |
| 71 | typedef struct { |
| 72 | size_t insns; /* instructions in the buffer */ |
| 73 | size_t reachable; /* reachable from instruction 0 */ |
| 74 | size_t count; /* findings (the first OMEGA_BW_HAZARD_MAX are kept) */ |
| 75 | OmegaBwHazard f[OMEGA_BW_HAZARD_MAX]; |
| 76 | int64_t bad_pc; /* -1, or the first instruction that could not be decoded or branches out of range */ |
| 77 | } OmegaBwHazardReport; |
| 78 | |
| 79 | /* Checks code[0..len). 0 = decoded and walked (r->count findings, possibly 0); -1 = not checked (bad_pc set, or a |
| 80 | * NULL / misaligned argument, or out of memory). Host only; allocates its work state. */ |
| 81 | int omega_bw_hazard_check(const uint8_t *code, size_t len, OmegaBwHazardReport *r); |
| 82 | |
| 83 | const char *omega_bw_hazard_kind(uint8_t kind); |
| 84 | |
| 85 | #endif /* OMEGA_BW_HAZARD_H */ |