Skip to content

Commit c819a75

Browse files
authored
Empty prefixes should be ignored in emission (#5057)
Previously they would result in an extra '_' in the same of the signal. Also delete duplicate implementation of prefix.
1 parent 68f12f5 commit c819a75

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

core/src/main/scala/chisel3/experimental/prefix.scala

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import chisel3.internal.{Builder, HasId}
2020
*/
2121
object prefix {
2222

23-
/** Use to add a prefix to any components generated in the provided scope
24-
* The prefix is the name of the provided which, which may not be known yet.
23+
/** Use to add a prefix to any components generated in the provided scope.
24+
*
25+
* The prefix is the name of the provided `HasId`, which may not be known yet.
2526
*
2627
* @param name The signal/instance whose name will be the prefix
2728
* @param f a function for which any generated components are given the prefix
@@ -37,10 +38,11 @@ object prefix {
3738
ret
3839
}
3940

40-
/** Use to add a prefix to any components generated in the provided scope
41+
/** Use to add a prefix to any components generated in the provided scope.
42+
*
4143
* The prefix is a string, which must be known when this function is used.
4244
*
43-
* @param name The name which will be the prefix
45+
* @param name The name which will be the prefix (and empty name is ignored)
4446
* @param f a function for which any generated components are given the prefix
4547
* @tparam T The return type of the provided function
4648
* @return The return value of the provided function
@@ -60,12 +62,7 @@ object prefix {
6062
// TODO(adkian-sifive) This is factored out because the Scala 3
6163
// compiler fails to diambiguate the apply overloads using the type
6264
// parameter
63-
private[chisel3] def applyString[T](name: String)(f: => T): T = {
64-
Builder.pushPrefix(name)
65-
val ret = f
66-
if (Builder.getPrefix.nonEmpty) Builder.popPrefix()
67-
ret
68-
}
65+
private[chisel3] def applyString[T](name: String)(f: => T): T = apply(name)(f)
6966
}
7067

7168
/** Use to eliminate any existing prefixes within the provided scope.

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ package object internal {
4444
builder.append('_')
4545
}
4646
prefix.foreach { p =>
47-
builder.append(p)
48-
builder.append('_')
47+
// Don't append _ if the prefix is empty
48+
if (p.nonEmpty) {
49+
builder.append(p)
50+
builder.append('_')
51+
}
4952
}
5053
if (temp) {
5154
// We've moved the leading _ to the front, drop it here

src/test/scala-2/chiselTests/naming/PrefixSpec.scala

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,4 +594,46 @@ class PrefixSpec extends AnyPropSpec with Matchers with FileCheck {
594594
}
595595
ChiselStage.emitCHIRRTL(new Test) should include("wire prefixed_wire :")
596596
}
597+
598+
property("Empty prefixes should be a no-op") {
599+
class MyModule extends Module {
600+
prefix("") {
601+
val w = Wire(UInt(3.W))
602+
}
603+
val x = prefix("") {
604+
val y = Wire(UInt(3.W))
605+
val z = Wire(UInt(3.W))
606+
y
607+
}
608+
}
609+
println(ChiselStage.emitCHIRRTL(new MyModule))
610+
ChiselStage
611+
.emitCHIRRTL(new MyModule)
612+
.fileCheck()(
613+
"""|CHECK: wire w :
614+
|CHECK: wire x :
615+
|CHECK: wire x_z :
616+
|""".stripMargin
617+
)
618+
}
619+
620+
property("skipPrefix should apply to empty prefixes") {
621+
class MyModule extends Module {
622+
// skipPrefix should remove the empty prefix, not x_
623+
val x = prefix("") {
624+
skipPrefix {
625+
val y = Wire(UInt(3.W))
626+
val z = Wire(UInt(3.W))
627+
y
628+
}
629+
}
630+
}
631+
ChiselStage
632+
.emitCHIRRTL(new MyModule)
633+
.fileCheck()(
634+
"""|CHECK: wire x :
635+
|CHECK: wire x_z :
636+
|""".stripMargin
637+
)
638+
}
597639
}

0 commit comments

Comments
 (0)