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/src/omega_bw_reconv.h # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_bw_reconv.h` 146 lines · 6610 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 #ifndef OMEGA_BW_RECONV_H 3 #define OMEGA_BW_RECONV_H 4 /* Structured warp reconvergence regions for the Blackwell IR emitters (omega #308). 5 * 6 * A region is the span between a BSSY (arm barrier Bn, name the join point) and the 7 * matching BSYNC (wait for every armed lane, continue as one warp). Inside a region the 8 * lanes of a warp may take different paths: a predicated forward branch, a loop whose 9 * trip count differs per lane, a break. Every path that leaves the region jumps to the 10 * BSYNC itself (that is where nvcc 13.0.88 sends loop exits on sm_121); the BSSY names the 11 * instruction after the BSYNC. Regions nest; the innermost open region owns Bn with 12 * n = depth, so nesting depth is bounded by the barrier register count and begin fails 13 * closed (returns -1) when that bound is reached. 14 * 15 * This header holds the bookkeeping only (indices, patching, bounds). Each emitter wraps 16 * it with its own control-word scheme: see region_begin / region_exit / region_join in 17 * src/omega_gpu_elementwise_api.c and src/omega_gpu_attention_api.c. 18 * 19 * Reference: Mesa NAK nak_nir_lower_cf.c (push_scope / pop_scope: bar_set_nv before a 20 * divergent if or loop, bar_sync_nv at its merge block, breaks jump to the sync) and the 21 * nvcc oracle listing in docs/gpu-reconvergence-308.md. */ 22 23 #include "omega_blackwell_codegen.h" 24 #if defined(OMEGA_GPU_NO_POSIX) || defined(CUDAPP_MIGRATION_PENDING_C_RECONV) 25 #define BW_RECONV_C_PRIMARY 1 26 #else 27 #include "cudapp_native.h" 28 static inline int64_t bw_reconv_step(uint64_t act, uint64_t w[8]) { 29 uint64_t out = UINT64_MAX; 30 return omega_cg_reconv(&out, act, w, 8) ? -1 : (int64_t)out; 31 } 32 #endif 33 34 #define BW_RECONV_MAX_DEPTH BW_RECONV_MAX_BAR 35 #define BW_RECONV_MAX_EXITS 8 /* forward exits (breaks) recorded per region */ 36 37 typedef struct { 38 int depth; /* open regions */ 39 int peak; /* deepest nesting seen (reported in receipts) */ 40 int opened; /* regions begun (for receipts) */ 41 int bssy_at[BW_RECONV_MAX_DEPTH]; 42 int exits[BW_RECONV_MAX_DEPTH][BW_RECONV_MAX_EXITS]; 43 int n_exits[BW_RECONV_MAX_DEPTH]; 44 } BwRegions; 45 46 /* Called after the emitter appended the BSSY at index `bssy_index`. Returns the barrier 47 * register to use, or -1 when the nesting bound is reached (the caller must fail the 48 * build: no code with a missing barrier may be emitted). The caller allocates the 49 * barrier BEFORE emitting: use bw_regions_next_bar to pick it, then bw_regions_begin. */ 50 static inline int bw_regions_next_bar(const BwRegions *r) { 51 #ifdef BW_RECONV_C_PRIMARY 52 return r->depth < BW_RECONV_MAX_DEPTH ? r->depth : -1; 53 #else 54 uint64_t w[8] = {(uint32_t)r->depth}; 55 return (int)bw_reconv_step(0, w); 56 #endif 57 } 58 static inline int bw_regions_begin(BwRegions *r, int bssy_index) { 59 #ifdef BW_RECONV_C_PRIMARY 60 if (r->depth >= BW_RECONV_MAX_DEPTH || bssy_index < 0) return -1; 61 r->bssy_at[r->depth] = bssy_index; 62 r->n_exits[r->depth] = 0; 63 r->depth++; 64 r->opened++; 65 if (r->depth > r->peak) r->peak = r->depth; 66 return r->depth - 1; 67 #else 68 uint64_t w[8] = {(uint32_t)r->depth, (uint32_t)r->peak, (uint32_t)r->opened, (uint32_t)bssy_index}; 69 int d = (int)bw_reconv_step(1, w); 70 if (d < 0) return -1; 71 r->bssy_at[d] = bssy_index; r->n_exits[d] = 0; 72 r->depth = (int)w[0]; r->peak = (int)w[1]; r->opened = (int)w[2]; 73 return d; 74 #endif 75 } 76 77 /* Records a forward branch (already appended at `bra_index`) that leaves the innermost 78 * open region; its target is patched to the BSYNC by bw_regions_join. -1 when no region 79 * is open or the per-region exit table is full. */ 80 static inline int bw_regions_exit(BwRegions *r, int bra_index) { 81 #ifdef BW_RECONV_C_PRIMARY 82 if (r->depth <= 0 || bra_index < 0) return -1; 83 int d = r->depth - 1; 84 if (r->n_exits[d] >= BW_RECONV_MAX_EXITS) return -1; 85 r->exits[d][r->n_exits[d]++] = bra_index; 86 return 0; 87 #else 88 uint64_t w[8] = {(uint32_t)r->depth, (uint32_t)bra_index}; 89 int d = (int)bw_reconv_step(2, w); 90 if (d < 0) return -1; 91 w[0] = (uint32_t)r->n_exits[d]; 92 int n = (int)bw_reconv_step(3, w); 93 if (n < 0) return -1; 94 r->exits[d][n] = bra_index; r->n_exits[d] = (int)w[0]; 95 return 0; 96 #endif 97 } 98 99 /* Called after the emitter appended the BSYNC at index `bsync_index` for the innermost 100 * open region. Patches the BSSY to name the instruction after the BSYNC and every 101 * recorded exit to jump to the BSYNC. Returns the barrier register closed, or -1 when 102 * no region is open or an index is inconsistent (BSSY must precede the BSYNC). */ 103 static inline int bw_regions_join(BwRegions *r, BlackwellIRProgram *p, int bsync_index) { 104 #ifdef BW_RECONV_C_PRIMARY 105 if (r->depth <= 0 || !p || bsync_index < 0 || (size_t)bsync_index >= p->count) return -1; 106 int d = --r->depth; 107 int at = r->bssy_at[d]; 108 if (at < 0 || at >= bsync_index || p->insns[at].op != BW_IR_BSSY || p->insns[bsync_index].op != BW_IR_BSYNC) return -1; 109 if (p->insns[at].bar_reg != (uint8_t)d || p->insns[bsync_index].bar_reg != (uint8_t)d) return -1; 110 p->insns[at].imm = (uint32_t)(bsync_index + 1 - at); 111 for (int i = 0; i < r->n_exits[d]; i++) { 112 int b = r->exits[d][i]; 113 if (b <= at || b >= bsync_index || p->insns[b].op != BW_IR_BRA) return -1; 114 p->insns[b].imm = (uint32_t)(bsync_index - b); 115 } 116 return d; 117 #else 118 uint64_t w[8] = {(uint32_t)r->depth, (uint32_t)bsync_index, p ? p->count : 0, p != NULL}; 119 int d = (int)bw_reconv_step(4, w); 120 /* The C oracle decrements before checking the instruction fields. */ 121 r->depth = (int)w[0]; 122 if (d < 0) return -1; 123 int at = r->bssy_at[d]; 124 w[0] = (uint32_t)at; w[1] = (uint32_t)bsync_index; 125 /* Index admission precedes either dereference. */ 126 w[2] = BW_IR_BSSY; w[3] = BW_IR_BSYNC; w[4] = w[5] = w[6] = (uint8_t)d; 127 if (bw_reconv_step(5, w) < 0) return -1; 128 w[2] = p->insns[at].op; w[3] = p->insns[bsync_index].op; 129 w[4] = p->insns[at].bar_reg; w[5] = p->insns[bsync_index].bar_reg; 130 int imm = (int)bw_reconv_step(5, w); 131 if (imm < 0) return -1; 132 p->insns[at].imm = (uint32_t)imm; 133 for (int i = 0; i < r->n_exits[d]; i++) { 134 int b = r->exits[d][i]; 135 w[0] = (uint32_t)b; w[1] = (uint32_t)at; w[2] = (uint32_t)bsync_index; w[3] = BW_IR_BRA; 136 if (bw_reconv_step(6, w) < 0) return -1; 137 w[3] = p->insns[b].op; 138 imm = (int)bw_reconv_step(6, w); 139 if (imm < 0) return -1; 140 p->insns[b].imm = (uint32_t)imm; 141 } 142 return d; 143 #endif 144 } 145 146 #endif /* OMEGA_BW_RECONV_H */