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 1113 - (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 229 | 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 :     | (Eval.FV fld) => S.E_Field fld
108 :     | (Eval.Img info) => S.E_LoadImage info
109 :     | (Eval.KV h) => e
110 :     (* end case *))
111 :     in
112 :     [S.S_Assign(x, rhs)]
113 :     end
114 :     | NONE => [stm]
115 :     (* end case *))
116 :     | S.S_IfThenElse(x, b1, b2) => (case Var.Map.find(env, x)
117 :     of SOME(Eval.BV b) => if b then doBlock b1 else doBlock b2
118 :     | NONE => [stm]
119 :     (* end case *))
120 :     | _ => [stm]
121 :     (* end case *))
122 :     in
123 :     S.Block(doBlock blk)
124 :     end
125 :    
126 : jhr 511 fun transform (prog as S.Program{globals, globalInit, strands}) = let
127 : jhr 269 val statics = findStatics globalInit
128 :     val staticEnv = Eval.evalStatics (statics, globalInit)
129 : jhr 240 val globalInit = reduce (staticEnv, globalInit)
130 : jhr 227 in
131 :     S.Program{
132 :     globals = globals,
133 :     globalInit = globalInit,
134 : jhr 511 strands = strands
135 : jhr 227 }
136 :     end
137 :    
138 : jhr 181 end

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