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 / tests / tools-om / ampcorp_diff.c

293 lines · 11345 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* ampcorp_diff: drives omega_ampere_corpus_capture (tools/ampere_corpus.c, either build) through the
3 * partition of tests/tools-om/README.md. This file defines omega_gpu_encode_program_target, so the
4 * encoder outcome (rc, bytes, stray writes into the buffer) is chosen per case. Each case runs in a child
5 * in a fresh folder; the report holds the child's exit or signal, its stderr and every file it wrote
6 * (name, size, FNV-1a 64; a link is listed by name), with the child's pid replaced by "PID". The two builds must report the same. */
7#include "omega_ampere_codegen.h"
8#include "omega_blackwell_codegen_qmma.h"
9#include <dirent.h>
10#include <stdint.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <signal.h>
15#include <sys/stat.h>
16#include <sys/wait.h>
17#include <unistd.h>
18#include <fcntl.h>
19
20void omega_ampere_corpus_capture(const BlackwellIRProgram *prog, const uint8_t *sm121, size_t len);
21
22static struct { int rc; size_t bytes; long poke; size_t fill; } enc;
23static long calls;
24
25int omega_gpu_encode_program_target(const BlackwellIRProgram *prog, uint32_t target_sm, uint8_t *code_buf,
26 size_t max_len, size_t *out_len)
27{
28 (void)prog;
29 calls++;
30 if (target_sm != 86 || max_len != 16384) abort();
31 for (size_t i = 0; i < enc.fill && i < max_len; i++) code_buf[i] = (uint8_t)(i * 7 + 3);
32 if (enc.poke >= 0) code_buf[enc.poke] ^= 1;
33 *out_len = enc.bytes;
34 return enc.rc;
35}
36
37static BlackwellIRProgram prog;
38static uint8_t sm121[16384];
39
40typedef struct {
41 const char *dir; /* NULL: unset; "@N": a folder name making "<dir>/<pid>-0.sm121.bin" N bytes long */
42 int kind; /* program: see build() */
43 size_t count;
44 int rc;
45 size_t bytes, len, fill;
46 long poke;
47 int repeat; /* captures in a row (serial 0..repeat-1); negative: safe, unsafe, safe */
48} Case;
49
50static void build(int kind, size_t count)
51{
52 memset(&prog, 0, sizeof(prog));
53 prog.count = count;
54 for (size_t i = 0; i < count; i++) {
55 prog.insns[i].op = (BlackwellIROpcode)(i % (BW_IR_MUFU_LG2 + 1));
56 prog.insns[i].imm = (uint32_t)(i * 2654435761u);
57 }
58 if (!count) return;
59 BlackwellIRInsn *last = &prog.insns[count - 1];
60 switch (kind) {
61 case 1: last->op = BW_IR_MUFU_LG2; break;
62 case 2: last->op = BW_IR_SHFL_BFLY; break;
63 case 3: last->op = BW_IR_ATOMG_ADD_F32_STRONG_GPU; last->imm = 0xffffffffu; break;
64 case 4: last->op = BW_IR_HMMA_BF16; last->imm = OMEGA_QMMA_MARK; break;
65 case 5: last->op = BW_IR_HMMA_BF16; last->imm = OMEGA_QMMA_MARK ^ 1u; break;
66 case 6: last->op = BW_IR_HMMA_BF16; last->imm = OMEGA_QMMA_MARK_F2E4; break;
67 case 7: last->op = BW_IR_FMNMX_MAX; last->imm = OMEGA_QMMA_MARK_F2E4; break;
68 case 8: last->op = BW_IR_FMNMX_MAX; last->imm = OMEGA_QMMA_MARK; break;
69 case 9: last->op = BW_IR_FMNMX_MAX; last->imm = OMEGA_QMMA_MARK_F2E4 + 1u; break;
70 case 10: prog.insns[0].op = BW_IR_HMMA_BF16; prog.insns[0].imm = OMEGA_QMMA_MARK; break;
71 default: break;
72 }
73}
74
75/* -DAMPCORP_GCOV (coverage build only): a child ends in _exit or abort, neither of which writes gcov data,
76 * so dump it explicitly on both paths. */
77#ifdef AMPCORP_GCOV
78extern void __gcov_dump(void);
79static void on_abort(int sig) { __gcov_dump(); signal(sig, SIG_DFL); raise(sig); }
80#define GCOV_START() signal(SIGABRT, on_abort)
81#define GCOV_DUMP() __gcov_dump()
82#else
83#define GCOV_START() ((void)0)
84#define GCOV_DUMP() ((void)0)
85#endif
86
87static unsigned ndig(unsigned long v) { unsigned n = 1; while (v /= 10) n++; return n; }
88
89static void child(const Case *c)
90{
91 char dir[8192];
92 GCOV_START();
93 if (c->dir && c->dir[0] == '@') {
94 /* "<dir>/<pid>-0.sm121.bin": dir length = N - pid digits - 13 */
95 long n = atol(c->dir + 1) - (long)ndig((unsigned long)getpid()) - 13;
96 memset(dir, 'x', (size_t)n);
97 dir[n] = 0;
98 for (long i = 200; i < n; i += 201) dir[i] = '/';
99 setenv("OMEGA_AMPERE_CORPUS_DIR", dir, 1);
100 } else if (c->dir && c->dir[0] == '!') {
101 /* "!SUFFIX": folder d, with "d/<pid>-0.SUFFIX" a link to /dev/full, so the write fails (ENOSPC) */
102 snprintf(dir, sizeof dir, "d/%ld-0.%s", (long)getpid(), c->dir + 1);
103 if (symlink("/dev/full", dir)) _exit(9);
104 setenv("OMEGA_AMPERE_CORPUS_DIR", "d", 1);
105 } else if (c->dir) {
106 setenv("OMEGA_AMPERE_CORPUS_DIR", c->dir, 1);
107 } else {
108 unsetenv("OMEGA_AMPERE_CORPUS_DIR");
109 }
110 build(c->kind, c->count);
111 enc.rc = c->rc; enc.bytes = c->bytes; enc.fill = c->fill; enc.poke = c->poke;
112 int reps = c->repeat < 0 ? 3 : c->repeat;
113 const char *safe = getenv("OMEGA_AMPERE_CORPUS_DIR");
114 char keep[8192];
115 snprintf(keep, sizeof keep, "%s", safe ? safe : "");
116 for (int r = 0; r < reps; r++) {
117 if (c->repeat < 0) setenv("OMEGA_AMPERE_CORPUS_DIR", r == 1 ? "d/../d" : keep, 1);
118 omega_ampere_corpus_capture(&prog, sm121, c->len);
119 }
120 printf(" encoder calls %ld\n", calls);
121 fflush(stdout);
122 GCOV_DUMP();
123 _exit(0);
124}
125
126static uint64_t fnv(const unsigned char *p, size_t n)
127{
128 uint64_t h = 1469598103934665603ull;
129 for (size_t i = 0; i < n; i++) h = (h ^ p[i]) * 1099511628211ull;
130 return h;
131}
132
133/* Print s with every occurrence of the decimal pid replaced by PID. */
134static void norm(FILE *o, const char *s, pid_t pid)
135{
136 char ps[32];
137 int pl = snprintf(ps, sizeof ps, "%ld", (long)pid);
138 while (*s) {
139 if (!strncmp(s, ps, (size_t)pl)) { fputs("PID", o); s += pl; } else fputc(*s++, o);
140 }
141}
142
143static int cmpstr(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); }
144
145static void listdir(const char *path, pid_t pid)
146{
147 DIR *d = opendir(path);
148 if (!d) return;
149 char *names[64];
150 int n = 0;
151 for (struct dirent *e; (e = readdir(d)) && n < 64;)
152 if (e->d_name[0] != '.' && e->d_type != DT_DIR) names[n++] = strdup(e->d_name);
153 closedir(d);
154 qsort(names, (size_t)n, sizeof names[0], cmpstr);
155 for (int i = 0; i < n; i++) {
156 char full[4096];
157 snprintf(full, sizeof full, "%s/%s", path, names[i]);
158 struct stat lst;
159 int entry = open(full, O_PATH | O_NOFOLLOW | O_CLOEXEC);
160 int is_link = entry >= 0 && fstat(entry, &lst) == 0 && S_ISLNK(lst.st_mode);
161 if (entry >= 0) close(entry);
162 if (is_link) {
163 printf(" link ");
164 norm(stdout, names[i], pid);
165 printf("\n");
166 unlink(full);
167 free(names[i]);
168 continue;
169 }
170 FILE *f = fopen(full, "rb");
171 static unsigned char buf[1 << 16];
172 size_t sz = f ? fread(buf, 1, sizeof buf, f) : 0;
173 if (f) fclose(f);
174 printf(" file ");
175 norm(stdout, names[i], pid);
176 printf(" %zu %016llx\n", sz, (unsigned long long)fnv(buf, sz));
177 unlink(full);
178 free(names[i]);
179 }
180}
181
182static const Case cases[] = {
183 /* the folder */
184 { NULL, 0, 4, 0, 32, 32, 32, -1, 1 },
185 { "", 0, 4, 0, 32, 32, 32, -1, 1 },
186 { "d", 0, 4, 0, 32, 32, 32, -1, 1 },
187 { "./d", 0, 4, 0, 32, 32, 32, -1, 1 },
188 { "d/", 0, 4, 0, 32, 32, 32, -1, 1 },
189 { "d//", 0, 4, 0, 32, 32, 32, -1, 1 },
190 { "e/../d", 0, 4, 0, 32, 32, 32, -1, 1 },
191 { "../d", 0, 4, 0, 32, 32, 32, -1, 1 },
192 { "d/..", 0, 4, 0, 32, 32, 32, -1, 1 },
193 { "d/../", 0, 4, 0, 32, 32, 32, -1, 1 },
194 { "..", 0, 4, 0, 32, 32, 32, -1, 1 },
195 { "//..//", 0, 4, 0, 32, 32, 32, -1, 1 },
196 { "d/...", 0, 4, 0, 32, 32, 32, -1, 1 },
197 { "d/.x", 0, 4, 0, 32, 32, 32, -1, 1 },
198 { "d/x.", 0, 4, 0, 32, 32, 32, -1, 1 },
199 { "d/.", 0, 4, 0, 32, 32, 32, -1, 1 },
200 { "d/a..", 0, 4, 0, 32, 32, 32, -1, 1 },
201 { "d/..a", 0, 4, 0, 32, 32, 32, -1, 1 },
202 { "d\\e", 0, 4, 0, 32, 32, 32, -1, 1 },
203 { "\\", 0, 4, 0, 32, 32, 32, -1, 1 },
204 { "d/\\..", 0, 4, 0, 32, 32, 32, -1, 1 },
205 { "nofolder", 0, 4, 0, 32, 32, 32, -1, 1 },
206 { "@4095", 0, 4, 0, 32, 32, 32, -1, 1 },
207 { "@4096", 0, 4, 0, 32, 32, 32, -1, 1 },
208 { "@4097", 0, 4, 0, 32, 32, 32, -1, 1 },
209 { "@4097", 2, 4, -2, 0, 32, 0, -1, 1 },
210 { "@4098", 2, 4, -2, 0, 32, 0, -1, 1 },
211 /* the serial */
212 { "d", 0, 4, 0, 32, 32, 32, -1, 3 },
213 { "d", 0, 4, 0, 32, 32, 32, -1, -1 },
214 { "d", 0, 4, 0, 32, 32, 32, -1, 12 },
215 /* the program and the encoder outcome */
216 { "d", 0, 0, 0, 0, 0, 0, -1, 1 },
217 { "d", 0, 0, 0, 16, 0, 16, -1, 1 },
218 { "d", 0, 1024, 0, 16384, 16384, 16384, -1, 1 },
219 { "d", 1, 9, 0, 144, 144, 144, -1, 1 },
220 { "d", 2, 9, -2, 0, 144, 0, -1, 1 },
221 { "d", 3, 9, -2, 0, 144, 0, -1, 1 },
222 { "d", 4, 9, -2, 0, 144, 0, -1, 1 },
223 { "d", 5, 9, 0, 144, 144, 144, -1, 1 },
224 { "d", 6, 9, 0, 144, 144, 144, -1, 1 },
225 { "d", 7, 9, -2, 0, 144, 0, -1, 1 },
226 { "d", 8, 9, 0, 144, 144, 144, -1, 1 },
227 { "d", 9, 9, 0, 144, 144, 144, -1, 1 },
228 { "d", 10, 9, -2, 0, 144, 0, -1, 1 },
229 { "d", 10, 1024, -2, 0, 144, 0, -1, 1 },
230 /* a refusal that is not clean */
231 { "d", 4, 9, -2, 0, 144, 0, 0, 1 },
232 { "d", 4, 9, -2, 0, 144, 0, 16383, 1 },
233 { "d", 4, 9, -2, 0, 144, 0, 8191, 1 },
234 { "d", 4, 9, -2, 1, 144, 0, -1, 1 },
235 { "d", 4, 9, -1, 0, 144, 0, -1, 1 },
236 { "d", 4, 9, 0, 0, 144, 0, -1, 1 },
237 { "d", 2, 9, 0, 144, 144, 144, -1, 1 },
238 /* an existing kernel the SM86 encoder does not take */
239 { "d", 0, 9, -2, 0, 144, 0, -1, 1 },
240 { "d", 0, 9, 0, 143, 144, 143, -1, 1 },
241 { "d", 0, 9, 0, 145, 144, 145, -1, 1 },
242 { "d", 0, 9, 7, 144, 144, 144, -1, 1 },
243 { "d", 0, 9, -2147483647 - 1, 144, 144, 144, -1, 1 },
244 { "d", 0, 9, 2147483647, 144, 144, 144, -1, 1 },
245 { "d", 0, 1024, -1, 16384, 16384, 16384, -1, 1 },
246 { "d", 0, 9, 0, 0, 0, 0, -1, 1 },
247 /* a write that fails: in fclose (buffered, short data) and in fwrite (longer than the stdio buffer) */
248 { "!refused", 4, 9, -2, 0, 144, 0, -1, 1 },
249 { "!sm121.bin", 0, 1024, 0, 16384, 16384, 16384, -1, 1 },
250 { "!ir", 0, 9, 0, 144, 144, 144, -1, 1 },
251};
252
253int main(void)
254{
255 for (size_t i = 0; i < sizeof sm121; i++) sm121[i] = (uint8_t)(i * 13 + 5);
256 for (size_t i = 0; i < sizeof cases / sizeof cases[0]; i++) {
257 const Case *c = &cases[i];
258 char cwd[64];
259 snprintf(cwd, sizeof cwd, "case%zu", i);
260 mkdir(cwd, 0700);
261 if (chdir(cwd)) return 2;
262 mkdir("d", 0700);
263 mkdir("e", 0700);
264 printf("case %zu\n", i);
265 fflush(stdout);
266 int pfd[2];
267 if (pipe(pfd)) return 2;
268 pid_t pid = fork();
269 if (pid == 0) {
270 dup2(pfd[1], 2);
271 close(pfd[0]);
272 child(c);
273 }
274 close(pfd[1]);
275 static char err[1 << 16];
276 size_t en = 0;
277 for (ssize_t r; (r = read(pfd[0], err + en, sizeof err - 1 - en)) > 0;) en += (size_t)r;
278 err[en] = 0;
279 close(pfd[0]);
280 int st = 0;
281 waitpid(pid, &st, 0);
282 printf(" %s %d\n", WIFSIGNALED(st) ? "signal" : "exit",
283 WIFSIGNALED(st) ? WTERMSIG(st) : WEXITSTATUS(st));
284 if (en) { printf(" stderr "); norm(stdout, err, pid); if (err[en - 1] != '\n') printf("\n"); }
285 listdir("d", pid);
286 listdir(".", pid);
287 rmdir("d");
288 rmdir("e");
289 if (chdir("..")) return 2;
290 rmdir(cwd);
291 }
292 return 0;
293}