Skip to content

Add DynamicGroup for runtime-defined option groups#5229

Merged
uenoku merged 5 commits intochipsalliance:mainfrom
uenoku:dev/hidetou/dynamic-choice-2
Apr 7, 2026
Merged

Add DynamicGroup for runtime-defined option groups#5229
uenoku merged 5 commits intochipsalliance:mainfrom
uenoku:dev/hidetou/dynamic-choice-2

Conversation

@uenoku
Copy link
Copy Markdown
Contributor

@uenoku uenoku commented Mar 11, 2026

Add DynamicGroup abstract-class API for creating reusable option groups at runtime.

Contributor Checklist

  • Did you add Scaladoc to every public function/method?
  • Did you add at least one test demonstrating the PR?
  • Did you delete any extraneous printlns/debugging code?
  • Did you specify the type of improvement?
  • Did you add appropriate documentation in docs/src?
  • Did you request a desired merge strategy?
  • Did you add text to be included in the Release Notes for this change?

Type of Improvement

  • Feature (or new API)

Desired Merge Strategy

  • Squash: The PR will be squashed and merged (choose this if you have no preference).

Release Notes

Introduces a new DynamicGroup class that allows option groups
to be defined with runtime parameters instead

Reviewer Checklist (only modified by reviewer)

  • Did you add the appropriate labels? (Select the most appropriate one based on the "Type of Improvement")
  • Did you mark the proper milestone (Bug fix: 3.6.x, 5.x, or 6.x depending on impact, API modification or big change: 7.0)?
  • Did you review?
  • Did you check whether all relevant Contributor checkboxes have been checked?
  • Did you do one of the following when ready to merge:
    • Squash: You/ the contributor Enable auto-merge (squash) and clean up the commit message.
    • Merge: Ensure that contributor has cleaned up their commit history, then merge with Create a merge commit.

@uenoku uenoku added the Feature New feature, will be included in release notes label Mar 11, 2026
@uenoku uenoku changed the title Add DynamicGroup for ModuleChoice Add DynamicGroup for runtime-defined option groups Mar 11, 2026
Copy link
Copy Markdown
Member

@seldridge seldridge left a comment

Choose a reason for hiding this comment

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

High-level: is there a way to do this which doesn't make everything string-based?

Comment thread core/src/main/scala/chisel3/choice/package.scala Outdated
Comment thread core/src/main/scala/chisel3/choice/package.scala Outdated
@uenoku
Copy link
Copy Markdown
Contributor Author

uenoku commented Mar 11, 2026

High-level: is there a way to do this which doesn't make everything string-based?

I can provide following level of trait based interface, though I didn't like it because of the amount of boilerplate.

trait PlatformType {
  def FPGA: Case
  def ASIC: Case
}
val platform = DynamicGroup[PlatformType]("Platform", Seq("FPGA", "ASIC")) { group =>
  new PlatformType {
    def FPGA = group("FPGA")
    def ASIC = group("ASIC")
  }
}
platform.FPGA

Ideally I wanted to do following but I thought scala3 reflection would be necessary to do this.

trait PlatformType {
  def FPGA: Case
  def ASIC: Case
}
val platform = DynamicGroup[PlatformType]("Platform")
platform.FPGA

@uenoku uenoku marked this pull request as ready for review March 13, 2026 08:14
@jackkoenig
Copy link
Copy Markdown
Contributor

We could make this work:

trait PlatformType extends DynamicGroup {
  object FPGA extends Case
  object ASIC extends Case
}
val platform = DynamicGroup[PlatformType]("Platform")
// or
val Platform = DynamicGroup(new PlatformType {})

Or something like that without Scala reflection

@uenoku uenoku marked this pull request as draft March 13, 2026 22:30
@uenoku
Copy link
Copy Markdown
Contributor Author

uenoku commented Mar 13, 2026

62ae69d implements trait based API that uses macro. I disclose that I needed quite a bit help from AI for writing DynamicGroupIntf.scala and it might be sloppy, so it might be better to start with a previous commit that's much more simpler. Though I believe that macro correctly implements API we generally want.

@uenoku uenoku marked this pull request as ready for review March 13, 2026 23:57
Add DynamicGroup trait-based API for creating reusable option groups at runtime.

- Add dynamicGroups HashMap to cache DynamicGroup instances
- Add getOrCreateDynamicGroup/getOrCreateDynamicGroupInstance for instance management
- Validate Groups with same name have same cases and order
- Group options by name in buildImpl to handle multiple Group objects
@uenoku uenoku force-pushed the dev/hidetou/dynamic-choice-2 branch from 62ae69d to 8f29623 Compare April 3, 2026 01:27
@uenoku uenoku requested review from jackkoenig and seldridge April 3, 2026 01:27
@uenoku
Copy link
Copy Markdown
Contributor Author

uenoku commented Apr 3, 2026

Simplified the builder implementation a bit more. Currently dynamic options are stored in builder separately for the early validation, but it might be redundant since they are checked in buildImpl as well.

@uenoku uenoku force-pushed the dev/hidetou/dynamic-choice-2 branch from 6341873 to f4064c1 Compare April 3, 2026 01:46
Replace the macro-based DynamicGroup implementation with a simpler constructor-based approach.
DynamicGroup is now an abstract class that takes a customName parameter instead of a trait materialized
via Scala 2 macros. This removes the DynamicGroupFactoryIntf, DynamicGroupMacros, and DynamicGroup.Factory
typeclass machinery.

Introduce DynamicCase for case declarations within DynamicGroup instances. Remove the dynamicGroups
HashMap from DynamicContext and related builder validation methods, unifying group handling for both
static and dynamic groups.
@uenoku uenoku force-pushed the dev/hidetou/dynamic-choice-2 branch from 275731d to 65508ca Compare April 6, 2026 21:16
Copy link
Copy Markdown
Contributor

@jackkoenig jackkoenig left a comment

Choose a reason for hiding this comment

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

1 nit but otherwise LGTM!

Comment thread core/src/main/scala/chisel3/choice/package.scala Outdated
@uenoku uenoku merged commit 1af6331 into chipsalliance:main Apr 7, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature New feature, will be included in release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants