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

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