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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4359 - (view) (download)

1 : jhr 3437 (* simplify.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 :     *
8 : jhr 3468 * Simplify the AST representation. This phase involves the following transformations:
9 :     *
10 : jhr 4317 * - types are simplified by removing meta variables (which will have been resolved)
11 : jhr 3468 *
12 : jhr 4317 * - expressions are simplified to involve a single operation on variables
13 : jhr 3468 *
14 : jhr 4317 * - global reductions are converted to MapReduce statements
15 : jhr 3468 *
16 : jhr 4317 * - other comprehensions and reductions are converted to foreach loops
17 : jhr 3468 *
18 : jhr 4317 * - unreachable code is pruned
19 : jhr 3468 *
20 : jhr 4317 * - negation of literal integers and reals are constant folded
21 : jhr 3437 *)
22 :    
23 :     structure Simplify : sig
24 :    
25 :     val transform : Error.err_stream * AST.program -> Simple.program
26 :    
27 :     end = struct
28 :    
29 :     structure TU = TypeUtil
30 :     structure S = Simple
31 : jhr 3445 structure STy = SimpleTypes
32 :     structure Ty = Types
33 : jhr 3437 structure VMap = Var.Map
34 : jhr 4193 structure II = ImageInfo
35 : jhr 4359 structure BV = BasisVars
36 : jhr 3437
37 : jhr 3445 (* convert a Types.ty to a SimpleTypes.ty *)
38 :     fun cvtTy ty = (case ty
39 :     of Ty.T_Var(Ty.TV{bind, ...}) => (case !bind
40 :     of NONE => raise Fail "unresolved type variable"
41 :     | SOME ty => cvtTy ty
42 :     (* end case *))
43 :     | Ty.T_Bool => STy.T_Bool
44 :     | Ty.T_Int => STy.T_Int
45 :     | Ty.T_String => STy.T_String
46 :     | Ty.T_Sequence(ty, NONE) => STy.T_Sequence(cvtTy ty, NONE)
47 : jhr 3452 | Ty.T_Sequence(ty, SOME dim) => STy.T_Sequence(cvtTy ty, SOME(TU.monoDim dim))
48 : jhr 4317 | Ty.T_Strand id => STy.T_Strand id
49 : jhr 4207 | Ty.T_Kernel _ => STy.T_Kernel
50 : jhr 3445 | Ty.T_Tensor shape => STy.T_Tensor(TU.monoShape shape)
51 : jhr 4193 | Ty.T_Image{dim, shape} =>
52 : jhr 4317 STy.T_Image(II.mkInfo(TU.monoDim dim, TU.monoShape shape))
53 : jhr 3445 | Ty.T_Field{diff, dim, shape} => STy.T_Field{
54 :     diff = TU.monoDiff diff,
55 :     dim = TU.monoDim dim,
56 :     shape = TU.monoShape shape
57 :     }
58 : jhr 4163 | Ty.T_Fun(tys1, ty2) => raise Fail "unexpected T_Fun in Simplify"
59 : jhr 4317 | Ty.T_Error => raise Fail "unexpected T_Error in Simplify"
60 : jhr 3445 (* end case *))
61 : jhr 3437
62 : jhr 3811 fun apiTypeOf x = let
63 : jhr 4317 fun cvtTy STy.T_Bool = APITypes.BoolTy
64 :     | cvtTy STy.T_Int = APITypes.IntTy
65 :     | cvtTy STy.T_String = APITypes.StringTy
66 :     | cvtTy (STy.T_Sequence(ty, len)) = APITypes.SeqTy(cvtTy ty, len)
67 :     | cvtTy (STy.T_Tensor shape) = APITypes.TensorTy shape
68 :     | cvtTy (STy.T_Image info) =
69 :     APITypes.ImageTy(II.dim info, II.voxelShape info)
70 :     | cvtTy ty = raise Fail "bogus API type"
71 :     in
72 :     cvtTy (SimpleVar.typeOf x)
73 :     end
74 : jhr 3811
75 : jhr 4117 fun newTemp (ty as STy.T_Image _) = SimpleVar.new ("img", SimpleVar.LocalVar, ty)
76 :     | newTemp ty = SimpleVar.new ("_t", SimpleVar.LocalVar, ty)
77 : jhr 3437
78 : jhr 4163 (* a property to map AST function variables to SimpleAST functions *)
79 :     local
80 :     fun cvt x = let
81 : jhr 4317 val Ty.T_Fun(paramTys, resTy) = Var.monoTypeOf x
82 :     in
83 :     SimpleFunc.new (Var.nameOf x, cvtTy resTy, List.map cvtTy paramTys)
84 :     end
85 : jhr 4163 in
86 :     val {getFn = cvtFunc, ...} = Var.newProp cvt
87 :     end
88 :    
89 : jhr 3456 (* a property to map AST variables to SimpleAST variables *)
90 :     local
91 :     fun cvt x = SimpleVar.new (Var.nameOf x, Var.kindOf x, cvtTy(Var.monoTypeOf x))
92 : jhr 4193 val {getFn, setFn, ...} = Var.newProp cvt
93 : jhr 3456 in
94 : jhr 4193 val cvtVar = getFn
95 :     fun newVarWithType (x, ty) = let
96 : jhr 4317 val x' = SimpleVar.new (Var.nameOf x, Var.kindOf x, ty)
97 :     in
98 :     setFn (x, x');
99 :     x'
100 :     end
101 : jhr 3456 end
102 : jhr 3452
103 : jhr 3456 fun cvtVars xs = List.map cvtVar xs
104 : jhr 3452
105 : jhr 3437 (* make a block out of a list of statements that are in reverse order *)
106 : jhr 3501 fun mkBlock stms = S.Block{props = PropList.newHolder(), code = List.rev stms}
107 : jhr 3437
108 :     (* simplify a statement into a single statement (i.e., a block if it expands
109 :     * into more than one new statement).
110 :     *)
111 : jhr 4113 fun simplifyBlock (errStrm, stm) = mkBlock (simplifyStmt (errStrm, stm, []))
112 : jhr 3437
113 : jhr 4193 (* convert the lhs variable of a var decl or assignment; if the rhs is a LoadImage,
114 :     * then we use the info from the proxy image to determine the type of the lhs
115 :     * variable.
116 :     *)
117 :     and cvtLHS (lhs, S.E_LoadImage(_, _, info)) = newVarWithType(lhs, STy.T_Image info)
118 :     | cvtLHS (lhs, _) = cvtVar lhs
119 :    
120 : jhr 3437 (* simplify the statement stm where stms is a reverse-order list of preceeding simplified
121 :     * statements. This function returns a reverse-order list of simplified statements.
122 :     * Note that error reporting is done in the typechecker, but it does not prune unreachable
123 :     * code.
124 :     *)
125 : jhr 3456 and simplifyStmt (errStrm, stm, stms) : S.stmt list = (case stm
126 : jhr 3437 of AST.S_Block body => let
127 : jhr 3456 fun simplify ([], stms) = stms
128 :     | simplify (stm::r, stms) = simplify (r, simplifyStmt (errStrm, stm, stms))
129 : jhr 3437 in
130 : jhr 3456 simplify (body, stms)
131 : jhr 3437 end
132 : jhr 3452 | AST.S_Decl(x, NONE) => let
133 : jhr 3456 val x' = cvtVar x
134 : jhr 3452 in
135 : jhr 3465 S.S_Var(x', NONE) :: stms
136 : jhr 3452 end
137 :     | AST.S_Decl(x, SOME e) => let
138 : jhr 3456 val (stms, e') = simplifyExp (errStrm, e, stms)
139 : jhr 4193 val x' = cvtLHS (x, e')
140 : jhr 3437 in
141 : jhr 3465 S.S_Var(x', SOME e') :: stms
142 : jhr 3437 end
143 : jhr 4310 (* FIXME: we should also define a "boolean negate" operation on AST expressions so that we can
144 : jhr 4254 * handle both cases!
145 :     *)
146 :     | AST.S_IfThenElse(AST.E_Orelse(e1, e2), s1 as AST.S_Block[], s2) =>
147 : jhr 4317 simplifyStmt (errStrm, AST.S_IfThenElse(e1, s1, AST.S_IfThenElse(e2, s1, s2)), stms)
148 : jhr 4254 | AST.S_IfThenElse(AST.E_Andalso(e1, e2), s1, s2 as AST.S_Block[]) =>
149 : jhr 4317 simplifyStmt (errStrm, AST.S_IfThenElse(e1, AST.S_IfThenElse(e2, s1, s2), s2), stms)
150 : jhr 3437 | AST.S_IfThenElse(e, s1, s2) => let
151 : jhr 3456 val (stms, x) = simplifyExpToVar (errStrm, e, stms)
152 : jhr 4113 val s1 = simplifyBlock (errStrm, s1)
153 :     val s2 = simplifyBlock (errStrm, s2)
154 : jhr 3437 in
155 : jhr 3456 S.S_IfThenElse(x, s1, s2) :: stms
156 : jhr 3437 end
157 : jhr 4317 | AST.S_Foreach((x, e), body) => let
158 :     val (stms, xs') = simplifyExpToVar (errStrm, e, stms)
159 :     val body' = simplifyBlock (errStrm, body)
160 :     in
161 :     S.S_Foreach(cvtVar x, xs', body') :: stms
162 :     end
163 : jhr 3452 | AST.S_Assign((x, _), e) => let
164 : jhr 3456 val (stms, e') = simplifyExp (errStrm, e, stms)
165 : jhr 4193 val x' = cvtLHS (x, e')
166 : jhr 3437 in
167 : jhr 4193 S.S_Assign(x', e') :: stms
168 : jhr 3437 end
169 :     | AST.S_New(name, args) => let
170 : jhr 3456 val (stms, xs) = simplifyExpsToVars (errStrm, args, stms)
171 : jhr 3437 in
172 : jhr 3456 S.S_New(name, xs) :: stms
173 : jhr 3437 end
174 : jhr 3456 | AST.S_Continue => S.S_Continue :: stms
175 :     | AST.S_Die => S.S_Die :: stms
176 :     | AST.S_Stabilize => S.S_Stabilize :: stms
177 : jhr 3437 | AST.S_Return e => let
178 : jhr 3456 val (stms, x) = simplifyExpToVar (errStrm, e, stms)
179 : jhr 3437 in
180 : jhr 3456 S.S_Return x :: stms
181 : jhr 3437 end
182 :     | AST.S_Print args => let
183 : jhr 3456 val (stms, xs) = simplifyExpsToVars (errStrm, args, stms)
184 : jhr 3437 in
185 : jhr 3456 S.S_Print xs :: stms
186 : jhr 3437 end
187 :     (* end case *))
188 :    
189 : jhr 3456 and simplifyExp (errStrm, exp, stms) = let
190 : jhr 4317 fun doBorderCtl (f, args) = let
191 : jhr 4359 val (ctl, arg) = if Var.same(BV.image_border, f)
192 : jhr 4317 then (BorderCtl.Default(hd args), hd(tl args))
193 : jhr 4359 else if Var.same(BV.image_clamp, f)
194 : jhr 4317 then (BorderCtl.Clamp, hd args)
195 : jhr 4359 else if Var.same(BV.image_mirror, f)
196 : jhr 4317 then (BorderCtl.Mirror, hd args)
197 : jhr 4359 else if Var.same(BV.image_wrap, f)
198 : jhr 4317 then (BorderCtl.Wrap, hd args)
199 :     else raise Fail "impossible"
200 :     in
201 :     S.E_BorderCtl(ctl, arg)
202 :     end
203 :     fun doPrimApply (f, tyArgs, args, ty) = let
204 :     val (stms, xs) = simplifyExpsToVars (errStrm, args, stms)
205 :     in
206 :     if Basis.isBorderCtl f
207 :     then (stms, doBorderCtl (f, xs))
208 : jhr 4359 else if Var.same(f, BV.fn_sphere_im)
209 : jhr 4349 then raise Fail "FIXME: implicit sphere query"
210 : jhr 4317 else (case Var.kindOf f
211 :     of Var.BasisVar => let
212 :     fun cvtTyArg (Types.TYPE tv) = S.TY(cvtTy(TU.resolve tv))
213 :     | cvtTyArg (Types.DIFF dv) = S.DIFF(TU.monoDiff(TU.resolveDiff dv))
214 :     | cvtTyArg (Types.SHAPE sv) = S.SHAPE(TU.monoShape(TU.resolveShape sv))
215 :     | cvtTyArg (Types.DIM dv) = S.DIM(TU.monoDim(TU.resolveDim dv))
216 :     val tyArgs = List.map cvtTyArg tyArgs
217 :     in
218 :     (stms, S.E_Prim(f, tyArgs, xs, cvtTy ty))
219 :     end
220 :     | _ => raise Fail "bogus prim application"
221 :     (* end case *))
222 :     end
223 :     in
224 :     case exp
225 :     of AST.E_Var(x, _) => (case Var.kindOf x
226 :     of Var.BasisVar => let
227 :     val ty = cvtTy(Var.monoTypeOf x)
228 :     val x' = newTemp ty
229 :     val stm = S.S_Var(x', SOME(S.E_Prim(x, [], [], ty)))
230 :     in
231 :     (stm::stms, S.E_Var x')
232 :     end
233 :     | _ => (stms, S.E_Var(cvtVar x))
234 :     (* end case *))
235 :     | AST.E_Lit lit => (stms, S.E_Lit lit)
236 :     | AST.E_Kernel h => (stms, S.E_Kernel h)
237 :     | AST.E_Select(e, (fld, _)) => let
238 :     val (stms, x) = simplifyExpToVar (errStrm, e, stms)
239 :     in
240 :     (stms, S.E_Select(x, cvtVar fld))
241 :     end
242 :     | AST.E_Prim(rator, tyArgs, args as [e], ty) => (case e
243 : jhr 4359 of AST.E_Lit(Literal.Int n) => if Var.same(BV.neg_i, rator)
244 : jhr 4317 then (stms, S.E_Lit(Literal.Int(~n))) (* constant-fold negation of integer literals *)
245 :     else doPrimApply (rator, tyArgs, args, ty)
246 :     | AST.E_Lit(Literal.Real f) =>
247 : jhr 4359 if Var.same(BV.neg_t, rator)
248 : jhr 4317 then (stms, S.E_Lit(Literal.Real(RealLit.negate f))) (* constant-fold negation of real literals *)
249 :     else doPrimApply (rator, tyArgs, args, ty)
250 :     | AST.E_Comprehension(e', (x, e''), seqTy) => if Basis.isReductionOp rator
251 :     then let
252 :     val {rator, init, mvs} = Util.reductionInfo rator
253 :     val (stms, xs) = simplifyExpToVar (errStrm, e'', stms)
254 :     val (bodyStms, bodyResult) = simplifyExpToVar (errStrm, e, [])
255 :     val acc = SimpleVar.new ("accum", Var.LocalVar, cvtTy ty)
256 :     val seqTy' as STy.T_Sequence(elemTy, NONE) = cvtTy seqTy
257 :     val initStm = S.S_Var(acc, SOME(S.E_Lit init))
258 :     val updateStm = S.S_Assign(acc,
259 :     S.E_Prim(rator, mvs, [acc, bodyResult], seqTy'))
260 :     val foreachStm = S.S_Foreach(cvtVar x, xs, mkBlock(updateStm :: bodyStms))
261 :     in
262 :     (foreachStm :: initStm :: stms, S.E_Var acc)
263 :     end
264 :     else doPrimApply (rator, tyArgs, args, ty)
265 :     | AST.E_ParallelMap(e', x, xs, _) =>
266 :     if Basis.isReductionOp rator
267 :     then let
268 : jhr 4359 val (result, stm) = simplifyReduction (errStrm, rator, e', x, xs, ty)
269 : jhr 4317 in
270 : jhr 4359 (stm :: stms, S.E_Var result)
271 : jhr 4317 end
272 :     else raise Fail "unsupported operation on parallel map"
273 :     | _ => doPrimApply (rator, tyArgs, args, ty)
274 :     (* end case *))
275 :     | AST.E_Prim(f, tyArgs, args, ty) => doPrimApply (f, tyArgs, args, ty)
276 :     | AST.E_Apply((f, _), args, ty) => let
277 :     val (stms, xs) = simplifyExpsToVars (errStrm, args, stms)
278 :     in
279 :     case Var.kindOf f
280 :     of Var.FunVar => (stms, S.E_Apply(SimpleFunc.use(cvtFunc f), xs))
281 :     | _ => raise Fail "bogus application"
282 :     (* end case *)
283 :     end
284 :     | AST.E_Comprehension(e, (x, e'), seqTy) => let
285 :     (* convert a comprehension to a foreach loop over the sequence defined by e' *)
286 :     val (stms, xs) = simplifyExpToVar (errStrm, e', stms)
287 :     val (bodyStms, bodyResult) = simplifyExpToVar (errStrm, e, [])
288 :     val seqTy' as STy.T_Sequence(elemTy, NONE) = cvtTy seqTy
289 :     val acc = SimpleVar.new ("accum", Var.LocalVar, seqTy')
290 :     val initStm = S.S_Var(acc, SOME(S.E_Seq([], seqTy')))
291 :     val updateStm = S.S_Assign(acc,
292 : jhr 4359 S.E_Prim(BV.at_dT, [S.TY elemTy], [acc, bodyResult], seqTy'))
293 : jhr 4317 val foreachStm = S.S_Foreach(cvtVar x, xs, mkBlock(updateStm :: bodyStms))
294 :     in
295 :     (foreachStm :: initStm :: stms, S.E_Var acc)
296 :     end
297 :     | AST.E_ParallelMap(e, x, xs, ty) => raise Fail "FIXME: ParallelMap"
298 :     | AST.E_Tensor(es, ty) => let
299 :     val (stms, xs) = simplifyExpsToVars (errStrm, es, stms)
300 :     in
301 :     (stms, S.E_Tensor(xs, cvtTy ty))
302 :     end
303 :     | AST.E_Seq(es, ty) => let
304 :     val (stms, xs) = simplifyExpsToVars (errStrm, es, stms)
305 :     in
306 :     (stms, S.E_Seq(xs, cvtTy ty))
307 :     end
308 :     | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
309 :     val (stms, x) = simplifyExpToVar (errStrm, e, stms)
310 :     fun f NONE = NONE
311 :     | f (SOME(AST.E_Lit(Literal.Int i))) = SOME(Int.fromLarge i)
312 :     | f _ = raise Fail "expected integer literal in slice"
313 :     val indices = List.map f indices
314 :     in
315 :     (stms, S.E_Slice(x, indices, cvtTy ty))
316 :     end
317 :     | AST.E_Cond(e1, e2, e3, ty) => let
318 :     (* a conditional expression gets turned into an if-then-else statememt *)
319 :     val result = newTemp(cvtTy ty)
320 :     val (stms, x) = simplifyExpToVar (errStrm, e1, S.S_Var(result, NONE) :: stms)
321 :     fun simplifyBranch e = let
322 :     val (stms, e) = simplifyExp (errStrm, e, [])
323 :     in
324 :     mkBlock (S.S_Assign(result, e)::stms)
325 :     end
326 :     val s1 = simplifyBranch e2
327 :     val s2 = simplifyBranch e3
328 :     in
329 :     (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
330 :     end
331 :     | AST.E_Orelse(e1, e2) => simplifyExp (
332 :     errStrm,
333 :     AST.E_Cond(e1, AST.E_Lit(Literal.Bool true), e2, Ty.T_Bool),
334 :     stms)
335 :     | AST.E_Andalso(e1, e2) => simplifyExp (
336 :     errStrm,
337 :     AST.E_Cond(e1, e2, AST.E_Lit(Literal.Bool false), Ty.T_Bool),
338 :     stms)
339 :     | AST.E_LoadNrrd(_, nrrd, ty) => (case cvtTy ty
340 :     of ty as STy.T_Sequence(_, NONE) => (stms, S.E_LoadSeq(ty, nrrd))
341 :     | ty as STy.T_Image info => let
342 :     val dim = II.dim info
343 :     val shape = II.voxelShape info
344 :     in
345 :     case NrrdInfo.getInfo (errStrm, nrrd)
346 :     of SOME nrrdInfo => (case II.fromNrrd(nrrdInfo, dim, shape)
347 :     of NONE => (
348 :     Error.error (errStrm, [
349 :     "nrrd file \"", nrrd, "\" does not have expected type"
350 :     ]);
351 :     (stms, S.E_LoadImage(ty, nrrd, II.mkInfo(dim, shape))))
352 :     | SOME imgInfo =>
353 :     (stms, S.E_LoadImage(STy.T_Image imgInfo, nrrd, imgInfo))
354 :     (* end case *))
355 :     | NONE => (
356 :     Error.warning (errStrm, [
357 :     "nrrd file \"", nrrd, "\" does not exist"
358 :     ]);
359 :     (stms, S.E_LoadImage(ty, nrrd, II.mkInfo(dim, shape))))
360 :     (* end case *)
361 :     end
362 :     | _ => raise Fail "bogus type for E_LoadNrrd"
363 :     (* end case *))
364 :     | AST.E_Coerce{dstTy, e=AST.E_Lit(Literal.Int n), ...} => (case cvtTy dstTy
365 :     of SimpleTypes.T_Tensor[] => (stms, S.E_Lit(Literal.Real(RealLit.fromInt n)))
366 :     | _ => raise Fail "impossible: bad coercion"
367 :     (* end case *))
368 :     | AST.E_Coerce{srcTy, dstTy, e} => let
369 :     val (stms, x) = simplifyExpToVar (errStrm, e, stms)
370 :     val dstTy = cvtTy dstTy
371 :     val result = newTemp dstTy
372 :     val rhs = S.E_Coerce{srcTy = cvtTy srcTy, dstTy = dstTy, x = x}
373 :     in
374 :     (S.S_Var(result, SOME rhs)::stms, S.E_Var result)
375 :     end
376 :     (* end case *)
377 :     end
378 : jhr 3437
379 : jhr 3456 and simplifyExpToVar (errStrm, exp, stms) = let
380 :     val (stms, e) = simplifyExp (errStrm, exp, stms)
381 : jhr 3437 in
382 :     case e
383 :     of S.E_Var x => (stms, x)
384 :     | _ => let
385 :     val x = newTemp (S.typeOf e)
386 :     in
387 : jhr 3465 (S.S_Var(x, SOME e)::stms, x)
388 : jhr 3437 end
389 :     (* end case *)
390 :     end
391 :    
392 : jhr 3456 and simplifyExpsToVars (errStrm, exps, stms) = let
393 : jhr 3437 fun f ([], xs, stms) = (stms, List.rev xs)
394 :     | f (e::es, xs, stms) = let
395 : jhr 3456 val (stms, x) = simplifyExpToVar (errStrm, e, stms)
396 : jhr 3437 in
397 :     f (es, x::xs, stms)
398 :     end
399 :     in
400 :     f (exps, [], stms)
401 :     end
402 :    
403 : jhr 4359 (* simplify a parallel map-reduce *)
404 :     and simplifyReduction (errStrm, rator, e, x, xs, resTy) = let
405 :     val rator' = if Var.same(BV.red_all, rator) then Reductions.ALL
406 :     else if Var.same(BV.red_exists, rator) then Reductions.EXISTS
407 :     else if Var.same(BV.red_max, rator) then Reductions.MAX
408 :     else if Var.same(BV.red_mean, rator) then raise Fail "FIXME: mean reduction"
409 :     else if Var.same(BV.red_min, rator) then Reductions.MIN
410 :     else if Var.same(BV.red_product, rator) then Reductions.PRODUCT
411 :     else if Var.same(BV.red_sum, rator) then Reductions.SUM
412 :     else if Var.same(BV.red_variance, rator) then raise Fail "FIXME: variance reduction"
413 :     else raise Fail "impossible: not a reduction"
414 :     val x' = cvtVar x
415 :     val result = SimpleVar.new ("res", Var.LocalVar, cvtTy resTy)
416 :     val (bodyStms, bodyResult) = simplifyExpToVar (errStrm, e, [])
417 :     (* FIXME: need to handle reductions over active/stable subsets of strands *)
418 :     val (func, args) = Util.makeFunction(
419 :     Var.nameOf rator, mkBlock(S.S_Return bodyResult :: bodyStms),
420 :     SimpleVar.typeOf bodyResult)
421 :     val mapReduceStm = S.S_MapReduce{
422 :     results = [result],
423 :     reductions = [rator'],
424 :     body = func,
425 :     args = args,
426 :     source = x'
427 :     }
428 :     in
429 :     (result, mapReduceStm)
430 :     end
431 :    
432 :    
433 : jhr 4113 (* simplify a block and then prune unreachable and dead code *)
434 :     fun simplifyAndPruneBlock errStrm blk =
435 : jhr 4317 DeadCode.eliminate (simplifyBlock (errStrm, blk))
436 : jhr 4113
437 : jhr 3995 fun simplifyStrand (errStrm, strand) = let
438 : jhr 4317 val AST.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
439 : jhr 3456 val params' = cvtVars params
440 :     fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
441 :     | simplifyState ((x, optE) :: r, xs, stms) = let
442 :     val x' = cvtVar x
443 : jhr 4317 in
444 :     case optE
445 :     of NONE => simplifyState (r, x'::xs, stms)
446 :     | SOME e => let
447 :     val (stms, e') = simplifyExp (errStrm, e, stms)
448 :     in
449 :     simplifyState (r, x'::xs, S.S_Assign(x', e') :: stms)
450 :     end
451 :     (* end case *)
452 :     end
453 : jhr 3456 val (xs, stm) = simplifyState (state, [], [])
454 : jhr 3437 in
455 : jhr 3452 S.Strand{
456 :     name = name,
457 :     params = params',
458 : jhr 3456 state = xs,
459 : jhr 4317 stateInit = stm,
460 :     initM = Option.map (simplifyAndPruneBlock errStrm) initM,
461 :     updateM = simplifyAndPruneBlock errStrm updateM,
462 :     stabilizeM = Option.map (simplifyAndPruneBlock errStrm) stabilizeM
463 : jhr 3452 }
464 : jhr 3437 end
465 :    
466 : jhr 3452 fun transform (errStrm, prog) = let
467 : jhr 4317 val AST.Program{
468 :     props, const_dcls, input_dcls, globals, globInit, strand, create, init, update
469 :     } = prog
470 :     val consts' = ref[]
471 :     val constInit = ref[]
472 :     val inputs' = ref[]
473 :     val globals' = ref[]
474 :     val globalInit = ref[]
475 :     val funcs = ref[]
476 :     fun simplifyConstDcl (x, SOME e) = let
477 :     val (stms, e') = simplifyExp (errStrm, e, [])
478 :     val x' = cvtVar x
479 :     in
480 :     consts' := x' :: !consts';
481 :     constInit := S.S_Assign(x', e') :: (stms @ !constInit)
482 :     end
483 :     fun simplifyInputDcl ((x, NONE), desc) = let
484 :     val x' = cvtVar x
485 :     val init = (case SimpleVar.typeOf x'
486 :     of STy.T_Image info => S.Image info
487 :     | _ => S.NoDefault
488 :     (* end case *))
489 :     val inp = S.INP{
490 :     var = x',
491 :     name = Var.nameOf x,
492 :     ty = apiTypeOf x',
493 :     desc = desc,
494 :     init = init
495 :     }
496 :     in
497 :     inputs' := inp :: !inputs'
498 :     end
499 :     | simplifyInputDcl ((x, SOME(AST.E_LoadNrrd(tvs, nrrd, ty))), desc) = let
500 :     val (x', init) = (case Var.monoTypeOf x
501 :     of Ty.T_Sequence(_, NONE) => (cvtVar x, S.LoadSeq nrrd)
502 :     | Ty.T_Image{dim, shape} => let
503 :     val dim = TU.monoDim dim
504 :     val shape = TU.monoShape shape
505 :     in
506 :     case NrrdInfo.getInfo (errStrm, nrrd)
507 :     of SOME nrrdInfo => (case II.fromNrrd(nrrdInfo, dim, shape)
508 :     of NONE => (
509 :     Error.error (errStrm, [
510 :     "proxy nrrd file \"", nrrd,
511 :     "\" does not have expected type"
512 :     ]);
513 :     (cvtVar x, S.Image(II.mkInfo(dim, shape))))
514 :     | SOME info =>
515 :     (newVarWithType(x, STy.T_Image info), S.Proxy(nrrd, info))
516 :     (* end case *))
517 :     | NONE => (
518 :     Error.warning (errStrm, [
519 :     "proxy nrrd file \"", nrrd, "\" does not exist"
520 :     ]);
521 :     (cvtVar x, S.Image(II.mkInfo(dim, shape))))
522 :     (* end case *)
523 :     end
524 :     | _ => raise Fail "impossible"
525 :     (* end case *))
526 :     val inp = S.INP{
527 :     var = x',
528 :     name = Var.nameOf x,
529 :     ty = apiTypeOf x',
530 :     desc = desc,
531 :     init = init
532 :     }
533 :     in
534 :     inputs' := inp :: !inputs'
535 :     end
536 :     | simplifyInputDcl ((x, SOME e), desc) = let
537 :     val x' = cvtVar x
538 :     val (stms, e') = simplifyExp (errStrm, e, [])
539 :     val inp = S.INP{
540 :     var = x',
541 :     name = Var.nameOf x,
542 :     ty = apiTypeOf x',
543 :     desc = desc,
544 :     init = S.ConstExpr
545 :     }
546 :     in
547 :     inputs' := inp :: !inputs';
548 :     constInit := S.S_Assign(x', e') :: (stms @ !constInit)
549 :     end
550 :     fun simplifyGlobalDcl (AST.D_Var(x, NONE)) = globals' := cvtVar x :: !globals'
551 :     | simplifyGlobalDcl (AST.D_Var(x, SOME e)) = let
552 :     val (stms, e') = simplifyExp (errStrm, e, [])
553 :     val x' = cvtLHS (x, e')
554 :     in
555 :     globals' := x' :: !globals';
556 :     globalInit := S.S_Assign(x', e') :: (stms @ !globalInit)
557 :     end
558 :     | simplifyGlobalDcl (AST.D_Func(f, params, body)) = let
559 :     val f' = cvtFunc f
560 :     val params' = cvtVars params
561 :     val body' = simplifyAndPruneBlock errStrm body
562 :     in
563 :     funcs := S.Func{f=f', params=params', body=body'} :: !funcs
564 :     end
565 :     val () = (
566 :     List.app simplifyConstDcl const_dcls;
567 :     List.app simplifyInputDcl input_dcls;
568 :     List.app simplifyGlobalDcl globals)
569 :     (* make the global-initialization block *)
570 :     val globInit = (case globInit
571 :     of SOME stm => mkBlock (simplifyStmt (errStrm, stm, !globalInit))
572 :     | NONE => mkBlock (!globalInit)
573 :     (* end case *))
574 :     (* if the globInit block is non-empty, record the fact in the property list *)
575 :     val props = (case globInit
576 :     of S.Block{code=[], ...} => props
577 :     | _ => Properties.GlobalInit :: props
578 :     (* end case *))
579 : jhr 3452 in
580 :     S.Program{
581 :     props = props,
582 : jhr 4317 consts = List.rev(!consts'),
583 : jhr 3452 inputs = List.rev(!inputs'),
584 : jhr 4317 constInit = mkBlock (!constInit),
585 : jhr 3452 globals = List.rev(!globals'),
586 : jhr 3995 globInit = globInit,
587 : jhr 3452 funcs = List.rev(!funcs),
588 : jhr 3456 strand = simplifyStrand (errStrm, strand),
589 : jhr 4113 create = Create.map (simplifyAndPruneBlock errStrm) create,
590 : jhr 4317 init = Option.map (simplifyAndPruneBlock errStrm) init,
591 :     update = Option.map (simplifyAndPruneBlock errStrm) update
592 : jhr 3452 }
593 :     end
594 :    
595 : jhr 3437 end

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