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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4756 - (view) (download)

1 : jhr 3513 (* normalize.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 :     *)
8 :    
9 :     structure Normalize : sig
10 :    
11 : jhr 4756 val rewrite : HighIR.program -> HighIR.program
12 : jhr 3513
13 : jhr 4756 val promote : HighIR.program -> HighIR.program
14 :    
15 : jhr 3513 end = struct
16 :    
17 :     structure IR = HighIR
18 :     structure Op = HighOps
19 :     structure V = IR.Var
20 : jhr 3517 structure ST = Stats
21 : jhr 3513
22 : jhr 3517 (********** Counters for statistics **********)
23 :     val cntUnused = ST.newCounter "high-opt:unused"
24 :    
25 : jhr 3513 structure UnusedElim = UnusedElimFn (
26 :     structure IR = IR
27 :     val cntUnused = cntUnused)
28 :    
29 :     fun useCount (IR.V{useCnt, ...}) = !useCnt
30 :    
31 :     (* adjust a variable's use count *)
32 :     fun incUse (IR.V{useCnt, ...}) = (useCnt := !useCnt + 1)
33 :     fun decUse (IR.V{useCnt, ...}) = (useCnt := !useCnt - 1)
34 :     fun use x = (incUse x; x)
35 :    
36 : jhr 4132 (*** OLD VERSION
37 : jhr 3587 fun getEinApp x = (case V.getDef x
38 : jhr 4317 of IR.EINAPP(e, arg) => SOME(e, arg)
39 :     | _ => NONE
40 :     (* end case *))
41 : jhr 4132 ****)
42 :     (* get the EIN application that "x" is bound to (if any). Note that we are conservative
43 :     * on following globals so as to avoid duplicating computation.
44 :     *)
45 :     fun getEinApp x = let
46 : jhr 4317 fun getEinRHS (IR.EINAPP app) = SOME app
47 :     | getEinRHS _ = NONE
48 :     in
49 :     case V.ty x
50 :     of HighTypes.KernelTy => getEinRHS(V.getDef x)
51 :     | HighTypes.FieldTy => getEinRHS(V.getDef x)
52 :     | _ => getEinRHS(V.getLocalDef x)
53 :     (* end case *)
54 :     end
55 : jhr 3513
56 : jhr 3520 (* doNormalize : EIN -> EIN
57 : jhr 3513 * Orders EIN, normalizes it, then cleans the summation
58 :     *)
59 : cchiw 3978 fun doNormalize e' = let
60 : cchiw 4555 (* DEBUG val _ =print(String.concat["\n\n\n do normalize:", EinPP.toString(e')])*)
61 : cchiw 3978 val ordered = Reorder.transform e'
62 : cchiw 4219 val rtn = case NormalizeEin.transform ordered
63 : jhr 4317 of NONE => ordered
64 :     | SOME e => EinSums.clean e
65 :     (* end case *)
66 : cchiw 4555 (* DEBUG val _ =print(String.concat["\n post sum normalize:", EinPP.toString(rtn)])*)
67 : cchiw 4219 in
68 :     rtn
69 : jhr 3513 end
70 :    
71 :     fun nameCnt e = String.concat[V.toString e, "(", Int.toString(useCount e), ")"]
72 :     fun nameCnts e = String.concatWithMap "" nameCnt e
73 :    
74 :     (* rewriteEin : EIN.Params*int*int*EIN*HighIR Vars* rhs list* EIN *HighIR Var
75 : jhr 4317 * -> int*EIN*int*rhs list
76 : jhr 4080 * Orders EIN, normalizes it, then cleans the summation orig-original EIN
77 : jhr 3513 *)
78 : jhr 3535 fun rewriteEin (params, place, changed, newE, newArgs, done, newEinApp, orig, lhs) = (
79 : jhr 4317 case List.nth(params, place)
80 :     of Ein.TEN(false, _) => (
81 :     incUse lhs; incUse newEinApp;
82 : cchiw 4087 (changed, orig, place+1, done@[newEinApp]))
83 : jhr 4317 | _ => let
84 :     val rtnArgs = done @ newArgs
85 :     val (c, subst) = Apply.apply(orig, place, newE)
86 : cchiw 4555 (* DEBUG val _ =print(String.concat["\n\n\n after substition:", EinPP.toString(subst)])*)
87 : jhr 4317 in
88 :     if c
89 :     then (true, subst, place + length newArgs, rtnArgs)
90 :     else (
91 :     incUse lhs; List.app incUse newArgs; decUse newEinApp;
92 :     (true, subst, place + length newArgs, rtnArgs))
93 :     end
94 :     (* end case *))
95 : jhr 3513
96 : jhr 4132 (* FIXME: it would be much more efficient to do all of the substitutions in one go,
97 :     * instead of repeatedly rewriting the term for each argument.
98 :     *)
99 : jhr 3513 (* doRHS: HighIR.var * rhs -> (var * rhs) list option
100 :     * Looks at each argument to the original EINAPP.
101 : jhr 3535 * If it is another EIN APP calls rewriteEin to do application
102 : jhr 3513 * "place"-The Param-id for the EIN operator.
103 :     * Keeps track of the place of the argument in substitution.
104 :     *)
105 :     fun doRHS (lhs, IR.EINAPP(ein, args)) = let
106 : cchiw 4555 (* DEBUG val _ =print(String.concat["\n\n\n doRHS:", EinPP.toString(ein)]) *)
107 : jhr 3513 fun rewrite (false, _, _, [], _) = NONE
108 :     | rewrite (true, orig, _, [], args') =
109 : jhr 4317 SOME[(lhs, IR.EINAPP(doNormalize orig, args'))]
110 : jhr 3587 | rewrite (changed, orig, place, e::es, args') = (case getEinApp e
111 : jhr 3513 of NONE => rewrite(changed, orig, place+1, es, args'@[e])
112 : jhr 3587 | SOME(newE, newA) => let
113 : jhr 3513 val Ein.EIN{params, index, body} = orig
114 :     val (changed, e', place', done') =
115 : jhr 4317 rewriteEin (params, place, changed, newE, newA, args', e, orig, lhs)
116 : jhr 3513 in
117 : jhr 4317 rewrite(changed, e', place', es, done')
118 : jhr 3513 end
119 : jhr 4317 (* end case *))
120 : jhr 3513 in
121 : jhr 4317 rewrite (false, ein, 0, args, [])
122 : jhr 3513 end
123 :     | doRHS _ = NONE
124 :    
125 :     structure Rewrite = RewriteFn (
126 :     struct
127 :     structure IR = IR
128 :     val doAssign = doRHS
129 :    
130 :     fun doMAssign _ = NONE
131 :     val elimUnusedVars = UnusedElim.reduce
132 :     end)
133 :    
134 : jhr 3615 structure Promote = PromoteFn (IR)
135 : jhr 3513
136 : jhr 4756 val rewrite = Rewrite.transform
137 :     val promote = Promote.transform
138 :    
139 :     (*
140 : jhr 3615 val transform = Promote.transform o Rewrite.transform
141 : jhr 4756 *)
142 : jhr 3615
143 : jhr 4756 (*DEBUG
144 : jhr 4032 fun transform prog = let
145 :     val prog = Rewrite.transform prog
146 :     val _ = HighPP.output(Log.logFile(), "AFTER REWRITE", prog)
147 :     val prog = Promote.transform prog
148 :     val _ = HighPP.output(Log.logFile(), "AFTER PROMOTE", prog)
149 :     in
150 : jhr 4317 prog
151 : jhr 4032 end
152 : jhr 4756 DEBUG*)
153 : jhr 4032
154 : jhr 3513 end

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