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 / om / c1 / sim.c

99 lines · 5086 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Test-only ABI glue. Language/checker/lowering remain in the pinned Omega
3 * checkout. Encoding and cubin packaging link CUDApp's default Omega units. */
4#include "omega_cubin.h"
5#include "omega_osc_gpu.h"
6#include <elf.h>
7#include "bw_warp_sim.h"
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12static unsigned c1_images;
13static int c1_guard_fixture(void) {
14 static const struct { BlackwellIROpcode op; int d, a; unsigned inv; uint32_t w0, w1, w2; } fx[] = {
15 {BW_IR_MOV_IMM, 2, -1, 0, 0x00020802, 5, 0xf00},
16 {BW_IR_MOV_IMM, 2, -1, 1, 0x00028802, 5, 0xf00},
17 {BW_IR_EXIT, -1, -1, 0, 0x0000094d, 0, 0x03800000},
18 {BW_IR_EXIT, -1, -1, 1, 0x0000894d, 0, 0x03800000},
19 /* WolframLanguageEvaluator:
20 * IntegerString[BitOr[BitAnd[FromDigits[#,16],FromDigits["ffff0fff",16]],
21 * BitShiftLeft[2,16]],16,8]& /@ {"7245","7305"} -> {"00020245","00020305"}. */
22 {BW_IR_I2FP_U32, 2, 1, 0, 0x00020245, 1, 0x00201000},
23 {BW_IR_F2I_U32, 2, 1, 0, 0x00020305, 1, 0x0020f000}
24 };
25 for (size_t i = 0; i < sizeof fx / sizeof fx[0]; ++i) {
26 BlackwellIRInsn in = {0}; uint32_t w[4];
27 in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a;
28 in.src2_vreg = in.src3_vreg = in.ureg = -1;
29 in.imm = fx[i].op == BW_IR_MOV_IMM ? 5 : 0;
30 in.predicate_p0 = true; in.predicate_not = fx[i].inv;
31 if (omega_blackwell_encode_one(&in, w) || w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2) return -1;
32 }
33 BlackwellIRInsn bad = {0}; uint32_t words[4];
34 bad.op = (BlackwellIROpcode)(BW_IR_LAST + 1);
35 if (!omega_blackwell_encode_one(&bad, words)) return -1;
36 return 0;
37}
38
39/* Read the emitted ELF rather than searching for a byte substring: .text must
40 * be a single exact payload. This tests the container path, not device loading. */
41static int c1_text(const uint8_t *image, size_t n, const OscGpuKernel *k) {
42 Elf64_Ehdr e; if (n < sizeof e) return -1; memcpy(&e, image, sizeof e);
43 if (memcmp(e.e_ident, ELFMAG, SELFMAG) || e.e_machine != 190 || e.e_shentsize != sizeof(Elf64_Shdr) ||
44 e.e_shoff > n || e.e_shnum > (n - e.e_shoff) / sizeof(Elf64_Shdr) || e.e_shstrndx >= e.e_shnum) return -1;
45 Elf64_Shdr names; memcpy(&names, image + e.e_shoff + e.e_shstrndx * sizeof names, sizeof names);
46 if (names.sh_offset > n || names.sh_size > n - names.sh_offset) return -1;
47 unsigned found = 0;
48 for (unsigned i = 0; i < e.e_shnum; ++i) {
49 Elf64_Shdr s; memcpy(&s, image + e.e_shoff + i * sizeof s, sizeof s);
50 if (s.sh_name >= names.sh_size) return -1;
51 const char *name = (const char *)image + names.sh_offset + s.sh_name;
52 if (!memchr(name, 0, names.sh_size - s.sh_name)) return -1;
53 if (strncmp(name, ".text.", 6)) continue;
54 if (s.sh_offset > n || s.sh_size > n - s.sh_offset || s.sh_size != k->code_size ||
55 memcmp(image + s.sh_offset, k->code, k->code_size)) return -1;
56 ++found;
57 }
58 return found == 1 ? 0 : -1;
59}
60
61static int c1_pack(OscGpuKernel *k) {
62 int rc = 0; char err[256] = {0};
63 OmegaCubinParam p[OMEGA_CUBIN_MAX_PARAMS]; size_t np = 0;
64 for (unsigned i = 0; i < k->nparams; ++i) {
65 const OscGpuParam *q = &k->params[i];
66 int buffer = q->kind == OSC_GPU_P_VIEW || q->kind == OSC_GPU_P_EDIT;
67 if (np + (buffer ? 2u : 1u) + 3u > OMEGA_CUBIN_MAX_PARAMS) { osc_gpu_free(k); return -1; }
68 p[np++] = (OmegaCubinParam){OSC_GPU_ARG_BASE + q->off, buffer ? 8u : 4u, buffer};
69 if (buffer) p[np++] = (OmegaCubinParam){OSC_GPU_ARG_BASE + q->off + 8u, 4, 0};
70 }
71 p[np++] = (OmegaCubinParam){OSC_GPU_ARG_BASE + k->status_off, 8, 1};
72 p[np++] = (OmegaCubinParam){OSC_GPU_ARG_BASE + k->blocks_off, 4, 0};
73 p[np++] = (OmegaCubinParam){OSC_GPU_ARG_BASE + k->batches_off, 4, 0};
74 uint8_t *image = NULL; size_t bytes = 0;
75 rc = omega_cubin_pack(k->name, k->code, k->code_size, p, np, OSC_GPU_ARG_BASE + k->arg_words * 4u,
76 omega_bw_gpr_declared(k->gpr_count), k->shared_bytes, &image, &bytes, err, sizeof err);
77 if (!rc) rc = c1_text(image, bytes, k);
78 if (!rc) {
79 /* Negative control: changed text must not round-trip as the original. */
80 k->code[0] ^= 1;
81 int caught = c1_text(image, bytes, k) != 0;
82 k->code[0] ^= 1;
83 if (!caught) rc = -1;
84 }
85 omega_cubin_free(image);
86 if (rc) { fprintf(stderr, "C1 cubin %s: %s\n", k->name, err); osc_gpu_free(k); return -1; }
87 ++c1_images; return 0;
88}
89#define omega_blackwell_verify_codegen_fixtures_guard c1_guard_fixture
90#define OSC_GPU_AFTER_COMPARE(k) do { if (c1_pack(k)) { CHECK(0, "C1 cubin packaging failed"); return -1; } } while (0)
91#define main c1_sim_main
92#include C1_TEST_SOURCE
93#undef main
94int main(int argc, char **argv) {
95 int rc = c1_sim_main(argc, argv);
96 if (!c1_images) rc = 1;
97 printf("C1 SIM %s: cubin text roundtrips=%u; changed-text mutants caught\n", rc ? "FAIL" : "PASS", c1_images);
98 return rc;
99}