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 5570 - (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 : jhr 5548 * COPYRIGHT (c) 2018 The University of Chicago
6 : jhr 3513 * 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 : jhr 5548 (* get the EIN application that "x" is bound to (if any). Note that we are
43 :     * conservative on following globals so as to avoid duplicating computation.
44 : jhr 4132 *)
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 :     val ordered = Reorder.transform e'
61 : cchiw 4219 val rtn = case NormalizeEin.transform ordered
62 : jhr 4317 of NONE => ordered
63 :     | SOME e => EinSums.clean e
64 :     (* end case *)
65 : cchiw 4219 in
66 :     rtn
67 : jhr 3513 end
68 :    
69 : jhr 5548 (* FIXME: what does this function do? *)
70 :     (* rewriteEin : _ -> boolean * EIN * int * HighIR.var list
71 :     *
72 :     * Parameters:
73 :     * changed -- boolean that is true if any rewriting has been done
74 :     * origParams -- Updated parameters for main EIN operator
75 :     * origEinOp -- Updated EIN operator for the main EIN operator
76 :     * origArgs -- Updated arguments for the main EIN operator
77 :     * place -- integer that represents which argument in main operator we are examining
78 :     * newLHS -- lhs variable for the new replaced argument
79 :     * newEinOp -- EIN operator for new replaced argument
80 :     * newArgs -- Arguments for the new replaced argument
81 :     *)
82 :     fun rewriteEin (changed, origParams, origEinOp, origArgs, place, newLHS, newEinOp, newArgs) = (
83 :     case List.nth(origParams, place)
84 : jhr 4317 of Ein.TEN(false, _) => (
85 : jhr 5548 (changed, origEinOp, place+1, origArgs@[newLHS]))
86 : jhr 5570 | _ => (case Apply.apply (origEinOp, place, newEinOp, newArgs, origArgs)
87 : jhr 5215 of SOME einOp => ( (* einOp is the result of the beta-reduction *)
88 : jhr 5548 decUse newLHS; List.app incUse newArgs;
89 :     (true, einOp, place + length newArgs, origArgs @ newArgs))
90 :     | NONE => ((* arg was unused in origEinOp so we can get rid of it *)
91 :     decUse newLHS;
92 :     (true, origEinOp, place, origArgs))
93 : jhr 5215 (* end case *))
94 : jhr 4317 (* 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 :     fun rewrite (false, _, _, [], _) = NONE
107 : jhr 5548 | rewrite (true, origEinOp, _, [], origArgs) =
108 :     SOME[(lhs, IR.EINAPP(doNormalize origEinOp, origArgs))]
109 :     | rewrite (changed, origEinOp, place, newLHS::xs, origArgs) = (
110 :     case getEinApp newLHS
111 :     of NONE => rewrite (changed, origEinOp, place+1, xs, origArgs@[ newLHS])
112 :     | SOME(newEinOp, newArgs) => let
113 :     val Ein.EIN{params, ...} = origEinOp
114 :     val (changed, origEinOp', place', origArgs') = rewriteEin (
115 :     changed, params, origEinOp, origArgs, place,
116 :     newLHS, newEinOp, newArgs)
117 : jhr 3513 in
118 : jhr 5548 rewrite (changed, origEinOp', place', xs, origArgs')
119 : jhr 3513 end
120 : jhr 4317 (* end case *))
121 : jhr 3513 in
122 : jhr 4317 rewrite (false, ein, 0, args, [])
123 : jhr 3513 end
124 :     | doRHS _ = NONE
125 :    
126 :     structure Rewrite = RewriteFn (
127 :     struct
128 :     structure IR = IR
129 :     val doAssign = doRHS
130 :    
131 :     fun doMAssign _ = NONE
132 :     val elimUnusedVars = UnusedElim.reduce
133 :     end)
134 :    
135 : jhr 3615 structure Promote = PromoteFn (IR)
136 : jhr 3513
137 : jhr 4822 val rewrite = Rewrite.transform
138 : jhr 4756 val promote = Promote.transform
139 :    
140 : jhr 3513 end

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