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

dialect: (riscv) Throw parser error if immediate is not given #1371

Merged
merged 6 commits into from Aug 1, 2023

Conversation

kingiler
Copy link
Collaborator

No description provided.

@kingiler kingiler marked this pull request as ready for review July 31, 2023 12:13
@codecov
Copy link

codecov bot commented Jul 31, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (d6676e2) 89.73% compared to head (c800fd9) 89.74%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1371   +/-   ##
=======================================
  Coverage   89.73%   89.74%           
=======================================
  Files         200      200           
  Lines       25700    25713   +13     
  Branches     3852     3854    +2     
=======================================
+ Hits        23063    23077   +14     
  Misses       2025     2025           
+ Partials      612      611    -1     
Files Changed Coverage Δ
tests/dialects/test_riscv.py 100.00% <100.00%> (ø)
xdsl/dialects/riscv.py 85.00% <100.00%> (+0.16%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kingiler kingiler added dialects Changes on the dialects bug Something isn't working minor For minor PRs, easy and quick to review, quickly mergeable labels Jul 31, 2023
@kingiler kingiler self-assigned this Jul 31, 2023
@@ -765,6 +765,8 @@ def custom_parse_attributes(cls, parser: Parser) -> Mapping[str, Attribute]:
)
elif immediate := parser.parse_optional_str_literal():
attributes["immediate"] = LabelAttr(immediate)
else:
parser.raise_error("Expected immediate")
Copy link
Member

Choose a reason for hiding this comment

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

Can you please make it non-optional?

Copy link
Collaborator Author

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 that is possible. Immediate is either integer or string literal.

Copy link
Member

Choose a reason for hiding this comment

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

I would recommend adding a parse_immediate and parse_optional_immediate helper pair that return either an int or a string literal (or None)

@kingiler kingiler requested a review from superlopuh July 31, 2023 13:14
Comment on lines 2995 to 3000
def _parse_int_or_str_literal(parser: Parser, msg: str) -> int | str:
if (term := parser.parse_optional_integer()) is not None:
return term
if (term := parser.parse_optional_str_literal()) is not None:
return term
parser.raise_error(msg)
Copy link
Member

Choose a reason for hiding this comment

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

I would recommend something like this:

Suggested change
def _parse_int_or_str_literal(parser: Parser, msg: str) -> int | str:
if (term := parser.parse_optional_integer()) is not None:
return term
if (term := parser.parse_optional_str_literal()) is not None:
return term
parser.raise_error(msg)
def _parse_optional_immediate_value(parser: Parser, integer_type: type[IntegerType]) -> IntegerAttr | LabelAttr | None:
"""
Parse an optional immediate value. If an int is parsed, an integer attr with the specified type is created.
"""
if (term := parser.parse_optional_integer()) is not None:
return IntegerAttr(immediate, type)
if (term := parser.parse_optional_str_literal()) is not None:
return LabelAttr(term)
return None
def _parse_immediate_value(parser: Parser, integer_type: type[IntegerType]) -> IntegerAttr | LabelAttr:
return parser.expect(lambda: _parse_optional_literal_value(parser, integer_type), "literal value")

It feels like the error message should be the same for all clients, and not a part of the helper API. We might have optional immediate values so an optional helper would also be nice.

Copy link
Member

@superlopuh superlopuh left a comment

Choose a reason for hiding this comment

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

LGTM, now just need some tests :)

@kingiler kingiler merged commit 3961727 into main Aug 1, 2023
10 checks passed
@kingiler kingiler deleted the fix_riscv_attr_rdrsimmj branch August 1, 2023 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dialects Changes on the dialects minor For minor PRs, easy and quick to review, quickly mergeable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants