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

SCM Repository

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

Annotation of /trunk/src/compiler/mid-to-low/mid-to-low.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1640 - (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 463 | {sign, num, denom=1} => FloatLit.fromInt(sign * IntInf.toInt num)
66 :     | {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 : jhr 1116 fun imul (r : DstIL.var, a, b) = (r, DstIL.OP(DstOp.Mul DstTy.intTy, [a, b]))
110 :     fun iadd (r : DstIL.var, a, b) = (r, DstIL.OP(DstOp.Add DstTy.intTy, [a, b]))
111 :     fun ilit (r : DstIL.var, n) = (r, DstIL.LIT(Literal.Int(IntInf.fromInt n)))
112 : jhr 1370 fun radd (r : DstIL.var, a, b) = (r, DstIL.OP(DstOp.Add DstTy.realTy, [a, b]))
113 : jhr 511
114 : jhr 465 (* expand the EvalKernel operations into vector operations. The parameters
115 :     * are
116 : jhr 459 * result -- the lhs variable to store the result
117 : jhr 465 * d -- the vector width of the operation, which should be equal
118 :     * to twice the support of the kernel
119 : jhr 459 * h -- the kernel
120 :     * k -- the derivative of the kernel to evaluate
121 : jhr 465 *
122 :     * The generated code is computing
123 :     *
124 :     * result = a_0 + x*(a_1 + x*(a_2 + ... x*a_n) ... )
125 :     *
126 :     * as a d-wide vector operation, where n is the degree of the kth derivative
127 :     * of h and the a_i are coefficient vectors that have an element for each
128 :     * piece of h. The computation is implemented as follows
129 :     *
130 :     * m_n = x * a_n
131 :     * s_{n-1} = a_{n-1} + m_n
132 :     * m_{n-1} = x * s_{n-1}
133 :     * s_{n-2} = a_{n-2} + m_{n-1}
134 :     * m_{n-2} = x * s_{n-2}
135 :     * ...
136 :     * s_1 = a_1 + m_2
137 :     * m_1 = x * s_1
138 :     * result = a_0 + m_1
139 : jhr 1116 *
140 :     * Note that the coeffient vectors are flipped (cf high-to-low/probe.sml).
141 : jhr 459 *)
142 : jhr 463 fun expandEvalKernel (result, d, h, k, [x]) = let
143 : jhr 459 val {isCont, segs} = Kernel.curve (h, k)
144 : jhr 465 (* degree of polynomial *)
145 :     val deg = List.length(hd segs) - 1
146 : jhr 463 (* convert to a vector of vectors to give fast access *)
147 : jhr 1116 val segs = Vector.fromList (List.rev (List.map Vector.fromList segs))
148 : jhr 463 (* get the kernel coefficient value for the d'th term of the i'th
149 :     * segment.
150 :     *)
151 : jhr 465 fun coefficient d i =
152 :     Literal.Float(ratToFloat (Vector.sub (Vector.sub(segs, i), d)))
153 : jhr 1116 val ty = DstTy.vecTy d
154 : jhr 463 val coeffs = List.tabulate (deg+1,
155 : jhr 465 fn i => DstIL.Var.new("a"^Int.toString i, ty))
156 : jhr 464 (* code to define the coefficient vectors *)
157 :     val coeffVecs = let
158 :     fun mk (x, (i, code)) = let
159 :     val lits = List.tabulate(d, coefficient i)
160 :     val vars = List.tabulate(d, fn _ => DstIL.Var.new("_f", DstTy.realTy))
161 :     val code =
162 :     ListPair.map (fn (x, lit) => (x, DstIL.LIT lit)) (vars, lits) @
163 : jhr 1116 (x, DstIL.CONS(DstIL.Var.ty x, vars)) :: code
164 : jhr 464 in
165 :     (i-1, code)
166 :     end
167 :     in
168 :     #2 (List.foldr mk (deg, []) coeffs)
169 :     end
170 : jhr 463 (* build the evaluation of the polynomials in reverse order *)
171 : jhr 465 fun pTmp i = DstIL.Var.new("prod" ^ Int.toString i, ty)
172 :     fun sTmp i = DstIL.Var.new("sum" ^ Int.toString i, ty)
173 :     fun eval (i, [coeff]) = let
174 :     val m = pTmp i
175 :     in
176 :     (m, [(m, DstIL.OP(DstOp.Mul ty, [x, coeff]))])
177 :     end
178 : jhr 467 | eval (i, coeff::r) = let
179 : jhr 465 val (m, stms) = eval(i+1, r)
180 :     val s = sTmp i
181 : jhr 467 val m' = pTmp i
182 : jhr 463 val stms =
183 : jhr 465 (m', DstIL.OP(DstOp.Mul ty, [x, s])) ::
184 :     (s, DstIL.OP(DstOp.Add ty, [coeff, m])) ::
185 : jhr 463 stms
186 :     in
187 : jhr 465 (m', stms)
188 : jhr 463 end
189 : jhr 1116 val evalCode = (case coeffs
190 :     of [a0] => (* constant function *)
191 :     [(result, DstIL.VAR a0)]
192 :     | a0::r => let
193 :     val (m, stms) = eval (1, r)
194 :     in
195 :     List.rev ((result, DstIL.OP(DstOp.Add ty, [a0, m]))::stms)
196 :     end
197 :     (* end case *))
198 : jhr 459 in
199 : jhr 464 coeffVecs @ evalCode
200 : jhr 459 end
201 : jhr 387
202 : jhr 1116 (* FIXME: we will get better down-stream CSE if we structure the address computation
203 :     * as
204 :     * (base + stride * (...)) + offset
205 :     * since the lhs argument will be the same for each sample.
206 :     *)
207 :     (* add code to handle the offset and stride when addressing non-scalar image data *)
208 :     fun adjustForStrideAndOffset (1, _, ix, code) = (ix, code)
209 :     | adjustForStrideAndOffset (stride, 0, ix, code) = let
210 :     val offp = DstIL.Var.new ("offp", DstTy.intTy)
211 :     val stride' = DstIL.Var.new ("stride", DstTy.intTy)
212 :     in
213 :     (offp, imul(offp, stride', ix) :: ilit(stride', stride) :: code)
214 :     end
215 :     | adjustForStrideAndOffset (stride, offset, ix, code) = let
216 :     val offp = DstIL.Var.new ("offp", DstTy.intTy)
217 :     val stride' = DstIL.Var.new ("stride", DstTy.intTy)
218 :     val offset' = DstIL.Var.new ("offset", DstTy.intTy)
219 :     val t = DstIL.Var.new ("t", DstTy.intTy)
220 :     val code =
221 :     iadd(offp, offset', t) ::
222 :     ilit (offset', offset) ::
223 :     imul(t, stride', ix) ::
224 :     ilit (stride', stride) ::
225 :     code
226 :     in
227 :     (offp, code)
228 :     end
229 :    
230 : jhr 465 (* compute the load address for a given set of voxels indices. For the
231 :     * operation
232 :     *
233 : jhr 1116 * VoxelAddress<info,offset>(i_1, ..., i_d)
234 : jhr 465 *
235 :     * the address is given by
236 :     *
237 : jhr 1116 * base + offset + stride * (i_1 + N_1 * (i_2 + N_2 * (... + N_{d-1} * i_d) ...))
238 : jhr 465 *
239 :     * where
240 :     * base -- base address of the image data
241 : jhr 1116 * stride -- number of samples per voxel
242 :     * offset -- offset of sample being addressed
243 : jhr 465 * N_i -- size of ith axis in elements
244 : jhr 1116 *
245 :     * Note that we are following the Nrrd convention that the axes are ordered
246 :     * in fastest to slowest order. We are also assuming the C semantics of address
247 :     * arithmetic, where the offset will be automatically scaled by the size of the
248 :     * elements.
249 : jhr 465 *)
250 : jhr 1116 fun expandVoxelAddress (result, info, offset, [img, ix]) = let
251 :     val dim = ImageInfo.dim info
252 :     val sizes = ImageInfo.sizes info
253 :     val stride = ImageInfo.stride info
254 :     val shape = ImageInfo.voxelShape info
255 :     val (offp, code) = adjustForStrideAndOffset (stride, offset, ix, [])
256 :     val addrTy = DstTy.AddrTy info
257 :     val base = DstIL.Var.new ("imgBaseAddr", addrTy)
258 :     val code = (result, DstIL.OP(DstOp.Add addrTy, [base, offp])) ::
259 :     (base, DstIL.OP(DstOp.ImageAddress info, [img])) ::
260 :     code
261 :     in
262 :     List.rev code
263 :     end
264 :     | expandVoxelAddress (result, info, offset, img::ix1::indices) = let
265 :     val dim = ImageInfo.dim info
266 :     val sizes = ImageInfo.sizes info
267 :     val stride = ImageInfo.stride info
268 :     val shape = ImageInfo.voxelShape info
269 :     (* get N_1 ... N_{d-1} *)
270 :     val sizes = List.take (sizes, List.length sizes - 1)
271 : jhr 511 (* generate the address computation code in reverse order *)
272 :     fun gen (d, [n], [ix]) = let
273 :     val n' = DstIL.Var.new ("n" ^ Int.toString d, DstTy.intTy)
274 :     val t = DstIL.Var.new ("t", DstTy.intTy)
275 :     val code = [
276 :     imul(t, n', ix),
277 : jhr 1116 ilit(n', n)
278 : jhr 511 ]
279 :     in
280 :     (t, code)
281 :     end
282 :     | gen (d, n::ns, ix::ixs) = let
283 :     val n' = DstIL.Var.new ("n" ^ Int.toString d, DstTy.intTy)
284 :     val t1 = DstIL.Var.new ("t1", DstTy.intTy)
285 :     val t2 = DstIL.Var.new ("t2", DstTy.intTy)
286 :     val (t, code) = gen (d+1, ns, ixs)
287 :     val code =
288 :     imul(t2, n', t1) ::
289 : jhr 1116 ilit(n', n) ::
290 : jhr 511 iadd(t1, ix, t) :: code
291 :     in
292 :     (t2, code)
293 :     end
294 : jhr 1116 val (tmp, code) = gen (0, sizes, indices)
295 :     val t = DstIL.Var.new ("index", DstTy.intTy)
296 :     val code = iadd(t, ix1, tmp) :: code
297 :     val (offp, code) = adjustForStrideAndOffset (stride, offset, t, code)
298 :     val addrTy = DstTy.AddrTy info
299 :     val base = DstIL.Var.new ("imgBaseAddr", addrTy)
300 :     val code = (result, DstIL.OP(DstOp.Add addrTy, [base, offp])) ::
301 :     (base, DstIL.OP(DstOp.ImageAddress info, [img])) ::
302 : jhr 511 code
303 :     in
304 :     List.rev code
305 :     end
306 : lamonts 345
307 : jhr 1370 (* expand trace(M) *)
308 :     fun expandTrace (y, d, [m]) = let
309 :     val matTy = DstTy.TensorTy[d,d]
310 :     val rowTy = DstTy.TensorTy[d]
311 : jhr 1640 fun f (i, dst) = if (i < d-1)
312 : jhr 1370 then let
313 :     val i' = Int.toString i
314 :     val ix = DstIL.Var.new ("ix" ^ i', DstTy.intTy)
315 :     val x = DstIL.Var.new ("x" ^ i', DstTy.realTy)
316 :     val acc = DstIL.Var.new ("acc" ^ i', DstTy.realTy)
317 :     val stms = f (i+1, acc)
318 :     in
319 :     radd(dst, acc, x) ::
320 :     (x, DstIL.OP(DstOp.Subscript(matTy), [m, ix, ix])) ::
321 :     ilit(ix, i) ::
322 :     stms
323 :     end
324 :     else let
325 :     val ix = DstIL.Var.new ("ix" ^ Int.toString i, DstTy.intTy)
326 :     in [
327 :     (dst, DstIL.OP(DstOp.Subscript(matTy), [m, ix, ix])),
328 :     ilit(ix, i)
329 :     ] end
330 :     in
331 : jhr 1640 List.rev (f (0, y))
332 : jhr 1370 end
333 :    
334 : jhr 431 fun expandOp (env, y, rator, args) = let
335 : jhr 1640 val args' = Env.renameList (env, args)
336 : jhr 465 fun assign rator' = [(y, DstIL.OP(rator', args'))]
337 : jhr 431 in
338 :     case rator
339 : jhr 459 of SrcOp.Add ty => assign (DstOp.Add ty)
340 :     | SrcOp.Sub ty => assign (DstOp.Sub ty)
341 :     | SrcOp.Mul ty => assign (DstOp.Mul ty)
342 :     | SrcOp.Div ty => assign (DstOp.Div ty)
343 :     | SrcOp.Neg ty => assign (DstOp.Neg ty)
344 : jhr 1116 | SrcOp.Abs ty => assign (DstOp.Abs ty)
345 : jhr 459 | SrcOp.LT ty => assign (DstOp.LT ty)
346 :     | SrcOp.LTE ty => assign (DstOp.LTE ty)
347 :     | SrcOp.EQ ty => assign (DstOp.EQ ty)
348 :     | SrcOp.NEQ ty => assign (DstOp.NEQ ty)
349 :     | SrcOp.GT ty => assign (DstOp.GT ty)
350 :     | SrcOp.GTE ty => assign (DstOp.GTE ty)
351 :     | SrcOp.Not => assign (DstOp.Not)
352 :     | SrcOp.Max => assign (DstOp.Max)
353 :     | SrcOp.Min => assign (DstOp.Min)
354 : jhr 1295 | SrcOp.Clamp ty => assign (DstOp.Clamp ty)
355 : jhr 1116 | SrcOp.Lerp ty => assign (DstOp.Lerp ty)
356 : jhr 459 | SrcOp.Dot d => assign (DstOp.Dot d)
357 : jhr 1116 | SrcOp.MulVecMat(d1, d2) => assign (DstOp.MulVecMat(d1, d2))
358 :     | SrcOp.MulMatVec(d1, d2) => assign (DstOp.MulMatVec(d1, d2))
359 :     | SrcOp.MulMatMat(d1, d2, d3) => assign (DstOp.MulMatMat(d1, d2, d3))
360 : jhr 459 | SrcOp.Cross => assign (DstOp.Cross)
361 : jhr 1116 | SrcOp.Norm ty => assign (DstOp.Norm ty)
362 :     | SrcOp.Normalize d => assign (DstOp.Normalize d)
363 :     | SrcOp.Scale ty => assign (DstOp.Scale ty)
364 :     | SrcOp.Zero ty => assign (DstOp.Zero ty)
365 : jhr 459 | SrcOp.PrincipleEvec ty => assign (DstOp.PrincipleEvec ty)
366 : jhr 1640 | SrcOp.EigenVals2x2 => assign (DstOp.EigenVals2x2)
367 :     | SrcOp.EigenVals3x3 => assign (DstOp.EigenVals3x3)
368 : jhr 1116 | SrcOp.Identity n => assign (DstOp.Identity n)
369 : jhr 1370 | SrcOp.Trace d => expandTrace (y, d, args')
370 : jhr 1640 | SrcOp.Select(ty as SrcTy.TupleTy tys, i) => assign (DstOp.Select(ty, i))
371 :     | SrcOp.Index(ty, i) => assign (DstOp.Index(ty, i))
372 :     | SrcOp.Subscript ty => assign (DstOp.Subscript ty)
373 : jhr 1116 | SrcOp.Ceiling d => assign (DstOp.Ceiling d)
374 : jhr 459 | SrcOp.Floor d => assign (DstOp.Floor d)
375 : jhr 1116 | SrcOp.Round d => assign (DstOp.Round d)
376 :     | SrcOp.Trunc d => assign (DstOp.Trunc d)
377 : jhr 459 | SrcOp.IntToReal => assign (DstOp.IntToReal)
378 : jhr 1116 | SrcOp.RealToInt d => assign (DstOp.RealToInt d)
379 :     | SrcOp.VoxelAddress(info, offset) => expandVoxelAddress (y, info, offset, args')
380 : jhr 459 | SrcOp.LoadVoxels(rty, d) => assign (DstOp.LoadVoxels(rty, d))
381 : jhr 460 | SrcOp.PosToImgSpace info => assign (DstOp.PosToImgSpace info)
382 : jhr 1116 | SrcOp.TensorToWorldSpace(info, ty) => assign (DstOp.TensorToWorldSpace(info, ty))
383 : jhr 465 | SrcOp.EvalKernel(d, h, k) => expandEvalKernel(y, d, h, k, args')
384 : jhr 459 | SrcOp.LoadImage info => assign (DstOp.LoadImage info)
385 :     | SrcOp.Inside info => assign (DstOp.Inside info)
386 : jhr 1301 | SrcOp.Input(ty, s, desc) => assign (DstOp.Input(ty, s, desc))
387 :     | SrcOp.InputWithDefault(ty, s, desc) =>
388 :     assign (DstOp.InputWithDefault(ty, s, desc))
389 : jhr 1640 | rator => raise Fail("bogus operator " ^ SrcOp.toString rator)
390 : jhr 431 (* end case *)
391 :     end
392 :    
393 : jhr 1116 (* expand a SrcIL assignment to a DstIL CFG *)
394 : jhr 387 fun expand (env, (y, rhs)) = let
395 : jhr 1640 val y' = Env.rename (env, y)
396 :     fun assign rhs = [DstIL.ASSGN(y', rhs)]
397 : jhr 387 in
398 :     case rhs
399 : jhr 1640 of SrcIL.STATE x => assign (DstIL.STATE(Env.renameSV(env, x)))
400 :     | SrcIL.VAR x => assign (DstIL.VAR(Env.rename(env, x)))
401 : jhr 387 | SrcIL.LIT lit => assign (DstIL.LIT lit)
402 : jhr 1640 | SrcIL.OP(rator, args) => List.map DstIL.ASSGN (expandOp (env, y', rator, args))
403 :     | SrcIL.APPLY(f, args) => assign (DstIL.APPLY(f, Env.renameList(env, args)))
404 :     | SrcIL.CONS(ty, args) => assign (DstIL.CONS(ty, Env.renameList(env, args)))
405 : jhr 387 (* end case *)
406 :     end
407 : lamonts 345
408 : jhr 1640 (* expand a SrcIL multi-assignment to a DstIL CFG *)
409 :     fun mexpand (env, (ys, rator, xs)) = let
410 :     val ys' = Env.renameList(env, ys)
411 :     val rator' = (case rator
412 :     of SrcOp.EigenVecs2x2 => DstOp.EigenVecs2x2
413 :     | SrcOp.EigenVecs3x3 => DstOp.EigenVecs3x3
414 :     | SrcOp.Print tys => DstOp.Print tys
415 :     | _ => raise Fail("bogus operator " ^ SrcOp.toString rator)
416 :     (* end case *))
417 :     val xs' = Env.renameList(env, xs)
418 :     val nd = DstIL.Node.mkMASSIGN(ys', rator', xs')
419 :     in
420 :     DstIL.CFG{entry=nd, exit=nd}
421 :     end
422 :    
423 : jhr 387 structure Trans = TranslateFn (
424 :     struct
425 : jhr 1640 open Env
426 :     val expand = DstIL.CFG.mkBlock o expand
427 :     val mexpand = mexpand
428 : jhr 387 end)
429 :    
430 : jhr 1116 fun translate prog = let
431 :     val prog = Trans.translate prog
432 : jhr 387 in
433 : jhr 1116 LowILCensus.init prog;
434 :     prog
435 : jhr 387 end
436 :    
437 : jhr 435 end

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