diff --git a/core/src/main/scala/chisel3/Module.scala b/core/src/main/scala/chisel3/Module.scala index ed3235046bc..bb2854857f6 100644 --- a/core/src/main/scala/chisel3/Module.scala +++ b/core/src/main/scala/chisel3/Module.scala @@ -263,13 +263,19 @@ package internal { record.forceName(None, default = this.desiredName, namespace) // Now take the Ref that forceName set and convert it to the correct Arg val instName = record.getRef match { - case Ref(name) => name - case bad => throwException(s"Internal Error! Cloned-module Record $record has unexpected ref $bad") + case Ref(name) => name + case ModuleCloneIO(_, name) => namespace.name(name) + case bad => throwException(s"Internal Error! Cloned-module Record $record has unexpected ref $bad") } // Set both the record and the module to have the same instance name record.setRef(ModuleCloneIO(getProto, instName), force = true) // force because we did .forceName first this.setRef(Ref(instName)) } + + def suggestName(name: String): Unit = { + _portsRecord.setRef(ModuleCloneIO(getProto, name)) // force because we did .forceName first + this.setRef(Ref(name)) + } } /** Represents a module viewed from a different instance context. diff --git a/core/src/main/scala/chisel3/experimental/hierarchy/Instance.scala b/core/src/main/scala/chisel3/experimental/hierarchy/Instance.scala index cc926771499..7e87c91051b 100644 --- a/core/src/main/scala/chisel3/experimental/hierarchy/Instance.scala +++ b/core/src/main/scala/chisel3/experimental/hierarchy/Instance.scala @@ -86,6 +86,9 @@ object Instance extends SourceInfoDoc { case Clone(x: IsClone[_] with BaseModule) => x.toAbsoluteTarget } + def suggestName(name: String): Unit = { + i.getInnerDataContext.get.asInstanceOf[ModuleClone[T]].suggestName(name) + } } /** A constructs an [[Instance]] from a [[Definition]] diff --git a/src/test/scala/chiselTests/experimental/hierarchy/InstanceSpec.scala b/src/test/scala/chiselTests/experimental/hierarchy/InstanceSpec.scala index 407a7237311..3ff13d54f4e 100644 --- a/src/test/scala/chiselTests/experimental/hierarchy/InstanceSpec.scala +++ b/src/test/scala/chiselTests/experimental/hierarchy/InstanceSpec.scala @@ -1066,4 +1066,41 @@ class InstanceSpec extends ChiselFunSpec with Utils { getFirrtlAndAnnos(new HasMultipleTypeParamsInside, Seq(aspect)) } } + it("11.1 suggestName for Instances") { + class Top extends Module { + val definition = Definition(new AddOne) + val inst0 = Instance(definition) + inst0.suggestName("potato") + } + val (chirrtl, _) = getFirrtlAndAnnos(new Top) + chirrtl.serialize should include("inst potato of AddOne") + } + it("11.2 suggestName at instantiation") { + class Top extends Module { + val k = Instance(Definition(new AddOne)).suggestName("potato") + } + val (chirrtl, _) = getFirrtlAndAnnos(new Top) + chirrtl.serialize should include("inst potato of AddOne") + } + it("11.3 suggestName with sanitization") { + class Top extends Module { + val definition = Definition(new AddOne) + val inst0 = Instance(definition) + val inst1 = Instance(definition) + inst0.suggestName("potato") + inst1.suggestName("potato") + } + val (chirrtl, _) = getFirrtlAndAnnos(new Top) + chirrtl.serialize should include("inst potato of AddOne") + chirrtl.serialize should include("inst potato_1 of AddOne") + } + it("11.4 suggestName with multi-def collision sanitization") { + class Top extends Module { + val inst0 = Module(new AddOne()).suggestName("potato") + val inst1 = Instance(Definition(new AddOne)).suggestName("potato") + } + val (chirrtl, _) = getFirrtlAndAnnos(new Top) + chirrtl.serialize should include("inst potato of AddOne") + chirrtl.serialize should include("inst potato_1 of AddOne_1") + } }