[PATCH] D31168: Set FMF for -ffp-contract=fast

Adam Nemet via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 30 09:03:31 PDT 2017


anemet updated this revision to Diff 93487.
anemet added a comment.

Also add 'contract' for CompoundAssignOperator (+= and -=).  For l-values,
these don't go through the expr-visitor in ScalarExprEmitter::Visit.  This
again piggybacks on how debug-locations are added.


https://reviews.llvm.org/D31168

Files:
  lib/CodeGen/CGExpr.cpp
  lib/CodeGen/CGExprScalar.cpp
  lib/CodeGen/CodeGenFunction.h
  test/CodeGen/ffp-contract-fast-option.cpp


Index: test/CodeGen/ffp-contract-fast-option.cpp
===================================================================
--- /dev/null
+++ test/CodeGen/ffp-contract-fast-option.cpp
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -O3 -ffp-contract=fast -triple %itanium_abi_triple -emit-llvm -o - %s | FileCheck %s
+
+float fp_contract_1(float a, float b, float c) {
+  // CHECK-LABEL: fp_contract_1fff(
+  // CHECK: fmul contract float
+  // CHECK: fadd contract float
+  return a * b + c;
+}
+
+float fp_contract_2(float a, float b, float c) {
+  // CHECK-LABEL: fp_contract_2fff(
+  // CHECK: fmul contract float
+  // CHECK: fsub contract float
+  return a * b - c;
+}
+
+void fp_contract_3(float *a, float b, float c) {
+  // CHECK-LABEL: fp_contract_3Pfff(
+  // CHECK: fmul contract float
+  // CHECK: fadd contract float
+  a[0] += b * c;
+}
+
+void fp_contract_4(float *a, float b, float c) {
+  // CHECK-LABEL: fp_contract_4Pfff(
+  // CHECK: fmul contract float
+  // CHECK: fsub contract float
+  a[0] -= b * c;
+}
Index: lib/CodeGen/CodeGenFunction.h
===================================================================
--- lib/CodeGen/CodeGenFunction.h
+++ lib/CodeGen/CodeGenFunction.h
@@ -3822,6 +3822,25 @@
   }
 };
 
+/// A RAII to apply the FP features from the expression to the IRBuilder.
+struct ApplyFPFeatures : public CGBuilderTy::FastMathFlagGuard {
+  static Optional<FPOptions> getFPFeatures(const Expr *E) {
+    if (const auto *BO = dyn_cast<BinaryOperator>(E))
+      return BO->getFPFeatures();
+    else if (const auto *CE = dyn_cast<CXXOperatorCallExpr>(E))
+      return CE->getFPFeatures();
+    return None;
+  }
+
+  ApplyFPFeatures(llvm::IRBuilderBase &Builder, const Expr *E)
+      : CGBuilderTy::FastMathFlagGuard(Builder) {
+    if (Optional<FPOptions> FPFeatures = getFPFeatures(E)) {
+      llvm::FastMathFlags FMF = Builder.getFastMathFlags();
+      FMF.setAllowContract(FPFeatures->allowFPContractAcrossStatement());
+      Builder.setFastMathFlags(FMF);
+    }
+  }
+};
 }  // end namespace CodeGen
 }  // end namespace clang
 
Index: lib/CodeGen/CGExprScalar.cpp
===================================================================
--- lib/CodeGen/CGExprScalar.cpp
+++ lib/CodeGen/CGExprScalar.cpp
@@ -258,6 +258,7 @@
 
   Value *Visit(Expr *E) {
     ApplyDebugLocation DL(CGF, E);
+    ApplyFPFeatures FPF(Builder, E);
     return StmtVisitor<ScalarExprEmitter, Value*>::Visit(E);
   }
 
Index: lib/CodeGen/CGExpr.cpp
===================================================================
--- lib/CodeGen/CGExpr.cpp
+++ lib/CodeGen/CGExpr.cpp
@@ -1013,6 +1013,7 @@
 ///
 LValue CodeGenFunction::EmitLValue(const Expr *E) {
   ApplyDebugLocation DL(*this, E);
+  ApplyFPFeatures FPF(Builder, E);
   switch (E->getStmtClass()) {
   default: return EmitUnsupportedLValue(E, "l-value expression");
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31168.93487.patch
Type: text/x-patch
Size: 2827 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170330/b419fa8e/attachment.bin>


More information about the cfe-commits mailing list