omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / omega_bw_reconv.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 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 | #ifndef OMEGA_BW_RECONV_H |
| 18 | #define OMEGA_BW_RECONV_H |
| 19 | /* Structured warp reconvergence regions for the Blackwell IR emitters (omega #308). |
| 20 | * |
| 21 | * A region is the span between a BSSY (arm barrier Bn, name the join point) and the |
| 22 | * matching BSYNC (wait for every armed lane, continue as one warp). Inside a region the |
| 23 | * lanes of a warp may take different paths: a predicated forward branch, a loop whose |
| 24 | * trip count differs per lane, a break. Every path that leaves the region jumps to the |
| 25 | * BSYNC itself (that is where nvcc 13.0.88 sends loop exits on sm_121); the BSSY names the |
| 26 | * instruction after the BSYNC. Regions nest; the innermost open region owns Bn with |
| 27 | * n = depth, so nesting depth is bounded by the barrier register count and begin fails |
| 28 | * closed (returns -1) when that bound is reached. |
| 29 | * |
| 30 | * This header holds the bookkeeping only (indices, patching, bounds). Each emitter wraps |
| 31 | * it with its own control-word scheme: see region_begin / region_exit / region_join in |
| 32 | * src/omega_gpu_elementwise_api.c and src/omega_gpu_attention_api.c. |
| 33 | * |
| 34 | * Reference: Mesa NAK nak_nir_lower_cf.c (push_scope / pop_scope: bar_set_nv before a |
| 35 | * divergent if or loop, bar_sync_nv at its merge block, breaks jump to the sync) and the |
| 36 | * nvcc oracle listing in docs/gpu-reconvergence-308.md. */ |
| 37 | |
| 38 | #include "omega_blackwell_codegen.h" |
| 39 | |
| 40 | #define BW_RECONV_MAX_DEPTH BW_RECONV_MAX_BAR |
| 41 | #define BW_RECONV_MAX_EXITS 8 /* forward exits (breaks) recorded per region */ |
| 42 | |
| 43 | typedef struct { |
| 44 | int depth; /* open regions */ |
| 45 | int peak; /* deepest nesting seen (reported in receipts) */ |
| 46 | int opened; /* regions begun (for receipts) */ |
| 47 | int bssy_at[BW_RECONV_MAX_DEPTH]; |
| 48 | int exits[BW_RECONV_MAX_DEPTH][BW_RECONV_MAX_EXITS]; |
| 49 | int n_exits[BW_RECONV_MAX_DEPTH]; |
| 50 | } BwRegions; |
| 51 | |
| 52 | /* Called after the emitter appended the BSSY at index `bssy_index`. Returns the barrier |
| 53 | * register to use, or -1 when the nesting bound is reached (the caller must fail the |
| 54 | * build: no code with a missing barrier may be emitted). The caller allocates the |
| 55 | * barrier BEFORE emitting: use bw_regions_next_bar to pick it, then bw_regions_begin. */ |
| 56 | static inline int bw_regions_next_bar(const BwRegions *r) { |
| 57 | return r->depth < BW_RECONV_MAX_DEPTH ? r->depth : -1; |
| 58 | } |
| 59 | static inline int bw_regions_begin(BwRegions *r, int bssy_index) { |
| 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 | } |
| 68 | |
| 69 | /* Records a forward branch (already appended at `bra_index`) that leaves the innermost |
| 70 | * open region; its target is patched to the BSYNC by bw_regions_join. -1 when no region |
| 71 | * is open or the per-region exit table is full. */ |
| 72 | static inline int bw_regions_exit(BwRegions *r, int bra_index) { |
| 73 | if (r->depth <= 0 || bra_index < 0) return -1; |
| 74 | int d = r->depth - 1; |
| 75 | if (r->n_exits[d] >= BW_RECONV_MAX_EXITS) return -1; |
| 76 | r->exits[d][r->n_exits[d]++] = bra_index; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /* Called after the emitter appended the BSYNC at index `bsync_index` for the innermost |
| 81 | * open region. Patches the BSSY to name the instruction after the BSYNC and every |
| 82 | * recorded exit to jump to the BSYNC. Returns the barrier register closed, or -1 when |
| 83 | * no region is open or an index is inconsistent (BSSY must precede the BSYNC). */ |
| 84 | static inline int bw_regions_join(BwRegions *r, BlackwellIRProgram *p, int bsync_index) { |
| 85 | if (r->depth <= 0 || !p || bsync_index < 0 || (size_t)bsync_index >= p->count) return -1; |
| 86 | int d = --r->depth; |
| 87 | int at = r->bssy_at[d]; |
| 88 | if (at < 0 || at >= bsync_index || p->insns[at].op != BW_IR_BSSY || p->insns[bsync_index].op != BW_IR_BSYNC) return -1; |
| 89 | if (p->insns[at].bar_reg != (uint8_t)d || p->insns[bsync_index].bar_reg != (uint8_t)d) return -1; |
| 90 | p->insns[at].imm = (uint32_t)(bsync_index + 1 - at); |
| 91 | for (int i = 0; i < r->n_exits[d]; i++) { |
| 92 | int b = r->exits[d][i]; |
| 93 | if (b <= at || b >= bsync_index || p->insns[b].op != BW_IR_BRA) return -1; |
| 94 | p->insns[b].imm = (uint32_t)(bsync_index - b); |
| 95 | } |
| 96 | return d; |
| 97 | } |
| 98 | |
| 99 | #endif /* OMEGA_BW_RECONV_H */ |