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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5040 - (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 : jhr 4815 fun decUse (IR.V{useCnt, ...}) = (useCnt := !useCnt - 1)
34 : jhr 3513 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 : cchiw 5040 | HighTypes.OFieldTy => getEinRHS(V.getDef x)
53 : jhr 4317 | _ => getEinRHS(V.getLocalDef x)
54 :     (* end case *)
55 :     end
56 : jhr 3513
57 : jhr 3520 (* doNormalize : EIN -> EIN
58 : jhr 3513 * Orders EIN, normalizes it, then cleans the summation
59 :     *)
60 : cchiw 3978 fun doNormalize e' = let
61 : cchiw 5022 val _ = ("\n\npost do normalize:"^EinPP.toString(e'))
62 : cchiw 3978 val ordered = Reorder.transform e'
63 : cchiw 4219 val rtn = case NormalizeEin.transform ordered
64 : jhr 4317 of NONE => ordered
65 :     | SOME e => EinSums.clean e
66 :     (* end case *)
67 : cchiw 4219 in
68 :     rtn
69 : jhr 3513 end
70 :    
71 : jhr 4814 (* FIXME: add documentation for this function's parameters and result *)
72 : jhr 4813 (* Orders EIN, normalizes it, then cleans the summation orig-original EIN
73 :     *
74 :     * changed -- boolean that is true if any rewriting has been done
75 :     * params
76 :     * place
77 : jhr 4815 * newEinOp
78 : jhr 4813 * newArgs
79 :     * done
80 : jhr 4815 * arg -- the argument that is bound to the application of newEinOp(newArgs)
81 : jhr 4813 * orig
82 :     * lhs
83 : jhr 3513 *)
84 : cchiw 4945 fun rewriteEin (changed, params, place, newEinOp, newArgs, done, arg, orig, lhs) = (
85 : cchiw 5022 ("\n\n*************************pre app:"^EinPP.toString(newEinOp));
86 : cchiw 4945 case List.nth(params, place)
87 : cchiw 5022 of Ein.TEN(false, _) => (
88 : jhr 4815 (changed, orig, place+1, done@[arg]))
89 : jhr 4821 | _ => (case Apply.apply (orig, place, newEinOp)
90 : cchiw 4999 of SOME einOp => let
91 : cchiw 5022 val _ = ("\n\n*************************post app:"^EinPP.toString(einOp));
92 : cchiw 4999 (* einOp is the result of the beta-reduction *)
93 :     in
94 :     (decUse arg; List.app incUse newArgs;
95 :     (true, einOp, place + length newArgs, done @ newArgs))
96 :     end
97 :     | NONE => ( (* arg was unused in orig, so we can get rid of it *)
98 :     decUse arg;
99 :     (true, orig, place, done))
100 :     (* end case *))
101 : jhr 4317 (* end case *))
102 : jhr 3513
103 : jhr 4132 (* FIXME: it would be much more efficient to do all of the substitutions in one go,
104 :     * instead of repeatedly rewriting the term for each argument.
105 :     *)
106 : jhr 3513 (* doRHS: HighIR.var * rhs -> (var * rhs) list option
107 :     * Looks at each argument to the original EINAPP.
108 : jhr 3535 * If it is another EIN APP calls rewriteEin to do application
109 : jhr 3513 * "place"-The Param-id for the EIN operator.
110 :     * Keeps track of the place of the argument in substitution.
111 :     *)
112 :     fun doRHS (lhs, IR.EINAPP(ein, args)) = let
113 : cchiw 5022 val _ = ("\ncentral ein"^EinPP.toString(ein))
114 :     fun rewrite (false, _, _, [], _) = (NONE)
115 :     | rewrite (true, einOp, _, [], args') =(
116 : cchiw 4999 SOME[(lhs, IR.EINAPP(doNormalize einOp, args'))])
117 : cchiw 5022 | rewrite (changed, einOp, place, x::xs, args') = (case getEinApp x
118 :     of NONE => (rewrite (changed, einOp, place+1, xs, args'@[x]))
119 : jhr 3587 | SOME(newE, newA) => let
120 : cchiw 5022
121 : cchiw 4999 val Ein.EIN{params, ...} = einOp
122 : cchiw 5022 val _ = ("\ninside ein"^EinPP.toString(einOp))
123 : jhr 4815 val (changed, einOp', place', done') =
124 :     rewriteEin (changed, params, place, newE, newA, args', x, einOp, lhs)
125 : jhr 3513 in
126 : jhr 4815 rewrite (changed, einOp', place', xs, done')
127 : jhr 3513 end
128 : jhr 4317 (* end case *))
129 : jhr 3513 in
130 : jhr 4317 rewrite (false, ein, 0, args, [])
131 : jhr 3513 end
132 :     | doRHS _ = NONE
133 :    
134 :     structure Rewrite = RewriteFn (
135 :     struct
136 :     structure IR = IR
137 :     val doAssign = doRHS
138 :    
139 :     fun doMAssign _ = NONE
140 :     val elimUnusedVars = UnusedElim.reduce
141 :     end)
142 :    
143 : jhr 3615 structure Promote = PromoteFn (IR)
144 : jhr 3513
145 : jhr 4822 val rewrite = Rewrite.transform
146 : jhr 4756 val promote = Promote.transform
147 :    
148 : jhr 3513 end

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