interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / src / omega_ampere_codegen.c

85 lines · 4041 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Field packing is om/core/ampack.om. The C oracle is selected only by
3 * CUDAPP_MIGRATION_PENDING_C_CORE. The complete SM86 encoding implementation is
4 * om/encoder/ampere_enc_sm86.om, translated by the pinned omega-bridge. */
5#include "omega_ampere_codegen.h"
6#include "omega_core_binding.h"
7
8int omega_gpu_encode_program_target(const BlackwellIRProgram *prog, uint32_t target_sm,
9 uint8_t *code_buf, size_t max_len, size_t *out_len)
10{
11 if (!out_len) return -1;
12 *out_len = 0;
13 if (!prog || !code_buf || prog->count > BW_MAX_IR_INSNS) return -1;
14 if (target_sm != 86 && target_sm != 121) return OMEGA_GPU_ENCODE_UNSUPPORTED;
15 const size_t needed = omega_bw_program_code_bytes(prog);
16 if (needed > max_len) return -1;
17 if (target_sm == 121) return omega_bw_encode_program(prog, code_buf, max_len, out_len);
18
19 uint64_t ins[8192], phys[288];
20 /* Fixed native output extent; staging permits short caller buffers and
21 * leaves them unchanged on a refused instruction or native trap. */
22 uint8_t code[16384];
23 for (size_t i = 0; i < prog->count; ++i) {
24 const BlackwellIRInsn *in = &prog->insns[i];
25 uint64_t *p = ins + i * 8;
26#ifdef CUDAPP_MIGRATION_PENDING_C_CORE
27 p[0] = (uint64_t)in->op;
28 p[1] = (uint64_t)(int64_t)in->dst_vreg;
29 p[2] = (uint64_t)(int64_t)in->src1_vreg;
30 p[3] = (uint64_t)(int64_t)in->src2_vreg;
31 p[4] = (uint64_t)(int64_t)in->src3_vreg;
32 p[5] = (uint64_t)(int64_t)in->ureg;
33 p[6] = (uint64_t)in->imm | ((uint64_t)in->control << 32);
34 p[7] = (uint64_t)in->is_uniform | ((uint64_t)in->predicate_p0 << 1)
35 | ((uint64_t)in->predicate_not << 2) | ((uint64_t)in->bar_reg << 8)
36 | ((uint64_t)in->dst_subreg << 16) | ((uint64_t)in->src1_subreg << 24)
37 | ((uint64_t)in->src2_subreg << 32) | ((uint64_t)in->src3_subreg << 40);
38#else
39 uint64_t raw[16], packed[16];
40 raw[0] = (uint32_t)in->op;
41 raw[1] = (uint32_t)in->dst_vreg;
42 raw[2] = (uint32_t)in->src1_vreg;
43 raw[3] = (uint32_t)in->src2_vreg;
44 raw[4] = (uint32_t)in->src3_vreg;
45 raw[5] = (uint32_t)in->ureg;
46 raw[6] = in->imm;
47 raw[7] = in->control;
48 raw[8] = (uint64_t)in->is_uniform;
49 raw[9] = (uint64_t)in->predicate_p0;
50 raw[10] = (uint64_t)in->predicate_not;
51 raw[11] = in->dst_subreg;
52 raw[12] = in->src1_subreg;
53 raw[13] = in->src2_subreg;
54 raw[14] = in->src3_subreg;
55 raw[15] = in->bar_reg;
56 if (CORE_VOID(ampack, 0, raw, 16, packed, 16) != OMEGA_OK) return OMEGA_GPU_ENCODE_UNSUPPORTED;
57 for (int k = 0; k < 8; ++k) p[k] = packed[k];
58#endif
59 }
60#ifdef CUDAPP_MIGRATION_PENDING_C_CORE
61 for (size_t i = 0; i < BW_MAX_VREGS; ++i)
62 phys[i] = (uint64_t)(int64_t)prog->regalloc.vreg_to_phys[i];
63 for (size_t i = 0; i < BW_MAX_UVREGS; ++i)
64 phys[256 + i] = (uint64_t)(int64_t)prog->regalloc.uvreg_to_phys[i];
65#else
66 for (size_t base = 0; base < (size_t)BW_MAX_VREGS + BW_MAX_UVREGS; base += 16) {
67 uint64_t raw[16], packed[16];
68 for (int k = 0; k < 16; ++k) {
69 size_t ix = base + (size_t)k;
70 int v = ix < BW_MAX_VREGS ? prog->regalloc.vreg_to_phys[ix]
71 : prog->regalloc.uvreg_to_phys[ix - BW_MAX_VREGS];
72 raw[k] = (uint32_t)v;
73 }
74 if (CORE_VOID(ampack, 1, raw, 16, packed, 16) != OMEGA_OK) return OMEGA_GPU_ENCODE_UNSUPPORTED;
75 for (int k = 0; k < 16; ++k) phys[base + (size_t)k] = packed[k];
76 }
77#endif
78 uint64_t bytes = 0;
79 uint32_t status = CORE_CALL(ampere_enc_sm86, &bytes, ins, 8192, phys, 288,
80 prog->count, 1, code, 16384);
81 if (status != OMEGA_OK || bytes != needed) return OMEGA_GPU_ENCODE_UNSUPPORTED;
82 for (size_t i = 0; i < needed; ++i) code_buf[i] = code[i];
83 *out_len = (size_t)bytes;
84 return 0;
85}