[llvm] [SelectionDAG] Fix NaN regression in fma dag-combine. (PR #146592)

James Y Knight via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 1 12:20:09 PDT 2025


https://github.com/jyknight created https://github.com/llvm/llvm-project/pull/146592

After 901e1390c9778a191256335d37802bc631c2d183 (#127770), the DAG combine would transform `fma(x, 0.0, 1.0)` into `1.0` if `-fp-contract=fast` was enabled, in addition to when 'x' is marked nnan/ninf.

It's only valid in the latter case, not the former, so delete the extra condition.

>From 0c9e0ab63887a6d665dbca00071e8ae861299d97 Mon Sep 17 00:00:00 2001
From: James Y Knight <jyknight at google.com>
Date: Tue, 1 Jul 2025 15:14:11 -0400
Subject: [PATCH] [SelectionDAG] Fix NaN regression in fma dag-combine.

After 901e1390c9778a191256335d37802bc631c2d183 (#127770), the DAG
combine would transform `fma(x, 0.0, 1.0)` into `1.0` if
`-fp-contract=fast` was enabled, in addition to when 'x' is marked
nnan/ninf. It's only valid in the latter case, not the former, so
delete the extra condition.
---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp     |  3 +--
 llvm/test/CodeGen/X86/dag-combiner-fma-folding.ll | 12 ++++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index c43677f8b925c..bfc061b404560 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -18087,8 +18087,7 @@ template <class MatchContextClass> SDValue DAGCombiner::visitFMA(SDNode *N) {
 
   // FIXME: use fast math flags instead of Options.UnsafeFPMath
   // TODO: Finally migrate away from global TargetOptions.
-  if (Options.AllowFPOpFusion == FPOpFusion::Fast ||
-      (Options.NoNaNsFPMath && Options.NoInfsFPMath) ||
+  if ((Options.NoNaNsFPMath && Options.NoInfsFPMath) ||
       (N->getFlags().hasNoNaNs() && N->getFlags().hasNoInfs())) {
     if (Options.NoSignedZerosFPMath || N->getFlags().hasNoSignedZeros() ||
         (N2CFP && !N2CFP->isExactlyValue(-0.0))) {
diff --git a/llvm/test/CodeGen/X86/dag-combiner-fma-folding.ll b/llvm/test/CodeGen/X86/dag-combiner-fma-folding.ll
index 2bd7dc445a02b..6291100f42c3d 100644
--- a/llvm/test/CodeGen/X86/dag-combiner-fma-folding.ll
+++ b/llvm/test/CodeGen/X86/dag-combiner-fma-folding.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
 ; RUN: llc -mtriple=x86_64-- --start-before=x86-isel -mattr=+avx,+fma %s -o - | FileCheck %s
+; RUN: llc -mtriple=x86_64-- --start-before=x86-isel -mattr=+avx,+fma %s -o - -fp-contract=fast | FileCheck %s
 
 define double @fma_folding(double %x) {
 ; CHECK-LABEL: fma_folding:
@@ -20,3 +21,14 @@ define double @fma_no_folding(double %x) {
   %fused = call contract nnan ninf double @llvm.fma.f64(double %x, double 0.0, double -0.0)
   ret double %fused
 }
+
+define double @fma_no_fold_potential_nan(double %x) {
+; CHECK-LABEL: fma_no_fold_potential_nan:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    vxorpd %xmm1, %xmm1, %xmm1
+; CHECK-NEXT:    vfmadd213sd {{.*#+}} xmm0 = (xmm1 * xmm0) + mem
+; CHECK-NEXT:    retq
+ %fused = call contract double @llvm.fma.f64(double %x, double 0.0, double 1.0)
+ ret double %fused
+}
+



More information about the llvm-commits mailing list