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

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