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 3349 - (view) (download)

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

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