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
Conversation
Codecov ReportPatch coverage:
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
☔ View full report in Codecov by Sentry. |
xdsl/dialects/riscv.py
Outdated
@@ -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") |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
65caab0
to
5c03ba7
Compare
xdsl/dialects/riscv.py
Outdated
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) |
There was a problem hiding this comment.
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:
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.
There was a problem hiding this 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 :)
No description provided.