-
Notifications
You must be signed in to change notification settings - Fork 610
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
[choice] Adds a means to declare a choice group without use #4554
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API of addGroup
looks great.
One suggestion on how to avoid the reflection and a nit in the test to use FileCheck
.
out := in | ||
} | ||
|
||
val chirrtl = ChiselStage.emitCHIRRTL(new ModuleWithoutChoice, Array("--full-stacktrace")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Array("--full-stacktrace")
can be dropped.
val tpe = typeOf[T] | ||
val classSymbol = tpe.typeSymbol.asClass | ||
val classMirror = cm.reflectClass(classSymbol) | ||
|
||
tpe.members.collect { | ||
// Look only for inner objects. Note, this is not recursive. | ||
case m: ModuleSymbol if m.isStatic => | ||
val instance = cm.reflectModule(m.asModule).instance | ||
// Confirms the instance is a subtype of Case | ||
if (cm.classSymbol(instance.getClass).toType <:< typeOf[Case]) { | ||
Builder.options += instance.asInstanceOf[Case] | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is clever reflection. 👍 However, it may be better to just make this information more easily accessible. Two changes would avoid the TypeTag
(which is a Scala3 migration annoyance, I think) and any work in this function:
- Add a
Builder
member for storinggroups
and not just options. - Add
mutable.LinkedHashSet
toGroup
that can be used to directly look up whatCase
s it supports. Then modifyCase
to insert into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started doing a (2) but wasn't sure when/how to finalize the set short of doing something either janky or switching the API in someway to use a different pattern.
But (1) seems really easy to do; the existing struct can probably just become a HashMap and we wouldn't need to reconstruct the the groups later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok i got rid of the type tag but i can't figure out how to avoid it altogether unless we change the API for defining these cases (so that they are not lazy initialized).
Alternatively, we could change the addGroup
API to to instead take a one or more cases. For example addGroupHint(c: Case)
would suffice to workaround my problem, but it seems rather hacky to not register the whole group and breaks the direct addLayer
analogy.
However, I doubt breaking the Group API here is a big deal since no one should be using this? I'd change the
Group` implementation to use a more factory like pattern that would force registration on the definition of the objects.
LMK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think the laziness of the cases seems wrong. Laziness of groups seems fine. (You only get a group if you use it, but you get all of its cases.)
This would be a change to have the Builder
register groups, have cases register that they are children of groups, and then pushing that change through to the converter.
However, all of this is independent of any change to the API. Given that, I will go ahead and approve this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this reflection works in Scala 3 and as we are actively trying to get Scala 3 compilation into CI, I think we need to address it sooner rather than later. Can we do something similar to how Layers
get the parents with an implicit rather than reflecting on the Group
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, can i break the existing API? If so this is pretty straight forward. Otherwise i might need one of you guys (i.e., someone with more Scala foo than i) to do it. The crux of it is that the Cases are lazily constructed so something has to reference them to bind them to the group. If you're coming into the office today maybe we can chat about it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can break it as much as you want. Just don't backport it to 6.x. This feature has no users and you won't be disturbing anyone.
Co-authored-by: Schuyler Eldridge <schuyler.eldridge@sifive.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
The internals of groups need to be reworked. That can be done in a follow-on (or backlogged for someone else to do).
Also there seems to be some consistency referring to groups, cases and options in the code. Do you want me to go through and update the code and docs to match either the structures in here, or the ABI docs? |
@davidbiancolin and I discussed this offline Unfortunately just having the We discussed an alternative proposal though to switch to the style of object Platform extends Group {
object FPGA extends Case
object ASIC extends Case
} You would write: object Platform extends Group {
val FPGA = Case
val ASIC = Case
val Special = Case("SpecialPlatform") // can manually pass the String too
} This requires a simple macro to grab the val name a la
What do you think @seldridge? Footnotes |
Everything in this API can be broken as there are no users. I wouldn't worry about that. I would prefer to not introduce any new macros. Why would something simple like having the I can see a potential reason to not require the nesting and to keep the implicit, though I'm not sure if it would come up. If we have builtin groups and cases defined, then we may want the ability to add new cases to an existing group. I do expect that we will eventually have built-in groups that memories use. It is possible that we want users to be able to add cases to these groups. (Alternatively, this could be just done with a generic blackbox.) I do agree that these are trying to describe an enum. |
The crux of it is The prior could look as follows:
But this seems like a terrible design. Alternatively, one could make case a factory:
Then you have the doubly define names that lead one towards using a macro as done in other libraries to auto-name the cases...
|
import scala.reflect.runtime.universe._ | ||
import scala.reflect.runtime.{currentMirror => cm} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
scala.reflect._
doesn't cross-compile with Scala 3, it needs to be put in core/src/main/scala-2/chisel3/
Per @seldridge's suggestion here I add analog of
addLayer
forModuleChoice
calledaddGroup
. This permits defining a group in a circuit without necessarily using that group in a circuit, allowing for commonly reused top-level modules to always have these choices defined regardless of what is instantiated in their module hierarchies.Short of reworking the group API a bit to more explicitly register the
Case
s in theGroup
at construction time i couldn't figure out a means to do what i wanted, so i resorted to using Scala reflection. If there's a smarter way to do this i'm all ears.Contributor Checklist
docs/src
?Type of Improvement
Desired Merge Strategy
Release Notes
Introduce
choice.addGroup
which enables declaration of achoice.Group
without its use (akin tolayer.addLayer
)Reviewer Checklist (only modified by reviewer)
3.6.x
,5.x
, or6.x
depending on impact, API modification or big change:7.0
)?Enable auto-merge (squash)
, clean up the commit message, and label withPlease Merge
.Create a merge commit
.