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 / tests / om_cubin_sweep_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* CUDApp: the cubin packer sweep of `make test-om-cubin`. Built with CUDAPP_MIGRATION_PENDING_CUBIN_DIFF, every |
| 3 | * omega_cubin_pack call below packs through om/cubin/cubin_elf.om and through the C writer and aborts on any |
| 4 | * difference of return code, length, bytes or message. This driver only chooses the descriptors and checks that |
| 5 | * the sweep reached every refusal class and enough images; it holds no expected bytes of its own. */ |
| 6 | #include "omega_cubin.h" |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | static unsigned long long calls, by_rc[8], image_bytes; |
| 13 | static uint8_t text[1u << 20]; |
| 14 | static uint64_t rng = 0x9e3779b97f4a7c15ull; |
| 15 | |
| 16 | static uint32_t next(void) { |
| 17 | rng ^= rng << 13; |
| 18 | rng ^= rng >> 7; |
| 19 | rng ^= rng << 17; |
| 20 | return (uint32_t)(rng >> 32); |
| 21 | } |
| 22 | |
| 23 | static int pack(const char *name, size_t tlen, const OmegaCubinParam *p, size_t np, uint32_t cbank, uint32_t gpr, |
| 24 | uint32_t shared) { |
| 25 | uint8_t *out = NULL; |
| 26 | size_t len = 0; |
| 27 | char err[256]; |
| 28 | int rc = omega_cubin_pack(name, name == NULL && tlen == 1 ? NULL : text, tlen, p, np, cbank, gpr, shared, &out, |
| 29 | &len, err, sizeof err); |
| 30 | calls++; |
| 31 | if (rc < 0 || rc > 7) { |
| 32 | fprintf(stderr, "FAIL: return code %d\n", rc); |
| 33 | exit(1); |
| 34 | } |
| 35 | by_rc[rc]++; |
| 36 | if (rc == OMEGA_CUBIN_OK) { |
| 37 | if (!out || len < 64 || memcmp(out, "\177ELF", 4) != 0) { |
| 38 | fprintf(stderr, "FAIL: image without an ELF header\n"); |
| 39 | exit(1); |
| 40 | } |
| 41 | image_bytes += len; |
| 42 | } else if (out || len || !err[0]) { |
| 43 | fprintf(stderr, "FAIL: refusal %d left an image or no message\n", rc); |
| 44 | exit(1); |
| 45 | } |
| 46 | omega_cubin_free(out); |
| 47 | return rc; |
| 48 | } |
| 49 | |
| 50 | /* n parameters of the sizes in sz (pointer when ptr bit i is set), packed in order with optional gaps. */ |
| 51 | static size_t layout(OmegaCubinParam *p, size_t n, int gaps) { |
| 52 | uint32_t end = OMEGA_CUBIN_PARAM_BASE; |
| 53 | static const uint32_t sizes[4] = { 1, 2, 4, 8 }; |
| 54 | for (size_t i = 0; i < n; i++) { |
| 55 | uint32_t s = sizes[next() % 4]; |
| 56 | uint32_t off = (end + s - 1) & ~(s - 1); |
| 57 | if (gaps && next() % 3 == 0) off += s * (next() % 3); |
| 58 | p[i].offset = off; |
| 59 | p[i].size = s; |
| 60 | p[i].is_pointer = (uint8_t)(s == 8 && next() % 2 ? 1 + next() % 255 : 0); |
| 61 | end = off + s; |
| 62 | } |
| 63 | return end; |
| 64 | } |
| 65 | |
| 66 | int main(void) { |
| 67 | OmegaCubinParam p[40]; |
| 68 | char name[80]; |
| 69 | for (size_t i = 0; i < sizeof text; i++) text[i] = (uint8_t)next(); |
| 70 | |
| 71 | /* Refusal classes, in the C order. */ |
| 72 | { |
| 73 | uint8_t *out = NULL; |
| 74 | size_t len = 0; |
| 75 | char err[64]; |
| 76 | if (omega_cubin_pack("k", text, 16, NULL, 0, 1024, 8, 0, NULL, &len, err, sizeof err) != OMEGA_CUBIN_E_ARG || |
| 77 | omega_cubin_pack("k", text, 16, NULL, 0, 1024, 8, 0, &out, NULL, err, sizeof err) != OMEGA_CUBIN_E_ARG) { |
| 78 | fprintf(stderr, "FAIL: null out accepted\n"); |
| 79 | return 1; |
| 80 | } |
| 81 | calls += 2; |
| 82 | by_rc[OMEGA_CUBIN_E_ARG] += 2; |
| 83 | } |
| 84 | pack(NULL, 16, NULL, 0, 1024, 8, 0); |
| 85 | pack(NULL, 1, NULL, 0, 1024, 8, 0); /* null text */ |
| 86 | static const char *bad[] = { "", "9k", "k-1", "k 1", "\xc3\xa9k", "_\x7f", "a.b" }; |
| 87 | for (size_t i = 0; i < sizeof bad / sizeof bad[0]; i++) pack(bad[i], 16, NULL, 0, 1024, 8, 0); |
| 88 | static const size_t tbad[] = { 0, 1, 15, 17, 24, 33, (1u << 24) + 16, (size_t)1 << 32 }; |
| 89 | for (size_t i = 0; i < sizeof tbad / sizeof tbad[0]; i++) pack("k", tbad[i], NULL, 0, 1024, 8, 0); |
| 90 | for (size_t n = 33; n <= 40; n++) pack("k", 16, p, n, 1024, 8, 0); |
| 91 | pack("k", 16, NULL, 0, 1024, 256, 0); |
| 92 | pack("k", 16, NULL, 0, 1024, 0xffffffffu, 0); |
| 93 | for (uint32_t s = 1; s < 4; s++) pack("k", 16, NULL, 0, 1024, 8, s); |
| 94 | pack("k", 16, NULL, 0, 1024, 8, 0xfffffffeu); |
| 95 | /* parameter refusals: overlap, unsorted, below the base, bad size, short pointer, misaligned */ |
| 96 | for (int k = 0; k < 4; k++) { |
| 97 | layout(p, 4, 0); |
| 98 | p[3].offset = p[2].offset + p[2].size - 1; |
| 99 | p[3].size = 1; |
| 100 | p[3].is_pointer = 0; |
| 101 | pack("k", 16, p, 4, 1024, 8, 0); |
| 102 | } |
| 103 | p[0] = (OmegaCubinParam){ 0x37c, 4, 0 }; |
| 104 | pack("k", 16, p, 1, 1024, 8, 0); |
| 105 | for (uint32_t s = 0; s <= 16; s++) { |
| 106 | p[0] = (OmegaCubinParam){ 0x380, s, 0 }; |
| 107 | pack("k", 16, p, 1, 1024, 8, 0); |
| 108 | p[0].is_pointer = 1; |
| 109 | pack("k", 16, p, 1, 1024, 8, 0); |
| 110 | } |
| 111 | for (uint32_t s = 2; s <= 8; s *= 2) |
| 112 | for (uint32_t o = 1; o < s; o++) { |
| 113 | p[0] = (OmegaCubinParam){ 0x380 + o, s, 0 }; |
| 114 | pack("k", 16, p, 1, 1024, 8, 0); |
| 115 | } |
| 116 | /* bank: parameter end beyond the bank, bank outside 0x380..0x400 */ |
| 117 | p[0] = (OmegaCubinParam){ 0x3f8, 8, 1 }; |
| 118 | pack("k", 16, p, 1, 0x3f4, 8, 0); |
| 119 | p[0] = (OmegaCubinParam){ 0xfffffff8u, 8, 0 }; |
| 120 | pack("k", 16, p, 1, 1024, 8, 0); |
| 121 | static const uint32_t cbad[] = { 0, 0x37f, 0x401, 0x800, 0xffffffffu }; |
| 122 | for (size_t i = 0; i < sizeof cbad / sizeof cbad[0]; i++) pack("k", 16, NULL, 0, cbad[i], 8, 0); |
| 123 | |
| 124 | /* Images. Parameter counts 0..32, three layouts each, the tight bank and the full one. */ |
| 125 | for (size_t n = 0; n <= OMEGA_CUBIN_MAX_PARAMS; n++) |
| 126 | for (int k = 0; k < 3; k++) { |
| 127 | uint32_t end = (uint32_t)layout(p, n, k); |
| 128 | if (end <= 1024) { |
| 129 | pack("kernel_sweep", 16 * (1 + next() % 64), p, n, 1024, next() % 256, 4 * (next() % 1024)); |
| 130 | if (end >= 0x380) pack("kernel_sweep", 32, p, n, end, 32, 0); |
| 131 | } else { |
| 132 | pack("kernel_sweep", 32, p, n, 1024, 32, 0); |
| 133 | } |
| 134 | } |
| 135 | /* Name lengths 1..65: 64 is the longest name, 65 is refused. */ |
| 136 | for (size_t l = 1; l <= OMEGA_CUBIN_MAX_NAME + 1; l++) { |
| 137 | for (size_t i = 0; i < l; i++) name[i] = "_aZz09Kq"[(i + l) % 8]; |
| 138 | name[0] = l % 2 ? '_' : 'k'; |
| 139 | name[l] = 0; |
| 140 | size_t n = l % 6; |
| 141 | layout(p, n, 1); |
| 142 | pack(name, 16 * l, p, n, 1024, (uint32_t)l, 0); |
| 143 | } |
| 144 | /* Text lengths from 16: every multiple of 16 to 4096, then powers of two to 1 MiB. */ |
| 145 | for (size_t t = 16; t <= 4096; t += 16) pack("text_len", t, NULL, 0, 0x380, 1, 0); |
| 146 | for (size_t t = 8192; t <= sizeof text; t *= 2) pack("text_len", t, NULL, 0, 0x400, 1, 0); |
| 147 | /* gpr 0..255 */ |
| 148 | for (uint32_t g = 0; g <= 255; g++) { |
| 149 | layout(p, 3, 0); |
| 150 | pack("gpr", 16, p, 3, 1024, g, g * 4); |
| 151 | } |
| 152 | /* shared reservations, large ones included */ |
| 153 | static const uint32_t shared[] = { 0, 4, 8, 252, 4096, 49152, 65536, 1u << 20, 100u << 20 }; |
| 154 | for (size_t i = 0; i < sizeof shared / sizeof shared[0]; i++) pack("shared", 64, NULL, 0, 1024, 16, shared[i]); |
| 155 | |
| 156 | static const char *names[8] = { "OK", "ARG", "TEXT", "PARAMS", "CBANK", "GPR", "SHARED", "MEM" }; |
| 157 | printf("cubin sweep: %llu calls, each packed by cubin_elf.om and the C writer and compared;", calls); |
| 158 | for (int r = 0; r < 8; r++) printf(" %s %llu", names[r], by_rc[r]); |
| 159 | printf("; %llu image bytes\n", image_bytes); |
| 160 | for (int r = 0; r < 7; r++) |
| 161 | if (by_rc[r] == 0) { |
| 162 | fprintf(stderr, "FAIL: the sweep reached no %s case\n", names[r]); |
| 163 | return 1; |
| 164 | } |
| 165 | if (by_rc[OMEGA_CUBIN_OK] < 400) { |
| 166 | fprintf(stderr, "FAIL: only %llu images\n", by_rc[OMEGA_CUBIN_OK]); |
| 167 | return 1; |
| 168 | } |
| 169 | return 0; |
| 170 | } |