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

make SerializableModuleGenerator work with D/I #4003

Merged
merged 2 commits into from
Apr 18, 2024
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
@@ -1,5 +1,7 @@
package chisel3.experimental

import chisel3.experimental.hierarchy.{Definition, Instance}
import chisel3.internal.{instantiable, Builder, BuilderContextCache}
import upickle.default._

import scala.reflect.runtime.universe
Expand All @@ -17,6 +19,7 @@ trait SerializableModuleParameter
* }}}
* 3. user should guarantee the module is reproducible on their own.
*/
@instantiable
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the ABI of SerializableModule, but it should be safe enough, since it only provide the new feature.

trait SerializableModule[T <: SerializableModuleParameter] { this: BaseModule =>
val parameter: T
}
Expand All @@ -42,6 +45,13 @@ object SerializableModuleGenerator {
)
}
)

/** cache instance of a generator. */
private case class CacheKey[P <: SerializableModuleParameter, M <: SerializableModule[P]](
parameter: P,
mTypeTag: universe.TypeTag[M])
extends BuilderContextCache.Key[Definition[M with BaseModule]]

}

/** the serializable module generator:
Expand All @@ -54,7 +64,7 @@ case class SerializableModuleGenerator[M <: SerializableModule[P], P <: Serializ
)(
implicit val pTag: universe.TypeTag[P],
implicit val mTag: universe.TypeTag[M]) {
private[chisel3] def construct: M = {
private[chisel3] def construct: M with BaseModule = {
require(
generator.getConstructors.length == 1,
s"""only allow constructing SerializableModule from SerializableModuleParameter via class Module(val parameter: Parameter),
Expand All @@ -75,9 +85,22 @@ case class SerializableModuleGenerator[M <: SerializableModule[P], P <: Serializ
|${generator.getConstructors.head.getParameterTypes.mkString("\n")}
|""".stripMargin
)
generator.getConstructors.head.newInstance(parameter).asInstanceOf[M]
generator.getConstructors.head.newInstance(parameter).asInstanceOf[M with BaseModule]
}

/** elaborate a module from this generator. */
def module(): M = construct
def module(): M with BaseModule = construct

/** get the definition from this generator. */
def definition(): Definition[M with BaseModule] = Builder.contextCache
.getOrElseUpdate(
SerializableModuleGenerator.CacheKey(
parameter,
implicitly[universe.TypeTag[M]]
),
Definition.do_apply(construct)(UnlocatableSourceInfo)
)

/** get an instance of from this generator. */
def instance(): Instance[M with BaseModule] = Instance.do_apply(definition())(UnlocatableSourceInfo)
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ class SerializableModuleGeneratorSpec extends ChiselFlatSpec with Utils {
circt.stage.ChiselStage.emitCHIRRTL(g.module())
}

"SerializableModuleGenerator" should "be able to elaborate with D/I" in {
val cir = circt.stage.ChiselStage.emitCHIRRTL(
new Module {
val i32 =
SerializableModuleGenerator(classOf[GCDSerializableModule], GCDSerializableModuleParameter(32)).instance()
val i16 =
SerializableModuleGenerator(classOf[GCDSerializableModule], GCDSerializableModuleParameter(16)).instance()
val ii32 =
SerializableModuleGenerator(classOf[GCDSerializableModule], GCDSerializableModuleParameter(32)).instance()
}
)
cir should include("inst i32 of GCDSerializableModule")
cir should include("inst i16 of GCDSerializableModule_1")
cir should include("inst ii32 of GCDSerializableModule")
}

case class FooParameter() extends SerializableModuleParameter

class InnerFoo(val parameter: FooParameter) extends RawModule with SerializableModule[FooParameter]
Expand Down
Loading