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

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