[pkg-d-commits] [ldc] 09/74: Add -fsave-optimization-record option.

Matthias Klumpp mak at moszumanska.debian.org
Thu Jul 13 20:54:11 UTC 2017


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

mak pushed a commit to annotated tag v1.3.0-beta2
in repository ldc.

commit 3fd99eb20753a17360f01249f8b9abc57a02cb21
Author: Johan Engelen <jbc.engelen at gmail.com>
Date:   Wed May 3 00:11:59 2017 +0200

    Add -fsave-optimization-record option.
    
    Resolves issue #2089
---
 driver/cl_options.cpp                   |  9 ++++++
 driver/cl_options.h                     |  4 +++
 driver/codegenerator.cpp                | 53 +++++++++++++++++++++++++++++++++
 driver/main.cpp                         |  6 ++++
 tests/driver/save_optimization_record.d | 21 +++++++++++++
 5 files changed, 93 insertions(+)

diff --git a/driver/cl_options.cpp b/driver/cl_options.cpp
index 9cac488..9f07305 100644
--- a/driver/cl_options.cpp
+++ b/driver/cl_options.cpp
@@ -510,6 +510,15 @@ cl::opt<LTOKind> ltoMode(
                    "Parallel importing and codegen (faster than 'full')")));
 #endif
 
+#if LDC_LLVM_VER >= 400
+cl::opt<std::string>
+    saveOptimizationRecord("fsave-optimization-record",
+                           cl::value_desc("filename"),
+                           cl::desc("Generate a YAML optimization record file "
+                                    "of optimizations performed by LLVM"),
+                           cl::ValueOptional);
+#endif
+
 static cl::extrahelp footer(
     "\n"
     "-d-debug can also be specified without options, in which case it enables "
diff --git a/driver/cl_options.h b/driver/cl_options.h
index aa76b0b..2c7826c 100644
--- a/driver/cl_options.h
+++ b/driver/cl_options.h
@@ -118,5 +118,9 @@ inline bool isUsingThinLTO() { return ltoMode == LTO_Thin; }
 inline bool isUsingLTO() { return false; }
 inline bool isUsingThinLTO() { return false; }
 #endif
+
+#if LDC_LLVM_VER >= 400
+extern cl::opt<std::string> saveOptimizationRecord;
+#endif
 }
 #endif
diff --git a/driver/codegenerator.cpp b/driver/codegenerator.cpp
index 5726ed3..d3e3a26 100644
--- a/driver/codegenerator.cpp
+++ b/driver/codegenerator.cpp
@@ -13,11 +13,16 @@
 #include "mars.h"
 #include "module.h"
 #include "scope.h"
+#include "driver/cl_options.h"
 #include "driver/linker.h"
 #include "driver/toobj.h"
 #include "gen/logger.h"
 #include "gen/modules.h"
 #include "gen/runtime.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Support/YAMLTraits.h"
 
 /// The module with the frontend-generated C main() definition.
 extern Module *g_entrypointModule;
@@ -27,6 +32,47 @@ extern Module *g_dMainModule;
 
 namespace {
 
+std::unique_ptr<llvm::tool_output_file>
+createAndSetDiagnosticsOutputFile(IRState &irs, llvm::LLVMContext &ctx,
+                                  llvm::StringRef filename) {
+  std::unique_ptr<llvm::tool_output_file> diagnosticsOutputFile;
+
+#if LDC_LLVM_VER >= 400
+  // Set LLVM Diagnostics outputfile if requested
+  if (opts::saveOptimizationRecord.getNumOccurrences() > 0) {
+    llvm::SmallString<128> diagnosticsFilename;
+    if (!opts::saveOptimizationRecord.empty()) {
+      diagnosticsFilename = opts::saveOptimizationRecord.getValue();
+    } else {
+      diagnosticsFilename = filename;
+      llvm::sys::path::replace_extension(diagnosticsFilename, "opt.yaml");
+    }
+
+    std::error_code EC;
+    diagnosticsOutputFile = llvm::make_unique<llvm::tool_output_file>(
+        diagnosticsFilename, EC, llvm::sys::fs::F_None);
+    if (EC) {
+      irs.dmodule->error("Could not create file %s: %s",
+                         diagnosticsFilename.c_str(), EC.message().c_str());
+      fatal();
+    }
+
+    ctx.setDiagnosticsOutputFile(
+        llvm::make_unique<llvm::yaml::Output>(diagnosticsOutputFile->os()));
+
+    // If there is instrumentation data available, also output function hotness
+    if (!global.params.genInstrProf && global.params.datafileInstrProf)
+      ctx.setDiagnosticHotnessRequested(true);
+  }
+#endif
+
+  return diagnosticsOutputFile;
+}
+
+} // anonymous namespace
+
+namespace {
+
 /// Add the linker options metadata flag.
 /// If the flag is already present, merge it with the new data.
 void emitLinkerOptions(IRState &irs, llvm::Module &M, llvm::LLVMContext &ctx) {
@@ -188,7 +234,14 @@ void CodeGenerator::writeAndFreeLLModule(const char *filename) {
       {llvm::MDString::get(ir_->context(), Version)};
   IdentMetadata->addOperand(llvm::MDNode::get(ir_->context(), IdentNode));
 
+  std::unique_ptr<llvm::tool_output_file> diagnosticsOutputFile =
+      createAndSetDiagnosticsOutputFile(*ir_, context_, filename);
+
   writeModule(&ir_->module, filename);
+
+  if (diagnosticsOutputFile)
+    diagnosticsOutputFile->keep();
+
   delete ir_;
   ir_ = nullptr;
 }
diff --git a/driver/main.cpp b/driver/main.cpp
index 0efa58f..562dd51 100644
--- a/driver/main.cpp
+++ b/driver/main.cpp
@@ -413,6 +413,12 @@ void parseCommandLine(int argc, char **argv, Strings &sourceFiles,
   toWinPaths(global.params.fileImppath);
 #endif
 
+#if LDC_LLVM_VER >= 400
+  if (saveOptimizationRecord.getNumOccurrences() > 0) {
+    global.params.outputSourceLocations = true;
+  }
+#endif
+
 // PGO options
 #if LDC_WITH_PGO
   if (genfileInstrProf.getNumOccurrences() > 0) {
diff --git a/tests/driver/save_optimization_record.d b/tests/driver/save_optimization_record.d
new file mode 100644
index 0000000..2fadd36
--- /dev/null
+++ b/tests/driver/save_optimization_record.d
@@ -0,0 +1,21 @@
+// REQUIRES: atleast_llvm400
+
+// Automatic output filename generation from LL output file
+// RUN: %ldc -c -betterC -O3 -g -fsave-optimization-record -output-ll -of=%t.1.ll %s \
+// RUN: && FileCheck %s --check-prefix=LLVM < %t.1.ll \
+// RUN: && FileCheck %s --check-prefix=YAML < %t.1.opt.yaml
+
+// Explicit filename specified
+// RUN: %ldc -c -betterC -O3 -g -fsave-optimization-record=%t.abcdefg -output-ll -of=%t.ll %s \
+// RUN: && FileCheck %s --check-prefix=LLVM < %t.ll \
+// RUN: && FileCheck %s --check-prefix=YAML < %t.abcdefg
+
+int alwaysInlined(int a) { return a; }
+int foo()
+{
+    // LLVM: 8329424
+    // YAML: File: save_optimization_record.d, Line: [[@LINE+1]]
+    return 8329423 + alwaysInlined(1);
+}
+
+// LLVM: !DILocation(line

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