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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2397 - (view) (download)

1 : lamonts 345 (* mid-to-low.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : lamonts 345 * All rights reserved.
5 :     *
6 :     * Translation from MidIL to LowIL representations.
7 :     *)
8 :    
9 :     structure MidToLow : sig
10 :    
11 : jhr 459 val translate : MidIL.program -> LowIL.program
12 : lamonts 345
13 : jhr 387 end = struct
14 : lamonts 345
15 :     structure SrcIL = MidIL
16 :     structure SrcOp = MidOps
17 : jhr 1640 structure SrcSV = SrcIL.StateVar
18 :     structure SrcTy = MidILTypes
19 : jhr 387 structure VTbl = SrcIL.Var.Tbl
20 : lamonts 345 structure DstIL = LowIL
21 : jhr 464 structure DstTy = LowILTypes
22 : lamonts 345 structure DstOp = LowOps
23 :    
24 : jhr 1640 (* instantiate the translation environment *)
25 :     local
26 :     type var_env = DstIL.var VTbl.hash_table
27 :     type state_var_env = DstIL.state_var SrcSV.Tbl.hash_table
28 : jhr 387
29 : jhr 1640 fun rename (env : var_env, x) = (case VTbl.find env x
30 :     of SOME x' => x'
31 :     | NONE => let
32 :     val x' = DstIL.Var.new (SrcIL.Var.name x, SrcIL.Var.ty x)
33 :     in
34 :     VTbl.insert env (x, x');
35 :     x'
36 :     end
37 :     (* end case *))
38 : lamonts 345
39 : jhr 1640 fun renameSV (env : state_var_env, x) = (case SrcSV.Tbl.find env x
40 :     of SOME x' => x'
41 :     | NONE => let
42 :     val x' = DstIL.StateVar.new (SrcSV.isOutput x, SrcSV.name x, SrcSV.ty x)
43 :     in
44 :     SrcSV.Tbl.insert env (x, x');
45 :     x'
46 :     end
47 :     (* end case *))
48 :     in
49 :     structure Env = TranslateEnvFn (
50 :     struct
51 :     structure SrcIL = SrcIL
52 :     structure DstIL = DstIL
53 :     type var_env = var_env
54 :     type state_var_env = state_var_env
55 :     val rename = rename
56 :     val renameSV = renameSV
57 :     end)
58 :     end (* local *)
59 :    
60 : jhr 463 (* convert a rational to a FloatLit.float value. We do this by long division
61 :     * with a cutoff when we get to 12 digits.
62 :     *)
63 :     fun ratToFloat r = (case Rational.explode r
64 : jhr 464 of {sign=0, ...} => FloatLit.zero false
65 : jhr 2356 | {sign, num, denom=1} => FloatLit.fromInt(IntInf.fromInt sign * num)
66 : jhr 463 | {sign, num, denom} => let
67 :     (* normalize so that num <= denom *)
68 :     val (denom, exp) = let
69 :     fun lp (n, denom) = if (denom < num)
70 :     then lp(n+1, denom*10)
71 : jhr 464 else (denom, n)
72 : jhr 463 in
73 : jhr 464 lp (1, denom)
74 : jhr 463 end
75 :     (* normalize so that num <= denom < 10*num *)
76 : jhr 464 val (num, exp) = let
77 :     fun lp (n, num) = if (10*num < denom)
78 :     then lp(n-1, 10*num)
79 :     else (num, n)
80 :     in
81 :     lp (exp, num)
82 :     end
83 : jhr 463 (* divide num/denom, computing the resulting digits *)
84 :     fun divLp (n, a) = let
85 :     val (q, r) = IntInf.divMod(a, denom)
86 :     in
87 :     if (r = 0) then (q, [])
88 :     else if (n < 12) then let
89 :     val (d, dd) = divLp(n+1, 10*r)
90 :     in
91 : jhr 464 if (d < 10)
92 :     then (q, (IntInf.toInt d)::dd)
93 :     else (q+1, 0::dd)
94 : jhr 463 end
95 :     else if (IntInf.div(10*r, denom) < 5)
96 :     then (q, [])
97 :     else (q+1, []) (* round up *)
98 :     end
99 : jhr 464 val digits = let
100 :     val (d, dd) = divLp (0, num)
101 :     in
102 :     (IntInf.toInt d)::dd
103 :     end
104 : jhr 463 in
105 : jhr 464 FloatLit.fromDigits{isNeg=(sign < 0), digits=digits, exp=exp}
106 : jhr 463 end
107 : jhr 464 (* end case *))
108 : jhr 463
109 : cchiw 2397 (*Note, do we need types?
110 :     fun imul (r : DstIL.var, a, b) = (r, DstIL.OP(DstOp.IMul DstTy.intTy, [a, b]))
111 :     fun iadd (r : DstIL.var, a, b) = (r, DstIL.OP(DstOp.IAdd DstTy.intTy, [a, b]))
112 :     fun ilit (r : DstIL.var, n) = (r, DstIL.LIT(Literal.Int(IntInf.fromInt n)))
113 :     fun radd (r : DstIL.var, a, b) = (r, DstIL.OP(DstOp.IAdd DstTy.realTy, [a, b]))
114 :     *)
115 :     fun imul (r : DstIL.var, a, b) = (r, DstIL.OP(DstOp.IMul, [a, b]))
116 :     fun iadd (r : DstIL.var, a, b) = (r, DstIL.OP(DstOp.IAdd, [a, b]))
117 : jhr 1116 fun ilit (r : DstIL.var, n) = (r, DstIL.LIT(Literal.Int(IntInf.fromInt n)))
118 : cchiw 2397 fun radd (r : DstIL.var, a, b) = (r, DstIL.OP(DstOp.IAdd, [a, b]))
119 : jhr 511
120 : jhr 465 (* expand the EvalKernel operations into vector operations. The parameters
121 :     * are
122 : jhr 459 * result -- the lhs variable to store the result
123 : jhr 465 * d -- the vector width of the operation, which should be equal
124 :     * to twice the support of the kernel
125 : jhr 459 * h -- the kernel
126 :     * k -- the derivative of the kernel to evaluate
127 : jhr 465 *
128 :     * The generated code is computing
129 :     *
130 :     * result = a_0 + x*(a_1 + x*(a_2 + ... x*a_n) ... )
131 :     *
132 :     * as a d-wide vector operation, where n is the degree of the kth derivative
133 :     * of h and the a_i are coefficient vectors that have an element for each
134 :     * piece of h. The computation is implemented as follows
135 :     *
136 :     * m_n = x * a_n
137 :     * s_{n-1} = a_{n-1} + m_n
138 :     * m_{n-1} = x * s_{n-1}
139 :     * s_{n-2} = a_{n-2} + m_{n-1}
140 :     * m_{n-2} = x * s_{n-2}
141 :     * ...
142 :     * s_1 = a_1 + m_2
143 :     * m_1 = x * s_1
144 :     * result = a_0 + m_1
145 : jhr 1116 *
146 :     * Note that the coeffient vectors are flipped (cf high-to-low/probe.sml).
147 : jhr 459 *)
148 : cchiw 2397
149 :    
150 :     (****************************)
151 :     (*Note, assuming Mult->IMult,
152 :     since otherwise would have created a tensor multiply or a scale.
153 :    
154 :     *)
155 :     fun decideAdd(n, arg)= case n
156 :     of DstTy.vecTy 1 => DstIL.OP(DstOp.IAdd, arg)
157 :     | DstTy.IntTy => DstIL.OP(DstOp.IAdd, arg)
158 :     |_ => let
159 :     val shape = shapeVarToTensor sv
160 :     val operator = EinOp.addTensors
161 :     DSTIL.EINAPP(S.transform(operator,[len(shape), shape]), n )
162 :    
163 :    
164 :     (****************************)
165 :    
166 : jhr 463 fun expandEvalKernel (result, d, h, k, [x]) = let
167 : jhr 459 val {isCont, segs} = Kernel.curve (h, k)
168 : jhr 465 (* degree of polynomial *)
169 :     val deg = List.length(hd segs) - 1
170 : jhr 463 (* convert to a vector of vectors to give fast access *)
171 : jhr 1116 val segs = Vector.fromList (List.rev (List.map Vector.fromList segs))
172 : jhr 463 (* get the kernel coefficient value for the d'th term of the i'th
173 :     * segment.
174 :     *)
175 : jhr 465 fun coefficient d i =
176 :     Literal.Float(ratToFloat (Vector.sub (Vector.sub(segs, i), d)))
177 : jhr 1116 val ty = DstTy.vecTy d
178 : jhr 463 val coeffs = List.tabulate (deg+1,
179 : jhr 465 fn i => DstIL.Var.new("a"^Int.toString i, ty))
180 : jhr 464 (* code to define the coefficient vectors *)
181 :     val coeffVecs = let
182 :     fun mk (x, (i, code)) = let
183 :     val lits = List.tabulate(d, coefficient i)
184 :     val vars = List.tabulate(d, fn _ => DstIL.Var.new("_f", DstTy.realTy))
185 :     val code =
186 :     ListPair.map (fn (x, lit) => (x, DstIL.LIT lit)) (vars, lits) @
187 : jhr 1116 (x, DstIL.CONS(DstIL.Var.ty x, vars)) :: code
188 : jhr 464 in
189 :     (i-1, code)
190 :     end
191 :     in
192 :     #2 (List.foldr mk (deg, []) coeffs)
193 :     end
194 : jhr 463 (* build the evaluation of the polynomials in reverse order *)
195 : jhr 465 fun pTmp i = DstIL.Var.new("prod" ^ Int.toString i, ty)
196 :     fun sTmp i = DstIL.Var.new("sum" ^ Int.toString i, ty)
197 :     fun eval (i, [coeff]) = let
198 :     val m = pTmp i
199 : cchiw 2397
200 : jhr 465 in
201 : cchiw 2397 (*(m, [(m, DstIL.OP(DstOp.Mul ty, [x, coeff]) )])*)
202 :    
203 :     (m, [(m, DstIL.OP(DstOp.IMul, [x, coeff]) )])
204 : jhr 465 end
205 : jhr 467 | eval (i, coeff::r) = let
206 : jhr 465 val (m, stms) = eval(i+1, r)
207 :     val s = sTmp i
208 : jhr 467 val m' = pTmp i
209 : cchiw 2397
210 :     (*val stms =
211 :     (m', DstIL.OP(DstOp.IMul ty, [x, s])) ::
212 :     (s, DstIL.OP(DstOp.IAdd ty, [coeff, m])) ::
213 :     stms*)
214 :    
215 :     val replaceAdd= decideAdd(ty, [coeff, m])
216 : jhr 463 val stms =
217 : cchiw 2397 (m', DstIL.OP(DstOp.IMul, [x, s])) ::
218 :     (s, replaceAdd) ::
219 :     stms
220 :    
221 :    
222 : jhr 463 in
223 : jhr 465 (m', stms)
224 : jhr 463 end
225 : jhr 1116 val evalCode = (case coeffs
226 :     of [a0] => (* constant function *)
227 :     [(result, DstIL.VAR a0)]
228 :     | a0::r => let
229 :     val (m, stms) = eval (1, r)
230 : cchiw 2397 (**COME BACK HERE *)
231 : jhr 1116 in
232 : cchiw 2397 List.rev ((result, DstIL.OP(DstOp.IAdd ty, [a0, m]))::stms)
233 : jhr 1116 end
234 :     (* end case *))
235 : jhr 459 in
236 : jhr 464 coeffVecs @ evalCode
237 : jhr 459 end
238 : jhr 387
239 : jhr 1116 (* FIXME: we will get better down-stream CSE if we structure the address computation
240 :     * as
241 :     * (base + stride * (...)) + offset
242 :     * since the lhs argument will be the same for each sample.
243 :     *)
244 :     (* add code to handle the offset and stride when addressing non-scalar image data *)
245 :     fun adjustForStrideAndOffset (1, _, ix, code) = (ix, code)
246 :     | adjustForStrideAndOffset (stride, 0, ix, code) = let
247 :     val offp = DstIL.Var.new ("offp", DstTy.intTy)
248 :     val stride' = DstIL.Var.new ("stride", DstTy.intTy)
249 :     in
250 :     (offp, imul(offp, stride', ix) :: ilit(stride', stride) :: code)
251 :     end
252 :     | adjustForStrideAndOffset (stride, offset, ix, code) = let
253 :     val offp = DstIL.Var.new ("offp", DstTy.intTy)
254 :     val stride' = DstIL.Var.new ("stride", DstTy.intTy)
255 :     val offset' = DstIL.Var.new ("offset", DstTy.intTy)
256 :     val t = DstIL.Var.new ("t", DstTy.intTy)
257 :     val code =
258 :     iadd(offp, offset', t) ::
259 :     ilit (offset', offset) ::
260 :     imul(t, stride', ix) ::
261 :     ilit (stride', stride) ::
262 :     code
263 :     in
264 :     (offp, code)
265 :     end
266 :    
267 : jhr 465 (* compute the load address for a given set of voxels indices. For the
268 :     * operation
269 :     *
270 : jhr 1116 * VoxelAddress<info,offset>(i_1, ..., i_d)
271 : jhr 465 *
272 :     * the address is given by
273 :     *
274 : jhr 1116 * base + offset + stride * (i_1 + N_1 * (i_2 + N_2 * (... + N_{d-1} * i_d) ...))
275 : jhr 465 *
276 :     * where
277 :     * base -- base address of the image data
278 : jhr 1116 * stride -- number of samples per voxel
279 :     * offset -- offset of sample being addressed
280 : jhr 465 * N_i -- size of ith axis in elements
281 : jhr 1116 *
282 :     * Note that we are following the Nrrd convention that the axes are ordered
283 :     * in fastest to slowest order. We are also assuming the C semantics of address
284 :     * arithmetic, where the offset will be automatically scaled by the size of the
285 :     * elements.
286 : jhr 465 *)
287 : jhr 1116 fun expandVoxelAddress (result, info, offset, [img, ix]) = let
288 :     val dim = ImageInfo.dim info
289 :     val stride = ImageInfo.stride info
290 :     val shape = ImageInfo.voxelShape info
291 :     val (offp, code) = adjustForStrideAndOffset (stride, offset, ix, [])
292 :     val addrTy = DstTy.AddrTy info
293 :     val base = DstIL.Var.new ("imgBaseAddr", addrTy)
294 : cchiw 2397 val code = (result, DstIL.OP(DstOp.IAdd addrTy, [base, offp])) ::
295 : jhr 1116 (base, DstIL.OP(DstOp.ImageAddress info, [img])) ::
296 :     code
297 :     in
298 :     List.rev code
299 :     end
300 :     | expandVoxelAddress (result, info, offset, img::ix1::indices) = let
301 :     val dim = ImageInfo.dim info
302 :     val sizes = ImageInfo.sizes info
303 :     val stride = ImageInfo.stride info
304 :     val shape = ImageInfo.voxelShape info
305 :     (* get N_1 ... N_{d-1} *)
306 : jhr 2356 (* FIXME: sizes is [] when the image does not have a proxy *)
307 : jhr 1116 val sizes = List.take (sizes, List.length sizes - 1)
308 : jhr 511 (* generate the address computation code in reverse order *)
309 :     fun gen (d, [n], [ix]) = let
310 :     val n' = DstIL.Var.new ("n" ^ Int.toString d, DstTy.intTy)
311 :     val t = DstIL.Var.new ("t", DstTy.intTy)
312 :     val code = [
313 :     imul(t, n', ix),
314 : jhr 1116 ilit(n', n)
315 : jhr 511 ]
316 :     in
317 :     (t, code)
318 :     end
319 :     | gen (d, n::ns, ix::ixs) = let
320 :     val n' = DstIL.Var.new ("n" ^ Int.toString d, DstTy.intTy)
321 :     val t1 = DstIL.Var.new ("t1", DstTy.intTy)
322 :     val t2 = DstIL.Var.new ("t2", DstTy.intTy)
323 :     val (t, code) = gen (d+1, ns, ixs)
324 :     val code =
325 :     imul(t2, n', t1) ::
326 : jhr 1116 ilit(n', n) ::
327 : jhr 511 iadd(t1, ix, t) :: code
328 :     in
329 :     (t2, code)
330 :     end
331 : jhr 2356 (* FIXME: sizes is [] when the image does not have a proxy *)
332 : jhr 1116 val (tmp, code) = gen (0, sizes, indices)
333 :     val t = DstIL.Var.new ("index", DstTy.intTy)
334 :     val code = iadd(t, ix1, tmp) :: code
335 :     val (offp, code) = adjustForStrideAndOffset (stride, offset, t, code)
336 :     val addrTy = DstTy.AddrTy info
337 :     val base = DstIL.Var.new ("imgBaseAddr", addrTy)
338 : cchiw 2397 val code = (result, DstIL.OP(DstOp.IAdd addrTy, [base, offp])) ::
339 : jhr 1116 (base, DstIL.OP(DstOp.ImageAddress info, [img])) ::
340 : jhr 511 code
341 :     in
342 :     List.rev code
343 :     end
344 : lamonts 345
345 : jhr 1370
346 : cchiw 2397
347 : jhr 431 fun expandOp (env, y, rator, args) = let
348 : jhr 1640 val args' = Env.renameList (env, args)
349 : jhr 465 fun assign rator' = [(y, DstIL.OP(rator', args'))]
350 : jhr 431 in
351 :     case rator
352 : cchiw 2397 of SrcOp.IAdd ty => assign (DstOp.IAdd ty)
353 :     | SrcOp.ISub ty => assign (DstOp.ISub ty)
354 :     | SrcOp.IMul ty => assign (DstOp.IMul ty)
355 :     | SrcOp.IDiv ty => assign (DstOp.IDiv ty)
356 :     | SrcOp.INeg ty => assign (DstOp.INeg ty)
357 : jhr 1116 | SrcOp.Abs ty => assign (DstOp.Abs ty)
358 : jhr 459 | SrcOp.LT ty => assign (DstOp.LT ty)
359 :     | SrcOp.LTE ty => assign (DstOp.LTE ty)
360 :     | SrcOp.EQ ty => assign (DstOp.EQ ty)
361 :     | SrcOp.NEQ ty => assign (DstOp.NEQ ty)
362 :     | SrcOp.GT ty => assign (DstOp.GT ty)
363 :     | SrcOp.GTE ty => assign (DstOp.GTE ty)
364 :     | SrcOp.Not => assign (DstOp.Not)
365 :     | SrcOp.Max => assign (DstOp.Max)
366 :     | SrcOp.Min => assign (DstOp.Min)
367 : jhr 1295 | SrcOp.Clamp ty => assign (DstOp.Clamp ty)
368 : jhr 1116 | SrcOp.Lerp ty => assign (DstOp.Lerp ty)
369 : cchiw 2397 | SrcOp.Norm ty => assign (DstOp.Norm ty)
370 : jhr 1116 | SrcOp.Normalize d => assign (DstOp.Normalize d)
371 :     | SrcOp.Scale ty => assign (DstOp.Scale ty)
372 :     | SrcOp.Zero ty => assign (DstOp.Zero ty)
373 : jhr 459 | SrcOp.PrincipleEvec ty => assign (DstOp.PrincipleEvec ty)
374 : jhr 1640 | SrcOp.EigenVals2x2 => assign (DstOp.EigenVals2x2)
375 :     | SrcOp.EigenVals3x3 => assign (DstOp.EigenVals3x3)
376 : cchiw 2397
377 : jhr 1640 | SrcOp.Select(ty as SrcTy.TupleTy tys, i) => assign (DstOp.Select(ty, i))
378 :     | SrcOp.Index(ty, i) => assign (DstOp.Index(ty, i))
379 : cchiw 2397 | SrcOp.ISubscript ty => assign (DstOp.ISubscript ty)
380 : jhr 1116 | SrcOp.Ceiling d => assign (DstOp.Ceiling d)
381 : jhr 459 | SrcOp.Floor d => assign (DstOp.Floor d)
382 : jhr 1116 | SrcOp.Round d => assign (DstOp.Round d)
383 :     | SrcOp.Trunc d => assign (DstOp.Trunc d)
384 : jhr 459 | SrcOp.IntToReal => assign (DstOp.IntToReal)
385 : jhr 1116 | SrcOp.RealToInt d => assign (DstOp.RealToInt d)
386 :     | SrcOp.VoxelAddress(info, offset) => expandVoxelAddress (y, info, offset, args')
387 : jhr 459 | SrcOp.LoadVoxels(rty, d) => assign (DstOp.LoadVoxels(rty, d))
388 : jhr 460 | SrcOp.PosToImgSpace info => assign (DstOp.PosToImgSpace info)
389 : jhr 1116 | SrcOp.TensorToWorldSpace(info, ty) => assign (DstOp.TensorToWorldSpace(info, ty))
390 : jhr 465 | SrcOp.EvalKernel(d, h, k) => expandEvalKernel(y, d, h, k, args')
391 : jhr 459 | SrcOp.LoadImage info => assign (DstOp.LoadImage info)
392 :     | SrcOp.Inside info => assign (DstOp.Inside info)
393 : jhr 1301 | SrcOp.Input(ty, s, desc) => assign (DstOp.Input(ty, s, desc))
394 :     | SrcOp.InputWithDefault(ty, s, desc) =>
395 :     assign (DstOp.InputWithDefault(ty, s, desc))
396 : jhr 1640 | rator => raise Fail("bogus operator " ^ SrcOp.toString rator)
397 : jhr 431 (* end case *)
398 :     end
399 :    
400 : jhr 1116 (* expand a SrcIL assignment to a DstIL CFG *)
401 : jhr 387 fun expand (env, (y, rhs)) = let
402 : jhr 1640 val y' = Env.rename (env, y)
403 :     fun assign rhs = [DstIL.ASSGN(y', rhs)]
404 : jhr 387 in
405 :     case rhs
406 : jhr 1640 of SrcIL.STATE x => assign (DstIL.STATE(Env.renameSV(env, x)))
407 :     | SrcIL.VAR x => assign (DstIL.VAR(Env.rename(env, x)))
408 : jhr 387 | SrcIL.LIT lit => assign (DstIL.LIT lit)
409 : jhr 1640 | SrcIL.OP(rator, args) => List.map DstIL.ASSGN (expandOp (env, y', rator, args))
410 :     | SrcIL.APPLY(f, args) => assign (DstIL.APPLY(f, Env.renameList(env, args)))
411 :     | SrcIL.CONS(ty, args) => assign (DstIL.CONS(ty, Env.renameList(env, args)))
412 : jhr 387 (* end case *)
413 :     end
414 : lamonts 345
415 : jhr 1640 (* expand a SrcIL multi-assignment to a DstIL CFG *)
416 :     fun mexpand (env, (ys, rator, xs)) = let
417 :     val ys' = Env.renameList(env, ys)
418 :     val rator' = (case rator
419 :     of SrcOp.EigenVecs2x2 => DstOp.EigenVecs2x2
420 :     | SrcOp.EigenVecs3x3 => DstOp.EigenVecs3x3
421 :     | SrcOp.Print tys => DstOp.Print tys
422 :     | _ => raise Fail("bogus operator " ^ SrcOp.toString rator)
423 :     (* end case *))
424 :     val xs' = Env.renameList(env, xs)
425 :     val nd = DstIL.Node.mkMASSIGN(ys', rator', xs')
426 :     in
427 :     DstIL.CFG{entry=nd, exit=nd}
428 :     end
429 :    
430 : jhr 387 structure Trans = TranslateFn (
431 :     struct
432 : jhr 1640 open Env
433 :     val expand = DstIL.CFG.mkBlock o expand
434 :     val mexpand = mexpand
435 : jhr 387 end)
436 :    
437 : jhr 1116 fun translate prog = let
438 :     val prog = Trans.translate prog
439 : jhr 387 in
440 : jhr 1116 LowILCensus.init prog;
441 :     prog
442 : jhr 387 end
443 :    
444 : jhr 435 end

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