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 3510 - (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 :     | S.E_Slice(x, indices, _) => (markUsed x; List.app (Option.app markUsed) indices)
89 :     | 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 :     fun analyze (S.Program{constInit, init, strand, create, update, ...}) = (
110 :     analyzeBlock constInit;
111 :     analyzeBlock init;
112 :     analyzeStrand strand;
113 : jhr 3485 case create of S.Create{code, ...} => analyzeBlock code;
114 : jhr 3468 Option.app analyzeBlock update)
115 :    
116 :     (* rewrite a block and remove references to unused variables *)
117 :     fun contractBlock blk = let
118 :     fun delete exp = (case exp
119 :     of S.E_Var x => unuse x
120 :     | S.E_Lit _ => ()
121 :     | S.E_Select(x, fld) => (unuse x; unuse fld)
122 :     | S.E_Apply(f, xs, _) => List.app unuse xs
123 :     | S.E_Prim(_, _, xs, _) => List.app unuse xs
124 :     | S.E_Tensor(xs, _) => List.app unuse xs
125 :     | S.E_Seq(xs, _) => List.app unuse xs
126 :     | S.E_Slice(x, indices, _) => (unuse x; List.app (Option.app unuse) indices)
127 :     | S.E_Coerce{x, ...} => unuse x
128 :     | S.E_LoadSeq _ => ()
129 :     | S.E_LoadImage _ => ()
130 :     (* end case *))
131 : jhr 3501 fun contractBlk (S.Block{props, code}) = let
132 : jhr 3468 fun contractStms [] = []
133 :     | contractStms (stm::stms) = (case stm
134 :     of S.S_Var(x, NONE) => if isUsed x
135 :     then stm :: contractStms stms
136 :     else (ST.tick cntUnusedLocal; contractStms stms)
137 :     | S.S_Var(x, SOME e) => if isUsed x
138 :     then stm :: contractStms stms
139 :     else (ST.tick cntUnusedLocal; delete e; contractStms stms)
140 :     | S.S_Assign(x, e) => if isUsed x
141 :     then stm :: contractStms stms
142 :     else (ST.tick cntDeadAssign; delete e; contractStms stms)
143 :     | S.S_IfThenElse(x, b1, b2) => (
144 :     case (contractBlk b1, contractBlk b2)
145 : jhr 3502 of (S.Block{code=[], ...}, S.Block{code=[], ...}) => (
146 : jhr 3468 ST.tick cntDeadIf; unuse x; contractStms stms)
147 :     | (b1, b2) => S.S_IfThenElse(x, b1, b2) :: contractStms stms
148 :     (* end case *))
149 :     | S.S_Foreach(x, xs, blk) => (
150 :     case contractBlk blk
151 : jhr 3501 of S.Block{code=[], ...} => (
152 : jhr 3468 ST.tick cntDeadForeach; unuse xs; contractStms stms)
153 :     | blk => S.S_Foreach(x, xs, blk) :: contractStms stms
154 :     (* end case *))
155 :     | _ => stm :: contractStms stms
156 :     (* end case *))
157 :     in
158 : jhr 3501 S.Block{props = props, code = contractStms code}
159 : jhr 3468 end
160 :     fun loop (nChanges, blk) = let
161 :     val blk = contractBlk blk
162 :     val n = sumChanges()
163 :     in
164 :     if (n <> nChanges) then loop (n, blk) else blk
165 :     end
166 :     in
167 :     loop (sumChanges(), blk)
168 :     end
169 :    
170 :     (* contract a strand *)
171 :     fun contractStrand strand = let
172 :     val S.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
173 :     in
174 :     S.Strand{
175 :     name = name, params = params, state = state,
176 :     stateInit = contractBlock stateInit,
177 :     initM = Option.map contractBlock initM,
178 :     updateM = contractBlock updateM,
179 :     stabilizeM = Option.map contractBlock stabilizeM
180 :     }
181 :     end
182 :    
183 :     (* contract a program *)
184 :     fun contractProg (nChanges, prog) = let
185 :     val S.Program{
186 :     props, consts, inputs, constInit, globals, funcs, init, strand, create, update
187 :     } = prog
188 :     val constInit = contractBlock constInit
189 :     val init = contractBlock init
190 :     val strand = contractStrand strand
191 :     val update = Option.map contractBlock update
192 :     val n = sumChanges()
193 :     in
194 :     if n = nChanges
195 :     then (n, prog)
196 :     else (n, S.Program{
197 :     props = props, consts = consts, inputs = inputs,
198 :     constInit = constInit, globals = globals, funcs = funcs,
199 :     init = init, strand = strand, create = create, update = update
200 :     })
201 :     end
202 :    
203 :     (* remove unused state variables from a strand and clear properties *)
204 :     fun finishStrand strand = let
205 :     val S.Strand{name, params, state, stateInit, initM, updateM, stabilizeM} = strand
206 :     val (used, unused) = List.partition isUsed state
207 :     in
208 :     List.app clrUsedMark used;
209 :     if List.null unused
210 :     then strand
211 : jhr 3509 else (
212 :     List.app (fn _ => ST.tick cntUnusedState) unused;
213 :     S.Strand{
214 :     name = name, params = params, state = used,
215 :     stateInit = stateInit,
216 :     initM = initM, updateM = updateM, stabilizeM = stabilizeM
217 :     })
218 : jhr 3468 end
219 :    
220 :     (* remove unused constant, global, and state variables from the program *)
221 :     fun finishProg prog = let
222 :     val S.Program{
223 :     props, consts, inputs, constInit, globals, funcs, init, strand, create, update
224 :     } = prog
225 : jhr 3509 fun removeUnused cntr x = if isUsed x
226 :     then true
227 :     else (ST.tick cntr; false)
228 : jhr 3510 val prog = S.Program{
229 :     props = props,
230 :     consts = List.filter (removeUnused cntUnusedConst) consts,
231 :     inputs = inputs,
232 :     constInit = constInit,
233 :     globals = List.filter (removeUnused cntUnusedGlobal) globals,
234 :     funcs = funcs,
235 :     init = init,
236 :     strand = finishStrand strand,
237 :     create = create,
238 :     update = update
239 :     }
240 : jhr 3468 in
241 : jhr 3510 List.app clrUsedMark consts;
242 :     List.app clrUsedMark globals;
243 :     prog
244 : jhr 3468 end
245 :    
246 :     fun transform prog = let
247 :     (* first we count the variable uses over the entire program *)
248 :     val () = analyze prog
249 :     (* then contract until there are no more changes *)
250 :     val n = sumChanges()
251 :     val (nChanges, prog) = let
252 :     fun loop (nChanges, prog) = let
253 :     val (n, prog) = contractProg (nChanges, prog)
254 :     in
255 :     if (n <> nChanges) then loop (n, prog) else (n, prog)
256 :     end
257 :     in
258 :     loop (n, prog)
259 :     end
260 :     in
261 :     (* finally we finish the program by removing unused constant, global, and state variables *)
262 :     if (n <> nChanges)
263 :     then finishProg prog
264 :     else prog (* no contraction occurred *)
265 :     end
266 :    
267 :     end

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