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

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