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 4191 - (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 :     val transform : HighIR.program -> HighIR.program
12 :    
13 :     end = struct
14 :    
15 :     structure IR = HighIR
16 :     structure Op = HighOps
17 :     structure V = IR.Var
18 : jhr 3517 structure ST = Stats
19 : jhr 3513
20 : jhr 3517 (********** Counters for statistics **********)
21 :     val cntUnused = ST.newCounter "high-opt:unused"
22 :    
23 : jhr 3513 structure UnusedElim = UnusedElimFn (
24 :     structure IR = IR
25 :     val cntUnused = cntUnused)
26 :    
27 :     fun useCount (IR.V{useCnt, ...}) = !useCnt
28 :    
29 :     (* adjust a variable's use count *)
30 :     fun incUse (IR.V{useCnt, ...}) = (useCnt := !useCnt + 1)
31 :     fun decUse (IR.V{useCnt, ...}) = (useCnt := !useCnt - 1)
32 :     fun use x = (incUse x; x)
33 :    
34 : jhr 4132 (*** OLD VERSION
35 : jhr 3587 fun getEinApp x = (case V.getDef x
36 :     of IR.EINAPP(e, arg) => SOME(e, arg)
37 : jhr 3513 | _ => NONE
38 :     (* end case *))
39 : jhr 4132 ****)
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 : jhr 3513
54 : jhr 3520 (* doNormalize : EIN -> EIN
55 : jhr 3513 * Orders EIN, normalizes it, then cleans the summation
56 :     *)
57 : cchiw 3978 fun doNormalize e' = let
58 : cchiw 4191 (* DEBUG val _ =print(String.concat["\n\n\n pre normalize:", EinPP.toString(e')]) *)
59 : cchiw 3978 val ordered = Reorder.transform e'
60 : cchiw 4191 val rtn = case NormalizeEin.transform ordered
61 : jhr 3520 of NONE => ordered
62 : jhr 4143 | SOME e => EinSums.clean e
63 : jhr 3520 (* end case *)
64 : cchiw 4191 (* val _ =print(String.concat["\n\n\n post normalize:", EinPP.toString(rtn)]) *)
65 :     in
66 :     rtn
67 : jhr 3513 end
68 :    
69 :     fun nameCnt e = String.concat[V.toString e, "(", Int.toString(useCount e), ")"]
70 :     fun nameCnts e = String.concatWithMap "" nameCnt e
71 :    
72 :     (* rewriteEin : EIN.Params*int*int*EIN*HighIR Vars* rhs list* EIN *HighIR Var
73 :     * -> int*EIN*int*rhs list
74 : jhr 4080 * Orders EIN, normalizes it, then cleans the summation orig-original EIN
75 : jhr 3513 *)
76 : jhr 3535 fun rewriteEin (params, place, changed, newE, newArgs, done, newEinApp, orig, lhs) = (
77 :     case List.nth(params, place)
78 : jhr 4088 of Ein.TEN(false, _) => (
79 :     incUse lhs; incUse newEinApp;
80 : cchiw 4087 (changed, orig, place+1, done@[newEinApp]))
81 : jhr 3513 | _ => let
82 :     val rtnArgs = done @ newArgs
83 : jhr 3521 val (c, subst) = Apply.apply(orig, place, newE)
84 : cchiw 4191 (* DEBUG val _ =print(String.concat["\n\n\n apply new:", EinPP.toString(newE)]) *)
85 :     (* DEBUG val _ =print(String.concat["\n\n\n apply orig:", EinPP.toString(orig)]) *)
86 :     (* DEBUG val _ =print(String.concat["\n\n\n after substition:", EinPP.toString(subst)])*)
87 : jhr 3513 in
88 : jhr 3521 if c
89 : jhr 3517 then (true, subst, place + length newArgs, rtnArgs)
90 : jhr 3513 else (
91 : jhr 3517 incUse lhs; List.app incUse newArgs; decUse newEinApp;
92 : jhr 3513 (true, subst, place + length newArgs, rtnArgs))
93 :     end
94 :     (* end case *))
95 :    
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 4191 (* DEBUG val _ =print(String.concat["\n\n\n*******************************************\n doRHS:", EinPP.toString(ein)]) *)
107 : jhr 3513 fun rewrite (false, _, _, [], _) = NONE
108 :     | rewrite (true, orig, _, [], args') =
109 : jhr 3517 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 :     rewriteEin (params, place, changed, newE, newA, args', e, orig, lhs)
116 :     in
117 :     rewrite(changed, e', place', es, done')
118 :     end
119 :     (* end case *))
120 :     in
121 :     rewrite (false, ein, 0, args, [])
122 :     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 3615 val transform = Promote.transform o Rewrite.transform
137 :    
138 : jhr 4168 (*DEBUG**
139 : jhr 4032 fun transform prog = let
140 :     val prog = Rewrite.transform prog
141 :     val _ = HighPP.output(Log.logFile(), "AFTER REWRITE", prog)
142 :     val prog = Promote.transform prog
143 :     val _ = HighPP.output(Log.logFile(), "AFTER PROMOTE", prog)
144 :     in
145 :     prog
146 :     end
147 : jhr 4168 **DEBUG*)
148 : jhr 4032
149 : jhr 3513 end

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