Skip to content

Commit 6107183

Browse files
committed
[chisel3] Add layers to DesignAnnotation
Add a field to the `DesignAnnotation` that stores all layers that were generated in a run of Chisel. This is intended to be used by testing APIs that need to know what layers exist in order to turn them all on or to error if a user tries to enable a layer that does not exist. Signed-off-by: Schuyler Eldridge <[email protected]>
1 parent 2c15a6f commit 6107183

File tree

6 files changed

+27
-16
lines changed

6 files changed

+27
-16
lines changed

core/src/main/scala/chisel3/internal/Builder.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ private[chisel3] object Builder extends LazyLogging {
11301130
case layer.LayerConfig.Inline => LayerConfig.Inline
11311131
case layer.LayerConfig.Root => ???
11321132
}
1133-
Layer(l.sourceInfo, l.name, config, children.map(foldLayers).toSeq)
1133+
Layer(l.sourceInfo, l.name, config, children.map(foldLayers).toSeq, l)
11341134
}
11351135

11361136
val optionDefs = groupByIntoSeq(options)(opt => opt.group).map {

core/src/main/scala/chisel3/internal/firrtl/IR.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,11 @@ private[chisel3] object ir {
425425
}
426426

427427
final case class Layer(
428-
sourceInfo: SourceInfo,
429-
name: String,
430-
config: LayerConfig,
431-
children: Seq[Layer])
428+
sourceInfo: SourceInfo,
429+
name: String,
430+
config: LayerConfig,
431+
children: Seq[Layer],
432+
chiselLayer: layer.Layer)
432433

433434
class LayerBlock(val sourceInfo: SourceInfo, val layer: chisel3.layer.Layer) extends Command {
434435
val region = new Block(sourceInfo)

src/main/scala/chisel3/stage/ChiselAnnotations.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,9 @@ object ChiselOutputFileAnnotation extends HasShellOptions {
392392
* @param design top-level Chisel design
393393
* @tparam DUT Type of the top-level Chisel design
394394
*/
395-
case class DesignAnnotation[DUT <: RawModule](design: DUT) extends NoTargetAnnotation with Unserializable
395+
case class DesignAnnotation[DUT <: RawModule](design: DUT, layers: Seq[chisel3.layer.Layer] = Seq.empty)
396+
extends NoTargetAnnotation
397+
with Unserializable
396398

397399
/** Use legacy Chisel width behavior.
398400
*

src/main/scala/chisel3/stage/phases/Elaborate.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import chisel3.Module
66
import chisel3.experimental.hierarchy.core.Definition
77
import chisel3.internal.ExceptionHelpers.ThrowableHelpers
88
import chisel3.internal.{Builder, BuilderContextCache, DynamicContext}
9+
import chisel3.internal.firrtl.ir
910
import chisel3.stage.{
1011
ChiselCircuitAnnotation,
1112
ChiselGeneratorAnnotation,
@@ -53,9 +54,16 @@ class Elaborate extends Phase {
5354
BuilderContextCache.empty,
5455
chiselOptions.layerMap
5556
)
56-
val (circuit, dut) =
57+
val (circuit, dut) = {
5758
Builder.build(Module(gen()), context)
58-
Seq(ChiselCircuitAnnotation(circuit), DesignAnnotation(dut))
59+
}
60+
61+
// Extract the Chisel layers from a circuit via an in-order walk.
62+
def walkLayers(layer: ir.Layer, layers: Seq[chisel3.layer.Layer] = Nil): Seq[chisel3.layer.Layer] = {
63+
layer.children.foldLeft(layers :+ layer.chiselLayer) { case (acc, x) => walkLayers(x, acc) }
64+
}
65+
66+
Seq(ChiselCircuitAnnotation(circuit), DesignAnnotation(dut, layers = circuit.layers.flatMap(walkLayers(_))))
5967
} catch {
6068
/* if any throwable comes back and we're in "stack trace trimming" mode, then print an error and trim the stack trace
6169
*/

src/test/scala/chiselTests/aop/SelectSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class SelectSpec extends ChiselFlatSpec {
193193
}
194194
val top = ChiselGeneratorAnnotation(() => {
195195
new Top()
196-
}).elaborate.collectFirst { case DesignAnnotation(design: Top) => design }.get
196+
}).elaborate.collectFirst { case DesignAnnotation(design: Top, _) => design }.get
197197
Select.collectDeep(top) { case x => x } should equal(Seq(top, top.inst0))
198198
Select.getDeep(top)(x => Seq(x)) should equal(Seq(top, top.inst0))
199199
Select.instances(top) should equal(Seq(top.inst0))
@@ -220,7 +220,7 @@ class SelectSpec extends ChiselFlatSpec {
220220
}
221221
val top = ChiselGeneratorAnnotation(() => {
222222
new Top()
223-
}).elaborate.collectFirst { case DesignAnnotation(design: Top) => design }.get
223+
}).elaborate.collectFirst { case DesignAnnotation(design: Top, _) => design }.get
224224
intercept[Exception] { Select.collectDeep(top) { case x => x } }
225225
intercept[Exception] { Select.getDeep(top)(x => Seq(x)) }
226226
intercept[Exception] { Select.instances(top) }

