[PATCH] D130232: Reassoc FMF should not optimize FMA(a, 0, b) to (b)

Sven van Haastregt via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 25 07:48:47 PDT 2022


svenvh updated this revision to Diff 447339.
svenvh marked an inline comment as done.
svenvh added a comment.

Added FIXME and updated CHECK lines to avoid CHECK-NOT.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D130232/new/

https://reviews.llvm.org/D130232

Files:
  llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
  llvm/test/CodeGen/AArch64/neon-fma-FMF.ll


Index: llvm/test/CodeGen/AArch64/neon-fma-FMF.ll
===================================================================
--- llvm/test/CodeGen/AArch64/neon-fma-FMF.ll
+++ llvm/test/CodeGen/AArch64/neon-fma-FMF.ll
@@ -54,3 +54,17 @@
 	%tmp2 = fsub <2 x float> %C, %tmp1;
 	ret <2 x float> %tmp2
 }
+
+; Regression test: contract FMF allows folding (A * 0 + B) to FMA(A, 0, B), but
+; reassoc FMF must not allow further folding to just (B) without additional
+; FMFs (ninf, nnan)
+define float @fma_zero(float %A, float %B) {
+; CHECK-LABEL: fma_zero:
+; CHECK:       // %bb.0:
+; CHECK-NEXT:    movi d2, #0000000000000000
+; CHECK-NEXT:    fmadd s0, s0, s2, s1
+; CHECK-NEXT:    ret
+	%tmp1 = fmul contract reassoc float %A, 0.0e+0;
+	%tmp2 = fadd contract reassoc float %B, %tmp1;
+	ret float %tmp2
+}
Index: llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -15002,7 +15002,7 @@
   // FMA nodes have flags that propagate to the created nodes.
   SelectionDAG::FlagInserter FlagsInserter(DAG, N);
 
-  bool UnsafeFPMath =
+  bool CanReassociate =
       Options.UnsafeFPMath || N->getFlags().hasAllowReassociation();
 
   // Constant fold FMA.
@@ -15026,7 +15026,8 @@
        CostN1 == TargetLowering::NegatibleCost::Cheaper))
     return DAG.getNode(ISD::FMA, DL, VT, NegN0, NegN1, N2);
 
-  if (UnsafeFPMath) {
+  // FIXME: use fast math flags instead of Options.UnsafeFPMath
+  if (Options.UnsafeFPMath) {
     if (N0CFP && N0CFP->isZero())
       return N2;
     if (N1CFP && N1CFP->isZero())
@@ -15043,7 +15044,7 @@
      !DAG.isConstantFPBuildVectorOrConstantFP(N1))
     return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
 
-  if (UnsafeFPMath) {
+  if (CanReassociate) {
     // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
     if (N2.getOpcode() == ISD::FMUL && N0 == N2.getOperand(0) &&
         DAG.isConstantFPBuildVectorOrConstantFP(N1) &&
@@ -15084,7 +15085,7 @@
     }
   }
 
-  if (UnsafeFPMath) {
+  if (CanReassociate) {
     // (fma x, c, x) -> (fmul x, (c+1))
     if (N1CFP && N0 == N2) {
       return DAG.getNode(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130232.447339.patch
Type: text/x-patch
Size: 2206 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220725/34a59221/attachment.bin>


More information about the llvm-commits mailing list