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

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