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 511 - (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 :     | NONE => raise Fail(concat["variable ", Var.uniqueNameOf x, " has no binding"])
47 :     (* end case *)
48 :     end
49 :     and mkStatics (env, statics, xs) =
50 :     List.foldl (fn (x, statics) => mkStatic(env, statics, x)) statics xs
51 :     fun doBlock (env, statics, S.Block stms) = let
52 :     fun doStmts (env, statics, []) = statics
53 :     | doStmts (env, statics, stm::stms) = let
54 :     val (env, statics) = doStmt (env, statics, stm)
55 :     in
56 :     doStmts (env, statics, stms)
57 :     end
58 :     in
59 :     doStmts (env, statics, stms)
60 :     end
61 :     and doStmt (env, statics, stm) = (case stm
62 :     of S.S_Assign(x, e) => let
63 :     val env = VMap.insert(env, x, e)
64 :     in
65 :     case e
66 :     of S.E_Apply(f, _, xs, _) =>
67 :     if Var.same(f, BV.fn_load)
68 :     then (env, mkStatic(env, statics, x))
69 :     else (env, statics)
70 :     | _ => (env, statics)
71 :     (* end case *)
72 :     end
73 :     | S.S_IfThenElse(x, b1, b2) => let
74 :     val statics1 = doBlock (env, statics, b1)
75 :     val statics2 = doBlock (env, statics, b2)
76 :     val n = VSet.numItems statics
77 :     in
78 :     if ((n <> VSet.numItems statics1) orelse (n <> VSet.numItems statics2))
79 :     then (env, mkStatic(env, statics, x))
80 :     else (env, statics)
81 :     end
82 :     | _ => (env, statics)
83 :     (* end case *))
84 :     val statics = doBlock (VMap.empty, VSet.empty, block)
85 :     in
86 : jhr 246 Log.msg "**** static variables: ";
87 :     VSet.app (fn x => Log.msg(" "^Var.uniqueNameOf x)) statics;
88 :     Log.msg "\n";
89 : jhr 269 statics
90 : jhr 227 end
91 :    
92 : jhr 240 (* given values for the static variables; reduce the static initialization code *)
93 :     fun reduce (env, blk) = let
94 :     fun doBlock (S.Block stms) =
95 :     List.foldr (fn (stm, stms) => doStmt stm @ stms) [] stms
96 :     and doStmt stm = (case stm
97 :     of S.S_Assign(x, e) => (case Var.Map.find(env, x)
98 :     of SOME v => let
99 :     val rhs = (case v
100 :     of (Eval.BV b) => S.E_Lit(Literal.Bool b)
101 :     | (Eval.SV s) => S.E_Lit(Literal.String s)
102 :     | (Eval.IV i) => S.E_Lit(Literal.Int i)
103 :     | (Eval.TV _) => e
104 :     | (Eval.FV fld) => S.E_Field fld
105 :     | (Eval.Img info) => S.E_LoadImage info
106 :     | (Eval.KV h) => e
107 :     (* end case *))
108 :     in
109 :     [S.S_Assign(x, rhs)]
110 :     end
111 :     | NONE => [stm]
112 :     (* end case *))
113 :     | S.S_IfThenElse(x, b1, b2) => (case Var.Map.find(env, x)
114 :     of SOME(Eval.BV b) => if b then doBlock b1 else doBlock b2
115 :     | NONE => [stm]
116 :     (* end case *))
117 :     | _ => [stm]
118 :     (* end case *))
119 :     in
120 :     S.Block(doBlock blk)
121 :     end
122 :    
123 : jhr 511 fun transform (prog as S.Program{globals, globalInit, strands}) = let
124 : jhr 269 val statics = findStatics globalInit
125 :     val staticEnv = Eval.evalStatics (statics, globalInit)
126 : jhr 240 val globalInit = reduce (staticEnv, globalInit)
127 : jhr 227 in
128 :     S.Program{
129 :     globals = globals,
130 :     globalInit = globalInit,
131 : jhr 511 strands = strands
132 : jhr 227 }
133 :     end
134 :    
135 : jhr 181 end

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