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

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