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

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