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 395, Thu Oct 14 16:52:15 2010 UTC branches/vis12/src/compiler/simplify/simplify.sml revision 2328, Fri Mar 22 18:21:07 2013 UTC
# Line 1  Line 1 
1  (* simplify.sml  (* simplify.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   * Simplify the AST representation.   * Simplify the AST representation.
# 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      (* 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 dcls) = let      fun simplifyProgram (AST.Program dcls) = let
83              val inputs = ref []
84            val globals = ref []            val globals = ref []
85            val globalInit = ref []            val globalInit = ref []
86            val actors = ref []            val funcs = ref []
87              val initially = ref NONE
88              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_Actor info => actors := simplifyActor info :: !actors                    | AST.D_Func(f, params, body) =>
148                    | AST.D_InitialArray(e, iters) => () (* FIXME *)                        funcs := S.Func{f=f, params=params, body=simplifyBlock body} :: !funcs
149                    | AST.D_InitialCollection(e, iters) => () (* FIXME *)                    | AST.D_Strand info => strands := simplifyStrand info :: !strands
150                      | AST.D_InitialArray(creat, iters) =>
151                          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 dcls;
157              S.Program{              S.Program{
158                    inputs = List.rev(!inputs),
159                  globals = List.rev(!globals),                  globals = List.rev(!globals),
160                  globalInit = mkBlock (!globalInit),                  globalInit = mkBlock (!globalInit),
161                  actors = List.rev(!actors)                  funcs = List.rev(!funcs),
162                    init = (case !initially
163    (* FIXME: the check for the initially block should really happen in typechecking *)
164                       of NONE => raise Fail "missing initially declaration"
165                        | SOME blk => blk
166                      (* end case *)),
167                    strands = List.rev(!strands)
168                }                }
169            end            end
170    
171      and simplifyActor {name, params, state, methods} = let      and simplifyInit (isArray, AST.C_Create(strand, exps), iters) = let
172              val (stms, xs) = simplifyExpsToVars (exps, [])
173              val creat = S.C_Create{
174                      argInit = mkBlock stms,
175                      name = strand,
176                      args = xs
177                    }
178              fun simplifyIter (AST.I_Range(x, e1, e2), (iters, stms)) = let
179                    val (stms, lo) = simplifyExpToVar (e1, stms)
180                    val (stms, hi) = simplifyExpToVar (e2, stms)
181                    in
182                      ({param=x, lo=lo, hi=hi}::iters, stms)
183                    end
184              val (iters, stms) = List.foldl simplifyIter ([], []) iters
185              in
186                S.Initially{
187                    isArray = isArray,
188                    rangeInit = mkBlock stms,
189                    iters = List.rev iters,
190                    create = creat
191                  }
192              end
193    
194        and simplifyStrand (AST.Strand{name, params, state, methods}) = let
195            fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)            fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
196              | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let              | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let
197                  val (stms, e') = simplifyExp (e, stms)                  val (stms, e') = simplifyExp (e, stms)
# Line 70  Line 200 
200                  end                  end
201            val (xs, stm) = simplifyState (state, [], [])            val (xs, stm) = simplifyState (state, [], [])
202            in            in
203              S.Actor{              S.Strand{
204                  name = name,                  name = name,
205                  params = params,                  params = params,
206                  state = xs, stateInit = stm,                  state = xs, stateInit = stm,
# Line 81  Line 211 
211      and simplifyMethod (AST.M_Method(name, body)) =      and simplifyMethod (AST.M_Method(name, body)) =
212            S.Method(name, simplifyBlock body)            S.Method(name, simplifyBlock body)
213    
214    (* 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
215     * than one new statement.     * into more than one new statement).
216     *)     *)
217      and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))      and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))
218    
219      (* simplify the statement stm where stms is a reverse-order list of preceeding simplified
220       * statements.  This function returns a reverse-order list of simplified statements.
221       * Note that error reporting is done in the typechecker, but it does not prune unreachable
222       * code.
223       *)
224      and simplifyStmt (stm, stms) = (case stm      and simplifyStmt (stm, stms) = (case stm
225             of AST.S_Block body => let             of AST.S_Block body => let
226                  fun simplify ([], stms) = stms                  fun simplify ([], stms) = stms
227                    | simplify (stm::r, stms) = simplify (r, simplifyStmt (stm, stms))                    | simplify (stm::r, stms) = if contIsNext stm
228                          then simplify (r, simplifyStmt (stm, stms))
229                          else simplifyStmt (stm, stms)  (* prune unreachable statements *)
230                  in                  in
231                    simplify (body, stms)                    simplify (body, stms)
232                  end                  end
# Line 117  Line 254 
254                  end                  end
255              | AST.S_Die => S.S_Die :: stms              | AST.S_Die => S.S_Die :: stms
256              | AST.S_Stabilize => S.S_Stabilize :: stms              | AST.S_Stabilize => S.S_Stabilize :: stms
257                | AST.S_Return e => let
258                    val (stms, x) = simplifyExpToVar (e, stms)
259                    in
260                      S.S_Return x :: stms
261                    end
262                | AST.S_Print args => let
263                    val (stms, xs) = simplifyExpsToVars (args, stms)
264                    in
265                      S.S_Print xs :: stms
266                    end
267            (* end case *))            (* end case *))
268    
269      and simplifyExp (exp, stms) = (      and simplifyExp (exp, stms) = (
# Line 143  Line 290 
290                  in                  in
291                    (stms, S.E_Cons xs)                    (stms, S.E_Cons xs)
292                  end                  end
293              | AST.E_Slice(e, indices) => let (* tensor slicing *)              | AST.E_Seq es => let
294                    val (stms, xs) = simplifyExpsToVars (es, stms)
295                    in
296                      (stms, S.E_Seq xs)
297                    end
298                | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
299                  val (stms, x) = simplifyExpToVar (e, stms)                  val (stms, x) = simplifyExpToVar (e, stms)
300                    fun f ([], ys, stms) = (stms, List.rev ys)
301                      | f (NONE::es, ys, stms) = f (es, NONE::ys, stms)
302                      | f (SOME e::es, ys, stms) = let
303                          val (stms, y) = simplifyExpToVar (e, stms)
304                  in                  in
305                    raise Fail "FIXME"                          f (es, SOME y::ys, stms)
306                  end                  end
307              | AST.E_Cond(e1, e2, e3) => let                  val (stms, indices) = f (indices, [], stms)
308                    in
309                      (stms, S.E_Slice(x, indices, ty))
310                    end
311                | AST.E_Cond(e1, e2, e3, ty) => let
312                (* a conditional expression gets turned into an if-then-else statememt *)                (* a conditional expression gets turned into an if-then-else statememt *)
313                  val result = newTemp Ty.T_Bool                  val result = newTemp ty
314                  val (stms, x) = simplifyExpToVar (e1, stms)                  val (stms, x) = simplifyExpToVar (e1, S.S_Var result :: stms)
315                  fun simplifyBranch e = let                  fun simplifyBranch e = let
316                        val (stms, e) = simplifyExp (e, [])                        val (stms, e) = simplifyExp (e, [])
317                        in                        in
318                          mkBlock (S.S_Assign(result, e)::stms)                          mkBlock (S.S_Assign(result, e)::stms)
319                        end                        end
320                  val s1 = simplifyBranch e1                  val s1 = simplifyBranch e2
321                  val s2 = simplifyBranch e2                  val s2 = simplifyBranch e3
322                  in                  in
323                    (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)                    (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
324                  end                  end
325                | AST.E_LoadNrrd(_, nrrd, ty) => (case TypeUtil.prune ty
326                     of ty as Ty.T_DynSequence _ => (stms, S.E_LoadSeq(ty, nrrd))
327                      | ty as Ty.T_Image{dim, shape} => let
328                          val dim = TypeUtil.monoDim dim
329                          val shp = TypeUtil.monoShape shape
330                          in
331                            case ImageInfo.fromNrrd(NrrdInfo.getInfo nrrd, dim, shp)
332                             of NONE => raise Fail(concat[
333                                    "nrrd file \"", nrrd, "\" does not have expected type"
334                                  ])
335                              | SOME info => (stms, S.E_LoadImage(ty, nrrd, info))
336                            (* end case *)
337                          end
338                      | _ => raise Fail "bogus type for E_LoadNrrd"
339                    (* end case *))
340                | AST.E_Coerce{srcTy, dstTy, e} => let
341                    val (stms, x) = simplifyExpToVar (e, stms)
342                    val result = newTemp dstTy
343                    val rhs = S.E_Coerce{srcTy = cvtTy srcTy, dstTy = cvtTy dstTy, x = x}
344                    in
345                      (S.S_Assign(result, rhs)::stms, S.E_Var result)
346                    end
347            (* end case *))            (* end case *))
348    
349      and simplifyExpToVar (exp, stms) = let      and simplifyExpToVar (exp, stms) = let
# Line 188  Line 370 
370              f (exps, [], stms)              f (exps, [], stms)
371            end            end
372    
373      fun transform ast = let      fun transform (errStrm, ast) = let
374            val simple = simplifyProgram ast            val simple = simplifyProgram ast
375            val _ = SimplePP.output (Log.logFile(), simple)       (* DEBUG *)            val _ = SimplePP.output (Log.logFile(), "simplify", simple)   (* DEBUG *)
376              val simple = Inliner.transform simple
377              val _ = SimplePP.output (Log.logFile(), "inlining", simple)   (* DEBUG *)
378    (*
379            val simple = Lift.transform simple            val simple = Lift.transform simple
380                    handle Eval.Error msg => (Error.error(errStrm, msg); simple)
381    *)
382            in            in
383              simple              simple
384            end            end

Legend:
Removed from v.395  
changed lines
  Added in v.2328

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