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 Instantiate for Product parameters #3210

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,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 @@ -296,6 +322,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