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

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/simplify/simplify.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/simplify/simplify.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2135 - (view) (download)

1 : jhr 171 (* simplify.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 171 * All rights reserved.
5 :     *
6 :     * Simplify the AST representation.
7 :     *)
8 :    
9 :     structure Simplify : sig
10 :    
11 : jhr 1140 val transform : Error.err_stream * AST.program -> Simple.program
12 : jhr 171
13 :     end = struct
14 :    
15 :     structure Ty = Types
16 :     structure S = Simple
17 : jhr 2013 structure InP = Inputs
18 : jhr 171
19 :     local
20 :     val tempName = Atom.atom "_t"
21 :     in
22 :     fun newTemp ty = Var.new (tempName, AST.LocalVar, ty)
23 :     end
24 :    
25 :     (* make a block out of a list of statements that are in reverse order *)
26 : jhr 197 fun mkBlock stms = S.Block(List.rev stms)
27 : jhr 171
28 : jhr 2012 (* convert an AST expression to an input initialization. Note that the Diderot grammar
29 :     * limits the forms of expression that we might encounter in this context.
30 :     *)
31 : jhr 2059 fun expToInit (ty, exp) = (case exp
32 : jhr 2013 of AST.E_Lit(Literal.Int n) => InP.Int n
33 :     | AST.E_Lit(Literal.Float f) => InP.Real f
34 :     | AST.E_Lit(Literal.String s) => InP.String s
35 :     | AST.E_Lit(Literal.Bool b) => InP.Bool b
36 : jhr 2011 | AST.E_Tuple es => raise Fail "E_Tuple not yet implemented"
37 :     | AST.E_Cons es => let
38 : jhr 2059 val shp = (case ty
39 :     of Ty.T_Tensor(Ty.Shape shp) => List.map (fn (Ty.DimConst d) => d) shp
40 :     | _ => raise Fail "not tensor type"
41 : jhr 2011 (* end case *))
42 : jhr 2059 fun flatten (AST.E_Lit(Literal.Int n), l) = FloatLit.fromInt n :: l
43 :     | flatten (AST.E_Lit(Literal.Float f), l) = f :: l
44 :     | flatten (AST.E_Coerce{e, ...}, l) = flatten(e, l)
45 :     | flatten (AST.E_Cons es, l) = flattenList (es, l)
46 :     | flatten _ = raise Fail "impossible"
47 :     and flattenList ([], l) = l
48 :     | flattenList (x::xs, l) = flatten(x, flattenList(xs, l))
49 : jhr 2011 in
50 : jhr 2059 InP.Tensor(shp, Vector.fromList(flattenList (es, [])))
51 : jhr 2011 end
52 : jhr 2059 (*
53 : jhr 2011 | AST.E_Seq es => ??
54 :     | AST.E_Coerce{srcTy, dstTy, e} => ??
55 : jhr 2014 *)
56 : jhr 2011 | _ => raise Fail "impossible initialization expression"
57 :     (* end case *))
58 :    
59 : jhr 2013 fun inputImage (nrrd, dim, shape) = let
60 :     val dim = TypeUtil.monoDim dim
61 :     val shp = TypeUtil.monoShape shape
62 :     in
63 :     case ImageInfo.fromNrrd(NrrdInfo.getInfo nrrd, dim, shp)
64 :     of NONE => raise Fail(concat["nrrd file \"", nrrd, "\" does not have expected type"])
65 :     | SOME info => InP.Proxy(nrrd, info)
66 :     (* end case *)
67 :     end
68 :    
69 : jhr 227 fun simplifyProgram (AST.Program dcls) = let
70 : jhr 2011 val inputs = ref []
71 : jhr 171 val globals = ref []
72 :     val globalInit = ref []
73 : jhr 2135 val funcs = ref []
74 : jhr 1116 val initially = ref NONE
75 : jhr 511 val strands = ref []
76 : jhr 1116 fun setInitially init = (case !initially
77 :     of NONE => initially := SOME init
78 :     (* FIXME: the check for multiple initially decls should happen in type checking *)
79 :     | SOME _ => raise Fail "multiple initially declarations"
80 :     (* end case *))
81 : jhr 171 fun simplifyDecl dcl = (case dcl
82 : jhr 1301 of AST.D_Input(x, desc, NONE) => let
83 : jhr 2011 val (ty, init) = (case Var.monoTypeOf x
84 : jhr 2013 of ty as Ty.T_Image{dim, shape} => let
85 : jhr 2014 val info = ImageInfo.mkInfo(TypeUtil.monoDim dim, TypeUtil.monoShape shape)
86 : jhr 2013 in
87 :     (ty, SOME(InP.Image info))
88 :     end
89 : jhr 2011 | ty => (ty, NONE)
90 : jhr 1996 (* end case *))
91 : jhr 2013 val inp = InP.INP{
92 : jhr 2011 ty = ty,
93 :     name = Var.nameOf x,
94 :     desc = desc,
95 :     init = init
96 :     }
97 : jhr 185 in
98 : jhr 2011 inputs := (x, inp) :: !inputs
99 : jhr 185 end
100 : jhr 1992 | AST.D_Input(x, desc, SOME(AST.E_LoadNrrd(tvs, nrrd, ty))) => let
101 : jhr 1996 (* load the nrrd proxy here *)
102 : jhr 1993 val info = NrrdInfo.getInfo nrrd
103 : jhr 2011 val (ty, init) = (case Var.monoTypeOf x
104 : jhr 2013 of ty as Ty.T_DynSequence _ => (ty, InP.DynSeq nrrd)
105 :     | ty as Ty.T_Image{dim, shape} => (ty, inputImage(nrrd, dim, shape))
106 : jhr 1992 | _ => raise Fail "impossible"
107 :     (* end case *))
108 : jhr 2013 val inp = InP.INP{
109 : jhr 2011 ty = ty,
110 :     name = Var.nameOf x,
111 :     desc = desc,
112 :     init = SOME init
113 :     }
114 : jhr 1992 in
115 : jhr 2011 inputs := (x, inp) :: !inputs
116 : jhr 1992 end
117 : jhr 1301 | AST.D_Input(x, desc, SOME e) => let
118 : jhr 2059 val ty = Var.monoTypeOf x
119 : jhr 2013 val inp = InP.INP{
120 : jhr 2059 ty = ty,
121 : jhr 2011 name = Var.nameOf x,
122 :     desc = desc,
123 : jhr 2059 init = SOME(expToInit(ty, e))
124 : jhr 2011 }
125 : jhr 171 in
126 : jhr 2011 inputs := (x, inp) :: !inputs
127 : jhr 171 end
128 :     | AST.D_Var(AST.VD_Decl(x, e)) => let
129 :     val (stms, e') = simplifyExp (e, [])
130 :     in
131 :     globals := x :: !globals;
132 :     globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
133 :     end
134 : jhr 2135 | AST.D_Func(f, params, body) =>
135 :     funcs := S.Func{f=f, params=params, body=simplifyBlock body} :: !funcs
136 : jhr 511 | AST.D_Strand info => strands := simplifyStrand info :: !strands
137 : jhr 1116 | AST.D_InitialArray(creat, iters) =>
138 :     setInitially (simplifyInit(true, creat, iters))
139 :     | AST.D_InitialCollection(creat, iters) =>
140 :     setInitially (simplifyInit(false, creat, iters))
141 : jhr 171 (* end case *))
142 :     in
143 :     List.app simplifyDecl dcls;
144 : jhr 175 S.Program{
145 : jhr 2011 inputs = List.rev(!inputs),
146 : jhr 171 globals = List.rev(!globals),
147 :     globalInit = mkBlock (!globalInit),
148 : jhr 2135 funcs = List.rev(!funcs),
149 : jhr 1116 init = (case !initially
150 :     (* FIXME: the check for the initially block should really happen in typechecking *)
151 :     of NONE => raise Fail "missing initially declaration"
152 :     | SOME blk => blk
153 :     (* end case *)),
154 : jhr 511 strands = List.rev(!strands)
155 : jhr 171 }
156 :     end
157 :    
158 : jhr 1116 and simplifyInit (isArray, AST.C_Create(strand, exps), iters) = let
159 :     val (stms, xs) = simplifyExpsToVars (exps, [])
160 :     val creat = S.C_Create{
161 :     argInit = mkBlock stms,
162 :     name = strand,
163 :     args = xs
164 :     }
165 :     fun simplifyIter (AST.I_Range(x, e1, e2), (iters, stms)) = let
166 :     val (stms, lo) = simplifyExpToVar (e1, stms)
167 :     val (stms, hi) = simplifyExpToVar (e2, stms)
168 :     in
169 :     ({param=x, lo=lo, hi=hi}::iters, stms)
170 :     end
171 :     val (iters, stms) = List.foldl simplifyIter ([], []) iters
172 :     in
173 :     S.Initially{
174 :     isArray = isArray,
175 :     rangeInit = mkBlock stms,
176 :     iters = List.rev iters,
177 :     create = creat
178 :     }
179 :     end
180 :    
181 : jhr 511 and simplifyStrand {name, params, state, methods} = let
182 : jhr 171 fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
183 :     | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let
184 :     val (stms, e') = simplifyExp (e, stms)
185 :     in
186 :     simplifyState (r, x::xs, S.S_Assign(x, e') :: stms)
187 :     end
188 :     val (xs, stm) = simplifyState (state, [], [])
189 :     in
190 : jhr 511 S.Strand{
191 : jhr 173 name = name,
192 : jhr 171 params = params,
193 :     state = xs, stateInit = stm,
194 :     methods = List.map simplifyMethod methods
195 :     }
196 :     end
197 :    
198 :     and simplifyMethod (AST.M_Method(name, body)) =
199 : jhr 197 S.Method(name, simplifyBlock body)
200 : jhr 171
201 : jhr 1116 (* simplify a statement into a single statement (i.e., a block if it expands
202 :     * into more than one new statement).
203 : jhr 171 *)
204 :     and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))
205 :    
206 :     and simplifyStmt (stm, stms) = (case stm
207 :     of AST.S_Block body => let
208 :     fun simplify ([], stms) = stms
209 :     | simplify (stm::r, stms) = simplify (r, simplifyStmt (stm, stms))
210 :     in
211 :     simplify (body, stms)
212 :     end
213 :     | AST.S_Decl(AST.VD_Decl(x, e)) => let
214 :     val (stms, e') = simplifyExp (e, stms)
215 :     in
216 :     S.S_Assign(x, e') :: stms
217 :     end
218 :     | AST.S_IfThenElse(e, s1, s2) => let
219 :     val (stms, x) = simplifyExpToVar (e, stms)
220 :     val s1 = simplifyBlock s1
221 :     val s2 = simplifyBlock s2
222 :     in
223 :     S.S_IfThenElse(x, s1, s2) :: stms
224 :     end
225 :     | AST.S_Assign(x, e) => let
226 :     val (stms, e') = simplifyExp (e, stms)
227 :     in
228 :     S.S_Assign(x, e') :: stms
229 :     end
230 :     | AST.S_New(name, args) => let
231 :     val (stms, xs) = simplifyExpsToVars (args, stms)
232 :     in
233 :     S.S_New(name, xs) :: stms
234 :     end
235 :     | AST.S_Die => S.S_Die :: stms
236 :     | AST.S_Stabilize => S.S_Stabilize :: stms
237 : jhr 2135 | AST.S_Return e => let
238 :     val (stms, x) = simplifyExpToVar (e, stms)
239 :     in
240 :     S.S_Return x :: stms
241 :     end
242 : jhr 1640 | AST.S_Print args => let
243 :     val (stms, xs) = simplifyExpsToVars (args, stms)
244 :     in
245 :     S.S_Print xs :: stms
246 :     end
247 : jhr 171 (* end case *))
248 :    
249 :     and simplifyExp (exp, stms) = (
250 :     case exp
251 : jhr 197 of AST.E_Var x => (case Var.kindOf x
252 :     of Var.BasisVar => let
253 :     val ty = Var.monoTypeOf x
254 :     val x' = newTemp ty
255 :     val stm = S.S_Assign(x', S.E_Apply(x, [], [], ty))
256 :     in
257 :     (stm::stms, S.E_Var x')
258 :     end
259 :     | _ => (stms, S.E_Var x)
260 :     (* end case *))
261 : jhr 171 | AST.E_Lit lit => (stms, S.E_Lit lit)
262 :     | AST.E_Tuple es => raise Fail "E_Tuple not yet implemented"
263 :     | AST.E_Apply(f, tyArgs, args, ty) => let
264 :     val (stms, xs) = simplifyExpsToVars (args, stms)
265 :     in
266 :     (stms, S.E_Apply(f, tyArgs, xs, ty))
267 :     end
268 :     | AST.E_Cons es => let
269 :     val (stms, xs) = simplifyExpsToVars (es, stms)
270 :     in
271 :     (stms, S.E_Cons xs)
272 :     end
273 : jhr 1688 | AST.E_Seq es => let
274 :     val (stms, xs) = simplifyExpsToVars (es, stms)
275 :     in
276 :     (stms, S.E_Seq xs)
277 :     end
278 : jhr 399 | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
279 : jhr 395 val (stms, x) = simplifyExpToVar (e, stms)
280 : jhr 399 fun f ([], ys, stms) = (stms, List.rev ys)
281 :     | f (NONE::es, ys, stms) = f (es, NONE::ys, stms)
282 :     | f (SOME e::es, ys, stms) = let
283 :     val (stms, y) = simplifyExpToVar (e, stms)
284 :     in
285 :     f (es, SOME y::ys, stms)
286 :     end
287 :     val (stms, indices) = f (indices, [], stms)
288 : jhr 395 in
289 : jhr 399 (stms, S.E_Slice(x, indices, ty))
290 : jhr 395 end
291 : jhr 416 | AST.E_Cond(e1, e2, e3, ty) => let
292 : jhr 171 (* a conditional expression gets turned into an if-then-else statememt *)
293 : jhr 416 val result = newTemp ty
294 : jhr 1116 val (stms, x) = simplifyExpToVar (e1, S.S_Var result :: stms)
295 : jhr 171 fun simplifyBranch e = let
296 :     val (stms, e) = simplifyExp (e, [])
297 :     in
298 :     mkBlock (S.S_Assign(result, e)::stms)
299 :     end
300 : jhr 415 val s1 = simplifyBranch e2
301 :     val s2 = simplifyBranch e3
302 : jhr 171 in
303 :     (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
304 :     end
305 : jhr 2026 | AST.E_LoadNrrd(_, nrrd, ty) => (case TypeUtil.prune ty
306 :     of ty as Ty.T_DynSequence _ => (stms, S.E_LoadSeq(ty, nrrd))
307 :     | ty as Ty.T_Image{dim, shape} => let
308 :     val dim = TypeUtil.monoDim dim
309 :     val shp = TypeUtil.monoShape shape
310 :     in
311 :     case ImageInfo.fromNrrd(NrrdInfo.getInfo nrrd, dim, shp)
312 :     of NONE => raise Fail(concat[
313 :     "nrrd file \"", nrrd, "\" does not have expected type"
314 :     ])
315 :     | SOME info => (stms, S.E_LoadImage(ty, nrrd, info))
316 :     (* end case *)
317 :     end
318 :     | _ => raise Fail "bogus type for E_LoadNrrd"
319 :     (* end case *))
320 : jhr 1687 | AST.E_Coerce{srcTy, dstTy, e} => let
321 :     val (stms, x) = simplifyExpToVar (e, stms)
322 :     val result = newTemp dstTy
323 :     in
324 :     (S.S_Assign(result, S.E_Coerce{srcTy=srcTy, dstTy=dstTy, x=x})::stms, S.E_Var result)
325 :     end
326 : jhr 171 (* end case *))
327 :    
328 :     and simplifyExpToVar (exp, stms) = let
329 :     val (stms, e) = simplifyExp (exp, stms)
330 :     in
331 :     case e
332 :     of S.E_Var x => (stms, x)
333 :     | _ => let
334 :     val x = newTemp (S.typeOf e)
335 :     in
336 :     (S.S_Assign(x, e)::stms, x)
337 :     end
338 :     (* end case *)
339 :     end
340 :    
341 :     and simplifyExpsToVars (exps, stms) = let
342 :     fun f ([], xs, stms) = (stms, List.rev xs)
343 :     | f (e::es, xs, stms) = let
344 :     val (stms, x) = simplifyExpToVar (e, stms)
345 :     in
346 :     f (es, x::xs, stms)
347 :     end
348 :     in
349 :     f (exps, [], stms)
350 :     end
351 :    
352 : jhr 1140 fun transform (errStrm, ast) = let
353 : jhr 227 val simple = simplifyProgram ast
354 : jhr 238 val _ = SimplePP.output (Log.logFile(), simple) (* DEBUG *)
355 : jhr 1993 (*
356 : jhr 227 val simple = Lift.transform simple
357 : jhr 1140 handle Eval.Error msg => (Error.error(errStrm, msg); simple)
358 : jhr 1993 *)
359 : jhr 227 in
360 :     simple
361 :     end
362 :    
363 : jhr 171 end

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