From 1957f5ef5c43439144cf779a343707872ca92d6a Mon Sep 17 00:00:00 2001 From: mergify[bot] Date: Mon, 17 Oct 2022 22:50:55 +0000 Subject: Add opt-in AutoCloneType for Records (backport #2781) (#2785) * Add opt-in AutoCloneType for Records (#2781) There is a new trait, chisel3.experimental.AutoCloneType that is mixed in to Bundle and can optionally be mixed in to user-defined Records. The compiler plugin prints a deprecation warning on any user-defined implementation of cloneType, telling the user to mix in AutoCloneType before upgrading to 3.6. (cherry picked from commit a234fd48ac8f5942c38fef5797292014e407b586) # Conflicts: # core/src/main/scala/chisel3/Aggregate.scala # plugin/src/main/scala/chisel3/internal/plugin/BundleComponent.scala * Resolve backport conflicts * Do not make MixedVec extend AutoCloneType It is a binary incompatible change that can wait for 3.6. * Waive MiMa false positives Co-authored-by: Jack Koenig --- src/test/scala/chiselTests/AutoClonetypeSpec.scala | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src') diff --git a/src/test/scala/chiselTests/AutoClonetypeSpec.scala b/src/test/scala/chiselTests/AutoClonetypeSpec.scala index 5d2cd496..353ae58c 100644 --- a/src/test/scala/chiselTests/AutoClonetypeSpec.scala +++ b/src/test/scala/chiselTests/AutoClonetypeSpec.scala @@ -6,6 +6,8 @@ import chisel3._ import chisel3.testers.TestUtils import chisel3.util.QueueIO import chisel3.stage.ChiselStage.elaborate +import chisel3.experimental.AutoCloneType +import scala.collection.immutable.ListMap class BundleWithIntArg(val i: Int) extends Bundle { val out = UInt(i.W) @@ -72,6 +74,25 @@ class InheritingBundle extends QueueIO(UInt(8.W), 8) { val error = Output(Bool()) } +class RecordAutoCloneType[T <: Data](gen: T) extends Record with AutoCloneType { + lazy val elements = ListMap("value" -> gen) + // This is a weird thing to do, but as only Bundles have these methods, it should be legal + protected def _elementsImpl: Iterable[(String, Any)] = elements + protected def _usingPlugin = false +} + +// Records that don't mixin AutoCloneType should still be able to implement the related methods +// NOTE: This is a very weird thing to do, don't do it. +class RecordWithVerbotenMethods(w: Int) extends Record { + lazy val elements = ListMap("value" -> UInt(w.W)) + override def cloneType: this.type = (new RecordWithVerbotenMethods(w)).asInstanceOf[this.type] + // Verboten methods + protected def _usingPlugin = false + protected override def _cloneTypeImpl = this.cloneType + + protected def _elementsImpl: Iterable[(String, Any)] = Nil +} + class AutoClonetypeSpec extends ChiselFlatSpec with Utils { "Bundles with Scala args" should "not need clonetype" in { @@ -400,4 +421,24 @@ class AutoClonetypeSpec extends ChiselFlatSpec with Utils { } elaborate(new MyModule) } + + it should "support Records that mixin AutoCloneType" in { + class MyModule extends Module { + val gen = new RecordAutoCloneType(UInt(8.W)) + val in = IO(Input(gen)) + val out = IO(Output(gen)) + out := in + } + elaborate(new MyModule) + } + + it should "support Records that don't mixin AutoCloneType and use forbidden methods" in { + class MyModule extends Module { + val gen = new RecordWithVerbotenMethods(8) + val in = IO(Input(gen)) + val out = IO(Output(gen)) + out := in + } + elaborate(new MyModule) + } } -- cgit v1.2.3