Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2867 - (view) (download)

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 : cchiw 2845 structure handleE=handleEin
31 : cchiw 2838
32 :     in
33 :    
34 : cchiw 2847 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 :     fun isZero e=handleE.isZero e
43 : cchiw 2838 fun itos i =Int.toString i
44 :     fun err str=raise Fail str
45 :     val cnt = ref 0
46 :     fun genName prefix = let
47 :     val n = !cnt
48 :     in
49 :     cnt := n+1;
50 :     String.concat[prefix, "_", Int.toString n]
51 :     end
52 :     fun testp n=(case testing
53 :     of 0=> 1
54 :     | _ =>(print(String.concat n);1)
55 :     (*end case*))
56 :    
57 :    
58 :     (* lift:ein_app*params*index*sum_id*args-> (ein_exp* params*args*code)
59 :     *lifts expression and returns replacement tensor
60 : cchiw 2843 * cleans the index and params of subexpression
61 :     *creates new param and replacement tensor for the original ein_exp
62 : cchiw 2838 *)
63 : cchiw 2845 fun lift(name,e,params,index,sx,args)=let
64 : cchiw 2867
65 : cchiw 2838 val (tshape,sizes,body)=cleanIndex(e,index,sx)
66 : cchiw 2843 val id=length(params)
67 :     val Rparams=params@[E.TEN(1,sizes)]
68 :     val Re=E.Tensor(id,tshape)
69 : cchiw 2845 val M = DstV.new (genName (name^"_l_"^itos id), DstTy.TensorTy sizes)
70 : cchiw 2843 val Rargs=args@[M]
71 :     val einapp=cleanParams(M,body,Rparams,sizes,Rargs)
72 :    
73 : cchiw 2838 in
74 : cchiw 2843 (Re,Rparams,Rargs,[einapp])
75 : cchiw 2838 end
76 :    
77 :     (* isOp: ein->int
78 :     * checks to see if this sub-expression is pulled out or split form original
79 :     * 0-becomes zero,1-remains the same, 2-operator
80 :     *)
81 :     fun isOp e =(case e
82 :     of E.Field _ => 0
83 :     | E.Conv _ => 0
84 :     | E.Apply _ => 0
85 :     | E.Lift _ => 0
86 :     | E.Neg _ => 1
87 : cchiw 2867 | E.Sqrt _ => 1
88 : cchiw 2838 | E.Add _ => 1
89 :     | E.Sub _ => 1
90 :     | E.Prod _ => 1
91 :     | E.Div _ => 1
92 :     | E.Sum _ => 1
93 :     | E.Probe _ => 1
94 :     | E.Partial _ => err(" Partial used after normalize")
95 :     | E.Krn _ => err("Krn used before expand")
96 :     | E.Value _ => err("Value used before expand")
97 :     | E.Img _ => err("Probe used before expand")
98 :     | _ => 2
99 :     (*end case*))
100 :    
101 : cchiw 2843 (* rewriteOp:ein_exp*params*index*args-> ein_exp*params*args*code
102 :     * If e1 an op then call lift() to replace it
103 : cchiw 2838 * Otherwise rewrite to 0 or it remains the same
104 :     *)
105 : cchiw 2845 fun rewriteOp(name,e1,params,index,sx,args)=(case (isOp e1)
106 : cchiw 2838 of 0 => (E.Const 0,params,args,[])
107 :     | 2 => (e1,params,args,[])
108 : cchiw 2867 | _ => lift(name,e1,params,index,sx,args)
109 : cchiw 2838 (*end*))
110 :    
111 : cchiw 2843 (* rewriteOps:ein_exp list*params*index*sum_id list*mid-il vars
112 :     -> ein_exp list*params*args*code
113 :     * calls rewriteOp on ein_exp list
114 : cchiw 2838 *)
115 : cchiw 2845 fun rewriteOps(name,list1,params,index,sx,args)=let
116 : cchiw 2838 fun m([],rest,params,args,code)=(rest,params,args,code)
117 :     | m(e1::es,rest,params,args,code)=let
118 : cchiw 2845 val (e1',params',args',code')= rewriteOp(name,e1,params,index,sx,args)
119 : cchiw 2838 in
120 :     m(es,rest@[e1'],params',args',code@code')
121 :     end
122 :     in
123 :     m(list1,[],params,args,[])
124 :     end
125 : cchiw 2843
126 : cchiw 2845 (*rewriteOrig: var* ein_exp* params*index list*mid-il vars
127 : cchiw 2843 When the operation is zero then we return a real.
128 : cchiw 2845 -Moved is Zero to before split.
129 : cchiw 2843 *)
130 : cchiw 2845 fun rewriteOrig(y,body,params,index,sx,args) =(case (isZero body)
131 :     of 1=> setEinZero y
132 : cchiw 2843 | _ => cleanParams(y,body,params,index,args)
133 : cchiw 2845 (*end case*))
134 : cchiw 2838
135 :     (* handleNeg:var*ein_exp *params*index*args-> (var*einap)*code
136 : cchiw 2843 * calls rewriteOp() lift on ein_exp
137 : cchiw 2838 *)
138 :     fun handleNeg(y,e1,params,index,args)=let
139 : cchiw 2845 val (e1',params',args',code)= rewriteOp(DstV.name y, e1,params,index,[],args)
140 : cchiw 2838 val body =E.Neg e1'
141 : cchiw 2845 val einapp= rewriteOrig(y,body,params',index,[],args')
142 :     in
143 :     (einapp,code)
144 :     end
145 : cchiw 2838
146 : cchiw 2867 (* handleSqrt:var*ein_exp *params*index*args-> (var*einap)*code
147 :     * calls rewriteOp() lift on ein_exp
148 :     *)
149 :     fun handleSqrt(y,e1,params,index,args)=let
150 :     val (e1',params',args',code)= rewriteOp(DstV.name y, e1,params,index,[],args)
151 :     val body =E.Sqrt e1'
152 :     val einapp= rewriteOrig(y,body,params',index,[],args')
153 :     in
154 :     (einapp,code)
155 :     end
156 :    
157 :    
158 : cchiw 2838 (* handleSub:var*ein_exp*ein_exp *params*index*args-> (var*einap)*code
159 : cchiw 2843 * calls rewriteOps() lift on ein_exp
160 : cchiw 2838 *)
161 :     fun handleSub(y,e1,e2,params,index,args)=let
162 : cchiw 2845 val ([e1',e2'],params',args',code)= rewriteOps(DstV.name y,[e1,e2],params,index,[],args)
163 : cchiw 2838 val body =E.Sub(e1',e2')
164 : cchiw 2845 val einapp= rewriteOrig(y,body,params',index,[],args')
165 :     in
166 :     (einapp,code)
167 :     end
168 : cchiw 2838
169 :     (* handleDiv:var*ein_exp *ein_exp*params*index*args-> (var*einap)*code
170 : cchiw 2843 * calls rewriteOp() lift on ein_exp
171 : cchiw 2838 *)
172 :     fun handleDiv(y,e1,e2,params,index,args)=let
173 : cchiw 2845 val (e1',params1',args1',code1')=rewriteOp(DstV.name y,e1,params,index,[],args)
174 :     val (e2',params2',args2',code2')=rewriteOp(DstV.name y,e2,params1',[],[],args1')
175 : cchiw 2838 val body =E.Div(e1',e2')
176 : cchiw 2845 val einapp= rewriteOrig(y,body,params2',index,[],args2')
177 :     in
178 :     (einapp,code1'@code2')
179 :     end
180 : cchiw 2838
181 :     (* handleAdd:var*ein_exp list *params*index*args-> (var*einap)*code
182 : cchiw 2843 * calls rewriteOps() lift on ein_exp
183 : cchiw 2838 *)
184 :     fun handleAdd(y,e1,params,index,args)=let
185 : cchiw 2845 val (e1',params',args',code)= rewriteOps(DstV.name y,e1,params,index,[],args)
186 : cchiw 2838 val body =E.Add e1'
187 : cchiw 2845 val einapp= rewriteOrig(y,body,params',index,[],args')
188 :     in
189 :     (einapp,code)
190 :     end
191 : cchiw 2838
192 :     (* handleProd:var*ein_exp list*params*index*args-> (var*einap)*code
193 : cchiw 2843 * calls rewriteOps() lift on ein_exp
194 : cchiw 2838 *)
195 :     fun handleProd(y,e1,params,index,args)=let
196 : cchiw 2845 val (e1',params',args',code)= rewriteOps(DstV.name y,e1,params,index,[],args)
197 : cchiw 2838 val body =E.Prod e1'
198 : cchiw 2845 val einapp= rewriteOrig(y,body,params',index,[],args')
199 :     in
200 :     (einapp,code)
201 :     end
202 : cchiw 2838
203 :     (* handleSumProd:var*ein_exp *params*index*args-> (var*einap)*code
204 : cchiw 2843 * calls rewriteOps() lift on ein_exp
205 : cchiw 2838 *)
206 :     fun handleSumProd(y,e1,params,index,sx,args)=let
207 : cchiw 2845 val (e1',params',args',code)= rewriteOps(DstV.name y,e1,params,index,sx,args)
208 : cchiw 2838 val body= E.Sum(sx,E.Prod e1')
209 : cchiw 2845 val einapp= rewriteOrig(y,body,params',index,sx,args')
210 :     in
211 :     (einapp,code)
212 :     end
213 : cchiw 2838
214 :     (* split:var*ein_app-> (var*einap)*code
215 :     * split ein expression into smaller pieces
216 : cchiw 2843 note we leave summation around probe exp
217 : cchiw 2838 *)
218 :     fun split(y,einapp as DstIL.EINAPP(Ein.EIN{params, index, body},args))=let
219 : cchiw 2867 val _=testp["\n split \n ",P.printbody body]
220 : cchiw 2843 val zero= (setEinZero y,[])
221 : cchiw 2838 val default=((y,einapp),[])
222 :     val sumIndex=ref []
223 : cchiw 2867 val str="Poorly formed EIN operator. Argument needs to be applied in High-IL"^(P.printbody body)
224 : cchiw 2838 fun rewrite b=(case b
225 : cchiw 2847 of E.Probe (E.Conv _,_) => default
226 : cchiw 2867 | E.Probe(E.Field _,_) => zero
227 : cchiw 2847 | E.Probe _ => raise Fail str
228 : cchiw 2838 | E.Conv _ => zero
229 :     | E.Field _ => zero
230 :     | E.Apply _ => zero
231 :     | E.Lift e => zero
232 :     | E.Delta _ => default
233 :     | E.Epsilon _ => default
234 : cchiw 2843 | E.Eps2 _ => default
235 : cchiw 2838 | E.Tensor _ => default
236 :     | E.Const _ => default
237 :     | E.Neg e1 => handleNeg(y,e1,params,index,args)
238 : cchiw 2867 | E.Sqrt e1 => handleSqrt(y,e1,params,index,args)
239 : cchiw 2838 | E.Sub (e1,e2) => handleSub(y,e1,e2,params,index,args)
240 :     | E.Div (e1,e2) => handleDiv(y,e1,e2,params,index,args)
241 : cchiw 2847 | E.Sum(_,E.Prod[E.Eps2 _, E.Probe(E.Conv _,_) ]) => default
242 :     | E.Sum(_,E.Prod[E.Epsilon _, E.Probe(E.Conv _,_) ]) => default
243 :     | E.Sum(_,E.Probe(E.Conv _,_)) => default
244 : cchiw 2838 | E.Sum(_,E.Conv _) => zero
245 :     | E.Sum(sx,E.Prod e1) => handleSumProd(y,e1,params,index,sx,args)
246 :     | E.Sum(sx,E.Neg n) => rewrite (E.Neg(E.Sum(sx,n)))
247 :     | E.Sum(sx,E.Add a) => rewrite (E.Add(List.map (fn e=> E.Sum(sx,e)) a))
248 :     | E.Sum(sx,E.Sub (e1,e2)) => rewrite (E.Sub(E.Sum(sx,e1),E.Sum(sx,e2)))
249 :     | E.Sum(sx,E.Div(e1,e2)) => rewrite(E.Div(E.Sum(sx,e1),E.Sum(sx,e2)))
250 :     | E.Sum(c1, E.Sum (c2,e)) => rewrite (E.Sum (c1@c2,e))
251 :     | E.Sum(sx,_) => default
252 :     | E.Add e1 => handleAdd(y,e1,params,index,args)
253 :     | E.Prod e1 => handleProd(y,e1,params,index,args)
254 :     | E.Partial _ => err(" Partial used after normalize")
255 :     | E.Krn _ => err("Krn used before expand")
256 :     | E.Value _ => err("Value used before expand")
257 :     | E.Img _ => err("Probe used before expand")
258 :     (*end case *))
259 :     val (einapp2,newbies) =rewrite body
260 :     in
261 :     (einapp2,newbies)
262 :     end
263 :     |split(y,app) =((y,app),[])
264 :    
265 :     (* iterMultiple:code*code=> (code*code)
266 :     * recursively split ein expression into smaller pieces
267 :     *)
268 :     fun iterMultiple(einapp2,newbies2)=let
269 :     fun itercode([],rest,code)=(rest,code)
270 :     | itercode(e1::newbies,rest,code)=let
271 :     val (einapp3,code3) =split(e1)
272 :     val (rest4,code4)=itercode(code3,[],[])
273 :     in itercode(newbies,rest@[einapp3],code4@rest4@code)
274 :     end
275 :     val(rest,code)= itercode(newbies2,[],[])
276 :     in
277 :     (einapp2,code@rest)
278 :     end
279 :    
280 : cchiw 2843 fun iterSplit(y,einapp as DstIL.EINAPP(Ein.EIN{params, index, body},args))=let
281 : cchiw 2857 val (einapp2,newbies2)=split (y,einapp)
282 : cchiw 2838 in
283 :     iterMultiple(einapp2,newbies2)
284 :     end
285 :    
286 :     (* gettest:code*code=> (code*code)
287 :     * print results for splitting einapp
288 :     *)
289 : cchiw 2845 fun gettest einapp=(case testing
290 : cchiw 2838 of 0=>iterSplit(einapp)
291 :     | _=>let
292 : cchiw 2845 val star="\n************* SPLIT********\n"
293 :     val _ =print(String.concat[star,"\n","start get test",printEINAPP einapp])
294 : cchiw 2838 val (einapp2,newbies)=iterSplit(einapp)
295 :     val a=printEINAPP einapp2
296 :     val b=String.concatWith",\n\t"(List.map printEINAPP newbies)
297 :     val _ =print(String.concat[printEINAPP einapp,"=>",a," newbies\n\t",b, "\n",a,star])
298 :     in
299 :     (einapp2,newbies)
300 :     end
301 :     (*end case*))
302 :    
303 :     end; (* local *)
304 :    
305 :     end (* local *)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0