Skip to content

Commit ae21048

Browse files
authored
[SelectionDAG] Fix NaN regression in fma dag-combine. (#146592)
After 901e139 (#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.
1 parent 475cd8d commit ae21048

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18087,8 +18087,7 @@ template <class MatchContextClass> SDValue DAGCombiner::visitFMA(SDNode *N) {
1808718087

1808818088
// FIXME: use fast math flags instead of Options.UnsafeFPMath
1808918089
// TODO: Finally migrate away from global TargetOptions.
18090-
if (Options.AllowFPOpFusion == FPOpFusion::Fast ||
18091-
(Options.NoNaNsFPMath && Options.NoInfsFPMath) ||
18090+
if ((Options.NoNaNsFPMath && Options.NoInfsFPMath) ||
1809218091
(N->getFlags().hasNoNaNs() && N->getFlags().hasNoInfs())) {
1809318092
if (Options.NoSignedZerosFPMath || N->getFlags().hasNoSignedZeros() ||
1809418093
(N2CFP && !N2CFP->isExactlyValue(-0.0))) {

llvm/test/CodeGen/X86/dag-combiner-fma-folding.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
22
; RUN: llc -mtriple=x86_64-- --start-before=x86-isel -mattr=+avx,+fma %s -o - | FileCheck %s
3+
; RUN: llc -mtriple=x86_64-- --start-before=x86-isel -mattr=+avx,+fma %s -o - -fp-contract=fast | FileCheck %s
34

45
define double @fma_folding(double %x) {
56
; CHECK-LABEL: fma_folding:
@@ -20,3 +21,14 @@ define double @fma_no_folding(double %x) {
2021
%fused = call contract nnan ninf double @llvm.fma.f64(double %x, double 0.0, double -0.0)
2122
ret double %fused
2223
}
24+
25+
define double @fma_no_fold_potential_nan(double %x) {
26+
; CHECK-LABEL: fma_no_fold_potential_nan:
27+
; CHECK: # %bb.0:
28+
; CHECK-NEXT: vxorpd %xmm1, %xmm1, %xmm1
29+
; CHECK-NEXT: vfmadd213sd {{.*#+}} xmm0 = (xmm1 * xmm0) + mem
30+
; CHECK-NEXT: retq
31+
%fused = call contract double @llvm.fma.f64(double %x, double 0.0, double 1.0)
32+
ret double %fused
33+
}
34+

0 commit comments

Comments
 (0)