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 2272 - (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 2272 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 :     | AST.E_Tuple es => raise Fail "E_Tuple not yet implemented"
37 :     | AST.E_Cons es => let
38 :     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 :     (* end case *))
42 :     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 :     in
50 :     InP.Tensor(shp, Vector.fromList(flattenList (es, [])))
51 :     end
52 : jhr 2059 (*
53 : jhr 2272 | AST.E_Seq es => ??
54 :     | AST.E_Coerce{srcTy, dstTy, e} => ??
55 : jhr 2014 *)
56 : jhr 2272 | _ => raise Fail "impossible initialization expression"
57 :     (* end case *))
58 : jhr 2011
59 : jhr 2013 fun inputImage (nrrd, dim, shape) = let
60 : jhr 2272 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 : jhr 2013
69 : jhr 2140 (* is the given statement's continuation the syntactically following statement? *)
70 :     fun contIsNext (AST.S_Block stms) = List.all contIsNext stms
71 : jhr 2156 | contIsNext (AST.S_IfThenElse(_, s1, s2)) = contIsNext s1 orelse contIsNext s2
72 : jhr 2140 | contIsNext AST.S_Die = false
73 :     | contIsNext AST.S_Stabilize = false
74 :     | contIsNext (AST.S_Return _) = false
75 :     | contIsNext _ = true
76 :    
77 : jhr 227 fun simplifyProgram (AST.Program dcls) = let
78 : jhr 2272 val inputs = ref []
79 :     val globals = ref []
80 :     val globalInit = ref []
81 :     val funcs = ref []
82 :     val initially = ref NONE
83 :     val strands = ref []
84 :     fun setInitially init = (case !initially
85 :     of NONE => initially := SOME init
86 : jhr 1116 (* FIXME: the check for multiple initially decls should happen in type checking *)
87 : jhr 2272 | SOME _ => raise Fail "multiple initially declarations"
88 :     (* end case *))
89 :     fun simplifyDecl dcl = (case dcl
90 :     of AST.D_Input(x, desc, NONE) => let
91 : jhr 2011 val (ty, init) = (case Var.monoTypeOf x
92 : jhr 2013 of ty as Ty.T_Image{dim, shape} => let
93 : jhr 2272 val info = ImageInfo.mkInfo(TypeUtil.monoDim dim, TypeUtil.monoShape shape)
94 :     in
95 :     (ty, SOME(InP.Image info))
96 :     end
97 : jhr 2011 | ty => (ty, NONE)
98 : jhr 1996 (* end case *))
99 : jhr 2272 val inp = InP.INP{
100 :     ty = ty,
101 :     name = Var.nameOf x,
102 :     desc = desc,
103 :     init = init
104 :     }
105 :     in
106 :     inputs := (x, inp) :: !inputs
107 :     end
108 : jhr 1992 | AST.D_Input(x, desc, SOME(AST.E_LoadNrrd(tvs, nrrd, ty))) => let
109 : jhr 1996 (* load the nrrd proxy here *)
110 : jhr 1993 val info = NrrdInfo.getInfo nrrd
111 : jhr 2011 val (ty, init) = (case Var.monoTypeOf x
112 : jhr 2013 of ty as Ty.T_DynSequence _ => (ty, InP.DynSeq nrrd)
113 :     | ty as Ty.T_Image{dim, shape} => (ty, inputImage(nrrd, dim, shape))
114 : jhr 1992 | _ => raise Fail "impossible"
115 :     (* end case *))
116 : jhr 2272 val inp = InP.INP{
117 :     ty = ty,
118 :     name = Var.nameOf x,
119 :     desc = desc,
120 :     init = SOME init
121 :     }
122 : jhr 1992 in
123 : jhr 2272 inputs := (x, inp) :: !inputs
124 : jhr 1992 end
125 : jhr 2272 | AST.D_Input(x, desc, SOME e) => let
126 :     val ty = Var.monoTypeOf x
127 :     val inp = InP.INP{
128 :     ty = ty,
129 :     name = Var.nameOf x,
130 :     desc = desc,
131 :     init = SOME(expToInit(ty, e))
132 :     }
133 :     in
134 :     inputs := (x, inp) :: !inputs
135 :     end
136 :     | AST.D_Var(AST.VD_Decl(x, e)) => let
137 :     val (stms, e') = simplifyExp (e, [])
138 :     in
139 :     globals := x :: !globals;
140 :     globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
141 :     end
142 :     | AST.D_Func(f, params, body) =>
143 :     funcs := S.Func{f=f, params=params, body=simplifyBlock body} :: !funcs
144 :     | AST.D_Strand info => strands := simplifyStrand info :: !strands
145 :     | AST.D_InitialArray(creat, iters) =>
146 :     setInitially (simplifyInit(true, creat, iters))
147 :     | AST.D_InitialCollection(creat, iters) =>
148 :     setInitially (simplifyInit(false, creat, iters))
149 :     (* end case *))
150 :     in
151 :     List.app simplifyDecl dcls;
152 :     S.Program{
153 :     inputs = List.rev(!inputs),
154 :     globals = List.rev(!globals),
155 :     globalInit = mkBlock (!globalInit),
156 :     funcs = List.rev(!funcs),
157 :     init = (case !initially
158 : jhr 1116 (* FIXME: the check for the initially block should really happen in typechecking *)
159 : jhr 2272 of NONE => raise Fail "missing initially declaration"
160 :     | SOME blk => blk
161 :     (* end case *)),
162 :     strands = List.rev(!strands)
163 :     }
164 :     end
165 : jhr 171
166 : jhr 1116 and simplifyInit (isArray, AST.C_Create(strand, exps), iters) = let
167 : jhr 2272 val (stms, xs) = simplifyExpsToVars (exps, [])
168 :     val creat = S.C_Create{
169 :     argInit = mkBlock stms,
170 :     name = strand,
171 :     args = xs
172 :     }
173 :     fun simplifyIter (AST.I_Range(x, e1, e2), (iters, stms)) = let
174 :     val (stms, lo) = simplifyExpToVar (e1, stms)
175 :     val (stms, hi) = simplifyExpToVar (e2, stms)
176 :     in
177 :     ({param=x, lo=lo, hi=hi}::iters, stms)
178 :     end
179 :     val (iters, stms) = List.foldl simplifyIter ([], []) iters
180 :     in
181 :     S.Initially{
182 :     isArray = isArray,
183 :     rangeInit = mkBlock stms,
184 :     iters = List.rev iters,
185 :     create = creat
186 :     }
187 :     end
188 : jhr 1116
189 : jhr 2211 and simplifyStrand (AST.Strand{name, params, state, methods}) = let
190 : jhr 2272 fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
191 :     | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let
192 :     val (stms, e') = simplifyExp (e, stms)
193 :     in
194 :     simplifyState (r, x::xs, S.S_Assign(x, e') :: stms)
195 :     end
196 :     val (xs, stm) = simplifyState (state, [], [])
197 :     in
198 :     S.Strand{
199 :     name = name,
200 :     params = params,
201 :     state = xs, stateInit = stm,
202 :     methods = List.map simplifyMethod methods
203 :     }
204 :     end
205 : jhr 171
206 :     and simplifyMethod (AST.M_Method(name, body)) =
207 : jhr 2272 S.Method(name, simplifyBlock body)
208 : jhr 171
209 : jhr 1116 (* simplify a statement into a single statement (i.e., a block if it expands
210 :     * into more than one new statement).
211 : jhr 171 *)
212 :     and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))
213 :    
214 : jhr 2147 (* simplify the statement stm where stms is a reverse-order list of preceeding simplified
215 :     * statements. This function returns a reverse-order list of simplified statements.
216 : jhr 2154 * Note that error reporting is done in the typechecker, but it does not prune unreachable
217 :     * code.
218 : jhr 2147 *)
219 : jhr 171 and simplifyStmt (stm, stms) = (case stm
220 : jhr 2272 of AST.S_Block body => let
221 :     fun simplify ([], stms) = stms
222 :     | simplify (stm::r, stms) = if contIsNext stm
223 :     then simplify (r, simplifyStmt (stm, stms))
224 :     else simplifyStmt (stm, stms) (* prune unreachable statements *)
225 :     in
226 :     simplify (body, stms)
227 :     end
228 :     | AST.S_Decl(AST.VD_Decl(x, e)) => let
229 :     val (stms, e') = simplifyExp (e, stms)
230 :     in
231 :     S.S_Assign(x, e') :: stms
232 :     end
233 :     | AST.S_IfThenElse(e, s1, s2) => let
234 :     val (stms, x) = simplifyExpToVar (e, stms)
235 :     val s1 = simplifyBlock s1
236 :     val s2 = simplifyBlock s2
237 :     in
238 :     S.S_IfThenElse(x, s1, s2) :: stms
239 :     end
240 :     | AST.S_Assign(x, e) => let
241 :     val (stms, e') = simplifyExp (e, stms)
242 :     in
243 :     S.S_Assign(x, e') :: stms
244 :     end
245 :     | AST.S_New(name, args) => let
246 :     val (stms, xs) = simplifyExpsToVars (args, stms)
247 :     in
248 :     S.S_New(name, xs) :: stms
249 :     end
250 :     | AST.S_Die => S.S_Die :: stms
251 :     | AST.S_Stabilize => S.S_Stabilize :: stms
252 :     | AST.S_Return e => let
253 :     val (stms, x) = simplifyExpToVar (e, stms)
254 :     in
255 :     S.S_Return x :: stms
256 :     end
257 : jhr 1640 | AST.S_Print args => let
258 : jhr 2272 val (stms, xs) = simplifyExpsToVars (args, stms)
259 : jhr 1640 in
260 :     S.S_Print xs :: stms
261 :     end
262 : jhr 2272 (* end case *))
263 : jhr 171
264 :     and simplifyExp (exp, stms) = (
265 : jhr 2272 case exp
266 :     of AST.E_Var x => (case Var.kindOf x
267 :     of Var.BasisVar => let
268 :     val ty = Var.monoTypeOf x
269 :     val x' = newTemp ty
270 :     val stm = S.S_Assign(x', S.E_Apply(x, [], [], ty))
271 :     in
272 :     (stm::stms, S.E_Var x')
273 :     end
274 :     | _ => (stms, S.E_Var x)
275 :     (* end case *))
276 :     | AST.E_Lit lit => (stms, S.E_Lit lit)
277 :     | AST.E_Tuple es => raise Fail "E_Tuple not yet implemented"
278 :     | AST.E_Apply(f, tyArgs, args, ty) => let
279 :     val (stms, xs) = simplifyExpsToVars (args, stms)
280 :     in
281 :     (stms, S.E_Apply(f, tyArgs, xs, ty))
282 :     end
283 :     | AST.E_Cons es => let
284 :     val (stms, xs) = simplifyExpsToVars (es, stms)
285 :     in
286 :     (stms, S.E_Cons xs)
287 :     end
288 :     | AST.E_Seq es => let
289 :     val (stms, xs) = simplifyExpsToVars (es, stms)
290 :     in
291 :     (stms, S.E_Seq xs)
292 :     end
293 :     | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
294 :     val (stms, x) = simplifyExpToVar (e, stms)
295 :     fun f ([], ys, stms) = (stms, List.rev ys)
296 :     | f (NONE::es, ys, stms) = f (es, NONE::ys, stms)
297 :     | f (SOME e::es, ys, stms) = let
298 :     val (stms, y) = simplifyExpToVar (e, stms)
299 :     in
300 :     f (es, SOME y::ys, stms)
301 :     end
302 :     val (stms, indices) = f (indices, [], stms)
303 :     in
304 :     (stms, S.E_Slice(x, indices, ty))
305 :     end
306 :     | AST.E_Cond(e1, e2, e3, ty) => let
307 :     (* a conditional expression gets turned into an if-then-else statememt *)
308 :     val result = newTemp ty
309 :     val (stms, x) = simplifyExpToVar (e1, S.S_Var result :: stms)
310 :     fun simplifyBranch e = let
311 :     val (stms, e) = simplifyExp (e, [])
312 :     in
313 :     mkBlock (S.S_Assign(result, e)::stms)
314 :     end
315 :     val s1 = simplifyBranch e2
316 :     val s2 = simplifyBranch e3
317 :     in
318 :     (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
319 :     end
320 : jhr 2026 | AST.E_LoadNrrd(_, nrrd, ty) => (case TypeUtil.prune ty
321 : jhr 2272 of ty as Ty.T_DynSequence _ => (stms, S.E_LoadSeq(ty, nrrd))
322 :     | ty as Ty.T_Image{dim, shape} => let
323 :     val dim = TypeUtil.monoDim dim
324 :     val shp = TypeUtil.monoShape shape
325 :     in
326 :     case ImageInfo.fromNrrd(NrrdInfo.getInfo nrrd, dim, shp)
327 :     of NONE => raise Fail(concat[
328 :     "nrrd file \"", nrrd, "\" does not have expected type"
329 :     ])
330 :     | SOME info => (stms, S.E_LoadImage(ty, nrrd, info))
331 :     (* end case *)
332 :     end
333 :     | _ => raise Fail "bogus type for E_LoadNrrd"
334 :     (* end case *))
335 : jhr 1687 | AST.E_Coerce{srcTy, dstTy, e} => let
336 :     val (stms, x) = simplifyExpToVar (e, stms)
337 : jhr 2272 val result = newTemp dstTy
338 : jhr 1687 in
339 :     (S.S_Assign(result, S.E_Coerce{srcTy=srcTy, dstTy=dstTy, x=x})::stms, S.E_Var result)
340 :     end
341 : jhr 2272 (* end case *))
342 : jhr 171
343 :     and simplifyExpToVar (exp, stms) = let
344 : jhr 2272 val (stms, e) = simplifyExp (exp, stms)
345 :     in
346 :     case e
347 :     of S.E_Var x => (stms, x)
348 :     | _ => let
349 :     val x = newTemp (S.typeOf e)
350 :     in
351 :     (S.S_Assign(x, e)::stms, x)
352 :     end
353 :     (* end case *)
354 :     end
355 : jhr 171
356 :     and simplifyExpsToVars (exps, stms) = let
357 : jhr 2272 fun f ([], xs, stms) = (stms, List.rev xs)
358 :     | f (e::es, xs, stms) = let
359 :     val (stms, x) = simplifyExpToVar (e, stms)
360 :     in
361 :     f (es, x::xs, stms)
362 :     end
363 :     in
364 :     f (exps, [], stms)
365 :     end
366 : jhr 171
367 : jhr 1140 fun transform (errStrm, ast) = let
368 : jhr 2272 val simple = simplifyProgram ast
369 :     val _ = SimplePP.output (Log.logFile(), "simplify", simple) (* DEBUG *)
370 :     val simple = Inliner.transform simple
371 :     val _ = SimplePP.output (Log.logFile(), "inlining", simple) (* DEBUG *)
372 : jhr 1993 (*
373 : jhr 2272 val simple = Lift.transform simple
374 :     handle Eval.Error msg => (Error.error(errStrm, msg); simple)
375 : jhr 1993 *)
376 : jhr 2272 in
377 :     simple
378 :     end
379 : jhr 227
380 : jhr 171 end

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