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

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