Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@cardigan1008
Copy link

Fixes #162003 (comment).

The current flag propagation assumes that if a select has both ninf and nnan, then the operands of the folded operation must be finite. While this assumption holds for fadd, fsub, and fmul, it does not hold for fdiv.

For example, assume we have:

A = 1.0, B = +Inf
A / B = 0.0  (finite, non-NaN)

The current transform would turn fdiv A, B; select ninf nnan cond, A/B, A; into A / (select ninf nnan cond, B, 1.0). If cond is true, the inner select returns B = +Inf, and due to the propagated ninf, this becomes poison.

This patch add check for operators before flag propagation to avoid fdiv cases.

Alive2: https://alive2.llvm.org/ce/z/o0MJmS

@cardigan1008 cardigan1008 requested a review from nikic as a code owner December 27, 2025 17:10
@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 Dec 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 27, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Yunbo Ni (cardigan1008)

Changes

Fixes #162003 (comment).

The current flag propagation assumes that if a select has both ninf and nnan, then the operands of the folded operation must be finite. While this assumption holds for fadd, fsub, and fmul, it does not hold for fdiv.

For example, assume we have:

A = 1.0, B = +Inf
A / B = 0.0  (finite, non-NaN)

The current transform would turn fdiv A, B; select ninf nnan cond, A/B, A; into A / (select ninf nnan cond, B, 1.0). If cond is true, the inner select returns B = +Inf, and due to the propagated ninf, this becomes poison.

This patch add check for operators before flag propagation to avoid fdiv cases.

Alive2: https://alive2.llvm.org/ce/z/o0MJmS


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp (+8-1)
  • (modified) llvm/test/Transforms/InstCombine/select-binop-foldable-floating-point.ll (+11)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index f52bac5e600cb..67d1845832725 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -555,8 +555,15 @@ Instruction *InstCombinerImpl::foldSelectIntoOp(SelectInst &SI, Value *TrueVal,
       // Examples: -inf + +inf = NaN, -inf - -inf = NaN, 0 * inf = NaN
       // Specifically, if the original select has both ninf and nnan, we can
       // safely propagate the flag.
+      // Note: This property holds for fadd, fsub, and fmul, but does not
+      // hold for fdiv (e.g. A / Inf == 0.0).
+      bool CanInferFiniteOperandsFromResult =
+          TVI->getOpcode() == Instruction::FAdd ||
+          TVI->getOpcode() == Instruction::FSub ||
+          TVI->getOpcode() == Instruction::FMul;
       NewSelFMF.setNoInfs(TVI->hasNoInfs() ||
-                          (NewSelFMF.noInfs() && NewSelFMF.noNaNs()));
+                          (CanInferFiniteOperandsFromResult &&
+                           NewSelFMF.noInfs() && NewSelFMF.noNaNs()));
       cast<Instruction>(NewSel)->setFastMathFlags(NewSelFMF);
     }
     NewSel->takeName(TVI);
diff --git a/llvm/test/Transforms/InstCombine/select-binop-foldable-floating-point.ll b/llvm/test/Transforms/InstCombine/select-binop-foldable-floating-point.ll
index c14dd469f1a6e..83fa28a406f75 100644
--- a/llvm/test/Transforms/InstCombine/select-binop-foldable-floating-point.ll
+++ b/llvm/test/Transforms/InstCombine/select-binop-foldable-floating-point.ll
@@ -409,3 +409,14 @@ define float @select_nnan_fdiv_invalid(i1 %cond, float %A, float %B) {
   %D = select nnan i1 %cond, float %C, float %A
   ret float %D
 }
+
+define float @select_fpclass_fdiv_nnan_ninf(i1 %cond, float %A, float %B) {
+; CHECK-LABEL: @select_fpclass_fdiv_nnan_ninf(
+; CHECK-NEXT:    [[C:%.*]] = select nnan i1 [[COND:%.*]], float [[B:%.*]], float 1.000000e+00
+; CHECK-NEXT:    [[D:%.*]] = fdiv float [[A:%.*]], [[C]]
+; CHECK-NEXT:    ret float [[D]]
+;
+  %C = fdiv float %A, %B
+  %D = select ninf nnan i1 %cond, float %C, float %A
+  ret float %D
+}

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

Labels

llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants