aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/firrtlTests/analyses/CircuitGraphSpec.scala
blob: dc06650d05dd181e841186909f2658a4b71c4008 (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
// SPDX-License-Identifier: Apache-2.0

package firrtlTests.analyses

import firrtl.analyses.CircuitGraph
import firrtl.annotations.CircuitTarget
import firrtl.options.Dependency
import firrtl.passes.ExpandWhensAndCheck
import firrtl.stage.{Forms, TransformManager}
import firrtl.testutils.FirrtlFlatSpec
import firrtl.{ChirrtlForm, CircuitState, FileUtils, UnknownForm}

class CircuitGraphSpec extends FirrtlFlatSpec {
  "CircuitGraph" should "find paths with deep hierarchy quickly" in {
    def mkChild(n: Int): String =
      s"""  module Child${n} :
         |    input in: UInt<8>
         |    output out: UInt<8>
         |    inst c1 of Child${n + 1}
         |    inst c2 of Child${n + 1}
         |    c1.in <= in
         |    c2.in <= c1.out
         |    out <= c2.out
         """.stripMargin
    def mkLeaf(n: Int): String =
      s"""  module Child${n} :
         |    input in: UInt<8>
         |    output out: UInt<8>
         |    wire middle: UInt<8>
         |    middle <= in
         |    out <= middle
         """.stripMargin
    (2 until 23 by 2).foreach { n =>
      val input = new StringBuilder()
      input ++=
        """circuit Child0:
          |""".stripMargin
      (0 until n).foreach { i => input ++= mkChild(i); input ++= "\n" }
      input ++= mkLeaf(n)
      val circuit = new firrtl.stage.transforms.Compiler(Seq(Dependency[ExpandWhensAndCheck]))
        .runTransform(
          CircuitState(parse(input.toString()), UnknownForm)
        )
        .circuit
      val circuitGraph = CircuitGraph(circuit)
      val C = CircuitTarget("Child0")
      val Child0 = C.module("Child0")
      circuitGraph.connectionPath(Child0.ref("in"), Child0.ref("out"))
    }
  }

}