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 / evidence / tools-om-2 / 2026-11-06 / coverage / ampere_corpus.c.gcov

246 lines · 11416 bytes Raw Blame History
1 -: 0:Source:ampere_corpus.c
2 -: 0:Graph:ac_cov-ampere_corpus.gcno
3 -: 0:Data:ac_cov-ampere_corpus.gcda
4 -: 0:Runs:63
5 -: 1:/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
6 -: 2:/* Test-only IR capture. Linked into the existing host-only corpus producers;
7 -: 3: * no driver API is called here. A refusal is legal for an explicit FP8 marker,
8 -: 4: * or for a training R5 opcode SM86 does not implement (op > BW_IR_MUFU_LG2).
9 -: 5: *
10 -: 6: * The decisions are seeded Omega (om/tools/ampcorp.om through src/cudapp_om_ampcorp.c): the folder
11 -: 7: * check, the IR metadata, the refusal classification, the check of the encoder outcome and the text of
12 -: 8: * the paths and of the message. This file keeps the I/O: getenv, the SM86 encoder call, open/fdopen/
13 -: 9: * fwrite, the serial counter and abort. The former C is the oracle behind the default-OFF switch
14 -: 10: * CUDAPP_MIGRATION_PENDING_C_TOOLS_AMPCORP (tests/tools-om/ampcorp.sh). */
15 -: 11:#include "omega_ampere_codegen.h"
16 -: 12:#include "omega_blackwell_codegen_tc.h"
17 -: 13:#include "omega_blackwell_codegen_qmma.h"
18 -: 14:#include <stdio.h>
19 -: 15:#include <stdlib.h>
20 -: 16:#include <string.h>
21 -: 17:#include <unistd.h>
22 -: 18:#include <fcntl.h>
23 -: 19:#include <sys/stat.h>
24 -: 20:
25 -: 21:#ifdef CUDAPP_MIGRATION_PENDING_C_TOOLS_AMPCORP
26function is_safe_corpus_dir called 77 returned 100% blocks executed 100%
27 77: 22:static int is_safe_corpus_dir(const char *dir)
28 -: 23:{
29 77: 24: if (!dir || !*dir) return 0;
30branch 0 taken 76 (fallthrough)
31branch 1 taken 1
32branch 2 taken 1 (fallthrough)
33branch 3 taken 75
34 75: 25: if (strchr(dir, '\\')) return 0;
35branch 0 taken 3 (fallthrough)
36branch 1 taken 72
37 250: 26: for (const char *component = dir; *component;) {
38branch 0 taken 185
39branch 1 taken 65 (fallthrough)
40 301: 27: while (*component == '/') ++component;
41branch 0 taken 116
42branch 1 taken 185 (fallthrough)
43 185: 28: const char *start = component;
44 20568: 29: while (*component && *component != '/') ++component;
45branch 0 taken 20501 (fallthrough)
46branch 1 taken 67
47branch 2 taken 20383
48branch 3 taken 118 (fallthrough)
49 185: 30: if ((size_t)(component - start) == 2 &&
50branch 0 taken 9 (fallthrough)
51branch 1 taken 176
52 9: 31: start[0] == '.' && start[1] == '.') return 0;
53branch 0 taken 8 (fallthrough)
54branch 1 taken 1
55branch 2 taken 7 (fallthrough)
56branch 3 taken 1
57 -: 32: }
58 65: 33: return 1;
59 -: 34:}
60 -: 35:
61function save called 112 returned 88% blocks executed 84%
62 112: 36:static void save(const char *dir, unsigned serial, const char *suffix, const void *data, size_t size)
63 -: 37:{
64 -: 38: char path[4096];
65 112: 39: int n = snprintf(path, sizeof(path), "%s/%ld-%u.%s", dir, (long)getpid(), serial, suffix);
66call 0 returned 112
67 112: 40: if (n < 0 || (size_t)n >= sizeof(path)) abort();
68branch 0 taken 112 (fallthrough)
69branch 1 taken 0
70branch 2 taken 3 (fallthrough)
71branch 3 taken 109
72call 4 returned 0
73 109: 41: int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
74call 0 returned 109
75 109: 42: if (fd < 0) { perror(path); abort(); }
76branch 0 taken 8 (fallthrough)
77branch 1 taken 101
78call 2 returned 8
79call 3 returned 0
80 101: 43: FILE *f = fdopen(fd, "wb");
81call 0 returned 101
82 101*: 44: if (!f) { perror(path); close(fd); abort(); }
83branch 0 taken 0 (fallthrough)
84branch 1 taken 101
85call 2 never executed
86call 3 never executed
87call 4 never executed
88 101: 45: if (fwrite(data, 1, size, f) != size || fclose(f) != 0) abort();
89call 0 returned 101
90branch 1 taken 100 (fallthrough)
91branch 2 taken 1
92call 3 returned 100
93branch 4 taken 2 (fallthrough)
94branch 5 taken 98
95call 6 returned 0
96 98: 46:}
97 -: 47:
98function omega_ampere_corpus_capture called 77 returned 62% blocks executed 100%
99 77: 48:void omega_ampere_corpus_capture(const BlackwellIRProgram *prog, const uint8_t *sm121, size_t len)
100 -: 49:{
101 77: 50: const char *dir = getenv("OMEGA_AMPERE_CORPUS_DIR");
102call 0 returned 77
103 83: 51: if (!is_safe_corpus_dir(dir)) return;
104call 0 returned 77
105branch 1 taken 12 (fallthrough)
106branch 2 taken 65
107 -: 52: static unsigned serial;
108 65: 53: const unsigned id = serial++;
109 -: 54: uint8_t code[16384];
110 -: 55: uint32_t metadata[BW_MAX_IR_INSNS][2];
111 65: 56: int fp8 = 0, blackwell_only = 0;
112 4527: 57: for (size_t i = 0; i < prog->count; ++i) {
113branch 0 taken 4462
114branch 1 taken 65 (fallthrough)
115 4462: 58: const BlackwellIRInsn *in = &prog->insns[i];
116 4462: 59: metadata[i][0] = (uint32_t)in->op;
117 4462: 60: metadata[i][1] = in->imm;
118 4462: 61: if (in->op > BW_IR_MUFU_LG2) blackwell_only = 1;
119branch 0 taken 5 (fallthrough)
120branch 1 taken 4457
121 4462: 62: if ((in->op == BW_IR_HMMA_BF16 && in->imm == OMEGA_QMMA_MARK) ||
122branch 0 taken 80 (fallthrough)
123branch 1 taken 4382
124branch 2 taken 70 (fallthrough)
125branch 3 taken 10
126 4462: 63: (in->op == BW_IR_FMNMX_MAX && in->imm == OMEGA_QMMA_MARK_F2E4)) fp8 = 1;
127branch 0 taken 71 (fallthrough)
128branch 1 taken 4381
129branch 2 taken 1 (fallthrough)
130branch 3 taken 70
131 -: 64: }
132 65: 65: memset(code, 0xa5, sizeof(code));
133 65: 66: size_t bytes = 0;
134 65: 67: int rc = omega_gpu_encode_program_target(prog, 86, code, sizeof(code), &bytes);
135call 0 returned 65
136 -: 68: /* SM121 already holds the published bytes. SM86 refuses the new opcodes in
137 -: 69: * its preflight and writes nothing. There is no post-encode patch. */
138 65: 70: if (fp8 || blackwell_only) {
139branch 0 taken 54 (fallthrough)
140branch 1 taken 11
141branch 2 taken 5 (fallthrough)
142branch 3 taken 49
143 16: 71: if (rc != OMEGA_GPU_ENCODE_UNSUPPORTED || bytes != 0) abort();
144branch 0 taken 13 (fallthrough)
145branch 1 taken 3
146branch 2 taken 1 (fallthrough)
147branch 3 taken 12
148call 4 returned 0
149 172042: 72: for (size_t i = 0; i < sizeof(code); ++i) if (code[i] != 0xa5) abort();
150branch 0 taken 3 (fallthrough)
151branch 1 taken 172030
152call 2 returned 0
153branch 3 taken 172033
154branch 4 taken 9 (fallthrough)
155 9: 73: save(dir, id, "refused", metadata, prog->count * sizeof(metadata[0]));
156call 0 returned 6
157 6: 74: return;
158 -: 75: }
159 49: 76: if (rc != 0 || bytes != len) {
160branch 0 taken 44 (fallthrough)
161branch 1 taken 5
162branch 2 taken 3 (fallthrough)
163branch 3 taken 41
164 8: 77: fprintf(stderr, "SM86 unexpectedly refused an existing kernel (%zu instructions, rc %d)\n", prog->count, rc);
165call 0 returned 8
166 8: 78: abort();
167call 0 returned 0
168 -: 79: }
169 41: 80: save(dir, id, "sm121.bin", sm121, len);
170call 0 returned 31
171 31: 81: save(dir, id, "sm86.bin", code, bytes);
172call 0 returned 31
173 31: 82: save(dir, id, "ir", metadata, prog->count * sizeof(metadata[0]));
174call 0 returned 30
175 -: 83:}
176 -: 84:#else
177 -: 85:#include "cudapp_native.h"
178 -: 86:
179 -: 87:/* The unit's arrays (sizes fixed by om/tools/ampcorp.om). The folder buffer holds any environment
180 -: 88: * string: Linux caps one at MAX_ARG_STRLEN (131072 bytes) with its name and zero. Not reentrant, as
181 -: 89: * the former C's serial counter already was not. */
182 -: 90:static uint64_t g_facts[16];
183 -: 91:static uint8_t g_dir[131072];
184 -: 92:static uint64_t g_ins[2048];
185 -: 93:static uint8_t g_code[16384];
186 -: 94:static uint8_t g_out[20480];
187 -: 95:
188 -: 96:static uint64_t decide(void)
189 -: 97:{
190 -: 98: uint64_t r = 0;
191 -: 99: if (omega_ampcorp(&r, g_facts, 16, g_dir, sizeof(g_dir), g_ins, 2048, g_code, sizeof(g_code),
192 -: 100: g_out, sizeof(g_out)) != OMEGA_OK) abort();
193 -: 101: return r;
194 -: 102:}
195 -: 103:
196 -: 104:static void save(const char *path, const void *data, size_t size)
197 -: 105:{
198 -: 106: int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
199 -: 107: if (fd < 0) { perror(path); abort(); }
200 -: 108: FILE *f = fdopen(fd, "wb");
201 -: 109: if (!f) { perror(path); close(fd); abort(); }
202 -: 110: if (fwrite(data, 1, size, f) != size || fclose(f) != 0) abort();
203 -: 111:}
204 -: 112:
205 -: 113:void omega_ampere_corpus_capture(const BlackwellIRProgram *prog, const uint8_t *sm121, size_t len)
206 -: 114:{
207 -: 115: const char *dir = getenv("OMEGA_AMPERE_CORPUS_DIR");
208 -: 116: if (!dir) return;
209 -: 117: size_t dn = strlen(dir);
210 -: 118: if (dn >= sizeof(g_dir) || prog->count > BW_MAX_IR_INSNS) abort();
211 -: 119: memcpy(g_dir, dir, dn);
212 -: 120: memset(g_facts, 0, sizeof(g_facts));
213 -: 121: g_facts[1] = dn;
214 -: 122: if (!decide()) return;
215 -: 123: static unsigned serial;
216 -: 124: const unsigned id = serial++;
217 -: 125: for (size_t i = 0; i < prog->count; ++i) {
218 -: 126: g_ins[2 * i] = (uint32_t)prog->insns[i].op;
219 -: 127: g_ins[2 * i + 1] = prog->insns[i].imm;
220 -: 128: }
221 -: 129: memset(g_code, 0xa5, sizeof(g_code));
222 -: 130: size_t bytes = 0;
223 -: 131: int rc = omega_gpu_encode_program_target(prog, 86, g_code, sizeof(g_code), &bytes);
224 -: 132: const uint64_t facts[16] = { 1, dn, prog->count, (uint64_t)(int64_t)rc, bytes, len, (uint64_t)(long)getpid(), id,
225 -: 133: BW_IR_MUFU_LG2, BW_IR_HMMA_BF16, OMEGA_QMMA_MARK, BW_IR_FMNMX_MAX,
226 -: 134: OMEGA_QMMA_MARK_F2E4, (uint64_t)(int64_t)OMEGA_GPU_ENCODE_UNSUPPORTED };
227 -: 135: memcpy(g_facts, facts, sizeof(facts));
228 -: 136: uint64_t r = decide();
229 -: 137: const char *path = (const char *)g_out + 8192;
230 -: 138: switch (r & 255) {
231 -: 139: case 1:
232 -: 140: save(path, g_out, prog->count * 8);
233 -: 141: return;
234 -: 142: case 2:
235 -: 143: save(path, sm121, len);
236 -: 144: save((const char *)g_out + 12288, g_code, bytes);
237 -: 145: save((const char *)g_out + 16384, g_out, prog->count * 8);
238 -: 146: return;
239 -: 147: case 4:
240 -: 148: fwrite(path, 1, (size_t)(r >> 8), stderr);
241 -: 149: abort();
242 -: 150: default: /* 3: the refusal wrote bytes or did not refuse; 5: the path does not fit 4096 bytes */
243 -: 151: abort();
244 -: 152: }
245 -: 153:}
246 -: 154:#endif