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 3995 - (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 : jhr 3510 (* for constant, global, strand-state variables, and local variables we count uses *)
41 : jhr 3468 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 : jhr 3510 fun useCnt x = (case peekFn x of SOME(ref n) => n | _ => 0)
55 :     fun isUsed x = (case SV.kindOf x
56 :     of SV.InputVar => true (* inputs are always in use *)
57 :     | SV.StrandOutputVar => true (* outputs are always in use *)
58 :     | _ => (case peekFn x of SOME(ref n) => (n > 0) | _ => false)
59 :     (* end case *))
60 : jhr 3468 fun clrUsedMark x = clrFn x
61 :     end (* local *)
62 :    
63 :     (* analyze a block for unused variables *)
64 :     fun analyzeBlock blk = let
65 : jhr 3501 fun analyzeBlk (S.Block{code, ...}) = List.app analyzeStm code
66 : jhr 3468 and analyzeStm stm = (case stm
67 :     of S.S_Var(x, NONE) => ()
68 :     | S.S_Var(x, SOME e) => analyzeExp e
69 :     | S.S_Assign(x, e) => analyzeExp e
70 :     | S.S_IfThenElse(x, b1, b2) => (markUsed x; analyzeBlk b1; analyzeBlk b2)
71 : jhr 3509 | S.S_Foreach(x, xs, blk) => (markUsed xs; analyzeBlk blk)
72 : jhr 3468 | S.S_New(strnd, xs) => List.app markUsed xs
73 :     | S.S_Continue => ()
74 :     | S.S_Die => ()
75 :     | S.S_Stabilize => ()
76 :     | S.S_Return x => markUsed x
77 :     | S.S_Print xs => List.app markUsed xs
78 :     | S.S_MapReduce{args, ...} => List.app markUsed args
79 :     (* end case *))
80 :     and analyzeExp exp = (case exp
81 :     of S.E_Var x => markUsed x
82 :     | S.E_Lit _ => ()
83 :     | S.E_Select(x, fld) => (markUsed x; markUsed fld)
84 :     | S.E_Apply(f, xs, _) => List.app markUsed xs
85 :     | S.E_Prim(_, _, xs, _) => List.app markUsed xs
86 :     | S.E_Tensor(xs, _) => List.app markUsed xs
87 :     | S.E_Seq(xs, _) => List.app markUsed xs
88 : jhr 3797 | S.E_Slice(x, _, _) => markUsed x
89 : jhr 3468 | S.E_Coerce{x, ...} => markUsed x
90 :     | S.E_LoadSeq _ => ()
91 :     | S.E_LoadImage _ => ()
92 :     (* end case *))
93 :     in
94 :     analyzeBlk blk
95 :     end
96 :    
97 :     (* count the variable uses in a strand *)
98 :     fun analyzeStrand (S.Strand{state, stateInit, initM, updateM, stabilizeM, ...}) = (
99 :     (* mark all outputs as being used *)
100 :     List.app
101 :     (fn x => (case SV.kindOf x of SV.StrandOutputVar => use x | _ => ()))
102 :     state;
103 :     analyzeBlock stateInit;
104 :     Option.app analyzeBlock initM;
105 :     analyzeBlock updateM;
106 :     Option.app analyzeBlock stabilizeM)
107 :    
108 :     (* an initial pass to count the variable uses over the entire program *)
109 : jhr 3995 fun analyze (S.Program{constInit, globInit, strand, create, init, update, ...}) = (
110 : jhr 3468 analyzeBlock constInit;
111 : jhr 3995 analyzeBlock globInit;
112 : jhr 3468 analyzeStrand strand;
113 : jhr 3485 case create of S.Create{code, ...} => analyzeBlock code;
114 : jhr 3995 Option.app analyzeBlock init;
115 : jhr 3468 Option.app analyzeBlock update)
116 :    
117 :     (* rewrite a block and remove references to unused variables *)
118 :     fun contractBlock blk = let
119 :     fun delete exp = (case exp
120 :     of S.E_Var x => unuse x
121 :     | S.E_Lit _ => ()
122 :     | S.E_Select(x, fld) => (unuse x; unuse fld)
123 :     | S.E_Apply(f, xs, _) => List.app unuse xs
124 :     | S.E_Prim(_, _, xs, _) => List.app unuse xs
125 :     | S.E_Tensor(xs, _) => List.app unuse xs
126 :     | S.E_Seq(xs, _) => List.app unuse xs
127 : jhr 3797 | S.E_Slice(x, _, _) => unuse x
128 : jhr 3468 | S.E_Coerce{x, ...} => unuse x
129 :     | S.E_LoadSeq _ => ()
130 :     | S.E_LoadImage _ => ()
131 :     (* end case *))
132 : jhr 3501 fun contractBlk (S.Block{props, code}) = let
133 : jhr 3468 fun contractStms [] = []
134 :     | contractStms (stm::stms) = (case stm
135 :     of S.S_Var(x, NONE) => if isUsed x
136 :     then stm :: contractStms stms
137 :     else (ST.tick cntUnusedLocal; contractStms stms)
138 :     | S.S_Var(x, SOME e) => if isUsed x
139 :     then stm :: contractStms stms
140 :     else (ST.tick cntUnusedLocal; delete e; contractStms stms)
141 :     | S.S_Assign(x, e) => if isUsed x
142 :     then stm :: contractStms stms
143 :     else (ST.tick cntDeadAssign; delete e; contractStms stms)
144 :     | S.S_IfThenElse(x, b1, b2) => (
145 :     case (contractBlk b1, contractBlk b2)
146 : jhr 3502 of (S.Block{code=[], ...}, S.Block{code=[], ...}) => (
147 : jhr 3468 ST.tick cntDeadIf; unuse x; contractStms stms)
148 :     | (b1, b2) => S.S_IfThenElse(x, b1, b2) :: contractStms stms
149 :     (* end case *))
150 :     | S.S_Foreach(x, xs, blk) => (
151 :     case contractBlk blk
152 : jhr 3501 of S.Block{code=[], ...} => (
153 : jhr 3468 ST.tick cntDeadForeach; unuse xs; contractStms stms)
154 :     | blk => S.S_Foreach(x, xs, blk) :: contractStms stms
155 :     (* end case *))
156 :     | _ => stm :: contractStms stms
157 :     (* end case *))
158 :     in
159 : jhr 3501 S.Block{props = props, code = contractStms code}
160 : jhr 3468 end
161 :     fun loop (nChanges, blk) = let
162 :     val blk = contractBlk blk
163 :     val n = sumChanges()
164 :     in
165 :     if (n <> nChanges) then loop (n, blk) else blk
166 :     end
167 :     in
168 :     loop (sumChanges(), blk)
169 :     end
170 :    
171 :     (* contract a strand *)
172 :     fun contractStrand strand = let
173 :     val S.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
174 :     in
175 :     S.Strand{
176 :     name = name, params = params, state = state,
177 :     stateInit = contractBlock stateInit,
178 :     initM = Option.map contractBlock initM,
179 :     updateM = contractBlock updateM,
180 :     stabilizeM = Option.map contractBlock stabilizeM
181 :     }
182 :     end
183 :    
184 :     (* contract a program *)
185 :     fun contractProg (nChanges, prog) = let
186 :     val S.Program{
187 : jhr 3995 props, consts, inputs, constInit, globals, funcs,
188 :     globInit, strand, create, init, update
189 : jhr 3468 } = prog
190 :     val constInit = contractBlock constInit
191 : jhr 3995 val globInit = contractBlock globInit
192 : jhr 3468 val strand = contractStrand strand
193 : jhr 3995 val init = Option.map contractBlock init
194 : jhr 3468 val update = Option.map contractBlock update
195 :     val n = sumChanges()
196 :     in
197 :     if n = nChanges
198 :     then (n, prog)
199 :     else (n, S.Program{
200 :     props = props, consts = consts, inputs = inputs,
201 :     constInit = constInit, globals = globals, funcs = funcs,
202 : jhr 3995 globInit = globInit, strand = strand, create = create,
203 :     init = init, update = update
204 : jhr 3468 })
205 :     end
206 :    
207 :     (* remove unused state variables from a strand and clear properties *)
208 :     fun finishStrand strand = let
209 :     val S.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
210 :     val (used, unused) = List.partition isUsed state
211 :     in
212 :     List.app clrUsedMark used;
213 :     if List.null unused
214 :     then strand
215 : jhr 3509 else (
216 :     List.app (fn _ => ST.tick cntUnusedState) unused;
217 :     S.Strand{
218 :     name = name, params = params, state = used,
219 :     stateInit = stateInit,
220 :     initM = initM, updateM = updateM, stabilizeM = stabilizeM
221 :     })
222 : jhr 3468 end
223 :    
224 :     (* remove unused constant, global, and state variables from the program *)
225 :     fun finishProg prog = let
226 :     val S.Program{
227 : jhr 3995 props, consts, inputs, constInit, globals, funcs,
228 :     globInit, strand, create, init, update
229 : jhr 3468 } = prog
230 : jhr 3509 fun removeUnused cntr x = if isUsed x
231 :     then true
232 :     else (ST.tick cntr; false)
233 : jhr 3510 val prog = S.Program{
234 :     props = props,
235 :     consts = List.filter (removeUnused cntUnusedConst) consts,
236 :     inputs = inputs,
237 :     constInit = constInit,
238 :     globals = List.filter (removeUnused cntUnusedGlobal) globals,
239 :     funcs = funcs,
240 : jhr 3995 globInit = globInit,
241 : jhr 3510 strand = finishStrand strand,
242 :     create = create,
243 : jhr 3995 init = init,
244 : jhr 3510 update = update
245 :     }
246 : jhr 3468 in
247 : jhr 3510 List.app clrUsedMark consts;
248 :     List.app clrUsedMark globals;
249 :     prog
250 : jhr 3468 end
251 :    
252 :     fun transform prog = let
253 :     (* first we count the variable uses over the entire program *)
254 :     val () = analyze prog
255 :     (* then contract until there are no more changes *)
256 :     val n = sumChanges()
257 :     val (nChanges, prog) = let
258 :     fun loop (nChanges, prog) = let
259 :     val (n, prog) = contractProg (nChanges, prog)
260 :     in
261 :     if (n <> nChanges) then loop (n, prog) else (n, prog)
262 :     end
263 :     in
264 :     loop (n, prog)
265 :     end
266 :     in
267 :     (* finally we finish the program by removing unused constant, global, and state variables *)
268 :     if (n <> nChanges)
269 :     then finishProg prog
270 :     else prog (* no contraction occurred *)
271 :     end
272 :    
273 :     end

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