Skip to content

Commit 557bc50

Browse files
authored
Add skipIfAlreadyInBlock arg to layer.block (#4327)
Add an argument to `layer.block` which can be used to prevent a layer block from being created if the current builder state indicates that we are already inside a layer block. This API is intended to be used for library writers to provide a way to _maybe_ put an operation into a layer block. Specifically, this is intended for verification statements which should go into a default layer block (e.g., `Verification.Assert`), but should not do this if the Chisel user is already in another layer block. Signed-off-by: Schuyler Eldridge <[email protected]>
1 parent 074c075 commit 557bc50

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

core/src/main/scala/chisel3/Layer.scala

+12-2
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,27 @@ object layer {
123123
* not a _proper_ ancestor requirement.)
124124
*
125125
* @param layer the layer this block is associated with
126+
* @param skipIfAlreadyInBlock if true, then this will not create a layer
127+
* block if already inside a layer block
126128
* @param thunk the Chisel code that goes into the layer block
127129
* @param sourceInfo a source locator
128130
* @throws java.lang.IllegalArgumentException if the layer of the currnet
129131
* layerblock is not an ancestor of the desired layer
130132
*/
131133
def block[A](
132-
layer: Layer
133-
)(thunk: => A
134+
layer: Layer,
135+
skipIfAlreadyInBlock: Boolean = false
136+
)(thunk: => A
134137
)(
135138
implicit sourceInfo: SourceInfo
136139
): Unit = {
140+
// Do nothing if we are already in a layer block and are not supposed to
141+
// create new layer blocks.
142+
if (skipIfAlreadyInBlock && Builder.layerStack.size > 1) {
143+
thunk
144+
return
145+
}
146+
137147
val _layer = Builder.layerMap.getOrElse(layer, layer)
138148
var layersToCreate = List.empty[Layer]
139149
var currentLayer = _layer

src/test/scala/chiselTests/LayerSpec.scala

+13
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ class LayerSpec extends ChiselFlatSpec with Utils with MatchesAndOmits {
6969
)()
7070
}
7171

72+
they should "respect the 'skipIfAlreadyInBlock' parameter" in {
73+
class Foo extends RawModule {
74+
layer.block(A, skipIfAlreadyInBlock = true) {
75+
// This will fail to compile if `skipIfAlreadyInBlock=false`.
76+
layer.block(C, skipIfAlreadyInBlock = true) {}
77+
}
78+
}
79+
80+
matchesAndOmits(ChiselStage.emitCHIRRTL(new Foo))(
81+
"layerblock A"
82+
)("layerblock C")
83+
}
84+
7285
they should "allow for defines to layer-colored probes" in {
7386

7487
class Foo extends RawModule {

0 commit comments

Comments
 (0)