diff --git a/core/src/main/scala/chisel3/Layer.scala b/core/src/main/scala/chisel3/Layer.scala index b9cb7185a26..05c7e9572b4 100644 --- a/core/src/main/scala/chisel3/Layer.scala +++ b/core/src/main/scala/chisel3/Layer.scala @@ -123,17 +123,27 @@ object layer { * not a _proper_ ancestor requirement.) * * @param layer the layer this block is associated with + * @param skipIfAlreadyInBlock if true, then this will not create a layer + * block if already inside a layer block * @param thunk the Chisel code that goes into the layer block * @param sourceInfo a source locator * @throws java.lang.IllegalArgumentException if the layer of the currnet * layerblock is not an ancestor of the desired layer */ def block[A]( - layer: Layer - )(thunk: => A + layer: Layer, + skipIfAlreadyInBlock: Boolean = false + )(thunk: => A )( implicit sourceInfo: SourceInfo ): Unit = { + // Do nothing if we are already in a layer block and are not supposed to + // create new layer blocks. + if (skipIfAlreadyInBlock && Builder.layerStack.size > 1) { + thunk + return + } + val _layer = Builder.layerMap.getOrElse(layer, layer) var layersToCreate = List.empty[Layer] var currentLayer = _layer diff --git a/src/test/scala/chiselTests/LayerSpec.scala b/src/test/scala/chiselTests/LayerSpec.scala index 54117e3adbe..dd95a7b8260 100644 --- a/src/test/scala/chiselTests/LayerSpec.scala +++ b/src/test/scala/chiselTests/LayerSpec.scala @@ -69,6 +69,19 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits { )() } + they should "respect the 'skipIfAlreadyInBlock' parameter" in { + class Foo extends RawModule { + layer.block(A, skipIfAlreadyInBlock = true) { + // This will fail to compile if `skipIfAlreadyInBlock=false`. + layer.block(C, skipIfAlreadyInBlock = true) {} + } + } + + matchesAndOmits(ChiselStage.emitCHIRRTL(new Foo))( + "layerblock A" + )("layerblock C") + } + they should "allow for defines to layer-colored probes" in { class Foo extends RawModule {