-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[InstCombine] Skip division sinking with allowReciprocal flag #167633
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
Conversation
|
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 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. |
|
@llvm/pr-subscribers-llvm-transforms Author: None (jinpzhanAMD) ChangesWhen the allowReciprocal fast-math flag is set, avoid reassociating (X / Y) * Z into (X * Z) / Y.
This change prevents InstCombine from interfering with backend-level Full diff: https://github.com/llvm/llvm-project/pull/167633.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
index a9aacc707cc20..88700af60d633 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -795,7 +795,8 @@ Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
return BinaryOperator::CreateFDivFMF(CC1, X, FMF);
}
if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) {
- // FIXME: This seems like it should also be checking for arcp
+ // Constant folding is not affected by allowReciprocal,
+ // since constants are calculated at compile time.
// (X / C1) * C --> X * (C / C1)
Constant *CDivC1 =
ConstantFoldBinaryOpOperands(Instruction::FDiv, C, C1, DL);
@@ -837,6 +838,12 @@ Instruction *InstCombinerImpl::foldFMulReassoc(BinaryOperator &I) {
m_Value(Z)))) {
BinaryOperator *DivOp = cast<BinaryOperator>(((Z == Op0) ? Op1 : Op0));
FastMathFlags FMF = I.getFastMathFlags() & DivOp->getFastMathFlags();
+ // Preserve (X / Y) * Z when reciprocal math is enabled to allow backend
+ // rcp instruction emission. Sinking to (X * Z) / Y would prevent this
+ // optimization, alter computation order and potentially affecting
+ // precision.
+ if (FMF.allowReciprocal())
+ return nullptr;
if (FMF.allowReassoc()) {
// Sink division: (X / Y) * Z --> (X * Z) / Y
auto *NewFMul = Builder.CreateFMulFMF(X, Z, FMF);
|
arsenm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing tests
| // Constant folding is not affected by allowReciprocal, | ||
| // since constants are calculated at compile time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't exactly how semantics works
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, despite the original comment, this really needs to be matching against the reassoc flag, not the arcp flag (since arcp explicitly doesn't allow reassociation of the division operation, only the inversion of the division).
If
This is unclear to me-- |
|
Thanks for the reviews. |
When the allowReciprocal fast-math flag is set, avoid reassociating (X / Y) * Z into (X * Z) / Y.
The original form (X / Y) * Z can be optimized by the backend using reciprocal instructions as X * rcp(Y) * Z, which may be more efficient than transforming it to (X * Z) / Y. Sinking the division would:
This change prevents InstCombine from interfering with backend-level
reciprocal optimizations and maintains the intended computation order.