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 4265 - (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 :     GV.newProp (fn x => raise Fail(concat["getGlobalVar(", GV.uniqueName x, ")"]))
33 :     in
34 :     fun mkGlobalVar x = (case peekFn x
35 :     of NONE => let
36 :     val x' = TGV.new {
37 :     name = GV.name x,
38 : jhr 3948 ty = U.trType (GV.ty x),
39 : jhr 3835 input = GV.isInput x,
40 :     output = false, (* FIXME: change once we support output globals *)
41 : jhr 3843 varying = GV.isVarying x,
42 :     apiTy = if GV.isInput x
43 : jhr 3948 then SOME(U.toAPIType (GV.ty x))
44 : jhr 3843 else NONE
45 : jhr 3835 }
46 :     in
47 :     setFn (x, x');
48 :     x'
49 :     end
50 :     | SOME x' => x'
51 :     (* end case *))
52 :     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 3948 then SOME(U.toAPIType (SV.ty x))
62 : jhr 3843 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 :     IR.Func.newProp (fn f => raise Fail("no binding for function " ^ IR.Func.toString f))
72 :     in
73 :     val getFuncVar = getFn
74 :     fun mkFuncVar (f, needsWrld, hasGlobs) = let
75 :     val (resTy, paramTys) = IR.Func.ty f
76 : jhr 4163 (* QUESTION: what about vector result/arguments? *)
77 : jhr 4175 val f' = TreeFunc.new (
78 :     IR.Func.name f, U.trType resTy, List.map U.trType paramTys,
79 :     needsWrld, hasGlobs)
80 :     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 :     V.newProp (fn x => raise Fail(V.toString x))
92 : jhr 3856
93 : jhr 3955 fun repOf (env, x) = (case peekFn x
94 : jhr 3856 of SOME b => b
95 :     | NONE => let
96 :     val rep = (case V.ty x
97 : jhr 3948 of Ty.TensorTy[d] => VEC(U.newVectorVars(Env.layoutVec env d))
98 : jhr 4092 | ty => if AssignTypes.isMemoryVar x
99 :     then VAR(U.newMemVar x)
100 :     else VAR(U.newLocalVar x)
101 : jhr 3856 (* 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 3955 of SOME x' => repOf (env, x')
111 : jhr 3853 | NONE => NOEQ
112 :     (* end case *))
113 : jhr 3856
114 :     fun useVar env = let
115 :     val useV = Env.useVar env
116 :     in
117 :     fn x => (case UnifyVars.eqClassOf x
118 : jhr 3955 of SOME x' => (case repOf (env, x')
119 : jhr 3856 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 :     of TTy.TensorTy(shp as _::_) => T.E_Op(TOp.TensorRef shp, [e])
142 :     | _ => e
143 :     (* end case *))
144 :    
145 : jhr 4067 (* turn an expression of type TensorRefTy to one of TensorRef *)
146 :     fun mkDeref e = (case TreeTypeOf.exp e
147 :     of TTy.TensorRefTy(shp as _::_) => T.E_Op(TOp.TensorCopy shp, [e])
148 :     | _ => e
149 :     (* end case *))
150 :    
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 4092 val x' = if AssignTypes.isMemoryVar x then U.newMemVar x else U.newLocalVar x
159 : jhr 3858 in
160 : jhr 3948 Env.bindSimple (env, x, T.E_Var x');
161 : jhr 3858 x'
162 :     end
163 :    
164 : jhr 4091 (* define a new local variable and bind x to it in the environment. *)
165 :     fun newMemLocal (env, x) = let
166 :     val x' = U.newMemVar x
167 :     in
168 :     Env.bindSimple (env, x, T.E_Var x');
169 :     x'
170 :     end
171 :    
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 :     of Env.RHS(ty, e) => let
178 :     val tmp = U.newTempVar ("t", ty)
179 :     in
180 :     (T.E_Var tmp, T.S_Assign(true, tmp, e) :: stms)
181 :     end
182 :     | Env.TREE e => (e, stms)
183 : jhr 3941 | Env.VEC(layout, es) => let
184 : jhr 3948 val tmp = U.newTempVar("_arg", TTy.TensorTy[#wid layout])
185 : jhr 3941 in
186 : jhr 3948 (T.E_Var tmp, T.S_Assign(true, tmp, T.E_Pack(layout, es)) :: stms)
187 : jhr 3941 end
188 : jhr 3850 (* end case *))
189 :    
190 : jhr 3948 fun singleArgs (env, args) = let
191 :     val singleArg = singleArg env
192 :     fun doArg (arg, (es, stms)) = let
193 :     val (e', stms') = singleArg (arg, stms)
194 : jhr 3941 in
195 : jhr 3948 (e'::es, stms')
196 : jhr 3941 end
197 :     in
198 : jhr 3948 List.foldr doArg ([], []) args
199 : jhr 3941 end
200 :    
201 : jhr 3948 fun simpleArg env = let
202 :     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 :     (T.E_Var tmp, T.S_Assign(true, tmp, e)::stms)
209 :     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 :     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 :    
227 :     fun vectorArg (env, x) = let
228 :     fun expToArg (e, stms) = (case V.ty x
229 : jhr 3843 of Ty.TensorTy[d] => let
230 :     val layout = Env.layoutVec env d
231 : jhr 4033 val e = mkRef e
232 : jhr 3843 val es = List.tabulate (
233 :     List.length(#pieces layout),
234 :     fn i => T.E_VLoad(layout, e, i))
235 :     in
236 : jhr 3948 (layout, es, stms)
237 : jhr 3843 end
238 : jhr 3855 | ty => raise Fail(concat[
239 :     "expected ", V.toString x, " : TensorTy[_], but found " ^ Ty.toString ty
240 :     ])
241 : jhr 3843 (* end case *))
242 : jhr 3948 in
243 :     case useVar env x
244 :     of Env.RHS(ty, e) => let
245 :     val tmp = U.newTempVar ("t", ty)
246 :     in
247 :     expToArg (T.E_Var tmp, [T.S_Assign(true, tmp, e)])
248 :     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 3948 val (layout, exps, stms) = vectorArg (env, x)
260 :     fun doArg (x, (argLists, stms)) = let
261 :     val (_, exps, stms') = vectorArg (env, x)
262 : jhr 3834 in
263 : jhr 3948 (ListPair.mapEq (op ::) (exps, argLists), stms'@stms)
264 : jhr 3834 end
265 : jhr 3948 val (argLists, stms) = List.foldl doArg ([exps], stms) xs
266 : jhr 3834 in
267 : jhr 3948 (layout, List.map List.rev argLists, List.rev stms)
268 : jhr 3834 end
269 :    
270 : jhr 3948 (* translation for simple vector operations *)
271 :     fun trVecOp (env, rator, args) = let
272 :     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 :     (T.E_Var tmp :: es, T.S_Assign(true, tmp, e)::stms)
278 :     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 :    
287 : jhr 3852 fun trOp (env, srcRator, args) = let
288 : jhr 4039 fun bindTREE rator = let
289 : jhr 3948 val (args, stms) = simpleArgs (env, args)
290 :     in
291 :     (Env.TREE(T.E_Op(rator, args)), stms)
292 :     end
293 : jhr 4065 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 : jhr 3948 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 : jhr 3835 fun bindVOp rator = let
304 : jhr 3948 val (layout, argss, stms) = vectorArgs (env, args)
305 : jhr 4077 fun mkArgs (w, [p], [args]) = [T.E_Op(rator(w, p), args)]
306 : jhr 3880 | mkArgs (w, p::ps, args::argss) =
307 : jhr 4077 T.E_Op(rator(p, p), args) :: mkArgs (w-p, ps, argss)
308 :     | mkArgs _ = raise Fail "bindVOp: arity mismatch"
309 : jhr 3880 val exps = mkArgs (#wid layout, #pieces layout, argss)
310 : jhr 3829 in
311 : jhr 3948 (Env.VEC(layout, exps), stms)
312 : jhr 3829 end
313 : jhr 3880 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 : jhr 3829 in
321 : jhr 3834 case srcRator
322 : jhr 4039 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 4039 | 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 :     | Op.Not => bindTREE TOp.Not
341 :     | Op.Abs ty => bindTREE (TOp.Abs (cvtScalarTy ty))
342 :     | Op.Max ty => bindTREE (TOp.Max (cvtScalarTy ty))
343 :     | Op.Min ty => bindTREE (TOp.Min (cvtScalarTy ty))
344 :     | Op.RClamp => bindTREE TOp.RClamp
345 :     | Op.RLerp => bindTREE TOp.RLerp
346 : jhr 3834 | Op.VAdd _ => bindVOp TOp.VAdd
347 :     | Op.VSub _ => bindVOp TOp.VSub
348 : jhr 3855 | Op.VScale _ => let
349 :     val [s, v] = args
350 : jhr 3948 val (s, stms) = simpleArg env (s, [])
351 :     val (layout, vs, stms') = vectorArg (env, v)
352 : jhr 3880 val exps = mkArgs (fn (w, p, x) => (TOp.VScale(w, p), [s, x])) (layout, vs)
353 : jhr 3855 in
354 : jhr 3948 (Env.VEC(layout, exps), stms@stms')
355 : jhr 3855 end
356 : jhr 3834 | Op.VMul _ => bindVOp TOp.VMul
357 :     | Op.VNeg _ => bindVOp TOp.VNeg
358 : jhr 3856 | Op.VSum _ => let
359 :     val [v] = args
360 : jhr 3948 val (layout, pieces, stms) = vectorArg (env, v)
361 :     val e::es = mkArgs (fn (w, p, x) => (TOp.VSum(w, p), [x])) (layout, pieces)
362 : jhr 3856 in
363 : jhr 3948 (Env.TREE(List.foldr (fn (e, es) => T.E_Op(TOp.RAdd, [e, es])) e es), stms)
364 : jhr 3856 end
365 : jhr 4056 | Op.VDot _ => let
366 :     val (layout, argss, stms) = vectorArgs (env, args)
367 : jhr 4077 fun mkArgs (w, [p], [args]) = [T.E_Op(TOp.VDot(w, p), args)]
368 : jhr 4056 | mkArgs (w, p::ps, args::argss) =
369 : jhr 4077 T.E_Op(TOp.VDot(p, p), args) :: mkArgs (w-p, ps, argss)
370 :     | mkArgs _ = raise Fail "VDot: arity mismatch"
371 : jhr 4056 val e::es = mkArgs (#wid layout, #pieces layout, argss)
372 :     in
373 :     (Env.TREE(List.foldr (fn (e, es) => T.E_Op(TOp.RAdd, [e, es])) e es), stms)
374 :     end
375 : jhr 3844 | Op.VIndex(_, i) => let
376 : jhr 3856 val [v] = args
377 : jhr 3948 val ({wid, pieces, ...}, es, stms) = vectorArg (env, v)
378 : jhr 3880 fun select (_, wid, [w], [e]) = Env.TREE(T.E_Op(TOp.VIndex(wid, w, i), [e]))
379 :     | select (i, wid, w::ws, e::es) =
380 : jhr 3844 if (i < w)
381 : jhr 3880 then Env.TREE(T.E_Op(TOp.VIndex(w, w, i), [e]))
382 :     else select (i-w, wid-w, ws, es)
383 : jhr 3844 | select _ = raise Fail("bogus " ^ Op.toString srcRator)
384 :     in
385 : jhr 3948 (select (i, wid, pieces, es), stms)
386 : jhr 3844 end
387 : jhr 3855 | Op.VClamp n => let
388 : jhr 4128 val [lo, hi, v] = args
389 :     val (lo, stms) = simpleArg env (lo, [])
390 : jhr 3948 val (hi, stms) = simpleArg env (hi, stms)
391 : jhr 4128 val (layout, vs, stms') = vectorArg (env, v)
392 : jhr 3880 val exps = mkArgs
393 : jhr 4128 (fn (w, p, x) => (TOp.VClamp(w, p), [lo, hi, x]))
394 : jhr 3880 (layout, vs)
395 : jhr 3855 in
396 : jhr 4128 (Env.VEC(layout, exps), List.revAppend(stms, List.rev stms))
397 : jhr 3855 end
398 : jhr 3834 | Op.VMapClamp n => bindVOp TOp.VMapClamp
399 : jhr 4077 | Op.VLerp n => let
400 :     val [u, v, t] = args
401 :     val (layout, us, stms1) = vectorArg (env, u)
402 :     val (_, vs, stms2) = vectorArg (env, v)
403 :     val (t, stms) = simpleArg env (t, stms2 @ stms1)
404 :     val exps = let
405 :     fun mkArgs (w, [p], [u], [v]) = [T.E_Op(TOp.VLerp(w, p), [u, v, t])]
406 :     | mkArgs (w, p::ps, u::ur, v::vr) =
407 :     T.E_Op(TOp.VLerp(p, p), [u, v, t]) :: mkArgs (w-p, ps, ur, vr)
408 :     | mkArgs _ = raise Fail "VLerp: arity mismatch"
409 :     in
410 :     mkArgs (#wid layout, #pieces layout, us, vs)
411 :     end
412 :     in
413 :     (Env.VEC(layout, exps), stms)
414 :     end
415 : jhr 4173 | Op.TensorIndex(ty as Ty.TensorTy[_], [i]) => let
416 :     val [arg] = args
417 :     fun mkOp e = Env.TREE(T.E_Op(TOp.TensorIndex(TreeTypeOf.exp e, [i]), [e]))
418 :     in
419 :     case useVar env arg
420 :     of Env.RHS(ty, e) => let
421 :     val tmp = U.newTempVar ("t", ty)
422 :     in
423 :     (mkOp(T.E_Var tmp), [T.S_Assign(true, tmp, e)])
424 :     end
425 :     | Env.TREE e => (mkOp e, [])
426 :     | Env.VEC(layout, es) => let
427 :     fun mkOp (w, pw, i, e) =
428 :     (Env.TREE(T.E_Op(TOp.VIndex(w, pw, i), [e])), [])
429 :     fun index (idx, w, [pw], [e]) = mkOp (w, pw, idx, e)
430 :     | index (idx, w, pw::ws, e::es) =
431 :     if (idx < pw)
432 :     then mkOp (pw, pw, idx, e)
433 :     else index (idx - pw, w - pw, ws, es)
434 :     | index _ = raise Fail "inconsistent"
435 :     in
436 :     index (i, #wid layout, #pieces layout, es)
437 :     end
438 :     (* end case *)
439 :     end
440 : jhr 4036 | Op.TensorIndex(ty, idxs) => let
441 :     val ([arg], stms) = simpleArgs (env, args)
442 :     val ty = TreeTypeOf.exp arg
443 : jhr 3958 in
444 : jhr 4036 (Env.TREE(T.E_Op(TOp.TensorIndex(ty, idxs), [arg])), stms)
445 : jhr 3958 end
446 : jhr 4036 | Op.ProjectLast(_, idxs) => let
447 :     val ([arg], stms) = simpleArgs (env, args)
448 :     val ty = TreeTypeOf.exp arg
449 : jhr 3958 in
450 : jhr 4036 (Env.TREE(T.E_Op(TOp.ProjectLast(ty, idxs), [arg])), stms)
451 : jhr 3958 end
452 : jhr 4039 | Op.Select(ty, i) => bindTREE (TOp.Select(U.trType ty, i))
453 :     | Op.Subscript ty => bindTREE (TOp.Subscript(U.trType ty))
454 :     | Op.MkDynamic(ty, n) => bindTREE (TOp.MkDynamic(U.trType ty, n))
455 : jhr 4065 | Op.Append ty => bindTREE' (TOp.Append(U.trType ty))
456 :     | Op.Prepend ty => bindTREE' (TOp.Prepend(U.trType ty))
457 : jhr 4039 | Op.Concat ty => bindTREE (TOp.Concat(U.trType ty))
458 :     | Op.Range => bindTREE TOp.Range
459 :     | Op.Length ty => bindTREE (TOp.Length(U.trType ty))
460 : jhr 4065 | Op.SphereQuery(ty1, ty2) => bindTREE' (TOp.SphereQuery(U.trType ty1, U.trType ty2))
461 : jhr 4039 | Op.Sqrt => bindTREE TOp.Sqrt
462 :     | Op.Cos => bindTREE TOp.Cos
463 :     | Op.ArcCos => bindTREE TOp.ArcCos
464 :     | Op.Sin => bindTREE TOp.Sin
465 :     | Op.ArcSin => bindTREE TOp.ArcSin
466 :     | Op.Tan => bindTREE TOp.Tan
467 :     | Op.ArcTan => bindTREE TOp.ArcTan
468 :     | Op.Exp => bindTREE TOp.Exp
469 :     | Op.Ceiling 1 => bindTREE TOp.RCeiling
470 : jhr 3880 | Op.Ceiling d => bindVOp TOp.VCeiling
471 : jhr 4039 | Op.Floor 1 => bindTREE TOp.RFloor
472 : jhr 3880 | Op.Floor d => bindVOp TOp.VFloor
473 : jhr 4039 | Op.Round 1 => bindTREE TOp.RRound
474 : jhr 3880 | Op.Round d => bindVOp TOp.VRound
475 : jhr 4039 | Op.Trunc 1 => bindTREE TOp.RTrunc
476 : jhr 3880 | Op.Trunc d => bindVOp TOp.VTrunc
477 : jhr 4039 | Op.IntToReal => bindTREE TOp.IntToReal
478 :     | Op.RealToInt 1 => bindTREE TOp.RealToInt
479 : jhr 3880 | Op.RealToInt d => let
480 : jhr 3966 val [v] = args
481 :     val (layout, args, stms) = vectorArg (env, v)
482 : jhr 3880 in
483 :     case #pieces layout
484 : jhr 3948 of [w] => (Env.TREE(T.E_Op(TOp.VToInt(d, w), args)), stms)
485 : jhr 3880 | _ => raise Fail(concat["FIXME: RealToInt<", Int.toString d, ">"])
486 :     (* end case *)
487 :     end
488 : jhr 3955 (* FIXME: reduction operators
489 : jhr 3834 | Op.R_All ty => ??
490 :     | Op.R_Exists ty => ??
491 :     | Op.R_Max ty => ??
492 :     | Op.R_Min ty => ??
493 :     | Op.R_Sum ty => ??
494 :     | Op.R_Product ty => ??
495 :     | Op.R_Mean ty => ??
496 :     | Op.R_Variance ty => ??
497 : jhr 3844 *)
498 : jhr 4039 | Op.Transform info => bindTREE (TOp.Transform info)
499 :     | Op.Translate info => bindTREE (TOp.Translate info)
500 :     | Op.ControlIndex(info, ctl, d) => bindTREE (TOp.ControlIndex(info, ctl, d))
501 :     | Op.LoadVoxel info => bindTREE (TOp.LoadVoxel info)
502 : jhr 4151 | Op.Inside(info, s) => (case ImageInfo.dim info
503 :     of 1 => bindTREE (TOp.Inside(VectorLayout.realLayout, info, s))
504 :     | d => let
505 :     val [x, img] = args
506 :     val (layout, args, stms) = vectorArg (env, x)
507 :     val (img, stms) = simpleArg env (img, stms)
508 :     in
509 :     (Env.TREE(T.E_Op(TOp.Inside(layout, info, s), args@[img])), stms)
510 :     end
511 :     (* end case *))
512 : jhr 4185 | Op.IndexInside(info, s) => bindTREE (TOp.IndexInside(info, s))
513 : jhr 4039 | Op.ImageDim(info, d) => bindTREE(TOp.ImageDim(info, d))
514 :     | Op.MathFn f => bindTREE (TOp.MathFn f)
515 : jhr 3844 | rator => raise Fail("bogus operator " ^ Op.toString srcRator)
516 : jhr 3829 (* end case *)
517 :     end
518 :    
519 : jhr 3955 (* FIXME: we need to defer the copy when possible *)
520 :     (* if required, add a TensorCopy operation to the rhs of an assignment *)
521 :     fun mkAssignRHS (TTy.TensorTy _, rhs) = (case TreeTypeOf.exp rhs
522 :     of TTy.TensorRefTy shp => T.E_Op(TOp.TensorCopy shp, [rhs])
523 :     | _ => rhs
524 :     (* end case *))
525 :     | mkAssignRHS (_, rhs) = rhs
526 :    
527 :     fun mkDefn' (x, rhs) = mkDefn (x, mkAssignRHS(TV.ty x, rhs))
528 :     fun mkAssign' (x, rhs) = mkAssign (x, mkAssignRHS(TV.ty x, rhs))
529 :    
530 : jhr 4265 local
531 :     fun trLHS (env, x) = (case eqClassRepOf (env, x)
532 :     of NOEQ => newMemLocal (env, x)
533 :     | VAR y => y
534 :     | _ => raise Fail "unexpected vector variable"
535 :     (* end case *))
536 :     in
537 :     fun trEigenVecs (env, vals, vecs, rator, x, stms) = let
538 :     val (e, stms) = simpleArg env (x, stms)
539 :     val stm = T.S_MAssign([trLHS (env, vals), trLHS (env, vecs)], T.E_Op(rator, [mkRef e]))
540 :     in
541 :     stm :: stms
542 :     end
543 :     fun trEigenVals (env, vals, rator, x, stms) = let
544 :     val (e, stms) = simpleArg env (x, stms)
545 :     val (isDcl, lhs) = (case eqClassRepOf (env, vals)
546 :     of NOEQ => (true, newMemLocal (env, vals))
547 :     | VAR y => (false, y)
548 :     | _ => raise Fail "unexpected vector variable"
549 :     (* end case *))
550 :     val stm = T.S_Assign(isDcl, lhs, T.E_Op(rator, [mkRef e]))
551 :     in
552 :     stm :: stms
553 :     end
554 :     end (* local *)
555 :    
556 : jhr 3852 (* cases:
557 :     x in EqClass
558 :     issue assignment; lhs is binding of representative (could be multiple vars)
559 :     useCount(x) > 1 and rhs is not simple
560 :     rhs is simple
561 :     rhs is vector
562 :     *)
563 : jhr 4265 fun trAssign (env, lhs, rhs, stms) = let
564 : jhr 3955 fun getLHS () = (case UnifyVars.eqClassOf lhs of SOME x => x | _ => lhs)
565 : jhr 3889 (* binding for the lhs variable, where the rhs is a simple expression. We check to
566 :     * see if it is part of an merged equivalence class, in which case we need to generate
567 :     * assigment(s)
568 : jhr 3836 *)
569 : jhr 4265 fun bindSimple (rhs, stms) = (case eqClassRepOf(env, lhs)
570 :     of NOEQ => (Env.bindSimple (env, lhs, rhs); stms)
571 :     | VAR x' => mkAssign' (x', rhs) :: stms
572 : jhr 3889 | VEC xs' => (case V.ty lhs
573 :     of Ty.TensorTy[d] => let
574 :     val layout = Env.layoutVec env d
575 : jhr 4033 val rhs = mkRef rhs
576 : jhr 3889 in
577 : jhr 4265 List.foldli
578 :     (fn (i, x', stms) => mkAssign(x', T.E_VLoad(layout, rhs, i)) :: stms)
579 :     stms
580 :     xs'
581 : jhr 3889 end
582 :     | _ => raise Fail "inconsistent"
583 :     (* end case *))
584 : jhr 3836 (* end case *))
585 : jhr 3948 fun assignOp (rator, args) = let
586 : jhr 4265 val (args, stms') = simpleArgs (env, args)
587 : jhr 3948 in
588 :     case eqClassRepOf(env, lhs)
589 :     of NOEQ => (
590 : jhr 4091 Env.bindVar (env, lhs, Env.RHS(U.trTempType(V.ty lhs), T.E_Op(rator, args)));
591 : jhr 4265 stms' @ stms)
592 :     | VAR x' => stms' @ mkAssign' (x', T.E_Op(rator, args)) :: stms
593 : jhr 3948 | VEC _ => raise Fail ("unexpected VEC for lhs " ^ V.toString lhs)
594 :     (* end case *)
595 :     end
596 : jhr 4091 (* bind the lhs to a tensor cons expression (including Op.Zero) *)
597 :     fun bindCons (args, Ty.TensorTy[d], stms) = let
598 :     val layout = Env.layoutVec env d
599 :     fun mkVecs (args, w::ws) = let
600 :     (* take arguments from args to build a vector value of width w; pad as
601 :     * necessary.
602 :     *)
603 :     fun take (0, args, es) = T.E_Vec(w, w, List.rev es) :: mkVecs (args, ws)
604 :     | take (i, [], es) = if #padded layout andalso null ws
605 :     then [T.E_Vec(w-i, w, List.rev es)]
606 :     else raise Fail "too few arguments for CONS"
607 :     | take (i, arg::args, es) = take (i-1, args, arg :: es)
608 :     in
609 :     take (w, args, [])
610 :     end
611 :     | mkVecs ([], []) = []
612 :     | mkVecs (_, []) = raise Fail "too many arguments for CONS"
613 :     val es = mkVecs (args, #pieces layout)
614 :     in
615 : jhr 4092 case eqClassRepOf(env, lhs)
616 :     of NOEQ => if (V.useCount lhs > 1)
617 :     then (Env.bindVar(env, lhs, Env.VEC(layout, es)); stms)
618 :     else let
619 :     val vs = U.newVectorVars layout
620 :     in
621 :     Env.bindVar (env, lhs, Env.VEC(layout, List.map T.E_Var vs));
622 :     ListPair.foldl (fn (v, e, stms) => mkDefn(v, e)::stms) stms (vs, es)
623 :     end
624 :     | VEC xs =>
625 : jhr 4091 ListPair.foldl (fn (x, e, stms) => mkAssign(x, e)::stms) stms (xs, es)
626 :     | _ => raise Fail "inconsistent"
627 :     (* end case *)
628 :     end
629 :     | bindCons (args, ty as Ty.TensorTy _, stms) = let
630 :     val ty = U.trType ty
631 :     val cons = T.E_Cons(args, ty)
632 :     in
633 :     case eqClassRepOf(env, lhs)
634 : jhr 4092 of NOEQ => if (V.useCount lhs > 1)
635 :     then mkDefn (newMemLocal (env, lhs), cons) :: stms
636 :     else (
637 :     Env.bindVar (env, lhs, Env.RHS(ty, cons));
638 :     stms)
639 : jhr 4091 | VAR x => mkAssign (x, cons) :: stms
640 :     | VEC xs => raise Fail "inconsistent"
641 :     (* end case *)
642 :     end
643 : jhr 3834 in
644 :     case rhs
645 : jhr 4265 of IR.GLOBAL x => bindSimple (T.E_Global(mkGlobalVar x), stms)
646 : jhr 3852 | IR.STATE(NONE, fld) =>
647 : jhr 4265 bindSimple (T.E_State(NONE, getStateVar fld), stms)
648 : jhr 3948 | IR.STATE(SOME x, fld) => let
649 : jhr 4265 val (arg, stms) = simpleArg env (x, stms)
650 : jhr 3948 in
651 : jhr 4265 bindSimple (T.E_State(SOME arg, getStateVar fld), stms)
652 : jhr 3948 end
653 : jhr 4204 | IR.VAR x => (case eqClassRepOf(env, lhs)
654 : jhr 4265 of NOEQ => (Env.bindVar(env, lhs, useVar env x); stms)
655 : jhr 4204 | VAR x' => (case useVar env x
656 : jhr 4265 of Env.RHS(_, e) => mkAssign' (x', e) :: stms
657 :     | Env.TREE e => mkAssign' (x', e) :: stms
658 : jhr 4204 (* end case *))
659 :     | VEC xs => let
660 :     val (_, es, stms) = vectorArg (env, x)
661 :     in
662 : jhr 4265 ListPair.foldl
663 :     (fn (x, e, stms) => mkAssign' (x, e) :: stms)
664 :     stms (xs, es)
665 : jhr 4204 end
666 :     (* end case *))
667 : jhr 4265 | IR.LIT lit => bindSimple (T.E_Lit lit, stms)
668 :     (* FIXME: use the general IR.OP case for eigen-vals, since the target info says that they
669 : jhr 4025 * are not inline.
670 :     *)
671 : jhr 4265 | 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 : jhr 3886 | IR.OP(Op.Zero(ty as Ty.TensorTy dd), []) => let
674 : jhr 3887 val z = T.E_Lit(Literal.Real(RealLit.zero false))
675 : jhr 3886 val sz = List.foldl Int.* 1 dd
676 : jhr 3948 in
677 : jhr 4265 bindCons (List.tabulate(sz, fn _ => z), ty, stms)
678 : jhr 3948 end
679 : jhr 3886 | IR.OP(Op.LoadSeq(ty, file), []) => let
680 : jhr 3955 val lhs = newLocal (env, getLHS ())
681 : jhr 3886 in
682 : jhr 4265 T.S_LoadNrrd(lhs, U.toAPIType ty, file) :: stms
683 : jhr 3886 end
684 :     | IR.OP(Op.LoadImage(ty, file), []) => let
685 : jhr 3955 val lhs = newLocal (env, getLHS ())
686 : jhr 3886 in
687 : jhr 4265 T.S_LoadNrrd(lhs, U.toAPIType ty, file) :: stms
688 : jhr 3886 end
689 : jhr 3852 | IR.OP(rator, args) => let
690 : jhr 4265 val (rhs, stms') = trOp (env, rator, args)
691 :     val stms = stms' @ stms
692 : jhr 4025 val emitBind = (V.useCount lhs > 1) orelse not(Env.isInlineOp env rator)
693 : jhr 3852 in
694 : jhr 4025 case (rhs, eqClassRepOf(env, lhs), emitBind)
695 : jhr 3948 of (_, NOEQ, false) => (Env.bindVar (env, lhs, rhs); stms)
696 : jhr 4026 (* FIXME: if the rhs has TensorRef type, then we should make the lhs TensorRef too! *)
697 : jhr 3955 | (Env.TREE e, NOEQ, true) => mkDefn'(newLocal(env, lhs), e) :: stms
698 :     | (Env.TREE e, VAR x', _) => mkAssign'(x', e) :: stms
699 : jhr 3854 | (Env.VEC(layout, es), NOEQ, true) => let
700 : jhr 3948 val vs = U.newVectorVars layout
701 : jhr 3854 in
702 : jhr 3948 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 : jhr 3854 end
705 : jhr 3948 | (Env.VEC(layout, es), VEC xs, _) =>
706 :     ListPair.foldl (fn (x, e, stms) => mkAssign(x, e)::stms) stms (xs, es)
707 : jhr 3854 | _ => raise Fail "inconsistent"
708 :     (* end case *)
709 : jhr 3852 end
710 : jhr 3941 | IR.CONS(args, ty) => let
711 : jhr 4265 val (es, stms') = simpleArgs (env, args)
712 : jhr 3941 in
713 : jhr 4265 bindCons (es, ty, stms' @ stms)
714 : jhr 3941 end
715 :     | IR.SEQ(args, ty) => let
716 : jhr 4265 val (es, stms') = singleArgs (env, args)
717 :     val stms = stms' @ stms
718 : jhr 3948 val ty = U.trType ty
719 : jhr 4067 (* 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 : jhr 4091 val seq = T.E_Seq(es, ty)
725 : jhr 3941 in
726 : jhr 4091 case eqClassRepOf(env, lhs)
727 : jhr 4092 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 : jhr 4091 | VAR x => mkAssign (x, seq) :: stms
733 :     | VEC xs => raise Fail "inconsistent"
734 :     (* end case *)
735 : jhr 3941 end
736 : jhr 4163 | IR.APPLY(f, args) => let
737 : jhr 4265 val (es, stms') = singleArgs (env, args)
738 : jhr 4163 in
739 :     Env.bindVar (env, lhs, Env.TREE(T.E_Apply(getFuncVar f, es)));
740 : jhr 4265 stms' @ stms
741 : jhr 4163 end
742 : jhr 3852 | rhs => raise Fail(concat["unexpected ", IR.RHS.toString rhs, " in LowIR code"])
743 : jhr 3834 (* 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 3856 fun useScalar x = (case useVar env x
766 : jhr 3948 of Env.RHS(_, e) => e
767 :     | Env.TREE e => e
768 : jhr 3835 | _ => raise Fail("expected scalar binding for " ^ V.toString x)
769 :     (* end case *))
770 : jhr 4092 (* analyze the CFG *)
771 : jhr 3834 val _ = UnifyVars.analyze cfg
772 : jhr 4092 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 3851 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 3835 | IR.FOREACH{var, src, bodyEntry, succ, ...} => let
822 : jhr 3948 val var' = U.newIterVar var
823 :     val _ = Env.bindSimple (env, var, T.E_Var var')
824 : jhr 3924 (* 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 3948 * Note also that we resolve the src scalar before processing the body
828 :     * of the loop so that any nested flushPendings do not cause Range
829 :     * bindings to be flushed.
830 : jhr 3924 *)
831 : jhr 3948 val mkStm = (case useScalar(!src)
832 :     of T.E_Op(TOp.Range, [lo, hi]) =>
833 :     (fn body => T.S_For(var', lo, hi, body))
834 :     | e =>
835 :     (fn body => T.S_Foreach(var', e, body))
836 : jhr 3924 (* end case *))
837 :     val stms = Env.flushPending (env, stms)
838 : jhr 3948 val stm = mkStm (doNode (!bodyEntry, [], []))
839 : jhr 3834 in
840 : jhr 3851 doNode (!succ, ifStk, stm::stms)
841 : jhr 3834 end
842 : jhr 3848 | 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 :     doNode (!succ, ifStk, trAssign (env, lhs, rhs, stms))
847 : jhr 3866 | IR.MASSIGN{stm=([], Op.Print tys, xs), succ, ...} => let
848 : jhr 3941 val (es, stms') = singleArgs (env, xs)
849 : jhr 4033 (* 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 : jhr 3848 in
855 : jhr 3941 doNode (!succ, ifStk, stm :: List.revAppend (stms', stms))
856 : jhr 3848 end
857 : jhr 4265 | IR.MASSIGN{stm=([vals, vecs], Op.EigenVecs2x2, [x]), succ, ...} =>
858 :     doNode (
859 :     !succ, ifStk,
860 :     trEigenVecs (env, vals, vecs, TOp.EigenVecs2x2, x, stms))
861 :     | IR.MASSIGN{stm=([vals, vecs], Op.EigenVecs3x3, [x]), succ, ...} =>
862 :     doNode (
863 :     !succ, ifStk,
864 :     trEigenVecs (env, vals, vecs, TOp.EigenVecs3x3, x, stms))
865 : jhr 3866 | IR.MASSIGN{stm=(ys, rator, xs), succ, ...} => raise Fail(concat[
866 :     "unexepected operator ", Op.toString rator, " for MASSIGN"
867 :     ])
868 : jhr 3834 | IR.GASSIGN{lhs, rhs, succ, ...} => let
869 : jhr 3948 val gv = mkGlobalVar lhs
870 : jhr 3955 fun mkGAssign (gv, e) = T.S_GAssign(gv, mkAssignRHS(TGV.ty gv, e))
871 : jhr 3948 val stm = (case useVar env rhs
872 : jhr 3955 of Env.RHS(_, e) => mkGAssign(gv, e)
873 :     | Env.TREE e => mkGAssign(gv, e)
874 : jhr 3948 | Env.VEC(layout, es) => let
875 :     val tmp = U.newTempVar("_arg", TTy.TensorTy[#wid layout])
876 :     in
877 :     T.S_GAssign(gv, T.E_Pack(layout, es))
878 :     end
879 :     (* end case *))
880 : jhr 3834 in
881 : jhr 3948 doNode (!succ, ifStk, stm :: stms)
882 : jhr 3834 end
883 : jhr 3851 | IR.NEW{strand, args, succ, ...} => let
884 : jhr 3941 val (es, stms') = singleArgs (env, args)
885 :     val stm = T.S_New(strand, es)
886 : jhr 3851 in
887 : jhr 3941 doNode (!succ, ifStk, stm :: List.revAppend (stms', stms))
888 : jhr 3851 end
889 : jhr 3834 | IR.SAVE{lhs, rhs, succ, ...} => let
890 : jhr 3948 val sv = getStateVar lhs
891 : jhr 3955 fun mkSAssign (sv, e) = T.S_Save(sv, mkAssignRHS(TSV.ty sv, e))
892 : jhr 3948 val stm = (case useVar env rhs
893 : jhr 3955 of Env.RHS(_, e) => mkSAssign(sv, e)
894 :     | Env.TREE e => mkSAssign(sv, e)
895 : jhr 3948 | Env.VEC(layout, es) => let
896 :     val tmp = U.newTempVar("_arg", TTy.TensorTy[#wid layout])
897 :     in
898 :     T.S_Save(sv, T.E_Pack(layout, es))
899 :     end
900 :     (* end case *))
901 : jhr 3834 in
902 : jhr 3948 doNode (!succ, ifStk, stm :: stms)
903 : jhr 3834 end
904 :     | k as IR.EXIT{kind, succ, ...} => (case (!succ, kind)
905 : jhr 4168 of (NONE, ExitKind.RETURN NONE) => join (ifStk, stms, k)
906 :     | (NONE, ExitKind.RETURN(SOME x)) => let
907 :     val (e', stms) = singleArg env (x, stms)
908 :     in
909 :     join (ifStk, T.S_Return e' :: stms, k)
910 :     end
911 :     | (NONE, ExitKind.ACTIVE) => join (ifStk, T.S_Active :: stms, k)
912 : jhr 3834 | (NONE, ExitKind.STABILIZE) => let
913 :     val stms = T.S_Stabilize :: stms
914 :     in
915 : jhr 3851 join (ifStk, stms, k)
916 : jhr 3834 end
917 : jhr 3851 | (NONE, ExitKind.DIE) => join (ifStk, T.S_Die :: stms, k)
918 :     | (NONE, ExitKind.UNREACHABLE) => join (ifStk, stms, k)
919 : jhr 4168 | (SOME nd, ExitKind.RETURN NONE) => doNode (nd, ifStk, stms)
920 :     | (SOME nd, ExitKind.RETURN(SOME x)) => let
921 :     val (e', stms) = singleArg env (x, stms)
922 :     in
923 :     doNode (nd, ifStk, T.S_Return e' :: stms)
924 :     end
925 : jhr 3851 | (SOME nd, ExitKind.ACTIVE) => doNode (nd, ifStk, T.S_Active :: stms)
926 :     | (SOME nd, ExitKind.STABILIZE) => doNode (nd, ifStk, T.S_Stabilize :: stms)
927 :     | (SOME nd, ExitKind.DIE) => doNode (nd, ifStk, T.S_Die :: stms)
928 :     | (SOME nd, ExitKind.UNREACHABLE) => doNode (nd, ifStk, stms)
929 : jhr 3834 (* end case *))
930 :     (* end case *))
931 :     in
932 : jhr 3857 doNode (IR.CFG.entry cfg, [], [])
933 : jhr 3834 end
934 :    
935 : jhr 3857 fun trCFG info cfg = ScopeVars.assignScopes ([], trCFGWithEnv (Env.new info, cfg))
936 : jhr 3835
937 : jhr 4175 (* Convert a user-defined function. We need to check for language features
938 :     * that require the world pointer (e.g., printing) and for references to global variables.
939 :     *)
940 : jhr 4163 fun trFunc info (IR.Func{name, params, body}) = let
941 :     val params' = List.map U.newParamVar params
942 : jhr 4166 val env = Env.new info
943 :     val _ = ListPair.appEq
944 :     (fn (x, x') => Env.bindSimple (env, x, T.E_Var x'))
945 :     (params, params')
946 :     val body' = ScopeVars.assignScopes (params', trCFGWithEnv (env, body))
947 : jhr 4175 val {needsWorld, usesGlobals} = Util.analyzeBlock body'
948 :     val name' = mkFuncVar (name, needsWorld, usesGlobals)
949 : jhr 4163 in
950 :     T.Func{name = name', params = params', body = body'}
951 :     end
952 :    
953 : jhr 3953 (* Build a strand method from a TreeIR block. We need to check for language features
954 :     * that require the world pointer (e.g., printing) and for references to global variables.
955 :     *)
956 : jhr 3952 fun mkMethod body = let
957 : jhr 4175 val {needsWorld, usesGlobals} = Util.analyzeBlock body
958 : jhr 3952 in
959 : jhr 4175 T.Method{needsW = needsWorld, hasG = usesGlobals, body = body}
960 : jhr 3952 end
961 :    
962 : jhr 3851 fun trStrand info strand = let
963 : jhr 3952 val trMethod = mkMethod o trCFG info
964 : jhr 3835 val IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
965 : jhr 3948 val params' = List.map U.newParamVar params
966 : jhr 3835 val state' = List.map getStateVar state
967 : jhr 3857 val stateInit' = let
968 :     val env = Env.new info
969 :     in
970 :     ListPair.appEq
971 : jhr 3948 (fn (x, x') => Env.bindSimple (env, x, T.E_Var x'))
972 : jhr 3857 (params, params');
973 : jhr 3952 mkMethod (ScopeVars.assignScopes (params', trCFGWithEnv (env, stateInit)))
974 : jhr 3857 end
975 : jhr 3835 in
976 :     T.Strand{
977 :     name = name,
978 :     params = params',
979 :     state = state',
980 : jhr 3857 stateInit = stateInit',
981 : jhr 3952 initM = Option.map trMethod initM,
982 :     updateM = trMethod updateM,
983 :     stabilizeM = Option.map trMethod stabilizeM
984 : jhr 3835 }
985 :     end
986 :    
987 : jhr 3851 fun translate (prog, info) = let
988 : jhr 3879 (* first step is to flatten any nested CONS nodes *)
989 :     val prog = Flatten.transform prog
990 : jhr 3834 val LowIR.Program{
991 : jhr 3995 props, consts, inputs, constInit, globals,
992 : jhr 4163 funcs, globInit, strand, create, init, update
993 : jhr 3834 } = prog
994 : jhr 3852 val trCFG = trCFG info
995 : jhr 3834 in
996 :     TreeIR.Program{
997 :     props = props,
998 : jhr 3861 target = info,
999 : jhr 3852 consts = List.map mkGlobalVar consts,
1000 :     inputs = List.map (Inputs.map mkGlobalVar) inputs,
1001 :     constInit = trCFG constInit,
1002 :     globals = List.map mkGlobalVar globals,
1003 : jhr 4163 funcs = List.map (trFunc info) funcs,
1004 : jhr 3995 globInit = trCFG globInit,
1005 : jhr 3851 strand = trStrand info strand,
1006 : jhr 4045 create = Create.map trCFG create,
1007 : jhr 3995 init = Option.map trCFG init,
1008 : jhr 3852 update = Option.map trCFG update
1009 : jhr 3834 }
1010 :     end
1011 :    
1012 : jhr 3828 end

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