From e74529d2d13d2c361b028c2f0abb289566b25428 Mon Sep 17 00:00:00 2001 From: Schuyler Eldridge Date: Wed, 12 Feb 2020 15:28:50 -0500 Subject: Add InstanceGraph helpers: reachable/unreachable Adds new APIs for querying sets of modules in an InstanceGraph: - modules: the set of all modules - reachableModules: set of modules reachable from the main/top - unreachableModules: set of modules not reachable from the main/top Signed-off-by: Schuyler Eldridge --- src/main/scala/firrtl/analyses/InstanceGraph.scala | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src') diff --git a/src/main/scala/firrtl/analyses/InstanceGraph.scala b/src/main/scala/firrtl/analyses/InstanceGraph.scala index b8b40065..7b60b110 100644 --- a/src/main/scala/firrtl/analyses/InstanceGraph.scala +++ b/src/main/scala/firrtl/analyses/InstanceGraph.scala @@ -140,6 +140,16 @@ class InstanceGraph(c: Circuit) { def getChildrenInstanceMap: collection.Map[OfModule, collection.Map[Instance, OfModule]] = childInstances.map(kv => kv._1.OfModule -> asOrderedMap(kv._2, (i: WDefInstance) => i.toTokens)) + /** The set of all modules in the circuit */ + lazy val modules: collection.Set[OfModule] = graph.getVertices.map(_.OfModule) + + /** The set of all modules in the circuit reachable from the top module */ + lazy val reachableModules: collection.Set[OfModule] = + mutable.LinkedHashSet(trueTopInstance.OfModule) ++ graph.reachableFrom(trueTopInstance).map(_.OfModule) + + /** The set of all modules *not* reachable in the circuit */ + lazy val unreachableModules: collection.Set[OfModule] = modules diff reachableModules + } object InstanceGraph { -- cgit v1.2.3 From adf1a2f43d7e36dc3a34daa2b397ad5db60356a7 Mon Sep 17 00:00:00 2001 From: Schuyler Eldridge Date: Thu, 13 Feb 2020 11:02:11 -0500 Subject: Add tests for (Un)?reachable InstanceGraph Methods Signed-off-by: Schuyler Eldridge --- .../firrtlTests/analyses/InstanceGraphTests.scala | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src') diff --git a/src/test/scala/firrtlTests/analyses/InstanceGraphTests.scala b/src/test/scala/firrtlTests/analyses/InstanceGraphTests.scala index ee6ecd5f..8f748732 100644 --- a/src/test/scala/firrtlTests/analyses/InstanceGraphTests.scala +++ b/src/test/scala/firrtlTests/analyses/InstanceGraphTests.scala @@ -244,4 +244,22 @@ circuit Top : OfModule("Bar") -> 0) iGraph.staticInstanceCount should be (expectedCounts) } + + behavior of "Reachable/Unreachable helper methods" + + they should "report correct reachable/unreachable counts" in { + val input = + """|circuit Top: + | module Unreachable: + | skip + | module Reachable: + | skip + | module Top: + | inst reachable of Reachable + |""".stripMargin + val iGraph = new InstanceGraph(ToWorkingIR.run(parse(input))) + iGraph.modules should contain theSameElementsAs Seq(OfModule("Top"), OfModule("Reachable"), OfModule("Unreachable")) + iGraph.reachableModules should contain theSameElementsAs Seq(OfModule("Top"), OfModule("Reachable")) + iGraph.unreachableModules should contain theSameElementsAs Seq(OfModule("Unreachable")) + } } -- cgit v1.2.3