2008-02-26 20:26:43 +00:00
|
|
|
//===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2012-03-31 10:50:14 +00:00
|
|
|
//
|
2008-02-26 20:26:43 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-03-31 10:50:14 +00:00
|
|
|
// This file implements the Link Time Optimization library. This library is
|
2008-02-26 20:26:43 +00:00
|
|
|
// intended to be used by linker to optimize code at link time.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-09-24 23:52:22 +00:00
|
|
|
#include "llvm/LTO/LTOCodeGenerator.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2009-06-17 06:52:10 +00:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2008-02-26 20:26:43 +00:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2013-11-12 21:44:01 +00:00
|
|
|
#include "llvm/CodeGen/RuntimeLibcalls.h"
|
2012-03-31 11:15:43 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-01-15 22:04:35 +00:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2014-01-07 21:19:40 +00:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2014-01-13 09:26:24 +00:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2013-07-22 18:40:34 +00:00
|
|
|
#include "llvm/InitializePasses.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/LTO/LTOModule.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Linker.h"
|
2010-03-12 18:44:54 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2011-06-29 01:14:12 +00:00
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/PassManager.h"
|
2010-03-12 18:44:54 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2013-06-17 18:05:35 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2010-03-12 18:44:54 +00:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2010-11-29 18:47:54 +00:00
|
|
|
#include "llvm/Support/Host.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-01-15 22:04:35 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-11-29 18:47:54 +00:00
|
|
|
#include "llvm/Support/Signals.h"
|
2011-08-24 18:08:43 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2010-12-09 18:06:07 +00:00
|
|
|
#include "llvm/Support/system_error.h"
|
2013-11-12 21:44:01 +00:00
|
|
|
#include "llvm/Target/TargetLibraryInfo.h"
|
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2013-09-30 16:39:19 +00:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2012-12-04 10:44:52 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
2013-03-29 23:28:55 +00:00
|
|
|
#include "llvm/Transforms/ObjCARC.h"
|
2008-02-26 20:26:43 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2012-03-31 10:49:43 +00:00
|
|
|
const char* LTOCodeGenerator::getVersionString() {
|
2008-02-26 20:26:43 +00:00
|
|
|
#ifdef LLVM_VERSION_INFO
|
2012-03-31 10:49:43 +00:00
|
|
|
return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
|
2008-02-26 20:26:43 +00:00
|
|
|
#else
|
2012-03-31 10:49:43 +00:00
|
|
|
return PACKAGE_NAME " version " PACKAGE_VERSION;
|
2008-02-26 20:26:43 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-03-31 10:50:14 +00:00
|
|
|
LTOCodeGenerator::LTOCodeGenerator()
|
2013-09-04 17:44:24 +00:00
|
|
|
: Context(getGlobalContext()), Linker(new Module("ld-temp.o", Context)),
|
|
|
|
TargetMach(NULL), EmitDwarfDebugInfo(false), ScopeRestrictionsDone(false),
|
Reapply "LTO: add API to set strategy for -internalize"
Reapply r199191, reverted in r199197 because it carelessly broke
Other/link-opts.ll. The problem was that calling
createInternalizePass("main") would select
createInternalizePass(bool("main")) instead of
createInternalizePass(ArrayRef<const char *>("main")). This commit
fixes the bug.
The original commit message follows.
Add API to LTOCodeGenerator to specify a strategy for the -internalize
pass.
This is a new attempt at Bill's change in r185882, which he reverted in
r188029 due to problems with the gold linker. This puts the onus on the
linker to decide whether (and what) to internalize.
In particular, running internalize before outputting an object file may
change a 'weak' symbol into an internal one, even though that symbol
could be needed by an external object file --- e.g., with arclite.
This patch enables three strategies:
- LTO_INTERNALIZE_FULL: the default (and the old behaviour).
- LTO_INTERNALIZE_NONE: skip -internalize.
- LTO_INTERNALIZE_HIDDEN: only -internalize symbols with hidden
visibility.
LTO_INTERNALIZE_FULL should be used when linking an executable.
Outputting an object file (e.g., via ld -r) is more complicated, and
depends on whether hidden symbols should be internalized. E.g., for
ld -r, LTO_INTERNALIZE_NONE can be used when -keep_private_externs, and
LTO_INTERNALIZE_HIDDEN can be used otherwise. However,
LTO_INTERNALIZE_FULL is inappropriate, since the output object file will
eventually need to link with others.
lto_codegen_set_internalize_strategy() sets the strategy for subsequent
calls to lto_codegen_write_merged_modules() and lto_codegen_compile*().
<rdar://problem/14334895>
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199244 91177308-0d34-0410-b5e6-96231b3b80d8
2014-01-14 18:52:17 +00:00
|
|
|
CodeModel(LTO_CODEGEN_PIC_MODEL_DYNAMIC),
|
2014-01-15 22:04:35 +00:00
|
|
|
InternalizeStrategy(LTO_INTERNALIZE_FULL), NativeObjectFile(NULL),
|
|
|
|
DiagHandler(NULL), DiagContext(NULL) {
|
2013-07-22 18:40:34 +00:00
|
|
|
initializeLTOPasses();
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2012-03-31 10:49:43 +00:00
|
|
|
LTOCodeGenerator::~LTOCodeGenerator() {
|
2013-09-04 17:44:24 +00:00
|
|
|
delete TargetMach;
|
|
|
|
delete NativeObjectFile;
|
2013-10-16 08:59:57 +00:00
|
|
|
TargetMach = NULL;
|
|
|
|
NativeObjectFile = NULL;
|
|
|
|
|
|
|
|
Linker.deleteModule();
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
for (std::vector<char *>::iterator I = CodegenOptions.begin(),
|
|
|
|
E = CodegenOptions.end();
|
|
|
|
I != E; ++I)
|
2012-03-31 10:49:43 +00:00
|
|
|
free(*I);
|
|
|
|
}
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2013-07-22 18:40:34 +00:00
|
|
|
// Initialize LTO passes. Please keep this funciton in sync with
|
2013-07-23 06:44:34 +00:00
|
|
|
// PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
|
2014-01-10 20:24:35 +00:00
|
|
|
// passes are initialized.
|
2013-07-22 18:40:34 +00:00
|
|
|
void LTOCodeGenerator::initializeLTOPasses() {
|
|
|
|
PassRegistry &R = *PassRegistry::getPassRegistry();
|
|
|
|
|
|
|
|
initializeInternalizePassPass(R);
|
|
|
|
initializeIPSCCPPass(R);
|
|
|
|
initializeGlobalOptPass(R);
|
|
|
|
initializeConstantMergePass(R);
|
|
|
|
initializeDAHPass(R);
|
|
|
|
initializeInstCombinerPass(R);
|
|
|
|
initializeSimpleInlinerPass(R);
|
|
|
|
initializePruneEHPass(R);
|
|
|
|
initializeGlobalDCEPass(R);
|
|
|
|
initializeArgPromotionPass(R);
|
|
|
|
initializeJumpThreadingPass(R);
|
|
|
|
initializeSROAPass(R);
|
|
|
|
initializeSROA_DTPass(R);
|
|
|
|
initializeSROA_SSAUpPass(R);
|
|
|
|
initializeFunctionAttrsPass(R);
|
|
|
|
initializeGlobalsModRefPass(R);
|
|
|
|
initializeLICMPass(R);
|
|
|
|
initializeGVNPass(R);
|
|
|
|
initializeMemCpyOptPass(R);
|
|
|
|
initializeDCEPass(R);
|
2013-08-06 02:43:45 +00:00
|
|
|
initializeCFGSimplifyPassPass(R);
|
2013-07-22 18:40:34 +00:00
|
|
|
}
|
|
|
|
|
2012-03-31 11:15:43 +00:00
|
|
|
bool LTOCodeGenerator::addModule(LTOModule* mod, std::string& errMsg) {
|
2013-09-04 17:44:24 +00:00
|
|
|
bool ret = Linker.linkInModule(mod->getLLVVMModule(), &errMsg);
|
2011-03-02 04:14:42 +00:00
|
|
|
|
|
|
|
const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
|
|
|
|
for (int i = 0, e = undefs.size(); i != e; ++i)
|
2013-09-04 17:44:24 +00:00
|
|
|
AsmUndefinedRefs[undefs[i]] = 1;
|
2011-03-02 04:14:42 +00:00
|
|
|
|
2013-08-07 05:19:23 +00:00
|
|
|
return !ret;
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
2012-03-31 10:50:14 +00:00
|
|
|
|
2013-09-30 16:39:19 +00:00
|
|
|
void LTOCodeGenerator::setTargetOptions(TargetOptions options) {
|
|
|
|
Options.LessPreciseFPMADOption = options.LessPreciseFPMADOption;
|
|
|
|
Options.NoFramePointerElim = options.NoFramePointerElim;
|
|
|
|
Options.AllowFPOpFusion = options.AllowFPOpFusion;
|
|
|
|
Options.UnsafeFPMath = options.UnsafeFPMath;
|
|
|
|
Options.NoInfsFPMath = options.NoInfsFPMath;
|
|
|
|
Options.NoNaNsFPMath = options.NoNaNsFPMath;
|
|
|
|
Options.HonorSignDependentRoundingFPMathOption =
|
|
|
|
options.HonorSignDependentRoundingFPMathOption;
|
|
|
|
Options.UseSoftFloat = options.UseSoftFloat;
|
|
|
|
Options.FloatABIType = options.FloatABIType;
|
|
|
|
Options.NoZerosInBSS = options.NoZerosInBSS;
|
|
|
|
Options.GuaranteedTailCallOpt = options.GuaranteedTailCallOpt;
|
|
|
|
Options.DisableTailCalls = options.DisableTailCalls;
|
|
|
|
Options.StackAlignmentOverride = options.StackAlignmentOverride;
|
|
|
|
Options.TrapFuncName = options.TrapFuncName;
|
|
|
|
Options.PositionIndependentExecutable = options.PositionIndependentExecutable;
|
|
|
|
Options.EnableSegmentedStacks = options.EnableSegmentedStacks;
|
|
|
|
Options.UseInitArray = options.UseInitArray;
|
|
|
|
}
|
|
|
|
|
2013-08-07 05:19:23 +00:00
|
|
|
void LTOCodeGenerator::setDebugInfo(lto_debug_model debug) {
|
2012-03-31 11:15:43 +00:00
|
|
|
switch (debug) {
|
|
|
|
case LTO_DEBUG_MODEL_NONE:
|
2013-09-04 17:44:24 +00:00
|
|
|
EmitDwarfDebugInfo = false;
|
2013-08-07 05:19:23 +00:00
|
|
|
return;
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2012-03-31 11:15:43 +00:00
|
|
|
case LTO_DEBUG_MODEL_DWARF:
|
2013-09-04 17:44:24 +00:00
|
|
|
EmitDwarfDebugInfo = true;
|
2013-08-07 05:19:23 +00:00
|
|
|
return;
|
2012-03-31 11:15:43 +00:00
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown debug format!");
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-07 05:19:23 +00:00
|
|
|
void LTOCodeGenerator::setCodePICModel(lto_codegen_model model) {
|
2012-03-31 11:15:43 +00:00
|
|
|
switch (model) {
|
|
|
|
case LTO_CODEGEN_PIC_MODEL_STATIC:
|
|
|
|
case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
|
|
|
|
case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
|
2013-09-04 17:44:24 +00:00
|
|
|
CodeModel = model;
|
2013-08-07 05:19:23 +00:00
|
|
|
return;
|
2012-03-31 11:15:43 +00:00
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown PIC model!");
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
Reapply "LTO: add API to set strategy for -internalize"
Reapply r199191, reverted in r199197 because it carelessly broke
Other/link-opts.ll. The problem was that calling
createInternalizePass("main") would select
createInternalizePass(bool("main")) instead of
createInternalizePass(ArrayRef<const char *>("main")). This commit
fixes the bug.
The original commit message follows.
Add API to LTOCodeGenerator to specify a strategy for the -internalize
pass.
This is a new attempt at Bill's change in r185882, which he reverted in
r188029 due to problems with the gold linker. This puts the onus on the
linker to decide whether (and what) to internalize.
In particular, running internalize before outputting an object file may
change a 'weak' symbol into an internal one, even though that symbol
could be needed by an external object file --- e.g., with arclite.
This patch enables three strategies:
- LTO_INTERNALIZE_FULL: the default (and the old behaviour).
- LTO_INTERNALIZE_NONE: skip -internalize.
- LTO_INTERNALIZE_HIDDEN: only -internalize symbols with hidden
visibility.
LTO_INTERNALIZE_FULL should be used when linking an executable.
Outputting an object file (e.g., via ld -r) is more complicated, and
depends on whether hidden symbols should be internalized. E.g., for
ld -r, LTO_INTERNALIZE_NONE can be used when -keep_private_externs, and
LTO_INTERNALIZE_HIDDEN can be used otherwise. However,
LTO_INTERNALIZE_FULL is inappropriate, since the output object file will
eventually need to link with others.
lto_codegen_set_internalize_strategy() sets the strategy for subsequent
calls to lto_codegen_write_merged_modules() and lto_codegen_compile*().
<rdar://problem/14334895>
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199244 91177308-0d34-0410-b5e6-96231b3b80d8
2014-01-14 18:52:17 +00:00
|
|
|
void
|
|
|
|
LTOCodeGenerator::setInternalizeStrategy(lto_internalize_strategy Strategy) {
|
|
|
|
switch (Strategy) {
|
|
|
|
case LTO_INTERNALIZE_FULL:
|
|
|
|
case LTO_INTERNALIZE_NONE:
|
|
|
|
case LTO_INTERNALIZE_HIDDEN:
|
|
|
|
InternalizeStrategy = Strategy;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown internalize strategy!");
|
|
|
|
}
|
|
|
|
|
2009-08-23 07:49:08 +00:00
|
|
|
bool LTOCodeGenerator::writeMergedModules(const char *path,
|
|
|
|
std::string &errMsg) {
|
2013-08-06 21:51:21 +00:00
|
|
|
if (!determineTarget(errMsg))
|
2013-08-07 05:19:23 +00:00
|
|
|
return false;
|
2009-08-23 07:49:08 +00:00
|
|
|
|
2013-08-08 23:51:04 +00:00
|
|
|
// mark which symbols can not be internalized
|
|
|
|
applyScopeRestrictions();
|
2009-08-23 07:49:08 +00:00
|
|
|
|
|
|
|
// create output file
|
|
|
|
std::string ErrInfo;
|
2013-07-16 19:44:17 +00:00
|
|
|
tool_output_file Out(path, ErrInfo, sys::fs::F_Binary);
|
2009-08-23 07:49:08 +00:00
|
|
|
if (!ErrInfo.empty()) {
|
|
|
|
errMsg = "could not open bitcode file for writing: ";
|
|
|
|
errMsg += path;
|
2013-08-07 05:19:23 +00:00
|
|
|
return false;
|
2009-08-23 07:49:08 +00:00
|
|
|
}
|
2012-03-31 10:50:14 +00:00
|
|
|
|
2009-08-23 07:49:08 +00:00
|
|
|
// write bitcode to it
|
2013-09-04 17:44:24 +00:00
|
|
|
WriteBitcodeToFile(Linker.getModule(), Out.os());
|
2010-09-01 14:20:41 +00:00
|
|
|
Out.os().close();
|
2010-05-27 20:19:47 +00:00
|
|
|
|
2010-09-01 14:20:41 +00:00
|
|
|
if (Out.os().has_error()) {
|
2009-08-23 07:49:08 +00:00
|
|
|
errMsg = "could not write bitcode file: ";
|
|
|
|
errMsg += path;
|
2010-09-01 14:20:41 +00:00
|
|
|
Out.os().clear_error();
|
2013-08-07 05:19:23 +00:00
|
|
|
return false;
|
2009-08-23 07:49:08 +00:00
|
|
|
}
|
2012-03-31 10:50:14 +00:00
|
|
|
|
2010-08-20 16:59:15 +00:00
|
|
|
Out.keep();
|
2013-08-07 05:19:23 +00:00
|
|
|
return true;
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2014-01-10 20:24:35 +00:00
|
|
|
bool LTOCodeGenerator::compile_to_file(const char** name,
|
2013-09-30 16:39:19 +00:00
|
|
|
bool disableOpt,
|
|
|
|
bool disableInline,
|
|
|
|
bool disableGVNLoadPRE,
|
|
|
|
std::string& errMsg) {
|
2013-08-12 21:07:31 +00:00
|
|
|
// make unique temp .o file to put generated object file
|
|
|
|
SmallString<128> Filename;
|
|
|
|
int FD;
|
|
|
|
error_code EC = sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
|
|
|
|
if (EC) {
|
|
|
|
errMsg = EC.message();
|
2013-08-07 05:19:23 +00:00
|
|
|
return false;
|
2013-08-12 21:07:31 +00:00
|
|
|
}
|
2011-03-22 20:57:13 +00:00
|
|
|
|
2013-08-12 21:07:31 +00:00
|
|
|
// generate object file
|
|
|
|
tool_output_file objFile(Filename.c_str(), FD);
|
2012-03-31 11:15:43 +00:00
|
|
|
|
2013-09-30 16:39:19 +00:00
|
|
|
bool genResult = generateObjectFile(objFile.os(), disableOpt, disableInline,
|
|
|
|
disableGVNLoadPRE, errMsg);
|
2013-08-12 21:07:31 +00:00
|
|
|
objFile.os().close();
|
|
|
|
if (objFile.os().has_error()) {
|
|
|
|
objFile.os().clear_error();
|
|
|
|
sys::fs::remove(Twine(Filename));
|
2013-08-07 05:19:23 +00:00
|
|
|
return false;
|
2013-08-12 21:07:31 +00:00
|
|
|
}
|
2012-03-31 11:15:43 +00:00
|
|
|
|
2013-08-12 21:07:31 +00:00
|
|
|
objFile.keep();
|
|
|
|
if (!genResult) {
|
|
|
|
sys::fs::remove(Twine(Filename));
|
2013-08-07 05:19:23 +00:00
|
|
|
return false;
|
2013-08-12 21:07:31 +00:00
|
|
|
}
|
2011-02-24 21:04:06 +00:00
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
NativeObjectPath = Filename.c_str();
|
|
|
|
*name = NativeObjectPath.c_str();
|
2013-08-07 05:19:23 +00:00
|
|
|
return true;
|
2011-03-22 20:57:13 +00:00
|
|
|
}
|
2011-02-24 21:04:06 +00:00
|
|
|
|
2013-09-30 16:39:19 +00:00
|
|
|
const void* LTOCodeGenerator::compile(size_t* length,
|
|
|
|
bool disableOpt,
|
|
|
|
bool disableInline,
|
|
|
|
bool disableGVNLoadPRE,
|
|
|
|
std::string& errMsg) {
|
2011-03-22 20:57:13 +00:00
|
|
|
const char *name;
|
2013-09-30 16:39:19 +00:00
|
|
|
if (!compile_to_file(&name, disableOpt, disableInline, disableGVNLoadPRE,
|
|
|
|
errMsg))
|
2011-03-22 20:57:13 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// remove old buffer if compile() called twice
|
2013-09-04 17:44:24 +00:00
|
|
|
delete NativeObjectFile;
|
2011-03-22 20:57:13 +00:00
|
|
|
|
|
|
|
// read .o file into memory buffer
|
|
|
|
OwningPtr<MemoryBuffer> BuffPtr;
|
|
|
|
if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) {
|
|
|
|
errMsg = ec.message();
|
2013-09-04 17:44:24 +00:00
|
|
|
sys::fs::remove(NativeObjectPath);
|
2013-08-12 21:07:31 +00:00
|
|
|
return NULL;
|
2011-03-22 20:57:13 +00:00
|
|
|
}
|
2013-09-04 17:44:24 +00:00
|
|
|
NativeObjectFile = BuffPtr.take();
|
2008-02-27 22:25:36 +00:00
|
|
|
|
2013-08-12 21:07:31 +00:00
|
|
|
// remove temp files
|
2013-09-04 17:44:24 +00:00
|
|
|
sys::fs::remove(NativeObjectPath);
|
2013-08-12 18:29:43 +00:00
|
|
|
|
2013-08-12 21:07:31 +00:00
|
|
|
// return buffer, unless error
|
2013-09-04 17:44:24 +00:00
|
|
|
if (NativeObjectFile == NULL)
|
2013-08-12 21:07:31 +00:00
|
|
|
return NULL;
|
2013-09-04 17:44:24 +00:00
|
|
|
*length = NativeObjectFile->getBufferSize();
|
|
|
|
return NativeObjectFile->getBufferStart();
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2013-05-23 21:21:50 +00:00
|
|
|
bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
|
2013-09-04 17:44:24 +00:00
|
|
|
if (TargetMach != NULL)
|
2013-08-06 21:51:21 +00:00
|
|
|
return true;
|
2012-08-06 22:52:45 +00:00
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
std::string TripleStr = Linker.getModule()->getTargetTriple();
|
2012-10-12 17:39:25 +00:00
|
|
|
if (TripleStr.empty())
|
|
|
|
TripleStr = sys::getDefaultTargetTriple();
|
|
|
|
llvm::Triple Triple(TripleStr);
|
2012-08-06 22:52:45 +00:00
|
|
|
|
|
|
|
// create target machine from info for merged modules
|
2012-10-12 17:39:25 +00:00
|
|
|
const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
|
2012-08-08 22:03:50 +00:00
|
|
|
if (march == NULL)
|
2013-08-06 21:51:21 +00:00
|
|
|
return false;
|
2012-08-06 22:52:45 +00:00
|
|
|
|
|
|
|
// The relocation model is actually a static member of TargetMachine and
|
|
|
|
// needs to be set before the TargetMachine is instantiated.
|
|
|
|
Reloc::Model RelocModel = Reloc::Default;
|
2013-09-04 17:44:24 +00:00
|
|
|
switch (CodeModel) {
|
2012-08-06 22:52:45 +00:00
|
|
|
case LTO_CODEGEN_PIC_MODEL_STATIC:
|
|
|
|
RelocModel = Reloc::Static;
|
|
|
|
break;
|
|
|
|
case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
|
|
|
|
RelocModel = Reloc::PIC_;
|
|
|
|
break;
|
|
|
|
case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
|
|
|
|
RelocModel = Reloc::DynamicNoPIC;
|
|
|
|
break;
|
2012-03-31 11:15:43 +00:00
|
|
|
}
|
2012-08-06 22:52:45 +00:00
|
|
|
|
|
|
|
// construct LTOModule, hand over ownership of module and target
|
|
|
|
SubtargetFeatures Features;
|
2012-10-12 17:39:25 +00:00
|
|
|
Features.getDefaultSubtargetFeatures(Triple);
|
2012-08-06 22:52:45 +00:00
|
|
|
std::string FeatureStr = Features.getString();
|
2012-10-12 17:39:25 +00:00
|
|
|
// Set a default CPU for Darwin triples.
|
2013-09-04 17:44:24 +00:00
|
|
|
if (MCpu.empty() && Triple.isOSDarwin()) {
|
2012-10-12 17:39:25 +00:00
|
|
|
if (Triple.getArch() == llvm::Triple::x86_64)
|
2013-09-04 17:44:24 +00:00
|
|
|
MCpu = "core2";
|
2012-10-12 17:39:25 +00:00
|
|
|
else if (Triple.getArch() == llvm::Triple::x86)
|
2013-09-04 17:44:24 +00:00
|
|
|
MCpu = "yonah";
|
2012-10-12 17:39:25 +00:00
|
|
|
}
|
2013-09-30 16:39:19 +00:00
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
TargetMach = march->createTargetMachine(TripleStr, MCpu, FeatureStr, Options,
|
|
|
|
RelocModel, CodeModel::Default,
|
|
|
|
CodeGenOpt::Aggressive);
|
2013-08-06 21:51:21 +00:00
|
|
|
return true;
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2012-03-31 11:15:43 +00:00
|
|
|
void LTOCodeGenerator::
|
|
|
|
applyRestriction(GlobalValue &GV,
|
2013-11-12 21:44:01 +00:00
|
|
|
const ArrayRef<StringRef> &Libcalls,
|
2013-09-04 20:08:46 +00:00
|
|
|
std::vector<const char*> &MustPreserveList,
|
|
|
|
SmallPtrSet<GlobalValue*, 8> &AsmUsed,
|
|
|
|
Mangler &Mangler) {
|
2011-03-02 04:14:42 +00:00
|
|
|
SmallString<64> Buffer;
|
2014-02-18 22:24:57 +00:00
|
|
|
TargetMach->getTargetLowering()->getNameWithPrefix(Buffer, &GV, Mangler);
|
2011-03-02 04:14:42 +00:00
|
|
|
|
|
|
|
if (GV.isDeclaration())
|
|
|
|
return;
|
2013-09-04 17:44:24 +00:00
|
|
|
if (MustPreserveSymbols.count(Buffer))
|
2013-09-04 20:08:46 +00:00
|
|
|
MustPreserveList.push_back(GV.getName().data());
|
2013-09-04 17:44:24 +00:00
|
|
|
if (AsmUndefinedRefs.count(Buffer))
|
2013-09-04 20:08:46 +00:00
|
|
|
AsmUsed.insert(&GV);
|
2013-11-12 21:44:01 +00:00
|
|
|
|
|
|
|
// Conservatively append user-supplied runtime library functions to
|
|
|
|
// llvm.compiler.used. These could be internalized and deleted by
|
|
|
|
// optimizations like -globalopt, causing problems when later optimizations
|
|
|
|
// add new library calls (e.g., llvm.memset => memset and printf => puts).
|
|
|
|
// Leave it to the linker to remove any dead code (e.g. with -dead_strip).
|
|
|
|
if (isa<Function>(GV) &&
|
|
|
|
std::binary_search(Libcalls.begin(), Libcalls.end(), GV.getName()))
|
|
|
|
AsmUsed.insert(&GV);
|
2011-03-02 04:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void findUsedValues(GlobalVariable *LLVMUsed,
|
|
|
|
SmallPtrSet<GlobalValue*, 8> &UsedValues) {
|
|
|
|
if (LLVMUsed == 0) return;
|
|
|
|
|
2013-04-24 17:54:35 +00:00
|
|
|
ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
|
2011-03-02 04:14:42 +00:00
|
|
|
for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
|
2012-03-31 10:50:14 +00:00
|
|
|
if (GlobalValue *GV =
|
2012-03-31 11:15:43 +00:00
|
|
|
dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
|
2011-03-02 04:14:42 +00:00
|
|
|
UsedValues.insert(GV);
|
|
|
|
}
|
|
|
|
|
2013-11-12 21:44:01 +00:00
|
|
|
static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls,
|
|
|
|
const TargetLibraryInfo& TLI,
|
|
|
|
const TargetLowering *Lowering)
|
|
|
|
{
|
|
|
|
// TargetLibraryInfo has info on C runtime library calls on the current
|
|
|
|
// target.
|
|
|
|
for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs);
|
|
|
|
I != E; ++I) {
|
|
|
|
LibFunc::Func F = static_cast<LibFunc::Func>(I);
|
|
|
|
if (TLI.has(F))
|
|
|
|
Libcalls.push_back(TLI.getName(F));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TargetLowering has info on library calls that CodeGen expects to be
|
|
|
|
// available, both from the C runtime and compiler-rt.
|
|
|
|
if (Lowering)
|
|
|
|
for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL);
|
|
|
|
I != E; ++I)
|
|
|
|
if (const char *Name
|
|
|
|
= Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I)))
|
|
|
|
Libcalls.push_back(Name);
|
|
|
|
|
2013-11-16 16:15:56 +00:00
|
|
|
array_pod_sort(Libcalls.begin(), Libcalls.end());
|
2013-11-12 21:44:01 +00:00
|
|
|
Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()),
|
|
|
|
Libcalls.end());
|
|
|
|
}
|
|
|
|
|
2010-03-12 18:44:54 +00:00
|
|
|
void LTOCodeGenerator::applyScopeRestrictions() {
|
Reapply "LTO: add API to set strategy for -internalize"
Reapply r199191, reverted in r199197 because it carelessly broke
Other/link-opts.ll. The problem was that calling
createInternalizePass("main") would select
createInternalizePass(bool("main")) instead of
createInternalizePass(ArrayRef<const char *>("main")). This commit
fixes the bug.
The original commit message follows.
Add API to LTOCodeGenerator to specify a strategy for the -internalize
pass.
This is a new attempt at Bill's change in r185882, which he reverted in
r188029 due to problems with the gold linker. This puts the onus on the
linker to decide whether (and what) to internalize.
In particular, running internalize before outputting an object file may
change a 'weak' symbol into an internal one, even though that symbol
could be needed by an external object file --- e.g., with arclite.
This patch enables three strategies:
- LTO_INTERNALIZE_FULL: the default (and the old behaviour).
- LTO_INTERNALIZE_NONE: skip -internalize.
- LTO_INTERNALIZE_HIDDEN: only -internalize symbols with hidden
visibility.
LTO_INTERNALIZE_FULL should be used when linking an executable.
Outputting an object file (e.g., via ld -r) is more complicated, and
depends on whether hidden symbols should be internalized. E.g., for
ld -r, LTO_INTERNALIZE_NONE can be used when -keep_private_externs, and
LTO_INTERNALIZE_HIDDEN can be used otherwise. However,
LTO_INTERNALIZE_FULL is inappropriate, since the output object file will
eventually need to link with others.
lto_codegen_set_internalize_strategy() sets the strategy for subsequent
calls to lto_codegen_write_merged_modules() and lto_codegen_compile*().
<rdar://problem/14334895>
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199244 91177308-0d34-0410-b5e6-96231b3b80d8
2014-01-14 18:52:17 +00:00
|
|
|
if (ScopeRestrictionsDone || !shouldInternalize())
|
2013-09-04 17:44:24 +00:00
|
|
|
return;
|
|
|
|
Module *mergedModule = Linker.getModule();
|
2010-03-12 18:44:54 +00:00
|
|
|
|
|
|
|
// Start off with a verification pass.
|
|
|
|
PassManager passes;
|
|
|
|
passes.add(createVerifierPass());
|
|
|
|
|
2012-03-31 10:50:14 +00:00
|
|
|
// mark which symbols can not be internalized
|
2014-01-03 19:21:54 +00:00
|
|
|
Mangler Mangler(TargetMach->getDataLayout());
|
2013-09-04 20:08:46 +00:00
|
|
|
std::vector<const char*> MustPreserveList;
|
|
|
|
SmallPtrSet<GlobalValue*, 8> AsmUsed;
|
2013-11-12 21:44:01 +00:00
|
|
|
std::vector<StringRef> Libcalls;
|
|
|
|
TargetLibraryInfo TLI(Triple(TargetMach->getTargetTriple()));
|
|
|
|
accumulateAndSortLibcalls(Libcalls, TLI, TargetMach->getTargetLowering());
|
2011-03-02 04:14:42 +00:00
|
|
|
|
|
|
|
for (Module::iterator f = mergedModule->begin(),
|
|
|
|
e = mergedModule->end(); f != e; ++f)
|
2013-11-12 21:44:01 +00:00
|
|
|
applyRestriction(*f, Libcalls, MustPreserveList, AsmUsed, Mangler);
|
2012-03-31 10:50:14 +00:00
|
|
|
for (Module::global_iterator v = mergedModule->global_begin(),
|
2011-03-02 04:14:42 +00:00
|
|
|
e = mergedModule->global_end(); v != e; ++v)
|
2013-11-12 21:44:01 +00:00
|
|
|
applyRestriction(*v, Libcalls, MustPreserveList, AsmUsed, Mangler);
|
2011-03-02 04:14:42 +00:00
|
|
|
for (Module::alias_iterator a = mergedModule->alias_begin(),
|
|
|
|
e = mergedModule->alias_end(); a != e; ++a)
|
2013-11-12 21:44:01 +00:00
|
|
|
applyRestriction(*a, Libcalls, MustPreserveList, AsmUsed, Mangler);
|
2011-03-02 04:14:42 +00:00
|
|
|
|
|
|
|
GlobalVariable *LLVMCompilerUsed =
|
|
|
|
mergedModule->getGlobalVariable("llvm.compiler.used");
|
2013-09-04 20:08:46 +00:00
|
|
|
findUsedValues(LLVMCompilerUsed, AsmUsed);
|
2011-03-02 04:14:42 +00:00
|
|
|
if (LLVMCompilerUsed)
|
|
|
|
LLVMCompilerUsed->eraseFromParent();
|
|
|
|
|
2013-09-04 20:08:46 +00:00
|
|
|
if (!AsmUsed.empty()) {
|
2013-09-04 17:44:24 +00:00
|
|
|
llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context);
|
2013-04-24 17:54:35 +00:00
|
|
|
std::vector<Constant*> asmUsed2;
|
2013-09-04 20:08:46 +00:00
|
|
|
for (SmallPtrSet<GlobalValue*, 16>::const_iterator i = AsmUsed.begin(),
|
|
|
|
e = AsmUsed.end(); i !=e; ++i) {
|
2013-04-24 17:54:35 +00:00
|
|
|
GlobalValue *GV = *i;
|
|
|
|
Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
|
|
|
|
asmUsed2.push_back(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
|
|
|
|
LLVMCompilerUsed =
|
|
|
|
new llvm::GlobalVariable(*mergedModule, ATy, false,
|
|
|
|
llvm::GlobalValue::AppendingLinkage,
|
|
|
|
llvm::ConstantArray::get(ATy, asmUsed2),
|
|
|
|
"llvm.compiler.used");
|
|
|
|
|
|
|
|
LLVMCompilerUsed->setSection("llvm.metadata");
|
2010-03-12 18:44:54 +00:00
|
|
|
}
|
2011-03-02 04:14:42 +00:00
|
|
|
|
Reapply "LTO: add API to set strategy for -internalize"
Reapply r199191, reverted in r199197 because it carelessly broke
Other/link-opts.ll. The problem was that calling
createInternalizePass("main") would select
createInternalizePass(bool("main")) instead of
createInternalizePass(ArrayRef<const char *>("main")). This commit
fixes the bug.
The original commit message follows.
Add API to LTOCodeGenerator to specify a strategy for the -internalize
pass.
This is a new attempt at Bill's change in r185882, which he reverted in
r188029 due to problems with the gold linker. This puts the onus on the
linker to decide whether (and what) to internalize.
In particular, running internalize before outputting an object file may
change a 'weak' symbol into an internal one, even though that symbol
could be needed by an external object file --- e.g., with arclite.
This patch enables three strategies:
- LTO_INTERNALIZE_FULL: the default (and the old behaviour).
- LTO_INTERNALIZE_NONE: skip -internalize.
- LTO_INTERNALIZE_HIDDEN: only -internalize symbols with hidden
visibility.
LTO_INTERNALIZE_FULL should be used when linking an executable.
Outputting an object file (e.g., via ld -r) is more complicated, and
depends on whether hidden symbols should be internalized. E.g., for
ld -r, LTO_INTERNALIZE_NONE can be used when -keep_private_externs, and
LTO_INTERNALIZE_HIDDEN can be used otherwise. However,
LTO_INTERNALIZE_FULL is inappropriate, since the output object file will
eventually need to link with others.
lto_codegen_set_internalize_strategy() sets the strategy for subsequent
calls to lto_codegen_write_merged_modules() and lto_codegen_compile*().
<rdar://problem/14334895>
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199244 91177308-0d34-0410-b5e6-96231b3b80d8
2014-01-14 18:52:17 +00:00
|
|
|
passes.add(
|
|
|
|
createInternalizePass(MustPreserveList, shouldOnlyInternalizeHidden()));
|
2011-03-02 04:14:42 +00:00
|
|
|
|
2010-03-12 18:44:54 +00:00
|
|
|
// apply scope restrictions
|
|
|
|
passes.run(*mergedModule);
|
2012-03-31 10:50:14 +00:00
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
ScopeRestrictionsDone = true;
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Optimize merged modules using various IPO passes
|
2011-05-22 00:20:07 +00:00
|
|
|
bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
|
2013-09-30 16:39:19 +00:00
|
|
|
bool DisableOpt,
|
|
|
|
bool DisableInline,
|
|
|
|
bool DisableGVNLoadPRE,
|
2011-05-22 00:20:07 +00:00
|
|
|
std::string &errMsg) {
|
2013-08-06 21:51:21 +00:00
|
|
|
if (!this->determineTarget(errMsg))
|
|
|
|
return false;
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
Module *mergedModule = Linker.getModule();
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2013-05-23 21:21:50 +00:00
|
|
|
// Mark which symbols can not be internalized
|
2012-04-09 22:18:01 +00:00
|
|
|
this->applyScopeRestrictions();
|
|
|
|
|
2012-03-31 11:15:43 +00:00
|
|
|
// Instantiate the pass manager to organize the passes.
|
|
|
|
PassManager passes;
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2012-03-31 11:15:43 +00:00
|
|
|
// Start off with a verification pass.
|
|
|
|
passes.add(createVerifierPass());
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2012-10-08 16:39:34 +00:00
|
|
|
// Add an appropriate DataLayout instance for this module...
|
2013-09-04 17:44:24 +00:00
|
|
|
passes.add(new DataLayout(*TargetMach->getDataLayout()));
|
2013-12-12 01:37:39 +00:00
|
|
|
|
2014-01-10 20:24:35 +00:00
|
|
|
// Add appropriate TargetLibraryInfo for this module.
|
2013-12-12 01:37:39 +00:00
|
|
|
passes.add(new TargetLibraryInfo(Triple(TargetMach->getTargetTriple())));
|
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
TargetMach->addAnalysisPasses(passes);
|
2012-03-31 10:50:14 +00:00
|
|
|
|
2012-04-16 10:58:38 +00:00
|
|
|
// Enabling internalize here would use its AllButMain variant. It
|
|
|
|
// keeps only main if it exists and does nothing for libraries. Instead
|
|
|
|
// we create the pass ourselves with the symbol list provided by the linker.
|
2013-05-23 21:21:50 +00:00
|
|
|
if (!DisableOpt)
|
2013-02-28 14:11:10 +00:00
|
|
|
PassManagerBuilder().populateLTOPassManager(passes,
|
2012-12-10 21:33:45 +00:00
|
|
|
/*Internalize=*/false,
|
2012-04-02 22:16:50 +00:00
|
|
|
!DisableInline,
|
|
|
|
DisableGVNLoadPRE);
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2012-03-31 11:15:43 +00:00
|
|
|
// Make sure everything is still good.
|
|
|
|
passes.add(createVerifierPass());
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2013-03-13 21:18:46 +00:00
|
|
|
PassManager codeGenPasses;
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
codeGenPasses.add(new DataLayout(*TargetMach->getDataLayout()));
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2012-03-31 11:15:43 +00:00
|
|
|
formatted_raw_ostream Out(out);
|
2010-09-01 14:20:41 +00:00
|
|
|
|
2013-03-29 23:28:55 +00:00
|
|
|
// If the bitcode files contain ARC code and were compiled with optimization,
|
|
|
|
// the ObjCARCContractPass must be run, so do it unconditionally here.
|
|
|
|
codeGenPasses.add(createObjCARCContractPass());
|
|
|
|
|
2013-09-04 17:44:24 +00:00
|
|
|
if (TargetMach->addPassesToEmitFile(codeGenPasses, Out,
|
|
|
|
TargetMachine::CGFT_ObjectFile)) {
|
2012-03-31 11:15:43 +00:00
|
|
|
errMsg = "target file type not supported";
|
2013-08-06 21:51:21 +00:00
|
|
|
return false;
|
2012-03-31 11:15:43 +00:00
|
|
|
}
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2012-03-31 11:15:43 +00:00
|
|
|
// Run our queue of passes all at once now, efficiently.
|
|
|
|
passes.run(*mergedModule);
|
2008-02-26 20:26:43 +00:00
|
|
|
|
2012-03-31 11:15:43 +00:00
|
|
|
// Run the code generator, and write assembly file
|
2013-03-13 21:18:46 +00:00
|
|
|
codeGenPasses.run(*mergedModule);
|
2009-07-26 22:16:39 +00:00
|
|
|
|
2013-08-06 21:51:21 +00:00
|
|
|
return true;
|
2008-02-26 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2012-03-31 10:49:43 +00:00
|
|
|
/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
|
|
|
|
/// LTO problems.
|
|
|
|
void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
|
|
|
|
for (std::pair<StringRef, StringRef> o = getToken(options);
|
|
|
|
!o.first.empty(); o = getToken(o.second)) {
|
|
|
|
// ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
|
|
|
|
// that.
|
2013-09-04 17:44:24 +00:00
|
|
|
if (CodegenOptions.empty())
|
2013-09-24 23:52:22 +00:00
|
|
|
CodegenOptions.push_back(strdup("libLLVMLTO"));
|
2013-09-04 17:44:24 +00:00
|
|
|
CodegenOptions.push_back(strdup(o.first.str().c_str()));
|
2012-03-31 10:49:43 +00:00
|
|
|
}
|
2008-07-08 21:14:10 +00:00
|
|
|
}
|
2013-10-02 14:36:23 +00:00
|
|
|
|
|
|
|
void LTOCodeGenerator::parseCodeGenDebugOptions() {
|
|
|
|
// if options were requested, set them
|
|
|
|
if (!CodegenOptions.empty())
|
|
|
|
cl::ParseCommandLineOptions(CodegenOptions.size(),
|
|
|
|
const_cast<char **>(&CodegenOptions[0]));
|
|
|
|
}
|
2014-01-15 22:04:35 +00:00
|
|
|
|
|
|
|
void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI,
|
|
|
|
void *Context) {
|
|
|
|
((LTOCodeGenerator *)Context)->DiagnosticHandler2(DI);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LTOCodeGenerator::DiagnosticHandler2(const DiagnosticInfo &DI) {
|
|
|
|
// Map the LLVM internal diagnostic severity to the LTO diagnostic severity.
|
|
|
|
lto_codegen_diagnostic_severity_t Severity;
|
|
|
|
switch (DI.getSeverity()) {
|
|
|
|
case DS_Error:
|
|
|
|
Severity = LTO_DS_ERROR;
|
|
|
|
break;
|
|
|
|
case DS_Warning:
|
|
|
|
Severity = LTO_DS_WARNING;
|
|
|
|
break;
|
|
|
|
case DS_Note:
|
|
|
|
Severity = LTO_DS_NOTE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Create the string that will be reported to the external diagnostic handler.
|
|
|
|
std::string MsgStorage;
|
|
|
|
raw_string_ostream Stream(MsgStorage);
|
|
|
|
DiagnosticPrinterRawOStream DP(Stream);
|
|
|
|
DI.print(DP);
|
|
|
|
Stream.flush();
|
|
|
|
|
|
|
|
// If this method has been called it means someone has set up an external
|
|
|
|
// diagnostic handler. Assert on that.
|
|
|
|
assert(DiagHandler && "Invalid diagnostic handler");
|
|
|
|
(*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,
|
|
|
|
void *Ctxt) {
|
|
|
|
this->DiagHandler = DiagHandler;
|
|
|
|
this->DiagContext = Ctxt;
|
|
|
|
if (!DiagHandler)
|
|
|
|
return Context.setDiagnosticHandler(NULL, NULL);
|
|
|
|
// Register the LTOCodeGenerator stub in the LLVMContext to forward the
|
|
|
|
// diagnostic to the external DiagHandler.
|
|
|
|
Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this);
|
|
|
|
}
|