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

SCM Repository

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

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

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

revision 3465, Sun Nov 29 20:04:16 2015 UTC revision 3846, Wed May 11 13:35:09 2016 UTC
# Line 5  Line 5 
5   * COPYRIGHT (c) 2015 The University of Chicago   * COPYRIGHT (c) 2015 The University of Chicago
6   * All rights reserved.   * All rights reserved.
7   *   *
8   * Simplify the AST representation.   * Simplify the AST representation.  This phase involves the following transformations:
9     *
10     *      - types are simplified by removing meta variables (which will have been resolved)
11     *
12     *      - expressions are simplified to involve a single operation on variables
13     *
14     *      - global reductions are converted to MapReduce statements
15     *
16     *      - other comprehensions and reductions are converted to foreach loops
17     *
18     *      - unreachable code is pruned
19     *
20     *      - negation of literal integers and reals are constant folded
21   *)   *)
22    
23  structure Simplify : sig  structure Simplify : sig
# Line 31  Line 43 
43              | Ty.T_String => STy.T_String              | Ty.T_String => STy.T_String
44              | Ty.T_Sequence(ty, NONE) => STy.T_Sequence(cvtTy ty, NONE)              | Ty.T_Sequence(ty, NONE) => STy.T_Sequence(cvtTy ty, NONE)
45              | Ty.T_Sequence(ty, SOME dim) => STy.T_Sequence(cvtTy ty, SOME(TU.monoDim dim))              | Ty.T_Sequence(ty, SOME dim) => STy.T_Sequence(cvtTy ty, SOME(TU.monoDim dim))
46              | Ty.T_Named id => STy.T_Named id              | Ty.T_Strand id => STy.T_Strand id
47              | Ty.T_Kernel n => STy.T_Kernel(TU.monoDiff n)              | Ty.T_Kernel n => STy.T_Kernel(TU.monoDiff n)
48              | Ty.T_Tensor shape => STy.T_Tensor(TU.monoShape shape)              | Ty.T_Tensor shape => STy.T_Tensor(TU.monoShape shape)
49              | Ty.T_Image{dim, shape} => STy.T_Image{              | Ty.T_Image{dim, shape} => STy.T_Image{
# Line 47  Line 59 
59              | Ty.T_Error => raise Fail "unexpected T_Error in Simplify"              | Ty.T_Error => raise Fail "unexpected T_Error in Simplify"
60            (* end case *))            (* end case *))
61    
62        fun apiTypeOf x = let
63              fun cvtTy STy.T_Bool = APITypes.BoolTy
64                | cvtTy STy.T_Int = APITypes.IntTy
65                | cvtTy STy.T_String = APITypes.StringTy
66                | cvtTy (STy.T_Sequence(ty, len)) = APITypes.SeqTy(cvtTy ty, len)
67                | cvtTy (STy.T_Tensor shape) = APITypes.TensorTy shape
68                | cvtTy (STy.T_Image{dim, shape}) = APITypes.ImageTy(dim, shape)
69                | cvtTy ty = raise Fail "bogus API type"
70              in
71                cvtTy (SimpleVar.typeOf x)
72              end
73    
74      fun newTemp ty = SimpleVar.new ("_t", SimpleVar.LocalVar, ty)      fun newTemp ty = SimpleVar.new ("_t", SimpleVar.LocalVar, ty)
75    
76    (* a property to map AST variables to SimpleAST variables *)    (* a property to map AST variables to SimpleAST variables *)
# Line 59  Line 83 
83      fun cvtVars xs = List.map cvtVar xs      fun cvtVars xs = List.map cvtVar xs
84    
85    (* 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 *)
86      fun mkBlock stms = S.Block(List.rev stms)      fun mkBlock stms = S.Block{props = PropList.newHolder(), code = List.rev stms}
87    
88      fun inputImage (errStrm, nrrd, dim, shape) = (      fun inputImage (errStrm, nrrd, dim, shape) = (
89            case ImageInfo.fromNrrd(NrrdInfo.getInfo(errStrm, nrrd), dim, shape)            case ImageInfo.fromNrrd(NrrdInfo.getInfo(errStrm, nrrd), dim, shape)
# Line 74  Line 98 
98        | CONT                    (* stm sequence falls through *)        | CONT                    (* stm sequence falls through *)
99        | EDIT of 'a              (* pruned code that has non-exiting paths *)        | EDIT of 'a              (* pruned code that has non-exiting paths *)
100    
101      fun pruneUnreachableCode (blk as S.Block stms) = let      fun pruneUnreachableCode blk = let
102            fun isExit S.S_Die = true            fun isExit S.S_Die = true
103              | isExit S.S_Stabilize = true              | isExit S.S_Stabilize = true
104              | isExit (S.S_Return _) = true              | isExit (S.S_Return _) = true
# Line 129  Line 153 
153                    | (PRUNE blk1, EXIT      ) => PRUNE(S.S_IfThenElse(x, blk1, blk2))                    | (PRUNE blk1, EXIT      ) => PRUNE(S.S_IfThenElse(x, blk1, blk2))
154                    | (PRUNE blk1, PRUNE blk2) => PRUNE(S.S_IfThenElse(x, blk1, blk2))                    | (PRUNE blk1, PRUNE blk2) => PRUNE(S.S_IfThenElse(x, blk1, blk2))
155                  (* end case *))                  (* end case *))
156            and pruneBlk (S.Block stms) = (case pruneStms stms            and pruneBlk (S.Block{props, code}) = (case pruneStms code
157                   of PRUNE stms => PRUNE(S.Block stms)                   of PRUNE stms => PRUNE(S.Block{props=props, code=stms})
158                    | EDIT stms => EDIT(S.Block stms)                    | EDIT stms => EDIT(S.Block{props=props, code=stms})
159                    | EXIT => EXIT (* different instances of ctl_flow_info *)                    | EXIT => EXIT (* different instances of ctl_flow_info *)
160                    | CONT => CONT                    | CONT => CONT
161                  (* end case *))                  (* end case *))
# Line 311  Line 335 
335                    in                    in
336                      (foreachStm :: initStm :: stms, S.E_Var acc)                      (foreachStm :: initStm :: stms, S.E_Var acc)
337                    end                    end
338                | AST.E_ParallelMap(e, x, xs, ty) => raise Fail "FIXME"                | AST.E_ParallelMap(e, x, xs, ty) => raise Fail "FIXME: ParallelMap"
339                | AST.E_Tensor(es, ty) => let                | AST.E_Tensor(es, ty) => let
340                    val (stms, xs) = simplifyExpsToVars (errStrm, es, stms)                    val (stms, xs) = simplifyExpsToVars (errStrm, es, stms)
341                    in                    in
# Line 324  Line 348 
348                    end                    end
349                | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)                | AST.E_Slice(e, indices, ty) => let (* tensor slicing *)
350                    val (stms, x) = simplifyExpToVar (errStrm, e, stms)                    val (stms, x) = simplifyExpToVar (errStrm, e, stms)
351                    fun f ([], ys, stms) = (stms, List.rev ys)                    fun f NONE = NONE
352                      | f (NONE::es, ys, stms) = f (es, NONE::ys, stms)                      | f (SOME(AST.E_Lit(Literal.Int i))) = SOME(Int.fromLarge i)
353                      | f (SOME e::es, ys, stms) = let                      | f _ = raise Fail "expected integer literal in slice"
354                          val (stms, y) = simplifyExpToVar (errStrm, e, stms)                    val indices = List.map f indices
                         in  
                           f (es, SOME y::ys, stms)  
                         end  
                   val (stms, indices) = f (indices, [], stms)  
