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 3955 - (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 3853 (* for variables that are in an equivalence class (see UnifyVars), we use a single
69 :     * TreeIR variable (or vector of variables) to represent them.
70 :     *)
71 :     datatype eq_var_rep = NOEQ | VAR of TV.t | VEC of TV.t list
72 :     local
73 :     val {peekFn : IR.var -> eq_var_rep option, setFn, ...} =
74 :     V.newProp (fn x => raise Fail(V.toString x))
75 : jhr 3856
76 : jhr 3955 fun repOf (env, x) = (case peekFn x
77 : jhr 3856 of SOME b => b
78 :     | NONE => let
79 :     val rep = (case V.ty x
80 : jhr 3948 of Ty.TensorTy[d] => VEC(U.newVectorVars(Env.layoutVec env d))
81 :     | ty => VAR(U.newLocalVar x)
82 : jhr 3856 (* end case *))
83 :     in
84 :     setFn (x, rep);
85 :     rep
86 :     end
87 :     (* end case *))
88 : jhr 3853 in
89 : jhr 3856
90 : jhr 3853 fun eqClassRepOf (env, x) = (case UnifyVars.eqClassOf x
91 : jhr 3955 of SOME x' => repOf (env, x')
92 : jhr 3853 | NONE => NOEQ
93 :     (* end case *))
94 : jhr 3856
95 :     fun useVar env = let
96 :     val useV = Env.useVar env
97 :     in
98 :     fn x => (case UnifyVars.eqClassOf x
99 : jhr 3955 of SOME x' => (case repOf (env, x')
100 : jhr 3856 of VAR x => Env.TREE(T.E_Var x)
101 :     | VEC xs => let
102 :     val Ty.TensorTy[d] = V.ty x
103 :     in
104 :     Env.VEC(Env.layoutVec env d, List.map T.E_Var xs)
105 :     end
106 :     | NOEQ => raise Fail "impossible"
107 :     (* end case *))
108 :     | NONE => useV x
109 :     (* end case *))
110 :     end
111 : jhr 3853 end
112 :    
113 : jhr 3848 fun mkBlock stms = T.Block{locals = ref [], body = stms}
114 : jhr 3834 fun mkIf (x, stms, []) = T.S_IfThen(x, mkBlock stms)
115 :     | mkIf (x, stms1, stms2) = T.S_IfThenElse(x, mkBlock stms1, mkBlock stms2)
116 : jhr 3857 fun mkAssign (x, e) = T.S_Assign(false, x, e)
117 :     fun mkDefn (x, e) = T.S_Assign(true, x, e)
118 : jhr 3889 val zero = T.E_Lit(Literal.Real(RealLit.zero false))
119 : jhr 3828
120 : jhr 3834 fun cvtScalarTy Ty.BoolTy = TTy.BoolTy
121 :     | cvtScalarTy Ty.IntTy = TTy.IntTy
122 : jhr 3835 | cvtScalarTy (Ty.TensorTy[]) = TTy.realTy
123 : jhr 3834 | cvtScalarTy ty = raise Fail(concat["cvtScalarTy(", Ty.toString ty, ")"])
124 :    
125 : jhr 3948 (* define a new local variable and bind x to it in the environment. *)
126 : jhr 3858 fun newLocal (env, x) = let
127 : jhr 3948 val x' = U.newLocalVar x
128 : jhr 3858 in
129 : jhr 3948 Env.bindSimple (env, x, T.E_Var x');
130 : jhr 3858 x'
131 :     end
132 :    
133 : jhr 3852 (* get a variable's binding as a single argument expression. This means that
134 : jhr 3941 * if x is bound to a vector of expressions, then we need to pack it. Since
135 :     * Pack gets translated to an array, we need to create a new temp.
136 : jhr 3852 *)
137 : jhr 3948 fun singleArg env (x, stms) = (case useVar env x
138 :     of Env.RHS(ty, e) => let
139 :     val tmp = U.newTempVar ("t", ty)
140 :     in
141 :     (T.E_Var tmp, T.S_Assign(true, tmp, e) :: stms)
142 :     end
143 :     | Env.TREE e => (e, stms)
144 : jhr 3941 | Env.VEC(layout, es) => let
145 : jhr 3948 val tmp = U.newTempVar("_arg", TTy.TensorTy[#wid layout])
146 : jhr 3941 in
147 : jhr 3948 (T.E_Var tmp, T.S_Assign(true, tmp, T.E_Pack(layout, es)) :: stms)
148 : jhr 3941 end
149 : jhr 3850 (* end case *))
150 :    
151 : jhr 3948 fun singleArgs (env, args) = let
152 :     val singleArg = singleArg env
153 :     fun doArg (arg, (es, stms)) = let
154 :     val (e', stms') = singleArg (arg, stms)
155 : jhr 3941 in
156 : jhr 3948 (e'::es, stms')
157 : jhr 3941 end
158 :     in
159 : jhr 3948 List.foldr doArg ([], []) args
160 : jhr 3941 end
161 :    
162 : jhr 3948 fun simpleArg env = let
163 :     val useVar = useVar env
164 :     in
165 :     fn (x, stms) => (case useVar x
166 :     of Env.RHS(ty, e) => let
167 :     val tmp = U.newTempVar ("t", ty)
168 :     in
169 :     (T.E_Var tmp, T.S_Assign(true, tmp, e)::stms)
170 :     end
171 :     | Env.TREE e => (e, stms)
172 :     | _ => raise Fail("expected simple binding for " ^ IR.Var.toString x)
173 :     (* end case *))
174 :     end
175 : jhr 3852
176 : jhr 3948 (* translation for simple operations (e.g., scalar operations) *)
177 :     fun simpleArgs (env, args) = let
178 :     val simpleArg = simpleArg env
179 :     fun doArg (arg, (es, stms)) = let
180 :     val (e', stms') = simpleArg (arg, stms)
181 :     in
182 :     (e'::es, stms')
183 :     end
184 :     in
185 :     List.foldr doArg ([], []) args
186 :     end
187 :    
188 :     fun vectorArg (env, x) = let
189 :     fun expToArg (e, stms) = (case V.ty x
190 : jhr 3843 of Ty.TensorTy[d] => let
191 :     val layout = Env.layoutVec env d
192 :     (* QUESTION: can "e" be a complicated expression or are we guaranteed that it will just
193 :     * be a memory reference?
194 :     *)
195 :     val es = List.tabulate (
196 :     List.length(#pieces layout),
197 :     fn i => T.E_VLoad(layout, e, i))
198 :     in
199 : jhr 3948 (layout, es, stms)
200 : jhr 3843 end
201 : jhr 3855 | ty => raise Fail(concat[
202 :     "expected ", V.toString x, " : TensorTy[_], but found " ^ Ty.toString ty
203 :     ])
204 : jhr 3843 (* end case *))
205 : jhr 3948 in
206 :     case useVar env x
207 :     of Env.RHS(ty, e) => let
208 :     val tmp = U.newTempVar ("t", ty)
209 :     in
210 :     expToArg (T.E_Var tmp, [T.S_Assign(true, tmp, e)])
211 :     end
212 :     | Env.TREE e => expToArg(e, [])
213 :     | Env.VEC(layout, es) => (layout, es, [])
214 :     (* end case *)
215 :     end
216 : jhr 3834
217 :     (* convert a list of LowIR variables, each of which are mapped
218 :     * to lists of of vector expressions, to a list of list of expressions
219 :     *)
220 :     fun vectorArgs (env, []) = raise Fail "unexpected empty argument list"
221 :     | vectorArgs (env, x::xs) = let
222 : jhr 3948 val (layout, exps, stms) = vectorArg (env, x)
223 :     fun doArg (x, (argLists, stms)) = let
224 :     val (_, exps, stms') = vectorArg (env, x)
225 : jhr 3834 in
226 : jhr 3948 (ListPair.mapEq (op ::) (exps, argLists), stms'@stms)
227 : jhr 3834 end
228 : jhr 3948 val (argLists, stms) = List.foldl doArg ([exps], stms) xs
229 : jhr 3834 in
230 : jhr 3948 (layout, List.map List.rev argLists, List.rev stms)
231 : jhr 3834 end
232 :    
233 : jhr 3948 (* translation for simple vector operations *)
234 :     fun trVecOp (env, rator, args) = let
235 :     val useVar = useVar env
236 :     fun doArg (x, (es, stms)) = (case useVar x
237 :     of Env.RHS(ty, e) => let
238 :     val tmp = U.newTempVar ("t", ty)
239 :     in
240 :     (T.E_Var tmp :: es, T.S_Assign(true, tmp, e)::stms)
241 :     end
242 :     | Env.TREE e => (e::es, stms)
243 :     | _ => raise Fail("expected simple binding for " ^ IR.Var.toString x)
244 :     (* end case *))
245 :     val (args, stms) = List.foldr doArg ([], []) args
246 :     in
247 :     (Env.TREE(T.E_Op(rator, args)), stms)
248 :     end
249 :    
250 : jhr 3852 fun trOp (env, srcRator, args) = let
251 : jhr 3948 fun bindOp rator = let
252 :     val (args, stms) = simpleArgs (env, args)
253 :     in
254 :     (Env.TREE(T.E_Op(rator, args)), stms)
255 :     end
256 :     fun bindRHS (ty, rator) = let
257 :     val (args, stms) = simpleArgs (env, args)
258 :     in
259 :     (Env.RHS(ty, T.E_Op(rator, args)), stms)
260 :     end
261 : jhr 3835 fun bindVOp rator = let
262 : jhr 3948 val (layout, argss, stms) = vectorArgs (env, args)
263 : jhr 3880 fun mkArgs (_, [], []) = []
264 :     | mkArgs (w, p::ps, args::argss) =
265 :     T.E_Op(rator(w, p), args) :: mkArgs (w-p, ps, argss)
266 :     val exps = mkArgs (#wid layout, #pieces layout, argss)
267 : jhr 3829 in
268 : jhr 3948 (Env.VEC(layout, exps), stms)
269 : jhr 3829 end
270 : jhr 3880 fun mkArgs f ({wid, pieces, ...} : TTy.vec_layout, args) = let
271 :     fun mk (w, [p], [x]) = [T.E_Op(f(w, p, x))]
272 :     | mk (w, p::ps, x::xs) = T.E_Op(f(p, p, x)) :: mk (w-p, ps, xs)
273 :     | mk _ = raise Fail "mkArgs: arity mismatch"
274 :     in
275 :     mk (wid, pieces, args)
276 :     end
277 : jhr 3829 in
278 : jhr 3834 case srcRator
279 :     of Op.IAdd => bindOp TOp.IAdd
280 :     | Op.ISub => bindOp TOp.ISub
281 :     | Op.IMul => bindOp TOp.IMul
282 :     | Op.IDiv => bindOp TOp.IDiv
283 :     | Op.IMod => bindOp TOp.IMod
284 :     | Op.INeg => bindOp TOp.INeg
285 : jhr 3843 (* QUESTION: should we just use VAdd 1, etc ?*)
286 : jhr 3834 | Op.RAdd => bindOp TOp.RAdd
287 :     | Op.RSub => bindOp TOp.RSub
288 :     | Op.RMul => bindOp TOp.RMul
289 :     | Op.RDiv => bindOp TOp.RDiv
290 :     | Op.RNeg => bindOp TOp.RNeg
291 :     | Op.LT ty => bindOp (TOp.LT (cvtScalarTy ty))
292 :     | Op.LTE ty => bindOp (TOp.LTE (cvtScalarTy ty))
293 :     | Op.EQ ty => bindOp (TOp.EQ (cvtScalarTy ty))
294 :     | Op.NEQ ty => bindOp (TOp.NEQ (cvtScalarTy ty))
295 :     | Op.GT ty => bindOp (TOp.GT (cvtScalarTy ty))
296 :     | Op.GTE ty => bindOp (TOp.GTE (cvtScalarTy ty))
297 :     | Op.Not => bindOp TOp.Not
298 :     | Op.Abs ty => bindOp (TOp.Abs (cvtScalarTy ty))
299 :     | Op.Max ty => bindOp (TOp.Max (cvtScalarTy ty))
300 :     | Op.Min ty => bindOp (TOp.Min (cvtScalarTy ty))
301 :     | Op.RClamp => bindOp TOp.RClamp
302 :     | Op.RLerp => bindOp TOp.RLerp
303 :     | Op.VAdd _ => bindVOp TOp.VAdd
304 :     | Op.VSub _ => bindVOp TOp.VSub
305 : jhr 3855 | Op.VScale _ => let
306 :     val [s, v] = args
307 : jhr 3948 val (s, stms) = simpleArg env (s, [])
308 :     val (layout, vs, stms') = vectorArg (env, v)
309 : jhr 3880 val exps = mkArgs (fn (w, p, x) => (TOp.VScale(w, p), [s, x])) (layout, vs)
310 : jhr 3855 in
311 : jhr 3948 (Env.VEC(layout, exps), stms@stms')
312 : jhr 3855 end
313 : jhr 3834 | Op.VMul _ => bindVOp TOp.VMul
314 :     | Op.VNeg _ => bindVOp TOp.VNeg
315 : jhr 3856 | Op.VSum _ => let
316 :     val [v] = args
317 : jhr 3948 val (layout, pieces, stms) = vectorArg (env, v)
318 :     val e::es = mkArgs (fn (w, p, x) => (TOp.VSum(w, p), [x])) (layout, pieces)
319 : jhr 3856 in
320 : jhr 3948 (Env.TREE(List.foldr (fn (e, es) => T.E_Op(TOp.RAdd, [e, es])) e es), stms)
321 : jhr 3856 end
322 : jhr 3844 | Op.VIndex(_, i) => let
323 : jhr 3856 val [v] = args
324 : jhr 3948 val ({wid, pieces, ...}, es, stms) = vectorArg (env, v)
325 : jhr 3880 fun select (_, wid, [w], [e]) = Env.TREE(T.E_Op(TOp.VIndex(wid, w, i), [e]))
326 :     | select (i, wid, w::ws, e::es) =
327 : jhr 3844 if (i < w)
328 : jhr 3880 then Env.TREE(T.E_Op(TOp.VIndex(w, w, i), [e]))
329 :     else select (i-w, wid-w, ws, es)
330 : jhr 3844 | select _ = raise Fail("bogus " ^ Op.toString srcRator)
331 :     in
332 : jhr 3948 (select (i, wid, pieces, es), stms)
333 : jhr 3844 end
334 : jhr 3855 | Op.VClamp n => let
335 :     val [v, lo, hi] = args
336 : jhr 3948 val (layout, vs, stms) = vectorArg (env, v)
337 :     val (lo, stms) = simpleArg env (lo, stms)
338 :     val (hi, stms) = simpleArg env (hi, stms)
339 : jhr 3880 val exps = mkArgs
340 :     (fn (w, p, x) => (TOp.VClamp(w, p), [x, lo, hi]))
341 :     (layout, vs)
342 : jhr 3855 in
343 : jhr 3948 (Env.VEC(layout, exps), List.rev stms)
344 : jhr 3855 end
345 : jhr 3834 | Op.VMapClamp n => bindVOp TOp.VMapClamp
346 :     | Op.VLerp n => bindVOp TOp.VLerp
347 : jhr 3948 | Op.TensorIndex(ty, idxs) => bindOp(TOp.TensorIndex(U.trType ty, idxs))
348 :     | Op.ProjectLast(ty, idxs) => bindOp(TOp.ProjectLast(U.trType ty, idxs))
349 :     | Op.Select(ty, i) => bindOp (TOp.Select(U.trType ty, i))
350 :     | Op.Subscript ty => bindOp (TOp.Subscript(U.trType ty))
351 :     | Op.MkDynamic(ty, n) => bindOp (TOp.MkDynamic(U.trType ty, n))
352 :     | Op.Append ty => bindOp (TOp.Append(U.trType ty))
353 :     | Op.Prepend ty => bindOp (TOp.Prepend(U.trType ty))
354 :     | Op.Concat ty => bindOp (TOp.Concat(U.trType ty))
355 : jhr 3853 | Op.Range => bindOp TOp.Range
356 : jhr 3948 | Op.Length ty => bindOp (TOp.Length(U.trType ty))
357 : jhr 3852 | Op.SphereQuery(ty1, ty2) => raise Fail "FIXME: SphereQuery"
358 : jhr 3834 | Op.Sqrt => bindOp TOp.Sqrt
359 :     | Op.Cos => bindOp TOp.Cos
360 :     | Op.ArcCos => bindOp TOp.ArcCos
361 :     | Op.Sin => bindOp TOp.Sin
362 :     | Op.ArcSin => bindOp TOp.ArcSin
363 :     | Op.Tan => bindOp TOp.Tan
364 :     | Op.ArcTan => bindOp TOp.ArcTan
365 : jhr 3880 | Op.Ceiling 1 => bindOp TOp.RCeiling
366 :     | Op.Ceiling d => bindVOp TOp.VCeiling
367 :     | Op.Floor 1 => bindOp TOp.RFloor
368 :     | Op.Floor d => bindVOp TOp.VFloor
369 :     | Op.Round 1 => bindOp TOp.RRound
370 :     | Op.Round d => bindVOp TOp.VRound
371 :     | Op.Trunc 1 => bindOp TOp.RTrunc
372 :     | Op.Trunc d => bindVOp TOp.VTrunc
373 : jhr 3835 | Op.IntToReal => bindOp TOp.IntToReal
374 : jhr 3880 | Op.RealToInt 1 => bindOp TOp.RealToInt
375 :     | Op.RealToInt d => let
376 :     val layout = Env.layoutVec env d
377 : jhr 3948 val (_, args, stms) = vectorArg (env, hd args)
378 : jhr 3880 in
379 :     case #pieces layout
380 : jhr 3948 of [w] => (Env.TREE(T.E_Op(TOp.VToInt(d, w), args)), stms)
381 : jhr 3880 | _ => raise Fail(concat["FIXME: RealToInt<", Int.toString d, ">"])
382 :     (* end case *)
383 :     end
384 : jhr 3955 (* FIXME: reduction operators
385 : jhr 3834 | Op.R_All ty => ??
386 :     | Op.R_Exists ty => ??
387 :     | Op.R_Max ty => ??
388 :     | Op.R_Min ty => ??
389 :     | Op.R_Sum ty => ??
390 :     | Op.R_Product ty => ??
391 :     | Op.R_Mean ty => ??
392 :     | Op.R_Variance ty => ??
393 : jhr 3844 *)
394 : jhr 3851 | Op.Transform info => bindOp (TOp.Transform info)
395 :     | Op.Translate info => bindOp (TOp.Translate info)
396 :     | Op.ControlIndex(info, ctl, d) => bindOp (TOp.ControlIndex(info, ctl, d))
397 :     | Op.LoadVoxel info => bindOp (TOp.LoadVoxel info)
398 : jhr 3852 | Op.Inside(info, s) => bindOp (TOp.Inside(info, s))
399 : jhr 3835 | Op.ImageDim(info, d) => bindOp(TOp.ImageDim(info, d))
400 : jhr 3834 | Op.MathFn f => bindOp (TOp.MathFn f)
401 : jhr 3844 | rator => raise Fail("bogus operator " ^ Op.toString srcRator)
402 : jhr 3829 (* end case *)
403 :     end
404 :    
405 : jhr 3955 (* FIXME: we need to defer the copy when possible *)
406 :     (* if required, add a TensorCopy operation to the rhs of an assignment *)
407 :     fun mkAssignRHS (TTy.TensorTy _, rhs) = (case TreeTypeOf.exp rhs
408 :     of TTy.TensorRefTy shp => T.E_Op(TOp.TensorCopy shp, [rhs])
409 :     | _ => rhs
410 :     (* end case *))
411 :     | mkAssignRHS (_, rhs) = rhs
412 :    
413 :     fun mkDefn' (x, rhs) = mkDefn (x, mkAssignRHS(TV.ty x, rhs))
414 :     fun mkAssign' (x, rhs) = mkAssign (x, mkAssignRHS(TV.ty x, rhs))
415 :    
416 : jhr 3852 (* cases:
417 :     x in EqClass
418 :     issue assignment; lhs is binding of representative (could be multiple vars)
419 :     useCount(x) > 1 and rhs is not simple
420 :     rhs is simple
421 :     rhs is vector
422 :     *)
423 : jhr 3834 fun trAssign (env, lhs, rhs) = let
424 : jhr 3955 fun getLHS () = (case UnifyVars.eqClassOf lhs of SOME x => x | _ => lhs)
425 :     fun bindRHS rhs = Env.bindVar (env, getLHS(), Env.RHS(U.trType(V.ty lhs), rhs))
426 : jhr 3889 (* binding for the lhs variable, where the rhs is a simple expression. We check to
427 :     * see if it is part of an merged equivalence class, in which case we need to generate
428 :     * assigment(s)
429 : jhr 3836 *)
430 : jhr 3853 fun bindSimple rhs = (case eqClassRepOf(env, lhs)
431 : jhr 3948 of NOEQ => (Env.bindSimple (env, lhs, rhs); [])
432 : jhr 3955 | VAR x' => [mkAssign' (x', rhs)]
433 : jhr 3889 | VEC xs' => (case V.ty lhs
434 :     of Ty.TensorTy[d] => let
435 :     val layout = Env.layoutVec env d
436 :     in
437 :     List.mapi
438 :     (fn (i, x') => mkAssign(x', T.E_VLoad(layout, rhs, i)))
439 :     xs'
440 :     end
441 :     | _ => raise Fail "inconsistent"
442 :     (* end case *))
443 : jhr 3836 (* end case *))
444 : jhr 3948 fun assignOp (rator, args) = let
445 :     val (args, stms) = simpleArgs (env, args)
446 :     in
447 :     case eqClassRepOf(env, lhs)
448 :     of NOEQ => (
449 :     bindRHS (T.E_Op(rator, args));
450 :     stms)
451 : jhr 3955 | VAR x' => stms @ [mkAssign' (x', T.E_Op(rator, args))]
452 : jhr 3948 | VEC _ => raise Fail ("unexpected VEC for lhs " ^ V.toString lhs)
453 :     (* end case *)
454 :     end
455 : jhr 3834 in
456 :     case rhs
457 : jhr 3844 of IR.GLOBAL x => bindSimple (T.E_Global(mkGlobalVar x))
458 : jhr 3852 | IR.STATE(NONE, fld) =>
459 :     bindSimple (T.E_State(NONE, getStateVar fld))
460 : jhr 3948 | IR.STATE(SOME x, fld) => let
461 :     val (arg, stms) = simpleArg env (x, [])
462 :     val stms' = bindSimple (T.E_State(SOME arg, getStateVar fld))
463 :     in
464 :     stms' @ stms
465 :     end
466 : jhr 3854 | IR.VAR x => raise Fail "FIXME: VAR"
467 : jhr 3850 | IR.LIT lit => bindSimple (T.E_Lit lit)
468 : jhr 3852 | IR.OP(Op.EigenVecs2x2, args) => assignOp (TOp.EigenVecs2x2, args)
469 :     | IR.OP(Op.EigenVecs3x3, args) => assignOp (TOp.EigenVecs3x3, args)
470 :     | IR.OP(Op.EigenVals2x2, args) => assignOp (TOp.EigenVals2x2, args)
471 :     | IR.OP(Op.EigenVals3x3, args) => assignOp (TOp.EigenVals3x3, args)
472 : jhr 3886 | IR.OP(Op.Zero(ty as Ty.TensorTy dd), []) => let
473 : jhr 3887 val z = T.E_Lit(Literal.Real(RealLit.zero false))
474 : jhr 3886 val sz = List.foldl Int.* 1 dd
475 : jhr 3948 in
476 :     bindRHS (T.E_Cons(List.tabulate(sz, fn _ => z), U.trType ty));
477 :     []
478 :     end
479 : jhr 3886 | IR.OP(Op.LoadSeq(ty, file), []) => let
480 : jhr 3955 val lhs = newLocal (env, getLHS ())
481 : jhr 3886 in
482 : jhr 3948 [T.S_LoadNrrd(lhs, U.toAPIType ty, file)]
483 : jhr 3886 end
484 :     | IR.OP(Op.LoadImage(ty, file), []) => let
485 : jhr 3955 val lhs = newLocal (env, getLHS ())
486 : jhr 3886 in
487 : jhr 3948 [T.S_LoadNrrd(lhs, U.toAPIType ty, file)]
488 : jhr 3886 end
489 : jhr 3852 | IR.OP(rator, args) => let
490 : jhr 3948 val (rhs, stms) = trOp (env, rator, args)
491 : jhr 3852 in
492 : jhr 3948 case (rhs, eqClassRepOf(env, lhs), V.useCount lhs > 1)
493 :     of (_, NOEQ, false) => (Env.bindVar (env, lhs, rhs); stms)
494 : jhr 3955 | (Env.TREE e, NOEQ, true) => mkDefn'(newLocal(env, lhs), e) :: stms
495 :     | (Env.TREE e, VAR x', _) => mkAssign'(x', e) :: stms
496 : jhr 3854 | (Env.VEC(layout, es), NOEQ, true) => let
497 : jhr 3948 val vs = U.newVectorVars layout
498 : jhr 3854 in
499 : jhr 3948 Env.bindVar (env, lhs, Env.VEC(layout, List.map T.E_Var vs));
500 :     ListPair.foldl (fn (v, e, stms) => mkDefn(v, e)::stms) stms (vs, es)
501 : jhr 3854 end
502 : jhr 3948 | (Env.VEC(layout, es), VEC xs, _) =>
503 :     ListPair.foldl (fn (x, e, stms) => mkAssign(x, e)::stms) stms (xs, es)
504 : jhr 3854 | _ => raise Fail "inconsistent"
505 :     (* end case *)
506 : jhr 3852 end
507 :     | IR.CONS(args, Ty.TensorTy[d]) => let
508 : jhr 3854 val layout = Env.layoutVec env d
509 : jhr 3948 val (args, stms) = simpleArgs (env, args)
510 : jhr 3854 fun mkVecs (args, w::ws) = let
511 : jhr 3858 (* take arguments from args to build a vector value of width w; pad as
512 :     * necessary.
513 :     *)
514 : jhr 3889 fun take (0, args, es) = T.E_Vec(w, w, List.rev es) :: mkVecs (args, ws)
515 : jhr 3855 | take (i, [], es) = if #padded layout andalso null ws
516 : jhr 3889 then [T.E_Vec(w-i, w, List.rev es)]
517 : jhr 3855 else raise Fail "too few arguments for CONS"
518 : jhr 3948 | take (i, arg::args, es) = take (i-1, args, arg :: es)
519 : jhr 3854 in
520 :     take (w, args, [])
521 :     end
522 :     | mkVecs ([], []) = []
523 :     | mkVecs (_, []) = raise Fail "too many arguments for CONS"
524 :     val es = mkVecs (args, #pieces layout)
525 : jhr 3852 in
526 : jhr 3854 case (eqClassRepOf(env, lhs), V.useCount lhs > 1)
527 : jhr 3948 of (NOEQ, false) => (Env.bindVar(env, lhs, Env.VEC(layout, es)); stms)
528 : jhr 3854 | (NOEQ, true) => let
529 : jhr 3948 val vs = U.newVectorVars layout
530 : jhr 3854 in
531 : jhr 3948 Env.bindVar (env, lhs, Env.VEC(layout, List.map T.E_Var vs));
532 :     ListPair.foldl (fn (v, e, stms) => mkDefn(v, e)::stms) stms (vs, es)
533 : jhr 3854 end
534 : jhr 3948 | (VEC xs, _) =>
535 :     ListPair.foldl (fn (x, e, stms) => mkAssign(x, e)::stms) stms (xs, es)
536 : jhr 3854 | _ => raise Fail "inconsistent"
537 :     (* end case *)
538 : jhr 3852 end
539 : jhr 3941 | IR.CONS(args, ty) => let
540 :     val (es, stms) = singleArgs (env, args)
541 : jhr 3948 val ty = U.trType ty
542 : jhr 3941 in
543 : jhr 3955 Env.bindVar (env, getLHS (), Env.RHS(ty, T.E_Cons(es, ty)));
544 : jhr 3948 stms
545 : jhr 3941 end
546 :     | IR.SEQ(args, ty) => let
547 :     val (es, stms) = singleArgs (env, args)
548 : jhr 3948 val ty = U.trType ty
549 : jhr 3941 in
550 : jhr 3955 Env.bindVar (env, getLHS (), Env.RHS(ty, T.E_Seq(es, ty)));
551 : jhr 3948 stms
552 : jhr 3941 end
553 : jhr 3852 | rhs => raise Fail(concat["unexpected ", IR.RHS.toString rhs, " in LowIR code"])
554 : jhr 3834 (* end case *)
555 :     end
556 : jhr 3856 handle ex => (
557 :     print(concat["trAssign: ", V.toString lhs, " = ", IR.RHS.toString rhs, "\n"]);
558 :     raise ex)
559 : jhr 3834
560 :     (* In order to reconstruct the block-structure from the CFG, we keep a stack of open ifs.
561 :     * the items on this stack distinguish between when we are processing the then and else
562 :     * branches of the if.
563 :     *)
564 :     datatype open_if
565 :     (* working on the "then" branch. The fields are statments that preceed the if, the condition,
566 :     * and the else-branch node.
567 :     *)
568 :     = THEN_BR of T.stm list * T.exp * IR.node
569 :     (* working on the "else" branch. The fields are statments that preceed the if, the condition,
570 :     * the "then" branch statements, and the node kind that terminated the "then" branch (will be
571 :     * a JOIN or EXIT(DIE, STABILIZE, or UNREACHABLE)).
572 :     *)
573 :     | ELSE_BR of T.stm list * T.exp * T.stm list * IR.node_kind
574 :    
575 : jhr 3852 fun trCFGWithEnv (env, cfg) = let
576 : jhr 3856 fun useScalar x = (case useVar env x
577 : jhr 3948 of Env.RHS(_, e) => e
578 :     | Env.TREE e => e
579 : jhr 3835 | _ => raise Fail("expected scalar binding for " ^ V.toString x)
580 :     (* end case *))
581 : jhr 3834 val _ = UnifyVars.analyze cfg
582 : jhr 3851 (* join (stk, stms, k): handle a control-flow join, where env is the
583 : jhr 3834 * current environment, stk is the stack of open ifs (the top of stk specifies
584 :     * which branch we are in), stms are the TreeIL statements preceding the join
585 :     * on the current path, and k is the kind of the join node (either JOIN or EXIT).
586 :     *)
587 : jhr 3851 fun join ([], _, IR.JOIN _) = raise Fail "JOIN with no open if"
588 : jhr 3852 | join ([], stms, _) = mkBlock (List.rev stms)
589 : jhr 3851 | join (THEN_BR(stms1, cond, elseBr)::stk, thenBlk, k) = let
590 :     val thenBlk = Env.flushPending (env, thenBlk)
591 : jhr 3834 in
592 : jhr 3851 doNode (elseBr, ELSE_BR(stms1, cond, thenBlk, k)::stk, [])
593 : jhr 3834 end
594 : jhr 3851 | join (ELSE_BR(stms, cond, thenBlk, k1)::stk, elseBlk, k2) = let
595 :     val elseBlk = Env.flushPending (env, elseBlk)
596 : jhr 3834 in
597 :     case (k1, k2)
598 : jhr 3848 of (IR.JOIN{succ, ...}, IR.JOIN _) => let
599 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
600 :     in
601 : jhr 3851 doNode (!succ, stk, stm::stms)
602 : jhr 3834 end
603 : jhr 3848 | (IR.JOIN{succ, ...}, _) => let
604 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
605 :     in
606 : jhr 3851 doNode (!succ, stk, stm::stms)
607 : jhr 3834 end
608 : jhr 3848 | (_, IR.JOIN{succ, ...}) => let
609 : jhr 3834 val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
610 :     in
611 : jhr 3851 doNode (!succ, stk, stm::stms)
612 : jhr 3834 end
613 :     | (_, _) => let
614 :     val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
615 :     in
616 : jhr 3852 mkBlock (List.rev(stm::stms))
617 : jhr 3834 end
618 :     (* end case *)
619 :     end
620 : jhr 3851 and doNode (nd : IR.node, ifStk : open_if list, stms) = (case IR.Node.kind nd
621 : jhr 3834 of IR.NULL => raise Fail "unexpected NULL"
622 : jhr 3851 | IR.ENTRY{succ} => doNode (!succ, ifStk, stms)
623 :     | k as IR.JOIN _ => join (ifStk, stms, k)
624 : jhr 3834 | IR.COND{cond, trueBranch, falseBranch, ...} => let
625 : jhr 3835 val cond = useScalar (!cond)
626 : jhr 3851 val stms = Env.flushPending (env, stms)
627 : jhr 3834 in
628 : jhr 3851 doNode (!trueBranch, THEN_BR(stms, cond, !falseBranch)::ifStk, [])
629 : jhr 3834 end
630 : jhr 3835 | IR.FOREACH{var, src, bodyEntry, succ, ...} => let
631 : jhr 3948 val var' = U.newIterVar var
632 :     val _ = Env.bindSimple (env, var, T.E_Var var')
633 : jhr 3924 (* note that the flatten phase on LowIR should guarantee that each
634 :     * FOREACH node that takes a Range argument has its own instance of
635 :     * the Range expression.
636 : jhr 3948 * Note also that we resolve the src scalar before processing the body
637 :     * of the loop so that any nested flushPendings do not cause Range
638 :     * bindings to be flushed.
639 : jhr 3924 *)
640 : jhr 3948 val mkStm = (case useScalar(!src)
641 :     of T.E_Op(TOp.Range, [lo, hi]) =>
642 :     (fn body => T.S_For(var', lo, hi, body))
643 :     | e =>
644 :     (fn body => T.S_Foreach(var', e, body))
645 : jhr 3924 (* end case *))
646 :     val stms = Env.flushPending (env, stms)
647 : jhr 3948 val stm = mkStm (doNode (!bodyEntry, [], []))
648 : jhr 3834 in
649 : jhr 3851 doNode (!succ, ifStk, stm::stms)
650 : jhr 3834 end
651 : jhr 3848 | IR.NEXT _ => mkBlock (List.rev stms)
652 : jhr 3834 | IR.COM {text, succ, ...} =>
653 : jhr 3851 doNode (!succ, ifStk, T.S_Comment text :: stms)
654 : jhr 3834 | IR.ASSIGN{stm=(lhs, rhs), succ, ...} => let
655 : jhr 3852 val stms' = trAssign (env, lhs, rhs)
656 : jhr 3834 in
657 : jhr 3851 doNode (!succ, ifStk, stms' @ stms)
658 : jhr 3834 end
659 : jhr 3866 | IR.MASSIGN{stm=([], Op.Print tys, xs), succ, ...} => let
660 : jhr 3941 val (es, stms') = singleArgs (env, xs)
661 : jhr 3948 val stm = T.S_Print(List.map U.trType tys, es)
662 : jhr 3848 in
663 : jhr 3941 doNode (!succ, ifStk, stm :: List.revAppend (stms', stms))
664 : jhr 3848 end
665 : jhr 3866 | IR.MASSIGN{stm=(ys, rator, xs), succ, ...} => raise Fail(concat[
666 :     "unexepected operator ", Op.toString rator, " for MASSIGN"
667 :     ])
668 : jhr 3834 | IR.GASSIGN{lhs, rhs, succ, ...} => let
669 : jhr 3948 val gv = mkGlobalVar lhs
670 : jhr 3955 fun mkGAssign (gv, e) = T.S_GAssign(gv, mkAssignRHS(TGV.ty gv, e))
671 : jhr 3948 val stm = (case useVar env rhs
672 : jhr 3955 of Env.RHS(_, e) => mkGAssign(gv, e)
673 :     | Env.TREE e => mkGAssign(gv, e)
674 : jhr 3948 | Env.VEC(layout, es) => let
675 :     val tmp = U.newTempVar("_arg", TTy.TensorTy[#wid layout])
676 :     in
677 :     T.S_GAssign(gv, T.E_Pack(layout, es))
678 :     end
679 :     (* end case *))
680 : jhr 3834 in
681 : jhr 3948 doNode (!succ, ifStk, stm :: stms)
682 : jhr 3834 end
683 : jhr 3851 | IR.NEW{strand, args, succ, ...} => let
684 : jhr 3941 val (es, stms') = singleArgs (env, args)
685 :     val stm = T.S_New(strand, es)
686 : jhr 3851 in
687 : jhr 3941 doNode (!succ, ifStk, stm :: List.revAppend (stms', stms))
688 : jhr 3851 end
689 : jhr 3834 | IR.SAVE{lhs, rhs, succ, ...} => let
690 : jhr 3948 val sv = getStateVar lhs
691 : jhr 3955 fun mkSAssign (sv, e) = T.S_Save(sv, mkAssignRHS(TSV.ty sv, e))
692 : jhr 3948 val stm = (case useVar env rhs
693 : jhr 3955 of Env.RHS(_, e) => mkSAssign(sv, e)
694 :     | Env.TREE e => mkSAssign(sv, e)
695 : jhr 3948 | Env.VEC(layout, es) => let
696 :     val tmp = U.newTempVar("_arg", TTy.TensorTy[#wid layout])
697 :     in
698 :     T.S_Save(sv, T.E_Pack(layout, es))
699 :     end
700 :     (* end case *))
701 : jhr 3834 in
702 : jhr 3948 doNode (!succ, ifStk, stm :: stms)
703 : jhr 3834 end
704 :     | k as IR.EXIT{kind, succ, ...} => (case (!succ, kind)
705 : jhr 3852 of (NONE, ExitKind.RETURN) => mkBlock (List.rev(T.S_Exit :: stms))
706 :     | (NONE, ExitKind.ACTIVE) => mkBlock (List.rev(T.S_Active :: stms))
707 : jhr 3834 | (NONE, ExitKind.STABILIZE) => let
708 :     val stms = T.S_Stabilize :: stms
709 :     in
710 : jhr 3851 join (ifStk, stms, k)
711 : jhr 3834 end
712 : jhr 3851 | (NONE, ExitKind.DIE) => join (ifStk, T.S_Die :: stms, k)
713 :     | (NONE, ExitKind.UNREACHABLE) => join (ifStk, stms, k)
714 :     | (SOME nd, ExitKind.ACTIVE) => doNode (nd, ifStk, T.S_Active :: stms)
715 :     | (SOME nd, ExitKind.STABILIZE) => doNode (nd, ifStk, T.S_Stabilize :: stms)
716 :     | (SOME nd, ExitKind.DIE) => doNode (nd, ifStk, T.S_Die :: stms)
717 :     | (SOME nd, ExitKind.UNREACHABLE) => doNode (nd, ifStk, stms)
718 : jhr 3834 | _ => raise Fail("unexpected continuation edge from "^IR.Node.toString nd)
719 :     (* end case *))
720 :     (* end case *))
721 :     in
722 : jhr 3857 doNode (IR.CFG.entry cfg, [], [])
723 : jhr 3834 end
724 :    
725 : jhr 3857 fun trCFG info cfg = ScopeVars.assignScopes ([], trCFGWithEnv (Env.new info, cfg))
726 : jhr 3835
727 : jhr 3953 (* Build a strand method from a TreeIR block. We need to check for language features
728 :     * that require the world pointer (e.g., printing) and for references to global variables.
729 :     *)
730 : jhr 3952 fun mkMethod body = let
731 :     fun chkBlock (T.Block{body, ...}, uG, nW) =
732 :     chkStms (body, uG, nW)
733 :     and chkStms ([], uG, nW) = (uG, nW)
734 :     | chkStms (stm::stms, uG, nW) = let
735 :     fun next (true, true) = (true, true)
736 :     | next (uG, nW) = chkStms (stms, uG, nW)
737 :     in
738 :     case stm
739 :     of T.S_Assign(_, _, e) => next (chkExp (e, uG, nW))
740 :     | T.S_MAssign(_, e) => next (chkExp (e, uG, nW))
741 :     | T.S_GAssign(_, e) => next (chkExp (e, true, nW))
742 :     | T.S_IfThen(e, blk) => let
743 :     val (uG, nW) = chkExp (e, uG, nW)
744 :     in
745 :     chkBlock (blk, uG, nW)
746 :     end
747 :     | T.S_IfThenElse(e, blk1, blk2) => let
748 :     val (uG, nW) = chkExp (e, uG, nW)
749 :     val (uG, nW) = chkBlock (blk1, uG, nW)
750 :     in
751 :     chkBlock (blk2, uG, nW)
752 :     end
753 :     | T.S_For(_, e1, e2, blk) => let
754 :     val (uG, nW) = chkExp (e1, uG, nW)
755 :     val (uG, nW) = chkExp (e2, uG, nW)
756 :     in
757 :     chkBlock (blk, uG, nW)
758 :     end
759 :     | T.S_Foreach(_, e, blk) => let
760 :     val (uG, nW) = chkExp (e, uG, nW)
761 :     in
762 :     chkBlock (blk, uG, nW)
763 :     end
764 :     | T.S_LoadNrrd _ => raise Fail "unexpected LoadNrrd"
765 :     | T.S_Input _ => raise Fail "unexpected Input"
766 :     | T.S_InputNrrd _ => raise Fail "unexpected InputNrrd"
767 :     | T.S_New(_, es) => next (chkExps (es, uG, true))
768 :     | T.S_Save(_, e) => next (chkExp (e, uG, nW))
769 :     | T.S_Print(_, es) => next (chkExps (es, uG, true))
770 :     | _ => next (uG, nW)
771 :     (* end case *)
772 :     end
773 :     and chkExps ([], uG, nW) = (uG, nW)
774 :     | chkExps (_, true, true) = (true, true)
775 :     | chkExps (e::es, uG, nW) = let
776 :     val (uG, nW) = chkExp (e, uG, nW)
777 :     in
778 :     chkExps (es, uG, nW)
779 :     end
780 :     and chkExp (T.E_Global _, uG, nW) = (true, nW)
781 :     | chkExp (T.E_State(SOME e, _), uG, nW) =
782 :     chkExp (e, uG, nW)
783 :     | chkExp (T.E_Op(TOp.SphereQuery _, es), uG, nW) = chkExps (es, uG, true)
784 :     | chkExp (T.E_Op(_, es), uG, nW) = chkExps (es, uG, nW)
785 :     | chkExp (T.E_Vec(_, _, es), uG, nW) = chkExps (es, uG, nW)
786 :     | chkExp (T.E_Cons(es, _), uG, nW) = chkExps (es, uG, nW)
787 :     | chkExp (T.E_Seq(es, _), uG, nW) = chkExps (es, uG, nW)
788 :     | chkExp (T.E_Pack(_, es), uG, nW) = chkExps (es, uG, nW)
789 :     | chkExp (T.E_VLoad(_, e, _), uG, nW) = chkExp (e, uG, nW)
790 :     | chkExp (_, uG, nW) = (uG, nW)
791 :     val (usesGlobals, needsWorld) = chkBlock (body, false, false)
792 :     in
793 :     T.Method{usesGlobals = usesGlobals, needsWorld = needsWorld, body = body}
794 :     end
795 :    
796 : jhr 3851 fun trStrand info strand = let
797 : jhr 3952 val trMethod = mkMethod o trCFG info
798 : jhr 3835 val IR.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
799 : jhr 3948 val params' = List.map U.newParamVar params
800 : jhr 3835 val state' = List.map getStateVar state
801 : jhr 3857 val stateInit' = let
802 :     val env = Env.new info
803 :     in
804 :     ListPair.appEq
805 : jhr 3948 (fn (x, x') => Env.bindSimple (env, x, T.E_Var x'))
806 : jhr 3857 (params, params');
807 : jhr 3952 mkMethod (ScopeVars.assignScopes (params', trCFGWithEnv (env, stateInit)))
808 : jhr 3857 end
809 : jhr 3835 in
810 :     T.Strand{
811 :     name = name,
812 :     params = params',
813 :     state = state',
814 : jhr 3857 stateInit = stateInit',
815 : jhr 3952 initM = Option.map trMethod initM,
816 :     updateM = trMethod updateM,
817 :     stabilizeM = Option.map trMethod stabilizeM
818 : jhr 3835 }
819 :     end
820 :    
821 : jhr 3851 fun translate (prog, info) = let
822 : jhr 3879 (* first step is to flatten any nested CONS nodes *)
823 :     val prog = Flatten.transform prog
824 : jhr 3834 val LowIR.Program{
825 :     props, consts, inputs, constInit, globals, globalInit, strand, create, update
826 :     } = prog
827 : jhr 3852 val trCFG = trCFG info
828 : jhr 3834 in
829 :     TreeIR.Program{
830 :     props = props,
831 : jhr 3861 target = info,
832 : jhr 3852 consts = List.map mkGlobalVar consts,
833 :     inputs = List.map (Inputs.map mkGlobalVar) inputs,
834 :     constInit = trCFG constInit,
835 :     globals = List.map mkGlobalVar globals,
836 :     globalInit = trCFG globalInit,
837 : jhr 3851 strand = trStrand info strand,
838 : jhr 3852 create = let
839 :     val IR.Create{dim, code} = create
840 :     in
841 :     T.Create{dim = dim, code = trCFG code}
842 :     end,
843 :     update = Option.map trCFG update
844 : jhr 3834 }
845 :     end
846 :    
847 : jhr 3828 end

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