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

Deprecate calling .viewAs on non-hardware (backport #3395) #3399

Merged
merged 2 commits into from
Jun 29, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ package object dataview {
// that are actually members of the target or view
val tex = unfoldView(te).find(x => targetContains(x) || x.isLit || x == DontCare).getOrElse(err("Target", te))
val vex = unfoldView(ve).find(viewFieldLookup.contains).getOrElse(err("View", ve))
if (!tex.isSynthesizable) {
Builder.deprecated(s".viewAs should only be called on hardware, this will become an error in Chisel 6.0.0")
}

(tex, vex) match {
/* Allow views where the types are equal. */
Expand Down
15 changes: 12 additions & 3 deletions core/src/main/scala/chisel3/internal/Builder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -815,9 +815,18 @@ private[chisel3] object Builder extends LazyLogging {
val absTarget = view.toAbsoluteTarget
val elts = getRecursiveFields.lazily(view, "").collect { case (elt: Element, _) => elt }
for (elt <- elts) {
val targetOfView = reify(elt)
renames.record(localTarget, targetOfView.toTarget)
renames.record(absTarget, targetOfView.toAbsoluteTarget)
// This is a hack to not crash when .viewAs is called on non-hardware
// It can be removed in Chisel 6.0.0 when it becomes illegal to call .viewAs on non-hardware
val targetOfViewOpt =
try {
Some(reify(elt))
} catch {
case _: NoSuchElementException => None
}
targetOfViewOpt.foreach { targetOfView =>
renames.record(localTarget, targetOfView.toTarget)
renames.record(absTarget, targetOfView.toAbsoluteTarget)
}
}
}
renames
Expand Down
11 changes: 11 additions & 0 deletions src/test/scala/chiselTests/experimental/DataView.scala
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,17 @@ class DataViewSpec extends ChiselFlatSpec {
}
}

// In Chisel 6.0.0 this will become an error, but for now it at least needs to not crash
it should "not crash when calling .viewAs on unbound hardware" in {
class MyBundle(val foo: UInt, val bar: UInt) extends Bundle
implicit val view =
DataView[(UInt, UInt), MyBundle](x => new MyBundle(x._1.cloneType, x._2.cloneType), _._1 -> _.foo, _._2 -> _.bar)
class MyModule extends Module {
(UInt(8.W), UInt(8.W)).viewAs[MyBundle]
}
ChiselStage.emitCHIRRTL(new MyModule)
}

it should "support literals as part of the target" in {
import ValidExtensions._
class MyModule extends Module {
Expand Down
Loading