interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / evidence / gpu-reconv-308 / T1a-GPU-RECONV-0a1812f / omega_bw_reconv.h

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