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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
// SPDX-License-Identifier: Apache-2.0
package chiselTests.experimental
import chiselTests.ChiselFlatSpec
import chisel3._
import chisel3.RawModule
import chisel3.stage.{ChiselGeneratorAnnotation, ChiselStage}
import chisel3.util.experimental.group
import firrtl.analyses.InstanceGraph
import firrtl.options.TargetDirAnnotation
import firrtl.stage.CompilerAnnotation
import firrtl.{LowFirrtlCompiler, ir => fir}
import scala.collection.mutable
class GroupSpec extends ChiselFlatSpec {
def collectInstances(c: fir.Circuit, top: Option[String] = None): Seq[String] =
new InstanceGraph(c).fullHierarchy.values.flatten.toSeq
.map(v => (top.getOrElse(v.head.name) +: v.tail.map(_.name)).mkString("."))
def collectDeclarations(m: fir.DefModule): Set[String] = {
val decs = mutable.HashSet[String]()
def onStmt(s: fir.Statement): fir.Statement = s.mapStmt(onStmt) match {
case d: fir.IsDeclaration => decs += d.name; d
case other => other
}
m.mapStmt(onStmt)
decs.toSet
}
def lower[T <: RawModule](gen: () => T): fir.Circuit = {
(new ChiselStage)
.execute(Array("--compiler", "low", "--target-dir", "test_run_dir"), Seq(ChiselGeneratorAnnotation(gen)))
.collectFirst {
case firrtl.stage.FirrtlCircuitAnnotation(circuit) => circuit
}
.get
}
"Module Grouping" should "compile to low FIRRTL" in {
class MyModule extends Module {
val io = IO(new Bundle {
val a = Input(Bool())
val b = Output(Bool())
})
val reg1 = RegInit(0.U)
reg1 := io.a
val reg2 = RegNext(reg1)
io.b := reg2
group(Seq(reg1, reg2), "DosRegisters", "doubleReg")
}
val firrtlCircuit = lower(() => new MyModule)
firrtlCircuit.modules.collect {
case m: fir.Module if m.name == "MyModule" =>
Set("doubleReg") should be(collectDeclarations(m))
case m: fir.Module if m.name == "DosRegisters" =>
Set("reg1", "reg2") should be(collectDeclarations(m))
}
val instances = collectInstances(firrtlCircuit, Some("MyModule")).toSet
Set("MyModule", "MyModule.doubleReg") should be(instances)
}
"Module Grouping" should "not include intermediate registers" in {
class MyModule extends Module {
val io = IO(new Bundle {
val a = Input(Bool())
val b = Output(Bool())
})
val reg1 = RegInit(0.U)
reg1 := io.a
val reg2 = RegNext(reg1)
val reg3 = RegNext(reg2)
io.b := reg3
group(Seq(reg1, reg3), "DosRegisters", "doubleReg")
}
val firrtlCircuit = lower(() => new MyModule)
firrtlCircuit.modules.collect {
case m: fir.Module if m.name == "MyModule" =>
Set("reg2", "doubleReg") should be(collectDeclarations(m))
case m: fir.Module if m.name == "DosRegisters" =>
Set("reg1", "reg3") should be(collectDeclarations(m))
}
val instances = collectInstances(firrtlCircuit, Some("MyModule")).toSet
Set("MyModule", "MyModule.doubleReg") should be(instances)
}
"Module Grouping" should "include intermediate wires" in {
class MyModule extends Module {
val io = IO(new Bundle {
val a = Input(Bool())
val b = Output(Bool())
})
val reg1 = RegInit(0.U)
reg1 := io.a
val wire = WireInit(reg1)
val reg3 = RegNext(wire)
io.b := reg3
group(Seq(reg1, reg3), "DosRegisters", "doubleReg")
}
val firrtlCircuit = lower(() => new MyModule)
firrtlCircuit.modules.collect {
case m: fir.Module if m.name == "MyModule" =>
Set("doubleReg") should be(collectDeclarations(m))
case m: fir.Module if m.name == "DosRegisters" =>
Set("reg1", "reg3", "wire") should be(collectDeclarations(m))
}
val instances = collectInstances(firrtlCircuit, Some("MyModule")).toSet
Set("MyModule", "MyModule.doubleReg") should be(instances)
}
}
|