Skip to content

Commit

Permalink
Fix Instantiate for Product parameters (#3210) (#3217)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8ca8b2e)

Co-authored-by: Jack Koenig <koenig@sifive.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
mergify[bot] and jackkoenig authored Apr 26, 2023
1 parent 6e5c2bb commit f793455
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ object Instantiate {

// Recursively box all Data (by traversing Products and Iterables) in DataBoxes
private def boxAllData(a: Any): Any = a match {
case d: Data => new DataBox(d) // Must check this before Iterable because Vec is Iterable
case d: Data => new DataBox(d) // Must check this before Iterable because Vec is Iterable
// Must check before Product, because many Iterables are Products, but can still be equal, eg. List(1) == Vector(1)
case it: Iterable[_] => it.map(boxAllData(_))
case p: Product => p.productIterator.map(boxAllData(_)).toSeq
case p: Product => Vector(p.getClass) ++ p.productIterator.map(boxAllData(_))
case other => other
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,32 @@ object InstantiateSpec {
@public val out = IO(Output(gen))
out := in
}

sealed trait MyEnumeration
case object FooEnum extends MyEnumeration
case object BarEnum extends MyEnumeration
case class FizzEnum(value: Int) extends MyEnumeration
case class BuzzEnum(value: Int) extends MyEnumeration

class ModuleParameterizedByProductTypes(param: MyEnumeration) extends Module {
override def desiredName = s"${this.getClass.getSimpleName}_$param"
val gen = param match {
case FooEnum => UInt(8.W)
case BarEnum => SInt(8.W)
case FizzEnum(n) => Vec(n, UInt(8.W))
case BuzzEnum(n) => Vec(n, SInt(8.W))
}
@public val in = IO(Input(gen))
@public val out = IO(Output(gen))
out := in
}

class ModuleParameterizedBySeq(param: Seq[Int]) extends Module {
override def desiredName = s"${this.getClass.getSimpleName}_" + param.mkString("_")
@public val in = param.map(w => IO(Input(UInt(w.W))))
@public val out = param.map(w => IO(Output(UInt(w.W))))
out.zip(in).foreach { case (o, i) => o := i }
}
}

class InstantiateSpec extends ChiselFunSpec with Utils {
Expand Down Expand Up @@ -309,6 +335,46 @@ class InstantiateSpec extends ChiselFunSpec with Utils {
val modules2 = convert(new MyTop).modules.map(_.name)
assert(modules2 == Seq("OneArg", "Top"))
}

it("should properly handle case objects as parameters") {
class MyTop extends Top {
val inst0 = Instantiate(new ModuleParameterizedByProductTypes(FooEnum))
val inst1 = Instantiate(new ModuleParameterizedByProductTypes(BarEnum))
}
val modules = convert(new MyTop).modules.map(_.name)
assert(
modules == Seq("ModuleParameterizedByProductTypes_FooEnum", "ModuleParameterizedByProductTypes_BarEnum", "Top")
)
}

it("should properly handle case classes as parameters") {
class MyTop extends Top {
val inst0 = Instantiate(new ModuleParameterizedByProductTypes(FizzEnum(3)))
val inst1 = Instantiate(new ModuleParameterizedByProductTypes(BuzzEnum(3)))
}
val modules = convert(new MyTop).modules.map(_.name)
assert(
modules == Seq(
"ModuleParameterizedByProductTypes_FizzEnum3",
"ModuleParameterizedByProductTypes_BuzzEnum3",
"Top"
)
)
}

it("should properly handle Iterables") {
class MyTop extends Top {
val inst0 = Instantiate(new ModuleParameterizedBySeq(List(1, 2, 3)))
val inst1 = Instantiate(new ModuleParameterizedBySeq(Vector(1, 2, 3)))
}
val modules = convert(new MyTop).modules.map(_.name)
assert(
modules == Seq(
"ModuleParameterizedBySeq_1_2_3",
"Top"
)
)
}
}

describe("Instantiate") {
Expand Down

0 comments on commit f793455

Please sign in to comment.