[PATCH] D26957: Fix for lost FastMathFlags in Reassociate optimization

Vyacheslav Klochkov via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 22 00:07:28 PST 2016


v_klochkov created this revision.
v_klochkov added subscribers: llvm-commits, majnemer, mzolotukhin, davide.

Hello,

Please review the fix for lost fast-math flags in Reassociate optimization.

If the input expression tree is made of FP MULs and FastMathFlags permit re-association, then the FastMathFlags must be propagated to the newly generated MUL expression tree.

Thank you,
Vyacheslav Klochkov


https://reviews.llvm.org/D26957

Files:
  llvm/lib/Transforms/Scalar/Reassociate.cpp
  llvm/test/Transforms/Reassociate/propagate-flags.ll


Index: llvm/lib/Transforms/Scalar/Reassociate.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -1778,6 +1778,12 @@
     return nullptr; // All distinct factors, so nothing left for us to do.
 
   IRBuilder<> Builder(I);
+  // The reassociate transformation for FP operations is performed only
+  // if unsafe algebra is permitted by FastMathFlags. Propagate those flags
+  // to the newly generated operations.
+  if (auto FPI = dyn_cast<FPMathOperator>(I))
+    Builder.setFastMathFlags(FPI->getFastMathFlags());
+
   Value *V = buildMinimalMultiplyDAG(Builder, Factors);
   if (Ops.empty())
     return V;
Index: llvm/test/Transforms/Reassociate/propagate-flags.ll
===================================================================
--- llvm/test/Transforms/Reassociate/propagate-flags.ll
+++ llvm/test/Transforms/Reassociate/propagate-flags.ll
@@ -0,0 +1,14 @@
+; RUN: opt < %s -reassociate -S | FileCheck %s
+ 
+; CHECK-LABEL: func
+; CHECK:       fmul fast double
+; CHECK-NEXT:  fmul fast double
+; CHECK-NEXT:  ret
+
+define double @func(double %a, double %b) {
+entry:
+  %mul1 = fmul fast double %a, %a
+  %mul2 = fmul fast double %b, %b
+  %mul3 = fmul fast double %mul1, %mul2
+  ret double %mul3
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26957.78836.patch
Type: text/x-patch
Size: 1331 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161122/95af6da9/attachment.bin>


More information about the llvm-commits mailing list