Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis12/src/compiler/tree-il/low-to-tree-fn.sml
ViewVC logotype

Annotation of /branches/vis12/src/compiler/tree-il/low-to-tree-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2023 - (view) (download)

1 : jhr 1115 (* low-to-tree-fn.sml
2 :     *
3 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * This module translates the LowIL representation of a program (i.e., a pure CFG) to
7 :     * a block-structured AST with nested expressions.
8 :     *
9 :     * NOTE: this translation is pretty dumb about variable coalescing (i.e., it doesn't do any).
10 :     *)
11 :    
12 :     functor LowToTreeFn (Target : sig
13 :    
14 : jhr 1640 val supportsPrinting : unit -> bool (* does the target support the Print op? *)
15 :    
16 : jhr 1115 (* tests for whether various expression forms can appear inline *)
17 :     val inlineCons : int -> bool (* can n'th-order tensor construction appear inline *)
18 :     val inlineMatrixExp : bool (* can matrix-valued expressions appear inline? *)
19 :    
20 :     end) : sig
21 :    
22 :     val translate : LowIL.program -> TreeIL.program
23 :    
24 :     end = struct
25 :    
26 :     structure IL = LowIL
27 :     structure Ty = LowILTypes
28 :     structure V = LowIL.Var
29 : jhr 1640 structure StV = LowIL.StateVar
30 : jhr 1115 structure Op = LowOps
31 :     structure Nd = LowIL.Node
32 :     structure CFG = LowIL.CFG
33 :     structure T = TreeIL
34 :     structure VA = VarAnalysis
35 : jhr 2012 structure InP = Inputs
36 : jhr 1115
37 :     (* create new tree IL variables *)
38 :     local
39 :     fun newVar (name, kind, ty) = T.V{
40 :     name = name,
41 :     id = Stamp.new(),
42 :     kind = kind,
43 :     ty = ty
44 :     }
45 :     val cnt = ref 0
46 :     fun genName prefix = let
47 :     val n = !cnt
48 :     in
49 :     cnt := n+1;
50 :     String.concat[prefix, "_", Int.toString n]
51 :     end
52 :     in
53 : jhr 1806 fun newInput x = newVar (V.name x, T.VK_Input, V.ty x)
54 :     fun newGlobal x = newVar (V.name x, T.VK_Global, V.ty x)
55 : jhr 1115 fun newParam x = newVar (genName("p_" ^ V.name x), T.VK_Local, V.ty x)
56 :     fun newLocal x = newVar (genName("l_" ^ V.name x), T.VK_Local, V.ty x)
57 :     fun newIter x = newVar (genName("i_" ^ V.name x), T.VK_Local, V.ty x)
58 :     end
59 :    
60 : jhr 1640 (* associate Tree IL state variables with Low IL variables using properties *)
61 :     local
62 :     fun mkStateVar x = T.SV{
63 :     name = StV.name x,
64 :     id = Stamp.new(),
65 :     ty = StV.ty x,
66 :     varying = VA.isVarying x,
67 :     output = StV.isOutput x
68 :     }
69 :     in
70 :     val {getFn = getStateVar, ...} = StV.newProp mkStateVar
71 :     end
72 :    
73 : jhr 1115 fun mkBlock stms = T.Block{locals=[], body=stms}
74 :     fun mkIf (x, stms, []) = T.S_IfThen(x, mkBlock stms)
75 :     | mkIf (x, stms1, stms2) = T.S_IfThenElse(x, mkBlock stms1, mkBlock stms2)
76 :    
77 :     (* an environment that tracks bindings of variables to target expressions and the list
78 :     * of locals that have been defined.
79 :     *)
80 :     local
81 :     structure VT = V.Tbl
82 :     fun decCount (IL.V{useCnt, ...}) = let
83 :     val n = !useCnt - 1
84 :     in
85 :     useCnt := n; (n <= 0)
86 :     end
87 :     datatype target_binding
88 :     = GLOB of T.var (* variable is global *)
89 :     | TREE of T.exp (* variable bound to target expression tree *)
90 :     | DEF of T.exp (* either a target variable or constant for a defined variable *)
91 :     datatype env = E of {
92 :     tbl : target_binding VT.hash_table,
93 :     locals : T.var list
94 :     }
95 :     in
96 :     (* DEBUG *)
97 :     fun bindToString binding = (case binding
98 :     of GLOB y => "GLOB " ^ T.Var.name y
99 :     | TREE e => "TREE"
100 :     | DEF(T.E_Var y) => "DEF " ^ T.Var.name y
101 :     | DEF e => "DEF"
102 :     (* end case *))
103 :     fun dumpEnv (E{tbl, ...}) = let
104 :     fun prEntry (x, binding) =
105 :     print(concat[" ", IL.Var.toString x, " --> ", bindToString binding, "\n"])
106 :     in
107 :     print "*** dump environment\n";
108 :     VT.appi prEntry tbl;
109 :     print "***\n"
110 :     end
111 :     (* DEBUG *)
112 :    
113 :     fun newEnv () = E{tbl = VT.mkTable (512, Fail "tbl"), locals=[]}
114 :    
115 :     (* use a variable. If it is a pending expression, we remove it from the table *)
116 :     fun useVar (env as E{tbl, ...}) x = (case VT.find tbl x
117 :     of SOME(GLOB x') => T.E_Var x'
118 :     | SOME(TREE e) => (
119 :     (*print(concat["useVar ", V.toString x, " ==> TREE\n"]);*)
120 :     ignore(VT.remove tbl x);
121 :     e)
122 :     | SOME(DEF e) => (
123 :     (*print(concat["useVar ", V.toString x, " ==> ", bindToString(DEF e), "; use count = ", Int.toString(V.useCount x), "\n"]);*)
124 :     (* if this is the last use of x, then remove it from the table *)
125 :     if (decCount x) then ignore(VT.remove tbl x) else ();
126 :     e)
127 :     | NONE => (
128 :     dumpEnv env;
129 :     raise Fail(concat ["useVar(", V.toString x, ")"])
130 :     )
131 :     (* end case *))
132 :    
133 :     (* record a local variable *)
134 :     fun addLocal (E{tbl, locals}, x) = E{tbl=tbl, locals=x::locals}
135 :    
136 :     fun global (E{tbl, ...}, x, x') = VT.insert tbl (x, GLOB x')
137 :    
138 :     (* insert a pending expression into the table. Note that x should only be used once! *)
139 :     fun insert (env as E{tbl, ...}, x, exp) = (
140 :     VT.insert tbl (x, TREE exp);
141 :     env)
142 :    
143 :     fun rename (env as E{tbl, ...}, x, x') = (
144 :     VT.insert tbl (x, DEF(T.E_Var x'));
145 :     env)
146 :    
147 :     fun peekGlobal (E{tbl, ...}, x) = (case VT.find tbl x
148 :     of SOME(GLOB x') => SOME x'
149 :     | _ => NONE
150 :     (* end case *))
151 :    
152 :     fun bindLocal (env, lhs, rhs) = if (V.useCount lhs = 1)
153 :     then (insert(env, lhs, rhs), [])
154 :     else let
155 :     val t = newLocal lhs
156 :     in
157 : jhr 1640 (rename(addLocal(env, t), lhs, t), [T.S_Assign([t], rhs)])
158 : jhr 1115 end
159 :    
160 :     fun bind (env, lhs, rhs) = (case peekGlobal (env, lhs)
161 : jhr 1640 of SOME x => (env, [T.S_Assign([x], rhs)])
162 : jhr 1115 | NONE => bindLocal (env, lhs, rhs)
163 :     (* end case *))
164 :    
165 :     (* set the definition of a variable, where the RHS is either a literal constant or a variable *)
166 :     fun bindSimple (env as E{tbl, ...}, lhs, rhs) = (
167 :     case peekGlobal (env, lhs)
168 : jhr 1640 of SOME x => (env, [T.S_Assign([x], rhs)])
169 : jhr 1115 | NONE => (VT.insert tbl (lhs, DEF rhs); (env, []))
170 :     (* end case *))
171 :    
172 :     (* at the end of a block, we need to assign any pending expressions to locals. The
173 :     * blkStms list and the resulting statement list are in reverse order.
174 :     *)
175 :     fun flushPending (E{tbl, locals}, blkStms) = let
176 :     fun doVar (x, TREE e, (locals, stms)) = let
177 :     val t = newLocal x
178 :     in
179 :     VT.insert tbl (x, DEF(T.E_Var t));
180 : jhr 1640 (t::locals, T.S_Assign([t], e)::stms)
181 : jhr 1115 end
182 :     | doVar (_, _, acc) = acc
183 :     val (locals, stms) = VT.foldi doVar (locals, blkStms) tbl
184 :     in
185 :     (E{tbl=tbl, locals=locals}, stms)
186 :     end
187 :    
188 :     fun doPhi ((lhs, rhs), (env, predBlks : T.stm list list)) = let
189 :     (* t will be the variable in the continuation of the JOIN *)
190 :     val t = newLocal lhs
191 :     val predBlks = ListPair.map
192 : jhr 1640 (fn (x, stms) => T.S_Assign([t], useVar env x)::stms)
193 : jhr 1115 (rhs, predBlks)
194 :     in
195 :     (rename (addLocal(env, t), lhs, t), predBlks)
196 :     end
197 :    
198 :     fun endScope (E{locals, ...}, stms) = T.Block{
199 :     locals = List.rev locals,
200 :     body = stms
201 :     }
202 :    
203 :     end
204 :    
205 :     (* Certain IL operators cannot be compiled to inline expressions. Return
206 :     * false for those and true for all others.
207 :     *)
208 :     fun isInlineOp rator = let
209 :     fun chkTensorTy (Ty.TensorTy[]) = true
210 :     | chkTensorTy (Ty.TensorTy[_]) = true
211 : jhr 1958 | chkTensorTy (Ty.TensorTy[_, _]) = Target.inlineMatrixExp
212 :     | chkTensorTy _ = false
213 : jhr 1115 in
214 :     case rator
215 :     of Op.LoadVoxels(_, 1) => true
216 :     | Op.LoadVoxels _ => false
217 :     | Op.Add ty => chkTensorTy ty
218 :     | Op.Sub ty => chkTensorTy ty
219 :     | Op.Neg ty => chkTensorTy ty
220 :     | Op.Scale ty => chkTensorTy ty
221 :     | Op.MulMatMat _ => Target.inlineMatrixExp
222 : jhr 1939 | Op.MulVecTen3 _ => false
223 :     | Op.MulTen3Vec _ => false
224 : jhr 1945 | Op.ColonMul(Ty.TensorTy dd1, Ty.TensorTy dd2) =>
225 :     (* if the result is a vector or scalar, then it is inline *)
226 :     (List.length dd1 + List.length dd2 > 5)
227 : jhr 1640 | Op.EigenVecs2x2 => false
228 :     | Op.EigenVecs3x3 => false
229 :     | Op.EigenVals2x2 => false
230 :     | Op.EigenVals3x3 => false
231 : jhr 1115 | Op.Identity _ => Target.inlineMatrixExp
232 :     | Op.Zero _ => Target.inlineMatrixExp
233 :     | Op.TensorToWorldSpace(_, ty) => chkTensorTy ty
234 :     | _ => true
235 :     (* end case *)
236 :     end
237 :    
238 : jhr 2023 (* translate input-variable initialization to a TreeIL expression *)
239 :     fun trInitialization (InP.String s) = ([], T.E_Lit(Literal.String s))
240 :     | trInitialization (InP.Int n) = ([], T.E_Lit(Literal.Int n))
241 :     | trInitialization (InP.Real f) = ([], T.E_Lit(Literal.Float f))
242 :     | trInitialization (InP.Bool b) = ([], T.E_Lit(Literal.Bool b))
243 :     | trInitialization (InP.Tensor(shp, vs)) = raise Fail "trInitialization: Tensor"
244 :     | trInitialization (InP.Seq vs) = raise Fail "trInitialization: Seq"
245 :     | trInitialization _ = raise Fail "trInitialization: impossible"
246 :    
247 : jhr 1115 (* translate a LowIL assignment to a list of zero or more target statements *)
248 :     fun doAssign (env, (lhs, rhs)) = let
249 :     fun doLHS () = (case peekGlobal(env, lhs)
250 :     of SOME lhs' => (env, lhs')
251 :     | NONE => let
252 :     val t = newLocal lhs
253 :     in
254 :     (rename (addLocal(env, t), lhs, t), t)
255 :     end
256 :     (* end case *))
257 :     (* for expressions that are going to be compiled to a call statement *)
258 :     fun assignExp (env, exp) = let
259 :     (* operations that return matrices may not be supported inline *)
260 :     val (env, t) = doLHS()
261 :     in
262 : jhr 1640 (env, [T.S_Assign([t], exp)])
263 : jhr 1115 end
264 :     in
265 :     case rhs
266 : jhr 1640 of IL.STATE x => bindSimple (env, lhs, T.E_State(getStateVar x))
267 :     | IL.VAR x => bindSimple (env, lhs, useVar env x)
268 : jhr 1115 | IL.LIT lit => bindSimple (env, lhs, T.E_Lit lit)
269 : jhr 2004 (* DEPRECATED
270 : jhr 1115 | IL.OP(Op.LoadImage info, [a]) => let
271 :     val (env, t) = doLHS()
272 :     in
273 :     (env, [T.S_LoadImage(t, ImageInfo.dim info, useVar env a)])
274 :     end
275 : jhr 2004 *)
276 : jhr 2023 | IL.OP(Op.Input(InP.INP{ty=Ty.DynSeqTy _, name, desc, init}), []) => let
277 :     val (env, t) = doLHS()
278 :     in
279 :     case init
280 :     of NONE => (env, [T.S_InputNrrd(t, name, desc, NONE)])
281 :     | SOME(InP.DynSeq nrrd) => (env, [T.S_InputNrrd(t, name, desc, SOME nrrd)])
282 :     | _ => raise Fail "bogus initialization for dynamic sequence"
283 :     (* end case *)
284 :     end
285 :     | IL.OP(Op.Input(InP.INP{ty=Ty.ImageTy _, name, desc, init}), []) => let
286 :     val (env, t) = doLHS()
287 :     in
288 :     case init
289 :     of SOME(InP.Proxy(nrrd, _)) => (env, [T.S_InputNrrd(t, name, desc, SOME nrrd)])
290 :     | SOME(InP.Image _) => (env, [T.S_InputNrrd(t, name, desc, NONE)])
291 :     | _ => raise Fail "bogus initialization for image"
292 :     (* end case *)
293 :     end
294 : jhr 2012 | IL.OP(Op.Input(InP.INP{ty, name, desc, init=NONE}), []) => let
295 : jhr 1115 val (env, t) = doLHS()
296 :     in
297 : jhr 1301 (env, [T.S_Input(t, name, desc, NONE)])
298 : jhr 1115 end
299 : jhr 2023 | IL.OP(Op.Input(InP.INP{ty, name, desc, init=SOME init}), []) => let
300 :     val (env, t) = doLHS()
301 :     val (stms, exp) = trInitialization init
302 :     in
303 :     (env, stms@[T.S_Input(t, name, desc, SOME exp)])
304 :     end
305 : jhr 2012 (*
306 : jhr 1301 | IL.OP(Op.InputWithDefault(ty, name, desc), [a]) => let
307 : jhr 1115 val (env, t) = doLHS()
308 :     in
309 : jhr 1301 (env, [T.S_Input(t, name, desc, SOME(useVar env a))])
310 : jhr 1115 end
311 : jhr 2009 | IL.OP(Op.InputNrrd(ty, name, desc), []) => let
312 : jhr 2004 val (env, t) = doLHS()
313 :     val stm = (case ty
314 :     of Ty.ImageTy imgInfo => T.S_InputImage(t, name, desc, NONE)
315 :     | Ty.DynSeqTy _ => T.S_InputSeq(t, name, desc, NONE)
316 :     | _ => raise Fail "bogus type for InputNrrd"
317 :     (* end case *))
318 :     in
319 :     (env, [stm])
320 :     end
321 : jhr 2009 | IL.OP(Op.InputNrrdWithDefault(ty, name, desc, nrrdName), []) => let
322 : jhr 2004 val (env, t) = doLHS()
323 :     val stm = (case ty
324 :     of Ty.ImageTy imgInfo => T.S_InputImage(t, name, desc, SOME nrrdName)
325 :     | Ty.DynSeqTy _ => T.S_InputSeq(t, name, desc, SOME nrrdName)
326 :     | _ => raise Fail "bogus type for InputNrrdWithDefault"
327 :     (* end case *))
328 :     in
329 :     (env, [stm])
330 :     end
331 : jhr 2012 *)
332 : jhr 1115 | IL.OP(rator, args) => let
333 :     val exp = T.E_Op(rator, List.map (useVar env) args)
334 :     in
335 :     if isInlineOp rator
336 :     then bind (env, lhs, exp)
337 :     else assignExp (env, exp)
338 :     end
339 :     | IL.APPLY(f, args) =>
340 :     bind (env, lhs, T.E_Apply(f, List.map (useVar env) args))
341 :     | IL.CONS(ty, args) => let
342 :     val inline = (case ty
343 : jhr 1640 of Ty.SeqTy(Ty.IntTy, _) => true
344 : jhr 1115 | Ty.TensorTy dd => Target.inlineCons(List.length dd)
345 : jhr 1689 | Ty.SeqTy _ => false
346 :     | Ty.DynSeqTy ty => false
347 :     | _ => raise Fail(concat["invalid CONS<", Ty.toString ty, ">"])
348 : jhr 1115 (* end case *))
349 :     val exp = T.E_Cons(ty, List.map (useVar env) args)
350 :     in
351 :     if inline
352 :     then bind (env, lhs, exp)
353 :     else assignExp (env, exp)
354 :     end
355 :     (* end case *)
356 :     end
357 :    
358 :     (* In order to reconstruct the block-structure from the CFG, we keep a stack of open ifs.
359 :     * the items on this stack distinguish between when we are processing the then and else
360 :     * branches of the if.
361 :     *)
362 :     datatype open_if
363 :     (* working on the "then" branch. The fields are statments that preceed the if, the condition,
364 :     * and the else-branch node.
365 :     *)
366 :     = THEN_BR of T.stm list * T.exp * IL.node
367 :     (* working on the "else" branch. The fields are statments that preceed the if, the condition,
368 :     * the "then" branch statements, and the node that terminated the "then" branch (will be
369 :     * a JOIN, DIE, or STABILIZE).
370 :     *)
371 :     | ELSE_BR of T.stm list * T.exp * T.stm list * IL.node_kind
372 :    
373 :     fun trCFG (env, prefix, finish, cfg) = let
374 :     fun join (env, [], _, IL.JOIN _) = raise Fail "JOIN with no open if"
375 :     | join (env, [], stms, _) = endScope (env, prefix @ List.rev stms)
376 :     | join (env, THEN_BR(stms1, cond, elseBr)::stk, thenBlk, k) = let
377 :     val (env, thenBlk) = flushPending (env, thenBlk)
378 :     in
379 :     doNode (env, ELSE_BR(stms1, cond, thenBlk, k)::stk, [], elseBr)
380 :     end
381 :     | join (env, ELSE_BR(stms, cond, thenBlk, k1)::stk, elseBlk, k2) = let
382 :     val (env, elseBlk) = flushPending (env, elseBlk)
383 :     in
384 :     case (k1, k2)
385 :     of (IL.JOIN{phis, succ, ...}, IL.JOIN _) => let
386 :     val (env, [thenBlk, elseBlk]) =
387 :     List.foldl doPhi (env, [thenBlk, elseBlk]) (!phis)
388 :     val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
389 :     in
390 :     doNode (env, stk, stm::stms, !succ)
391 :     end
392 :     | (IL.JOIN{phis, succ, ...}, _) => let
393 :     val (env, [thenBlk]) = List.foldl doPhi (env, [thenBlk]) (!phis)
394 :     val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
395 :     in
396 :     doNode (env, stk, stm::stms, !succ)
397 :     end
398 :     | (_, IL.JOIN{phis, succ, ...}) => let
399 :     val (env, [elseBlk]) = List.foldl doPhi (env, [elseBlk]) (!phis)
400 :     val stm = mkIf(cond, List.rev thenBlk, List.rev elseBlk)
401 :     in
402 :     doNode (env, stk, stm::stms, !succ)
403 :     end
404 :     | (_, _) => raise Fail "no path to exit unimplemented" (* FIXME *)
405 :     (* end case *)
406 :     end
407 :     and doNode (env, ifStk : open_if list, stms, nd) = (
408 :     case Nd.kind nd
409 :     of IL.NULL => raise Fail "unexpected NULL"
410 :     | IL.ENTRY{succ} => doNode (env, ifStk, stms, !succ)
411 :     | k as IL.JOIN{phis, succ, ...} => join (env, ifStk, stms, k)
412 :     | IL.COND{cond, trueBranch, falseBranch, ...} => let
413 :     val cond = useVar env cond
414 :     val (env, stms) = flushPending (env, stms)
415 :     in
416 :     doNode (env, THEN_BR(stms, cond, !falseBranch)::ifStk, [], !trueBranch)
417 :     end
418 :     | IL.COM {text, succ, ...} =>
419 :     doNode (env, ifStk, T.S_Comment text :: stms, !succ)
420 :     | IL.ASSIGN{stm, succ, ...} => let
421 :     val (env, stms') = doAssign (env, stm)
422 :     in
423 :     doNode (env, ifStk, stms' @ stms, !succ)
424 :     end
425 : jhr 1640 | IL.MASSIGN{stm=(ys, rator, xs), succ, ...} => let
426 :     fun doit () = let
427 :     fun doLHSVar (y, (env, ys)) = (case peekGlobal(env, y)
428 :     of SOME y' => (env, y'::ys)
429 :     | NONE => let
430 :     val t = newLocal y
431 :     in
432 :     (rename (addLocal(env, t), y, t), t::ys)
433 :     end
434 :     (* end case *))
435 :     val (env, ys) = List.foldr doLHSVar (env, []) ys
436 :     val exp = T.E_Op(rator, List.map (useVar env) xs)
437 :     val stm = T.S_Assign(ys, exp)
438 :     in
439 :     doNode (env, ifStk, stm :: stms, !succ)
440 :     end
441 :     in
442 :     case rator
443 :     of Op.Print _ => if Target.supportsPrinting()
444 :     then doit ()
445 :     else doNode (env, ifStk, stms, !succ)
446 :     | _ => doit()
447 :     (* end case *)
448 :     end
449 : jhr 1115 | IL.NEW{strand, args, succ, ...} => raise Fail "NEW unimplemented"
450 : jhr 1640 | IL.SAVE{lhs, rhs, succ, ...} => let
451 :     val stm = T.S_Save([getStateVar lhs], useVar env rhs)
452 :     in
453 :     doNode (env, ifStk, stm::stms, !succ)
454 :     end
455 : jhr 1115 | k as IL.EXIT{kind, live, ...} => (case kind
456 :     of ExitKind.FRAGMENT =>
457 :     endScope (env, prefix @ List.revAppend(stms, finish env))
458 : jhr 1232 | ExitKind.SINIT => let
459 :     (* FIXME: we should probably call flushPending here! *)
460 : jhr 1640 val suffix = finish env @ [T.S_Exit[]]
461 : jhr 1232 in
462 :     endScope (env, prefix @ List.revAppend(stms, suffix))
463 :     end
464 : jhr 1115 | ExitKind.RETURN => let
465 :     (* FIXME: we should probably call flushPending here! *)
466 :     val suffix = finish env @ [T.S_Exit(List.map (useVar env) live)]
467 :     in
468 :     endScope (env, prefix @ List.revAppend(stms, suffix))
469 :     end
470 :     | ExitKind.ACTIVE => let
471 :     (* FIXME: we should probably call flushPending here! *)
472 : jhr 1640 val suffix = finish env @ [T.S_Active]
473 : jhr 1115 in
474 :     endScope (env, prefix @ List.revAppend(stms, suffix))
475 :     end
476 :     | ExitKind.STABILIZE => let
477 :     (* FIXME: we should probably call flushPending here! *)
478 : jhr 1640 val stms = T.S_Stabilize :: stms
479 : jhr 1115 in
480 :     (* FIXME: we should probably call flushPending here! *)
481 :     join (env, ifStk, stms, k)
482 :     end
483 :     | ExitKind.DIE => join (env, ifStk, T.S_Die :: stms, k)
484 :     (* end case *))
485 :     (* end case *))
486 :     in
487 :     doNode (env, [], [], CFG.entry cfg)
488 :     end
489 :    
490 :     fun trInitially (env, IL.Initially{isArray, rangeInit, iters, create=(createInit, strand, args)}) =
491 :     let
492 :     val iterPrefix = trCFG (env, [], fn _ => [], rangeInit)
493 :     fun cvtIter ((param, lo, hi), (env, iters)) = let
494 :     val param' = newIter param
495 :     val env = rename (env, param, param')
496 :     in
497 :     (env, (param', useVar env lo, useVar env hi)::iters)
498 :     end
499 :     val (env, iters) = List.foldr cvtIter (env, []) iters
500 :     val createPrefix = trCFG (env, [], fn _ => [], createInit)
501 :     in {
502 :     isArray = isArray,
503 :     iterPrefix = iterPrefix,
504 :     iters = iters,
505 :     createPrefix = createPrefix,
506 :     strand = strand,
507 :     args = List.map (useVar env) args
508 :     } end
509 :    
510 : jhr 1640 fun trMethod env (IL.Method{name, body}) = T.Method{
511 :     name = name,
512 :     body = trCFG (env, [], fn _ => [], body)
513 :     }
514 : jhr 1115
515 : jhr 1640 fun trStrand globalEnv (IL.Strand{name, params, state, stateInit, methods}) = let
516 : jhr 1115 val params' = List.map newParam params
517 : jhr 1640 val env = ListPair.foldlEq (fn (x, x', env) => rename(env, x, x')) globalEnv (params, params')
518 : jhr 1115 in
519 :     T.Strand{
520 :     name = name,
521 :     params = params',
522 : jhr 1640 state = List.map getStateVar state,
523 : jhr 1115 stateInit = trCFG (env, [], fn _ => [], stateInit),
524 : jhr 1640 methods = List.map (trMethod env) methods
525 : jhr 1115 }
526 :     end
527 :    
528 : jhr 1301 (* split the globalInit into the part that specifies the inputs and the rest of
529 :     * the global initialization.
530 :     *)
531 :     fun splitGlobalInit globalInit = let
532 :     fun walk (nd, lastInput, live) = (case Nd.kind nd
533 :     of IL.ENTRY{succ} => walk (!succ, lastInput, live)
534 :     | IL.COM{succ, ...} => walk (!succ, lastInput, live)
535 :     | IL.ASSIGN{stm=(lhs, rhs), succ, ...} => (case rhs
536 :     of IL.OP(Op.Input _, _) => walk (!succ, nd, lhs::live)
537 :     | _ => walk (!succ, lastInput, live)
538 :     (* end case *))
539 :     | _ => if Nd.isNULL lastInput
540 :     then let (* no inputs *)
541 :     val entry = Nd.mkENTRY()
542 :     val exit = Nd.mkEXIT(ExitKind.RETURN, [])
543 :     in
544 :     Nd.addEdge (entry, exit);
545 :     {inputInit = IL.CFG{entry=entry, exit=exit}, globalInit = globalInit}
546 :     end
547 :     else let (* split at lastInput *)
548 :     val inputExit = Nd.mkEXIT(ExitKind.RETURN, live)
549 :     val globalEntry = Nd.mkENTRY()
550 :     val [gFirst] = Nd.succs lastInput
551 :     in
552 :     Nd.replaceInEdge {src = lastInput, oldDst = gFirst, dst = inputExit};
553 :     Nd.replaceOutEdge {oldSrc = lastInput, src = globalEntry, dst = gFirst};
554 :     {
555 :     inputInit = IL.CFG{entry = IL.CFG.entry globalInit, exit = inputExit},
556 :     globalInit = IL.CFG{entry = globalEntry, exit = IL.CFG.exit globalInit}
557 :     }
558 :     end
559 :     (* end case *))
560 :     in
561 :     walk (IL.CFG.entry globalInit, Nd.dummy, [])
562 :     end
563 :    
564 : jhr 2015 (*
565 :     (* translate an input initialization *)
566 :     fun trInitialization init = let
567 :     in
568 :     case init
569 :     of Inputs.String s => ([], CL.mkStr s)
570 :     | Inputs.Bool b => ([], CL.mkBool b)
571 :     | Inputs.Int n => ([], CL.mkIntTy(n, !N.gIntTy))
572 :     | Inputs.Real f => ([], CL.mkFlt(f, !N.gRealTy))
573 :     | Inputs.Tensor([d], vs) => let
574 :     val ty = !gRealTy
575 :     fun mk i = fn i => CL.mkFlt(Vector.sub(vs, i), ty)
576 :     in
577 :     case shp
578 :     of [d] => ([], CL.mkApply(N.mkVec d, List.tabulate(d, mk)))
579 :     | _ => raise Fail "FIXME"
580 :     (* end case *)
581 :     end
582 :     | Inputs.Seq vs =>
583 :     | Inputs.DynSeq nrrd =>
584 :     | Inputs.Proxy(nrrd, info) =>
585 :     | Inputs.
586 :     (* end case *)
587 :     end
588 :     *)
589 :    
590 : jhr 1115 fun translate prog = let
591 :     (* first we do a variable analysis pass on the Low IL *)
592 : jhr 1640 val prog as IL.Program{props, globalInit, initially, strands} = VA.optimize prog
593 : jhr 1115 (* FIXME: here we should do a contraction pass to eliminate unused variables that VA may have created *)
594 : jhr 1232 val _ = (* DEBUG *)
595 :     LowPP.output (Log.logFile(), "LowIL after variable analysis", prog)
596 : jhr 1115 val env = newEnv()
597 :     val globals = List.map
598 :     (fn x => let val x' = newGlobal x in global(env, x, x'); x' end)
599 :     (IL.CFG.liveAtExit globalInit)
600 : jhr 1301 val {inputInit, globalInit} = splitGlobalInit globalInit
601 : jhr 1131 val strands = List.map (trStrand env) strands
602 : jhr 1115 in
603 :     T.Program{
604 : jhr 1640 props = props,
605 : jhr 1115 globals = globals,
606 : jhr 1301 inputInit = trCFG (env, [], fn _ => [], inputInit),
607 : jhr 1115 globalInit = trCFG (env, [], fn _ => [], globalInit),
608 : jhr 1131 strands = strands,
609 : jhr 1115 initially = trInitially (env, initially)
610 :     }
611 :     end
612 :    
613 :     end

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