Skip to content

Commit

Permalink
Share Node objects between siblings (#4259)
Browse files Browse the repository at this point in the history
(cherry picked from commit a60f981)
  • Loading branch information
jackkoenig authored and mergify[bot] committed Jul 11, 2024
1 parent db47e0d commit d7b1238
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
8 changes: 5 additions & 3 deletions core/src/main/scala/chisel3/Aggregate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,9 @@ sealed class Vec[T <: Data] private[chisel3] (gen: => T, val length: Int) extend
// simpler.
private lazy val self: Seq[T] = {
val _self = Vector.fill(length)(gen)
val thisNode = Node(this) // Share the same Node for all elements.
for ((elt, i) <- _self.zipWithIndex)
elt.setRef(this, i)
elt.setRef(thisNode, i)
_self
}

Expand Down Expand Up @@ -382,7 +383,7 @@ sealed class Vec[T <: Data] private[chisel3] (gen: => T, val length: Int) extend
port.bind(ChildBinding(this), reconstructedResolvedDirection)

val i = Vec.truncateIndex(p, length)(UnlocatableSourceInfo)
port.setRef(this, i)
port.setRef(Node(this), i)

port
}
Expand Down Expand Up @@ -992,9 +993,10 @@ abstract class Record extends Aggregate {
!opaqueType || (_elements.size == 1 && _elements.head._1 == ""),
s"Opaque types must have exactly one element with an empty name, not ${_elements.size}: ${elements.keys.mkString(", ")}"
)
val thisNode = Node(this) // Share the same Node for all elements.
// Names of _elements have already been namespaced (and therefore sanitized)
for ((name, elt) <- _elements) {
elt.setRef(this, name, opaque = opaqueType)
elt.setRef(thisNode, name, opaque = opaqueType)
}
}

Expand Down
10 changes: 5 additions & 5 deletions core/src/main/scala/chisel3/internal/Builder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,13 @@ private[chisel3] trait HasId extends chisel3.InstanceId {
_refVar = imm
}
}
private[chisel3] def setRef(parent: HasId, name: String, opaque: Boolean = false): Unit = {
if (!opaque) setRef(Slot(Node(parent), name))
else setRef(OpaqueSlot(Node(parent)))
private[chisel3] def setRef(parent: Node, name: String, opaque: Boolean = false): Unit = {
if (!opaque) setRef(Slot(parent, name))
else setRef(OpaqueSlot(parent))
}

private[chisel3] def setRef(parent: HasId, index: Int): Unit = setRef(Index(Node(parent), ILit(index)))
private[chisel3] def setRef(parent: HasId, index: UInt): Unit = setRef(Index(Node(parent), index.ref))
private[chisel3] def setRef(parent: Node, index: Int): Unit = setRef(Index(parent, ILit(index)))
private[chisel3] def setRef(parent: Node, index: UInt): Unit = setRef(Index(parent, index.ref))
private[chisel3] def getRef: Arg = _ref.get
private[chisel3] def getOptionRef: Option[Arg] = _ref

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/chisel3/properties/Object.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scala.language.experimental.macros

import chisel3.{Module, RawModule, SpecifiedDirection}
import chisel3.experimental.{BaseModule, SourceInfo}
import chisel3.internal.firrtl.ir.{DefClass, DefObject}
import chisel3.internal.firrtl.ir.{DefClass, DefObject, Node}
import chisel3.internal.sourceinfo.InstTransform
import chisel3.internal.{throwException, Builder, HasId, NamedComponent, ObjectFieldBinding}

Expand Down Expand Up @@ -65,14 +65,14 @@ class DynamicObject private[chisel3] (val className: ClassType) extends HasId wi
*/
def getField[T](name: String)(implicit tpe: PropertyType[T]): Property[tpe.Type] = {
val field = Property[T]()
field.setRef(this, name)
field.setRef(Node(this), name)
field.bind(ObjectFieldBinding(_parent.get), SpecifiedDirection.Unspecified)
field
}

def getField[T](name: String, property: Property[T]): Property[T] = {
val field = property.cloneType
field.setRef(this, name)
field.setRef(Node(this), name)
field.bind(ObjectFieldBinding(_parent.get), SpecifiedDirection.Unspecified)
field
}
Expand Down

0 comments on commit d7b1238

Please sign in to comment.