Skip to content

Commit 4fa08f8

Browse files
authored
Add new InlineAllInstances trait (#4508)
The InlineInstance trait can be attached to a module, to cause firtool to inline instance of that module. The inline annotation is tricky to wield because, if an inline module dedups with another module, we will inline all instances of both modules. To make InlineInstance easier to use, whenever we mark a module as inline, we also mark it as "no dedup". This of course does not work well when we have a module which we want to inline AND dedup. So, this PR adds a new InlineAllInstances trait which allows a module to be marked inline without blocking dedup.
1 parent 59a9904 commit 4fa08f8

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/main/scala/chisel3/util/experimental/Inline.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ trait InlineInstance { self: BaseModule =>
5050
.map(chisel3.experimental.annotate(_))
5151
}
5252

53+
/** Inlines all instances of a module. If this module dedups with any other
54+
* module, instances of that other module will also be inlined.
55+
*/
56+
trait InlineInstanceAllowDedup { self: BaseModule =>
57+
chisel3.experimental.annotate(
58+
new ChiselAnnotation {
59+
def toFirrtl: Annotation = InlineAnnotation(self.toNamed)
60+
}
61+
)
62+
}
63+
5364
/** Flattens an instance of a module
5465
*
5566
* @example {{{
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package chiselTests
4+
5+
import chisel3._
6+
import circt.stage.ChiselStage
7+
import org.scalatest.matchers.should.Matchers
8+
import chiselTests.{ChiselFlatSpec, MatchesAndOmits}
9+
import chisel3.util.experimental.{InlineInstance, InlineInstanceAllowDedup}
10+
11+
class InlineInstanceSpec extends ChiselFlatSpec with MatchesAndOmits {
12+
class ModuleA extends RawModule {
13+
val w = dontTouch(WireInit(false.B))
14+
}
15+
16+
class ModuleB extends RawModule with InlineInstance {
17+
val w = dontTouch(WireInit(false.B))
18+
}
19+
20+
class TopModule extends RawModule {
21+
val a = Module(new ModuleA)
22+
val b = Module(new ModuleB)
23+
}
24+
25+
"InlineInstanceAllowDedup" should "Inline any module that dedups with a module marked inline" in {
26+
val verilog = ChiselStage.emitSystemVerilog(new TopModule)
27+
matchesAndOmits(verilog)(
28+
"module TopModule()",
29+
"module ModuleA();"
30+
)(
31+
"module ModuleB()"
32+
)
33+
}
34+
}
35+
36+
class InlineInstanceAllowDedupSpec extends ChiselFlatSpec with MatchesAndOmits {
37+
class ModuleA extends RawModule {
38+
val w = dontTouch(WireInit(false.B))
39+
}
40+
41+
class ModuleB extends RawModule with InlineInstanceAllowDedup {
42+
val w = dontTouch(WireInit(false.B))
43+
}
44+
45+
class TopModule extends RawModule {
46+
val a = Module(new ModuleA)
47+
val b = Module(new ModuleB)
48+
}
49+
50+
"InlineInstanceAllowDedup" should "Inline any module that dedups with a module marked inline" in {
51+
val verilog = ChiselStage.emitSystemVerilog(new TopModule)
52+
matchesAndOmits(verilog)(
53+
"module TopModule()"
54+
)(
55+
"module ModuleA()",
56+
"module ModuleB()"
57+
)
58+
}
59+
}

0 commit comments

Comments
 (0)