Home My Page Projects Code Snippets Project Openings diderot

# SCM Repository

[diderot] Annotation of /branches/charisee/src/compiler/high-to-mid/split.sml
 [diderot] / branches / charisee / src / compiler / high-to-mid / split.sml

# Annotation of /branches/charisee/src/compiler/high-to-mid/split.sml

 1 : cchiw 2843 (* Currently under construction 2 : cchiw 2838 * 3 : * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu) 4 : * All rights reserved. 5 : *) 6 : cchiw 2843 7 : (* 8 : During the transition from high-IL to mid-IL, complicated EIN expressions are split into simpler ones in order to better identify methods for code generation and common subexpressions. Combining EIN operators in the optimization phase can lead to large and complicated EIN operators. A general code generator would need to expand every operation to work on scalars, which could miss the opportunity for vectorization and lead to poor code generation. Instead, every EIN operator is split into a set of simple EIN operators. Each EIN expression then only has one operation working on constants, tensors, deltas, epsilons, images and kernels. 9 : 10 : (1) When the outer EIN operator is $\in {--, +, -, *, /, \sum}$ then for each subexpression analyze to see if they need to be rewritten. 11 : 12 : (1a.) When a subexpression is a field expression $\circledast,\nabla$ then it becomes 0. When it is another operation ${@ --, +, -, *, /, \sum}$ then we lift that subexpression and create a new EIN operator. We replace the subexpression with a tensor expression that represent it's size. 13 : 14 : (1b) Call cleanIndex.sml to clean the indices in the subexpression, and get the shape for the tensor replacement. 15 : 16 : (1c) Call cleanParams.sml to clean the params in the subexpression.\\ 17 : *) 18 : cchiw 2838 19 : structure Split = struct 20 : 21 : local 22 : 23 : structure E = Ein 24 : structure DstIL = MidIL 25 : structure DstTy = MidILTypes 26 : structure DstV = DstIL.Var 27 : structure P=Printer 28 : structure cleanP=cleanParams 29 : structure cleanI=cleanIndex 30 : 31 : cchiw 3033 32 : cchiw 2838 in 33 : 34 : cchiw 3033 val testing=0 35 : cchiw 2838 fun setEin(params,index,body)=Ein.EIN{params=params, index=index, body=body} 36 : fun assignEinApp(y,params,index,body,args)= (y,DstIL.EINAPP(setEin(params,index,body),args)) 37 : cchiw 2843 val einappzero=DstIL.EINAPP(setEin([],[],E.Const 0),[]) 38 : fun setEinZero y= (y,einappzero) 39 : cchiw 2838 fun cleanParams e =cleanP.cleanParams e 40 : fun cleanIndex e =cleanI.cleanIndex e 41 : cchiw 2845 fun printEINAPP e=MidToString.printEINAPP e 42 : cchiw 3033 43 : 44 : 45 : cchiw 2838 fun itos i =Int.toString i 46 : cchiw 2923 fun filterSca e=Filter.filterSca e 47 : cchiw 2838 fun err str=raise Fail str 48 : val cnt = ref 0 49 : fun genName prefix = let 50 : val n = !cnt 51 : in 52 : cnt := n+1; 53 : String.concat[prefix, "_", Int.toString n] 54 : end 55 : fun testp n=(case testing 56 : of 0=> 1 57 : | _ =>(print(String.concat n);1) 58 : (*end case*)) 59 : 60 : 61 : (* lift:ein_app*params*index*sum_id*args-> (ein_exp* params*args*code) 62 : *lifts expression and returns replacement tensor 63 : cchiw 2843 * cleans the index and params of subexpression 64 : *creates new param and replacement tensor for the original ein_exp 65 : cchiw 2838 *) 66 : cchiw 2845 fun lift(name,e,params,index,sx,args)=let 67 : cchiw 2867 68 : cchiw 2838 val (tshape,sizes,body)=cleanIndex(e,index,sx) 69 : cchiw 2843 val id=length(params) 70 : val Rparams=params@[E.TEN(1,sizes)] 71 : val Re=E.Tensor(id,tshape) 72 : cchiw 2845 val M = DstV.new (genName (name^"_l_"^itos id), DstTy.TensorTy sizes) 73 : cchiw 2843 val Rargs=args@[M] 74 : val einapp=cleanParams(M,body,Rparams,sizes,Rargs) 75 : 76 : cchiw 2838 in 77 : cchiw 2843 (Re,Rparams,Rargs,[einapp]) 78 : cchiw 2838 end 79 : 80 : (* isOp: ein->int 81 : * checks to see if this sub-expression is pulled out or split form original 82 : * 0-becomes zero,1-remains the same, 2-operator 83 : *) 84 : fun isOp e =(case e 85 : of E.Field _ => 0 86 : | E.Conv _ => 0 87 : | E.Apply _ => 0 88 : | E.Lift _ => 0 89 : | E.Neg _ => 1 90 : cchiw 2870 | E.Sqrt _ => 1 91 : cchiw 2923 | E.PowInt _ => 1 92 : | E.PowReal _ => 1 93 : cchiw 2838 | E.Add _ => 1 94 : | E.Sub _ => 1 95 : | E.Prod _ => 1 96 : | E.Div _ => 1 97 : | E.Sum _ => 1 98 : | E.Probe _ => 1 99 : | E.Partial _ => err(" Partial used after normalize") 100 : | E.Krn _ => err("Krn used before expand") 101 : | E.Value _ => err("Value used before expand") 102 : | E.Img _ => err("Probe used before expand") 103 : | _ => 2 104 : (*end case*)) 105 : 106 : cchiw 2843 (* rewriteOp:ein_exp*params*index*args-> ein_exp*params*args*code 107 : * If e1 an op then call lift() to replace it 108 : cchiw 2838 * Otherwise rewrite to 0 or it remains the same 109 : *) 110 : cchiw 2845 fun rewriteOp(name,e1,params,index,sx,args)=(case (isOp e1) 111 : cchiw 2838 of 0 => (E.Const 0,params,args,[]) 112 : | 2 => (e1,params,args,[]) 113 : cchiw 2867 | _ => lift(name,e1,params,index,sx,args) 114 : cchiw 2838 (*end*)) 115 : 116 : cchiw 2843 (* rewriteOps:ein_exp list*params*index*sum_id list*mid-il vars 117 : -> ein_exp list*params*args*code 118 : * calls rewriteOp on ein_exp list 119 : cchiw 2838 *) 120 : cchiw 2845 fun rewriteOps(name,list1,params,index,sx,args)=let 121 : cchiw 2838 fun m([],rest,params,args,code)=(rest,params,args,code) 122 : | m(e1::es,rest,params,args,code)=let 123 : cchiw 3017 124 : cchiw 2845 val (e1',params',args',code')= rewriteOp(name,e1,params,index,sx,args) 125 : cchiw 3033 (*val _ =testp["rewriteOP:\n",P.printbody e1,"\n\t=>", P.printbody e1']*) 126 : cchiw 2838 in 127 : m(es,rest@[e1'],params',args',code@code') 128 : end 129 : in 130 : m(list1,[],params,args,[]) 131 : end 132 : cchiw 2843 133 : cchiw 2845 (*rewriteOrig: var* ein_exp* params*index list*mid-il vars 134 : cchiw 2843 When the operation is zero then we return a real. 135 : cchiw 2845 -Moved is Zero to before split. 136 : cchiw 2843 *) 137 : cchiw 3033 fun rewriteOrig(y,body,params,index,sx,args) =cleanParams(y,body,params,index,args) 138 : cchiw 2838 139 : (* handleNeg:var*ein_exp *params*index*args-> (var*einap)*code 140 : cchiw 2843 * calls rewriteOp() lift on ein_exp 141 : cchiw 2838 *) 142 : fun handleNeg(y,e1,params,index,args)=let 143 : cchiw 2922 val (e1',params',args',code)= rewriteOp("neg", e1,params,index,[],args) 144 : cchiw 2838 val body =E.Neg e1' 145 : cchiw 2845 val einapp= rewriteOrig(y,body,params',index,[],args') 146 : in 147 : (einapp,code) 148 : end 149 : cchiw 2838 150 : cchiw 2867 (* handleSqrt:var*ein_exp *params*index*args-> (var*einap)*code 151 : * calls rewriteOp() lift on ein_exp 152 : *) 153 : fun handleSqrt(y,e1,params,index,args)=let 154 : cchiw 2922 val (e1',params',args',code)= rewriteOp("sqrt", e1,params,index,[],args) 155 : cchiw 2867 val body =E.Sqrt e1' 156 : val einapp= rewriteOrig(y,body,params',index,[],args') 157 : in 158 : (einapp,code) 159 : end 160 : 161 : 162 : cchiw 3033 (* handlePowInt:var*ein_exp *params*index*args-> (var*einap)*code 163 : * calls rewriteOp() lift on ein_exp 164 : *) 165 : fun handlePowInt(y,(e1,n1),params,index,args)=let 166 : val (e1',params',args',code)= rewriteOp("powint", e1,params,index,[],args) 167 : val body =E.PowInt(e1',n1) 168 : val einapp= rewriteOrig(y,body,params',index,[],args') 169 : in 170 : (einapp,code) 171 : end 172 : cchiw 2870 173 : 174 : cchiw 2922 (* handlePowReal:var*ein_exp *params*index*args-> (var*einap)*code 175 : * calls rewriteOp() lift on ein_exp 176 : *) 177 : fun handlePowReal(y,(e1,n1),params,index,args)=let 178 : val (e1',params',args',code)= rewriteOp("powreal", e1,params,index,[],args) 179 : val body =E.PowReal(e1',n1) 180 : val einapp= rewriteOrig(y,body,params',index,[],args') 181 : in 182 : (einapp,code) 183 : end 184 : cchiw 2870 185 : 186 : cchiw 2838 (* handleSub:var*ein_exp*ein_exp *params*index*args-> (var*einap)*code 187 : cchiw 2843 * calls rewriteOps() lift on ein_exp 188 : cchiw 2838 *) 189 : fun handleSub(y,e1,e2,params,index,args)=let 190 : cchiw 2922 val ([e1',e2'],params',args',code)= rewriteOps("subt",[e1,e2],params,index,[],args) 191 : cchiw 2838 val body =E.Sub(e1',e2') 192 : cchiw 2845 val einapp= rewriteOrig(y,body,params',index,[],args') 193 : in 194 : (einapp,code) 195 : end 196 : cchiw 2838 197 : (* handleDiv:var*ein_exp *ein_exp*params*index*args-> (var*einap)*code 198 : cchiw 2843 * calls rewriteOp() lift on ein_exp 199 : cchiw 2838 *) 200 : fun handleDiv(y,e1,e2,params,index,args)=let 201 : cchiw 2923 val (e1',params1',args1',code1')=rewriteOp("div-num",e1,params,index,[],args) 202 : val (e2',params2',args2',code2')=rewriteOp("div-denom",e2,params1',index,[],args1') 203 : (*val (e2',params2',args2',code2')=rewriteOp("div-denom",e2,params1',[],[],args1')*) 204 : cchiw 2838 val body =E.Div(e1',e2') 205 : cchiw 2845 val einapp= rewriteOrig(y,body,params2',index,[],args2') 206 : in 207 : (einapp,code1'@code2') 208 : end 209 : cchiw 2838 210 : (* handleAdd:var*ein_exp list *params*index*args-> (var*einap)*code 211 : cchiw 2843 * calls rewriteOps() lift on ein_exp 212 : cchiw 2838 *) 213 : fun handleAdd(y,e1,params,index,args)=let 214 : cchiw 3030 215 : cchiw 2922 val (e1',params',args',code)= rewriteOps("add",e1,params,index,[],args) 216 : cchiw 2838 val body =E.Add e1' 217 : cchiw 2845 val einapp= rewriteOrig(y,body,params',index,[],args') 218 : in 219 : (einapp,code) 220 : end 221 : cchiw 2838 222 : (* handleProd:var*ein_exp list*params*index*args-> (var*einap)*code 223 : cchiw 2843 * calls rewriteOps() lift on ein_exp 224 : cchiw 2838 *) 225 : fun handleProd(y,e1,params,index,args)=let 226 : cchiw 2922 val (e1',params',args',code)= rewriteOps("prod",e1,params,index,[],args) 227 : cchiw 2838 val body =E.Prod e1' 228 : cchiw 2845 val einapp= rewriteOrig(y,body,params',index,[],args') 229 : in 230 : (einapp,code) 231 : end 232 : cchiw 2838 233 : (* handleSumProd:var*ein_exp *params*index*args-> (var*einap)*code 234 : cchiw 2843 * calls rewriteOps() lift on ein_exp 235 : cchiw 2838 *) 236 : fun handleSumProd(y,e1,params,index,sx,args)=let 237 : cchiw 2922 val (e1',params',args',code)= rewriteOps("sumprod",e1,params,index,sx,args) 238 : cchiw 2838 val body= E.Sum(sx,E.Prod e1') 239 : cchiw 2845 val einapp= rewriteOrig(y,body,params',index,sx,args') 240 : in 241 : (einapp,code) 242 : end 243 : cchiw 2838 244 : (* split:var*ein_app-> (var*einap)*code 245 : * split ein expression into smaller pieces 246 : cchiw 2843 note we leave summation around probe exp 247 : cchiw 2838 *) 248 : fun split(y,einapp as DstIL.EINAPP(Ein.EIN{params, index, body},args))=let 249 : cchiw 2843 val zero= (setEinZero y,[]) 250 : cchiw 2838 val default=((y,einapp),[]) 251 : val sumIndex=ref [] 252 : cchiw 2867 val str="Poorly formed EIN operator. Argument needs to be applied in High-IL"^(P.printbody body) 253 : cchiw 3017 val _=testp["\n\nStarting split",P.printbody body] 254 : fun rewrite b=(case b 255 : cchiw 2847 of E.Probe (E.Conv _,_) => default 256 : cchiw 2870 | E.Probe(E.Field _,_) => raise Fail str 257 : cchiw 2847 | E.Probe _ => raise Fail str 258 : cchiw 2838 | E.Conv _ => zero 259 : | E.Field _ => zero 260 : | E.Apply _ => zero 261 : | E.Lift e => zero 262 : | E.Delta _ => default 263 : | E.Epsilon _ => default 264 : cchiw 2843 | E.Eps2 _ => default 265 : cchiw 2838 | E.Tensor _ => default 266 : | E.Const _ => default 267 : cchiw 2923 | E.ConstR _ => default 268 : cchiw 2838 | E.Neg e1 => handleNeg(y,e1,params,index,args) 269 : cchiw 2867 | E.Sqrt e1 => handleSqrt(y,e1,params,index,args) 270 : cchiw 2923 | E.PowInt e1 => handlePowInt(y,e1,params,index,args) 271 : | E.PowReal e1 => handlePowReal(y,e1,params,index,args) 272 : cchiw 2838 | E.Sub (e1,e2) => handleSub(y,e1,e2,params,index,args) 273 : | E.Div (e1,e2) => handleDiv(y,e1,e2,params,index,args) 274 : cchiw 2847 | E.Sum(_,E.Prod[E.Eps2 _, E.Probe(E.Conv _,_) ]) => default 275 : | E.Sum(_,E.Prod[E.Epsilon _, E.Probe(E.Conv _,_) ]) => default 276 : | E.Sum(_,E.Probe(E.Conv _,_)) => default 277 : cchiw 2838 | E.Sum(sx,E.Prod e1) => handleSumProd(y,e1,params,index,sx,args) 278 : cchiw 3033 | E.Sum(sx,E.Add [a0,a1,a2]) => err(" \n summation not distributed:add3\n found:\n\t"^P.printbody a0^"\nin \n"^str) 279 : 280 : | E.Sum(sx,E.Add n) => err(" \n summation not distributed:add\n"^Int.toString(length n)^"found:\n\t"^P.printbody b^"\nin \n"^str) 281 : | E.Sum(sx,E.Sub _) => err(" summation not distributed:sub"^str) 282 : | E.Sum(sx,E.Sqrt _) => err(" summation not distributed:sqrt"^str) 283 : | E.Sum(sx,E.Neg _) => err(" summation not distributed:neg"^str) 284 : | E.Sum(sx,_) => err(" summation not distributed:"^str) 285 : cchiw 2838 | E.Add e1 => handleAdd(y,e1,params,index,args) 286 : | E.Prod e1 => handleProd(y,e1,params,index,args) 287 : | E.Partial _ => err(" Partial used after normalize") 288 : | E.Krn _ => err("Krn used before expand") 289 : | E.Value _ => err("Value used before expand") 290 : | E.Img _ => err("Probe used before expand") 291 : (*end case *)) 292 : val (einapp2,newbies) =rewrite body 293 : in 294 : (einapp2,newbies) 295 : end 296 : |split(y,app) =((y,app),[]) 297 : cchiw 2923 298 : cchiw 2838 (* iterMultiple:code*code=> (code*code) 299 : * recursively split ein expression into smaller pieces 300 : *) 301 : fun iterMultiple(einapp2,newbies2)=let 302 : cchiw 3017 fun itercode([],rest,code,_)=(rest,code) 303 : | itercode(e1::newbies,rest,code,cnt)=let 304 : val _ =testp["\n\n******* split term **",Int.toString cnt," *****","\n \n",printEINAPP(e1),"\n=>\n"] 305 : cchiw 3033 val (einapp3,code3) = split e1 306 : cchiw 3017 val _ =testp["\n\t===>\n",printEINAPP(einapp3),"\nand\n",(String.concatWith",\n\t"(List.map printEINAPP code3))] 307 : val (rest4,code4)=itercode(code3,[],[],cnt+1) 308 : in itercode(newbies,rest@[einapp3],code4@rest4@code,cnt+2) 309 : cchiw 2838 end 310 : cchiw 3017 val(rest,code)= itercode(newbies2,[],[],1) 311 : cchiw 2838 in 312 : (einapp2,code@rest) 313 : end 314 : 315 : cchiw 3017 316 : cchiw 2838 end; (* local *) 317 : 318 : end (* local *)