@@ -142,20 +142,32 @@ def keep_or_swap(p):
142142# that only produce non-proxy objects
143143# NOTE needed_proxies is an in/out argument, it takes an initial set of Variables you want to keep, and return
144144# all the needed proxies of the input trace
145- def dce (trace : Trace , needed_proxies : None | set [Variable ] = None ) -> Trace :
146- start_time_ns = time .perf_counter_ns ()
145+ def dce_bsyms (
146+ bsyms : list [BoundSymbolInterface ],
147+ output : Any ,
148+ needed_proxies : None | set [Variable ] = None ,
149+ ) -> Trace | list [BoundSymbolInterface ]:
150+ """Runs a Dead Code Elimination (DCE) pass
151+
152+ Args:
153+ bsyms: The list of bound symbols to run the DCE pass on.
154+ needed_proxies: The set of variables to keep.
155+ output: The output of the list of bound symbols.
147156
148- producer_map : ProxyDict = producers (trace )
157+ Returns:
158+ The list of bound symbols after the DCE pass.
159+ """
160+ producer_map : ProxyDict = producers (bsyms )
149161
150- flat_trace_outputs , _ = tree_flatten (trace . output )
162+ flat_trace_outputs , _ = tree_flatten (output )
151163 if needed_proxies is None :
152164 needed_proxies : set [Variable ] = set (tuple (variableify (x ) for x in flat_trace_outputs if isinstance (x , Proxy )))
153165 else :
154166 needed_proxies .update (tuple (variableify (x ) for x in flat_trace_outputs if isinstance (x , Proxy )))
155167 dced = []
156168
157169 bsym : BoundSymbol
158- for bsym in reversed (trace . bound_symbols ):
170+ for bsym in reversed (bsyms ):
159171 # Preserves symbols that should never be collected
160172 if has_tags (bsym , {prims .OpTags .DONT_DCE }):
161173 needed = True
@@ -182,19 +194,28 @@ def dce(trace: Trace, needed_proxies: None | set[Variable] = None) -> Trace:
182194 for x in nbsym .flat_proxy_args :
183195 needed_proxies .add (variableify (x ))
184196
185- dcetrace = from_trace (trace )
186197 dced_bound_symbols = list (reversed (dced ))
187198 # duplicate number proxies happen with the symbolic shapes and are
188199 # not covered by the above (due to being in tuples?).
189200 dced_bound_symbols = remove_duplicate_number_proxies (dced_bound_symbols )
190- dcetrace .bound_symbols = dced_bound_symbols
201+
202+ return dced_bound_symbols
203+
204+
205+ def dce (trace : Trace , needed_proxies : set [Variable ] = None ) -> Trace :
206+ start_time_ns = time .perf_counter_ns ()
207+
208+ bsyms = trace .bound_symbols
209+ dced_bsyms = dce_bsyms (bsyms , trace .output , needed_proxies )
210+ result = from_trace (trace )
211+ result .bound_symbols = dced_bsyms
191212
192213 end_time_ns = time .perf_counter_ns ()
193214 elapsed_time_ns = end_time_ns - start_time_ns
194215 elapsed_time_millis = elapsed_time_ns // 1000000
195- dcetrace .set_provenance (TraceProvenance (f"Dead Code Elimination (took { elapsed_time_millis } milliseconds)" ))
196216
197- return dcetrace
217+ result .set_provenance (TraceProvenance (f"Dead Code Elimination (took { elapsed_time_millis } milliseconds)" ))
218+ return result
198219
199220
200221#
0 commit comments