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_cubin.h

118 lines · 6312 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/*
3 * omega_cubin: package Omega-generated sm_86 or sm_121 machine code into the
4 * NVIDIA cubin ELF container. Device loading is separately qualified.
5 *
6 * This is a container writer, not a GPU driver, compiler or verifier. It does
7 * not know a device address, open a device, allocate memory or launch work. It
8 * writes only the bytes that describe the kernel: text, kernel parameter
9 * metadata, an empty constant bank (the loader fills parameters), and the
10 * standard note/compat sections. The input text must already have been
11 * produced and digest-bound by the existing CUDApp code generator.
12 *
13 * Layout facts below were established from a ptxas 13.0.88 -arch=sm_121
14 * reference output and are re-checked by nvdisasm/cuobjdump in
15 * tools/artifact_smoke.sh (the C test checks the digest/identity layer):
16 * - ELF64 little-endian, e_machine 190 (EM_CUDA), e_flags 0x6007902 (sm_121),
17 * OS/ABI 0x41 (NV), ABI version 8;
18 * - kernel parameters live in constant bank 0 starting at byte 0x380;
19 * - a function symbol must carry STO_CUDA_ENTRY (st_other 0x10);
20 * - .nv.info.<fn> carries EIATTR_KPARAM_INFO entries (format 0x1704), the
21 * parameter constant-bank size (0x1903), and the index of the function's
22 * constant-bank section symbol (0x0a04);
23 * - the module-level .nv.info names the first function symbol and its
24 * register count (0x2f04), and .nv.info.<fn> repeats that count as the
25 * maximum (0x1b03). Both come from gpr_count, which must be the declared
26 * count (>= allocator peak + 2): REGCOUNT equal to the peak loads but
27 * faults with 715 ILLEGAL_INSTRUCTION on GB10 (G2 window
28 * 20261030T235858Z-10fa8c, H matrix). .nv.compat and .note.nv.cuinfo are
29 * required by the offline tools;
30 * - .note.nv.tkinfo (note type 2000, owner "NVIDIA Corp", linked from
31 * .note.nv.cuinfo) is required by the driver: without it cuModuleLoadData
32 * returns 200 INVALID_IMAGE on GB10, with it the same image loads (G2
33 * windows 20261030T235211Z-0352f1 and 20261030T235858Z-10fa8c,
34 * evidence/MAX-INTEROP/G2). Its strings are this writer's own.
35 * Nothing here is copied from an NVIDIA binary; these are ABI facts checked
36 * against the output of NVIDIA's ptxas (a proprietary SDK tool, used on this
37 * host as an oracle only; see PROVENANCE.md).
38 *
39 * Limits, deliberate and recorded:
40 * - The writer emits five program headers (PT_PHDR, PT_LOAD for the header
41 * table, text, shared and constant), matching the ptxas reference layout.
42 * The 200 an image with them returned (window 20261024T044814Z-4dc098)
43 * was the missing .note.nv.tkinfo (H7 refused, H6 ran); whether the
44 * headers are necessary is open.
45 * - sm_86 has no Blackwell capmerc dependency. sm_121 output is preserved;
46 * this writer does not synthesize capmerc or assert driver loadability.
47 * - EIATTR_EXIT_INSTR_OFFSETS is omitted because this writer does not decode
48 * SASS and will not fabricate the EXIT byte offset. ptxas emits it; the
49 * driver does not need it: the tkinfo + declared-register image ran
50 * bit-exact without it (G2 window 20261030T235858Z-10fa8c, H6).
51 * - sh_info of the .nv.* sections uses this writer's section indices, which
52 * cuobjdump accepts; it does not reproduce ptxas's symbol indices.
53 */
54#ifndef OMEGA_CUBIN_H
55#define OMEGA_CUBIN_H
56
57#include <stddef.h>
58#include <stdint.h>
59
60#define OMEGA_CUBIN_ABI_VERSION 8u
61#define OMEGA_CUBIN_EFLAGS_SM121 0x6007902u
62#define OMEGA_CUBIN_SM_121 121u
63#define OMEGA_CUBIN_SM_86 86u
64#define OMEGA_CUBIN_EFLAGS_SM86 ((86u << 16) | 0x500u | 86u)
65#define OMEGA_CUBIN_PARAM_BASE_SM86 0x160u
66#define OMEGA_CUBIN_PARAM_BASE 0x380u
67#define OMEGA_CUBIN_CBANK_MAX 1024u
68#define OMEGA_CUBIN_MAX_PARAMS 32u
69#define OMEGA_CUBIN_MAX_NAME 64u
70
71/* Named refusals. 0 is the only acceptance. */
72enum {
73 OMEGA_CUBIN_OK = 0,
74 OMEGA_CUBIN_E_ARG = 1, /* NULL/empty input, empty or invalid name */
75 OMEGA_CUBIN_E_TEXT = 2, /* text length 0, not a multiple of 16, or too large */
76 OMEGA_CUBIN_E_PARAMS = 3, /* too many, unsorted, overlapping, wrong size/alignment */
77 OMEGA_CUBIN_E_CBANK = 4, /* parameter area exceeds the 1024-byte bank */
78 OMEGA_CUBIN_E_GPR = 5, /* register count above 255 */
79 OMEGA_CUBIN_E_SHARED = 6, /* reserved shared size not a multiple of 4 */
80 OMEGA_CUBIN_E_MEM = 7, /* allocation failure */
81 OMEGA_CUBIN_E_TARGET = 8 /* unsupported explicit SM target */
82};
83
84/* One kernel parameter: its byte offset in constant bank 0, its size (8 for a
85 * pointer/u64, 4 for u32, 2 for u16, 1 for u8), and whether it is a pointer
86 * (which selects the parameter-info type word). */
87typedef struct {
88 uint32_t offset;
89 uint32_t size;
90 uint8_t is_pointer;
91} OmegaCubinParam;
92
93const char *omega_cubin_rc_name(int rc);
94
95/* Pack text into a cubin. *out receives a malloc'd buffer of *out_len bytes;
96 * free with omega_cubin_free. Refuses malformed descriptors rather than
97 * emitting a best-effort container. */
98int omega_cubin_pack(const char *fn_name,
99 const uint8_t *text, size_t text_len,
100 const OmegaCubinParam *params, size_t nparams,
101 uint32_t cbank_size, uint32_t gpr_count, /* declared registers (omega_bw_gpr_declared), not the allocator peak */
102 uint32_t shared_reserved,
103 uint8_t **out, size_t *out_len, char *err, size_t err_cap);
104
105/* Explicit target; no auto-detection or architecture fallback. Parameters use
106 * the selected target's constant-bank ABI (sm_86: 0x160; sm_121: 0x380).
107 * The legacy entry above remains exactly the sm_121 contract.
108 * This only packages bytes: callers must select the matching encoder too. */
109int omega_cubin_pack_target(uint32_t target_sm, const char *fn_name,
110 const uint8_t *text, size_t text_len,
111 const OmegaCubinParam *params, size_t nparams,
112 uint32_t cbank_size, uint32_t gpr_count,
113 uint32_t shared_reserved,
114 uint8_t **out, size_t *out_len, char *err, size_t err_cap);
115
116void omega_cubin_free(uint8_t *p);
117
118#endif /* OMEGA_CUBIN_H */