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: (llvm) Registering llvm.icmp #3356

Merged
merged 12 commits into from
Nov 1, 2024
Merged

dialects: (llvm) Registering llvm.icmp #3356

merged 12 commits into from
Nov 1, 2024

Conversation

lfrenot
Copy link
Collaborator

@lfrenot lfrenot commented Oct 30, 2024

No description provided.

@lfrenot lfrenot added the dialects Changes on the dialects label Oct 30, 2024
@lfrenot lfrenot requested a review from tobiasgrosser October 30, 2024 11:37
@lfrenot lfrenot self-assigned this Oct 30, 2024
Copy link
Contributor

@tobiasgrosser tobiasgrosser left a comment

Choose a reason for hiding this comment

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

Otherwise, this looks great to me. Maybe @superlopuh and @luisacicolini have comments?

tests/filecheck/dialects/llvm/arithmetic.mlir Show resolved Hide resolved
@lfrenot
Copy link
Collaborator Author

lfrenot commented Oct 30, 2024

I'm getting this error

# |     raise ParseError(at_position, msg)
# | xdsl.utils.exceptions.ParseError: stdin:26:72
# |   %icmp_eq = "llvm.icmp"(%arg0, %arg1) <{"predicate" = #llvm.predicateeq}> : (i32, i32) -> i32
# |                                                                         ^
# |                                                                         'llvm.predicateeq' is not registered

Since the predicate is supposed to be an i64 (according to mlir-opt --mlir-print-op-generic), I guess I have to change the way I'm representing it right now

Comment on lines 627 to 646
if i == 0:
return ICmpPredicateFlag.EQ
elif i == 1:
return ICmpPredicateFlag.NE
elif i == 2:
return ICmpPredicateFlag.SLT
elif i == 3:
return ICmpPredicateFlag.SLE
elif i == 4:
return ICmpPredicateFlag.SGE
elif i == 5:
return ICmpPredicateFlag.SGT
elif i == 6:
return ICmpPredicateFlag.ULT
elif i == 7:
return ICmpPredicateFlag.ULE
elif i == 8:
return ICmpPredicateFlag.UGE
elif i == 9:
return ICmpPredicateFlag.UGT
Copy link
Member

Choose a reason for hiding this comment

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

There has to be a better way of doing this, I'd expect some helper to exist in the Python enum implementation, otherwise we should cache this mapping.

@superlopuh
Copy link
Member

This PR adds an operation and an attribute, and only registers the new op in the dialect.

@lfrenot
Copy link
Collaborator Author

lfrenot commented Oct 30, 2024

# | ValueError: 0 is not a valid ICmpPredicateFlag

I don't get why I'm getting this error with the current def of ICmpPredicateFlag

Copy link

codecov bot commented Oct 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.12%. Comparing base (e6c7282) to head (ee7b874).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3356      +/-   ##
==========================================
+ Coverage   90.10%   90.12%   +0.01%     
==========================================
  Files         449      449              
  Lines       56599    56693      +94     
  Branches     5429     5434       +5     
==========================================
+ Hits        51001    51092      +91     
- Misses       4164     4166       +2     
- Partials     1434     1435       +1     

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

@lfrenot
Copy link
Collaborator Author

lfrenot commented Oct 30, 2024

@superlopuh
Does it look good for you?
Do you know how to properly do the enum thing?

lhs = operand_def(T)
rhs = operand_def(T)
res = result_def(T)
predicate = prop_def(Attribute)
Copy link
Member

Choose a reason for hiding this comment

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

why this change? it was better before as a strenum, no?

@superlopuh
Copy link
Member

I would recommend reverting your last change, and looking into how to index the enum cases

@superlopuh
Copy link
Member

superlopuh commented Oct 30, 2024

from enum import StrEnum


class MyEnum(StrEnum):
    a = "a"
    b = "b"


MY_ENUM_CASES = tuple(MyEnum)

print(MY_ENUM_CASES[1])

This seems to work

@superlopuh
Copy link
Member

and is not O(number of cases)

xdsl/dialects/llvm.py Outdated Show resolved Hide resolved
@lfrenot
Copy link
Collaborator Author

lfrenot commented Oct 31, 2024

@superlopuh do you have any idea on how to fix the current error?

Co-authored-by: Sasha Lopoukhine <[email protected]>
@superlopuh
Copy link
Member

Ah no, it looks like I was too quick to approve. I would recommend marking the PR as draft if the tests don't pass, and reaching out on Zulip to iterate

@superlopuh
Copy link
Member

I've actually never used this part of the API before, I'm happy to take a look in a bit if you're stuck, but it would be worth messaging on Zulip in case someone with more experience with your issue knows the answer directly. In this case pinging @alexarice would probably be a good idea, I think he's dealt with these attributes somewhat recently.



@irdl_attr_definition
class ICmpPredicateAttr(EnumAttribute[ICmpPredicateFlag]):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe you want OpaqueSyntaxAttribute or SpacedOpaqueSyntaxAttribute here to get it to print properly



@irdl_op_definition
class ICmpOp(IRDLOperation, ABC):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this marked as an abstract class?

self.predicate.print_parameter(printer)

def print(self, printer: Printer):
printer.print(' "')
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think in general, print_string is preferred to print but this is a super nit

@lfrenot
Copy link
Collaborator Author

lfrenot commented Oct 31, 2024

@alexarice Thank you for the suggestions, that worked

@superlopuh are we good to merge then?

Copy link
Collaborator

@alexarice alexarice left a comment

Choose a reason for hiding this comment

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

I just tested the mlir version and it does this:

bin/mlir-opt ../../test.mlir --mlir-print-op-generic
"builtin.module"() ({
  %0:2 = "test.op"() : () -> (i32, i32)
  %1 = "llvm.icmp"(%0#0, %0#1) <{predicate = 0 : i64}> : (i32, i32) -> i1
}) : () -> ()

I feel the generic output of your function is not the same. Unsure if this is a blocker but I just wanted to point it out.

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! The issue Alex pointed out is worth looking into, but I don't want to block on that necessarily. It would be great if you could take a quick look at the generic syntax before merging, and comment here.

@tobiasgrosser
Copy link
Contributor

Let's merge this for now. @lfrenot, can you still check @alexarice's comment.

@tobiasgrosser tobiasgrosser merged commit 05506a1 into main Nov 1, 2024
14 checks passed
@tobiasgrosser tobiasgrosser deleted the llvm-icmp branch November 1, 2024 03:18
@tobiasgrosser
Copy link
Contributor

I just tested the mlir version and it does this:

bin/mlir-opt ../../test.mlir --mlir-print-op-generic
"builtin.module"() ({
  %0:2 = "test.op"() : () -> (i32, i32)
  %1 = "llvm.icmp"(%0#0, %0#1) <{predicate = 0 : i64}> : (i32, i32) -> i1
}) : () -> ()

I feel the generic output of your function is not the same. Unsure if this is a blocker but I just wanted to point it out.

@lfrenot, we should indeed be consistent with MLIR here. I just tried to use your icmp implementation to parse our test cases, but it failed as MLIR emits an integer attribute and you added your own attribute type. It would be great if this could be fixed.

EdmundGoodman pushed a commit to EdmundGoodman/xdsl that referenced this pull request Dec 6, 2024
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.

4 participants