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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 234, Thu Aug 5 21:34:29 2010 UTC revision 1116, Thu May 5 04:49:02 2011 UTC
# Line 1  Line 1 
1  (* lift.sml  (* lift.sml
2   *   *
3   * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)   * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4   * All rights reserved.   * All rights reserved.
5   *   *
6   * Lift field operations to global scope and split global initialization to   * Lift field operations to global scope and split global initialization to
7   * pre and post image loading phases.   * pre and post image loading phases.
8     *
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     * This evaluation process could be extended to the body of the strands too.
15   *)   *)
16    
17  structure Lift : sig  structure Lift : sig
# Line 18  Line 25 
25      structure VSet = Var.Set      structure VSet = Var.Set
26      structure VMap = Var.Map      structure VMap = Var.Map
27    
28  (*    (* identify the image load operations and their antecedents; in terms of BTA,
     BV.op_at  
     BV.op_D  
     BV.fn_convolve  
     BV.fn_load  
 *)  
   
   (* the kinds of things a variable in Simple AST can be bound to *)  
     datatype var_binding  
       = RHS of S.exp  
       | Param  
   
   (* identify the image load operations and lift them and their antecedents; in terms of BTA,  
29     * this phase is essentially determining what must be static in order to get the image     * this phase is essentially determining what must be static in order to get the image
30     * info needed for the rest of the compile.     * info needed for the rest of the compile.
31     *)     *)
32      fun liftLoads block = let      fun findStatics block = let
33          (* analysis to compute the set of static variables *)          (* analysis to compute the set of static variables *)
34            fun mkStatic (env, statics, x) = if VSet.member(statics, x)            fun mkStatic (env, statics, x) = if VSet.member(statics, x)
35                  then statics                  then statics
# Line 48  Line 43 
43                        | SOME(S.E_Cons ys) => mkStatics (env, statics, ys)                        | SOME(S.E_Cons ys) => mkStatics (env, statics, ys)
44                        | SOME(S.E_Input(_, _, SOME y)) => mkStatic (env, statics, y)                        | SOME(S.E_Input(_, _, SOME y)) => mkStatic (env, statics, y)
45                        | SOME _ => statics                        | SOME _ => statics
46                        | NONE => raise Fail(concat["variable ", Var.uniqueNameOf x, " has no binding"])                        | NONE => raise Fail(concat[
47                                "variable ", Var.uniqueNameOf x, " has no binding"
48                              ])
49                      (* end case *)                      (* end case *)
50                    end                    end
51            and mkStatics (env, statics, xs) =            and mkStatics (env, statics, xs) =
# Line 80  Line 77 
77                        val statics2 = doBlock (env, statics, b2)                        val statics2 = doBlock (env, statics, b2)
78                        val n = VSet.numItems statics                        val n = VSet.numItems statics
79                        in                        in
80                          if ((n <> VSet.numItems statics1) orelse (n <> VSet.numItems statics2))                          if ((n <> VSet.numItems statics1)
81                            orelse (n <> VSet.numItems statics2))
82                            then (env, mkStatic(env, statics, x))                            then (env, mkStatic(env, statics, x))
83                            else (env, statics)                            else (env, statics)
84                        end                        end
85                    | _ => (env, statics)                    | _ => (env, statics)
86                  (* end case *))                  (* end case *))
87            val statics = doBlock (VMap.empty, VSet.empty, block)            val statics = doBlock (VMap.empty, VSet.empty, block)
         (* lift out the static code *)  
           fun doBlock (S.Block stms) = let  
                 fun doStmts ([], staticStms) = S.Block(List.rev staticStms)  
                   | doStmts (stm::stms, staticStms) = (case doStmt stm  
                        of SOME stm => doStmts (stms, stm::staticStms)  
                         | NONE => doStmts (stms, staticStms)  
                       (* end case *))  
88                  in                  in
89                    doStmts (stms, [])  Log.msg "**** static variables: ";
90    VSet.app (fn x => Log.msg(" "^Var.uniqueNameOf x)) statics;
91    Log.msg "\n";
92                statics
93                  end                  end
94    
95      (* 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            and doStmt stm = (case stm
100                   of S.S_Assign(x, e) => if VSet.member(statics, x)                   of S.S_Assign(x, e) => (case Var.Map.find(env, x)
101                        then SOME stm                         of SOME v => let
102                        else NONE                              val rhs = (case v
103                    | S.S_IfThenElse(x, b1, b2) => if VSet.member(statics, x)                                     of (Eval.BV b) => S.E_Lit(Literal.Bool b)
104                        then SOME(S.S_IfThenElse(x, doBlock b1, doBlock b2))                                      | (Eval.SV s) => S.E_Lit(Literal.String s)
105                        else NONE                                      | (Eval.IV i) => S.E_Lit(Literal.Int i)
106                    | _ => NONE                                      | (Eval.TV _) => e
107                                        | (Eval.RV _) => e (* FIXME: need way to convert from real to float literal *)
108                                        | (Eval.ImgV arg) => S.E_LoadImage arg
109                                      (* 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 *))                  (* end case *))
119            val staticBlock = doBlock block                    | _ => [stm]
120                    (* end case *))
121              in
122                S.Block(doBlock blk)
123              end
124    
125        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            in
131  print "**** static variables: ";              S.Initially{
132  VSet.app (fn x => print(" "^Var.uniqueNameOf x)) statics;                  isArray = isArray,
133  print "\n";                  rangeInit = reduce (env, rangeInit),
134              staticBlock                  iters = iters,
135                    create = reduceCreate create
136                  }
137            end            end
138    
139      fun transform (prog as S.Program{globals, staticInit, globalInit, actors}) = let      fun transform (prog as S.Program{globals, globalInit, init, strands}) = let
140            val staticInit = liftLoads globalInit            val statics = findStatics globalInit
141              val staticEnv = Eval.evalStatics (statics, globalInit)
142              val globalInit = reduce (staticEnv, globalInit)
143              val init = reduceInit (staticEnv, init)
144            in            in
145              S.Program{              S.Program{
146                  globals = globals,                  globals = globals,
                 staticInit = staticInit,  
147                  globalInit = globalInit,                  globalInit = globalInit,
148                  actors = actors                  init = init,
149                    strands = strands
150                }                }
151            end            end
152    

Legend:
Removed from v.234  
changed lines
  Added in v.1116

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