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

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