@@ -216,8 +216,7 @@ def replace_net_with_wire(new_wire):
216
216
217
217
218
218
def common_subexp_elimination (block = None , abs_thresh = 1 , percent_thresh = 0 ):
219
- """
220
- Common Subexpression Elimination for PyRTL blocks
219
+ """ Common Subexpression Elimination for PyRTL blocks.
221
220
222
221
:param block: the block to run the subexpression elimination on
223
222
:param abs_thresh: absolute threshold for stopping optimization
@@ -235,6 +234,18 @@ def common_subexp_elimination(block=None, abs_thresh=1, percent_thresh=0):
235
234
236
235
237
236
def _find_common_subexps (block ):
237
+ """ Finds nets that can be considered the same based on op type, op param, and arguments.
238
+
239
+ :param block: Block to operate over
240
+ :return dict[LogicNet, [LogicNet]]: mapping from a logic net (with a placehold dest)
241
+ representing the common subexp, to a list of nets matching that common subexp that
242
+ can be replaced with the single common subexp.
243
+
244
+ Nets are the "same" if 1) their op types are the same, 2) their op_params are
245
+ the same (e.g. same memory if a memory-related op), and 3) their arguments are
246
+ the same (same constant value and bitwidth for const wires, otherwise same wire
247
+ object). The destination wire for a net is not considered.
248
+ """
238
249
net_table = {} # {net (without dest) : [net, ...]
239
250
t = tuple () # just a placeholder
240
251
const_dict = {}
@@ -252,6 +263,11 @@ def _find_common_subexps(block):
252
263
253
264
254
265
def _const_to_int (wire , const_dict ):
266
+ """ Return a repr a Const (a tuple composed of width and value) for comparison with an 'is'.
267
+
268
+ If the wire is not a Const, just return the wire itself; comparison will be
269
+ done on the identity of the wire object instead.
270
+ """
255
271
if isinstance (wire , Const ):
256
272
# a very bad hack to make sure two consts will compare
257
273
# correctly with an 'is'
@@ -268,6 +284,12 @@ def _const_to_int(wire, const_dict):
268
284
269
285
270
286
def _replace_subexps (block , net_table ):
287
+ """ Removes unnecessary nets, connecting the common net's dest wire to unnecessary net's dest.
288
+
289
+ :param block: The block to operate over.
290
+ :param net_table: A mapping from common subexpression (a net) to a list of nets
291
+ that can be replaced with that common net.
292
+ """
271
293
wire_map = {}
272
294
unnecessary_nets = []
273
295
for nets in net_table .values ():
@@ -282,6 +304,16 @@ def _has_normal_dest_wire(net):
282
304
283
305
284
306
def _process_nets_to_discard (nets , wire_map , unnecessary_nets ):
307
+ """ Helper for tracking how a group of related nets should be replaced with a common one.
308
+
309
+ :param nets: List of nets that are considered equal and which should
310
+ be replaced by a single common net.
311
+ :param wire_map: Dict that will be updated with a mapping from every
312
+ old destination wire that needs to be removed, to the new destination
313
+ wire with which it should be replaced.
314
+ :param unnecessary_nets: List of nets that are to be discarded.
315
+
316
+ """
285
317
if len (nets ) == 1 :
286
318
return # also deals with nets with no dest wires
287
319
nets_to_consider = list (filter (_has_normal_dest_wire , nets ))
@@ -297,7 +329,9 @@ def _process_nets_to_discard(nets, wire_map, unnecessary_nets):
297
329
298
330
299
331
def _remove_unlistened_nets (block ):
300
- """ Removes all nets that are not connected to an output wirevector
332
+ """ Removes all nets that are not connected to an output wirevector.
333
+
334
+ :param block: The block to operate over.
301
335
"""
302
336
303
337
listened_nets = set ()
@@ -326,7 +360,12 @@ def add_to_listened(net):
326
360
327
361
328
362
def _remove_unused_wires (block , keep_inputs = True ):
329
- """ Removes all unconnected wires from a block"""
363
+ """ Removes all unconnected wires from a block's wirevector_set.
364
+
365
+ :param block: The block to operate over.
366
+ :param keep_inputs: If True, retain any Input wires that are not connected
367
+ to any net.
368
+ """
330
369
valid_wires = set ()
331
370
for logic_net in block .logic :
332
371
valid_wires .update (logic_net .args , logic_net .dests )
0 commit comments