From 01a68af64f454411efc75543e35d187e1e8cd19e Mon Sep 17 00:00:00 2001 From: Schuyler Eldridge Date: Wed, 24 Jan 2024 18:51:36 -0500 Subject: [PATCH 1/2] Add DataMirror.getLayerColor Add a utility to reflect on the optional color of a layer. This will be used by libraries that need to know if they need to put generated logic in a layerblock or not. Signed-off-by: Schuyler Eldridge --- .../scala/chisel3/reflect/DataMirror.scala | 9 ++++++++ .../chiselTests/reflect/DataMirrorSpec.scala | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/core/src/main/scala/chisel3/reflect/DataMirror.scala b/core/src/main/scala/chisel3/reflect/DataMirror.scala index 08827a369a3..0cf3ab34873 100644 --- a/core/src/main/scala/chisel3/reflect/DataMirror.scala +++ b/core/src/main/scala/chisel3/reflect/DataMirror.scala @@ -57,6 +57,15 @@ object DataMirror { */ def hasProbeTypeModifier(x: Data): Boolean = x.probeInfo.nonEmpty + /** Return the optional layer color of a `Data`. + * @param x the `Data` to examine + * @return a `Some[Layer]` if the data has a layer color, `None` otherwise + */ + def getLayerColor(x: Data): Option[layer.Layer] = x.probeInfo match { + case None => None + case Some(Data.ProbeInfo(_, color)) => color + } + /** Get an early guess for the name of this [[Data]] * * '''Warning: it is not guaranteed that this name will end up in the output FIRRTL or Verilog.''' diff --git a/src/test/scala/chiselTests/reflect/DataMirrorSpec.scala b/src/test/scala/chiselTests/reflect/DataMirrorSpec.scala index 99fd17f9295..a643d380cf3 100644 --- a/src/test/scala/chiselTests/reflect/DataMirrorSpec.scala +++ b/src/test/scala/chiselTests/reflect/DataMirrorSpec.scala @@ -3,6 +3,7 @@ package chiselTests.reflect import chisel3._ +import chisel3.probe.Probe import chisel3.reflect.DataMirror import chiselTests.ChiselFlatSpec import circt.stage.ChiselStage @@ -178,4 +179,24 @@ class DataMirrorSpec extends ChiselFlatSpec { assert(DataMirror.isFullyAligned(UInt(8.W))) } + "getLayerColor" should "return a layer color if one exists" in { + object A extends layer.Layer(layer.Convention.Bind) + class Foo extends Bundle { + val a = Bool() + val b = Probe(Bool()) + val c = Probe(Bool(), A) + } + + val foo = new Foo + + info("a non-probe returns None") + DataMirror.getLayerColor(foo.a) should be(None) + + info("an uncolored probe returns None") + DataMirror.getLayerColor(foo.b) should be(None) + + info("a probe colored with A returns Some(A)") + DataMirror.getLayerColor(foo.c) should be(Some(A)) + } + } From 2f1284f52e836357f845225483cbcc6a9267083a Mon Sep 17 00:00:00 2001 From: Schuyler Eldridge Date: Wed, 24 Jan 2024 19:07:52 -0500 Subject: [PATCH 2/2] fixup! Add DataMirror.getLayerColor --- core/src/main/scala/chisel3/reflect/DataMirror.scala | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/src/main/scala/chisel3/reflect/DataMirror.scala b/core/src/main/scala/chisel3/reflect/DataMirror.scala index 0cf3ab34873..2edf6b112e9 100644 --- a/core/src/main/scala/chisel3/reflect/DataMirror.scala +++ b/core/src/main/scala/chisel3/reflect/DataMirror.scala @@ -61,9 +61,8 @@ object DataMirror { * @param x the `Data` to examine * @return a `Some[Layer]` if the data has a layer color, `None` otherwise */ - def getLayerColor(x: Data): Option[layer.Layer] = x.probeInfo match { - case None => None - case Some(Data.ProbeInfo(_, color)) => color + def getLayerColor(x: Data): Option[layer.Layer] = x.probeInfo.collect { + case Data.ProbeInfo(_, Some(color)) => color } /** Get an early guess for the name of this [[Data]]