355                    in                    in
356                      (stms, S.E_Slice(x, indices, cvtTy ty))                      (stms, S.E_Slice(x, indices, cvtTy ty))
357                    end                    end
# Line 406  Line 426 
426                      | SOME e => let                      | SOME e => let
427                          val (stms, e') = simplifyExp (errStrm, e, stms)                          val (stms, e') = simplifyExp (errStrm, e, stms)
428                          in                          in
429                            simplifyState (r, x'::xs, S.S_Var(x', SOME e') :: stms)                            simplifyState (r, x'::xs, S.S_Assign(x', e') :: stms)
430                          end                          end
431                    (* end case *)                    (* end case *)
432                  end                  end
# Line 423  Line 443 
443                }                }
444            end            end
445    
446      fun simplifyCreate (errStrm, AST.C_Grid(dim, stm)) = S.C_Grid(dim, simplifyBlock errStrm stm)      fun simplifyCreate (errStrm, AST.C_Grid(d, blk)) =
447        | simplifyCreate (errStrm, AST.C_Collection stm) = S.C_Collection(simplifyBlock errStrm stm)            S.Create{dim = SOME d, code = simplifyBlock errStrm blk}
448          | simplifyCreate (errStrm, AST.C_Collection blk) =
449              S.Create{dim = NONE, code = simplifyBlock errStrm blk}
450    
451      fun transform (errStrm, prog) = let      fun transform (errStrm, prog) = let
452            val AST.Program{            val AST.Program{
# Line 446  Line 468 
468            fun simplifyInputDcl ((x, NONE), desc) = let            fun simplifyInputDcl ((x, NONE), desc) = let
469                  val x' = cvtVar x                  val x' = cvtVar x
470                  val init = (case SimpleVar.typeOf x'                  val init = (case SimpleVar.typeOf x'
471                         of SimpleTypes.T_Image{dim, shape} => let                         of STy.T_Image{dim, shape} => let
472                              val info = ImageInfo.mkInfo(dim, shape)                              val info = ImageInfo.mkInfo(dim, shape)
473                              in                              in
474                                S.Image info                                S.Image info
# Line 455  Line 477 
477                        (* end case *))                        (* end case *))
478                  val inp = S.INP{                  val inp = S.INP{
479                          var = x',                          var = x',
480                            name = Var.nameOf x,
481                            ty =  apiTypeOf x',
482                          desc = desc,                          desc = desc,
483                          init = init                          init = init
484                        }                        }
# Line 472  Line 496 
496                        (* end case *))                        (* end case *))
497                  val inp = S.INP{                  val inp = S.INP{
498                          var = x',                          var = x',
499                            name = Var.nameOf x,
500                            ty = apiTypeOf x',
501                          desc = desc,                          desc = desc,
502                          init = init                          init = init
503                        }                        }
# Line 483  Line 509 
509                  val (stms, e') = simplifyExp (errStrm, e, [])                  val (stms, e') = simplifyExp (errStrm, e, [])
510                  val inp = S.INP{                  val inp = S.INP{
511                          var = x',                          var = x',
512                            name = Var.nameOf x,
513                            ty = apiTypeOf x',
514                          desc = desc,                          desc = desc,
515                          init = S.ConstExpr                          init = S.ConstExpr
516                        }                        }

Legend:
Removed from v.3465  
changed lines
  Added in v.3846

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