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: Print FloatAttr special values #3377

Merged
merged 11 commits into from
Nov 4, 2024
Merged

Conversation

n-io
Copy link
Collaborator

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

This prints FloatAttr special values nan, inf, -inf as a hexadecimal binary representation to align with mlir-opt as described in #3370

Also see the FloatAttr docs or give it a try on godbolt.

@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
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 (d6db473) to head (87201d4).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3377      +/-   ##
==========================================
+ Coverage   90.12%   90.13%   +0.01%     
==========================================
  Files         452      452              
  Lines       57098    57134      +36     
  Branches     5491     5493       +2     
==========================================
+ Hits        51459    51499      +40     
+ Misses       4183     4180       -3     
+ Partials     1456     1455       -1     

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

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.

We do not depend on numpy, and JAX is an optional dependency, so we can't use it for core features.

@n-io n-io requested a review from superlopuh November 1, 2024 15:52
Co-authored-by: Sasha Lopoukhine <superlopuh@gmail.com>
@n-io n-io requested a review from superlopuh November 2, 2024 21:28
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.

Thank you!

xdsl/printer.py Outdated
Comment on lines 541 to 552
if isinstance(attr_type, Float16Type):
self.print_string(
f"{hex(struct.unpack('<H', struct.pack('<e', value.data))[0])} : "
)
elif isinstance(attr_type, Float32Type):
self.print_string(
f"{hex(struct.unpack('<I', struct.pack('<f', value.data))[0])} : "
)
elif isinstance(attr_type, Float64Type):
self.print_string(
f"{hex(struct.unpack('<Q', struct.pack('<d', value.data))[0])} : "
)
Copy link
Member

Choose a reason for hiding this comment

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

Now that I think about it, it feels like it would be better to use the helpers in bitwise_casts after all, using the struct or ctypes approach. We will need the logic in a few places, such as when storing float data in assembly, and other optimisations, so it would be worth having it in an easily reusable place.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done as suggested.

@n-io
Copy link
Collaborator Author

n-io commented Nov 4, 2024

I have added further bitwise_cast functions and outlined the print float logic, as it is also used for printing dense elements.

@n-io n-io merged commit 12bbf96 into main Nov 4, 2024
14 checks passed
@n-io n-io deleted the nicolai/print-float-attr-specials branch November 4, 2024 12:13
n-io added 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>
lfrenot pushed a commit that referenced this pull request Nov 4, 2024
This prints `FloatAttr` special values `nan`, `inf`, `-inf` as a
hexadecimal binary representation to align with mlir-opt as described in
#3370

Also see the [FloatAttr
docs](https://mlir.llvm.org/docs/Dialects/Builtin/#floatattr) or give it
a try on [godbolt](https://godbolt.org/z/89TfsvaKT).

---------

Co-authored-by: n-io <n-io@users.noreply.github.com>
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>
lfrenot pushed a commit that referenced this pull request Nov 4, 2024
This prints `FloatAttr` special values `nan`, `inf`, `-inf` as a
hexadecimal binary representation to align with mlir-opt as described in

Also see the [FloatAttr
docs](https://mlir.llvm.org/docs/Dialects/Builtin/#floatattr) or give it
a try on [godbolt](https://godbolt.org/z/89TfsvaKT).

---------

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
This prints `FloatAttr` special values `nan`, `inf`, `-inf` as a
hexadecimal binary representation to align with mlir-opt as described in
xdslproject#3370

Also see the [FloatAttr
docs](https://mlir.llvm.org/docs/Dialects/Builtin/#floatattr) or give it
a try on [godbolt](https://godbolt.org/z/89TfsvaKT).

---------

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.

3 participants