[pkg-d-commits] [ldc] 05/149: Implement -ffast-math.

Matthias Klumpp mak at moszumanska.debian.org
Sun Apr 23 22:36:52 UTC 2017


This is an automated email from the git hooks/post-receive script.

mak pushed a commit to annotated tag v1.2.0
in repository ldc.

commit cd3ae57d145054b74375371ed7a9c0e90ab938b5
Author: Johan Engelen <jbc.engelen at gmail.com>
Date:   Fri Nov 25 19:45:05 2016 +0100

    Implement -ffast-math.
    
    Resolves #1874.
---
 driver/cl_options.cpp     | 17 +++++++++++++++++
 driver/cl_options.h       | 10 ++++++++++
 driver/main.cpp           |  2 ++
 ir/irfunction.cpp         |  4 +++-
 tests/codegen/ffastmath.d | 15 +++++++++++++++
 5 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/driver/cl_options.cpp b/driver/cl_options.cpp
index 6fcbfb4..9df4beb 100644
--- a/driver/cl_options.cpp
+++ b/driver/cl_options.cpp
@@ -14,6 +14,7 @@
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/IR/DataLayout.h"
+#include "llvm/IR/Operator.h"
 
 namespace opts {
 
@@ -410,6 +411,18 @@ cl::opt<bool> disableLinkerStripDead(
     cl::desc("Do not try to remove unused symbols during linking"),
     cl::init(false));
 
+// Math options
+bool fFastMath; // Storage for the dynamically created ffast-math option.
+llvm::FastMathFlags defaultFMF;
+void setDefaultMathOptions(llvm::TargetMachine &target) {
+  if (fFastMath) {
+    defaultFMF.setUnsafeAlgebra();
+
+    llvm::TargetOptions &TO = target.Options;
+    TO.UnsafeFPMath = true;
+  }
+}
+
 cl::opt<bool, true>
     allinst("allinst",
             cl::desc("generate code for all template instantiations"),
@@ -503,11 +516,15 @@ void createClashingOptions() {
   // LLVM 3.7 introduces compiling as shared library. The result
   // is a clash in the command line options.
   renameAndHide("color", "llvm-color");
+  renameAndHide("ffast-math", "llvm-ffast-math");
 
   // Step 2. Add the LDC options.
   new cl::opt<bool, true, FlagParser<bool>>(
       "color", cl::desc("Force colored console output"),
       cl::location(global.params.color));
+  new cl::opt<bool, true>(
+      "ffast-math", cl::desc("Set @fastmath for all functions."),
+      cl::location(fFastMath), cl::init(false), cl::ZeroOrMore);
 }
 
 /// Hides command line options exposed from within LLVM that are unlikely
diff --git a/driver/cl_options.h b/driver/cl_options.h
index ae80cd8..df48880 100644
--- a/driver/cl_options.h
+++ b/driver/cl_options.h
@@ -26,6 +26,11 @@
 // FIXME: Just for the BOUDNSCHECK enum; this is not pretty
 #include "globals.h"
 
+namespace llvm {
+class FastMathFlags;
+class TargetMachine;
+}
+
 namespace opts {
 namespace cl = llvm::cl;
 
@@ -78,6 +83,11 @@ extern cl::opt<bool, true> singleObj;
 extern cl::opt<bool> linkonceTemplates;
 extern cl::opt<bool> disableLinkerStripDead;
 
+// Math options
+extern bool fFastMath;
+extern llvm::FastMathFlags defaultFMF;
+void setDefaultMathOptions(llvm::TargetMachine &target);
+
 extern cl::opt<BOUNDSCHECK> boundsCheck;
 extern bool nonSafeBoundsChecks;
 
diff --git a/driver/main.cpp b/driver/main.cpp
index b31a3b8..cd017df 100644
--- a/driver/main.cpp
+++ b/driver/main.cpp
@@ -981,6 +981,8 @@ int cppmain(int argc, char **argv) {
       global.obj_ext = "obj";
   }
 
+  opts::setDefaultMathOptions(*gTargetMachine);
+
   // allocate the target abi
   gABI = TargetABI::getTarget();
 
diff --git a/ir/irfunction.cpp b/ir/irfunction.cpp
index eb6ef06..b148e27 100644
--- a/ir/irfunction.cpp
+++ b/ir/irfunction.cpp
@@ -8,13 +8,15 @@
 //===----------------------------------------------------------------------===//
 
 #include "ir/irfunction.h"
+
+#include "driver/cl_options.h"
 #include "gen/llvm.h"
 #include "gen/llvmhelpers.h"
 #include "gen/irstate.h"
 #include "gen/tollvm.h"
 #include "ir/irdsymbol.h"
 
-IrFunction::IrFunction(FuncDeclaration *fd) : FMF() {
+IrFunction::IrFunction(FuncDeclaration *fd) : FMF(opts::defaultFMF) {
   decl = fd;
 
   Type *t = fd->type->toBasetype();
diff --git a/tests/codegen/ffastmath.d b/tests/codegen/ffastmath.d
new file mode 100644
index 0000000..d433ea8
--- /dev/null
+++ b/tests/codegen/ffastmath.d
@@ -0,0 +1,15 @@
+// Test -ffast-math
+
+// RUN: %ldc -ffast-math -O0 -release -c -output-ll -of=%t.ll %s && FileCheck %s < %t.ll
+
+import ldc.attributes;
+
+// CHECK-LABEL: define{{.*}} @foo
+// CHECK-SAME: #[[ATTR:[0-9]+]]
+extern (C) double foo(double a, double b)
+{
+    // CHECK: fmul fast
+    return a * b;
+}
+
+// CHECK-DAG: attributes #[[ATTR]] ={{.*}} "unsafe-fp-math"="true"

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-d/ldc.git



More information about the pkg-d-commits mailing list