[pkg-d-commits] [ldc] 62/211: Apply TargetMachine options as function attributes in IR. Needed for LTO.

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


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

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

commit 00dfb4d138b4403406225bcf40e29c0101704450
Author: Johan Engelen <jbc.engelen at gmail.com>
Date:   Wed Oct 5 12:54:43 2016 +0200

    Apply TargetMachine options as function attributes in IR. Needed for LTO.
---
 gen/functions.cpp                  | 61 +++++++++++++++++++++++++++++++-------
 tests/codegen/attr_targetoptions.d | 27 +++++++++++++++++
 tests/codegen/inlineIR_math.d      |  2 +-
 3 files changed, 78 insertions(+), 12 deletions(-)

diff --git a/gen/functions.cpp b/gen/functions.cpp
index 7d42df5..4a1f9dc 100644
--- a/gen/functions.cpp
+++ b/gen/functions.cpp
@@ -18,6 +18,7 @@
 #include "mtype.h"
 #include "statement.h"
 #include "template.h"
+#include "driver/cl_options.h"
 #include "gen/abi.h"
 #include "gen/arrays.h"
 #include "gen/classes.h"
@@ -42,6 +43,8 @@
 #include "ir/irmodule.h"
 #include "llvm/IR/Intrinsics.h"
 #include "llvm/IR/CFG.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Target/TargetOptions.h"
 #include <iostream>
 
 llvm::FunctionType *DtoFunctionType(Type *type, IrFuncTy &irFty, Type *thistype,
@@ -419,19 +422,55 @@ void applyParamAttrsToLLFunc(TypeFunction *f, IrFuncTy &irFty,
   func->setAttributes(newAttrs);
 }
 
-void applyDefaultMathAttributes(IrFunction *irFunc) {
+/// Applies TargetMachine options as function attributes in the IR (options for
+/// which attributes exist).
+/// This is e.g. needed for LTO: it tells the linker/LTO-codegen what settings
+/// to use.
+/// It is also needed because "unsafe-fp-math" is not properly reset in LLVM
+/// between function definitions, i.e. if a function does not define a value for
+/// "unsafe-fp-math" it will be compiled using the value of the previous
+/// function. Therefore, each function must explicitly define the value (clang
+/// does the same). See https://llvm.org/bugs/show_bug.cgi?id=23172
+void applyTargetMachineAttributes(llvm::Function &func,
+                                  const llvm::TargetMachine &target) {
+  const llvm::TargetOptions &TO = target.Options;
+
   // TODO: implement commandline switches to change the default values.
 
-  // "unsafe-fp-math" is not properly reset in LLVM between function
-  // definitions, i.e. if a function does not define a value for
-  // "unsafe-fp-math" it will be compiled using the value of the previous
-  // function. Therefore, each function must explicitly define the value (clang
-  // does the same).
-  // See https://llvm.org/bugs/show_bug.cgi?id=23172
-  irFunc->func->addFnAttr("unsafe-fp-math", "false");
-}
+  // Target CPU capabilities
+  func.addFnAttr("target-cpu", target.getTargetCPU());
+  auto featStr = target.getTargetFeatureString();
+  if (!featStr.empty())
+    func.addFnAttr("target-features", featStr);
+
+  // Floating point settings
+  func.addFnAttr("unsafe-fp-math", TO.UnsafeFPMath ? "true" : "false");
+  func.addFnAttr("less-precise-fpmad",
+                 TO.LessPreciseFPMADOption ? "true" : "false");
+  func.addFnAttr("no-infs-fp-math", TO.NoInfsFPMath ? "true" : "false");
+  func.addFnAttr("no-nans-fp-math", TO.NoNaNsFPMath ? "true" : "false");
+#if LDC_LLVM_VER < 307
+  func.addFnAttr("use-soft-float", TO.UseSoftFloat ? "true" : "false");
+#else
+  switch (TO.FloatABIType) {
+  case llvm::FloatABI::Default:
+    break;
+  case llvm::FloatABI::Soft:
+    func.addFnAttr("use-soft-float", "true");
+    break;
+  case llvm::FloatABI::Hard:
+    func.addFnAttr("use-soft-float", "false");
+    break;
+  }
+#endif
+
+  // Frame pointer elimination
+  func.addFnAttr("no-frame-pointer-elim",
+                 opts::disableFpElim ? "true" : "false");
 }
 
+} // anonymous namespace
+
 ////////////////////////////////////////////////////////////////////////////////
 
 void DtoDeclareFunction(FuncDeclaration *fdecl) {
@@ -523,9 +562,9 @@ void DtoDeclareFunction(FuncDeclaration *fdecl) {
     }
   }
 
