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

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/simplify/simple-contract.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/simplify/simple-contract.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3485 - (view) (download)

1 : jhr 3468 (* simple-contract.sml
2 :     *
3 :     * This is a limited contraction phase for the SimpleAST representation. The purpose is
4 :     * to eliminate unused variables and dead code. Specifically, the following transformations
5 :     * are performed:
6 :     *
7 :     * -- unused constant and global variables are elminated
8 :     *
9 :     * -- unused strand state variables are eliminated (but not outputs)
10 :     *
11 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
12 :     *
13 :     * COPYRIGHT (c) 2015 The University of Chicago
14 :     * All rights reserved.
15 :     *)
16 :    
17 :     structure SimpleContract : sig
18 :    
19 :     val transform : Simple.program -> Simple.program
20 :    
21 :     end = struct
22 :    
23 :     structure S = Simple
24 :     structure SV = SimpleVar
25 :     structure ST = Stats
26 :    
27 :     (********** Counters for statistics **********)
28 :     val cntUnusedConst = ST.newCounter "simple-contract:unused-constant"
29 :     val cntUnusedGlobal = ST.newCounter "simple-contract:unused-global-var"
30 :     val cntUnusedState = ST.newCounter "simple-contract:unused-state-var"
31 :     val cntUnusedLocal = ST.newCounter "simple-contract:unused-local-var"
32 :     val cntDeadAssign = ST.newCounter "simple-contract:dead-assign"
33 :     val cntDeadIf = ST.newCounter "simple-contract:dead-if"
34 :     val cntDeadForeach = ST.newCounter "simple-contract:dead-foreach"
35 :     val firstCounter = cntUnusedConst
36 :     val lastCounter = cntDeadForeach
37 :    
38 :     fun sumChanges () = ST.sum {from=firstCounter, to=lastCounter}
39 :    
40 :     (* for constant, global, and strand-state variables, we count uses *)
41 :     local
42 :     val {clrFn, getFn, peekFn, ...} = SV.newProp (fn _ => ref 0)
43 :     in
44 :     fun use x = let val r = getFn x in r := !r + 1 end
45 :     fun unuse x = let val r = getFn x in r := !r - 1 end
46 :     fun markUsed x = (case SV.kindOf x
47 :     of SV.ConstVar => use x
48 :     | SV.GlobalVar => use x
49 :     | SV.StrandStateVar => use x
50 :     | SV.StrandOutputVar => use x
51 :     | SV.LocalVar => use x
52 :     | _ => ()
53 :     (* end case *))
54 :     fun isUsed x = (case peekFn x of SOME(ref n) => (n > 0) | _ => false)
55 :     fun clrUsedMark x = clrFn x
56 :     end (* local *)
57 :    
58 :     (* analyze a block for unused variables *)
59 :     fun analyzeBlock blk = let
60 :     fun analyzeBlk (S.Block stms) = List.app analyzeStm stms
61 :     and analyzeStm stm = (case stm
62 :     of S.S_Var(x, NONE) => ()
63 :     | S.S_Var(x, SOME e) => analyzeExp e
64 :     | S.S_Assign(x, e) => analyzeExp e
65 :     | S.S_IfThenElse(x, b1, b2) => (markUsed x; analyzeBlk b1; analyzeBlk b2)
66 :     | S.S_Foreach(x, xs, blk) => analyzeBlk blk
67 :     | S.S_New(strnd, xs) => List.app markUsed xs
68 :     | S.S_Continue => ()
69 :     | S.S_Die => ()
70 :     | S.S_Stabilize => ()
71 :     | S.S_Return x => markUsed x
72 :     | S.S_Print xs => List.app markUsed xs
73 :     | S.S_MapReduce{args, ...} => List.app markUsed args
74 :     (* end case *))
75 :     and analyzeExp exp = (case exp
76 :     of S.E_Var x => markUsed x
77 :     | S.E_Lit _ => ()
78 :     | S.E_Select(x, fld) => (markUsed x; markUsed fld)
79 :     | S.E_Apply(f, xs, _) => List.app markUsed xs
80 :     | S.E_Prim(_, _, xs, _) => List.app markUsed xs
81 :     | S.E_Tensor(xs, _) => List.app markUsed xs
82 :     | S.E_Seq(xs, _) => List.app markUsed xs
83 :     | S.E_Slice(x, indices, _) => (markUsed x; List.app (Option.app markUsed) indices)
84 :     | S.E_Coerce{x, ...} => markUsed x
85 :     | S.E_LoadSeq _ => ()
86 :     | S.E_LoadImage _ => ()
87 :     (* end case *))
88 :     in
89 :     analyzeBlk blk
90 :     end
91 :    
92 :     (* count the variable uses in a strand *)
93 :     fun analyzeStrand (S.Strand{state, stateInit, initM, updateM, stabilizeM, ...}) = (
94 :     (* mark all outputs as being used *)
95 :     List.app
96 :     (fn x => (case SV.kindOf x of SV.StrandOutputVar => use x | _ => ()))
97 :     state;
98 :     analyzeBlock stateInit;
99 :     Option.app analyzeBlock initM;
100 :     analyzeBlock updateM;
101 :     Option.app analyzeBlock stabilizeM)
102 :    
103 :     (* an initial pass to count the variable uses over the entire program *)
104 :     fun analyze (S.Program{constInit, init, strand, create, update, ...}) = (
105 :     analyzeBlock constInit;
106 :     analyzeBlock init;
107 :     analyzeStrand strand;
108 : jhr 3485 case create of S.Create{code, ...} => analyzeBlock code;
109 : jhr 3468 Option.app analyzeBlock update)
110 :    
111 :     (* rewrite a block and remove references to unused variables *)
112 :     fun contractBlock blk = let
113 :     fun delete exp = (case exp
114 :     of S.E_Var x => unuse x
115 :     | S.E_Lit _ => ()
116 :     | S.E_Select(x, fld) => (unuse x; unuse fld)
117 :     | S.E_Apply(f, xs, _) => List.app unuse xs
118 :     | S.E_Prim(_, _, xs, _) => List.app unuse xs
119 :     | S.E_Tensor(xs, _) => List.app unuse xs
120 :     | S.E_Seq(xs, _) => List.app unuse xs
121 :     | S.E_Slice(x, indices, _) => (unuse x; List.app (Option.app unuse) indices)
122 :     | S.E_Coerce{x, ...} => unuse x
123 :     | S.E_LoadSeq _ => ()
124 :     | S.E_LoadImage _ => ()
125 :     (* end case *))
126 :     fun contractBlk (S.Block stms) = let
127 :     fun contractStms [] = []
128 :     | contractStms (stm::stms) = (case stm
129 :     of S.S_Var(x, NONE) => if isUsed x
130 :     then stm :: contractStms stms
131 :     else (ST.tick cntUnusedLocal; contractStms stms)
132 :     | S.S_Var(x, SOME e) => if isUsed x
133 :     then stm :: contractStms stms
134 :     else (ST.tick cntUnusedLocal; delete e; contractStms stms)
135 :     | S.S_Assign(x, e) => if isUsed x
136 :     then stm :: contractStms stms
137 :     else (ST.tick cntDeadAssign; delete e; contractStms stms)
138 :     | S.S_IfThenElse(x, b1, b2) => (
139 :     case (contractBlk b1, contractBlk b2)
140 :     of (S.Block[], S.Block[]) => (
141 :     ST.tick cntDeadIf; unuse x; contractStms stms)
142 :     | (b1, b2) => S.S_IfThenElse(x, b1, b2) :: contractStms stms
143 :     (* end case *))
144 :     | S.S_Foreach(x, xs, blk) => (
145 :     case contractBlk blk
146 :     of S.Block[] => (
147 :     ST.tick cntDeadForeach; unuse xs; contractStms stms)
148 :     | blk => S.S_Foreach(x, xs, blk) :: contractStms stms
149 :     (* end case *))
150 :     | _ => stm :: contractStms stms
151 :     (* end case *))
152 :     in
153 :     S.Block (contractStms stms)
154 :     end
155 :     fun loop (nChanges, blk) = let
156 :     val blk = contractBlk blk
157 :     val n = sumChanges()
158 :     in
159 :     if (n <> nChanges) then loop (n, blk) else blk
160 :     end
161 :     in
162 :     loop (sumChanges(), blk)
163 :     end
164 :    
165 :     (* contract a strand *)
166 :     fun contractStrand strand = let
167 :     val S.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
168 :     in
169 :     S.Strand{
170 :     name = name, params = params, state = state,
171 :     stateInit = contractBlock stateInit,
172 :     initM = Option.map contractBlock initM,
173 :     updateM = contractBlock updateM,
174 :     stabilizeM = Option.map contractBlock stabilizeM
175 :     }
176 :     end
177 :    
178 :     (* contract a program *)
179 :     fun contractProg (nChanges, prog) = let
180 :     val S.Program{
181 :     props, consts, inputs, constInit, globals, funcs, init, strand, create, update
182 :     } = prog
183 :     val constInit = contractBlock constInit
184 :     val init = contractBlock init
185 :     val strand = contractStrand strand
186 :     val update = Option.map contractBlock update
187 :     val n = sumChanges()
188 :     in
189 :     List.app clrUsedMark consts;
190 :     List.app clrUsedMark globals;
191 :     if n = nChanges
192 :     then (n, prog)
193 :     else (n, S.Program{
194 :     props = props, consts = consts, inputs = inputs,
195 :     constInit = constInit, globals = globals, funcs = funcs,
196 :     init = init, strand = strand, create = create, update = update
197 :     })
198 :     end
199 :    
200 :     (* remove unused state variables from a strand and clear properties *)
201 :     fun finishStrand strand = let
202 :     val S.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
203 :     val (used, unused) = List.partition isUsed state
204 :     in
205 :     List.app clrUsedMark used;
206 :     if List.null unused
207 :     then strand
208 :     else S.Strand{
209 :     name = name, params = params, state = used,
210 :     stateInit = stateInit,
211 :     initM = initM, updateM = updateM, stabilizeM = stabilizeM
212 :     }
213 :     end
214 :    
215 :     (* remove unused constant, global, and state variables from the program *)
216 :     fun finishProg prog = let
217 :     val S.Program{
218 :     props, consts, inputs, constInit, globals, funcs, init, strand, create, update
219 :     } = prog
220 :     in
221 :     S.Program{
222 :     props = props,
223 :     consts = List.filter isUsed consts,
224 :     inputs = inputs,
225 :     constInit = constInit,
226 :     globals = List.filter isUsed globals,
227 :     funcs = funcs,
228 :     init = init,
229 :     strand = finishStrand strand,
230 :     create = create,
231 :     update = update
232 :     }
233 :     end
234 :    
235 :     fun transform prog = let
236 :     (* first we count the variable uses over the entire program *)
237 :     val () = analyze prog
238 :     (* then contract until there are no more changes *)
239 :     val n = sumChanges()
240 :     val (nChanges, prog) = let
241 :     fun loop (nChanges, prog) = let
242 :     val (n, prog) = contractProg (nChanges, prog)
243 :     in
244 :     if (n <> nChanges) then loop (n, prog) else (n, prog)
245 :     end
246 :     in
247 :     loop (n, prog)
248 :     end
249 :     in
250 :     (* finally we finish the program by removing unused constant, global, and state variables *)
251 :     if (n <> nChanges)
252 :     then finishProg prog
253 :     else prog (* no contraction occurred *)
254 :     end
255 :    
256 :     end

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