blob: 486c3be88f40cd4a35bdcafc6fe09b2669b4e1a4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
// SPDX-License-Identifier: Apache-2.0
package firrtl
package analyses
import ir._
import scala.annotation.tailrec
import scala.collection.JavaConverters._
/** This is not intended to be used as a metric for the size of an actual
* circuit, but rather to debug the compiler itself
* Works because all FirrtlNodes implement Product (case classes do)
*/
class NodeCount private (node: FirrtlNode) {
// Counts the number of unique objects in a Firrtl graph
// There is no IdentityHashSet
private val identityMap = new java.util.IdentityHashMap[Any, Boolean]()
// This is a strict subset of the keys of identityMap
private val regularSet = new collection.mutable.HashSet[Any]
@tailrec
private final def rec(xs: List[Any]): Unit =
if (xs.isEmpty) {} else {
val node = xs.head
require(
node.isInstanceOf[Product] || !node.isInstanceOf[FirrtlNode],
"Unexpected FirrtlNode that does not implement Product!"
)
val moreToVisit =
if (identityMap.containsKey(node)) List.empty
else { // Haven't seen yet
identityMap.put(node, true)
regularSet += node
node match { // FirrtlNodes are Products
case p: Product => p.productIterator
case i: Iterable[Any] => i
case _ => List.empty
}
}
rec(moreToVisit ++: xs.tail)
}
rec(List(node))
/** Number of nodes that are referentially unique
*
* !(a eq b)
*/
def unique: Long = identityMap.size
/** Number of nodes that are different
*
* !(a == b)
*/
def nonequivalent: Long = regularSet.size
/** Number of nodes in this NodeCount that are NOT present in that NodeCount */
def uniqueFrom(that: NodeCount): Long =
this.identityMap.keySet.asScala.count(!that.identityMap.containsKey(_))
}
object NodeCount {
def apply(node: FirrtlNode) = new NodeCount(node)
}
|