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 / codegen_base / differential.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include "omega_gpu_om_ir.h" |
| 3 | #include "omega_bw_fields.h" |
| 4 | #include "omega_cubin.h" |
| 5 | #include <assert.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | int legacy_omega_gpu_om_ir_replay(OmegaOmIrUnit, uint64_t, uint64_t, uint64_t, uint64_t, |
| 11 | BlackwellIRProgram *, uint64_t *); |
| 12 | int legacy_omega_bw_encode_program(const BlackwellIRProgram *, uint8_t *, size_t, size_t *); |
| 13 | int sass_omega_bw_encode_program(const BlackwellIRProgram *, uint8_t *, size_t, size_t *); |
| 14 | int legacy_omega_blackwell_encode_one(const BlackwellIRInsn *, uint32_t *); |
| 15 | int legacy_omega_gpu_om_ir_build(uint64_t, uint64_t, uint64_t, uint64_t, uint32_t, |
| 16 | BlackwellIRProgram *, uint8_t *, size_t, OmegaBlackwellKernel *, uint32_t *, uint64_t *); |
| 17 | int legacy_omega_gpu_om_ir_data(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t *); |
| 18 | const char *legacy_omega_gpu_om_ir_rc_name(int); |
| 19 | static uint64_t tape[OMEGA_OM_IR_WORDS], metadata[OMEGA_OM_IR_META]; |
| 20 | static uint32_t emit_status; |
| 21 | static uint64_t emit_result; |
| 22 | static unsigned cases, images; |
| 23 | static uint32_t emitter(uint64_t *r, uint64_t k, uint64_t a, uint64_t b, uint64_t c, |
| 24 | void *ins, uint64_t ni, void *meta, uint64_t nm) { |
| 25 | (void)k; (void)a; (void)b; (void)c; |
| 26 | assert(ni == OMEGA_OM_IR_WORDS && nm == OMEGA_OM_IR_META); |
| 27 | memcpy(ins, tape, sizeof tape); memcpy(meta, metadata, sizeof metadata); *r = emit_result; return emit_status; |
| 28 | } |
| 29 | static void reset(void) { |
| 30 | memset(tape, 0, sizeof tape); memset(metadata, 0, sizeof metadata); |
| 31 | metadata[0] = 1; emit_result = 0; emit_status = 0; |
| 32 | for (unsigned i = 0; i < BW_MAX_IR_INSNS; ++i) { |
| 33 | tape[8 * i] = BW_IR_EXIT; |
| 34 | for (unsigned f = 1; f <= 5; ++f) tape[8 * i + f] = UINT64_MAX; |
| 35 | } |
| 36 | for (unsigned v = 0; v < BW_MAX_VREGS; ++v) metadata[64 + v] = 1; |
| 37 | for (unsigned u = 0; u < BW_MAX_UVREGS; ++u) metadata[320 + u] = 1; |
| 38 | } |
| 39 | static void compare_images(const BlackwellIRProgram *p, const BlackwellIRProgram *q) { |
| 40 | uint8_t a[OMEGA_BW_CODE_CAP_MAX], b[OMEGA_BW_CODE_CAP_MAX], c[OMEGA_BW_CODE_CAP_MAX]; |
| 41 | size_t an = 0, bn = 0, cn = 0; |
| 42 | int ar = omega_bw_encode_program(p, a, sizeof a, &an); |
| 43 | int br = legacy_omega_bw_encode_program(q, b, sizeof b, &bn); |
| 44 | int cr = sass_omega_bw_encode_program(q, c, sizeof c, &cn); |
| 45 | assert(ar == br && ar == cr); |
| 46 | if (ar) return; |
| 47 | #ifdef CODEGEN_BASE_RED |
| 48 | a[0] ^= 1; |
| 49 | #endif |
| 50 | assert(an == bn && memcmp(a, b, an) == 0); |
| 51 | assert(an == cn && memcmp(a, c, an) == 0); |
| 52 | uint8_t *ac = NULL, *bc = NULL; size_t al = 0, bl = 0; char err[128]; |
| 53 | assert(omega_cubin_pack("codegen_base", a, an, NULL, 0, 1024, 64, 0, &ac, &al, err, sizeof err) == 0); |
| 54 | assert(omega_cubin_pack("codegen_base", b, bn, NULL, 0, 1024, 64, 0, &bc, &bl, err, sizeof err) == 0); |
| 55 | assert(al == bl && memcmp(ac, bc, al) == 0); |
| 56 | omega_cubin_free(ac); omega_cubin_free(bc); ++images; |
| 57 | } |
| 58 | static int check(int encode) { |
| 59 | BlackwellIRProgram p, q; uint64_t pe = UINT64_MAX, qe = UINT64_MAX; |
| 60 | memset(&p, 0, sizeof p); memset(&q, 0, sizeof q); |
| 61 | int a = omega_gpu_om_ir_replay(emitter, 0, 0, 0, 0, &p, &pe); |
| 62 | int b = legacy_omega_gpu_om_ir_replay(emitter, 0, 0, 0, 0, &q, &qe); |
| 63 | if (a != b) { fprintf(stderr, "case %u replay %d C %d\n", cases, a, b); abort(); } |
| 64 | assert(pe == qe); ++cases; |
| 65 | if (a == 0) { |
| 66 | assert(memcmp(&p, &q, sizeof p) == 0); |
| 67 | if (encode) compare_images(&p, &q); |
| 68 | } |
| 69 | return a; |
| 70 | } |
| 71 | static void boundaries(void) { |
| 72 | static const struct { unsigned slot; uint64_t values[16]; unsigned count; } fields[] = { |
| 73 | {0, {0,1,2,7,8,9,31,32,33,1023,1024,1025,UINT32_MAX}, 13}, |
| 74 | {1, {0,1,2,159,160,161,255,256,257,UINT32_MAX}, 10}, |
| 75 | {2, {0,1,2,31,32,33,UINT32_MAX}, 7}, |
| 76 | {5, {0,1,2,23,24,25,UINT32_MAX}, 7}, |
| 77 | {6, {0,1,127,128,129,245,246,247,UINT32_MAX}, 9}, |
| 78 | {7, {0,1,2,UINT32_MAX}, 4} |
| 79 | }; |
| 80 | reset(); assert(check(1) == 0); |
| 81 | for (unsigned f = 0; f < sizeof fields / sizeof fields[0]; ++f) |
| 82 | for (unsigned v = 0; v < fields[f].count; ++v) { |
| 83 | reset(); metadata[fields[f].slot] = fields[f].values[v]; |
| 84 | check(fields[f].slot == 0); |
| 85 | } |
| 86 | static const uint64_t widths[] = {0,1,2,3,4,5,UINT32_MAX}; |
| 87 | for (unsigned v = 0; v < sizeof widths / sizeof widths[0]; ++v) { |
| 88 | reset(); metadata[1] = 1; metadata[64] = widths[v]; check(0); |
| 89 | reset(); metadata[2] = 1; metadata[320] = widths[v]; check(0); |
| 90 | } |
| 91 | static const uint64_t regs[] = {0,1,30,31,32,33,254,255,256,257,UINT32_MAX,UINT64_MAX}; |
| 92 | for (unsigned f = 1; f <= 5; ++f) |
| 93 | for (unsigned v = 0; v < sizeof regs / sizeof regs[0]; ++v) { |
| 94 | reset(); tape[f] = regs[v]; check(0); |
| 95 | } |
| 96 | for (unsigned op = 0; op <= (unsigned)BW_IR_LAST + 1; ++op) { |
| 97 | reset(); tape[0] = op; check(1); |
| 98 | } |
| 99 | reset(); tape[0] = UINT32_MAX; check(0); |
| 100 | /* All control/flag wire bits go through the real encoder, not only the field unit. */ |
| 101 | for (unsigned bit = 0; bit < 64; ++bit) { |
| 102 | reset(); metadata[1] = 1; tape[0] = BW_IR_MOV_IMM; tape[1] = 0; |
| 103 | tape[6] = UINT64_C(1) << bit; check(1); |
| 104 | tape[7] = UINT64_C(1) << bit; check(1); |
| 105 | } |
| 106 | reset(); metadata[7] = 1; metadata[1] = 161; assert(check(0) == OMEGA_OM_IR_LIVE_RANGE); |
| 107 | for (unsigned s = 0; s < 4; ++s) { |
| 108 | reset(); emit_status = s; metadata[4] = 42; check(0); |
| 109 | reset(); emit_result = s; metadata[4] = 42; check(0); |
| 110 | } |
| 111 | } |
| 112 | static void loops(void) { |
| 113 | reset(); metadata[0] = 5; metadata[1] = 2; metadata[5] = 1; |
| 114 | metadata[16] = 1; metadata[17] = 3; |
| 115 | tape[0] = BW_IR_MOV_IMM; tape[1] = 0; tape[6] = 42; |
| 116 | tape[8] = BW_IR_MOV_IMM; tape[9] = 1; |
| 117 | tape[16] = BW_IR_IADD3; tape[17] = 1; tape[18] = 0; |
| 118 | assert(check(1) == 0); /* v0 is live into the loop and must extend from record 2 to 3 */ |
| 119 | tape[34] = 1; assert(check(0) == OMEGA_OM_IR_LOOP); |
| 120 | tape[33] = 1; tape[34] = UINT64_MAX; assert(check(0) == 0); /* redefine before a later read */ |
| 121 | metadata[0] = 6; tape[42] = 1; assert(check(0) == 0); |
| 122 | tape[39] = 1; assert(check(0) == OMEGA_OM_IR_LOOP); /* uniform destination is not a redefinition */ |
| 123 | tape[39] = 0; tape[42] = UINT64_MAX; |
| 124 | for (unsigned f = 2; f <= 4; ++f) { |
| 125 | tape[33] = UINT64_MAX; tape[32 + f] = 1; |
| 126 | assert(check(0) == OMEGA_OM_IR_LOOP); |
| 127 | tape[32 + f] = UINT64_MAX; |
| 128 | } |
| 129 | /* Definition after the loop, pre-loop-only use, and a use after closure. */ |
| 130 | reset(); metadata[0] = 5; metadata[1] = 2; metadata[5] = 1; |
| 131 | metadata[16] = 1; metadata[17] = 3; |
| 132 | tape[0] = BW_IR_MOV_IMM; tape[1] = 0; |
| 133 | tape[32] = BW_IR_MOV_IMM; tape[33] = 1; assert(check(1) == 0); |
| 134 | tape[34] = 0; assert(check(1) == 0); |
| 135 | tape[34] = UINT64_MAX; tape[26] = 0; assert(check(1) == 0); /* last use at closure */ |
| 136 | reset(); metadata[0] = 5; metadata[1] = 1; metadata[7] = 1; |
| 137 | tape[0] = BW_IR_MOV_IMM; tape[1] = 0; |
| 138 | static const uint64_t hints[] = {0,1,3,4,5,6,UINT32_MAX}; |
| 139 | for (unsigned i = 0; i < sizeof hints / sizeof hints[0]; ++i) { |
| 140 | metadata[352] = hints[i]; check(0); |
| 141 | } |
| 142 | reset(); metadata[1] = 1; metadata[7] = 1; metadata[352] = 1; |
| 143 | assert(check(0) == OMEGA_OM_IR_LIVE_RANGE); /* never defined */ |
| 144 | static const uint64_t endpoints[] = {0,1,2,3,4,5,UINT32_MAX,UINT64_MAX}; |
| 145 | for (unsigned s = 0; s < sizeof endpoints / sizeof endpoints[0]; ++s) |
| 146 | for (unsigned e = 0; e < sizeof endpoints / sizeof endpoints[0]; ++e) { |
| 147 | reset(); metadata[0] = 5; metadata[1] = 1; metadata[5] = 1; |
| 148 | metadata[16] = endpoints[s]; metadata[17] = endpoints[e]; check(0); |
| 149 | } |
| 150 | reset(); metadata[0] = 5; metadata[5] = 2; metadata[17] = 3; metadata[19] = 2; |
| 151 | assert(check(0) == OMEGA_OM_IR_REPLAY); |
| 152 | metadata[19] = 3; assert(check(0) == 0); /* two loops sharing their closing instruction */ |
| 153 | } |
| 154 | static void wire_fields(void) { |
| 155 | static const uint32_t values[] = {0,1,7,8,9,15,16,17,31,32,33,255,256,257,UINT32_MAX}; |
| 156 | for (unsigned slot = 0; slot < 16; ++slot) |
| 157 | for (unsigned v = 0; v < sizeof values / sizeof values[0]; ++v) { |
| 158 | uint64_t raw[16] = {0}, rec[8] = {0}, r = 0, expected[8] = {0}; |
| 159 | raw[slot] = values[v]; |
| 160 | for (unsigned f = 0; f < 6; ++f) expected[f] = raw[f]; |
| 161 | expected[6] = raw[6] | raw[7] << 32; |
| 162 | expected[7] = raw[8] | raw[9] << 1 | raw[10] << 2 | raw[11] << 8 | |
| 163 | raw[12] << 16 | raw[13] << 24 | raw[14] << 32 | raw[15] << 40; |
| 164 | assert(omega_cg_fields(&r, 0, raw, 16, rec, 8) == 0 && r == 0); |
| 165 | assert(memcmp(rec, expected, sizeof rec) == 0); |
| 166 | } |
| 167 | for (unsigned bit = 0; bit < 64; ++bit) { |
| 168 | uint64_t raw[16] = {0}, rec[8] = {0}, r = 0; |
| 169 | rec[6] = rec[7] = UINT64_C(1) << bit; |
| 170 | assert(omega_cg_fields(&r, 1, raw, 16, rec, 8) == 0 && r == 0); |
| 171 | assert(raw[6] == (uint32_t)rec[6] && raw[7] == rec[6] >> 32); |
| 172 | assert(raw[8] == (rec[7] & 1) && raw[9] == ((rec[7] >> 1) & 1) && raw[10] == ((rec[7] >> 2) & 1)); |
| 173 | assert(raw[11] == ((rec[7] >> 48) & 255)); |
| 174 | for (unsigned f = 0; f < 4; ++f) assert(raw[12 + f] == ((rec[7] >> (16 + 8 * f)) & 255)); |
| 175 | } |
| 176 | uint64_t raw[16], rec[8] = {0}, r = 0; |
| 177 | for (unsigned i = 0; i < 16; ++i) raw[i] = i + 1; |
| 178 | assert(omega_cg_fields(&r, 2, raw, 16, rec, 8) == 0 && r == 0); |
| 179 | assert(raw[0] == 0x04030201 && raw[1] == 0x08070605 && raw[2] == 0x0c0b0a09 && raw[3] == 0x100f0e0d); |
| 180 | assert(omega_cg_fields(&r, 3, raw, 16, rec, 8) == 0 && r == OMEGA_OM_IR_REPLAY); |
| 181 | for (unsigned v = 0; v < BW_MAX_VREGS; ++v) { |
| 182 | BlackwellIRInsn in = { .op = BW_IR_MOV_IMM, .dst_vreg = (int)v, .src1_vreg = -1, |
| 183 | .src2_vreg = -1, .src3_vreg = -1, .ureg = -1, .imm = UINT32_MAX }; |
| 184 | uint32_t a[4], b[4]; |
| 185 | assert(omega_blackwell_encode_one(&in, a) == legacy_omega_blackwell_encode_one(&in, b)); |
| 186 | assert(memcmp(a, b, sizeof a) == 0); |
| 187 | } |
| 188 | } |
| 189 | static void budgets(void) { |
| 190 | static const uint32_t vals[] = {0,1,2,7,8,9,63,64,65,127,128,129,245,246,247,1023,1024,1025,UINT32_MAX}; |
| 191 | for (unsigned pi = 0; pi < sizeof vals / sizeof vals[0]; ++pi) |
| 192 | for (unsigned ti = 0; ti < sizeof vals / sizeof vals[0]; ++ti) { |
| 193 | uint32_t peak = vals[pi], th = vals[ti], decl = omega_bw_gpr_declared(peak); |
| 194 | if (decl < 64) decl = 64; |
| 195 | uint64_t expected = th == 0 ? UINT64_MAX : |
| 196 | peak > BW_PHYS_GPR_MAX || !omega_bw_gpr_declared_ok(decl, peak) || (uint64_t)decl * th > 65536 ? 0 : decl; |
| 197 | uint64_t r = 0; |
| 198 | assert(omega_cg_budget(&r, peak, omega_bw_gpr_declared(peak), th) == 0); |
| 199 | assert(r == expected); |
| 200 | } |
| 201 | } |
| 202 | static void builders(void) { |
| 203 | static const uint32_t threads[] = {0,1,63,64,65,1024,1025,UINT32_MAX}; |
| 204 | BlackwellIRProgram p, q; uint8_t a[OMEGA_BW_CODE_CAP_MAX], b[OMEGA_BW_CODE_CAP_MAX]; |
| 205 | OmegaBlackwellKernel x, y; uint32_t pd, qd; uint64_t pe, qe; |
| 206 | for (unsigned kind = 0; kind <= 3; ++kind) |
| 207 | for (unsigned arg = 0; arg <= 12; ++arg) |
| 208 | for (unsigned t = 0; t < sizeof threads / sizeof threads[0]; ++t) |
| 209 | for (unsigned cap = 0; cap < 2; ++cap) { |
| 210 | size_t n = cap ? sizeof a : 0; |
| 211 | int ar = omega_gpu_om_ir_build(kind, arg, 0, 0, threads[t], &p, a, n, &x, &pd, &pe); |
| 212 | int br = legacy_omega_gpu_om_ir_build(kind, arg, 0, 0, threads[t], &q, b, n, &y, &qd, &qe); |
| 213 | assert(ar == br); |
| 214 | if (!ar) { |
| 215 | assert(pd == qd && x.code_size == y.code_size); |
| 216 | assert(memcmp(a, b, x.code_size) == 0 && memcmp(&p, &q, sizeof p) == 0); |
| 217 | } |
| 218 | } |
| 219 | for (unsigned kind = 0; kind <= 3; ++kind) { |
| 220 | uint64_t a[OMEGA_OM_IR_WORDS], b[OMEGA_OM_IR_WORDS]; |
| 221 | assert(omega_gpu_om_ir_data(kind, 2, 0, 0, a) == legacy_omega_gpu_om_ir_data(kind, 2, 0, 0, b)); |
| 222 | assert(memcmp(a, b, sizeof a) == 0); |
| 223 | } |
| 224 | for (int rc = -1; rc <= 12; ++rc) |
| 225 | assert(strcmp(omega_gpu_om_ir_rc_name(rc), legacy_omega_gpu_om_ir_rc_name(rc)) == 0); |
| 226 | assert(omega_gpu_om_ir_data(0, 0, 0, 0, NULL) == OMEGA_OM_IR_REPLAY); |
| 227 | assert(legacy_omega_gpu_om_ir_data(0, 0, 0, 0, NULL) == OMEGA_OM_IR_REPLAY); |
| 228 | } |
| 229 | int main(void) { |
| 230 | boundaries(); loops(); wire_fields(); budgets(); builders(); |
| 231 | printf("CODEGEN BASE PASS: %u replay partitions; %u byte-identical SASS/cubin images; wire bits and budget boundaries\n", cases, images); |
| 232 | return 0; |
| 233 | } |