-  // Set default math function attributes here, such that they can be overridden
+  // First apply the TargetMachine attributes, such that they can be overridden
   // by UDAs.
-  applyDefaultMathAttributes(irFunc);
+  applyTargetMachineAttributes(*func, *gTargetMachine);
   applyFuncDeclUDAs(fdecl, irFunc);
 
   // main
diff --git a/tests/codegen/attr_targetoptions.d b/tests/codegen/attr_targetoptions.d
new file mode 100644
index 0000000..8368ea3
--- /dev/null
+++ b/tests/codegen/attr_targetoptions.d
@@ -0,0 +1,27 @@
+// Tests that our TargetMachine options are added as function attributes
+
+// RUN: %ldc -c -output-ll -of=%t.ll %s && FileCheck %s --check-prefix=DEFAULT < %t.ll
+// RUN: %ldc -c -output-ll -of=%t.ll %s -disable-fp-elim && FileCheck %s --check-prefix=FRAMEPTR < %t.ll
+// RUN: %ldc -c -output-ll -of=%t.ll %s -mattr=test && FileCheck %s --check-prefix=ATTR < %t.ll
+
+// DEFAULT: define{{.*}} @{{.*}}3fooFZv{{.*}} #[[KEYVALUE:[0-9]+]]
+// FRAMEPTR: define{{.*}} @{{.*}}3fooFZv{{.*}} #[[KEYVALUE:[0-9]+]]
+// ATTR: define{{.*}} @{{.*}}3fooFZv{{.*}} #[[KEYVALUE:[0-9]+]]
+void foo()
+{
+}
+
+// DEFAULT: attributes #[[KEYVALUE]]
+// DEFAULT-DAG: "target-cpu"=
+// DEFAULT-DAG: "use-soft-float"="{{(true|false)}}"
+// DEFAULT-DAG: "no-frame-pointer-elim"="false"
+// DEFAULT-DAG: "unsafe-fp-math"="false"
+// DEFAULT-DAG: "less-precise-fpmad"="false"
+// DEFAULT-DAG: "no-infs-fp-math"="false"
+// DEFAULT-DAG: "no-nans-fp-math"="false"
+
+// FRAMEPTR: attributes #[[KEYVALUE]]
+// FRAMEPTR-DAG: "no-frame-pointer-elim"="true"
+
+// ATTR: attributes #[[KEYVALUE]]
+// ATTR-DAG: "target-features"="{{.*}}+test{{.*}}"
diff --git a/tests/codegen/inlineIR_math.d b/tests/codegen/inlineIR_math.d
index 7475d5b..837fa34 100644
--- a/tests/codegen/inlineIR_math.d
+++ b/tests/codegen/inlineIR_math.d
@@ -113,4 +113,4 @@ double neverInlinedEnclosingFunction()
 // LLVM-DAG: attributes #[[UNSAFEFPMATH]] ={{.*}} "unsafe-fp-math"="true"
 // LLVM-DAG: attributes #[[UNSAFEFPMATH2]] ={{.*}} "unsafe-fp-math"="true"
 // LLVM-DAG: attributes #[[UNSAFEFPMATH3]] ={{.*}} "unsafe-fp-math"="false"
-// LLVM-DAG: attributes #[[FEAT]] ={{.*}} "target-features"="+fma"
+// LLVM-DAG: attributes #[[FEAT]] ={{.*}} "target-features"="{{.*}}+fma{{.*}}"

-- 
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