Skip to content

Conversation

@7FM
Copy link
Contributor

@7FM 7FM commented Nov 25, 2024

The pattern select %x, true, false => %x is only valid in case that the return type is identical to the type of %x (i.e., i1). Hence, the check isInteger(1) was replaced with isSignlessInteger(1).

Fixes: #117554

@7FM 7FM added the mlir:arith label Nov 25, 2024
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Nov 25, 2024

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-arith

Author: None (7FM)

Changes

The pattern select %x, true, false => %x is only valid in case that the return type is identical to the type of %x (i.e., i1). Hence, the check isInteger(1) was replaced with isSignlessInteger(1).

Fixes: #117554


Full diff: https://github.com/llvm/llvm-project/pull/117555.diff

1 Files Affected:

  • (modified) mlir/lib/Dialect/Arith/IR/ArithOps.cpp (+2-1)
diff --git a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
index 254f54d9e459e1..f2f23954d5c191 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
+++ b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
@@ -2314,7 +2314,8 @@ OpFoldResult arith::SelectOp::fold(FoldAdaptor adaptor) {
     return trueVal;
 
   // select %x, true, false => %x
-  if (getType().isInteger(1) && matchPattern(adaptor.getTrueValue(), m_One()) &&
+  if (getType().isSignlessInteger(1) &&
+      matchPattern(adaptor.getTrueValue(), m_One()) &&
       matchPattern(adaptor.getFalseValue(), m_Zero()))
     return condition;
 

@llvmbot llvmbot added the mlir label Nov 25, 2024
@github-actions
Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Discourse for more information.

The pattern `select %x, true, false => %x` is only valid in case that the return type is identical to the type of `%x` (i.e., i1).
Hence, the check `isInteger(1)` was replaced with `isSignlessInteger(1)`.
@7FM 7FM requested a review from kuhar November 25, 2024 13:45
Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

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

We should add a LIT test for the case that used to fail.

if (getType().isSignlessInteger(1) &&
matchPattern(adaptor.getTrueValue(), m_One()) &&
matchPattern(adaptor.getFalseValue(), m_Zero()))
return condition;
Copy link
Member

Choose a reason for hiding this comment

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

I think we could also have a canon pattern that can broadcast the condition in the vector case, but that's probably better for a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since I am not familiar with the Arith + vector use case, I would prefer a separate PR for this.

@joker-eph
Copy link
Collaborator

The pattern select %x, true, false => %x is only valid in case that the return type is identical to the type of %x (i.e., i1). Hence, the check isInteger(1) was replaced with isSignlessInteger(1).

Can we still replace by a bitcast when the types are different?

@7FM
Copy link
Contributor Author

7FM commented Nov 25, 2024

We should add a LIT test for the case that used to fail.

I agree, but at first glance, I couldn't find an upstream MLIR dialect that supports unsigned/signed constants to trigger this bug. But I might have missed a suitable dialect.

@7FM
Copy link
Contributor Author

7FM commented Nov 25, 2024

The pattern select %x, true, false => %x is only valid in case that the return type is identical to the type of %x (i.e., i1). Hence, the check isInteger(1) was replaced with isSignlessInteger(1).

Can we still replace by a bitcast when the types are different?

error: 'arith.bitcast' op result #0 must be signless-integer-or-float-like or memref of signless-integer or float, but got 'ui1'
I don't think so, at least not without further modifications.

@kuhar
Copy link
Member

kuhar commented Nov 25, 2024

We should add a LIT test for the case that used to fail.

I agree, but at first glance, I couldn't find an upstream MLIR dialect that supports unsigned/signed constants to trigger this bug. But I might have missed a suitable dialect.

How about adding a new test op to the test dialect? We already use test.with_bounds in arith tests.

@7FM
Copy link
Contributor Author

7FM commented Nov 26, 2024

We should add a LIT test for the case that used to fail.

I agree, but at first glance, I couldn't find an upstream MLIR dialect that supports unsigned/signed constants to trigger this bug. But I might have missed a suitable dialect.

How about adding a new test op to the test dialect? We already use test.with_bounds in arith tests.

Thanks for the hint! I didn't know that such a dialect existed.

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

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

LGTM

@kuhar kuhar merged commit 619e4b7 into llvm:main Nov 26, 2024
8 checks passed
@github-actions
Copy link

@7FM Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MLIR][Arith] Invalid SelectOp folding

4 participants