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

Enum deficiencies #373

Closed
ducky64 opened this issue Nov 17, 2016 · 11 comments
Closed

Enum deficiencies #373

ducky64 opened this issue Nov 17, 2016 · 11 comments
Labels
feature request Feature New feature, will be included in release notes
Milestone

Comments

@ducky64
Copy link
Contributor

ducky64 commented Nov 17, 2016

  • Enum values can't be used in testing, since they only return a Chisel-land data type rather than a Scala-land Int.
  • nodeType may be meaningless / boilerplate - I don't see a case for anything other than UInt. That nodeType exists also forces Chisel to keep the Bits.fromInt method which isn't exposed or used elsewhere.
  • Enum values aren't type-safe, they return generic Chisel types like UInt which aren't treated differently. Whether this is desirable is an open question, much like the Scala debate between Enumeration and case objects

Thoughts?

@aswaterman
Copy link
Member

Re: point 2: I'd favor creating Enum(n) which implicitly does the same thing as Enum(UInt(log2Ceil(n)), n) currently does. You could then deprecate Enum(nodeType, n) in new-chisel3. I agree, it's not really obvious what an SInt enum would do, since there's no facility to start at anything besides 0.

(In C, having signed enums is occasionally useful, because you're allowed to set the start number to something negative. I don't see a compelling reason to follow their lead in this case, though.)

@aswaterman
Copy link
Member

Re: the other points: despite its downsides, it's still useful.

@ducky64
Copy link
Contributor Author

ducky64 commented Nov 18, 2016

Yes, we definitely do need to keep the current Enum (perhaps with nodeType deprecated, and assertion-checked to be a UInt so we can remove Bits.fromInt), but it would be interesting to think of what a more featured enum system would look like.

@ducky64
Copy link
Contributor Author

ducky64 commented Nov 23, 2016

So in the attempt to deprecate old style enums I came across some more issues:

  • The Symbol-based enum generators (def apply[T <: Bits](nodeType: T, l: Symbol *) and def apply[T <: Bits](nodeType: T, l: List[Symbol]) are really old code, and are used in neither chisel3 tests nor rocket-chip. Since they're not compile-time-checked and (in my opinion) strictly inferior to the list one, I'm thinking of removing those completely (without even a deprecation phase).
  • Technically, enums can take a nodeType of specified width, though this doesn't see use in rocket either, and it's unclear whether this would ever be a good idea (since the user has no control over the numeric values). I'm thinking of dropping this feature and having chisel3 assert out if a width is specified.

Thoughts?

ducky64 added a commit that referenced this issue Nov 24, 2016
Get rid of some cruft exposed in #373

This also allows Bits.fromtInt(...) to be removed. Yay!

All old APIs (with some new restrictions, rocket still works fine) are preserved without deprecation in Chisel._, aside from the non-compile-time-checkable Map[] enum constructor which probably should have been deprecated during chisel2. The Map[] enums have been removed from chisel3._ without deprecation.

The new restriction is that nodeType (legacy API) may only be of UInt type with unspecified width. Note that Bits() creates a UInt, and if you can't control the enum values, it makes little sense to specify a bitwidth.
@mwachs5
Copy link
Contributor

mwachs5 commented Nov 25, 2016

I have to say that as a new Chisel user, the first point,
"Enum values can't be used in testing, since they only return a Chisel-land data type rather than a Scala-land Int." immediately bit me, and made me wonder what the point of Enums is.

Actually I'm still not sure what the point is vs using a non-Chisel Scala Enum (however you like doing that) since the Enum-ness is not reflected in the actual emitted Verilog. If you're going to go through the trouble of creating a special Chisel Enum, why not emit the Verilog equivalent so that the Verilog would have meaningful names/values in them (something which most Waveform viewers can pick up on). I wish that there was a way to not throw this Verilog feature away.

@ducky64
Copy link
Contributor Author

ducky64 commented Nov 28, 2016

So right now even Chisel doesn't know the names you've given to the enum variables, this is because of the way it's structured.

It seems Scala Enumerations are written completely in Scala (though using some fancy reflection method) and it should be possible to get names from those, so a more full-featured version of Chisel enums can be written like that.

There's also another style of doing enums in Scala which involves defining the type as a trait and the values as case objects extending that trait. Macros or reflection can then be used to get all the enumeration values. This style allows greater flexibility at the expense of more lines of code (it's typical to define one case object per line).

One of those two above patterns could be adapted to support Chisel-specific functionality like enum type bitwidth, UInt literal conversion, and maybe name propagation.

@colinschmidt
Copy link
Contributor

Adding the names to the output seems really cool/useful, but I think that FIRRTL would most likely not preserve this. (constant-prop and whatnot)

@schoeberl
Copy link
Contributor

schoeberl commented Dec 6, 2016 via email

@seldridge
Copy link
Member

One old issue with this, the current Chisel Enum does suffer from the 22-limit on tuples. The map-based Enum was the only way of getting around this that I was aware of. It would be good if there was a fallback method that didn't require the use of the Compatibility package.

@ducky64 ducky64 added this to the 3.1.0 milestone Apr 18, 2017
@ducky64 ducky64 added the Feature New feature, will be included in release notes label Nov 8, 2017
@ducky64 ducky64 modified the milestones: 3.1.0, 3.2.0 Dec 13, 2017
@jackkoenig
Copy link
Contributor

A minor issue with the current Enum(n) implementation is that you cannot use capitalized val names (common style for constants). Because Enum names things via unapply, Scala attempts to match capitalized variables rather than binding new ones.

@chick
Copy link
Contributor

chick commented Dec 17, 2018

Resolved by Strong Enums

@chick chick closed this as completed Dec 17, 2018
mwachs5 pushed a commit that referenced this issue Dec 29, 2022
Bumps [chisel3](https://github.com/freechipsproject/chisel3) from `41d0d4c` to `8fe8764`.
- [Release notes](https://github.com/freechipsproject/chisel3/releases)
- [Commits](41d0d4c...8fe8764)

---
updated-dependencies:
- dependency-name: chisel3
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Feature New feature, will be included in release notes
Projects
None yet
Development

No branches or pull requests

8 participants