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

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/low-to-tree/low-to-tree.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/low-to-tree/low-to-tree.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4469 - (view) (download)

1 : jhr 3828 (* low-to-tree.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2016 The University of Chicago
6 :     * All rights reserved.
7 :     *)
8 :    
9 :     structure LowToTree : sig
10 :    
11 : jhr 3861 val translate : LowIR.program * TreeIR.target_info -> TreeIR.program
12 : jhr 3828
13 :     end = struct
14 :    
15 : jhr 3834 structure IR = LowIR
16 :     structure V = LowIR.Var
17 :     structure Ty = LowTypes
18 :     structure Op = LowOps
19 : jhr 3835 structure GV = IR.GlobalVar
20 :     structure SV = IR.StateVar
21 : jhr 3834 structure T = TreeIR
22 :     structure TTy = TreeTypes
23 :     structure TOp = TreeOps
24 :     structure TV = TreeVar
25 :     structure TGV = TreeGlobalVar
26 :     structure TSV = TreeStateVar
27 : jhr 3948 structure U = Util
28 : jhr 3828
29 : jhr 3835 (* associate Tree IL globals variables with Low IL variables using properties *)
30 :     local
31 :     val {setFn, peekFn, ...} =
32 : jhr 4317 GV.newProp (fn x => raise Fail(concat["getGlobalVar(", GV.uniqueName x, ")"]))
33 : jhr 3835 in
34 :     fun mkGlobalVar x = (case peekFn x
35 : jhr 4317 of NONE => let
36 :     val x' = TGV.new {
37 :     name = GV.name x,
38 :     ty = U.trType (GV.ty x),
39 :     input = GV.isInput x,
40 :     output = false, (* FIXME: change once we support output globals *)
41 :     varying = GV.isVarying x,
42 :     apiTy = if GV.isInput x
43 :     then SOME(U.toAPIType (GV.ty x))
44 :     else NONE
45 :     }
46 :     in
47 :     setFn (x, x');
48 :     x'
49 :     end
50 :     | SOME x' => x'
51 :     (* end case *))
52 : jhr 3835 end
53 :    
54 :     (* associate Tree IL state variables with Low IL variables using properties *)
55 :     local
56 :     fun mkStateVar x = TSV.new {
57 :     name = SV.name x,
58 : jhr 3948 ty = U.trType (SV.ty x),
59 : jhr 3843 varying = SV.isVarying x,
60 : jhr 4469 shared = SV.isShared x,
61 : jhr 3843 apiTy = if (SV.isOutput x)
62 : jhr 4317 then SOME(U.toAPIType (SV.ty x))
63 :     else NONE
64 : jhr 3835 }
65 :     in
66 :     val {getFn = getStateVar, ...} = SV.newProp mkStateVar
67 :     end
68 :    
69 : jhr 4163 (* associate Tree IL function variables with Low IL variables using properties *)
70 :     local
71 : jhr 4175 val {setFn, getFn : IR.func -> TreeFunc.t, ...} =
72 : jhr 4317 IR.Func.newProp (fn f => raise Fail("no binding for function " ^ IR.Func.toString f))
73 : jhr 4175 in
74 :     val getFuncVar = getFn
75 :     fun mkFuncVar (f, needsWrld, hasGlobs) = let
76 : jhr 4317 val (resTy, paramTys) = IR.Func.ty f
77 : jhr 4163 (* QUESTION: what about vector result/arguments? *)
78 : jhr 4317 val f' = TreeFunc.new (
79 :     IR.Func.name f, U.trType resTy, List.map U.trType paramTys,
80 : jhr 4386 needsWrld, hasGlobs, Flatten.isMapFunc f)
81 : jhr 4317 in
82 :     setFn (f, f'); f'
83 :     end
84 : jhr 4163 end
85 :    
86 : jhr 3853 (* for variables that are in an equivalence class (see UnifyVars), we use a single
87 :     * TreeIR variable (or vector of variables) to represent them.
88 :     *)
89 :     datatype eq_var_rep = NOEQ | VAR of TV.t | VEC of TV.t list
90 :     local
91 :     val {peekFn : IR.var -> eq_var_rep option, setFn, ...} =
92 : jhr 4317 V.newProp (fn x => raise Fail(V.toString x))
93 : jhr 3856
94 : jhr 3955 fun repOf (env, x) = (case peekFn x
95 : jhr 4317 of SOME b => b
96 :     | NONE => let
97 :     val rep = (case V.ty x
98 :     of Ty.TensorTy[d] => VEC(U.newVectorVars(Env.layoutVec env d))
99 :     | ty => if AssignTypes.isMemoryVar x
100 :     then VAR(U.newMemVar x)
101 :     else VAR(U.newLocalVar x)
102 :     (* end case *))
103 :     in
104 :     setFn (x, rep);
105 :     rep
106 :     end
107 :     (* end case *))
108 : jhr 3853 in
109 : jhr 3856
110 : jhr 3853 fun eqClassRepOf (env, x) = (case UnifyVars.eqClassOf x
111 : jhr 4317 of SOME x' => repOf (env, x')
112 :     | NONE => NOEQ
113 :     (* end case *))
114 : jhr 3856
115 :     fun useVar env = let
116 : jhr 4317 val useV = Env.useVar env
117 :     in
118 :     fn x => (case UnifyVars.eqClassOf x
119 :     of SOME x' => (case repOf (env, x')
120 :     of VAR x => Env.TREE(T.E_Var x)
121 :     | VEC xs => let
122 :     val Ty.TensorTy[d] = V.ty x
123 :     in
124 :     Env.VEC(Env.layoutVec env d, List.map T.E_Var xs)
125 :     end
126 :     | NOEQ => raise Fail "impossible"
127 :     (* end case *))
128 :     | NONE => useV x
129 :     (* end case *))
130 :     end
131 : jhr 3853 end
132 :    
133 : jhr 3848 fun mkBlock stms = T.Block{locals = ref [], body = stms}
134 : jhr 3834 fun mkIf (x, stms, []) = T.S_IfThen(x, mkBlock stms)
135 :     | mkIf (x, stms1, stms2) = T.S_IfThenElse(x, mkBlock stms1, mkBlock stms2)
136 : jhr 3857 fun mkAssign (x, e) = T.S_Assign(false, x, e)
137 :     fun mkDefn (x, e) = T.S_Assign(true, x, e)
138 : jhr 3889 val zero = T.E_Lit(Literal.Real(RealLit.zero false))
139 : jhr 3828
140 : jhr 4033 (* turn an expression of type TensorTy to one of TensorTyRef *)
141 :     fun mkRef e = (case TreeTypeOf.exp e
142 : jhr 4317 of TTy.TensorTy(shp as _::_) => T.E_Op(TOp.TensorRef shp, [e])
143 :     | _ => e
144 :     (* end case *))
145 : jhr 4033
146 : jhr 4067 (* turn an expression of type TensorRefTy to one of TensorRef *)
147 :     fun mkDeref e = (case TreeTypeOf.exp e
148 : jhr 4317 of TTy.TensorRefTy(shp as _::_) => T.E_Op(TOp.TensorCopy shp, [e])
149 :     | _ => e
150 :     (* end case *))
151 : jhr 4067
152 : jhr 3834 fun cvtScalarTy Ty.BoolTy = TTy.BoolTy
153 :     | cvtScalarTy Ty.IntTy = TTy.IntTy
154 : jhr 3835 | cvtScalarTy (Ty.TensorTy[]) = TTy.realTy
155 : jhr 3834 | cvtScalarTy ty = raise Fail(concat["cvtScalarTy(", Ty.toString ty, ")"])
156 :    
157 : jhr 3948 (* define a new local variable and bind x to it in the environment. *)
158 : jhr 3858 fun newLocal (env, x) = let
159 : jhr 4317 val x' = if AssignTypes.isMemoryVar x then U.newMemVar x else U.newLocalVar x
160 :     in
161 :     Env.bindSimple (env, x, T.E_Var x');
162 :     x'
163 :     end
164 : jhr 3858
165 : jhr 4091 (* define a new local variable and bind x to it in the environment. *)
166 :     fun newMemLocal (env, x) = let
167 : jhr 4317 val x' = U.newMemVar x
168 :     in
169 :     Env.bindSimple (env, x, T.E_Var x');
170 :     x'
171 :     end
172 : jhr 4091
173 : jhr 3852 (* get a variable's binding as a single argument expression. This means that
174 : jhr 3941 * if x is bound to a vector of expressions, then we need to pack it. Since
175 :     * Pack gets translated to an array, we need to create a new temp.
176 : jhr 3852 *)
177 : jhr 3948 fun singleArg env (x, stms) = (case useVar env x
178 : jhr 4317 of Env.RHS(ty, e) => let
179 :     val tmp = U.newTempVar ("t", ty)
180 :     in
181 : jhr 4387 (T.E_Var tmp, mkDefn(tmp, e) :: stms)
182 : jhr 4317 end
183 :     | Env.TREE e => (e, stms)
184 :     | Env.VEC(layout, es) => let
185 :     val tmp = U.newTempVar("_arg", TTy.TensorTy[#wid layout])
186 :     in
187 : jhr 4387 (T.E_Var tmp, mkDefn(tmp, T.E_Pack(layout, es)) :: stms)
188 : jhr 4317 end
189 :     (* end case *))
190 : jhr 3850
191 : jhr 3948 fun singleArgs (env, args) = let
192 : jhr 4317 val singleArg = singleArg env
193 :     fun doArg (arg, (es, stms)) = let
194 :     val (e', stms') = singleArg (arg, stms)
195 :     in
196 :     (e'::es, stms')
197 :     end
198 :     in
199 :     List.foldr doArg ([], []) args
200 :     end
201 : jhr 3941
202 : jhr 3948 fun simpleArg env = let
203 : jhr 4317 val useVar = useVar env
204 :     in
205 :     fn (x, stms) => (case useVar x
206 :     of Env.RHS(ty, e) => let
207 :     val tmp = U.newTempVar ("t", ty)
208 :     in
209 : jhr 4387 (T.E_Var tmp, mkDefn(tmp, e)::stms)
210 : jhr 4317 end
211 :     | Env.TREE e => (e, stms)
212 :     | _ => raise Fail("expected simple binding for " ^ IR.Var.toString x)
213 :     (* end case *))
214 :     end
215 : jhr 3852
216 : jhr 3948 (* translation for simple operations (e.g., scalar operations) *)
217 :     fun simpleArgs (env, args) = let
218 : jhr 4317 val simpleArg = simpleArg env
219 :     fun doArg (arg, (es, stms)) = let
220 :     val (e', stms') = simpleArg (arg, stms)
221 :     in
222 :     (e'::es, stms')
223 :     end
224 :     in
225 :     List.foldr doArg ([], []) args
226 :     end
227 : jhr 3948
228 :     fun vectorArg (env, x) = let
229 : jhr 4317 fun expToArg (e, stms) = (case V.ty x
230 :     of Ty.TensorTy[d] => let
231 :     val layout = Env.layoutVec env d
232 :     val e = mkRef e
233 :     val es = List.tabulate (
234 :     List.length(#pieces layout),
235 :     fn i => T.E_VLoad(layout, e, i))
236 :     in
237 :     (layout, es, stms)
238 :     end
239 :     | ty => raise Fail(concat[
240 :     "expected ", V.toString x, " : TensorTy[_], but found " ^ Ty.toString ty
241 :     ])
242 :     (* end case *))
243 :     in
244 :     case useVar env x
245 :     of Env.RHS(ty, e) => let
246 :     val tmp = U.newTempVar ("t", ty)
247 :     in
248 : jhr 4387 expToArg (T.E_Var tmp, [mkDefn(tmp, e)])
249 : jhr 4317 end
250 :     | Env.TREE e => expToArg(e, [])
251 :     | Env.VEC(layout, es) => (layout, es, [])
252 :     (* end case *)
253 :     end
254 : jhr 3834
255 :     (* convert a list of LowIR variables, each of which are mapped
256 :     * to lists of of vector expressions, to a list of list of expressions
257 :     *)
258 :     fun vectorArgs (env, []) = raise Fail "unexpected empty argument list"
259 :     | vectorArgs (env, x::xs) = let
260 : jhr 4317 val (layout, exps, stms) = vectorArg (env, x)
261 :     fun doArg (x, (argLists, stms)) = let
262 :     val (_, exps, stms') = vectorArg (env, x)
263 :     in
264 :     (ListPair.mapEq (op ::) (exps, argLists), stms'@stms)
265 :     end
266 :     val (argLists, stms) = List.foldl doArg ([exps], stms) xs
267 :     in
268 :     (layout, List.map List.rev argLists, List.rev stms)
269 :     end
270 : jhr 3834
271 : jhr 3948 (* translation for simple vector operations *)
272 :     fun trVecOp (env, rator, args) = let
273 : jhr 4317 val useVar = useVar env
274 :     fun doArg (x, (es, stms)) = (case useVar x
275 :     of Env.RHS(ty, e) => let
276 :     val tmp = U.newTempVar ("t", ty)
277 :     in
278 : jhr 4387 (T.E_Var tmp :: es, mkDefn(tmp, e)::stms)
279 : jhr 4317 end
280 :     | Env.TREE e => (e::es, stms)
281 :     | _ => raise Fail("expected simple binding for " ^ IR.Var.toString x)
282 :     (* end case *))
283 :     val (args, stms) = List.foldr doArg ([], []) args
284 :     in
285 :     (Env.TREE(T.E_Op(rator, args)), stms)
286 :     end
287 : jhr 3948
288 : jhr 3852 fun trOp (env, srcRator, args) = let
289 : jhr 4317 fun bindTREE rator = let
290 :     val (args, stms) = simpleArgs (env, args)
291 :     in
292 :     (Env.TREE(T.E_Op(rator, args)), stms)
293 :     end
294 :     fun bindTREE' rator = let
295 :     val (args, stms) = singleArgs (env, args)
296 :     in
297 :     (Env.TREE(T.E_Op(rator, args)), stms)
298 :     end
299 :     fun bindRHS (ty, rator) = let
300 :     val (args, stms) = simpleArgs (env, args)
301 :     in
302 :     (Env.RHS(ty, T.E_Op(rator, args)), stms)
303 :     end
304 :     fun bindVOp rator = let
305 :     val (layout, argss, stms) = vectorArgs (env, args)
306 :     fun mkArgs (w, [p], [args]) = [T.E_Op(rator(w, p), args)]
307 :     | mkArgs (w, p::ps, args::argss) =
308 :     T.E_Op(rator(p, p), args) :: mkArgs (w-p, ps, argss)
309 :     | mkArgs _ = raise Fail "bindVOp: arity mismatch"
310 :     val exps = mkArgs (#wid layout, #pieces layout, argss)
311 :     in
312 :     (Env.VEC(layout, exps), stms)
313 :     end
314 :     fun mkArgs f ({wid, pieces, ...} : TTy.vec_layout, args) = let
315 :     fun mk (w, [p], [x]) = [T.E_Op(f(w, p, x))]
316 :     | mk (w, p::ps, x::xs) = T.E_Op(f(p, p, x)) :: mk (w-p, ps, xs)
317 :     | mk _ = raise Fail "mkArgs: arity mismatch"
318 :     in
319 :     mk (wid, pieces, args)
320 :     end
321 :     in
322 :     case srcRator
323 :     of Op.IAdd => bindTREE TOp.IAdd
324 :     | Op.ISub => bindTREE TOp.ISub
325 :     | Op.IMul => bindTREE TOp.IMul
326 :     | Op.IDiv => bindTREE TOp.IDiv
327 :     | Op.IMod => bindTREE TOp.IMod
328 :     | Op.INeg => bindTREE TOp.INeg
329 : jhr 3843 (* QUESTION: should we just use VAdd 1, etc ?*)
330 : jhr 4317 | Op.RAdd => bindTREE TOp.RAdd
331 :     | Op.RSub => bindTREE TOp.RSub
332 :     | Op.RMul => bindTREE TOp.RMul
333 :     | Op.RDiv => bindTREE TOp.RDiv
334 :     | Op.RNeg => bindTREE TOp.RNeg
335 :     | Op.LT ty => bindTREE (TOp.LT (cvtScalarTy ty))
336 :     | Op.LTE ty => bindTREE (TOp.LTE (cvtScalarTy ty))
337 :     | Op.EQ ty => bindTREE (TOp.EQ (cvtScalarTy ty))
338 :     | Op.NEQ ty => bindTREE (TOp.NEQ (cvtScalarTy ty))
339 :     | Op.GT ty => bindTREE (TOp.GT (cvtScalarTy ty))
340 :     | Op.GTE ty => bindTREE (TOp.GTE (cvtScalarTy ty))
341 : jhr 4434 | Op.BAnd => bindTREE TOp.BAnd
342 :     | Op.BOr => bindTREE TOp.BOr
343 :     | Op.BNot => bindTREE TOp.BNot
344 : jhr 4317 | Op.Abs ty => bindTREE (TOp.Abs (cvtScalarTy ty))
345 :     | Op.Max ty => bindTREE (TOp.Max (cvtScalarTy ty))
346 :     | Op.Min ty => bindTREE (TOp.Min (cvtScalarTy ty))
347 :     | Op.RClamp => bindTREE TOp.RClamp
348 :     | Op.RLerp => bindTREE TOp.RLerp
349 :     | Op.VAdd _ => bindVOp TOp.VAdd
350 :     | Op.VSub _ => bindVOp TOp.VSub
351 :     | Op.VScale _ => let
352 :     val [s, v] = args
353 :     val (s, stms) = simpleArg env (s, [])
354 :     val (layout, vs, stms') = vectorArg (env, v)
355 :     val exps = mkArgs (fn (w, p, x) => (TOp.VScale(w, p), [s, x])) (layout, vs)
356 :     in
357 :     (Env.VEC(layout, exps), stms@stms')
358 :     end
359 :     | Op.VMul _ => bindVOp TOp.VMul
360 :     | Op.VNeg _ => bindVOp TOp.VNeg
361 :     | Op.VSum _ => let
362 :     val [v] = args
363 :     val (layout, pieces, stms) = vectorArg (env, v)
364 :     val e::es = mkArgs (fn (w, p, x) => (TOp.VSum(w, p), [x])) (layout, pieces)
365 :     in
366 :     (Env.TREE(List.foldr (fn (e, es) => T.E_Op(TOp.RAdd, [e, es])) e es), stms)
367 :     end
368 :     | Op.VDot _ => let
369 :     val (layout, argss, stms) = vectorArgs (env, args)
370 :     fun mkArgs (w, [p], [args]) = [T.E_Op(TOp.VDot(w, p), args)]
371 :     | mkArgs (w, p::ps, args::argss) =
372 :     T.E_Op(TOp.VDot(p, p), args) :: mkArgs (w-p, ps, argss)
373 :     | mkArgs _ = raise Fail "VDot: arity mismatch"
374 :     val e::es = mkArgs (#wid layout, #pieces layout, argss)
375 :     in
376 :     (Env.TREE(List.foldr (fn (e, es) => T.E_Op(TOp.RAdd, [e, es])) e es), stms)
377 :     end
378 :     | Op.VIndex(_, i) => let
379 :     val [v] = args
380 :     val ({wid, pieces, ...}, es, stms) = vectorArg (env, v)
381 :     fun select (_, wid, [w], [e]) = Env.TREE(T.E_Op(TOp.VIndex(wid, w, i), [e]))
382 :     | select (i, wid, w::ws, e::es) =
383 :     if (i < w)
384 :     then Env.TREE(T.E_Op(TOp.VIndex(w, w, i), [e]))
385 :     else select (i-w, wid-w, ws, es)
386 :     | select _ = raise Fail("bogus " ^ Op.toString srcRator)
387 :     in
388 :     (select (i, wid, pieces, es), stms)
389 :     end
390 :     | Op.VClamp n => let
391 :     val [lo, hi, v] = args
392 :     val (lo, stms) = simpleArg env (lo, [])
393 :     val (hi, stms) = simpleArg env (hi, stms)
394 :     val (layout, vs, stms') = vectorArg (env, v)
395 :     val exps = mkArgs
396 :     (fn (w, p, x) => (TOp.VClamp(w, p), [lo, hi, x]))
397 :     (layout, vs)
398 :     in
399 :     (Env.VEC(layout, exps), List.revAppend(stms, List.rev stms))
400 :     end
401 :     | Op.VMapClamp n => bindVOp TOp.VMapClamp
402 :     | Op.VLerp n => let
403 :     val [u, v, t] = args
404 :     val (layout, us, stms1) = vectorArg (env, u)
405 :     val (_, vs, stms2) = vectorArg (env, v)
406 :     val (t, stms) = simpleArg env (t, stms2 @ stms1)
407 :     val exps = let
408 :     fun mkArgs (w, [p], [u], [v]) = [T.E_Op(TOp.VLerp(w, p), [u, v, t])]
409 :     | mkArgs (w, p::ps, u::ur, v::vr) =
410 :     T.E_Op(TOp.VLerp(p, p), [u, v, t]) :: mkArgs (w-p, ps, ur, vr)
411 :     | mkArgs _ = raise Fail "VLerp: arity mismatch"
412 :     in
413 :     mkArgs (#wid layout, #pieces layout, us, vs)
414 :     end
415 :     in
416 :     (Env.VEC(layout, exps), stms)
417 :     end
418 :     | Op.TensorIndex(ty as Ty.TensorTy[_], [i]) => let
419 :     val [arg] = args
420 :     fun mkOp e = Env.TREE(T.E_Op(TOp.TensorIndex(TreeTypeOf.exp e, [i]), [e]))
421 :     in
422 :     case useVar env arg
423 :     of Env.RHS(ty, e) => let
424 :     val tmp = U.newTempVar ("t", ty)
425 :     in
426 : jhr 4387 (mkOp(T.E_Var tmp), [mkDefn(tmp, e)])
427 : jhr 4317 end
428 :     | Env.TREE e => (mkOp e, [])
429 :     | Env.VEC(layout, es) => let
430 :     fun mkOp (w, pw, i, e) =
431 :     (Env.TREE(T.E_Op(TOp.VIndex(w, pw, i), [e])), [])
432 :     fun index (idx, w, [pw], [e]) = mkOp (w, pw, idx, e)
433 :     | index (idx, w, pw::ws, e::es) =
434 :     if (idx < pw)
435 :     then mkOp (pw, pw, idx, e)
436 :     else index (idx - pw, w - pw, ws, es)
437 :     | index _ = raise Fail "inconsistent"
438 :     in
439 :     index (i, #wid layout, #pieces layout, es)
440 :     end
441 :     (* end case *)
442 :     end
443 :     | Op.TensorIndex(ty, idxs) => let
444 :     val ([arg], stms) = simpleArgs (env, args)
445 :     val ty = TreeTypeOf.exp arg
446 :     in
447 :     (Env.TREE(T.E_Op(TOp.TensorIndex(ty, idxs), [arg])), stms)
448 :     end
449 :     | Op.ProjectLast(_, idxs) => let
450 :     val ([arg], stms) = simpleArgs (env, args)
451 :     val ty = TreeTypeOf.exp arg
452 :     in
453 :     (Env.TREE(T.E_Op(TOp.ProjectLast(ty, idxs), [arg])), stms)
454 :     end
455 :     | Op.Select(ty, i) => bindTREE (TOp.Select(U.trType ty, i))
456 :     | Op.Subscript ty => bindTREE (TOp.Subscript(U.trType ty))
457 :     | Op.MkDynamic(ty, n) => bindTREE (TOp.MkDynamic(U.trType ty, n))
458 : jhr 4412 | Op.Append ty => let
459 : jhr 4434 val (args as [_, item], stms) = singleArgs (env, args)
460 :     in
461 :     (Env.TREE(T.E_Op(TOp.Append(U.trType ty, TreeTypeOf.exp item), args)), stms)
462 :     end
463 : jhr 4412 | Op.Prepend ty => let
464 : jhr 4434 val (args as [item, _], stms) = singleArgs (env, args)
465 :     in
466 :     (Env.TREE(T.E_Op(TOp.Prepend(U.trType ty, TreeTypeOf.exp item), args)), stms)
467 :     end
468 : jhr 4317 | Op.Concat ty => bindTREE (TOp.Concat(U.trType ty))
469 :     | Op.Range => bindTREE TOp.Range
470 :     | Op.Length ty => bindTREE (TOp.Length(U.trType ty))
471 : jhr 4349 | Op.SphereQuery(dim, ty) => bindTREE' (TOp.SphereQuery(dim, U.trType ty))
472 : jhr 4317 | Op.Sqrt => bindTREE TOp.Sqrt
473 :     | Op.Cos => bindTREE TOp.Cos
474 :     | Op.ArcCos => bindTREE TOp.ArcCos
475 :     | Op.Sin => bindTREE TOp.Sin
476 :     | Op.ArcSin => bindTREE TOp.ArcSin
477 :     | Op.Tan => bindTREE TOp.Tan
478 :     | Op.ArcTan => bindTREE TOp.ArcTan
479 : jhr 4039 | Op.Exp => bindTREE TOp.Exp
480 : jhr 4317 | Op.Ceiling 1 => bindTREE TOp.RCeiling
481 :     | Op.Ceiling d => bindVOp TOp.VCeiling
482 :     | Op.Floor 1 => bindTREE TOp.RFloor
483 :     | Op.Floor d => bindVOp TOp.VFloor
484 :     | Op.Round 1 => bindTREE TOp.RRound
485 :     | Op.Round d => bindVOp TOp.VRound
486 :     | Op.Trunc 1 => bindTREE TOp.RTrunc
487 :     | Op.Trunc d => bindVOp TOp.VTrunc
488 :     | Op.IntToReal => bindTREE TOp.IntToReal
489 :     | Op.RealToInt 1 => bindTREE TOp.RealToInt
490 :     | Op.RealToInt d => let
491 :     val [v] = args
492 :     val (layout, args, stms) = vectorArg (env, v)
493 :     in
494 :     case #pieces layout
495 :     of [w] => (Env.TREE(T.E_Op(TOp.VToInt(d, w), args)), stms)
496 :     | _ => raise Fail(concat["FIXME: RealToInt<", Int.toString d, ">"])
497 :     (* end case *)
498 :     end
499 : jhr 4394 | Op.NumStrands set => bindRHS (TTy.IntTy, TOp.NumStrands set)
500 : jhr 4362 | Op.Strands(ty, set) => bindTREE (TOp.Strands(U.trType ty, set))
501 : jhr 4317 | Op.Transform info => bindTREE (TOp.Transform info)
502 :     | Op.Translate info => bindTREE (TOp.Translate info)
503 :     | Op.ControlIndex(info, ctl, d) => bindTREE (TOp.ControlIndex(info, ctl, d))
504 :     | Op.LoadVoxel info => bindTREE (TOp.LoadVoxel info)
505 :     | Op.Inside(info, s) => (case ImageInfo.dim info
506 :     of 1 => bindTREE (TOp.Inside(VectorLayout.realLayout, info, s))
507 :     | d => let
508 :     val [x, img] = args
509 :     val (layout, args, stms) = vectorArg (env, x)
510 :     val (img, stms) = simpleArg env (img, stms)
511 :     in
512 :     (Env.TREE(T.E_Op(TOp.Inside(layout, info, s), args@[img])), stms)
513 :     end
514 :     (* end case *))
515 :     | Op.IndexInside(info, s) => bindTREE (TOp.IndexInside(info, s))
516 :     | Op.ImageDim(info, d) => bindTREE(TOp.ImageDim(info, d))
517 :     | Op.MathFn f => bindTREE (TOp.MathFn f)
518 :     | rator => raise Fail("bogus operator " ^ Op.toString srcRator)
519 :     (* end case *)
520 :     end
521 : jhr 3829
522 : jhr 3955 (* FIXME: we need to defer the copy when possible *)
523 :     (* if required, add a TensorCopy operation to the rhs of an assignment *)
524 :     fun mkAssignRHS (TTy.TensorTy _, rhs) = (case TreeTypeOf.exp rhs
525 : jhr 4317 of TTy.TensorRefTy shp => T.E_Op(TOp.TensorCopy shp, [rhs])
526 :     | _ => rhs
527 :     (* end case *))
528 : jhr 3955 | mkAssignRHS (_, rhs) = rhs
529 :    
530 :     fun mkDefn' (x, rhs) = mkDefn (x, mkAssignRHS(TV.ty x, rhs))
531 :     fun mkAssign' (x, rhs) = mkAssign (x, mkAssignRHS(TV.ty x, rhs))
532 :    
533 : jhr 4265 local
534 :     fun trLHS (env, x) = (case eqClassRepOf (env, x)
535 : jhr 4317 of NOEQ => newMemLocal (env, x)
536 :     | VAR y => y
537 :     | _ => raise Fail "unexpected vector variable"
538 :     (* end case *))
539 : jhr 4265 in
540 :     fun trEigenVecs (env, vals, vecs, rator, x, stms) = let
541 : jhr 4317 val (e, stms) = simpleArg env (x, stms)
542 :     val stm = T.S_MAssign([trLHS (env, vals), trLHS (env, vecs)], T.E_Op(rator, [mkRef e]))
543 :     in
544 :     stm :: stms
545 :     end
546 : jhr 4265 fun trEigenVals (env, vals, rator, x, stms) = let
547 : jhr 4317 val (e, stms) = simpleArg env (x, stms)
548 :     val (isDcl, lhs) = (case eqClassRepOf (env, vals)
549 :     of NOEQ => (true, newMemLocal (env, vals))
550 :     | VAR y => (false, y)
551 :     | _ => raise Fail "unexpected vector variable"
552 :     (* end case *))
553 :     val stm = T.S_Assign(isDcl, lhs, T.E_Op(rator, [mkRef e]))
554 :     in
555 :     stm :: stms
556 :     end
557 : jhr 4265 end (* local *)
558 :    
559 : jhr 3852 (* cases:
560 : jhr 4317 x in EqClass
561 :     issue assignment; lhs is binding of representative (could be multiple vars)
562 :     useCount(x) > 1 and rhs is not simple
563 :     rhs is simple
564 :     rhs is vector
565 : jhr 3852 *)
566 : jhr 4265 fun trAssign (env, lhs, rhs, stms) = let
567 : jhr 4317 fun getLHS () = (case UnifyVars.eqClassOf lhs of SOME x => x | _ => lhs)
568 :     (* binding for the lhs variable, where the rhs is a simple expression. We check to
569 :     * see if it is part of an merged equivalence class, in which case we need to generate
570 :     * assigment(s)
571 :     *)
572 :     fun bindSimple (rhs, stms) = (case eqClassRepOf(env, lhs)
573 :     of NOEQ => (Env.bindSimple (env, lhs, rhs); stms)
574 :     | VAR x' => mkAssign' (x', rhs) :: stms
575 :     | VEC xs' => (case V.ty lhs
576 :     of Ty.TensorTy[d] => let
577 :     val layout = Env.layoutVec env d
578 :     val rhs = mkRef rhs
579 :     in
580 :     List.foldli
581 :     (fn (i, x', stms) => mkAssign(x', T.E_VLoad(layout, rhs, i)) :: stms)
582 :     stms
583 :     xs'
584 :     end
585 :     | _ => raise Fail "inconsistent"
586 :     (* end case *))
587 :     (* end case *))
588 :     fun assignOp (rator, args) = let
589 :     val (args, stms') = simpleArgs (env, args)
590 :     in
591 :     case eqClassRepOf(env, lhs)
592 :     of NOEQ => (
593 :     Env.bindVar (env, lhs, Env.RHS(U.trTempType(V.ty lhs), T.E_Op(rator, args)));
594 :     stms' @ stms)
595 :     | VAR x' => stms' @ mkAssign' (x', T.E_Op(rator, args)) :: stms
596 :     | VEC _ => raise Fail ("unexpected VEC for lhs " ^ V.toString lhs)
597 :     (* end case *)
598 :     end
599 :     (* bind the lhs to a tensor cons expression (including Op.Zero) *)
600 :     fun bindCons (args, Ty.TensorTy[d], stms) = let
601 :     val layout = Env.layoutVec env d
602 :     fun mkVecs (args, w::ws) = let
603 :     (* take arguments from args to build a vector value of width w; pad as
604 :     * necessary.
605 :     *)
606 :     fun take (0, args, es) = T.E_Vec(w, w, List.rev es) :: mkVecs (args, ws)
607 :     | take (i, [], es) = if #padded layout andalso null ws
608 :     then [T.E_Vec(w-i, w, List.rev es)]
609 :     else raise Fail "too few arguments for CONS"
610 :     | take (i, arg::args, es) = take (i-1, args, arg :: es)
611 :     in
612 :     take (w, args, [])
613 :     end
614 :     | mkVecs ([], []) = []
615 :     | mkVecs (_, []) = raise Fail "too many arguments for CONS"
616 :     val es = mkVecs (args, #pieces layout)
617 :     in
618 :     case eqClassRepOf(env, lhs)
619 :     of NOEQ => if (V.useCount lhs > 1)
620 :     then let
621 :     val vs = U.newVectorVars layout
622 :     in
623 :     Env.bindVar (env, lhs, Env.VEC(layout, List.map T.E_Var vs));
624 :     ListPair.foldl (fn (v, e, stms) => mkDefn(v, e)::stms) stms (vs, es)
625 :     end
626 :     else (Env.bindVar(env, lhs, Env.VEC(layout, es)); stms)
627 :     | VEC xs =>
628 :     ListPair.foldl (fn (x, e, stms) => mkAssign(x, e)::stms) stms (xs, es)
629 :     | _ => raise Fail "inconsistent"
630 :     (* end case *)
631 :     end
632 :     | bindCons (args, ty as Ty.TensorTy _, stms) = let
633 :     val ty = U.trType ty
634 :     val cons = T.E_Cons(args, ty)
635 :     in
636 :     case eqClassRepOf(env, lhs)
637 :     of NOEQ => if (V.useCount lhs > 1)
638 :     then mkDefn (newMemLocal (env, lhs), cons) :: stms
639 :     else (
640 :     Env.bindVar (env, lhs, Env.RHS(ty, cons));
641 :     stms)
642 :     | VAR x => mkAssign (x, cons) :: stms
643 :     | VEC xs => raise Fail "inconsistent"
644 :     (* end case *)
645 :     end
646 :     in
647 :     case rhs
648 :     of IR.GLOBAL x => bindSimple (T.E_Global(mkGlobalVar x), stms)
649 :     | IR.STATE(NONE, fld) =>
650 :     bindSimple (T.E_State(NONE, getStateVar fld), stms)
651 :     | IR.STATE(SOME x, fld) => let
652 :     val (arg, stms) = simpleArg env (x, stms)
653 :     in
654 :     bindSimple (T.E_State(SOME arg, getStateVar fld), stms)
655 :     end
656 :     | IR.VAR x => (case eqClassRepOf(env, lhs)
657 :     of NOEQ => (Env.bindVar(env, lhs, useVar env x); stms)
658 :     | VAR x' => (case useVar env x
659 :     of Env.RHS(_, e) => mkAssign' (x', e) :: stms
660 :     | Env.TREE e => mkAssign' (x', e) :: stms
661 :     (* end case *))
662 :     | VEC xs => let
663 :     val (_, es, stms') = vectorArg (env, x)
664 :     val stms = stms' @ stms
665 :     in
666 :     ListPair.foldl
667 :     (fn (x, e, stms) => mkAssign' (x, e) :: stms)
668 :     stms (xs, es)
669 :     end
670 :     (* end case *))
671 :     | IR.LIT lit => bindSimple (T.E_Lit lit, stms)
672 :     | IR.OP(Op.EigenVals2x2, [x]) => trEigenVals (env, lhs, TOp.EigenVals2x2, x, stms)
673 :     | IR.OP(Op.EigenVals3x3, [x]) => trEigenVals (env, lhs, TOp.EigenVals3x3, x, stms)
674 :     | IR.OP(Op.Zero(ty as Ty.TensorTy dd), []) => let
675 :     val z = T.E_Lit(Literal.Real(RealLit.zero false))
676 :     val sz = List.foldl Int.* 1 dd
677 :     in
678 :     bindCons (List.tabulate(sz, fn _ => z), ty, stms)
679 :     end
680 :     | IR.OP(Op.LoadSeq(ty, file), []) => let
681 :     val lhs = newLocal (env, getLHS ())
682 :     in
683 :     T.S_LoadNrrd(lhs, U.toAPIType ty, file) :: stms
684 :     end
685 :     | IR.OP(Op.LoadImage(ty, file), []) => let
686 :     val lhs = newLocal (env, getLHS ())
687 :     in
688 :     T.S_LoadNrrd(lhs, U.toAPIType ty, file) :: stms
689 :     end
690 :     | IR.OP(rator, args) => let
691 :     val (rhs, stms') = trOp (env, rator, args)
692 :     val stms = stms' @ stms
693 :     val emitBind = (V.useCount lhs > 1) orelse not(Env.isInlineOp env rator)
694 :     in
695 :     case (rhs, eqClassRepOf(env, lhs), emitBind)
696 :     of (_, NOEQ, false) => (Env.bindVar (env, lhs, rhs); stms)
697 :     | (Env.TREE e, NOEQ, true) => mkDefn'(newLocal(env, lhs), e) :: stms
698 :     | (Env.TREE e, VAR x', _) => mkAssign'(x', e) :: stms
699 :     | (Env.VEC(layout, es), NOEQ, true) => let
700 :     val vs = U.newVectorVars layout
701 :     in
702 :     Env.bindVar (env, lhs, Env.VEC(layout, List.map T.E_Var vs));
703 :     ListPair.foldl (fn (v, e, stms) => mkDefn(v, e)::stms) stms (vs, es)
704 :     end
705 :     | (Env.VEC(layout, es), VEC xs, _) =>
706 :     ListPair.foldl (fn (x, e, stms) => mkAssign(x, e)::stms) stms (xs, es)
707 :     | _ => raise Fail "inconsistent"
708 :     (* end case *)
709 :     end
710 :     | IR.CONS(args, ty) => let
711 :     val (es, stms') = simpleArgs (env, args)
712 :     in
713 :     bindCons (es, ty, stms' @ stms)
714 :     end
715 :     | IR.SEQ(args, ty) => let
716 :     val (es, stms') = singleArgs (env, args)
717 :     val stms = stms' @ stms
718 :     val ty = U.trType ty
719 :     (* if we are dealing with a sequence of tensors, then we need to copy references *)
720 :     val es = (case ty
721 :     of TTy.SeqTy(TTy.TensorTy _, _) => List.map mkDeref es
722 :     | _ => es
723 :     (* end case *))
724 :     val seq = T.E_Seq(es, ty)
725 :     in
726 :     case eqClassRepOf(env, lhs)
727 :     of NOEQ => if (V.useCount lhs > 1)
728 :     then mkDefn (newMemLocal (env, lhs), seq) :: stms
729 :     else (
730 :     Env.bindVar (env, lhs, Env.RHS(ty, seq));
731 :     stms)
732 :     | VAR x => mkAssign (x, seq) :: stms
733 :     | VEC xs => raise Fail "inconsistent"
734 :     (* end case *)
735 :     end
736 :     | IR.APPLY(f, args) => let
737 :     val (es, stms') = singleArgs (env, args)
738 :     in
739 :     Env.bindVar (env, lhs, Env.TREE(T.E_Apply(getFuncVar f, es)));
740 :     stms' @ stms
741 :     end
742 :     | rhs => raise Fail(concat["unexpected ", IR.RHS.toString rhs, " in LowIR code"])
743 :     (* end case *)
744 :     end
745 : jhr 3856 handle ex => (
746 :     print(concat["trAssign: ", V.toString lhs, " = ", IR.RHS.toString rhs, "\n"]);
747 :     raise ex)
748 : jhr 3834
749 :     (* In order to reconstruct the block-structure from the CFG, we keep a stack of open ifs.
750 :     * the items on this stack distinguish between when we are processing the then and else
751 :     * branches of the if.
752 :     *)
753 :     datatype open_if
754 :     (* working on the "then" branch. The fields are statments that preceed the if, the condition,
755 :     * and the else-branch node.
756 :     *)
757 :     = THEN_BR of T.stm list * T.exp * IR.node
758 :     (* working on the "else" branch. The fields are statments that preceed the if, the condition,
759 :     * the "then" branch statements, and the node kind that terminated the "then" branch (will be
760 :     * a JOIN or EXIT(DIE, STABILIZE, or UNREACHABLE)).
761 :     *)
762 :     | ELSE_BR of T.stm list * T.exp * T.stm list * IR.node_kind
763 :    
764 : jhr 3852 fun trCFGWithEnv (env, cfg) = let
765 : jhr 4317 fun useScalar x = (case useVar env x
766 :     of Env.RHS(_, e) => e
767 :     | Env.TREE e => e
768 :     | _ => raise Fail("expected scalar binding for " ^ V.toString x)
769 :     (* end case *))
770 :     (* analyze the CFG *)
771 :     val _ = UnifyVars.analyze cfg
772 :     val _ = AssignTypes.analyze cfg
773 : jhr 3851 (* join (stk, stms, k): handle a control-flow join, where env is the
774 : jhr 3834 * current environment, stk is the stack of open ifs (the top of stk specifies
775 :     * which branch we are in), stms are the TreeIL statements preceding the join
776 :     * on the current path, and k is the kind of the join node (either JOIN or EXIT).
777 :     *)
778 : jhr 3851 fun join ([], _, IR.JOIN _) = raise Fail "JOIN with no open if"
779 : jhr 3852 | join ([], stms, _) = mkBlock (List.rev stms)
780 : jhr 3851 | join (THEN_BR(stms1, cond, elseBr)::stk, thenBlk, k) = let
781 :     val thenBlk = Env.flushPending (env, thenBlk)
782 : jhr 3834 in
783 : jhr 3851 doNode (elseBr, ELSE_BR(stms1, cond, thenBlk, k)::stk, [])
784 : jhr 3834 end
785 : jhr 3851 | join (ELSE_BR(stms, cond, thenBlk, k1)::stk, elseBlk, k2) = let
786 :     val elseBlk = Env.flushPending (env, elseBlk)
787 : jhr 3834 in
788 :     case (k1, k2)
789 : jhr 3848 of (IR.JOIN{succ, ...}, IR.JOIN _) => let
790 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
791 :     in
792 : jhr 3851 doNode (!succ, stk, stm::stms)
793 : jhr 3834 end
794 : jhr 3848 | (IR.JOIN{succ, ...}, _) => let
795 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
796 :     in
797 : jhr 3851 doNode (!succ, stk, stm::stms)
798 : jhr 3834 end
799 : jhr 3848 | (_, IR.JOIN{succ, ...}) => let
800 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
801 :     in
802 : jhr 3851 doNode (!succ, stk, stm::stms)
803 : jhr 3834 end
804 :     | (_, _) => let
805 :     val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
806 :     in
807 : jhr 3852 mkBlock (List.rev(stm::stms))
808 : jhr 3834 end
809 :     (* end case *)
810 :     end
811 : jhr 4317 and doNode (nd : IR.node, ifStk : open_if list, stms) = (case IR.Node.kind nd
812 : jhr 3834 of IR.NULL => raise Fail "unexpected NULL"
813 : jhr 3851 | IR.ENTRY{succ} => doNode (!succ, ifStk, stms)
814 :     | k as IR.JOIN _ => join (ifStk, stms, k)
815 : jhr 3834 | IR.COND{cond, trueBranch, falseBranch, ...} => let
816 : jhr 3835 val cond = useScalar (!cond)
817 : jhr 3851 val stms = Env.flushPending (env, stms)
818 : jhr 3834 in
819 : jhr 3851 doNode (!trueBranch, THEN_BR(stms, cond, !falseBranch)::ifStk, [])
820 : jhr 3834 end
821 : jhr 4317 | IR.FOREACH{var, src, bodyEntry, succ, ...} => let
822 :     val var' = U.newIterVar var
823 :     val _ = Env.bindSimple (env, var, T.E_Var var')
824 :     (* note that the flatten phase on LowIR should guarantee that each
825 :     * FOREACH node that takes a Range argument has its own instance of
826 :     * the Range expression.
827 : jhr 4344 * Note also that we resolve the src before processing the body
828 : jhr 4317 * of the loop so that any nested flushPendings do not cause Range
829 :     * bindings to be flushed.
830 :     *)
831 : jhr 4369 val (src', stms) = simpleArg env (!src, stms)
832 :     val stms = Env.flushPending (env, stms)
833 : jhr 4344 val mkStm = (case src'
834 : jhr 4317 of T.E_Op(TOp.Range, [lo, hi]) =>
835 :     (fn body => T.S_For(var', lo, hi, body))
836 :     | e =>
837 :     (fn body => T.S_Foreach(var', e, body))
838 :     (* end case *))
839 :     val stm = mkStm (doNode (!bodyEntry, [], []))
840 :     in
841 :     doNode (!succ, ifStk, stm::stms)
842 :     end
843 :     | IR.NEXT _ => mkBlock (List.rev stms)
844 : jhr 3834 | IR.COM {text, succ, ...} =>
845 : jhr 3851 doNode (!succ, ifStk, T.S_Comment text :: stms)
846 : jhr 4265 | IR.ASSIGN{stm=(lhs, rhs), succ, ...} =>
847 : jhr 4317 doNode (!succ, ifStk, trAssign (env, lhs, rhs, stms))
848 : jhr 4362 | IR.MASSIGN{stm=([], IR.OP(Op.Print tys, xs)), succ, ...} => let
849 : jhr 4317 val (es, stms') = singleArgs (env, xs)
850 :     (* translate TensorTy to TensorRefTy in the type list *)
851 :     fun trType (Ty.TensorTy(shp as _::_)) = TTy.TensorRefTy shp
852 :     | trType ty = U.trType ty
853 :     val tys = List.map trType tys
854 :     val stm = T.S_Print(tys, List.map mkRef es)
855 :     in
856 :     doNode (!succ, ifStk, stm :: List.revAppend (stms', stms))
857 :     end
858 : jhr 4362 | IR.MASSIGN{stm=([vals, vecs], IR.OP(Op.EigenVecs2x2, [x])), succ, ...} =>
859 : jhr 4317 doNode (
860 :     !succ, ifStk,
861 :     trEigenVecs (env, vals, vecs, TOp.EigenVecs2x2, x, stms))
862 : jhr 4362 | IR.MASSIGN{stm=([vals, vecs], IR.OP(Op.EigenVecs3x3, [x])), succ, ...} =>
863 : jhr 4317 doNode (
864 :     !succ, ifStk,
865 :     trEigenVecs (env, vals, vecs, TOp.EigenVecs3x3, x, stms))
866 : jhr 4378 | IR.MASSIGN{stm=(ys, IR.MAPREDUCE mrs), succ, ...} =>
867 : jhr 4380 doNode (!succ, ifStk, trMapReduce (env, ys, mrs, stms))
868 : jhr 4362 | IR.MASSIGN{stm=(ys, rhs), succ, ...} => raise Fail(concat[
869 :     "unexepected rhs ", IR.RHS.toString rhs, " for MASSIGN"
870 : jhr 4317 ])
871 : jhr 3834 | IR.GASSIGN{lhs, rhs, succ, ...} => let
872 : jhr 4317 val gv = mkGlobalVar lhs
873 :     fun mkGAssign (gv, e) = T.S_GAssign(gv, mkAssignRHS(TGV.ty gv, e))
874 :     val stm = (case useVar env rhs
875 :     of Env.RHS(_, e) => mkGAssign(gv, e)
876 :     | Env.TREE e => mkGAssign(gv, e)
877 :     | Env.VEC(layout, es) => let
878 :     val tmp = U.newTempVar("_arg", TTy.TensorTy[#wid layout])
879 :     in
880 :     T.S_GAssign(gv, T.E_Pack(layout, es))
881 :     end
882 :     (* end case *))
883 : jhr 3834 in
884 : jhr 3948 doNode (!succ, ifStk, stm :: stms)
885 : jhr 3834 end
886 : jhr 3851 | IR.NEW{strand, args, succ, ...} => let
887 : jhr 4317 val (es, stms') = singleArgs (env, args)
888 :     val stm = T.S_New(strand, es)
889 :     in
890 :     doNode (!succ, ifStk, stm :: List.revAppend (stms', stms))
891 :     end
892 : jhr 3834 | IR.SAVE{lhs, rhs, succ, ...} => let
893 : jhr 4317 val sv = getStateVar lhs
894 :     fun mkSAssign (sv, e) = T.S_Save(sv, mkAssignRHS(TSV.ty sv, e))
895 :     val stm = (case useVar env rhs
896 :     of Env.RHS(_, e) => mkSAssign(sv, e)
897 :     | Env.TREE e => mkSAssign(sv, e)
898 :     | Env.VEC(layout, es) => let
899 :     val tmp = U.newTempVar("_arg", TTy.TensorTy[#wid layout])
900 :     in
901 :     T.S_Save(sv, T.E_Pack(layout, es))
902 :     end
903 :     (* end case *))
904 : jhr 3834 in
905 : jhr 3948 doNode (!succ, ifStk, stm :: stms)
906 : jhr 3834 end
907 :     | k as IR.EXIT{kind, succ, ...} => (case (!succ, kind)
908 : jhr 4168 of (NONE, ExitKind.RETURN NONE) => join (ifStk, stms, k)
909 : jhr 4317 | (NONE, ExitKind.RETURN(SOME x)) => let
910 :     val (e', stms) = singleArg env (x, stms)
911 :     in
912 :     join (ifStk, T.S_Return e' :: stms, k)
913 :     end
914 : jhr 4168 | (NONE, ExitKind.ACTIVE) => join (ifStk, T.S_Active :: stms, k)
915 : jhr 3834 | (NONE, ExitKind.STABILIZE) => let
916 :     val stms = T.S_Stabilize :: stms
917 :     in
918 : jhr 3851 join (ifStk, stms, k)
919 : jhr 3834 end
920 : jhr 3851 | (NONE, ExitKind.DIE) => join (ifStk, T.S_Die :: stms, k)
921 :     | (NONE, ExitKind.UNREACHABLE) => join (ifStk, stms, k)
922 : jhr 4317 | (SOME nd, ExitKind.RETURN NONE) => doNode (nd, ifStk, stms)
923 :     | (SOME nd, ExitKind.RETURN(SOME x)) => let
924 :     val (e', stms) = singleArg env (x, stms)
925 :     in
926 :     doNode (nd, ifStk, T.S_Return e' :: stms)
927 :     end
928 : jhr 3851 | (SOME nd, ExitKind.ACTIVE) => doNode (nd, ifStk, T.S_Active :: stms)
929 :     | (SOME nd, ExitKind.STABILIZE) => doNode (nd, ifStk, T.S_Stabilize :: stms)
930 :     | (SOME nd, ExitKind.DIE) => doNode (nd, ifStk, T.S_Die :: stms)
931 :     | (SOME nd, ExitKind.UNREACHABLE) => doNode (nd, ifStk, stms)
932 : jhr 3834 (* end case *))
933 :     (* end case *))
934 : jhr 4317 in
935 :     doNode (IR.CFG.entry cfg, [], [])
936 :     end
937 : jhr 3834
938 : jhr 4386 and trMapReduce (env, ys, mrs as (_, _, x::_)::rest, stms) = let
939 : jhr 4434 (* get the strand type of the first map-reduce *)
940 :     val IR.OP(Op.Strands(Ty.StrandTy strand, _), _) = IR.Var.getDef x
941 :     val srcVar = U.newTempVar("strand", TTy.StrandPtrTy strand)
942 :     fun mkMR (y, (r, f, src::args), (mrs, stms)) = let
943 :     val lhs = (case eqClassRepOf (env, y)
944 :     of NOEQ => newMemLocal (env, y)
945 :     | VAR y => y
946 :     | _ => raise Fail "unexpected vector variable"
947 :     (* end case *))
948 :     val srcSet = (case IR.Var.getDef src
949 :     of IR.OP(Op.Strands(_, set), _) => set
950 :     | rhs => raise Fail("expected Strands, but found " ^ IR.RHS.toString rhs)
951 :     (* end case *))
952 :     val (es, stms') = singleArgs (env, args)
953 :     val mr = T.MapReduce(lhs, r, getFuncVar f, T.E_Var srcVar :: es, srcSet)
954 :     in
955 :     (mr :: mrs, stms' @ stms)
956 :     end
957 :     val (mrs, stms) = ListPair.foldl mkMR ([], stms) (ys, mrs)
958 :     val mrStm = T.S_MapReduce(List.rev mrs, srcVar)
959 :     in
960 :     mrStm :: stms
961 :     end
962 : jhr 4380
963 : jhr 3857 fun trCFG info cfg = ScopeVars.assignScopes ([], trCFGWithEnv (Env.new info, cfg))
964 : jhr 3835
965 : jhr 4380 (* conversion for global initially and update blocks where we have to handle map-reduce
966 :     * statements.
967 :     *)
968 :     fun trGlobalCFG (info, funcs) cfg =
969 : jhr 4434 ScopeVars.assignScopes ([], trCFGWithEnv (Env.newWithFuncs (info, funcs), cfg))
970 : jhr 4380
971 : jhr 4175 (* Convert a user-defined function. We need to check for language features
972 :     * that require the world pointer (e.g., printing) and for references to global variables.
973 :     *)
974 : jhr 4386 fun trFunc info (IR.Func{name, params, body}) = let
975 : jhr 4434 val params' = List.map U.newParamVar params
976 :     val env = Env.new info
977 :     val _ = ListPair.appEq
978 :     (fn (x, x') => Env.bindSimple (env, x, T.E_Var x'))
979 :     (params, params')
980 :     val body' = ScopeVars.assignScopes (params', trCFGWithEnv (env, body))
981 :     val {needsWorld, usesGlobals} = Util.analyzeBlock body'
982 :     val name' = mkFuncVar (name, needsWorld, usesGlobals)
983 :     in
984 :     T.Func{name = name', params = params', body = body'}
985 :     end
986 : jhr 4163
987 : jhr 3953 (* Build a strand method from a TreeIR block. We need to check for language features
988 :     * that require the world pointer (e.g., printing) and for references to global variables.
989 :     *)
990 : jhr 3952 fun mkMethod body = let
991 : jhr 4317 val {needsWorld, usesGlobals} = Util.analyzeBlock body
992 :     in
993 :     T.Method{needsW = needsWorld, hasG = usesGlobals, body = body}
994 :     end
995 : jhr 3952
996 : jhr 3851 fun trStrand info strand = let
997 : jhr 4369 val IR.Strand{
998 :     name, params, spatialDim, state, stateInit, initM, updateM, stabilizeM
999 :     } = strand
1000 : jhr 4317 val trMethod = mkMethod o trCFG info
1001 : jhr 3948 val params' = List.map U.newParamVar params
1002 : jhr 4317 val state' = List.map getStateVar state
1003 :     val stateInit' = let
1004 :     val env = Env.new info
1005 :     in
1006 :     ListPair.appEq
1007 :     (fn (x, x') => Env.bindSimple (env, x, T.E_Var x'))
1008 :     (params, params');
1009 :     mkMethod (ScopeVars.assignScopes (params', trCFGWithEnv (env, stateInit)))
1010 :     end
1011 :     in
1012 :     T.Strand{
1013 :     name = name,
1014 :     params = params',
1015 : jhr 4369 spatialDim = spatialDim,
1016 : jhr 4317 state = state',
1017 :     stateInit = stateInit',
1018 :     initM = Option.map trMethod initM,
1019 :     updateM = trMethod updateM,
1020 :     stabilizeM = Option.map trMethod stabilizeM
1021 :     }
1022 :     end
1023 : jhr 3835
1024 : jhr 3851 fun translate (prog, info) = let
1025 : jhr 4317 (* first step is to flatten any nested CONS nodes *)
1026 :     val prog = Flatten.transform prog
1027 :     val LowIR.Program{
1028 :     props, consts, inputs, constInit, globals,
1029 :     funcs, globInit, strand, create, init, update
1030 :     } = prog
1031 :     val trCFG = trCFG info
1032 : jhr 4380 val trGlobalCFG = trGlobalCFG (info, funcs)
1033 : jhr 4317 in
1034 :     TreeIR.Program{
1035 :     props = props,
1036 :     target = info,
1037 :     consts = List.map mkGlobalVar consts,
1038 :     inputs = List.map (Inputs.map mkGlobalVar) inputs,
1039 :     constInit = trCFG constInit,
1040 :     globals = List.map mkGlobalVar globals,
1041 : jhr 4386 funcs = List.map (trFunc info) funcs,
1042 : jhr 4317 globInit = trCFG globInit,
1043 :     strand = trStrand info strand,
1044 :     create = Create.map trCFG create,
1045 : jhr 4380 init = Option.map trGlobalCFG init,
1046 :     update = Option.map trGlobalCFG update
1047 : jhr 4317 }
1048 :     end
1049 : jhr 3834
1050 : jhr 3828 end

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