Skip to content

Conversation

@jinpzhanAMD
Copy link

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:

  1. Change the computation order, potentially affecting precision
  2. Prevent the backend from using efficient rcp instructions
  3. Miss opportunities for FMA or other specialized instructions

This change prevents InstCombine from interfering with backend-level
reciprocal optimizations and maintains the intended computation order.

@jinpzhanAMD jinpzhanAMD requested a review from nikic as a code owner November 12, 2025 03:27
@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 llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Nov 12, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 12, 2025

@llvm/pr-subscribers-llvm-transforms

Author: None (jinpzhanAMD)

Changes

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:

  1. Change the computation order, potentially affecting precision
  2. Prevent the backend from using efficient rcp instructions
  3. Miss opportunities for FMA or other specialized instructions

This change prevents InstCombine from interfering with backend-level
reciprocal optimizations and maintains the intended computation order.


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (+8-1)
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);

@dtcxzyw dtcxzyw added the floating-point Floating-point math label Nov 12, 2025
@dtcxzyw dtcxzyw requested a review from arsenm November 12, 2025 15:47
Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

Missing tests

Comment on lines +798 to +799
// Constant folding is not affected by allowReciprocal,
// since constants are calculated at compile time.
Copy link
Contributor

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

Copy link
Contributor

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).

@jcranmer-intel
Copy link
Contributor

1. Change the computation order, potentially affecting precision

If reassoc flag is enabled, the user is explicitly letting us change the computation order. (We do have some issues with the canonical reassociation interrupting thinks like FMA formation, but that's a somewhat orthogonal issue).

2. Prevent the backend from using efficient rcp instructions

This is unclear to me--(X * Z) / Y seems to me to be just as easily be turned into (X * Z) * rcp(Y) as the original (X / Y) * Z can be turned into (X * rcp(Y)) * Z. What's the actual hangup?

@jinpzhanAMD
Copy link
Author

Thanks for the reviews.
Reassoc flag and the arcp flag are two independent (orthogonal) fmf flags, which shouldn't depend on each other.
The precision issue I'm working on can be fixed from our internal repo, thus I would close this PR.

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

Labels

floating-point Floating-point math llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants