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

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