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 2156, Mon Feb 18 17:02:06 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      local      local
20        val tempName = Atom.atom "_t"        val tempName = Atom.atom "_t"
# Line 24  Line 25 
25    (* 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 *)
26      fun mkBlock stms = S.Block(List.rev stms)      fun mkBlock stms = S.Block(List.rev stms)
27    
28      (* convert an AST expression to an input initialization.  Note that the Diderot grammar
29       * limits the forms of expression that we might encounter in this context.
30       *)
31        fun expToInit (ty, exp) = (case exp
32               of AST.E_Lit(Literal.Int n) => InP.Int n
33                | AST.E_Lit(Literal.Float f) => InP.Real f
34                | AST.E_Lit(Literal.String s) => InP.String s
35                | AST.E_Lit(Literal.Bool b) => InP.Bool b
36                | AST.E_Tuple es => raise Fail "E_Tuple not yet implemented"
37                | AST.E_Cons es => let
38                    val shp = (case ty
39                           of Ty.T_Tensor(Ty.Shape shp) => List.map (fn (Ty.DimConst d) => d) shp
40                            | _ => raise Fail "not tensor type"
41                          (* end case *))
42                    fun flatten (AST.E_Lit(Literal.Int n), l) = FloatLit.fromInt n :: l
43                      | flatten (AST.E_Lit(Literal.Float f), l) = f :: l
44                      | flatten (AST.E_Coerce{e, ...}, l) = flatten(e, l)
45                      | flatten (AST.E_Cons es, l) = flattenList (es, l)
46                      | flatten _ = raise Fail "impossible"
47                    and flattenList ([], l) = l
48                      | flattenList (x::xs, l) = flatten(x, flattenList(xs, l))
49                    in
50                      InP.Tensor(shp, Vector.fromList(flattenList (es, [])))
51                    end
52    (*
53                | AST.E_Seq es => ??
54                | AST.E_Coerce{srcTy, dstTy, e} => ??
55    *)
56                | _ => raise Fail "impossible initialization expression"
57              (* end case *))
58    
59        fun inputImage (nrrd, dim, shape) = let
60              val dim = TypeUtil.monoDim dim
61              val shp = TypeUtil.monoShape shape
62              in
63                case ImageInfo.fromNrrd(NrrdInfo.getInfo nrrd, dim, shp)
64                 of NONE => raise Fail(concat["nrrd file \"", nrrd, "\" does not have expected type"])
65                  | SOME info => InP.Proxy(nrrd, info)
66                (* end case *)
67              end
68    
69      (* is the given statement's continuation the syntactically following statement? *)
70        fun contIsNext (AST.S_Block stms) = List.all contIsNext stms
71          | contIsNext (AST.S_IfThenElse(_, s1, s2)) = contIsNext s1 orelse contIsNext s2
72          | contIsNext AST.S_Die = false
73          | contIsNext AST.S_Stabilize = false
74          | contIsNext (AST.S_Return _) = false
75          | contIsNext _ = true
76    
77      fun simplifyProgram (AST.Program dcls) = let      fun simplifyProgram (AST.Program dcls) = let
78              val inputs = ref []
79            val globals = ref []            val globals = ref []
80            val globalInit = ref []            val globalInit = ref []
81            val actors = ref []            val funcs = ref []
82              val initially = ref NONE
83              val strands = ref []
84              fun setInitially init = (case !initially
85                     of NONE => initially := SOME init
86    (* FIXME: the check for multiple initially decls should happen in type checking *)
87                      | SOME _ => raise Fail "multiple initially declarations"
88                    (* end case *))
89            fun simplifyDecl dcl = (case dcl            fun simplifyDecl dcl = (case dcl
90                   of AST.D_Input(x, NONE) => let                   of AST.D_Input(x, desc, NONE) => let
91                        val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, NONE)                        val (ty, init) = (case Var.monoTypeOf x
92                                 of ty as Ty.T_Image{dim, shape} => let
93                                      val info = ImageInfo.mkInfo(TypeUtil.monoDim dim, TypeUtil.monoShape shape)
94                        in                        in
95                          globals := x :: !globals;                                      (ty, SOME(InP.Image info))
                         globalInit := S.S_Assign(x, e') :: !globalInit  
96                        end                        end
97                    | AST.D_Input(x, SOME e) => let                                | ty => (ty, NONE)
98                        val (stms, x') = simplifyExpToVar (e, [])                              (* end case *))
99                        val e' = S.E_Input(Var.monoTypeOf x, Var.nameOf x, SOME x')                        val inp = InP.INP{
100                                  ty = ty,
101                                  name = Var.nameOf x,
102                                  desc = desc,
103                                  init = init
104                                }
105                        in                        in
106                          globals := x :: !globals;                          inputs := (x, inp) :: !inputs
107                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)                        end
108                      | AST.D_Input(x, desc, SOME(AST.E_LoadNrrd(tvs, nrrd, ty))) => let
109                        (* load the nrrd proxy here *)
110                          val info = NrrdInfo.getInfo nrrd
111                          val (ty, init) = (case Var.monoTypeOf x
112                                 of ty as Ty.T_DynSequence _ => (ty, InP.DynSeq nrrd)
113                                  | ty as Ty.T_Image{dim, shape} => (ty, inputImage(nrrd, dim, shape))
114                                  | _ => raise Fail "impossible"
115                                (* end case *))
116                          val inp = InP.INP{
117                                  ty = ty,
118                                  name = Var.nameOf x,
119                                  desc = desc,
120                                  init = SOME init
121                                }
122                          in
123                            inputs := (x, inp) :: !inputs
124                          end
125                      | AST.D_Input(x, desc, SOME e) => let
126                          val ty = Var.monoTypeOf x
127                          val inp = InP.INP{
128                                  ty = ty,
129                                  name = Var.nameOf x,
130                                  desc = desc,
131                                  init = SOME(expToInit(ty, e))
132                                }
133                          in
134                            inputs := (x, inp) :: !inputs
135                        end                        end
136                    | AST.D_Var(AST.VD_Decl(x, e)) => let                    | AST.D_Var(AST.VD_Decl(x, e)) => let
137                        val (stms, e') = simplifyExp (e, [])                        val (stms, e') = simplifyExp (e, [])
# Line 48  Line 139 
139                          globals := x :: !globals;                          globals := x :: !globals;
140                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)                          globalInit := S.S_Assign(x, e') :: (stms @ !globalInit)
141                        end                        end
142                    | AST.D_Actor info => actors := simplifyActor info :: !actors                    | AST.D_Func(f, params, body) =>
143                    | AST.D_InitialArray(e, iters) => () (* FIXME *)                        funcs := S.Func{f=f, params=params, body=simplifyBlock body} :: !funcs
144                    | AST.D_InitialCollection(e, iters) => () (* FIXME *)                    | AST.D_Strand info => strands := simplifyStrand info :: !strands
145                      | AST.D_InitialArray(creat, iters) =>
146                          setInitially (simplifyInit(true, creat, iters))
147                      | AST.D_InitialCollection(creat, iters) =>
148                          setInitially (simplifyInit(false, creat, iters))
149                  (* end case *))                  (* end case *))
150            in            in
151              List.app simplifyDecl dcls;              List.app simplifyDecl dcls;
152              S.Program{              S.Program{
153                    inputs = List.rev(!inputs),
154                  globals = List.rev(!globals),                  globals = List.rev(!globals),
155                  globalInit = mkBlock (!globalInit),                  globalInit = mkBlock (!globalInit),
156                  actors = List.rev(!actors)                  funcs = List.rev(!funcs),
157                    init = (case !initially
158    (* FIXME: the check for the initially block should really happen in typechecking *)
159                       of NONE => raise Fail "missing initially declaration"
160                        | SOME blk => blk
161                      (* end case *)),
162                    strands = List.rev(!strands)
163                  }
164              end
165    
166        and simplifyInit (isArray, AST.C_Create(strand, exps), iters) = let
167              val (stms, xs) = simplifyExpsToVars (exps, [])
168              val creat = S.C_Create{
169                      argInit = mkBlock stms,
170                      name = strand,
171                      args = xs
172                    }
173              fun simplifyIter (AST.I_Range(x, e1, e2), (iters, stms)) = let
174                    val (stms, lo) = simplifyExpToVar (e1, stms)
175                    val (stms, hi) = simplifyExpToVar (e2, stms)
176                    in
177                      ({param=x, lo=lo, hi=hi}::iters, stms)
178                    end
179              val (iters, stms) = List.foldl simplifyIter ([], []) iters
180              in
181                S.Initially{
182                    isArray = isArray,
183                    rangeInit = mkBlock stms,
184                    iters = List.rev iters,
185                    create = creat
186                }                }
187            end            end
188    
189      and simplifyActor {name, params, state, methods} = let      and simplifyStrand {name, params, state, methods} = let
190            fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)            fun simplifyState ([], xs, stms) = (List.rev xs, mkBlock stms)
191              | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let              | simplifyState (AST.VD_Decl(x, e) :: r, xs, stms) = let
192                  val (stms, e') = simplifyExp (e, stms)                  val (stms, e') = simplifyExp (e, stms)
# Line 70  Line 195 
195                  end                  end
196            val (xs, stm) = simplifyState (state, [], [])            val (xs, stm) = simplifyState (state, [], [])
197            in            in
198              S.Actor{              S.Strand{
199                  name = name,                  name = name,
200                  params = params,                  params = params,
201                  state = xs, stateInit = stm,                  state = xs, stateInit = stm,
# Line 81  Line 206 
206      and simplifyMethod (AST.M_Method(name, body)) =      and simplifyMethod (AST.M_Method(name, body)) =
207            S.Method(name, simplifyBlock body)            S.Method(name, simplifyBlock body)
208    
209    (* 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
210     * than one new statement.     * into more than one new statement).
211     *)     *)
212      and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))      and simplifyBlock stm = mkBlock (simplifyStmt (stm, []))
213    
214      (* simplify the statement stm where stms is a reverse-order list of preceeding simplified
215       * statements.  This function returns a reverse-order list of simplified statements.
216       * Note that error reporting is done in the typechecker, but it does not prune unreachable
217       * code.
218       *)
219      and simplifyStmt (stm, stms) = (case stm      and simplifyStmt (stm, stms) = (case stm
220             of AST.S_Block body => let             of AST.S_Block body => let
221                  fun simplify ([], stms) = stms                  fun simplify ([], stms) = stms
222                    | simplify (stm::r, stms) = simplify (r, simplifyStmt (stm, stms))                    | simplify (stm::r, stms) = if contIsNext stm
223                          then simplify (r, simplifyStmt (stm, stms))
224                          else simplifyStmt (stm, stms)  (* prune unreachable statements *)
225                  in                  in
226                    simplify (body, stms)                    simplify (body, stms)
227                  end                  end
# Line 117  Line 249 
249                  end                  end
250              | AST.S_Die => S.S_Die :: stms              | AST.S_Die => S.S_Die :: stms
251              | AST.S_Stabilize => S.S_Stabilize :: stms              | AST.S_Stabilize => S.S_Stabilize :: stms
252                | AST.S_Return e => let
253                    val (stms, x) = simplifyExpToVar (e, stms)
254                    in
255                      S.S_Return x :: stms
256                    end
257                | AST.S_Print args => let
258                    val (stms, xs) = simplifyExpsToVars (args, stms)
259                    in
260                      S.S_Print xs :: stms
261                    end
262            (* end case *))            (* end case *))
263    
264      and simplifyExp (exp, stms) = (      and simplifyExp (exp, stms) = (
# Line 143  Line 285 
285                  in                  in
286                    (stms, S.E_Cons xs)                    (stms, S.E_Cons xs)
287                  end                  end
288              | AST.E_Slice(e, indices) => let (* tensor slicing *)              | AST.E_Seq es => let
289                    val (stms, xs) = simplifyExpsToVars (es, stms)
290                    in
291                      (stms, S.E_Seq xs)
292                    end
293                | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
294                  val (stms, x) = simplifyExpToVar (e, stms)                  val (stms, x) = simplifyExpToVar (e, stms)
295                    fun f ([], ys, stms) = (stms, List.rev ys)
296                      | f (NONE::es, ys, stms) = f (es, NONE::ys, stms)
297                      | f (SOME e::es, ys, stms) = let
298                          val (stms, y) = simplifyExpToVar (e, stms)
299                  in                  in
300                    raise Fail "FIXME"                          f (es, SOME y::ys, stms)
301                  end                  end
302              | AST.E_Cond(e1, e2, e3) => let                  val (stms, indices) = f (indices, [], stms)
303                    in
304                      (stms, S.E_Slice(x, indices, ty))
305                    end
306                | AST.E_Cond(e1, e2, e3, ty) => let
307                (* a conditional expression gets turned into an if-then-else statememt *)                (* a conditional expression gets turned into an if-then-else statememt *)
308                  val result = newTemp Ty.T_Bool                  val result = newTemp ty
309                  val (stms, x) = simplifyExpToVar (e1, stms)                  val (stms, x) = simplifyExpToVar (e1, S.S_Var result :: stms)
310                  fun simplifyBranch e = let                  fun simplifyBranch e = let
311                        val (stms, e) = simplifyExp (e, [])                        val (stms, e) = simplifyExp (e, [])
312                        in                        in
313                          mkBlock (S.S_Assign(result, e)::stms)                          mkBlock (S.S_Assign(result, e)::stms)
314                        end                        end
315                  val s1 = simplifyBranch e1                  val s1 = simplifyBranch e2
316                  val s2 = simplifyBranch e2                  val s2 = simplifyBranch e3
317                  in                  in
318                    (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)                    (S.S_IfThenElse(x, s1, s2) :: stms, S.E_Var result)
319                  end                  end
320                | AST.E_LoadNrrd(_, nrrd, ty) => (case TypeUtil.prune ty
321                     of ty as Ty.T_DynSequence _ => (stms, S.E_LoadSeq(ty, nrrd))
322                      | ty as Ty.T_Image{dim, shape} => let
323                          val dim = TypeUtil.monoDim dim
324                          val shp = TypeUtil.monoShape shape
325                          in
326                            case ImageInfo.fromNrrd(NrrdInfo.getInfo nrrd, dim, shp)
327                             of NONE => raise Fail(concat[
328                                    "nrrd file \"", nrrd, "\" does not have expected type"
329                                  ])
330                              | SOME info => (stms, S.E_LoadImage(ty, nrrd, info))
331                            (* end case *)
332                          end
333                      | _ => raise Fail "bogus type for E_LoadNrrd"
334                    (* end case *))
335                | AST.E_Coerce{srcTy, dstTy, e} => let
336                    val (stms, x) = simplifyExpToVar (e, stms)
337                    val result = newTemp dstTy
338                    in
339                      (S.S_Assign(result, S.E_Coerce{srcTy=srcTy, dstTy=dstTy, x=x})::stms, S.E_Var result)
340                    end
341            (* end case *))            (* end case *))
342    
343      and simplifyExpToVar (exp, stms) = let      and simplifyExpToVar (exp, stms) = let
# Line 188  Line 364 
364              f (exps, [], stms)              f (exps, [], stms)
365            end            end
366    
367      fun transform ast = let      fun transform (errStrm, ast) = let
368            val simple = simplifyProgram ast            val simple = simplifyProgram ast
369            val _ = SimplePP.output (Log.logFile(), simple)       (* DEBUG *)            val _ = SimplePP.output (Log.logFile(), "simplify", simple)   (* DEBUG *)
370              val simple = Inliner.transform simple
371              val _ = SimplePP.output (Log.logFile(), "inlining", simple)   (* DEBUG *)
372    (*
373            val simple = Lift.transform simple            val simple = Lift.transform simple
374                    handle Eval.Error msg => (Error.error(errStrm, msg); simple)
375    *)
376            in            in
377              simple              simple
378            end            end

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

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