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
CUDApp / evidence / tools-om-2 / 2026-11-06 / coverage / ampere_corpus.c.gcov
| 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 |
| 26 | function 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; |
| 30 | branch 0 taken 76 (fallthrough) |
| 31 | branch 1 taken 1 |
| 32 | branch 2 taken 1 (fallthrough) |
| 33 | branch 3 taken 75 |
| 34 | 75: 25: if (strchr(dir, '\\')) return 0; |
| 35 | branch 0 taken 3 (fallthrough) |
| 36 | branch 1 taken 72 |
| 37 | 250: 26: for (const char *component = dir; *component;) { |
| 38 | branch 0 taken 185 |
| 39 | branch 1 taken 65 (fallthrough) |
| 40 | 301: 27: while (*component == '/') ++component; |
| 41 | branch 0 taken 116 |
| 42 | branch 1 taken 185 (fallthrough) |
| 43 | 185: 28: const char *start = component; |
| 44 | 20568: 29: while (*component && *component != '/') ++component; |
| 45 | branch 0 taken 20501 (fallthrough) |
| 46 | branch 1 taken 67 |
| 47 | branch 2 taken 20383 |
| 48 | branch 3 taken 118 (fallthrough) |
| 49 | 185: 30: if ((size_t)(component - start) == 2 && |
| 50 | branch 0 taken 9 (fallthrough) |
| 51 | branch 1 taken 176 |
| 52 | 9: 31: start[0] == '.' && start[1] == '.') return 0; |
| 53 | branch 0 taken 8 (fallthrough) |
| 54 | branch 1 taken 1 |
| 55 | branch 2 taken 7 (fallthrough) |
| 56 | branch 3 taken 1 |
| 57 | -: 32: } |
| 58 | 65: 33: return 1; |
| 59 | -: 34:} |
| 60 | -: 35: |
| 61 | function 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); |
| 66 | call 0 returned 112 |
| 67 | 112: 40: if (n < 0 || (size_t)n >= sizeof(path)) abort(); |
| 68 | branch 0 taken 112 (fallthrough) |
| 69 | branch 1 taken 0 |
| 70 | branch 2 taken 3 (fallthrough) |
| 71 | branch 3 taken 109 |
| 72 | call 4 returned 0 |
| 73 | 109: 41: int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); |
| 74 | call 0 returned 109 |
| 75 | 109: 42: if (fd < 0) { perror(path); abort(); } |
| 76 | branch 0 taken 8 (fallthrough) |
| 77 | branch 1 taken 101 |
| 78 | call 2 returned 8 |
| 79 | call 3 returned 0 |
| 80 | 101: 43: FILE *f = fdopen(fd, "wb"); |
| 81 | call 0 returned 101 |
| 82 | 101*: 44: if (!f) { perror(path); close(fd); abort(); } |
| 83 | branch 0 taken 0 (fallthrough) |
| 84 | branch 1 taken 101 |
| 85 | call 2 never executed |
| 86 | call 3 never executed |
| 87 | call 4 never executed |
| 88 | 101: 45: if (fwrite(data, 1, size, f) != size || fclose(f) != 0) abort(); |
| 89 | call 0 returned 101 |
| 90 | branch 1 taken 100 (fallthrough) |
| 91 | branch 2 taken 1 |
| 92 | call 3 returned 100 |
| 93 | branch 4 taken 2 (fallthrough) |
| 94 | branch 5 taken 98 |
| 95 | call 6 returned 0 |
| 96 | 98: 46:} |
| 97 | -: 47: |
| 98 | function 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"); |
| 102 | call 0 returned 77 |
| 103 | 83: 51: if (!is_safe_corpus_dir(dir)) return; |
| 104 | call 0 returned 77 |
| 105 | branch 1 taken 12 (fallthrough) |
| 106 | branch 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) { |
| 113 | branch 0 taken 4462 |
| 114 | branch 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; |
| 119 | branch 0 taken 5 (fallthrough) |
| 120 | branch 1 taken 4457 |
| 121 | 4462: 62: if ((in->op == BW_IR_HMMA_BF16 && in->imm == OMEGA_QMMA_MARK) || |
| 122 | branch 0 taken 80 (fallthrough) |
| 123 | branch 1 taken 4382 |
| 124 | branch 2 taken 70 (fallthrough) |
| 125 | branch 3 taken 10 |
| 126 | 4462: 63: (in->op == BW_IR_FMNMX_MAX && in->imm == OMEGA_QMMA_MARK_F2E4)) fp8 = 1; |
| 127 | branch 0 taken 71 (fallthrough) |
| 128 | branch 1 taken 4381 |
| 129 | branch 2 taken 1 (fallthrough) |
| 130 | branch 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); |
| 135 | call 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) { |
| 139 | branch 0 taken 54 (fallthrough) |
| 140 | branch 1 taken 11 |
| 141 | branch 2 taken 5 (fallthrough) |
| 142 | branch 3 taken 49 |
| 143 | 16: 71: if (rc != OMEGA_GPU_ENCODE_UNSUPPORTED || bytes != 0) abort(); |
| 144 | branch 0 taken 13 (fallthrough) |
| 145 | branch 1 taken 3 |
| 146 | branch 2 taken 1 (fallthrough) |
| 147 | branch 3 taken 12 |
| 148 | call 4 returned 0 |
| 149 | 172042: 72: for (size_t i = 0; i < sizeof(code); ++i) if (code[i] != 0xa5) abort(); |
| 150 | branch 0 taken 3 (fallthrough) |
| 151 | branch 1 taken 172030 |
| 152 | call 2 returned 0 |
| 153 | branch 3 taken 172033 |
| 154 | branch 4 taken 9 (fallthrough) |
| 155 | 9: 73: save(dir, id, "refused", metadata, prog->count * sizeof(metadata[0])); |
| 156 | call 0 returned 6 |
| 157 | 6: 74: return; |
| 158 | -: 75: } |
| 159 | 49: 76: if (rc != 0 || bytes != len) { |
| 160 | branch 0 taken 44 (fallthrough) |
| 161 | branch 1 taken 5 |
| 162 | branch 2 taken 3 (fallthrough) |
| 163 | branch 3 taken 41 |
| 164 | 8: 77: fprintf(stderr, "SM86 unexpectedly refused an existing kernel (%zu instructions, rc %d)\n", prog->count, rc); |
| 165 | call 0 returned 8 |
| 166 | 8: 78: abort(); |
| 167 | call 0 returned 0 |
| 168 | -: 79: } |
| 169 | 41: 80: save(dir, id, "sm121.bin", sm121, len); |
| 170 | call 0 returned 31 |
| 171 | 31: 81: save(dir, id, "sm86.bin", code, bytes); |
| 172 | call 0 returned 31 |
| 173 | 31: 82: save(dir, id, "ir", metadata, prog->count * sizeof(metadata[0])); |
| 174 | call 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 |