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

Implement auto typeName generation for Records #3504

Merged
merged 20 commits into from
Sep 14, 2023
Merged

Conversation

jared-barocsi
Copy link
Contributor

Adds the HasAutoTypename trait, which mixes into Record and generates a def typeName statement constructed from the Record's constructor parameters.

Example:

class MyBundle(foo: Int, bar: Int) extends Bundle with HasAutoTypename {
  val x = UInt(foo.W)
  val x = UInt(foo.W)
  // This line is automatically generated by the compiler plugin through HasAutoTypename
  // override def typeName: String = s"${getClass.getSimpleName}_$foo_$bar"
}

val bundle = Wire(new MyBundle(8, 4))
bundle.typeName // MyBundle_8_4

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

  • New API

Desired Merge Strategy

Squash and merge

Release Notes

Add experimental HasAutoTypename trait

  • Allows users to automatically generate a typeName for Bundle objects through the compiler plugin.

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.5.x, 3.6.x, or 5.x depending on impact, API modification or big change: 6.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), clean up the commit message, and label with Please Merge.
    • Merge: Ensure that contributor has cleaned up their commit history, then merge with Create a merge commit.

@jared-barocsi jared-barocsi added the Feature New feature, will be included in release notes label Aug 24, 2023
Copy link
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.

Generally looks good but please remove the code duplication in the plugin. Per anonymous Bundle issues, we can deal with that in a follow on PR.


/** Auto generate a type name for this Bundle using the bundle arguments supplied by the compiler plugin.
*/
override def typeName: String = autoTypeName(getClass.getSimpleName, _typeNameConParams)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably think about what the behavior should be for anonymous classes, eg. what if someone writes:

class MyModule(n: Int) extends Module {
  val io = IO(new Bundle with HasAutoTypename {
    val foo = Input(UInt(n.W))
  })
}

I think you will get a name that is something like _packageMyModulee0e14f5_8 which is a pretty terrible name indeed.

I think there's a compelling argument that you should not be able to mix HasAutoTypename into anonymous classes since it's going to be a disaster, and arguably not any inner class (inner classes have their outer object as the first argument, that's where packageMyModuleee0e145f comes from, that's the .toString of the instance of MyModule above).

Also .getSimpleName throws an exception in some circumstances on Java8 which is still supported by Chisel, see scala/bug#2034 and this is why we have more complicated logic for naming Modules:

def desiredName: String = {

I'm not going to sandbag this PR of this issue, but more careful handling either needs to be in this PR or come shortly after.

Copy link
Contributor

Choose a reason for hiding this comment

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

We chatted offline but I think the best path forward is to just find a way to ban inner classes from using HasAutoTypename in the compiler plugin, then we don't need to worry about the .getSimpleName issue either because that only affects inner classes.

docs/src/cookbooks/naming.md Outdated Show resolved Hide resolved
Comment on lines +9 to +29
class Top(gen: Bundle) extends Module {
val in = IO(Input(gen))
val out = IO(Output(gen))
override def desiredName = s"Top_${out.typeName}"
}

class SimpleBundle(width: Int) extends Bundle with HasAutoTypename {
val foo = UInt(width.W)
}

class StringlyBundle(width: Int, desc: String) extends Bundle with HasAutoTypename {
val foo = UInt(width.W)
}

class DataBundle(data: Data) extends Bundle with HasAutoTypename {
val foo = data
}

class BundleBundle(gen: HasAutoTypename) extends Bundle with HasAutoTypename {
val foo = gen
}
Copy link
Contributor

@jackkoenig jackkoenig Sep 14, 2023

Choose a reason for hiding this comment

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

Please add a test for a Bundle with multiple parameter lists, including implicit eg.

class MultipleParameterLists(x: Int)(y: List[Int])(implicit z: String) extends Bundle with HasAutoTypename { ... }

Copy link
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.

Approved but please add the requested test and address Megan's comments about the ScalaDoc

@jared-barocsi jared-barocsi merged commit f512e8e into main Sep 14, 2023
14 checks passed
@jared-barocsi jared-barocsi deleted the autotypename branch September 14, 2023 22:39
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.

4 participants