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_blackwell_codegen_tern.c
| 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 | /* omega_blackwell_codegen_tern.c: KT1 W2A8 ternary GEMV kernel. |
| 18 | * Emitter and stall scheduler live in pure OM-2 (om/gpu/cg_tern.om); this file is the thin fail-closed C ABI wrapper. */ |
| 19 | #include "omega_blackwell_codegen_tern.h" |
| 20 | #include "omega_bw_om_ir.h" |
| 21 | #include "omega_native.h" |
| 22 | #include <string.h> |
| 23 | |
| 24 | static void tern_pack_spec(const OmegaTernGemvSpec *s, uint64_t sp[8]) { |
| 25 | memset(sp, 0, 8u * sizeof(uint64_t)); |
| 26 | sp[0] = s->k; |
| 27 | sp[1] = s->layout; |
| 28 | sp[2] = s->rows; |
| 29 | sp[3] = s->warps; |
| 30 | sp[4] = s->depth; |
| 31 | sp[5] = s->epi; |
| 32 | sp[6] = (uint64_t)(int64_t)s->mutant; |
| 33 | } |
| 34 | |
| 35 | uint32_t omega_tern_gemv_chunks(const OmegaTernGemvSpec *s) { |
| 36 | if (!s || s->k == 0) return 0; |
| 37 | return (s->k + OMEGA_TERN_CHUNK_MAX - 1u) / OMEGA_TERN_CHUNK_MAX; |
| 38 | } |
| 39 | |
| 40 | int omega_tern_gemv_spec_ok(const OmegaTernGemvSpec *s) { |
| 41 | if (!s) return 0; |
| 42 | uint64_t sp[8], ok = 0; |
| 43 | tern_pack_spec(s, sp); |
| 44 | if (omega2_cg_tern_spec_ok(&ok, sp, 8u) != 0) return 0; |
| 45 | return ok != 0 ? 1 : 0; |
| 46 | } |
| 47 | |
| 48 | uint32_t omega_tern_gemv_steps(const OmegaTernGemvSpec *s) { return omega_tern_gemv_spec_ok(s) ? s->k / 256u : 0; } |
| 49 | uint32_t omega_tern_gemv_out_slot_bytes(const OmegaTernGemvSpec *s) { return s ? 2u * s->rows * s->warps * 4u : 0; } |
| 50 | |
| 51 | int omega_tern_gemv_coalesced_out(const OmegaTernGemvSpec *s) { |
| 52 | if (!omega_tern_gemv_spec_ok(s) || omega_tern_gemv_chunks(s) != 1 || 2u * s->rows * s->warps > 32u) return 0; |
| 53 | return s->k * 2u + 2u * omega_tern_gemv_out_slot_bytes(s) <= OMEGA_TERN_SHARED_MAX; |
| 54 | } |
| 55 | |
| 56 | uint32_t omega_tern_gemv_shared_bytes(const OmegaTernGemvSpec *s) { |
| 57 | if (!omega_tern_gemv_spec_ok(s)) return 0; |
| 58 | const uint32_t x_bytes = (s->k / omega_tern_gemv_chunks(s)) * 2u; |
| 59 | return omega_tern_gemv_coalesced_out(s) ? x_bytes + 2u * omega_tern_gemv_out_slot_bytes(s) : x_bytes; |
| 60 | } |
| 61 | |
| 62 | uint64_t omega_tern_word_offset(uint32_t layout, uint32_t R, uint32_t k, uint32_t r, uint32_t c) { |
| 63 | const uint64_t kw = k / 16u; |
| 64 | if (layout == OMEGA_TERN_LAYOUT_ROWMAJOR) return ((uint64_t)r * kw + c) * 4u; |
| 65 | const uint64_t blk = r / (2u * R), j = r % (2u * R); |
| 66 | return blk * 2u * R * kw * 4u + (uint64_t)c * 2u * R * 4u + j * 4u; |
| 67 | } |
| 68 | |
| 69 | int omega_blackwell_codegen_tern_gemv_prog(const OmegaTernGemvSpec *spec, BlackwellIRProgram *prog) { |
| 70 | if (!prog) return -1; |
| 71 | omega_bw_ir_init(prog); |
| 72 | if (!spec) return -1; |
| 73 | uint64_t sp[8], ins[8192], meta[512], rc = 1; |
| 74 | memset(ins, 0, sizeof ins); |
| 75 | memset(meta, 0, sizeof meta); |
| 76 | tern_pack_spec(spec, sp); |
| 77 | if (omega2_cg_tern_emit(&rc, sp, 8u, ins, 8192u, meta, 512u) != 0 || rc != 0) return -1; |
| 78 | if (omega_bw_ir_from_om(ins, meta, prog) != 0) return -1; |
| 79 | uint64_t v2p[256], bsz[256]; |
| 80 | for (int v = 0; v < 256; v++) { |
| 81 | if (v < prog->regalloc.num_vregs && prog->regalloc.vreg_to_phys[v] >= 0) { |
| 82 | v2p[v] = (uint64_t)prog->regalloc.vreg_to_phys[v]; |
| 83 | bsz[v] = prog->regalloc.intervals[v].bundle_size ? prog->regalloc.intervals[v].bundle_size : 1u; |
| 84 | } else { |
| 85 | v2p[v] = 0xffffffffffffffffull; |
| 86 | bsz[v] = 0u; |
| 87 | } |
| 88 | } |
| 89 | rc = 1; |
| 90 | if (omega2_cg_tern_sched_stalls(&rc, ins, 8192u, (uint64_t)prog->count, v2p, 256u, bsz, 256u) != 0 || rc != 0) { |
| 91 | omega_bw_ir_init(prog); |
| 92 | return -1; |
| 93 | } |
| 94 | for (size_t i = 0; i < prog->count; i++) prog->insns[i].control = (uint32_t)(ins[i * 8u + 6u] >> 32); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | int omega_tern_gemv_prog_stats(const OmegaTernGemvSpec *spec, uint32_t *insns, uint32_t *gprs) { |
| 99 | static BlackwellIRProgram prog; |
| 100 | if (omega_blackwell_codegen_tern_gemv_prog(spec, &prog) != 0) return -1; |
| 101 | if (insns) *insns = (uint32_t)prog.count; |
| 102 | if (gprs) *gprs = prog.regalloc.peak_gpr_usage; |
| 103 | return 0; |
| 104 | } |