Skip to content

Commit

Permalink
Wrap printfs in the Verification layerblock (#4506)
Browse files Browse the repository at this point in the history
Change the emission of printfs to emit these under the `Verification`
layerblock.  This is done as part of replacing the `firtool`
extract-test-code feature with layers and the "advanced layer sink"
`firtool` feature.

The extract-test-code feature will extract asserts _and_ printfs
together. This is technically a deviation from this behavior.  However, it
seems better to not lump these in with asserts.  Nonetheless, this is a
backwards compatible change from the perspective of extract-test-code
because the `Verification` layer is always loaded if the `Assert` layer is
present.  (Prints will always show up if you turn on assertions.)

In the future, we probably want to add a `Debug` layer under the
`Verification` layer where these will live.  I would like to hold off on
making this change until extract-test-code is gone.

Signed-off-by: Schuyler Eldridge <[email protected]>
  • Loading branch information
seldridge authored Nov 13, 2024
1 parent 7ade4df commit abaf2b2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
5 changes: 4 additions & 1 deletion core/src/main/scala-2/chisel3/PrintfMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package chisel3
import chisel3.internal._
import chisel3.internal.Builder.pushCommand
import chisel3.experimental.SourceInfo
import chisel3.{layer, layers}
import scala.language.experimental.macros
import scala.reflect.macros.blackbox

Expand Down Expand Up @@ -75,7 +76,9 @@ object PrintfMacrosCompat {

Printable.checkScope(pable)

pushCommand(chisel3.internal.firrtl.ir.Printf(printfId, sourceInfo, clock.ref, pable))
layer.block(layers.Verification, skipIfAlreadyInBlock = true, skipIfLayersEnabled = true) {
pushCommand(chisel3.internal.firrtl.ir.Printf(printfId, sourceInfo, clock.ref, pable))
}
printfId
}

Expand Down
9 changes: 4 additions & 5 deletions src/test/scala/chiselTests/Printf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,20 @@ import circt.stage.ChiselStage
class SinglePrintfTester() extends Module {
val x = 254.U
printf("x=%x", x)
stop()
}

class ASCIIPrintfTester() extends Module {
printf((0x20 to 0x7e).map(_.toChar).mkString.replace("%", "%%"))
stop()
}

class MultiPrintfTester() extends Module {
val x = 254.U
val y = 255.U
printf("x=%x y=%x", x, y)
stop()
}

class ASCIIPrintableTester extends Module {
printf(PString((0x20 to 0x7e).map(_.toChar).mkString("")))
stop()
}

class ScopeTesterModule extends Module {
Expand All @@ -42,7 +38,10 @@ class ScopeTesterModule extends Module {

class PrintfSpec extends ChiselFlatSpec {
"A printf with a single argument" should "elaborate" in {
ChiselStage.emitCHIRRTL(new SinglePrintfTester)
val chirrtl = ChiselStage.emitCHIRRTL(new SinglePrintfTester)

info("printfs are wrapped in the `Verification` layerblock by default")
chirrtl should include("layerblock Verification")
}
"A printf with multiple arguments" should "elaborate" in {
ChiselStage.emitCHIRRTL(new MultiPrintfTester)
Expand Down

0 comments on commit abaf2b2

Please sign in to comment.