Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tests/codegen_f64_test.c # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / codegen_f64_test.c` 173 lines · 11622 bytes Raw Blame History 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 6 #include 7 #include 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;ib?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=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=0);} 151 for(unsigned i=1;i=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;iregalloc.vreg_to_phys[i];CHECK(r>=0&&!(r&1)&&r+1regalloc.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 }