src/test/scala/chiselTests/experimental/TraceSpec.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class TraceSpec extends ChiselFlatSpec with Matchers {
7272
}
7373

7474
val (testDir, annos) = compile("TraceFromAnnotations", () => new Module1)
75-
val dut = annos.collectFirst { case DesignAnnotation(dut) => dut }.get.asInstanceOf[Module1]
75+
val dut = annos.collectFirst { case DesignAnnotation(dut: Module1, _) => dut }.get
7676
// out of Builder.
7777

7878
val oneTarget = finalTarget(annos)(dut.m0.r.a.a).head
@@ -215,7 +215,7 @@ class TraceSpec extends ChiselFlatSpec with Matchers {
215215
}
216216

217217
val (_, annos) = compile("TraceFromCollideBundle", () => new CollideModule)
218-
val dut = annos.collectFirst { case DesignAnnotation(dut) => dut }.get.asInstanceOf[CollideModule]
218+
val dut = annos.collectFirst { case DesignAnnotation(dut: CollideModule, _) => dut }.get
219219

220220
val topName = "CollideModule"
221221

@@ -259,7 +259,7 @@ class TraceSpec extends ChiselFlatSpec with Matchers {
259259
}
260260

261261
val (_, annos) = compile("Inline", () => new Module1)
262-
val dut = annos.collectFirst { case DesignAnnotation(dut) => dut }.get.asInstanceOf[Module1]
262+
val dut = annos.collectFirst { case DesignAnnotation(dut: Module1, _) => dut }.get
263263

264264
val m0_i = finalTarget(annos)(dut.m0.i).head
265265
m0_i should be(refTarget("Module1", "m0_i"))
@@ -276,7 +276,7 @@ class TraceSpec extends ChiselFlatSpec with Matchers {
276276
}
277277

278278
val (_, annos) = compile("ConstantProp", () => new Module0)
279-
val dut = annos.collectFirst { case DesignAnnotation(dut) => dut }.get.asInstanceOf[Module0]
279+
val dut = annos.collectFirst { case DesignAnnotation(dut: Module0, _) => dut }.get
280280

281281
val i0 = finalTarget(annos)(dut.i0).head
282282
i0 should be(refTarget("Module0", "i0"))
@@ -319,7 +319,7 @@ class TraceSpec extends ChiselFlatSpec with Matchers {
319319
}
320320

321321
val (_, annos) = compile("NestedModule", () => new M3)
322-
val m3 = annos.collectFirst { case DesignAnnotation(dut) => dut }.get.asInstanceOf[M3]
322+
val m3 = annos.collectFirst { case DesignAnnotation(dut: M3, _) => dut }.get
323323

324324
val m2_m1_not = finalTarget(annos)(m3.m2.m1.bar).head
325325
val m2_not = finalTarget(annos)(m3.m2.foo).head
@@ -337,7 +337,7 @@ class TraceSpec extends ChiselFlatSpec with Matchers {
337337
Seq(a, b).foreach { a => traceName(a); dontTouch(a) }
338338
}
339339
val (_, annos) = compile("NestedModule", () => new M)
340-
val dut = annos.collectFirst { case DesignAnnotation(dut) => dut }.get.asInstanceOf[M]
340+
val dut = annos.collectFirst { case DesignAnnotation(dut: M, _) => dut }.get
341341
val allTargets = finalTargetMap(annos)
342342
allTargets(dut.a.toAbsoluteTarget) should be(Seq(refTarget("M", "a")))
343343
allTargets(dut.b(0).toAbsoluteTarget) should be(Seq(refTarget("M", "b_0")))

0 commit comments

Comments
 (0)