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_gpu_om_ir.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* ARCH-0042 ABI/replay dispatch shell; wire admission and lifetime policy are seeded Omega. */ |
| 3 | #include "omega_gpu_om_ir.h" |
| 4 | |
| 5 | #include <string.h> |
| 6 | |
| 7 | #include "cudapp_native.h" |
| 8 | #include "omega_bw_fields.h" |
| 9 | #ifdef OMEGA_GPU_NO_POSIX |
| 10 | #include "omega_native.h" |
| 11 | #define omega_ra_live omega2_ra_live |
| 12 | #endif |
| 13 | |
| 14 | #define NONE64 UINT64_MAX |
| 15 | |
| 16 | #ifdef CUDAPP_MIGRATION_PENDING_C_CODEGEN_BASE |
| 17 | static int field(uint64_t w, int *out) { |
| 18 | if (w == NONE64) { *out = -1; return 0; } |
| 19 | if (w >= (uint64_t)BW_MAX_VREGS) return -1; |
| 20 | *out = (int)w; |
| 21 | return 0; |
| 22 | } |
| 23 | #endif |
| 24 | |
| 25 | /* Math-library data generation is outside the NO_POSIX engine boundary. */ |
| 26 | #ifndef OMEGA_GPU_NO_POSIX |
| 27 | static int call_unit(uint64_t kind, uint64_t a, uint64_t b, uint64_t c, uint64_t *ins, uint64_t *meta, |
| 28 | uint64_t *result) { |
| 29 | *result = 0; |
| 30 | if (omega_mathlibs_ir(result, kind, a, b, c, ins, OMEGA_OM_IR_WORDS, meta, OMEGA_OM_IR_META) != OMEGA_OK) |
| 31 | return OMEGA_OM_IR_BRIDGE; |
| 32 | if (*result == 1) return OMEGA_OM_IR_REFUSED; |
| 33 | if (*result != 0) return OMEGA_OM_IR_EMIT; |
| 34 | return OMEGA_OM_IR_OK; |
| 35 | } |
| 36 | |
| 37 | int omega_gpu_om_ir_data(uint64_t kind, uint64_t a, uint64_t b, uint64_t c, uint64_t *words) { |
| 38 | static uint64_t meta[OMEGA_OM_IR_META]; |
| 39 | uint64_t result; |
| 40 | if (!words) return OMEGA_OM_IR_REPLAY; |
| 41 | memset(words, 0, OMEGA_OM_IR_WORDS * sizeof *words); |
| 42 | return call_unit(kind, a, b, c, words, meta, &result); |
| 43 | } |
| 44 | #endif |
| 45 | |
| 46 | #ifdef CUDAPP_MIGRATION_PENDING_C_CODEGEN_BASE |
| 47 | /* The elementwise emitter's check (src/omega_gpu_elementwise_api.c check_loop_invariant): no value first defined |
| 48 | * inside a loop may be read after the loop before it is redefined. */ |
| 49 | static int loop_invariant_ok(const BlackwellIRProgram *p, const uint64_t *meta) { |
| 50 | const OmegaRegAlloc *ra = &p->regalloc; |
| 51 | for (uint64_t L = 0; L < meta[5]; L++) { |
| 52 | int start = (int)meta[16 + 2 * L], end = (int)meta[17 + 2 * L]; |
| 53 | for (int v = 0; v < ra->num_vregs; v++) { |
| 54 | int fd = ra->intervals[v].first_def; |
| 55 | if (fd < start || fd > end) continue; |
| 56 | for (size_t i = (size_t)end + 1; i < p->count; i++) { |
| 57 | const BlackwellIRInsn *in = &p->insns[i]; |
| 58 | if (in->src1_vreg == v || in->src2_vreg == v || in->src3_vreg == v) return 0; |
| 59 | if (in->dst_vreg == v && !in->is_uniform) break; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | /* A value live into a loop and read inside it is read again by every later iteration: hold it to the closing BRA |
| 67 | * (the elementwise emitter's loop_end fixup). */ |
| 68 | static void extend_loop(BlackwellIRProgram *p, int start, int end) { |
| 69 | for (int v = 0; v < p->regalloc.num_vregs; v++) { |
| 70 | OmegaLiveInterval *iv = &p->regalloc.intervals[v]; |
| 71 | if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) |
| 72 | iv->last_use = end; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | int omega_gpu_om_ir_replay(OmegaOmIrUnit unit, uint64_t kind, uint64_t a, uint64_t b, uint64_t c, |
| 77 | BlackwellIRProgram *prog, uint64_t *emit_err) { |
| 78 | uint64_t ins[OMEGA_OM_IR_WORDS] = {0}, meta[OMEGA_OM_IR_META] = {0}; |
| 79 | uint64_t result = 0; |
| 80 | if (emit_err) *emit_err = 0; |
| 81 | if (!unit || !prog) return OMEGA_OM_IR_REPLAY; |
| 82 | uint32_t status = unit(&result, kind, a, b, c, ins, OMEGA_OM_IR_WORDS, meta, OMEGA_OM_IR_META); |
| 83 | if (emit_err) *emit_err = meta[4]; |
| 84 | if (status != OMEGA_OK) return OMEGA_OM_IR_BRIDGE; |
| 85 | if (result == 1) return OMEGA_OM_IR_REFUSED; |
| 86 | if (result != 0) return OMEGA_OM_IR_EMIT; |
| 87 | uint64_t n = meta[0]; |
| 88 | if (n == 0 || n > BW_MAX_IR_INSNS || meta[1] > BW_MAX_VREGS || meta[2] > BW_MAX_UVREGS || meta[5] > 24) |
| 89 | return OMEGA_OM_IR_REPLAY; |
| 90 | if (meta[6] != 0 && |
| 91 | (meta[6] <= (uint64_t)BW_PHYS_GPR_MAX || meta[6] > (uint64_t)BW_PHYS_GPR_HW_LIMIT)) |
| 92 | return OMEGA_OM_IR_REPLAY; |
| 93 | if (meta[7] != 0) { |
| 94 | uint64_t check = 0; |
| 95 | if (omega_ra_live(&check, 0, meta[7], meta[1], 0, 0, 0) != 0) |
| 96 | return OMEGA_OM_IR_BRIDGE; |
| 97 | if (check != 0) return (int)check; |
| 98 | } |
| 99 | |
| 100 | omega_bw_ir_init(prog); |
| 101 | if (meta[6] != 0 && omega_bw_ir_set_gpr_limit(prog, (uint32_t)meta[6]) != 0) |
| 102 | return OMEGA_OM_IR_REPLAY; |
| 103 | for (uint64_t v = 0; v < meta[1]; v++) { |
| 104 | uint64_t k = meta[64 + v]; |
| 105 | int got = k == 4 ? omega_bw_ir_alloc_vreg128(prog) : |
| 106 | k == 2 ? omega_bw_ir_alloc_vreg64(prog) : k == 1 ? omega_bw_ir_alloc_vreg(prog) : -1; |
| 107 | if (got != (int)v) return OMEGA_OM_IR_REPLAY; |
| 108 | } |
| 109 | for (uint64_t u = 0; u < meta[2]; u++) { |
| 110 | uint64_t k = meta[320 + u]; |
| 111 | int got = k == 2 ? omega_bw_ir_alloc_uvreg64(prog) : k == 1 ? omega_bw_ir_alloc_uvreg(prog) : -1; |
| 112 | if (got != (int)u) return OMEGA_OM_IR_REPLAY; |
| 113 | } |
| 114 | |
| 115 | uint64_t next_loop = 0; |
| 116 | for (uint64_t i = 0; i < n; i++) { |
| 117 | const uint64_t *r = &ins[i * 8]; |
| 118 | BlackwellIRInsn in; |
| 119 | memset(&in, 0, sizeof in); |
| 120 | /* Append-only enum includes binary64 through BW_IR_S32_F64 (79). |
| 121 | * Keep one bound: the boundary gate exercises LAST and LAST+1. */ |
| 122 | if (r[0] > (uint64_t)BW_IR_LAST) return OMEGA_OM_IR_REPLAY; |
| 123 | in.op = (BlackwellIROpcode)r[0]; |
| 124 | if (field(r[1], &in.dst_vreg) || field(r[2], &in.src1_vreg) || field(r[3], &in.src2_vreg) || |
| 125 | field(r[4], &in.src3_vreg)) |
| 126 | return OMEGA_OM_IR_REPLAY; |
| 127 | if (r[5] == NONE64) in.ureg = -1; |
| 128 | else if (r[5] < (uint64_t)BW_MAX_UVREGS) in.ureg = (int)r[5]; |
| 129 | else return OMEGA_OM_IR_REPLAY; |
| 130 | in.imm = (uint32_t)r[6]; |
| 131 | in.control = (uint32_t)(r[6] >> 32); |
| 132 | in.is_uniform = (r[7] & 1u) != 0; |
| 133 | in.predicate_p0 = (r[7] & 2u) != 0; |
| 134 | in.predicate_not = (r[7] & 4u) != 0; |
| 135 | in.dst_subreg = (uint8_t)(r[7] >> 16); |
| 136 | in.src1_subreg = (uint8_t)(r[7] >> 24); |
| 137 | in.src2_subreg = (uint8_t)(r[7] >> 32); |
| 138 | in.src3_subreg = (uint8_t)(r[7] >> 40); |
| 139 | in.bar_reg = (uint8_t)(r[7] >> 48); |
| 140 | if (omega_bw_ir_append(prog, &in) < 0) return OMEGA_OM_IR_REPLAY; |
| 141 | while (next_loop < meta[5] && meta[17 + 2 * next_loop] == i) { |
| 142 | extend_loop(prog, (int)meta[16 + 2 * next_loop], (int)i); |
| 143 | next_loop++; |
| 144 | } |
| 145 | } |
| 146 | if (next_loop != meta[5]) return OMEGA_OM_IR_REPLAY; |
| 147 | if (meta[7] != 0) { |
| 148 | for (int v = 0; v < prog->regalloc.num_vregs; ++v) { |
| 149 | uint64_t hint = meta[352 + v], check = 0; |
| 150 | OmegaLiveInterval *iv = &prog->regalloc.intervals[v]; |
| 151 | if (omega_ra_live(&check, 1, hint, (uint64_t)iv->first_def, |
| 152 | (uint64_t)iv->last_use, n, (uint64_t)v) != 0) |
| 153 | return OMEGA_OM_IR_BRIDGE; |
| 154 | if (check != 0) return (int)check; |
| 155 | if (hint != 0) iv->last_use = (int)hint; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if (omega_bw_regalloc_solve(prog) != 0) return OMEGA_OM_IR_REGALLOC; |
| 160 | if (!loop_invariant_ok(prog, meta)) return OMEGA_OM_IR_LOOP; |
| 161 | return OMEGA_OM_IR_OK; |
| 162 | } |
| 163 | #else |
| 164 | int omega_gpu_om_ir_replay(OmegaOmIrUnit unit, uint64_t kind, uint64_t a, uint64_t b, uint64_t c, |
| 165 | BlackwellIRProgram *prog, uint64_t *emit_err) { |
| 166 | uint64_t ins[OMEGA_OM_IR_WORDS] = {0}, meta[OMEGA_OM_IR_META] = {0}, life[512] = {0}; |
| 167 | uint64_t result = 0; |
| 168 | if (emit_err) *emit_err = 0; |
| 169 | if (!unit || !prog) return OMEGA_OM_IR_REPLAY; |
| 170 | uint32_t status = unit(&result, kind, a, b, c, ins, OMEGA_OM_IR_WORDS, meta, OMEGA_OM_IR_META); |
| 171 | if (emit_err) *emit_err = meta[4]; |
| 172 | if (status != OMEGA_OK) return OMEGA_OM_IR_BRIDGE; |
| 173 | if (result == 1) return OMEGA_OM_IR_REFUSED; |
| 174 | if (result != 0) return OMEGA_OM_IR_EMIT; |
| 175 | if (omega_cg_replay(&result, ins, OMEGA_OM_IR_WORDS, meta, OMEGA_OM_IR_META, |
| 176 | BW_IR_LAST) != OMEGA_OK) return OMEGA_OM_IR_BRIDGE; |
| 177 | if (result != 0) return (int)result; |
| 178 | omega_bw_ir_init(prog); |
| 179 | if (meta[6] != 0 && omega_bw_ir_set_gpr_limit(prog, (uint32_t)meta[6]) != 0) |
| 180 | return OMEGA_OM_IR_REPLAY; |
| 181 | for (uint64_t v = 0; v < meta[1]; ++v) { |
| 182 | uint64_t width = meta[64 + v]; |
| 183 | int got = width == 4 ? omega_bw_ir_alloc_vreg128(prog) : |
| 184 | width == 2 ? omega_bw_ir_alloc_vreg64(prog) : omega_bw_ir_alloc_vreg(prog); |
| 185 | if (got != (int)v) return OMEGA_OM_IR_REPLAY; |
| 186 | } |
| 187 | for (uint64_t u = 0; u < meta[2]; ++u) { |
| 188 | int got = meta[320 + u] == 2 ? omega_bw_ir_alloc_uvreg64(prog) : omega_bw_ir_alloc_uvreg(prog); |
| 189 | if (got != (int)u) return OMEGA_OM_IR_REPLAY; |
| 190 | } |
| 191 | for (uint64_t i = 0; i < meta[0]; ++i) { |
| 192 | BlackwellIRInsn in = {0}; |
| 193 | if (omega_bw_fields_unpack(&ins[i * 8], &in) != 0) return OMEGA_OM_IR_BRIDGE; |
| 194 | if (omega_bw_ir_append(prog, &in) < 0) return OMEGA_OM_IR_REPLAY; |
| 195 | } |
| 196 | for (int v = 0; v < prog->regalloc.num_vregs; ++v) { |
| 197 | life[2 * v] = (uint64_t)(int64_t)prog->regalloc.intervals[v].first_def; |
| 198 | life[2 * v + 1] = (uint64_t)(int64_t)prog->regalloc.intervals[v].last_use; |
| 199 | } |
| 200 | if (omega_cg_life(&result, meta, OMEGA_OM_IR_META, life, 512) != OMEGA_OK) return OMEGA_OM_IR_BRIDGE; |
| 201 | if (result != 0) return (int)result; |
| 202 | for (int v = 0; v < prog->regalloc.num_vregs; ++v) |
| 203 | prog->regalloc.intervals[v].last_use = (int)(int64_t)life[2 * v + 1]; |
| 204 | if (omega_bw_regalloc_solve(prog) != 0) return OMEGA_OM_IR_REGALLOC; |
| 205 | if (omega_cg_invar(&result, ins, OMEGA_OM_IR_WORDS, meta, OMEGA_OM_IR_META, life, 512) != OMEGA_OK) |
| 206 | return OMEGA_OM_IR_BRIDGE; |
| 207 | return (int)result; |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | #ifndef OMEGA_GPU_NO_POSIX |
| 212 | int omega_gpu_om_ir_build(uint64_t kind, uint64_t a, uint64_t b, uint64_t c, uint32_t threads, |
| 213 | BlackwellIRProgram *prog, uint8_t *code, size_t code_cap, |
| 214 | OmegaBlackwellKernel *kernel, uint32_t *declared, uint64_t *emit_err) { |
| 215 | if (!prog || !code || !kernel || !declared || threads == 0) return OMEGA_OM_IR_REPLAY; |
| 216 | int rc = omega_gpu_om_ir_replay(omega_mathlibs_ir, kind, a, b, c, prog, emit_err); |
| 217 | if (rc != OMEGA_OM_IR_OK) return rc; |
| 218 | uint32_t peak = prog->regalloc.peak_gpr_usage; |
| 219 | #ifdef CUDAPP_MIGRATION_PENDING_C_CODEGEN_BASE |
| 220 | uint32_t decl = omega_bw_gpr_declared(peak); |
| 221 | if (decl < 64u) decl = 64u; |
| 222 | if (peak > BW_PHYS_GPR_MAX || !omega_bw_gpr_declared_ok(decl, peak) || (uint64_t)decl * threads > 65536u) |
| 223 | return OMEGA_OM_IR_BUDGET; |
| 224 | #else |
| 225 | uint64_t budget = 0; |
| 226 | if (omega_cg_budget(&budget, peak, omega_bw_gpr_declared(peak), threads) != OMEGA_OK) |
| 227 | return OMEGA_OM_IR_BRIDGE; |
| 228 | if (budget == 0) return OMEGA_OM_IR_BUDGET; |
| 229 | uint32_t decl = (uint32_t)budget; |
| 230 | #endif |
| 231 | if (omega_blackwell_kernel_encode_into(prog, code, code_cap, kernel) != 0) return OMEGA_OM_IR_ENCODE; |
| 232 | *declared = decl; |
| 233 | return OMEGA_OM_IR_OK; |
| 234 | } |
| 235 | #endif |
| 236 | |
| 237 | const char *omega_gpu_om_ir_rc_name(int rc) { |
| 238 | switch (rc) { |
| 239 | case OMEGA_OM_IR_OK: return "ok"; |
| 240 | case OMEGA_OM_IR_REFUSED: return "refused"; |
| 241 | case OMEGA_OM_IR_EMIT: return "emit"; |
| 242 | case OMEGA_OM_IR_BRIDGE: return "bridge"; |
| 243 | case OMEGA_OM_IR_REPLAY: return "replay"; |
| 244 | case OMEGA_OM_IR_REGALLOC: return "regalloc"; |
| 245 | case OMEGA_OM_IR_LOOP: return "loop"; |
| 246 | case OMEGA_OM_IR_BUDGET: return "budget"; |
| 247 | case OMEGA_OM_IR_ENCODE: return "encode"; |
| 248 | case OMEGA_OM_IR_LIVE_FLAG: return "liveness-flag"; |
| 249 | case OMEGA_OM_IR_LIVE_RANGE: return "liveness-range"; |
| 250 | default: return "?"; |
| 251 | } |
| 252 | } |