From edd8ff602b0a961ff1e169bf202e454d28bb0372 Mon Sep 17 00:00:00 2001 From: Jack Koenig Date: Thu, 4 Jul 2024 15:15:03 -0700 Subject: [PATCH] Move circuitVar from HasId to BaseModule --- core/src/main/scala/chisel3/Module.scala | 7 +++++++ .../main/scala/chisel3/internal/Builder.scala | 18 ++++++++---------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/core/src/main/scala/chisel3/Module.scala b/core/src/main/scala/chisel3/Module.scala index 4165bd5f4c8..aabbeb88d0d 100644 --- a/core/src/main/scala/chisel3/Module.scala +++ b/core/src/main/scala/chisel3/Module.scala @@ -426,6 +426,13 @@ package experimental { abstract class BaseModule extends HasId with IsInstantiable { _parent.foreach(_.addId(this)) + // Set if the returned top-level module of a nested call to the Chisel Builder, see Definition.apply + private var _circuitVar: BaseModule = null // using nullable var for better memory usage + private[chisel3] def _circuit: Option[BaseModule] = Option(_circuitVar) + private[chisel3] def _circuit_=(target: Option[BaseModule]): Unit = { + _circuitVar = target.getOrElse(null) + } + // Protected so it can be overridden by the compiler plugin protected def _sourceInfo: SourceInfo = UnlocatableSourceInfo diff --git a/core/src/main/scala/chisel3/internal/Builder.scala b/core/src/main/scala/chisel3/internal/Builder.scala index 5f38b2ee632..53bcb974f91 100644 --- a/core/src/main/scala/chisel3/internal/Builder.scala +++ b/core/src/main/scala/chisel3/internal/Builder.scala @@ -122,13 +122,6 @@ private[chisel3] trait HasId extends chisel3.InstanceId { _parentVar = target.getOrElse(null) } - // Set if the returned top-level module of a nested call to the Chisel Builder, see Definition.apply - private var _circuitVar: BaseModule = null // using nullable var for better memory usage - private[chisel3] def _circuit: Option[BaseModule] = Option(_circuitVar) - private[chisel3] def _circuit_=(target: Option[BaseModule]): Unit = { - _circuitVar = target.getOrElse(null) - } - private[chisel3] val _id: Long = Builder.idGen.next // TODO: remove this, but its removal seems to cause a nasty Scala compiler crash. @@ -351,9 +344,14 @@ private[chisel3] trait HasId extends chisel3.InstanceId { } def circuitName: String = _parent match { case None => - _circuit match { - case None => instanceName - case Some(o) => o.circuitName + // Only modules have circuits + this match { + case b: BaseModule => + b._circuit match { + case Some(c) => c.circuitName + case None => instanceName + } + case _ => instanceName } case Some(ViewParent) => reifyParent.circuitName case Some(p) => p.circuitName