Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix literal handling for views of empty Aggregates (backport #4071) #4074

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions core/src/main/scala/chisel3/Aggregate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ sealed abstract class Aggregate extends Data {
}

topBindingOpt match {
// Don't accidentally invent a literal value for a view that is empty
case Some(_: AggregateViewBinding) if this.getElements.isEmpty =>
reifySingleData(this) match {
case Some(target: Aggregate) => target.checkingLitOption(checkForDontCares)
case _ =>
val msg =
s"It should not be possible to have an empty Aggregate view that doesn't reify to a single target, but got $this"
Builder.exception(msg)(UnlocatableSourceInfo)
}
case Some(_: BundleLitBinding | _: VecLitBinding | _: AggregateViewBinding) =>
// Records store elements in reverse order and higher indices are more significant in Vecs
this.getElements.foldRight(Option(BigInt(0)))(shiftAdd)
Expand Down
34 changes: 34 additions & 0 deletions src/test/scala/chiselTests/experimental/DataView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import chisel3._
import chisel3.experimental.conversions._
import chisel3.experimental.dataview._
import chisel3.experimental.{Analog, HWTuple2}
import chisel3.experimental.BundleLiterals._
import chisel3.experimental.VecLiterals._
import chisel3.reflect.DataMirror.internal.chiselTypeClone
import chisel3.util.{Decoupled, DecoupledIO, Valid, ValidIO}
import chiselTests.ChiselFlatSpec
Expand Down Expand Up @@ -999,6 +1001,38 @@ class DataViewSpec extends ChiselFlatSpec {
ChiselStage.emitCHIRRTL(new MyModule)
}

it should "NOT invent literal values for views of empty Aggregates" in {
class MyModule extends Module {
val emptyBundle = IO(Output(new Bundle {}))
val emptyVec = IO(Output(Vec(0, UInt(8.W))))

val bundleView = emptyBundle.viewAs[Bundle]
val vecView = emptyVec.viewAs[Vec[UInt]]

emptyBundle.litOption should be(None)
bundleView.litOption should be(None)
emptyVec.litOption should be(None)
vecView.litOption should be(None)
}
ChiselStage.emitCHIRRTL(new MyModule)
}

it should "support literal values for views of empty Aggregates" in {
class MyModule extends Module {
val emptyBundle = (new Bundle {}).Lit()
val emptyVec = Vec(0, UInt(8.W)).Lit()

val bundleView = emptyBundle.viewAs[Bundle]
val vecView = emptyVec.viewAs[Vec[UInt]]

emptyBundle.litOption should be(Some(0))
bundleView.litOption should be(Some(0))
emptyVec.litOption should be(Some(0))
vecView.litOption should be(Some(0))
}
ChiselStage.emitCHIRRTL(new MyModule)
}

behavior.of("PartialDataView")

it should "still error if the mapping is non-total in the view" in {
Expand Down
Loading