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_om_ir.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Shared unpacker for pure-OM-2 Blackwell IR emitters (om/gpu/cg_*.om). |
| 3 | * Strictly validates every metadata field, register/subregister bound, opcode, |
| 4 | * and loop back-edge before unpacking into BlackwellIRProgram and running |
| 5 | * omega_bw_regalloc_solve. Fails closed on any error or out-of-range field. |
| 6 | */ |
| 7 | #ifndef OMEGA_BW_OM_IR_H |
| 8 | #define OMEGA_BW_OM_IR_H |
| 9 | |
| 10 | #include "omega_blackwell_codegen.h" |
| 11 | #include <string.h> |
| 12 | |
| 13 | static inline int omega_bw_om_check_reg(int64_t r64, uint8_t sub, uint64_t limit, const uint64_t *widths) { |
| 14 | if (r64 == -1) return sub == 0 ? 0 : -1; |
| 15 | if (r64 < 0 || (uint64_t)r64 >= limit) return -1; |
| 16 | uint64_t w = widths[(uint64_t)r64]; |
| 17 | if (sub >= w) return -1; |
| 18 | return 0; |
| 19 | } |
| 20 | |
| 21 | static inline int omega_bw_ir_from_om_limit(const uint64_t ins[8192], const uint64_t meta[512], BlackwellIRProgram *prog, uint32_t gpr_limit) { |
| 22 | if (!prog) return -1; |
| 23 | omega_bw_ir_init(prog); |
| 24 | if (gpr_limit != 0u && omega_bw_ir_set_gpr_limit(prog, gpr_limit) != 0) return -1; |
| 25 | if (!ins || !meta) return -1; |
| 26 | if (meta[5] != 0) return -1; /* emitter recorded an internal overflow or validation error */ |
| 27 | const uint64_t n = meta[0], nv = meta[1], nu = meta[2], nl = meta[3]; |
| 28 | if (n == 0 || n > BW_MAX_IR_INSNS || nv > BW_MAX_VREGS || nu > BW_MAX_UVREGS || nl > 16u) return -1; |
| 29 | for (uint64_t v = 0; v < nv; v++) { |
| 30 | uint64_t w = meta[64u + v]; |
| 31 | if (w != 1u && w != 2u && w != 4u) goto fail; |
| 32 | int r = (w == 4u) ? omega_bw_ir_alloc_vreg128(prog) : (w == 2u) ? omega_bw_ir_alloc_vreg64(prog) : omega_bw_ir_alloc_vreg(prog); |
| 33 | if (r != (int)v) goto fail; |
| 34 | } |
| 35 | for (uint64_t u = 0; u < nu; u++) { |
| 36 | uint64_t w = meta[352u + u]; |
| 37 | if (w != 1u && w != 2u) goto fail; |
| 38 | int r = (w == 2u) ? omega_bw_ir_alloc_uvreg64(prog) : omega_bw_ir_alloc_uvreg(prog); |
| 39 | if (r != (int)u) goto fail; |
| 40 | } |
| 41 | for (uint64_t i = 0; i < n; i++) { |
| 42 | const uint64_t *r = &ins[i * 8u]; |
| 43 | int64_t op64 = (int64_t)r[0]; |
| 44 | if (op64 < 0 || op64 > (int64_t)BW_IR_ATOMG_ADD_F32_STRONG_GPU) goto fail; |
| 45 | if ((r[7] >> 48) != 0) goto fail; |
| 46 | bool is_u = (r[7] & 1u) != 0; |
| 47 | uint8_t dsub = (uint8_t)((r[7] >> 16) & 0xffu); |
| 48 | uint8_t s1sub = (uint8_t)((r[7] >> 24) & 0xffu); |
| 49 | uint8_t s2sub = (uint8_t)((r[7] >> 32) & 0xffu); |
| 50 | uint8_t s3sub = (uint8_t)((r[7] >> 40) & 0xffu); |
| 51 | uint8_t bar = (uint8_t)((r[7] >> 8) & 0xffu); |
| 52 | if (bar >= BW_RECONV_MAX_BAR) goto fail; |
| 53 | if (is_u) { |
| 54 | if (omega_bw_om_check_reg((int64_t)r[1], dsub, nu, &meta[352]) != 0) goto fail; |
| 55 | if (omega_bw_om_check_reg((int64_t)r[2], s1sub, nu, &meta[352]) != 0) goto fail; |
| 56 | if (omega_bw_om_check_reg((int64_t)r[3], s2sub, nu, &meta[352]) != 0) goto fail; |
| 57 | if (omega_bw_om_check_reg((int64_t)r[4], s3sub, nu, &meta[352]) != 0) goto fail; |
| 58 | } else { |
| 59 | if (omega_bw_om_check_reg((int64_t)r[1], dsub, nv, &meta[64]) != 0) goto fail; |
| 60 | if (omega_bw_om_check_reg((int64_t)r[2], s1sub, nv, &meta[64]) != 0) goto fail; |
| 61 | if (omega_bw_om_check_reg((int64_t)r[3], s2sub, nv, &meta[64]) != 0) goto fail; |
| 62 | if (omega_bw_om_check_reg((int64_t)r[4], s3sub, nv, &meta[64]) != 0) goto fail; |
| 63 | } |
| 64 | if (omega_bw_om_check_reg((int64_t)r[5], 0, nu, &meta[352]) != 0) goto fail; |
| 65 | |
| 66 | BlackwellIRInsn in; |
| 67 | memset(&in, 0, sizeof in); |
| 68 | in.op = (BlackwellIROpcode)(int)op64; |
| 69 | in.dst_vreg = (int)(int64_t)r[1]; |
| 70 | in.src1_vreg = (int)(int64_t)r[2]; |
| 71 | in.src2_vreg = (int)(int64_t)r[3]; |
| 72 | in.src3_vreg = (int)(int64_t)r[4]; |
| 73 | in.ureg = (int)(int64_t)r[5]; |
| 74 | in.imm = (uint32_t)(r[6] & 0xffffffffu); |
| 75 | in.control = (uint32_t)(r[6] >> 32); |
| 76 | in.is_uniform = is_u; |
| 77 | in.predicate_p0 = ((r[7] >> 1) & 1u) != 0; |
| 78 | in.predicate_not = ((r[7] >> 2) & 1u) != 0; |
| 79 | in.bar_reg = bar; |
| 80 | in.dst_subreg = dsub; |
| 81 | in.src1_subreg = s1sub; |
| 82 | in.src2_subreg = s2sub; |
| 83 | in.src3_subreg = s3sub; |
| 84 | if (omega_bw_ir_append(prog, &in) < 0) goto fail; |
| 85 | } |
| 86 | for (uint64_t l = 0; l < nl; l++) { |
| 87 | const uint64_t s = meta[320u + 2u * l], e = meta[320u + 2u * l + 1u]; |
| 88 | if (s > e || e >= n) goto fail; |
| 89 | const int start = (int)s, end = (int)e; |
| 90 | OmegaRegAlloc *ra = &prog->regalloc; |
| 91 | for (int v = 0; v < ra->num_vregs; v++) { |
| 92 | OmegaLiveInterval *iv = &ra->intervals[v]; |
| 93 | if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; |
| 94 | } |
| 95 | } |
| 96 | if (omega_bw_regalloc_solve(prog) != 0) goto fail; |
| 97 | return 0; |
| 98 | fail: |
| 99 | omega_bw_ir_init(prog); |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | static inline int omega_bw_ir_from_om(const uint64_t ins[8192], const uint64_t meta[512], BlackwellIRProgram *prog) { |
| 104 | return omega_bw_ir_from_om_limit(ins, meta, prog, 0u); |
| 105 | } |
| 106 | |
| 107 | #endif /* OMEGA_BW_OM_IR_H */ |