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_f64_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #include "omega_blackwell_codegen.h" |
| 3 | #include "omega_gpu_om_ir.h" |
| 4 | #include "bw_warp_sim.h" |
| 5 | #include <float.h> |
| 6 | #include <fenv.h> |
| 7 | #include <stdlib.h> |
| 8 | |
| 9 | #define CHECK(x) do { if (!(x)) { fprintf(stderr,"f64 check line %d: %s\n",__LINE__,#x); return 0; } } while (0) |
| 10 | static uint64_t bits(double d) { uint64_t u; memcpy(&u,&d,8); return u; } |
| 11 | static double value(uint64_t u) { double d; memcpy(&d,&u,8); return d; } |
| 12 | static BlackwellIRInsn instruction(unsigned op) { |
| 13 | BlackwellIRInsn in={.op=(BlackwellIROpcode)op,.dst_vreg=0,.src1_vreg=1,.src2_vreg=2,.src3_vreg=3,.ureg=-1}; |
| 14 | if(op==BW_IR_DSETP)in.dst_vreg=-1; |
| 15 | return in; |
| 16 | } |
| 17 | static int encoding(BlackwellIRProgram *p) { |
| 18 | /* Independent fields extracted from our small nvcc 13.0.88 / nvdisasm probes. |
| 19 | * DADD's second source is in word 2, NOT word 1. All register forms, |
| 20 | * subregister offsets, aliasing, register extremes, and three guard forms. */ |
| 21 | const unsigned maps[][4]={{0,2,4,6},{8,8,8,8},{250,252,0,2},{4,12,18,22}}; |
| 22 | const uint32_t base[]={0x7229,0x7228,0x722b,0x722a,0x7308,0x7308,0x7310,0x7310,0x7312,0x7312,0x7311}; |
| 23 | const uint32_t mod[]={0,0,0,0x03f06000,0x1800,0x1c00,0x201800,0x301000,0x201c00,0x201800,0x30d100}; |
| 24 | uint8_t code[16384];size_t n; |
| 25 | for(unsigned op=BW_IR_DADD;op<=BW_IR_LAST;op++)for(unsigned m=0;m<4;m++)for(unsigned g=0;g<3;g++)for(unsigned sub=0;sub<2;sub++) { |
| 26 | if(sub&&(maps[m][0]<2||maps[m][1]<2||maps[m][2]<2||maps[m][3]<2))continue; |
| 27 | omega_bw_ir_init(p); |
| 28 | for(unsigned k=0;k<4;k++){CHECK(omega_bw_ir_alloc_vreg64(p)==(int)k);p->regalloc.vreg_to_phys[k]=(int)maps[m][k]-(int)(sub*2);} |
| 29 | BlackwellIRInsn in=instruction(op);in.control=0x000e2400; |
| 30 | in.dst_subreg=in.src1_subreg=in.src2_subreg=in.src3_subreg=sub*2; |
| 31 | in.predicate_p0=g!=0;in.predicate_not=g==2; |
| 32 | CHECK(!omega_bw_ir_append(p,&in));CHECK(!omega_bw_encode_program(p,code,sizeof code,&n)); |
| 33 | uint32_t w[4];memcpy(w,code,16); |
| 34 | unsigned d=maps[m][0],a=maps[m][1],b=maps[m][2],c=maps[m][3]; |
| 35 | uint32_t want[4]={base[op-BW_IR_DADD],0,mod[op-BW_IR_DADD],in.control}; |
| 36 | if(op<=BW_IR_DFMA){want[0]|=(d<<16)|(a<<24);if(op==BW_IR_DADD)want[2]=b;else {want[1]=b;if(op==BW_IR_DFMA)want[2]=c;}} |
| 37 | else if(op==BW_IR_DSETP){want[0]|=a<<24;want[1]=b;} |
| 38 | else {want[0]|=d<<16;want[1]=a;} |
| 39 | want[0]=(want[0]&~0xf000u)|((g==0?7u:g==1?0u:8u)<<12); |
| 40 | CHECK(!memcmp(w,want,16)); |
| 41 | if(op==BW_IR_DSETP)for(unsigned mode=0;mode<16;mode++) { |
| 42 | p->insns[0].imm=0x03f00000u|(mode<<12); |
| 43 | CHECK(!omega_bw_encode_program(p,code,sizeof code,&n));memcpy(w,code,16); |
| 44 | CHECK(w[2]==p->insns[0].imm); |
| 45 | } |
| 46 | } |
| 47 | /* Scalar halves need not be even; MUFU operates explicitly on high words. */ |
| 48 | for(unsigned op=BW_IR_MUFU_RCP64H;op<=BW_IR_S32_F64;op++) { |
| 49 | omega_bw_ir_init(p);for(unsigned k=0;k<4;k++){CHECK(omega_bw_ir_alloc_vreg64(p)==(int)k);p->regalloc.vreg_to_phys[k]=(int)(k*2+4);} |
| 50 | BlackwellIRInsn in=instruction(op);in.control=0;unsigned d=4,a=6; |
| 51 | if(op==BW_IR_MUFU_RCP64H||op==BW_IR_MUFU_RSQ64H){in.dst_subreg=in.src1_subreg=1;d++;a++;} |
| 52 | else if(op==BW_IR_F64_F32||op==BW_IR_F64_S32||op==BW_IR_F64_U32){in.src1_subreg=1;a++;} |
| 53 | else {in.dst_subreg=1;d++;} |
| 54 | CHECK(!omega_bw_ir_append(p,&in));CHECK(!omega_bw_encode_program(p,code,sizeof code,&n)); |
| 55 | uint32_t w[4];memcpy(w,code,16); |
| 56 | CHECK(w[0]==(base[op-BW_IR_DADD]|(d<<16))&&w[1]==a&&w[2]==mod[op-BW_IR_DADD]&&w[3]==0x000e2400); |
| 57 | } |
| 58 | /* The implicit DFMA addend is RZ; P0 is the only exposed predicate output. */ |
| 59 | omega_bw_ir_init(p);for(unsigned k=0;k<4;k++){CHECK(omega_bw_ir_alloc_vreg64(p)==(int)k);p->regalloc.vreg_to_phys[k]=(int)(k*2);} |
| 60 | BlackwellIRInsn zero=instruction(BW_IR_DFMA);zero.src3_vreg=-1;CHECK(!omega_bw_ir_append(p,&zero)); |
| 61 | CHECK(!omega_bw_encode_program(p,code,sizeof code,&n));uint32_t zw[4];memcpy(zw,code,16);CHECK(zw[2]==255); |
| 62 | p->insns[0]=instruction(BW_IR_DSETP);p->insns[0].dst_vreg=0; |
| 63 | CHECK(omega_bw_encode_program(p,code,sizeof code,&n)!=0); |
| 64 | /* Encoder-side pair validation also protects direct/replayed physical maps. */ |
| 65 | for(unsigned op=BW_IR_DADD;op<=BW_IR_LAST;op++) { |
| 66 | if(op==BW_IR_MUFU_RCP64H||op==BW_IR_MUFU_RSQ64H)continue; |
| 67 | for(unsigned operand=0;operand<4;operand++) { |
| 68 | int pair=op<=BW_IR_DFMA?(operand<3||(op==BW_IR_DFMA&&operand==3)):op==BW_IR_DSETP?(operand==1||operand==2): |
| 69 | (op==BW_IR_F64_F32||op==BW_IR_F64_S32||op==BW_IR_F64_U32)?operand==0:operand==1; |
| 70 | if(!pair)continue; |
| 71 | for(unsigned invalid=0;invalid<4;invalid++) { |
| 72 | omega_bw_ir_init(p);for(unsigned k=0;k<4;k++){CHECK(omega_bw_ir_alloc_vreg64(p)==(int)k);p->regalloc.vreg_to_phys[k]=(int)(k*2);} |
| 73 | const int bad[]={3,254,256,-2};p->regalloc.vreg_to_phys[operand]=bad[invalid]; |
| 74 | BlackwellIRInsn in=instruction(op);CHECK(!omega_bw_ir_append(p,&in)); |
| 75 | CHECK(omega_bw_encode_program(p,code,sizeof code,&n)!=0); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | omega_bw_ir_init(p);for(unsigned k=0;k<4;k++){CHECK(omega_bw_ir_alloc_vreg64(p)==(int)k);p->regalloc.vreg_to_phys[k]=(int)(k*2);} |
| 80 | BlackwellIRInsn unknown=instruction(BW_IR_LAST+1);CHECK(!omega_bw_ir_append(p,&unknown)); |
| 81 | CHECK(omega_bw_encode_program(p,code,sizeof code,&n)!=0); |
| 82 | return 1; |
| 83 | } |
| 84 | static int arithmetic(BlackwellIRProgram *p) { |
| 85 | omega_bw_ir_init(p);for(unsigned i=0;i<4;i++){CHECK(omega_bw_ir_alloc_vreg64(p)==(int)i);p->regalloc.vreg_to_phys[i]=(int)(i*2);} |
| 86 | WsSim s={.p=p};WsLane l={0}; |
| 87 | const double v[]={0.0,-0.0,1.0,-1.0,DBL_MAX,-DBL_MAX,DBL_MIN,-DBL_MIN,DBL_TRUE_MIN,-DBL_TRUE_MIN,INFINITY,-INFINITY,NAN,0x1.0000000000001p0,0x1.fffffffffffffp-1,0x1p-53}; |
| 88 | for(unsigned i=0;i<sizeof v/sizeof *v;i++)for(unsigned j=0;j<sizeof v/sizeof *v;j++)for(unsigned op=BW_IR_DADD;op<=BW_IR_DSETP;op++) { |
| 89 | double a=v[i],b=v[j],c=-1.0,want=op==BW_IR_DADD?a+b:op==BW_IR_DMUL?a*b:fma(a,b,c); |
| 90 | ws_wr64(&l,1,bits(a),p);ws_wr64(&l,2,bits(b),p);ws_wr64(&l,3,bits(c),p); |
| 91 | BlackwellIRInsn in=instruction(op); |
| 92 | if(op==BW_IR_DSETP)for(unsigned mode=0;mode<16;mode++) { |
| 93 | in.imm=0x03f00000u|(mode<<12);ws_lane_exec(&s,&l,&in,0,0,0,0); |
| 94 | unsigned relation=(isnan(a)||isnan(b))?8u:a<b?1u:a>b?4u:2u; |
| 95 | CHECK(!s.err&&l.p0==!!(relation&mode)); |
| 96 | } else { |
| 97 | ws_lane_exec(&s,&l,&in,0,0,0,0);double got=value(ws_rd64(&l,0,p)); |
| 98 | CHECK(!s.err&&(isnan(want)?isnan(got):bits(got)==bits(want))); |
| 99 | } |
| 100 | } |
| 101 | /* A fused cancellation must differ from separately rounded multiply/add. */ |
| 102 | ws_wr64(&l,1,bits(0x1.0000000000001p0),p);ws_wr64(&l,2,bits(0x1.ffffffffffffep-1),p);ws_wr64(&l,3,bits(-1.0),p); |
| 103 | BlackwellIRInsn in=instruction(BW_IR_DFMA);ws_lane_exec(&s,&l,&in,0,0,0,0); |
| 104 | CHECK(value(ws_rd64(&l,0,p))==-0x1p-104); |
| 105 | for(unsigned i=0;i<sizeof v/sizeof *v;i++) { |
| 106 | ws_wr64(&l,1,bits(v[i]),p); |
| 107 | in=instruction(BW_IR_F32_F64);ws_lane_exec(&s,&l,&in,0,0,0,0); |
| 108 | float want=(float)v[i],got=ws_u2f(l.r[0]);CHECK(isnan(want)?isnan(got):ws_f2u(want)==ws_f2u(got)); |
| 109 | in=instruction(BW_IR_S32_F64);ws_lane_exec(&s,&l,&in,0,0,0,0); |
| 110 | int32_t iwant=isnan(v[i])?INT32_MIN:v[i]>=INT32_MAX?INT32_MAX:v[i]<=INT32_MIN?INT32_MIN:(int32_t)v[i]; |
| 111 | CHECK(l.r[0]==(uint32_t)iwant&&!s.err); |
| 112 | } |
| 113 | const double ties[]={0x1.000001p0,0x1.000003p0,-0x1.000001p0,0x1p-150,0x1.8p-149,2147483647.5,-2147483648.5}; |
| 114 | for(unsigned i=0;i<sizeof ties/sizeof *ties;i++) { |
| 115 | ws_wr64(&l,1,bits(ties[i]),p);in=instruction(BW_IR_F32_F64);ws_lane_exec(&s,&l,&in,0,0,0,0); |
| 116 | CHECK(l.r[0]==ws_f2u((float)ties[i])); |
| 117 | } |
| 118 | const uint32_t ints[]={0,1,0xffffffffu,0x80000000u,0x7fffffffu,0x01000001u}; |
| 119 | for(unsigned i=0;i<sizeof ints/sizeof *ints;i++)for(unsigned op=BW_IR_F64_F32;op<=BW_IR_F64_U32;op++)if(op!=BW_IR_F32_F64) { |
| 120 | l.r[2]=ints[i];in=instruction(op);ws_lane_exec(&s,&l,&in,0,0,0,0); |
| 121 | double want=op==BW_IR_F64_F32?(double)ws_u2f(ints[i]):op==BW_IR_F64_S32?(double)(int32_t)ints[i]:(double)ints[i]; |
| 122 | double got=value(ws_rd64(&l,0,p));CHECK(!s.err&&(isnan(want)?isnan(got):bits(got)==bits(want))); |
| 123 | } |
| 124 | /* MUFU is a scalar HIGH word op; the low destination word must survive. |
| 125 | * PTX ftz f64 semantics: ignored low input, signed flush, canonical NaN. |
| 126 | * We require the structural contract and exact special values, not chip |
| 127 | * bit identity for the deliberately approximate normal high word. */ |
| 128 | const uint32_t high[]={0,0x80000000u,1,0x80000001u,0x7ff00000u,0xfff00000u,0x7ff00001u,0x3ff00000u,0x40100000u}; |
| 129 | for(unsigned op=BW_IR_MUFU_RCP64H;op<=BW_IR_MUFU_RSQ64H;op++)for(unsigned i=0;i<sizeof high/sizeof *high;i++) { |
| 130 | l.r[0]=0x12345678;l.r[2]=0xffffffff;l.r[3]=high[i]; |
| 131 | in=instruction(op);in.dst_subreg=in.src1_subreg=1;ws_lane_exec(&s,&l,&in,0,0,0,0); |
| 132 | CHECK(!s.err&&l.r[0]==0x12345678); |
| 133 | uint32_t want=0; |
| 134 | if(i<=3)want=(high[i]&0x80000000u)|0x7ff00000u; |
| 135 | else if(i==4)want=0; |
| 136 | else if(i==5)want=op==BW_IR_MUFU_RCP64H?0x80000000u:0x7fffffffu; |
| 137 | else if(i==6)want=0x7fffffffu; |
| 138 | else if(i==7)want=0x3ff00000u; |
| 139 | else want=op==BW_IR_MUFU_RCP64H?0x3fd00000u:0x3fe00000u; |
| 140 | CHECK(l.r[1]==want); |
| 141 | } |
| 142 | /* Exercise full simulator conductor as well as its per-lane path. */ |
| 143 | omega_bw_ir_init(p);in=instruction(BW_IR_EXIT);CHECK(!omega_bw_ir_append(p,&in));s.threads=32; |
| 144 | CHECK(!ws_run(&s,1,1));return 1; |
| 145 | } |
| 146 | static int pressure(BlackwellIRProgram *p) { |
| 147 | /* Simultaneous live DADD inputs consume aligned pairs, not scalar GPRs. */ |
| 148 | for(unsigned count=2;count<=128;count++) { |
| 149 | omega_bw_ir_init(p);for(unsigned i=0;i<count;i++)CHECK(omega_bw_ir_alloc_vreg64(p)==(int)i); |
| 150 | for(unsigned i=0;i<count;i++){BlackwellIRInsn in=instruction(BW_IR_MOV_IMM);in.dst_vreg=(int)i;in.src1_vreg=in.src2_vreg=in.src3_vreg=-1;CHECK(omega_bw_ir_append(p,&in)>=0);} |
| 151 | for(unsigned i=1;i<count;i++){BlackwellIRInsn in=instruction(BW_IR_DADD);in.dst_vreg=0;in.src1_vreg=0;in.src2_vreg=(int)i;in.src3_vreg=-1;CHECK(omega_bw_ir_append(p,&in)>=0);} |
| 152 | int rc=omega_bw_regalloc_solve(p); |
| 153 | if(rc){CHECK(count==(BW_PHYS_GPR_MAX-BW_PHYS_GPR_START)/2+1);return 1;} |
| 154 | for(unsigned i=0;i<count;i++){int r=p->regalloc.vreg_to_phys[i];CHECK(r>=0&&!(r&1)&&r+1<BW_PHYS_GPR_MAX);for(unsigned j=0;j<i;j++)CHECK(r!=p->regalloc.vreg_to_phys[j]);} |
| 155 | } |
| 156 | return 0; |
| 157 | } |
| 158 | static uint32_t boundary(uint64_t *result,uint64_t op,uint64_t a,uint64_t b,uint64_t c,void *iv,uint64_t ni,void *mv,uint64_t nm) { |
| 159 | uint64_t *ir=iv,*meta=mv;(void)a;(void)b;(void)c; |
| 160 | if(ni!=OMEGA_OM_IR_WORDS||nm!=OMEGA_OM_IR_META)return 1; |
| 161 | memset(ir,0,ni*sizeof *ir);memset(meta,0,nm*sizeof *meta); |
| 162 | for(unsigned i=0;i<3;i++)for(unsigned f=1;f<=5;f++)ir[i*8+f]=UINT64_MAX; |
| 163 | ir[0]=BW_IR_MOV_IMM;ir[1]=0;ir[6]=0x3f800000u; |
| 164 | ir[8]=op;ir[9]=1;ir[10]=0;ir[16]=BW_IR_EXIT; |
| 165 | meta[0]=3;meta[1]=2;meta[64]=meta[65]=1;*result=0;return 0; |
| 166 | } |
| 167 | int main(void) { |
| 168 | if(fesetround(FE_TONEAREST))return 2; |
| 169 | BlackwellIRProgram *p=malloc(sizeof *p);if(!p)return 2; |
| 170 | int okay=encoding(p)&&arithmetic(p)&&pressure(p); |
| 171 | if(okay)okay=omega_gpu_om_ir_replay(boundary,BW_IR_LAST,0,0,0,p,NULL)==OMEGA_OM_IR_OK&&omega_gpu_om_ir_replay(boundary,BW_IR_LAST+1,0,0,0,p,NULL)==OMEGA_OM_IR_REPLAY; |
| 172 | free(p);puts(okay?"codegen-f64: PASS (oracle bytes, IEEE simulation, pair pressure, replay boundary)":"codegen-f64: FAIL");return !okay; |
| 173 | } |