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

SCM Repository

[diderot] Diff of /branches/vis15/src/compiler/high-opt/normalize.sml
ViewVC logotype

Diff of /branches/vis15/src/compiler/high-opt/normalize.sml

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

revision 3535, Thu Dec 24 15:45:26 2015 UTC revision 4138, Fri Jul 1 14:46:08 2016 UTC
# Line 31  Line 31 
31      fun decUse (IR.V{useCnt, ...}) = (useCnt := !useCnt - 1)      fun decUse (IR.V{useCnt, ...}) = (useCnt := !useCnt - 1)
32      fun use x = (incUse x; x)      fun use x = (incUse x; x)
33    
34      fun isEin x = (case V.binding x  (*** OLD VERSION
35             of IR.VB_RHS(rhs as IR.EINAPP(e, arg)) => SOME rhs      fun getEinApp x = (case V.getDef x
36              | IR.VB_RHS(IR.VAR x') => isEin x'             of IR.EINAPP(e, arg) => SOME(e, arg)
37              | _ => NONE              | _ => NONE
38            (* end case *))            (* end case *))
39    ****)
40      (* get the EIN application that "x" is bound to (if any).  Note that we are conservative
41       * on following globals so as to avoid duplicating computation.
42       *)
43        fun getEinApp x = let
44              fun getEinRHS (IR.EINAPP app) = SOME app
45                | getEinRHS _ = NONE
46              in
47                case V.ty x
48                 of HighTypes.KernelTy => getEinRHS(V.getDef x)
49                  | HighTypes.FieldTy => getEinRHS(V.getDef x)
50                  | _ => getEinRHS(V.getLocalDef x)
51                (* end case *)
52              end
53    
54   (* doNormalize : EIN -> EIN   (* doNormalize : EIN -> EIN
55    * Orders EIN, normalizes it, then cleans the summation    * Orders EIN, normalizes it, then cleans the summation
56    *)    *)
57      fun doNormalize e = let      fun doNormalize e' = let
58            val ordered = Reorder.transform e  val _ =print(String.concat["\n\n\n do normalize:", EinPP.toString(e')])
59              val ordered = Reorder.transform e'
60            in            in
61              case NormalizeEin.transform ordered              case NormalizeEin.transform ordered
62               of NONE => ordered               of NONE => ordered
63                | SOME e => EinSums.clean e  | SOME e => (print(String.concat["\n transform :=>", EinPP.toString(e)]) ; EinSums.clean e)
64              (* end case *)              (* end case *)
65            end            end
66    
# Line 58  Line 73 
73     *)     *)
74      fun rewriteEin (params, place, changed, newE, newArgs, done, newEinApp, orig, lhs) = (      fun rewriteEin (params, place, changed, newE, newArgs, done, newEinApp, orig, lhs) = (
75            case List.nth(params, place)            case List.nth(params, place)
76             of Ein.TEN(0, _) => (changed, orig, place+1, done@[newEinApp])             of Ein.TEN(false, _) => (
77                    incUse lhs; incUse newEinApp;
78                    (changed, orig, place+1, done@[newEinApp]))
79              | _ => let              | _ => let
80                  val rtnArgs = done @ newArgs                  val rtnArgs = done @ newArgs
81                  val (c, subst) = Apply.apply(orig, place, newE)                  val (c, subst) = Apply.apply(orig, place, newE)
82    val _ =print(String.concat["\n\n\n after substition:", EinPP.toString(subst)])
83                  in                  in
84                    if c                    if c
85                      then (true, subst, place + length newArgs, rtnArgs)                      then (true, subst, place + length newArgs, rtnArgs)
# Line 71  Line 89 
89                  end                  end
90            (* end case *))            (* end case *))
91    
92    (* FIXME: it would be much more efficient to do all of the substitutions in one go,
93     * instead of repeatedly rewriting the term for each argument.
94     *)
95    (* doRHS: HighIR.var * rhs -> (var * rhs) list option    (* doRHS: HighIR.var * rhs -> (var * rhs) list option
96     * Looks at each argument to the original EINAPP.     * Looks at each argument to the original EINAPP.
97     * If it is another EIN APP calls rewriteEin to do application     * If it is another EIN APP calls rewriteEin to do application
# Line 78  Line 99 
99     * Keeps track of the place of the argument in substitution.     * Keeps track of the place of the argument in substitution.
100     *)     *)
101      fun doRHS (lhs, IR.EINAPP(ein, args)) = let      fun doRHS (lhs, IR.EINAPP(ein, args)) = let
102    val _ =print(String.concat["\n\n\n doRHS:", EinPP.toString(ein)])
103            fun rewrite (false, _, _, [], _) = NONE            fun rewrite (false, _, _, [], _) = NONE
104              | rewrite (true, orig, _, [], args') =              | rewrite (true, orig, _, [], args') =
105                  SOME[(lhs, IR.EINAPP(doNormalize orig, args'))]                  SOME[(lhs, IR.EINAPP(doNormalize orig, args'))]
106              | rewrite (changed, orig, place, e::es, args') = (case isEin e              | rewrite (changed, orig, place, e::es, args') = (case getEinApp e
107                   of NONE => rewrite(changed, orig, place+1, es, args'@[e])                   of NONE => rewrite(changed, orig, place+1, es, args'@[e])
108                    | SOME(IR.EINAPP(newE, newA)) => let                    | SOME(newE, newA) => let
109                        val Ein.EIN{params, index, body} = orig                        val Ein.EIN{params, index, body} = orig
110                        val (changed, e', place', done') =                        val (changed, e', place', done') =
111                              rewriteEin (params, place, changed, newE, newA, args', e, orig, lhs)                              rewriteEin (params, place, changed, newE, newA, args', e, orig, lhs)
112                        in                        in
113                          rewrite(changed, e', place', es, done')                          rewrite(changed, e', place', es, done')
114                        end                        end
                   | _ => raise Fail "isEin did not work"  
115                  (* end case *))                  (* end case *))
116            in            in
117              rewrite (false, ein, 0, args, [])              rewrite (false, ein, 0, args, [])
# Line 106  Line 127 
127          val elimUnusedVars = UnusedElim.reduce          val elimUnusedVars = UnusedElim.reduce
128        end)        end)
129    
130      val transform = Rewrite.transform      structure Promote = PromoteFn (IR)
131    
132        val transform = Promote.transform o Rewrite.transform
133    
134    (*DEBUG*
135    fun transform prog = let
136          val prog = Rewrite.transform prog
137          val _ = HighPP.output(Log.logFile(), "AFTER REWRITE", prog)
138          val prog = Promote.transform prog
139          val _ = HighPP.output(Log.logFile(), "AFTER PROMOTE", prog)
140          in
141            prog
142          end
143    *DEBUG*)
144    
145    end    end

Legend:
Removed from v.3535  
changed lines
  Added in v.4138

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