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

SCM Repository

[diderot] Diff of /branches/vis12/src/compiler/simplify/simplify.sml
ViewVC logotype

Diff of /branches/vis12/src/compiler/simplify/simplify.sml

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

trunk/src/compiler/simplify/simplify.sml revision 511, Tue Feb 8 17:01:43 2011 UTC branches/vis12/src/compiler/simplify/simplify.sml revision 2365, Fri Apr 12 12:52:31 2013 UTC
# Line 8  Line 8 
8    
9  structure Simplify : sig  structure Simplify : sig
10    
11      val transform : AST.program -> Simple.program      val transform : Error.err_stream * AST.program -> Simple.program
12    
13    end = struct    end = struct
14    
15      structure Ty = Types      structure Ty = Types
16      structure S = Simple      structure S = Simple
17        structure InP = Inputs
18    
19      (* the SimpleAST and AST currently use the same type representation, but
20       * we prune out meta variables.
21       *)
22        val cvtTy = TypeUtil.prune
23    
24      local      local
25        val tempName = Atom.atom "_t"        val tempName = Atom.atom "_t"
26      in      in
27      fun newTemp ty = Var.new (tempName, AST.LocalVar, ty)      fun newTemp ty = Var.new (tempName, AST.LocalVar, cvtTy ty)
28      end      end
29    
30    (* make a block out of a list of statements that are in reverse order *)    (* make a block out of a list of statements that are in reverse order *)
31      fun mkBlock stms = S.Block(List.rev stms)      fun mkBlock stms = S.Block(List.rev stms)
32    
33      fun simplifyProgram (AST.Program dcls) = let    (* convert an AST expression to an input initialization.  Note that the Diderot grammar
34       * limits the forms of expression that we might encounter in this context.
35       *)
36        fun expToInit (ty, exp) = (case exp
37               of AST.E_Lit(Literal.Int n) => InP.Int n
38                | AST.E_Lit(Literal.Float f) => InP.Real f
39                | AST.E_Lit(Literal.String s) => InP.String s
40                | AST.E_Lit(Literal.Bool b) => InP.Bool b
41                | AST.E_Tuple es => raise Fail "E_Tuple not yet implemented"
42                | AST.E_Cons es => let
43                    val shp = (case ty
44                           of Ty.T_Tensor(Ty.Shape shp) => List.map (fn (Ty.DimConst d) => d) shp
45                            | _ => raise Fail "not tensor type"
46                          (* end case *))
47                    fun flatten (AST.E_Lit(Literal.Int n), l) = FloatLit.fromInt n :: l
48                      | flatten (AST.E_Lit(Literal.Float f), l) = f :: l
49                      | flatten (AST.E_Coerce{e, ...}, l) = flatten(e, l)
50                      | flatten (AST.E_Cons es, l) = flattenList (es, l)
51                      | flatten _ = raise Fail "impossible"
52                    and flattenList ([], l) = l
53                      | flattenList (x::xs, l) = flatten(x, flattenList(xs, l))
54                    in
55                      InP.Tensor(shp, Vector.fromList(flattenList (es, [])))
56                    end
57    (*
58                | AST.E_Seq es => ??
59                | AST.E_Coerce{srcTy, dstTy, e} => ??
60    *)
61                | _ => raise Fail "impossible initialization expression"
62              (* end case *))
63    
64        fun inputImage (nrrd, dim, shape) = let
65              val dim = TypeUtil.monoDim dim
66              val shp = TypeUtil.monoShape shape
67              in
68                case ImageInfo.fromNrrd(NrrdInfo.getInfo nrrd, dim, shp)
69                 of NONE => raise Fail(concat["nrrd file \"", nrrd, "\" does not have expected type"])
70                  | SOME info => InP.Proxy(nrrd, info)
71                (* end case *)
72              end
73    
74      (* is the given statement's continuation the syntactically following statement? *)
75        fun contIsNext (AST.S_Block stms) = List.all contIsNext stms
76          | contIsNext (AST.S_IfThenElse(_, s1, s2)) = contIsNext s1 orelse contIsNext s2
77          | contIsNext AST.S_Die = false
78          | contIsNext AST.S_Stabilize = false
79          | contIsNext (AST.S_Return _) = false
80          | contIsNext _ = true
81    
82        fun simplifyProgram (AST.Program{props, decls}) = let
83              val inputs = ref []
84            val globals = ref []            val globals = ref []
85            val globalInit = ref []            val globalInit = ref []
86              val funcs = ref []
87              val initially = ref NONE
88            val strands = ref []            val strands = ref []
89              fun setInitially init = (case !initially
90                     of NONE => initially := SOME init
91    (* FIXME: the check for multiple initially decls should happen in type checking *)
92                      | SOME _ => raise Fail "multiple initially declarations"
93                    (* end case *))
94            fun simplifyDecl dcl = (case dcl            fun simplifyDecl dcl = (case dcl
95                   of AST.D_Input(x, NONE) => let                   of AST.D_Input(x, desc, NONE) => let
96                        val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, NONE)                        val (ty, init) = (case Var.monoTypeOf x
97                                 of ty as Ty.T_Image{dim, shape} => let
98                                      val info = ImageInfo.mkInfo(TypeUtil.monoDim dim, TypeUtil.monoShape shape)
99                        in                        in
100                          globals := x :: !globals;                                      (ty, SOME(InP.Image info))
                         globalInit := S.S_Assign(x, e') :: !globalInit  
101                        end                        end
102                    | AST.D_Input(x, SOME e) => let                                | ty => (ty, NONE)
103                        val (stms, x') = simplifyExpToVar (e, [])                              (* end case *))
104                        val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, SOME x')                        val inp = InP.INP{
105                                  ty = ty,
106                                  name = Var.nameOf x,
107                                  desc = desc,
108                                  init = init
109                                }
110                        in                        in
111                          globals := x :: !globals;                          inputs := (x, inp) :: !inputs
112                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)                        end
113                      | AST.D_Input(x, desc, SOME(AST.E_LoadNrrd(tvs, nrrd, ty))) => let
114                        (* load the nrrd proxy here *)
115                          val info = NrrdInfo.getInfo nrrd
116                          val (ty, init) = (case Var.monoTypeOf x
117                                 of ty as Ty.T_DynSequence _ => (ty, InP.DynSeq nrrd)
118                                  | ty as Ty.T_Image{dim, shape} => (ty, inputImage(nrrd, dim, shape))
119                                  | _ => raise Fail "impossible"
120                                (* end case *))
121                          val inp = InP.INP{
122                                  ty = ty,
123                                  name = Var.nameOf x,
124                                  desc = desc,
125                                  init = SOME init
126                                }
127                          in
128                            inputs := (x, inp) :: !inputs
129                          end
130                      | AST.D_Input(x, desc, SOME e) => let
131                          val ty = Var.monoTypeOf x
132                          val inp = InP.INP{
133                                  ty = ty,
134                                  name = Var.nameOf x,
135                                  desc = desc,
136                                  init = SOME(expToInit(ty, e))
137                                }
138                          in
139                            inputs := (x, inp) :: !inputs
140                        end                        end
141                    | AST.D_Var(AST.VD_Decl(x, e)) => let                    | AST.D_Var(AST.VD_Decl(x, e)) => let
142                        val (stms, e') = simplifyExp (e, [])                        val (stms, e') = simplifyExp (e, [])
# Line 48  Line 144 
144                          globals := x :: !globals;                          globals := x :: !globals;
145                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
146                        end                        end
147                      | AST.D_Func(f, params, body) =>
148                          funcs := S.Func{f=f, params=params, body=simplifyBlock body} :: !funcs
149                    | AST.D_Strand info => strands := simplifyStrand info :: !strands                    | AST.D_Strand info => strands := simplifyStrand info :: !strands
150                    | AST.D_InitialArray(e, iters) => () (* FIXME *)                    | AST.D_InitialArray(creat, iters) =>
151                    | AST.D_InitialCollection(e, iters) => () (* FIXME *)                        setInitially (simplifyInit(true, creat, iters))
152                      | AST.D_InitialCollection(creat, iters) =>
153                          setInitially (simplifyInit(false, creat, iters))
154                  (* end case *))                  (* end case *))
155            in            in
156              List.app simplifyDecl dcls;              List.app simplifyDecl decls;
157              S.Program{              S.Program{
158                    props = props,
159                    inputs = List.rev(!inputs),
160                  globals = List.rev(!globals),                  globals = List.rev(!globals),
161                  globalInit = mkBlock (!globalInit),                  globalInit = mkBlock (!globalInit),
162                    funcs = List.rev(!funcs),
163                    init = (case !initially
164    (* FIXME: the check for the initially block should really happen in typechecking *)
165                       of NONE => raise Fail "missing initially declaration"
166                        | SOME blk => blk
167                      (* end case *)),
168                  strands = List.rev(!strands)                  strands = List.rev(!strands)
169                }                }
170            end            end
171    
172      and simplifyStrand {name, params, state, methods} = let      and simplifyInit (isArray, AST.C_Create(strand, exps), iters) = let
173              val (stms, xs) = simplifyExpsToVars (exps, [])
174              val creat = S.C_Create{
175                      argInit = mkBlock stms,
176                      name = strand,
177                      args = xs
178                    }
179              fun simplifyIter (AST.I_Range(x, e1, e2), (iters, stms)) = let
180                    val (stms, lo) = simplifyExpToVar (e1, stms)
181                    val (stms, hi) = simplifyExpToVar (e2, stms)
182                    in
183                      ({param=x, lo=lo, hi=hi}::iters, stms)
184                    end
185              val (iters, stms) = List.foldl simplifyIter ([], []) iters
186              in
187                S.Initially{
188                    isArray = isArray,
189                    rangeInit = mkBlock stms,
190                    iters = List.rev iters,
191                    create = creat
192                  }
193              end
194    
195        and simplifyStrand (AST.Strand{name, params, state, methods}) = let
196            fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)            fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
197              | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let              | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let
198                  val (stms, e') = simplifyExp (e, stms)                  val (stms, e') = simplifyExp (e, stms)
# Line 81  Line 212 
212      and simplifyMethod (AST.M_Method(name, body)) =      and simplifyMethod (AST.M_Method(name, body)) =
213            S.Method(name, simplifyBlock body)            S.Method(name, simplifyBlock body)
214    
215    (* simplify a statement into a single statement (i.e., a block if it expands into more    (* simplify a statement into a single statement (i.e., a block if it expands
216     * than one new statement.     * into more than one new statement).
217     *)     *)
218      and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))      and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))
219    
220      (* simplify the statement stm where stms is a reverse-order list of preceeding simplified
221       * statements.  This function returns a reverse-order list of simplified statements.
222       * Note that error reporting is done in the typechecker, but it does not prune unreachable
223       * code.
224       *)
225      and simplifyStmt (stm, stms) = (case stm      and simplifyStmt (stm, stms) = (case stm
226             of AST.S_Block body => let             of AST.S_Block body => let
227                  fun simplify ([], stms) = stms                  fun simplify ([], stms) = stms
228                    | simplify (stm::r, stms) = simplify (r, simplifyStmt (stm, stms))                    | simplify (stm::r, stms) = if contIsNext stm
229                          then simplify (r, simplifyStmt (stm, stms))
230                          else simplifyStmt (stm, stms)  (* prune unreachable statements *)
231                  in                  in
232                    simplify (body, stms)                    simplify (body, stms)
233                  end                  end
# Line 117  Line 255 
255                  end                  end
256              | AST.S_Die => S.S_Die :: stms              | AST.S_Die => S.S_Die :: stms
257              | AST.S_Stabilize => S.S_Stabilize :: stms              | AST.S_Stabilize => S.S_Stabilize :: stms
258                | AST.S_Return e => let
259                    val (stms, x) = simplifyExpToVar (e, stms)
260                    in
261                      S.S_Return x :: stms
262                    end
263                | AST.S_Print args => let
264                    val (stms, xs) = simplifyExpsToVars (args, stms)
265                    in
266                      S.S_Print xs :: stms
267                    end
268            (* end case *))            (* end case *))
269    
270      and simplifyExp (exp, stms) = (      and simplifyExp (exp, stms) = (
# Line 143  Line 291 
291                  in                  in
292                    (stms, S.E_Cons xs)                    (stms, S.E_Cons xs)
293                  end                  end
294                | AST.E_Seq es => let
295                    val (stms, xs) = simplifyExpsToVars (es, stms)
296                    in
297                      (stms, S.E_Seq xs)
298                    end
299              | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)              | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
300                  val (stms, x) = simplifyExpToVar (e, stms)                  val (stms, x) = simplifyExpToVar (e, stms)
301                  fun f ([], ys, stms) = (stms, List.rev ys)                  fun f ([], ys, stms) = (stms, List.rev ys)
# Line 159  Line 312 
312              | AST.E_Cond(e1, e2, e3, ty) => let              | AST.E_Cond(e1, e2, e3, ty) => let
313                (* a conditional expression gets turned into an if-then-else statememt *)                (* a conditional expression gets turned into an if-then-else statememt *)
314                  val result = newTemp ty                  val result = newTemp ty
315                  val (stms, x) = simplifyExpToVar (e1, stms)                  val (stms, x) = simplifyExpToVar (e1, S.S_Var result :: stms)
316                  fun simplifyBranch e = let                  fun simplifyBranch e = let
317                        val (stms, e) = simplifyExp (e, [])                        val (stms, e) = simplifyExp (e, [])
318                        in                        in
# Line 170  Line 323 
323                  in                  in
324                    (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)                    (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
325                  end                  end
326                | AST.E_LoadNrrd(_, nrrd, ty) => (case TypeUtil.prune ty
327                     of ty as Ty.T_DynSequence _ => (stms, S.E_LoadSeq(ty, nrrd))
328                      | ty as Ty.T_Image{dim, shape} => let
329                          val dim = TypeUtil.monoDim dim
330                          val shp = TypeUtil.monoShape shape
331                          in
332                            case ImageInfo.fromNrrd(NrrdInfo.getInfo nrrd, dim, shp)
333                             of NONE => raise Fail(concat[
334                                    "nrrd file \"", nrrd, "\" does not have expected type"
335                                  ])
336                              | SOME info => (stms, S.E_LoadImage(ty, nrrd, info))
337                            (* end case *)
338                          end
339                      | _ => raise Fail "bogus type for E_LoadNrrd"
340                    (* end case *))
341                | AST.E_Coerce{srcTy, dstTy, e} => let
342                    val (stms, x) = simplifyExpToVar (e, stms)
343                    val result = newTemp dstTy
344                    val rhs = S.E_Coerce{srcTy = cvtTy srcTy, dstTy = cvtTy dstTy, x = x}
345                    in
346                      (S.S_Assign(result, rhs)::stms, S.E_Var result)
347                    end
348            (* end case *))            (* end case *))
349    
350      and simplifyExpToVar (exp, stms) = let      and simplifyExpToVar (exp, stms) = let
# Line 196  Line 371 
371              f (exps, [], stms)              f (exps, [], stms)
372            end            end
373    
374      fun transform ast = let      fun transform (errStrm, ast) = let
375            val simple = simplifyProgram ast            val simple = simplifyProgram ast
376            val _ = SimplePP.output (Log.logFile(), simple)       (* DEBUG *)            val _ = SimplePP.output (Log.logFile(), "simplify", simple)   (* DEBUG *)
377              val simple = Inliner.transform simple
378              val _ = SimplePP.output (Log.logFile(), "inlining", simple)   (* DEBUG *)
379    (*
380            val simple = Lift.transform simple            val simple = Lift.transform simple
381                    handle Eval.Error msg => (Error.error(errStrm, msg); simple)
382    *)
383            in            in
384              simple              simple
385            end            end

Legend:
Removed from v.511  
changed lines
  Added in v.2365

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