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 467 - (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 465 (* expand the EvalKernel operations into vector operations. The parameters
85 :     * are
86 : jhr 459 * result -- the lhs variable to store the result
87 : jhr 465 * d -- the vector width of the operation, which should be equal
88 :     * to twice the support of the kernel
89 : jhr 459 * h -- the kernel
90 :     * k -- the derivative of the kernel to evaluate
91 : jhr 465 *
92 :     * The generated code is computing
93 :     *
94 :     * result = a_0 + x*(a_1 + x*(a_2 + ... x*a_n) ... )
95 :     *
96 :     * as a d-wide vector operation, where n is the degree of the kth derivative
97 :     * of h and the a_i are coefficient vectors that have an element for each
98 :     * piece of h. The computation is implemented as follows
99 :     *
100 :     * m_n = x * a_n
101 :     * s_{n-1} = a_{n-1} + m_n
102 :     * m_{n-1} = x * s_{n-1}
103 :     * s_{n-2} = a_{n-2} + m_{n-1}
104 :     * m_{n-2} = x * s_{n-2}
105 :     * ...
106 :     * s_1 = a_1 + m_2
107 :     * m_1 = x * s_1
108 :     * result = a_0 + m_1
109 : jhr 459 *)
110 : jhr 463 fun expandEvalKernel (result, d, h, k, [x]) = let
111 : jhr 459 val {isCont, segs} = Kernel.curve (h, k)
112 : jhr 465 (* degree of polynomial *)
113 :     val deg = List.length(hd segs) - 1
114 : jhr 463 (* convert to a vector of vectors to give fast access *)
115 :     val segs = Vector.fromList (List.map Vector.fromList segs)
116 :     (* get the kernel coefficient value for the d'th term of the i'th
117 :     * segment.
118 :     *)
119 : jhr 465 fun coefficient d i =
120 :     Literal.Float(ratToFloat (Vector.sub (Vector.sub(segs, i), d)))
121 : jhr 463 val ty = DstTy.VecTy d
122 :     val coeffs = List.tabulate (deg+1,
123 : jhr 465 fn i => DstIL.Var.new("a"^Int.toString i, ty))
124 : jhr 464 (* code to define the coefficient vectors *)
125 :     val coeffVecs = let
126 :     fun mk (x, (i, code)) = let
127 :     val lits = List.tabulate(d, coefficient i)
128 :     val vars = List.tabulate(d, fn _ => DstIL.Var.new("_f", DstTy.realTy))
129 :     val code =
130 :     ListPair.map (fn (x, lit) => (x, DstIL.LIT lit)) (vars, lits) @
131 :     (x, DstIL.CONS vars) :: code
132 :     in
133 :     (i-1, code)
134 :     end
135 :     in
136 :     #2 (List.foldr mk (deg, []) coeffs)
137 :     end
138 : jhr 463 (* build the evaluation of the polynomials in reverse order *)
139 : jhr 465 fun pTmp i = DstIL.Var.new("prod" ^ Int.toString i, ty)
140 :     fun sTmp i = DstIL.Var.new("sum" ^ Int.toString i, ty)
141 :     fun eval (i, [coeff]) = let
142 :     val m = pTmp i
143 :     in
144 :     (m, [(m, DstIL.OP(DstOp.Mul ty, [x, coeff]))])
145 :     end
146 : jhr 467 | eval (i, coeff::r) = let
147 : jhr 465 val (m, stms) = eval(i+1, r)
148 :     val s = sTmp i
149 : jhr 467 val m' = pTmp i
150 : jhr 463 val stms =
151 : jhr 465 (m', DstIL.OP(DstOp.Mul ty, [x, s])) ::
152 :     (s, DstIL.OP(DstOp.Add ty, [coeff, m])) ::
153 : jhr 463 stms
154 :     in
155 : jhr 465 (m', stms)
156 : jhr 463 end
157 : jhr 465 val evalCode = let
158 :     val a0::r = coeffs
159 :     val (m, stms) = eval (1, r)
160 :     in
161 :     List.rev ((result, DstIL.OP(DstOp.Add ty, [a0, m]))::stms)
162 :     end
163 : jhr 459 in
164 : jhr 464 coeffVecs @ evalCode
165 : jhr 459 end
166 : jhr 387
167 : jhr 465 (* compute the load address for a given set of voxels indices. For the
168 :     * operation
169 :     *
170 :     * VoxelAddress<info>(i_1, ..., i_d)
171 :     *
172 :     * the address is given by
173 :     *
174 :     * base + szb * (i_1 + N_2 * (i_2 + N_3 * (... + N_d * i_d) ...))
175 :     *
176 :     * where
177 :     * base -- base address of the image data
178 :     * szb -- image-element size in bytes
179 :     * N_i -- size of ith axis in elements
180 :     *)
181 :     fun expandVoxelAddress (result, info, indices) = raise Fail "unimplemented"
182 : lamonts 345
183 : jhr 431 fun expandOp (env, y, rator, args) = let
184 : jhr 465 val args' = renameList(env, args)
185 :     fun assign rator' = [(y, DstIL.OP(rator', args'))]
186 : jhr 431 in
187 :     case rator
188 : jhr 459 of SrcOp.Add ty => assign (DstOp.Add ty)
189 :     | SrcOp.Sub ty => assign (DstOp.Sub ty)
190 :     | SrcOp.Mul ty => assign (DstOp.Mul ty)
191 :     | SrcOp.Div ty => assign (DstOp.Div ty)
192 :     | SrcOp.Neg ty => assign (DstOp.Neg ty)
193 :     | SrcOp.LT ty => assign (DstOp.LT ty)
194 :     | SrcOp.LTE ty => assign (DstOp.LTE ty)
195 :     | SrcOp.EQ ty => assign (DstOp.EQ ty)
196 :     | SrcOp.NEQ ty => assign (DstOp.NEQ ty)
197 :     | SrcOp.GT ty => assign (DstOp.GT ty)
198 :     | SrcOp.GTE ty => assign (DstOp.GTE ty)
199 :     | SrcOp.Not => assign (DstOp.Not)
200 :     | SrcOp.Max => assign (DstOp.Max)
201 :     | SrcOp.Min => assign (DstOp.Min)
202 :     | SrcOp.Sin => assign (DstOp.Sin)
203 :     | SrcOp.Cos => assign (DstOp.Cos)
204 :     | SrcOp.Pow => assign (DstOp.Pow)
205 :     | SrcOp.Dot d => assign (DstOp.Dot d)
206 :     | SrcOp.Cross => assign (DstOp.Cross)
207 : jhr 460 | SrcOp.Select(ty, i)=> assign (DstOp.Select(ty, i))
208 : jhr 459 | SrcOp.Norm d => assign (DstOp.Norm d)
209 :     | SrcOp.Scale d => assign (DstOp.Scale d)
210 :     | SrcOp.InvScale d => assign (DstOp.InvScale d)
211 :     | SrcOp.CL => assign (DstOp.CL)
212 :     | SrcOp.PrincipleEvec ty => assign (DstOp.PrincipleEvec ty)
213 :     | SrcOp.Subscript ty => assign (DstOp.Subscript ty)
214 :     | SrcOp.Floor d => assign (DstOp.Floor d)
215 :     | SrcOp.IntToReal => assign (DstOp.IntToReal)
216 :     | SrcOp.TruncToInt d => assign (DstOp.TruncToInt d)
217 :     | SrcOp.RoundToInt d => assign (DstOp.RoundToInt d)
218 :     | SrcOp.CeilToInt d => assign (DstOp.CeilToInt d)
219 :     | SrcOp.FloorToInt d => assign (DstOp.FloorToInt d)
220 : jhr 465 | SrcOp.VoxelAddress info => expandVoxelAddress (y, info, args')
221 : jhr 459 | SrcOp.LoadVoxels(rty, d) => assign (DstOp.LoadVoxels(rty, d))
222 : jhr 460 | SrcOp.PosToImgSpace info => assign (DstOp.PosToImgSpace info)
223 :     | SrcOp.GradToWorldSpace info => assign (DstOp.GradToWorldSpace info)
224 : jhr 465 | SrcOp.EvalKernel(d, h, k) => expandEvalKernel(y, d, h, k, args')
225 : jhr 459 | SrcOp.LoadImage info => assign (DstOp.LoadImage info)
226 :     | SrcOp.Inside info => assign (DstOp.Inside info)
227 :     | SrcOp.Input(ty, name) => assign (DstOp.Input(ty, name))
228 :     | SrcOp.InputWithDefault(ty, name) => assign (DstOp.InputWithDefault(ty, name))
229 : jhr 431 (* end case *)
230 :     end
231 :    
232 : jhr 387 (* expand a SrcIL assignment to a list of DstIL assignments *)
233 :     fun expand (env, (y, rhs)) = let
234 :     val y' = rename (env, y)
235 :     fun assign rhs = [(y', rhs)]
236 :     in
237 :     case rhs
238 :     of SrcIL.VAR x => assign (DstIL.VAR(rename(env, x)))
239 :     | SrcIL.LIT lit => assign (DstIL.LIT lit)
240 :     | SrcIL.OP(rator, args) => expandOp (env, y', rator, args)
241 :     | SrcIL.CONS args => assign (DstIL.CONS(renameList(env, args)))
242 :     (* end case *)
243 :     end
244 : lamonts 345
245 : jhr 387 structure Trans = TranslateFn (
246 :     struct
247 :     structure SrcIL = SrcIL
248 :     structure DstIL = DstIL
249 :    
250 :     type var_env = var_env
251 :    
252 :     val rename = rename
253 :     val expand = expand
254 :     end)
255 :    
256 :     fun translate (SrcIL.Program{globals, globalInit, actors}) = let
257 :     val env = VTbl.mkTable (256, Fail "env")
258 :     fun transMethod (SrcIL.Method{name, stateIn, stateOut, body}) =
259 :     DstIL.Method{
260 :     name = name,
261 :     stateIn = renameList (env, stateIn),
262 :     stateOut = renameList (env, stateOut),
263 :     body = Trans.translate (env, body)
264 :     }
265 :     fun transActor (SrcIL.Actor{name, params, state, stateInit, methods}) =
266 :     DstIL.Actor{
267 :     name = name,
268 :     params = renameList (env, params),
269 :     state = renameList (env, state),
270 :     stateInit = Trans.translate (env, stateInit),
271 :     methods = List.map transMethod methods
272 :     }
273 :     in
274 :     DstIL.Program{
275 :     globals = renameList (env, globals),
276 :     globalInit = Trans.translate (env, globalInit),
277 :     actors = List.map transActor actors
278 :     }
279 :     end
280 :    
281 : jhr 435 end

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