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 / regalloc_om / differential.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include "omega_blackwell_codegen.h" |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | /* Reuse, rather than copy, the repository's canonical builder corpus. */ |
| 7 | #define main regalloc_canonical_fixture_driver |
| 8 | #include "../test_omega_codegen_freestanding.c" |
| 9 | #undef main |
| 10 | void legacy_omega_bw_ir_init(BlackwellIRProgram *); |
| 11 | int legacy_omega_bw_ir_alloc_vreg(BlackwellIRProgram *); |
| 12 | int legacy_omega_bw_ir_alloc_vreg64(BlackwellIRProgram *); |
| 13 | int legacy_omega_bw_ir_alloc_vreg128(BlackwellIRProgram *); |
| 14 | int legacy_omega_bw_ir_alloc_uvreg(BlackwellIRProgram *); |
| 15 | int legacy_omega_bw_ir_alloc_uvreg64(BlackwellIRProgram *); |
| 16 | int legacy_omega_bw_ir_append(BlackwellIRProgram *, const BlackwellIRInsn *); |
| 17 | int legacy_omega_bw_regalloc_solve(BlackwellIRProgram *); |
| 18 | int legacy_omega_bw_encode_program(const BlackwellIRProgram *, uint8_t *, size_t, size_t *); |
| 19 | int legacy_omega_blackwell_codegen_matmul_i32_prog(const OmegaMatMulSpec *, BlackwellIRProgram *); |
| 20 | int legacy_omega_blackwell_codegen_matmul_tensor_prog(const OmegaMatMulSpec *, BlackwellIRProgram *); |
| 21 | int legacy_omega_blackwell_codegen_matmul_tensor_loop_prog(const OmegaMatMulSpec *, uint32_t, int, BlackwellIRProgram *); |
| 22 | int legacy_omega_blackwell_codegen_matmul_frag_prog(const OmegaMatMulSpec *, uint32_t, uint32_t, int, BlackwellIRProgram *); |
| 23 | int legacy_omega_blackwell_codegen_matmul_frag_block_prog(const OmegaMatMulSpec *, uint32_t, uint32_t, uint32_t, uint32_t, int, BlackwellIRProgram *); |
| 24 | uint32_t legacy_omega_bw_gpr_declared(uint32_t); |
| 25 | int legacy_omega_bw_gpr_declared_ok(uint32_t, uint32_t); |
| 26 | int legacy_omega_bw_ir_set_gpr_limit(BlackwellIRProgram *, uint32_t); |
| 27 | void legacy_omega_matmul_frag_block_shape(uint32_t, uint32_t, uint32_t *, uint32_t *); |
| 28 | uint32_t legacy_omega_matmul_frag_block_unroll(uint32_t, uint32_t, uint32_t); |
| 29 | uint32_t legacy_omega_matmul_frag_unroll(uint32_t); |
| 30 | static BlackwellIRProgram a, b; |
| 31 | static uint8_t ca[BW_MAX_IR_INSNS * 16], cb[BW_MAX_IR_INSNS * 16]; |
| 32 | static unsigned checked, refused, encoded; |
| 33 | static void fail(const char *what, unsigned at) { fprintf(stderr,"FAIL %s case=%u\n",what,at); exit(1); } |
| 34 | static int same(void) { return memcmp(&a, &b, sizeof a) == 0; } |
| 35 | static void compare(int ar, int br, unsigned at) { |
| 36 | if (ar != br) fail("refusal", at); |
| 37 | if (!same()) { |
| 38 | for (unsigned i=0;i<BW_MAX_IR_INSNS;++i) |
| 39 | if(memcmp(&a.insns[i],&b.insns[i],sizeof a.insns[i])) { |
| 40 | fprintf(stderr,"insn=%u op=%d/%d imm=%u/%u dst=%d/%d src=%d,%d,%d/%d,%d,%d ctrl=%x/%x\n", |
| 41 | i,a.insns[i].op,b.insns[i].op,a.insns[i].imm,b.insns[i].imm, |
| 42 | a.insns[i].dst_vreg,b.insns[i].dst_vreg,a.insns[i].src1_vreg,a.insns[i].src2_vreg, |
| 43 | a.insns[i].src3_vreg,b.insns[i].src1_vreg,b.insns[i].src2_vreg,b.insns[i].src3_vreg, |
| 44 | a.insns[i].control,b.insns[i].control);break; |
| 45 | } |
| 46 | for(unsigned v=0;v<BW_MAX_VREGS;++v) |
| 47 | if(memcmp(&a.regalloc.intervals[v],&b.regalloc.intervals[v],sizeof a.regalloc.intervals[v])) { |
| 48 | fprintf(stderr,"interval=%u first=%d/%d last=%d/%d phys=%d/%d\n",v, |
| 49 | a.regalloc.intervals[v].first_def,b.regalloc.intervals[v].first_def, |
| 50 | a.regalloc.intervals[v].last_use,b.regalloc.intervals[v].last_use, |
| 51 | a.regalloc.intervals[v].phys_reg,b.regalloc.intervals[v].phys_reg);break; |
| 52 | } |
| 53 | fail("program/map/partial-state",at); |
| 54 | } |
| 55 | if (ar != 0) { ++refused; return; } |
| 56 | size_t na = 0, nb = 0; |
| 57 | ar = omega_bw_encode_program(&a, ca, sizeof ca, &na); |
| 58 | br = legacy_omega_bw_encode_program(&b, cb, sizeof cb, &nb); |
| 59 | if (ar != br) fail("encode-refusal", at); |
| 60 | if (ar == 0) { |
| 61 | if (na != nb || memcmp(ca,cb,na)) fail("encoded-bytes",at); |
| 62 | ++encoded; |
| 63 | } |
| 64 | } |
| 65 | static int alloc_one(BlackwellIRProgram *p, unsigned w, int old, int uni) { |
| 66 | if (uni) return old ? (w==2 ? legacy_omega_bw_ir_alloc_uvreg64(p) : legacy_omega_bw_ir_alloc_uvreg(p)) |
| 67 | : (w==2 ? omega_bw_ir_alloc_uvreg64(p) : omega_bw_ir_alloc_uvreg(p)); |
| 68 | if (old) return w==4 ? legacy_omega_bw_ir_alloc_vreg128(p) : w==2 ? legacy_omega_bw_ir_alloc_vreg64(p) : legacy_omega_bw_ir_alloc_vreg(p); |
| 69 | return w==4 ? omega_bw_ir_alloc_vreg128(p) : w==2 ? omega_bw_ir_alloc_vreg64(p) : omega_bw_ir_alloc_vreg(p); |
| 70 | } |
| 71 | /* Fixed-seed 64-bit LCG (Knuth MMIX constants), high 31 bits: the same case stream on |
| 72 | * every libc, unlike rand(). Values span rand()'s [0, 2^31-1] range used below. */ |
| 73 | static int rnd(void) { |
| 74 | static uint64_t s = 1; |
| 75 | s = s * 6364136223846793005ULL + 1442695040888963407ULL; |
| 76 | return (int)(s >> 33); |
| 77 | } |
| 78 | static int operand(unsigned nv) { return rnd() % (nv + 1) - 1; } |
| 79 | static void random_case(unsigned at) { |
| 80 | omega_bw_ir_init(&a); legacy_omega_bw_ir_init(&b); |
| 81 | unsigned nv = 1u + (unsigned)rnd()%256u, nu=(unsigned)rnd()%33u; |
| 82 | static const unsigned widths[]={1,2,4}; |
| 83 | for (unsigned v=0;v<nv;++v) { |
| 84 | unsigned w=widths[rnd()%3]; |
| 85 | if (alloc_one(&a,w,0,0)!=alloc_one(&b,w,1,0)) fail("alloc",at); |
| 86 | } |
| 87 | for (unsigned u=0;u<nu;++u) { |
| 88 | unsigned w=(rnd()%2) ? 1 : 2; |
| 89 | if (alloc_one(&a,w,0,1)!=alloc_one(&b,w,1,1)) fail("ualloc",at); |
| 90 | } |
| 91 | unsigned n=1u+(unsigned)rnd()%1024u; |
| 92 | static const BlackwellIROpcode ops[]={BW_IR_MOV_IMM,BW_IR_IMAD,BW_IR_LDCU,BW_IR_LDCU64,BW_IR_MOV_RZ,BW_IR_IADD3}; |
| 93 | for (unsigned i=0;i<n;++i) { |
| 94 | BlackwellIRInsn in={0}; |
| 95 | in.op=ops[rnd()%6]; in.dst_vreg=operand(nv); in.src1_vreg=operand(nv); |
| 96 | in.src2_vreg=operand(nv); in.src3_vreg=operand(nv); in.ureg=operand(nu); |
| 97 | in.imm=(uint32_t)rnd(); in.is_uniform=rnd()%2; |
| 98 | if (in.op==BW_IR_LDCU || in.op==BW_IR_LDCU64) in.dst_vreg=operand(nu); |
| 99 | int ar=omega_bw_ir_append(&a,&in),br=legacy_omega_bw_ir_append(&b,&in); |
| 100 | if(ar!=br) fail("append",at); |
| 101 | } |
| 102 | if (!same()) fail("intervals",at); |
| 103 | if (at%4==0) a.regalloc.gpr_limit=b.regalloc.gpr_limit=246; |
| 104 | if (at%8==0) a.regalloc.gpr_limit=b.regalloc.gpr_limit=3; |
| 105 | compare(omega_bw_regalloc_solve(&a),legacy_omega_bw_regalloc_solve(&b),at); |
| 106 | ++checked; |
| 107 | } |
| 108 | static void builders(void) { |
| 109 | for(unsigned k=0;k<=64;++k) for(unsigned n=0;n<=32;n+=8) for(unsigned p=0;p<3;++p) { |
| 110 | OmegaMatMulSpec s={0}; s.m=16; s.k=k; s.n=n; s.precision=p; |
| 111 | memset(&a,0,sizeof a); memset(&b,0,sizeof b); |
| 112 | compare(omega_blackwell_codegen_matmul_i32_prog(&s,&a),legacy_omega_blackwell_codegen_matmul_i32_prog(&s,&b),checked++); |
| 113 | memset(&a,0,sizeof a); memset(&b,0,sizeof b); |
| 114 | compare(omega_blackwell_codegen_matmul_tensor_prog(&s,&a),legacy_omega_blackwell_codegen_matmul_tensor_prog(&s,&b),checked++); |
| 115 | for(unsigned grid=0;grid<=2;++grid) for(int mut=0;mut<=1;++mut) { |
| 116 | memset(&a,0,sizeof a); memset(&b,0,sizeof b); |
| 117 | compare(omega_blackwell_codegen_matmul_tensor_loop_prog(&s,grid,mut,&a),legacy_omega_blackwell_codegen_matmul_tensor_loop_prog(&s,grid,mut,&b),checked++); |
| 118 | for(unsigned u=1;u<=8;u*=2) { |
| 119 | memset(&a,0,sizeof a); memset(&b,0,sizeof b); |
| 120 | compare(omega_blackwell_codegen_matmul_frag_prog(&s,grid,u,mut,&a),legacy_omega_blackwell_codegen_matmul_frag_prog(&s,grid,u,mut,&b),checked++); |
| 121 | } |
| 122 | for(unsigned rt=1;rt<=8;rt*=2) for(unsigned ct=1;ct<=4;ct*=2) for(unsigned u=1;u<=2;++u) { |
| 123 | memset(&a,0,sizeof a); memset(&b,0,sizeof b); |
| 124 | compare(omega_blackwell_codegen_matmul_frag_block_prog(&s,grid,u,rt,ct,mut,&a),legacy_omega_blackwell_codegen_matmul_frag_block_prog(&s,grid,u,rt,ct,mut,&b),checked++); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | static int fixture_program(const Fixture *f, BlackwellIRProgram *p, int legacy) { |
| 130 | OmegaMatMulSpec s; |
| 131 | if (omega_matmul_spec_init(&s, f->m, f->k, f->n, f->prec)) return -1; |
| 132 | switch (f->kind) { |
| 133 | case FX_I32: return legacy ? legacy_omega_blackwell_codegen_matmul_i32_prog(&s,p) : omega_blackwell_codegen_matmul_i32_prog(&s,p); |
| 134 | case FX_TENSOR: return legacy ? legacy_omega_blackwell_codegen_matmul_tensor_prog(&s,p) : omega_blackwell_codegen_matmul_tensor_prog(&s,p); |
| 135 | case FX_LOOP: return legacy ? legacy_omega_blackwell_codegen_matmul_tensor_loop_prog(&s,f->grid_x,f->mutant,p) : omega_blackwell_codegen_matmul_tensor_loop_prog(&s,f->grid_x,f->mutant,p); |
| 136 | case FX_FRAG: return legacy ? legacy_omega_blackwell_codegen_matmul_frag_prog(&s,f->grid_x,fx_unroll(f),f->mutant,p) : omega_blackwell_codegen_matmul_frag_prog(&s,f->grid_x,fx_unroll(f),f->mutant,p); |
| 137 | case FX_BLOCK: return legacy ? legacy_omega_blackwell_codegen_matmul_frag_block_prog(&s,f->grid_x,fx_unroll(f),f->rt,f->ct,f->mutant,p) : omega_blackwell_codegen_matmul_frag_block_prog(&s,f->grid_x,fx_unroll(f),f->rt,f->ct,f->mutant,p); |
| 138 | } |
| 139 | return -1; |
| 140 | } |
| 141 | static void canonical_builders(void) { |
| 142 | for (size_t i=0;i<NFX;++i) { |
| 143 | memset(&a,0,sizeof a); memset(&b,0,sizeof b); |
| 144 | int ar=fixture_program(&FX[i],&a,0), br=fixture_program(&FX[i],&b,1); |
| 145 | compare(ar,br,checked++); |
| 146 | } |
| 147 | } |
| 148 | static void policies(void) { |
| 149 | static const uint32_t edge[]={0,1,2,7,8,127,128,129,245,246,247,UINT32_MAX-9,UINT32_MAX-2,UINT32_MAX}; |
| 150 | for (unsigned i=0;i<10000;++i) { |
| 151 | uint32_t x=i<sizeof edge/sizeof edge[0] ? edge[i] : ((uint32_t)rnd()<<1)^(uint32_t)rnd(); |
| 152 | uint32_t y=((uint32_t)rnd()<<1)^(uint32_t)rnd(), z=(uint32_t)rnd(); |
| 153 | if(omega_bw_gpr_declared(x)!=legacy_omega_bw_gpr_declared(x)) fail("declared-gpr",i); |
| 154 | if(omega_bw_gpr_declared_ok(x,y)!=legacy_omega_bw_gpr_declared_ok(x,y)) fail("declared-gpr-refusal",i); |
| 155 | omega_bw_ir_init(&a); legacy_omega_bw_ir_init(&b); |
| 156 | if(omega_bw_ir_set_gpr_limit(&a,x)!=legacy_omega_bw_ir_set_gpr_limit(&b,x)||!same()) fail("gpr-limit",i); |
| 157 | uint32_t ar,ac,br,bc; |
| 158 | omega_matmul_frag_block_shape(x,y,&ar,&ac); |
| 159 | legacy_omega_matmul_frag_block_shape(x,y,&br,&bc); |
| 160 | if(ar!=br||ac!=bc) fail("block-shape",i); |
| 161 | if(omega_matmul_frag_block_unroll(x,y,z)!=legacy_omega_matmul_frag_block_unroll(x,y,z)) fail("block-unroll",i); |
| 162 | if(omega_matmul_frag_unroll(x)!=legacy_omega_matmul_frag_unroll(x)) fail("frag-unroll",i); |
| 163 | } |
| 164 | puts("PASS scalar register and matmul policies cases=10000 including uint32 wrap boundaries"); |
| 165 | } |
| 166 | int main(int argc, char **argv) { |
| 167 | (void)argv; |
| 168 | /* Exhaust every public register-count boundary, including one refusal beyond capacity. */ |
| 169 | omega_bw_ir_init(&a); legacy_omega_bw_ir_init(&b); |
| 170 | for(unsigned i=0;i<=256;++i) if(alloc_one(&a,1,0,0)!=alloc_one(&b,1,1,0)) fail("capacity",i); |
| 171 | for(unsigned i=0;i<=32;++i) if(alloc_one(&a,1,0,1)!=alloc_one(&b,1,1,1)) fail("uniform-capacity",i); |
| 172 | for(unsigned i=0;i<10000;++i) random_case(i); |
| 173 | if(argc==1) { builders(); canonical_builders(); } |
| 174 | policies(); |
| 175 | b=a; b.insns[0].imm^=1; |
| 176 | if(same()) fail("negative-record-flip-not-caught",0); |
| 177 | printf("PASS regalloc differential cases=%u refused=%u encoded=%u record-flip=caught\n",checked,refused,encoded); |
| 178 | return 0; |
| 179 | } |