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

[FIRRTL] Add type alias parser #5449

Merged
merged 6 commits into from
Jun 26, 2023
Merged

[FIRRTL] Add type alias parser #5449

merged 6 commits into from
Jun 26, 2023

Conversation

uenoku
Copy link
Member

@uenoku uenoku commented Jun 21, 2023

Separated from #5167. Add parser changes for type alias. https://github.com/chipsalliance/firrtl-spec/blob/main/spec.md#type-alias

Currently type alias for non-base types are stripped immediately while parsing with warnings.

On top of #5417

This commit adds minimal changes BaseTypeAliasType for base type alias.
 Implement storage and methods for FIRRTLBaseType and FieldIDTypeInterface.

This doesn't include recursive property chagnes and
`FIRRTLBaseType::getAnonnymousType` whcih requires more broader changes.
This implements `type_isa/type_cast/type_dyn_cast/type_dyn_cast_or_null`.
When a given type is an alias, these casts will visit its inner type recursively.
These type casts can be used anywhere and if the requested type is
non-base type or FIRRTLBaseType itself, then it will optimized in the
compile time,
@@ -4317,6 +4375,12 @@ ParseResult FIRCircuitParser::parseCircuit(
emitError("unexpected token in circuit");
return failure();

case FIRToken::kw_type: {
Copy link
Member Author

Choose a reason for hiding this comment

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

On the second thought, I didn't find much useful to reject type declarations based on FIRRTL version since type declaration is a completely new thing (in a similar way to property or enum), which doesn't involve any backward compatibility. So I remove the version check here.

@uenoku uenoku requested a review from prithayan June 21, 2023 16:21
Base automatically changed from dev/uenoku/implement-type-cast to main June 22, 2023 17:29
test/Dialect/FIRRTL/parse-errors.fir Show resolved Hide resolved
lib/Dialect/FIRRTL/Import/FIRParser.cpp Outdated Show resolved Hide resolved
uenoku and others added 2 commits June 23, 2023 17:06
Co-authored-by: Prithayan Barua <prithayan@gmail.com>
@uenoku uenoku merged commit 9ab2978 into main Jun 26, 2023
5 checks passed
@uenoku uenoku deleted the dev/uenoku/parse-alias branch June 26, 2023 13:59
@dtzSiFive
Copy link
Contributor

Is there a requirement re:declaring type declarations "before" (in source code position) their use?

Quick testing suggests modules can use type aliases defined after, but type aliases themselves cannot do this.

On one hand, requiring definitions before their use seems reasonable but it does seem a little inconsistent with using other top-level definitions where order doesn't matter. Is this the desired/expected/preferred behavior?

If nothing else this should be an intentional decision and aligned with spec (didn't check).

@uenoku
Copy link
Member Author

uenoku commented Jun 26, 2023

Good point, will clarify in the spec. I don't have a strong opinion but I guess we want consistent behavior to other top-level definitions. However allowing uses of type alias before declarations also implies that we can define recursive types. Not sure what recurisive types in FIRRTL, but. at least currently the type storage cannot handle cycles in type references. So we will need to detect and reject cycles in types. Besides we will need to modify parseModule so that we can lazily resolve the type alias in ports.

@dtzSiFive
Copy link
Contributor

Good points re:recursion + needing to figure out how to resolve port types.

Not sure what right answer is, other alternative is require all type aliases def-before-use (and maybe before first module)? Not sure how generator-friendly that would be.

@uenoku
Copy link
Member Author

uenoku commented Jun 27, 2023

other alternative is require all type aliases def-before-use (and maybe before first module)?

Yep, that sounds reasonable restriction. I agree that it's not bad to limit type declarations to be before first module. Verifying def-before-use is also tricky since we will have to associate type declarations with their parsed positions in fir files. I'm leaning toward just restricting type declarations to be before the first module (as it's simplest).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants