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

dialects: (arith) Add support for bitcast operation #3805

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jakedves
Copy link

Implementation of MLIR's arith.bitcast op. Test cases are passing but type checking on t1 and t2 variables isn't

@jakedves jakedves marked this pull request as draft January 29, 2025 17:03
@jakedves jakedves changed the title dialects: (arith) Add support for bitcast operation [WIP] dialects: (arith) Add support for bitcast operation Jan 29, 2025
Copy link

codecov bot commented Jan 29, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.23%. Comparing base (2338bb0) to head (eac6de1).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3805   +/-   ##
=======================================
  Coverage   91.23%   91.23%           
=======================================
  Files         461      461           
  Lines       57487    57534   +47     
  Branches     5548     5556    +8     
=======================================
+ Hits        52449    52494   +45     
- Misses       3615     3616    +1     
- Partials     1423     1424    +1     

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

xdsl/dialects/arith.py Outdated Show resolved Hide resolved
xdsl/dialects/arith.py Outdated Show resolved Hide resolved
@jakedves jakedves marked this pull request as ready for review January 29, 2025 17:27
@jakedves jakedves changed the title [WIP] dialects: (arith) Add support for bitcast operation dialects: (arith) Add support for bitcast operation Jan 29, 2025
Copy link
Collaborator

@compor compor left a comment

Choose a reason for hiding this comment

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

Very good, thanks for this. I've left a few comments.

)

@staticmethod
def _equal_bitwidths(type_a: Attribute, type_b: Attribute) -> bool:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this something that could be reused? If so, maybe pull out as a utility method?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree, it feels like this could be in xdsl.utils.type

Comment on lines +1283 to +1285
raise VerifyException(
"Expected operand and result types to be signless-integer-or-float-like"
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I find it a bit confusing that this returns bool and can also throw an exception.
Maybe leave the exception raising to clients and just plainly return true/false here.

Comment on lines +292 to +298
err_msg1 = "Expected operand and result types to be signless-integer-or-float-like"
err_msg2 = "'arith.bitcast' operand and result types must have equal bitwidths"
err_msg3 = "'arith.bitcast' operand and result must both be containers or scalars"
err_msg4 = (
"'arith.bitcast' operand and result type elements must have equal bitwidths"
)
err_msg5 = "'arith.bitcast' operand and result types must have the same shape"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please make these messages part of the parametrize decorator to match each individual case

super().__init__(operands=[in_arg], result_types=[target_type])

def verify_(self) -> None:
# check if we have a ContainerType
Copy link
Collaborator

Choose a reason for hiding this comment

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

this comment doesn't add much and it's a bit confusing since we first check for shaped types

xdsl/dialects/arith.py Show resolved Hide resolved
@compor compor requested a review from superlopuh January 30, 2025 07:51
@compor compor added the dialects Changes on the dialects label Jan 30, 2025
(VectorType(i64, [3]), VectorType(f64, [3]), True),
(VectorType(f32, [3]), VectorType(i32, [3]), True),
# false cases
(i1, i32, False),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would personally split out the tests that error from the others.

name = "arith.bitcast"

input = operand_def(signlessIntegerLike | floatingPointLike)
result = result_def(signlessIntegerLike | floatingPointLike)
Copy link
Collaborator

Choose a reason for hiding this comment

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

The mlir operation also accepts memrefs

res_type = self.result.type

if isinstance(in_type, ShapedType) and isinstance(res_type, ShapedType):
if in_type.get_shape() != res_type.get_shape():
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it not enough for these to have_compatible_shape?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good point, the logic in upstream MLIR is enforced by the SameOperandsAndResultShape which reuses heavily parts of the SameOperandsAndResultType trait that we got in xDSL recently.
Actually, this operation observes the SameOperandsAndResultShape, but since we don't have that yet, maybe reuse part of that infrastructure in the custom verifier here for now as @alexarice suggests/questions.

)

@staticmethod
def _equal_bitwidths(type_a: Attribute, type_b: Attribute) -> bool:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree, it feels like this could be in xdsl.utils.type

"'arith.bitcast' operand and result types must have the same shape"
)

t1 = get_element_type_or_self(in_type)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could this check not go right at the start of the function? This would save you checking again in the scalar case at the bottom

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

Successfully merging this pull request may close these issues.

3 participants