Skip to content

[InstCombine] Infer exact for lshr by cttz #136696

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

Merged
merged 13 commits into from
Apr 29, 2025
Merged

[InstCombine] Infer exact for lshr by cttz #136696

merged 13 commits into from
Apr 29, 2025

Conversation

houngkoungting
Copy link
Contributor

@houngkoungting houngkoungting commented Apr 22, 2025

Infer the 'exact' flag on an 'lshr' or 'ashr' instruction when the shift amount is computed via a 'cttz' intrinsic on the same operand.

Proof: https://alive2.llvm.org/ce/z/CQR2PG

Fixes #131444.

@houngkoungting houngkoungting requested a review from nikic as a code owner April 22, 2025 13:36
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 Apr 22, 2025

@llvm/pr-subscribers-llvm-transforms

Author: 黃國庭 (houngkoungting)

Changes

Fix #131444
This patch adds a pattern . It match to infer the 'exact' flag on an 'lshr' instruction when the shift amount is computed via a 'cttz' intrinsic on the same operand.


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp (+12)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 90cd279e8a457..c39b7dae434e0 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -994,6 +994,18 @@ static bool setShiftFlags(BinaryOperator &I, const SimplifyQuery &Q) {
       I.setIsExact();
       return true;
     }
+    //Fix #131444
+    if (auto *Cttz = dyn_cast<IntrinsicInst>(I.getOperand(1))) {
+      if (Cttz->getIntrinsicID() == Intrinsic::cttz &&
+          Cttz->getOperand(0) == I.getOperand(0)) {
+        if (auto *Const = dyn_cast<ConstantInt>(Cttz->getOperand(1))) {
+          if (Const->isOne()) {  
+            I.setIsExact();
+            return true;
+          }
+        }
+      }
+    }
   }
 
   // Compute what we know about shift count.

@tclin914
Copy link
Contributor

Could you provide the testcase for this change

@tclin914 tclin914 self-requested a review April 22, 2025 14:12
@nikic
Copy link
Contributor

nikic commented Apr 22, 2025

Note that an existing InstCombine test is failing as well, please update it.

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.

Copy link

github-actions bot commented Apr 22, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

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

Please add some tests to demonstrate the change. See also https://llvm.org/docs/InstCombineContributorGuide.html#tests

@houngkoungting
Copy link
Contributor Author

HI @nikic @tclin914 @dtcxzyw , I think I missed several steps.
Should I close this PR and open a new one with everything fixed? Thanks!

@dtcxzyw
Copy link
Member

dtcxzyw commented Apr 24, 2025

Should I close this PR and open a new one with everything fixed? Thanks!

It is okay to fix them in this PR.

@houngkoungting
Copy link
Contributor Author

HI @tclin914 @nikic @dtcxzyw ,I've updated it. Please take a look when you have time. Thanks!

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

For the proofs, please prefer linking to alive2.llvm.org instead of directly embedding the proof in the PR, e.g. like this: https://alive2.llvm.org/ce/z/CQR2PG

Note that this also shows that you do not need the second argument to be true. The reason false works is that if the input is zero, cttz will return the bit width and shifting by the bit width returns poison. Adding an extra exact flag doesn't affect this.

@houngkoungting
Copy link
Contributor Author

HI @nikic , I’m not sure if it should be like the cttz-shift-exact.ll I updated.Please take a look when you have time. Thanks!

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

Looks mostly good now.

%sh = ashr i32 %x, %cttz
ret i32 %sh
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Please also add one negative test where the cttz operand is an unrelated value.

@houngkoungting
Copy link
Contributor Author

HI @nikic ,I've added the negative test case where the cttz operand is unrelated.Please take a look when you have time. Thanks!

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@nikic nikic merged commit f37ab15 into llvm:main Apr 29, 2025
7 checks passed
Copy link

@houngkoungting 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!

IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Infer the 'exact' flag on an 'lshr' or 'ashr' instruction when the shift
amount is computed via a 'cttz' intrinsic on the same operand.
 
Proof: https://alive2.llvm.org/ce/z/CQR2PG

Fixes llvm#131444.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Infer the 'exact' flag on an 'lshr' or 'ashr' instruction when the shift
amount is computed via a 'cttz' intrinsic on the same operand.
 
Proof: https://alive2.llvm.org/ce/z/CQR2PG

Fixes llvm#131444.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Infer the 'exact' flag on an 'lshr' or 'ashr' instruction when the shift
amount is computed via a 'cttz' intrinsic on the same operand.
 
Proof: https://alive2.llvm.org/ce/z/CQR2PG

Fixes llvm#131444.
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
Infer the 'exact' flag on an 'lshr' or 'ashr' instruction when the shift
amount is computed via a 'cttz' intrinsic on the same operand.
 
Proof: https://alive2.llvm.org/ce/z/CQR2PG

Fixes llvm#131444.
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
Infer the 'exact' flag on an 'lshr' or 'ashr' instruction when the shift
amount is computed via a 'cttz' intrinsic on the same operand.
 
Proof: https://alive2.llvm.org/ce/z/CQR2PG

Fixes llvm#131444.
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.

[InstCombine] Infer exact for lshr by cttz
5 participants