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

Fix bundle elements performance regression #2396

Merged
merged 2 commits into from
Feb 4, 2022
Merged
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
25 changes: 19 additions & 6 deletions core/src/main/scala/chisel3/Aggregate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1207,8 +1207,19 @@ abstract class Bundle(implicit compileOptions: CompileOptions) extends Record {
* assert(uint === "h12345678".U) // This will pass
* }}}
*/
final lazy val elements: SeqMap[String, Data] = {
val hardwareFields = _elementsImpl.flatMap {
final lazy val elements: SeqMap[String, Data] =
// _elementsImpl is a method, only call it once
_elementsImpl match {
// Those not using plugin-generated _elementsImpl use the old reflective implementation
case oldElements: VectorMap[_, _] => oldElements.asInstanceOf[VectorMap[String, Data]]
// Plugin-generated _elementsImpl are incomplete and need some processing
case rawElements => _processRawElements(rawElements)
}

// The compiler plugin is imperfect at picking out elements statically so we process at runtime
// checking for errors and filtering out mistakes
private def _processRawElements(rawElements: Iterable[(String, Any)]): SeqMap[String, Data] = {
val hardwareFields = rawElements.flatMap {
case (name, data: Data) =>
if (data.isSynthesizable) {
Some(s"$name: $data")
Expand Down Expand Up @@ -1241,7 +1252,7 @@ abstract class Bundle(implicit compileOptions: CompileOptions) extends Record {
if (hardwareFields.nonEmpty) {
throw ExpectedChiselTypeException(s"Bundle: $this contains hardware fields: " + hardwareFields.mkString(","))
}
VectorMap(_elementsImpl.toSeq.flatMap {
VectorMap(rawElements.toSeq.flatMap {
case (name, data: Data) =>
Some(name -> data)
case (name, Some(data: Data)) =>
Expand All @@ -1251,15 +1262,17 @@ abstract class Bundle(implicit compileOptions: CompileOptions) extends Record {
case ((an, a), (bn, b)) => (a._id > b._id) || ((a eq b) && (an > bn))
}: _*)
}
/*
* This method will be overwritten by the Chisel-Plugin

/* The old, reflective implementation of Bundle.elements
* This method is optionally overwritten by the compiler plugin for much better performance
*/
protected def _elementsImpl: Iterable[(String, Any)] = {
val nameMap = LinkedHashMap[String, Data]()
for (m <- getPublicFields(classOf[Bundle])) {
getBundleField(m) match {
case Some(d: Data) =>
// Checking for chiselType is done in elements method
requireIsChiselType(d)

if (nameMap contains m.getName) {
require(nameMap(m.getName) eq d)
} else {
Expand Down