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

core: Parse FloatAttr from hexadecimal bit representation #3378

Merged
merged 5 commits into from
Nov 4, 2024

Conversation

n-io
Copy link
Collaborator

@n-io n-io commented Nov 1, 2024

FloatAttrs can have their values specified by a hexadecimal literal. This should be interpreted as the bit representation of the float. However, it is currently interpreted as an int number and then converted to a float, leading to incorrect behaviour.

This PR is the second part of #3370 after #3377

@n-io n-io added bug Something isn't working core xDSL core (ir, textual format, ...) labels Nov 1, 2024
@n-io n-io self-assigned this Nov 1, 2024
Comment on lines 1147 to 1150
hexadecimal_token: Token | None = None
if self._current_token.text[:2] in ["0x", "0X"]:
hexadecimal_token = self._current_token

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 this is the best way to do this, but it prevents losing the information that the token is a hexadecimal literal instead of a generic integer, until the attribute type is parsed.

xdsl/utils/lexer.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Nov 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.13%. Comparing base (654cab0) to head (e23bcbd).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3378      +/-   ##
==========================================
- Coverage   90.13%   90.13%   -0.01%     
==========================================
  Files         452      452              
  Lines       57134    57158      +24     
  Branches     5493     5498       +5     
==========================================
+ Hits        51499    51520      +21     
  Misses       4180     4180              
- Partials     1455     1458       +3     

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

Comment on lines 351 to 362
if self.kind == Token.Kind.INTEGER_LIT and self.text[:2] in ["0x", "0X"]:
match len(self.text):
# the number corresponds to `len('0x') + (float_bitwidth / 4)`
case 6:
return struct.unpack("<e", struct.pack("<H", int(self.text, 16)))[0]
case 10:
return struct.unpack("<f", struct.pack("<I", int(self.text, 16)))[0]
case 18:
return struct.unpack("<d", struct.pack("<Q", int(self.text, 16)))[0]
case _:
# todo support further bitwidths
pass
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't the bitwidth be determined by the floating point type? I would expect this to be in a helper function that takes an int and a bitwidth, possibly in the bitwise_casts file, with dedicated tests, and for the logic to be done inline in parse_optional_number

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good spot, let me fix that

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hadn't really considered implied leading zeros, but mlir-opt supports it

@n-io n-io requested a review from superlopuh November 4, 2024 12:37
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!

@n-io n-io merged commit 26970d9 into main Nov 4, 2024
15 checks passed
@n-io n-io deleted the nicolai/parse-hexadecimal-float-attributes branch November 4, 2024 12:45
lfrenot pushed a commit that referenced this pull request Nov 4, 2024
`FloatAttr`s can have their values specified by a hexadecimal literal.
This should be interpreted as the bit representation of the float.
However, it is currently interpreted as an int number and then converted
to a float, leading to incorrect behaviour.

This PR is the second part of #3370 after #3377

---------

Co-authored-by: n-io <n-io@users.noreply.github.com>
EdmundGoodman pushed a commit to EdmundGoodman/xdsl that referenced this pull request Dec 6, 2024
…t#3378)

`FloatAttr`s can have their values specified by a hexadecimal literal.
This should be interpreted as the bit representation of the float.
However, it is currently interpreted as an int number and then converted
to a float, leading to incorrect behaviour.

This PR is the second part of xdslproject#3370 after xdslproject#3377

---------

Co-authored-by: n-io <n-io@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working core xDSL core (ir, textual format, ...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants