Add -fsave-optimization-record option.

Resolves issue #2089
This commit is contained in:
Johan Engelen 2017-05-03 00:11:59 +02:00
parent d61452c6a8
commit 3fd99eb207
5 changed files with 93 additions and 0 deletions

View file

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

View file

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

View file

@ -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;
}

View file

@ -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) {

View file

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