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

SCM Repository

[diderot] Annotation of /trunk/src/compiler/simplify/lift.sml
ViewVC logotype

Annotation of /trunk/src/compiler/simplify/lift.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2356 - (view) (download)

1 : jhr 181 (* lift.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 181 * All rights reserved.
5 :     *
6 : jhr 234 * Lift field operations to global scope and split global initialization to
7 :     * pre and post image loading phases.
8 : jhr 246 *
9 :     * NOTE: this process can be streamlined as follows:
10 :     * 1) identify the static variables
11 :     * 2) evaluate eagerly, with the proviso that if the lhs is not static, then
12 :     * only evaluate if the rhs is static and the operation is supported
13 :     * 3) reduce code
14 : jhr 511 * This evaluation process could be extended to the body of the strands too.
15 : jhr 181 *)
16 :    
17 :     structure Lift : sig
18 :    
19 :     val transform : Simple.program -> Simple.program
20 :    
21 :     end = struct
22 :    
23 :     structure BV = BasisVars
24 : jhr 227 structure S = Simple
25 :     structure VSet = Var.Set
26 :     structure VMap = Var.Map
27 : jhr 181
28 : jhr 269 (* identify the image load operations and their antecedents; in terms of BTA,
29 : jhr 227 * this phase is essentially determining what must be static in order to get the image
30 :     * info needed for the rest of the compile.
31 :     *)
32 : jhr 269 fun findStatics block = let
33 : jhr 227 (* analysis to compute the set of static variables *)
34 :     fun mkStatic (env, statics, x) = if VSet.member(statics, x)
35 :     then statics
36 :     else let
37 :     val statics = VSet.add(statics, x)
38 :     in
39 :     case VMap.find(env, x)
40 :     of SOME(S.E_Var y) => mkStatic (env, statics, y)
41 :     | SOME(S.E_Tuple ys) => mkStatics (env, statics, ys)
42 :     | SOME(S.E_Apply(_, _, ys, _)) => mkStatics (env, statics, ys)
43 :     | SOME(S.E_Cons ys) => mkStatics (env, statics, ys)
44 : jhr 1301 | SOME(S.E_Input(_, _, _, SOME y)) => mkStatic (env, statics, y)
45 : jhr 227 | SOME _ => statics
46 : jhr 1113 | NONE => raise Fail(concat[
47 :     "variable ", Var.uniqueNameOf x, " has no binding"
48 :     ])
49 : jhr 227 (* end case *)
50 :     end
51 :     and mkStatics (env, statics, xs) =
52 :     List.foldl (fn (x, statics) => mkStatic(env, statics, x)) statics xs
53 :     fun doBlock (env, statics, S.Block stms) = let
54 :     fun doStmts (env, statics, []) = statics
55 :     | doStmts (env, statics, stm::stms) = let
56 :     val (env, statics) = doStmt (env, statics, stm)
57 :     in
58 :     doStmts (env, statics, stms)
59 :     end
60 :     in
61 :     doStmts (env, statics, stms)
62 :     end
63 :     and doStmt (env, statics, stm) = (case stm
64 :     of S.S_Assign(x, e) => let
65 :     val env = VMap.insert(env, x, e)
66 :     in
67 :     case e
68 :     of S.E_Apply(f, _, xs, _) =>
69 :     if Var.same(f, BV.fn_load)
70 :     then (env, mkStatic(env, statics, x))
71 :     else (env, statics)
72 :     | _ => (env, statics)
73 :     (* end case *)
74 :     end
75 :     | S.S_IfThenElse(x, b1, b2) => let
76 :     val statics1 = doBlock (env, statics, b1)
77 :     val statics2 = doBlock (env, statics, b2)
78 :     val n = VSet.numItems statics
79 :     in
80 : jhr 1113 if ((n <> VSet.numItems statics1)
81 :     orelse (n <> VSet.numItems statics2))
82 : jhr 227 then (env, mkStatic(env, statics, x))
83 :     else (env, statics)
84 :     end
85 :     | _ => (env, statics)
86 :     (* end case *))
87 :     val statics = doBlock (VMap.empty, VSet.empty, block)
88 :     in
89 : jhr 246 Log.msg "**** static variables: ";
90 :     VSet.app (fn x => Log.msg(" "^Var.uniqueNameOf x)) statics;
91 :     Log.msg "\n";
92 : jhr 269 statics
93 : jhr 227 end
94 :    
95 : jhr 240 (* given values for the static variables; reduce the static initialization code *)
96 :     fun reduce (env, blk) = let
97 :     fun doBlock (S.Block stms) =
98 :     List.foldr (fn (stm, stms) => doStmt stm @ stms) [] stms
99 :     and doStmt stm = (case stm
100 :     of S.S_Assign(x, e) => (case Var.Map.find(env, x)
101 :     of SOME v => let
102 :     val rhs = (case v
103 :     of (Eval.BV b) => S.E_Lit(Literal.Bool b)
104 :     | (Eval.SV s) => S.E_Lit(Literal.String s)
105 :     | (Eval.IV i) => S.E_Lit(Literal.Int i)
106 :     | (Eval.TV _) => e
107 : jhr 1116 | (Eval.RV _) => e (* FIXME: need way to convert from real to float literal *)
108 :     | (Eval.ImgV arg) => S.E_LoadImage arg
109 : jhr 240 (* end case *))
110 :     in
111 :     [S.S_Assign(x, rhs)]
112 :     end
113 :     | NONE => [stm]
114 :     (* end case *))
115 :     | S.S_IfThenElse(x, b1, b2) => (case Var.Map.find(env, x)
116 :     of SOME(Eval.BV b) => if b then doBlock b1 else doBlock b2
117 :     | NONE => [stm]
118 :     (* end case *))
119 :     | _ => [stm]
120 :     (* end case *))
121 :     in
122 :     S.Block(doBlock blk)
123 :     end
124 :    
125 : jhr 1116 fun reduceInit (env, S.Initially{isArray, rangeInit, iters, create}) = let
126 :     fun reduceCreate (S.C_Create{argInit, name, args}) = S.C_Create{
127 :     argInit = reduce (env, argInit),
128 :     name = name, args = args
129 :     }
130 :     in
131 :     S.Initially{
132 :     isArray = isArray,
133 :     rangeInit = reduce (env, rangeInit),
134 :     iters = iters,
135 :     create = reduceCreate create
136 :     }
137 :     end
138 :    
139 : jhr 2356 fun transform (prog as S.Program{globals, globalInit, funcs, init, strands}) = let
140 : jhr 269 val statics = findStatics globalInit
141 :     val staticEnv = Eval.evalStatics (statics, globalInit)
142 : jhr 240 val globalInit = reduce (staticEnv, globalInit)
143 : jhr 1116 val init = reduceInit (staticEnv, init)
144 : jhr 227 in
145 :     S.Program{
146 :     globals = globals,
147 :     globalInit = globalInit,
148 : jhr 2356 funcs = funcs,
149 : jhr 1116 init = init,
150 : jhr 511 strands = strands
151 : jhr 227 }
152 :     end
153 :    
154 : jhr 181 end

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