summaryrefslogtreecommitdiffstats
path: root/contrib/llvm/lib/LTO
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/llvm/lib/LTO')
-rw-r--r--contrib/llvm/lib/LTO/LTO.cpp119
-rw-r--r--contrib/llvm/lib/LTO/LTOCodeGenerator.cpp359
-rw-r--r--contrib/llvm/lib/LTO/LTOModule.cpp98
-rw-r--r--contrib/llvm/lib/LTO/ThinLTOCodeGenerator.cpp835
-rw-r--r--contrib/llvm/lib/LTO/UpdateCompilerUsed.cpp146
5 files changed, 1369 insertions, 188 deletions
diff --git a/contrib/llvm/lib/LTO/LTO.cpp b/contrib/llvm/lib/LTO/LTO.cpp
new file mode 100644
index 0000000..10226c4
--- /dev/null
+++ b/contrib/llvm/lib/LTO/LTO.cpp
@@ -0,0 +1,119 @@
+//===-LTO.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.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements functions and classes used to support LTO.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/LTO/LTO.h"
+#include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace llvm {
+
+// Simple helper to load a module from bitcode
+std::unique_ptr<Module> loadModuleFromBuffer(const MemoryBufferRef &Buffer,
+ LLVMContext &Context, bool Lazy) {
+ SMDiagnostic Err;
+ ErrorOr<std::unique_ptr<Module>> ModuleOrErr(nullptr);
+ if (Lazy) {
+ ModuleOrErr =
+ getLazyBitcodeModule(MemoryBuffer::getMemBuffer(Buffer, false), Context,
+ /* ShouldLazyLoadMetadata */ Lazy);
+ } else {
+ ModuleOrErr = parseBitcodeFile(Buffer, Context);
+ }
+ if (std::error_code EC = ModuleOrErr.getError()) {
+ Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
+ EC.message());
+ Err.print("ThinLTO", errs());
+ report_fatal_error("Can't load module, abort.");
+ }
+ return std::move(ModuleOrErr.get());
+}
+
+static void thinLTOResolveWeakForLinkerGUID(
+ GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
+ DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
+ function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
+ isPrevailing,
+ function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
+ recordNewLinkage) {
+ for (auto &S : GVSummaryList) {
+ if (GlobalInvolvedWithAlias.count(S.get()))
+ continue;
+ GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
+ if (!GlobalValue::isWeakForLinker(OriginalLinkage))
+ continue;
+ // We need to emit only one of these. The prevailing module will keep it,
+ // but turned into a weak, while the others will drop it when possible.
+ if (isPrevailing(GUID, S.get())) {
+ if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
+ S->setLinkage(GlobalValue::getWeakLinkage(
+ GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
+ }
+ // Alias can't be turned into available_externally.
+ else if (!isa<AliasSummary>(S.get()) &&
+ (GlobalValue::isLinkOnceODRLinkage(OriginalLinkage) ||
+ GlobalValue::isWeakODRLinkage(OriginalLinkage)))
+ S->setLinkage(GlobalValue::AvailableExternallyLinkage);
+ if (S->linkage() != OriginalLinkage)
+ recordNewLinkage(S->modulePath(), GUID, S->linkage());
+ }
+}
+
+// Resolve Weak and LinkOnce values in the \p Index.
+//
+// We'd like to drop these functions if they are no longer referenced in the
+// current module. However there is a chance that another module is still
+// referencing them because of the import. We make sure we always emit at least
+// one copy.
+void thinLTOResolveWeakForLinkerInIndex(
+ ModuleSummaryIndex &Index,
+ function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
+ isPrevailing,
+ function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
+ recordNewLinkage) {
+ // We won't optimize the globals that are referenced by an alias for now
+ // Ideally we should turn the alias into a global and duplicate the definition
+ // when needed.
+ DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
+ for (auto &I : Index)
+ for (auto &S : I.second)
+ if (auto AS = dyn_cast<AliasSummary>(S.get()))
+ GlobalInvolvedWithAlias.insert(&AS->getAliasee());
+
+ for (auto &I : Index)
+ thinLTOResolveWeakForLinkerGUID(I.second, I.first, GlobalInvolvedWithAlias,
+ isPrevailing, recordNewLinkage);
+}
+
+static void thinLTOInternalizeAndPromoteGUID(
+ GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
+ function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
+ for (auto &S : GVSummaryList) {
+ if (isExported(S->modulePath(), GUID)) {
+ if (GlobalValue::isLocalLinkage(S->linkage()))
+ S->setLinkage(GlobalValue::ExternalLinkage);
+ } else if (!GlobalValue::isLocalLinkage(S->linkage()))
+ S->setLinkage(GlobalValue::InternalLinkage);
+ }
+}
+
+// Update the linkages in the given \p Index to mark exported values
+// as external and non-exported values as internal.
+void thinLTOInternalizeAndPromoteInIndex(
+ ModuleSummaryIndex &Index,
+ function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
+ for (auto &I : Index)
+ thinLTOInternalizeAndPromoteGUID(I.second, I.first, isExported);
+}
+}
diff --git a/contrib/llvm/lib/LTO/LTOCodeGenerator.cpp b/contrib/llvm/lib/LTO/LTOCodeGenerator.cpp
index 66df23b..1da2d18 100644
--- a/contrib/llvm/lib/LTO/LTOCodeGenerator.cpp
+++ b/contrib/llvm/lib/LTO/LTOCodeGenerator.cpp
@@ -12,7 +12,9 @@
//
//===----------------------------------------------------------------------===//
-#include "llvm/LTO/LTOCodeGenerator.h"
+#include "llvm/LTO/legacy/LTOCodeGenerator.h"
+
+#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
@@ -23,6 +25,7 @@
#include "llvm/Config/config.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
+#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/DiagnosticPrinter.h"
@@ -32,7 +35,8 @@
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/InitializePasses.h"
-#include "llvm/LTO/LTOModule.h"
+#include "llvm/LTO/legacy/LTOModule.h"
+#include "llvm/LTO/legacy/UpdateCompilerUsed.h"
#include "llvm/Linker/Linker.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
@@ -51,6 +55,7 @@
#include "llvm/Target/TargetRegisterInfo.h"
#include "llvm/Target/TargetSubtargetInfo.h"
#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/IPO/Internalize.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/Transforms/ObjCARC.h"
#include <system_error>
@@ -64,9 +69,33 @@ const char* LTOCodeGenerator::getVersionString() {
#endif
}
+namespace llvm {
+cl::opt<bool> LTODiscardValueNames(
+ "lto-discard-value-names",
+ cl::desc("Strip names from Value during LTO (other than GlobalValue)."),
+#ifdef NDEBUG
+ cl::init(true),
+#else
+ cl::init(false),
+#endif
+ cl::Hidden);
+
+cl::opt<bool> LTOStripInvalidDebugInfo(
+ "lto-strip-invalid-debug-info",
+ cl::desc("Strip invalid debug info metadata during LTO instead of aborting."),
+#ifdef NDEBUG
+ cl::init(true),
+#else
+ cl::init(false),
+#endif
+ cl::Hidden);
+}
+
LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
: Context(Context), MergedModule(new Module("ld-temp.o", Context)),
TheLinker(new Linker(*MergedModule)) {
+ Context.setDiscardValueNames(LTODiscardValueNames);
+ Context.enableDebugTypeODRUniquing();
initializeLTOPasses();
}
@@ -78,28 +107,26 @@ LTOCodeGenerator::~LTOCodeGenerator() {}
void LTOCodeGenerator::initializeLTOPasses() {
PassRegistry &R = *PassRegistry::getPassRegistry();
- initializeInternalizePassPass(R);
- initializeIPSCCPPass(R);
- initializeGlobalOptPass(R);
- initializeConstantMergePass(R);
+ initializeInternalizeLegacyPassPass(R);
+ initializeIPSCCPLegacyPassPass(R);
+ initializeGlobalOptLegacyPassPass(R);
+ initializeConstantMergeLegacyPassPass(R);
initializeDAHPass(R);
initializeInstructionCombiningPassPass(R);
initializeSimpleInlinerPass(R);
initializePruneEHPass(R);
- initializeGlobalDCEPass(R);
+ initializeGlobalDCELegacyPassPass(R);
initializeArgPromotionPass(R);
initializeJumpThreadingPass(R);
initializeSROALegacyPassPass(R);
- initializeSROA_DTPass(R);
- initializeSROA_SSAUpPass(R);
- initializePostOrderFunctionAttrsPass(R);
- initializeReversePostOrderFunctionAttrsPass(R);
+ initializePostOrderFunctionAttrsLegacyPassPass(R);
+ initializeReversePostOrderFunctionAttrsLegacyPassPass(R);
initializeGlobalsAAWrapperPassPass(R);
- initializeLICMPass(R);
- initializeMergedLoadStoreMotionPass(R);
- initializeGVNPass(R);
- initializeMemCpyOptPass(R);
- initializeDCEPass(R);
+ initializeLegacyLICMPassPass(R);
+ initializeMergedLoadStoreMotionLegacyPassPass(R);
+ initializeGVNLegacyPassPass(R);
+ initializeMemCpyOptLegacyPassPass(R);
+ initializeDCELegacyPassPass(R);
initializeCFGSimplifyPassPass(R);
}
@@ -113,6 +140,9 @@ bool LTOCodeGenerator::addModule(LTOModule *Mod) {
for (int i = 0, e = undefs.size(); i != e; ++i)
AsmUndefinedRefs[undefs[i]] = 1;
+ // We've just changed the input, so let's make sure we verify it.
+ HasVerifiedInput = false;
+
return !ret;
}
@@ -128,9 +158,12 @@ void LTOCodeGenerator::setModule(std::unique_ptr<LTOModule> Mod) {
const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs();
for (int I = 0, E = Undefs.size(); I != E; ++I)
AsmUndefinedRefs[Undefs[I]] = 1;
+
+ // We've just changed the input, so let's make sure we verify it.
+ HasVerifiedInput = false;
}
-void LTOCodeGenerator::setTargetOptions(TargetOptions Options) {
+void LTOCodeGenerator::setTargetOptions(const TargetOptions &Options) {
this->Options = Options;
}
@@ -169,6 +202,9 @@ bool LTOCodeGenerator::writeMergedModules(const char *Path) {
if (!determineTarget())
return false;
+ // We always run the verifier once on the merged module.
+ verifyMergedModuleOnce();
+
// mark which symbols can not be internalized
applyScopeRestrictions();
@@ -281,7 +317,7 @@ bool LTOCodeGenerator::determineTarget() {
if (TargetMach)
return true;
- std::string TripleStr = MergedModule->getTargetTriple();
+ TripleStr = MergedModule->getTargetTriple();
if (TripleStr.empty()) {
TripleStr = sys::getDefaultTargetTriple();
MergedModule->setTargetTriple(TripleStr);
@@ -290,8 +326,8 @@ bool LTOCodeGenerator::determineTarget() {
// create target machine from info for merged modules
std::string ErrMsg;
- const Target *march = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
- if (!march) {
+ MArch = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
+ if (!MArch) {
emitError(ErrMsg);
return false;
}
@@ -311,147 +347,167 @@ bool LTOCodeGenerator::determineTarget() {
MCpu = "cyclone";
}
- TargetMach.reset(march->createTargetMachine(TripleStr, MCpu, FeatureStr,
- Options, RelocModel,
- CodeModel::Default, CGOptLevel));
+ TargetMach = createTargetMachine();
return true;
}
-void LTOCodeGenerator::
-applyRestriction(GlobalValue &GV,
- ArrayRef<StringRef> Libcalls,
- std::vector<const char*> &MustPreserveList,
- SmallPtrSetImpl<GlobalValue*> &AsmUsed,
- Mangler &Mangler) {
- // There are no restrictions to apply to declarations.
- if (GV.isDeclaration())
- return;
+std::unique_ptr<TargetMachine> LTOCodeGenerator::createTargetMachine() {
+ return std::unique_ptr<TargetMachine>(
+ MArch->createTargetMachine(TripleStr, MCpu, FeatureStr, Options,
+ RelocModel, CodeModel::Default, CGOptLevel));
+}
- // There is nothing more restrictive than private linkage.
- if (GV.hasPrivateLinkage())
+// If a linkonce global is present in the MustPreserveSymbols, we need to make
+// sure we honor this. To force the compiler to not drop it, we add it to the
+// "llvm.compiler.used" global.
+void LTOCodeGenerator::preserveDiscardableGVs(
+ Module &TheModule,
+ llvm::function_ref<bool(const GlobalValue &)> mustPreserveGV) {
+ SetVector<Constant *> UsedValuesSet;
+ if (GlobalVariable *LLVMUsed =
+ TheModule.getGlobalVariable("llvm.compiler.used")) {
+ ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
+ for (auto &V : Inits->operands())
+ UsedValuesSet.insert(cast<Constant>(&V));
+ LLVMUsed->eraseFromParent();
+ }
+ llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(TheModule.getContext());
+ auto mayPreserveGlobal = [&](GlobalValue &GV) {
+ if (!GV.isDiscardableIfUnused() || GV.isDeclaration())
+ return;
+ if (!mustPreserveGV(GV))
+ return;
+ if (GV.hasAvailableExternallyLinkage()) {
+ emitWarning(
+ (Twine("Linker asked to preserve available_externally global: '") +
+ GV.getName() + "'").str());
+ return;
+ }
+ if (GV.hasInternalLinkage()) {
+ emitWarning((Twine("Linker asked to preserve internal global: '") +
+ GV.getName() + "'").str());
+ return;
+ }
+ UsedValuesSet.insert(ConstantExpr::getBitCast(&GV, i8PTy));
+ };
+ for (auto &GV : TheModule)
+ mayPreserveGlobal(GV);
+ for (auto &GV : TheModule.globals())
+ mayPreserveGlobal(GV);
+ for (auto &GV : TheModule.aliases())
+ mayPreserveGlobal(GV);
+
+ if (UsedValuesSet.empty())
return;
- SmallString<64> Buffer;
- TargetMach->getNameWithPrefix(Buffer, &GV, Mangler);
-
- if (MustPreserveSymbols.count(Buffer))
- MustPreserveList.push_back(GV.getName().data());
- if (AsmUndefinedRefs.count(Buffer))
- AsmUsed.insert(&GV);
-
- // 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);
+ llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedValuesSet.size());
+ auto *LLVMUsed = new llvm::GlobalVariable(
+ TheModule, ATy, false, llvm::GlobalValue::AppendingLinkage,
+ llvm::ConstantArray::get(ATy, UsedValuesSet.getArrayRef()),
+ "llvm.compiler.used");
+ LLVMUsed->setSection("llvm.metadata");
}
-static void findUsedValues(GlobalVariable *LLVMUsed,
- SmallPtrSetImpl<GlobalValue*> &UsedValues) {
- if (!LLVMUsed) return;
+void LTOCodeGenerator::applyScopeRestrictions() {
+ if (ScopeRestrictionsDone)
+ return;
- ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
- for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
- if (GlobalValue *GV =
- dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
- UsedValues.insert(GV);
-}
+ // Declare a callback for the internalize pass that will ask for every
+ // candidate GlobalValue if it can be internalized or not.
+ SmallString<64> MangledName;
+ auto mustPreserveGV = [&](const GlobalValue &GV) -> bool {
+ // Unnamed globals can't be mangled, but they can't be preserved either.
+ if (!GV.hasName())
+ return false;
+
+ // Need to mangle the GV as the "MustPreserveSymbols" StringSet is filled
+ // with the linker supplied name, which on Darwin includes a leading
+ // underscore.
+ MangledName.clear();
+ MangledName.reserve(GV.getName().size() + 1);
+ Mangler::getNameWithPrefix(MangledName, GV.getName(),
+ MergedModule->getDataLayout());
+ return MustPreserveSymbols.count(MangledName);
+ };
+
+ // Preserve linkonce value on linker request
+ preserveDiscardableGVs(*MergedModule, mustPreserveGV);
+
+ if (!ShouldInternalize)
+ return;
-// Collect names of runtime library functions. User-defined functions with the
-// same names are added to llvm.compiler.used to prevent them from being
-// deleted by optimizations.
-static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls,
- const TargetLibraryInfo& TLI,
- const Module &Mod,
- const TargetMachine &TM) {
- // 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));
+ if (ShouldRestoreGlobalsLinkage) {
+ // Record the linkage type of non-local symbols so they can be restored
+ // prior
+ // to module splitting.
+ auto RecordLinkage = [&](const GlobalValue &GV) {
+ if (!GV.hasAvailableExternallyLinkage() && !GV.hasLocalLinkage() &&
+ GV.hasName())
+ ExternalSymbols.insert(std::make_pair(GV.getName(), GV.getLinkage()));
+ };
+ for (auto &GV : *MergedModule)
+ RecordLinkage(GV);
+ for (auto &GV : MergedModule->globals())
+ RecordLinkage(GV);
+ for (auto &GV : MergedModule->aliases())
+ RecordLinkage(GV);
}
- SmallPtrSet<const TargetLowering *, 1> TLSet;
+ // Update the llvm.compiler_used globals to force preserving libcalls and
+ // symbols referenced from asm
+ updateCompilerUsed(*MergedModule, *TargetMach, AsmUndefinedRefs);
- for (const Function &F : Mod) {
- const TargetLowering *Lowering =
- TM.getSubtargetImpl(F)->getTargetLowering();
-
- if (Lowering && TLSet.insert(Lowering).second)
- // TargetLowering has info on library calls that CodeGen expects to be
- // available, both from the C runtime and compiler-rt.
- 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);
- }
+ internalizeModule(*MergedModule, mustPreserveGV);
- array_pod_sort(Libcalls.begin(), Libcalls.end());
- Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()),
- Libcalls.end());
+ ScopeRestrictionsDone = true;
}
-void LTOCodeGenerator::applyScopeRestrictions() {
- if (ScopeRestrictionsDone || !ShouldInternalize)
+/// Restore original linkage for symbols that may have been internalized
+void LTOCodeGenerator::restoreLinkageForExternals() {
+ if (!ShouldInternalize || !ShouldRestoreGlobalsLinkage)
return;
- // Start off with a verification pass.
- legacy::PassManager passes;
- passes.add(createVerifierPass());
+ assert(ScopeRestrictionsDone &&
+ "Cannot externalize without internalization!");
- // mark which symbols can not be internalized
- Mangler Mangler;
- std::vector<const char*> MustPreserveList;
- SmallPtrSet<GlobalValue*, 8> AsmUsed;
- std::vector<StringRef> Libcalls;
- TargetLibraryInfoImpl TLII(Triple(TargetMach->getTargetTriple()));
- TargetLibraryInfo TLI(TLII);
-
- accumulateAndSortLibcalls(Libcalls, TLI, *MergedModule, *TargetMach);
-
- for (Function &f : *MergedModule)
- applyRestriction(f, Libcalls, MustPreserveList, AsmUsed, Mangler);
- for (GlobalVariable &v : MergedModule->globals())
- applyRestriction(v, Libcalls, MustPreserveList, AsmUsed, Mangler);
- for (GlobalAlias &a : MergedModule->aliases())
- applyRestriction(a, Libcalls, MustPreserveList, AsmUsed, Mangler);
-
- GlobalVariable *LLVMCompilerUsed =
- MergedModule->getGlobalVariable("llvm.compiler.used");
- findUsedValues(LLVMCompilerUsed, AsmUsed);
- if (LLVMCompilerUsed)
- LLVMCompilerUsed->eraseFromParent();
-
- if (!AsmUsed.empty()) {
- llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context);
- std::vector<Constant*> asmUsed2;
- for (auto *GV : AsmUsed) {
- Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
- asmUsed2.push_back(c);
- }
+ if (ExternalSymbols.empty())
+ return;
- 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");
+ auto externalize = [this](GlobalValue &GV) {
+ if (!GV.hasLocalLinkage() || !GV.hasName())
+ return;
- LLVMCompilerUsed->setSection("llvm.metadata");
- }
+ auto I = ExternalSymbols.find(GV.getName());
+ if (I == ExternalSymbols.end())
+ return;
- passes.add(createInternalizePass(MustPreserveList));
+ GV.setLinkage(I->second);
+ };
- // apply scope restrictions
- passes.run(*MergedModule);
+ std::for_each(MergedModule->begin(), MergedModule->end(), externalize);
+ std::for_each(MergedModule->global_begin(), MergedModule->global_end(),
+ externalize);
+ std::for_each(MergedModule->alias_begin(), MergedModule->alias_end(),
+ externalize);
+}
- ScopeRestrictionsDone = true;
+void LTOCodeGenerator::verifyMergedModuleOnce() {
+ // Only run on the first call.
+ if (HasVerifiedInput)
+ return;
+ HasVerifiedInput = true;
+
+ if (LTOStripInvalidDebugInfo) {
+ bool BrokenDebugInfo = false;
+ if (verifyModule(*MergedModule, &dbgs(), &BrokenDebugInfo))
+ report_fatal_error("Broken module found, compilation aborted!");
+ if (BrokenDebugInfo) {
+ emitWarning("Invalid debug info found, debug info will be stripped");
+ StripDebugInfo(*MergedModule);
+ }
+ }
+ if (verifyModule(*MergedModule, &dbgs()))
+ report_fatal_error("Broken module found, compilation aborted!");
}
/// Optimize merged modules using various IPO passes
@@ -461,6 +517,10 @@ bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline,
if (!this->determineTarget())
return false;
+ // We always run the verifier once on the merged module, the `DisableVerify`
+ // parameter only applies to subsequent verify.
+ verifyMergedModuleOnce();
+
// Mark which symbols can not be internalized
this->applyScopeRestrictions();
@@ -497,6 +557,10 @@ bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> Out) {
if (!this->determineTarget())
return false;
+ // We always run the verifier once on the merged module. If it has already
+ // been called in optimize(), this call will return early.
+ verifyMergedModuleOnce();
+
legacy::PassManager preCodeGenPasses;
// If the bitcode files contain ARC code and were compiled with optimization,
@@ -504,14 +568,22 @@ bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> Out) {
preCodeGenPasses.add(createObjCARCContractPass());
preCodeGenPasses.run(*MergedModule);
+ // Re-externalize globals that may have been internalized to increase scope
+ // for splitting
+ restoreLinkageForExternals();
+
// Do code generation. We need to preserve the module in case the client calls
// writeMergedModules() after compilation, but we only need to allow this at
// parallelism level 1. This is achieved by having splitCodeGen return the
// original module at parallelism level 1 which we then assign back to
// MergedModule.
- MergedModule =
- splitCodeGen(std::move(MergedModule), Out, MCpu, FeatureStr, Options,
- RelocModel, CodeModel::Default, CGOptLevel, FileType);
+ MergedModule = splitCodeGen(std::move(MergedModule), Out, {},
+ [&]() { return createTargetMachine(); }, FileType,
+ ShouldRestoreGlobalsLinkage);
+
+ // If statistics were requested, print them out after codegen.
+ if (llvm::AreStatisticsEnabled())
+ llvm::PrintStatistics();
return true;
}
@@ -599,3 +671,10 @@ void LTOCodeGenerator::emitError(const std::string &ErrMsg) {
else
Context.diagnose(LTODiagnosticInfo(ErrMsg));
}
+
+void LTOCodeGenerator::emitWarning(const std::string &ErrMsg) {
+ if (DiagHandler)
+ (*DiagHandler)(LTO_DS_WARNING, ErrMsg.c_str(), DiagContext);
+ else
+ Context.diagnose(LTODiagnosticInfo(ErrMsg, DS_Warning));
+}
diff --git a/contrib/llvm/lib/LTO/LTOModule.cpp b/contrib/llvm/lib/LTO/LTOModule.cpp
index 409b949..a1d6f93 100644
--- a/contrib/llvm/lib/LTO/LTOModule.cpp
+++ b/contrib/llvm/lib/LTO/LTOModule.cpp
@@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
-#include "llvm/LTO/LTOModule.h"
+#include "llvm/LTO/legacy/LTOModule.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/CodeGen/Analysis.h"
@@ -26,14 +26,13 @@
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCParser/MCAsmParser.h"
+#include "llvm/MC/MCParser/MCTargetAsmParser.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCSymbol.h"
-#include "llvm/MC/MCTargetAsmParser.h"
#include "llvm/MC/SubtargetFeature.h"
#include "llvm/Object/IRObjectFile.h"
#include "llvm/Object/ObjectFile.h"
-#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/MemoryBuffer.h"
@@ -54,11 +53,6 @@ LTOModule::LTOModule(std::unique_ptr<object::IRObjectFile> Obj,
llvm::TargetMachine *TM)
: IRFile(std::move(Obj)), _target(TM) {}
-LTOModule::LTOModule(std::unique_ptr<object::IRObjectFile> Obj,
- llvm::TargetMachine *TM,
- std::unique_ptr<LLVMContext> Context)
- : OwnedContext(std::move(Context)), IRFile(std::move(Obj)), _target(TM) {}
-
LTOModule::~LTOModule() {}
/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
@@ -80,6 +74,18 @@ bool LTOModule::isBitcodeFile(const char *Path) {
return bool(BCData);
}
+bool LTOModule::isThinLTO() {
+ // Right now the detection is only based on the summary presence. We may want
+ // to add a dedicated flag at some point.
+ return hasGlobalValueSummary(IRFile->getMemoryBufferRef(),
+ [](const DiagnosticInfo &DI) {
+ DiagnosticPrinterRawOStream DP(errs());
+ DI.print(DP);
+ errs() << '\n';
+ return;
+ });
+}
+
bool LTOModule::isBitcodeForTarget(MemoryBuffer *Buffer,
StringRef TriplePrefix) {
ErrorOr<MemoryBufferRef> BCOrErr =
@@ -102,53 +108,61 @@ std::string LTOModule::getProducerString(MemoryBuffer *Buffer) {
ErrorOr<std::unique_ptr<LTOModule>>
LTOModule::createFromFile(LLVMContext &Context, const char *path,
- TargetOptions options) {
+ const TargetOptions &options) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
MemoryBuffer::getFile(path);
- if (std::error_code EC = BufferOrErr.getError())
+ if (std::error_code EC = BufferOrErr.getError()) {
+ Context.emitError(EC.message());
return EC;
+ }
std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
- return makeLTOModule(Buffer->getMemBufferRef(), options, &Context);
+ return makeLTOModule(Buffer->getMemBufferRef(), options, Context,
+ /* ShouldBeLazy*/ false);
}
ErrorOr<std::unique_ptr<LTOModule>>
LTOModule::createFromOpenFile(LLVMContext &Context, int fd, const char *path,
- size_t size, TargetOptions options) {
+ size_t size, const TargetOptions &options) {
return createFromOpenFileSlice(Context, fd, path, size, 0, options);
}
ErrorOr<std::unique_ptr<LTOModule>>
LTOModule::createFromOpenFileSlice(LLVMContext &Context, int fd,
const char *path, size_t map_size,
- off_t offset, TargetOptions options) {
+ off_t offset, const TargetOptions &options) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
MemoryBuffer::getOpenFileSlice(fd, path, map_size, offset);
- if (std::error_code EC = BufferOrErr.getError())
+ if (std::error_code EC = BufferOrErr.getError()) {
+ Context.emitError(EC.message());
return EC;
+ }
std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
- return makeLTOModule(Buffer->getMemBufferRef(), options, &Context);
+ return makeLTOModule(Buffer->getMemBufferRef(), options, Context,
+ /* ShouldBeLazy */ false);
}
ErrorOr<std::unique_ptr<LTOModule>>
LTOModule::createFromBuffer(LLVMContext &Context, const void *mem,
- size_t length, TargetOptions options,
+ size_t length, const TargetOptions &options,
StringRef path) {
- return createInContext(mem, length, options, path, &Context);
-}
-
-ErrorOr<std::unique_ptr<LTOModule>>
-LTOModule::createInLocalContext(const void *mem, size_t length,
- TargetOptions options, StringRef path) {
- return createInContext(mem, length, options, path, nullptr);
+ StringRef Data((const char *)mem, length);
+ MemoryBufferRef Buffer(Data, path);
+ return makeLTOModule(Buffer, options, Context, /* ShouldBeLazy */ false);
}
ErrorOr<std::unique_ptr<LTOModule>>
-LTOModule::createInContext(const void *mem, size_t length,
- TargetOptions options, StringRef path,
- LLVMContext *Context) {
+LTOModule::createInLocalContext(std::unique_ptr<LLVMContext> Context,
+ const void *mem, size_t length,
+ const TargetOptions &options, StringRef path) {
StringRef Data((const char *)mem, length);
MemoryBufferRef Buffer(Data, path);
- return makeLTOModule(Buffer, options, Context);
+ // If we own a context, we know this is being used only for symbol extraction,
+ // not linking. Be lazy in that case.
+ ErrorOr<std::unique_ptr<LTOModule>> Ret =
+ makeLTOModule(Buffer, options, *Context, /* ShouldBeLazy */ true);
+ if (Ret)
+ (*Ret)->OwnedContext = std::move(Context);
+ return Ret;
}
static ErrorOr<std::unique_ptr<Module>>
@@ -158,8 +172,10 @@ parseBitcodeFileImpl(MemoryBufferRef Buffer, LLVMContext &Context,
// Find the buffer.
ErrorOr<MemoryBufferRef> MBOrErr =
IRObjectFile::findBitcodeInMemBuffer(Buffer);
- if (std::error_code EC = MBOrErr.getError())
+ if (std::error_code EC = MBOrErr.getError()) {
+ Context.emitError(EC.message());
return EC;
+ }
if (!ShouldBeLazy) {
// Parse the full file.
@@ -180,19 +196,10 @@ parseBitcodeFileImpl(MemoryBufferRef Buffer, LLVMContext &Context,
}
ErrorOr<std::unique_ptr<LTOModule>>
-LTOModule::makeLTOModule(MemoryBufferRef Buffer, TargetOptions options,
- LLVMContext *Context) {
- std::unique_ptr<LLVMContext> OwnedContext;
- if (!Context) {
- OwnedContext = llvm::make_unique<LLVMContext>();
- Context = OwnedContext.get();
- }
-
- // If we own a context, we know this is being used only for symbol
- // extraction, not linking. Be lazy in that case.
+LTOModule::makeLTOModule(MemoryBufferRef Buffer, const TargetOptions &options,
+ LLVMContext &Context, bool ShouldBeLazy) {
ErrorOr<std::unique_ptr<Module>> MOrErr =
- parseBitcodeFileImpl(Buffer, *Context,
- /* ShouldBeLazy */ static_cast<bool>(OwnedContext));
+ parseBitcodeFileImpl(Buffer, Context, ShouldBeLazy);
if (std::error_code EC = MOrErr.getError())
return EC;
std::unique_ptr<Module> &M = *MOrErr;
@@ -223,19 +230,14 @@ LTOModule::makeLTOModule(MemoryBufferRef Buffer, TargetOptions options,
CPU = "cyclone";
}
- TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
- options);
+ TargetMachine *target =
+ march->createTargetMachine(TripleStr, CPU, FeatureStr, options, None);
M->setDataLayout(target->createDataLayout());
std::unique_ptr<object::IRObjectFile> IRObj(
new object::IRObjectFile(Buffer, std::move(M)));
- std::unique_ptr<LTOModule> Ret;
- if (OwnedContext)
- Ret.reset(new LTOModule(std::move(IRObj), target, std::move(OwnedContext)));
- else
- Ret.reset(new LTOModule(std::move(IRObj), target));
-
+ std::unique_ptr<LTOModule> Ret(new LTOModule(std::move(IRObj), target));
Ret->parseSymbols();
Ret->parseMetadata();
diff --git a/contrib/llvm/lib/LTO/ThinLTOCodeGenerator.cpp b/contrib/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
new file mode 100644
index 0000000..bfb0980
--- /dev/null
+++ b/contrib/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
@@ -0,0 +1,835 @@
+//===-ThinLTOCodeGenerator.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.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the Thin Link Time Optimization library. This library is
+// intended to be used by linker to optimize code at link time.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
+
+#ifdef HAVE_LLVM_REVISION
+#include "LLVMLTORevision.h"
+#endif
+
+#include "llvm/ADT/Statistic.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/Analysis/ModuleSummaryAnalysis.h"
+#include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
+#include "llvm/Bitcode/BitcodeWriterPass.h"
+#include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
+#include "llvm/IR/DiagnosticPrinter.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/Mangler.h"
+#include "llvm/IRReader/IRReader.h"
+#include "llvm/LTO/LTO.h"
+#include "llvm/Linker/Linker.h"
+#include "llvm/MC/SubtargetFeature.h"
+#include "llvm/Object/IRObjectFile.h"
+#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
+#include "llvm/Support/CachePruning.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/SHA1.h"
+#include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/ThreadPool.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/IPO/FunctionImport.h"
+#include "llvm/Transforms/IPO/Internalize.h"
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/ObjCARC.h"
+#include "llvm/Transforms/Utils/FunctionImportUtils.h"
+
+#include <numeric>
+
+using namespace llvm;
+
+#define DEBUG_TYPE "thinlto"
+
+namespace llvm {
+// Flags -discard-value-names, defined in LTOCodeGenerator.cpp
+extern cl::opt<bool> LTODiscardValueNames;
+}
+
+namespace {
+
+static cl::opt<int> ThreadCount("threads",
+ cl::init(std::thread::hardware_concurrency()));
+
+static void diagnosticHandler(const DiagnosticInfo &DI) {
+ DiagnosticPrinterRawOStream DP(errs());
+ DI.print(DP);
+ errs() << '\n';
+}
+
+// Simple helper to save temporary files for debug.
+static void saveTempBitcode(const Module &TheModule, StringRef TempDir,
+ unsigned count, StringRef Suffix) {
+ if (TempDir.empty())
+ return;
+ // User asked to save temps, let dump the bitcode file after import.
+ auto SaveTempPath = TempDir + llvm::utostr(count) + Suffix;
+ std::error_code EC;
+ raw_fd_ostream OS(SaveTempPath.str(), EC, sys::fs::F_None);
+ if (EC)
+ report_fatal_error(Twine("Failed to open ") + SaveTempPath +
+ " to save optimized bitcode\n");
+ WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true);
+}
+
+static const GlobalValueSummary *
+getFirstDefinitionForLinker(const GlobalValueSummaryList &GVSummaryList) {
+ // If there is any strong definition anywhere, get it.
+ auto StrongDefForLinker = llvm::find_if(
+ GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
+ auto Linkage = Summary->linkage();
+ return !GlobalValue::isAvailableExternallyLinkage(Linkage) &&
+ !GlobalValue::isWeakForLinker(Linkage);
+ });
+ if (StrongDefForLinker != GVSummaryList.end())
+ return StrongDefForLinker->get();
+ // Get the first *linker visible* definition for this global in the summary
+ // list.
+ auto FirstDefForLinker = llvm::find_if(
+ GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
+ auto Linkage = Summary->linkage();
+ return !GlobalValue::isAvailableExternallyLinkage(Linkage);
+ });
+ // Extern templates can be emitted as available_externally.
+ if (FirstDefForLinker == GVSummaryList.end())
+ return nullptr;
+ return FirstDefForLinker->get();
+}
+
+// Populate map of GUID to the prevailing copy for any multiply defined
+// symbols. Currently assume first copy is prevailing, or any strong
+// definition. Can be refined with Linker information in the future.
+static void computePrevailingCopies(
+ const ModuleSummaryIndex &Index,
+ DenseMap<GlobalValue::GUID, const GlobalValueSummary *> &PrevailingCopy) {
+ auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
+ return GVSummaryList.size() > 1;
+ };
+
+ for (auto &I : Index) {
+ if (HasMultipleCopies(I.second))
+ PrevailingCopy[I.first] = getFirstDefinitionForLinker(I.second);
+ }
+}
+
+static StringMap<MemoryBufferRef>
+generateModuleMap(const std::vector<MemoryBufferRef> &Modules) {
+ StringMap<MemoryBufferRef> ModuleMap;
+ for (auto &ModuleBuffer : Modules) {
+ assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
+ ModuleMap.end() &&
+ "Expect unique Buffer Identifier");
+ ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer;
+ }
+ return ModuleMap;
+}
+
+static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
+ if (renameModuleForThinLTO(TheModule, Index))
+ report_fatal_error("renameModuleForThinLTO failed");
+}
+
+static void
+crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
+ StringMap<MemoryBufferRef> &ModuleMap,
+ const FunctionImporter::ImportMapTy &ImportList) {
+ ModuleLoader Loader(TheModule.getContext(), ModuleMap);
+ FunctionImporter Importer(Index, Loader);
+ Importer.importFunctions(TheModule, ImportList);
+}
+
+static void optimizeModule(Module &TheModule, TargetMachine &TM) {
+ // Populate the PassManager
+ PassManagerBuilder PMB;
+ PMB.LibraryInfo = new TargetLibraryInfoImpl(TM.getTargetTriple());
+ PMB.Inliner = createFunctionInliningPass();
+ // FIXME: should get it from the bitcode?
+ PMB.OptLevel = 3;
+ PMB.LoopVectorize = true;
+ PMB.SLPVectorize = true;
+ PMB.VerifyInput = true;
+ PMB.VerifyOutput = false;
+
+ legacy::PassManager PM;
+
+ // Add the TTI (required to inform the vectorizer about register size for
+ // instance)
+ PM.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
+
+ // Add optimizations
+ PMB.populateThinLTOPassManager(PM);
+
+ PM.run(TheModule);
+}
+
+// Convert the PreservedSymbols map from "Name" based to "GUID" based.
+static DenseSet<GlobalValue::GUID>
+computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
+ const Triple &TheTriple) {
+ DenseSet<GlobalValue::GUID> GUIDPreservedSymbols(PreservedSymbols.size());
+ for (auto &Entry : PreservedSymbols) {
+ StringRef Name = Entry.first();
+ if (TheTriple.isOSBinFormatMachO() && Name.size() > 0 && Name[0] == '_')
+ Name = Name.drop_front();
+ GUIDPreservedSymbols.insert(GlobalValue::getGUID(Name));
+ }
+ return GUIDPreservedSymbols;
+}
+
+std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
+ TargetMachine &TM) {
+ SmallVector<char, 128> OutputBuffer;
+
+ // CodeGen
+ {
+ raw_svector_ostream OS(OutputBuffer);
+ legacy::PassManager PM;
+
+ // If the bitcode files contain ARC code and were compiled with optimization,
+ // the ObjCARCContractPass must be run, so do it unconditionally here.
+ PM.add(createObjCARCContractPass());
+
+ // Setup the codegen now.
+ if (TM.addPassesToEmitFile(PM, OS, TargetMachine::CGFT_ObjectFile,
+ /* DisableVerify */ true))
+ report_fatal_error("Failed to setup codegen");
+
+ // Run codegen now. resulting binary is in OutputBuffer.
+ PM.run(TheModule);
+ }
+ return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
+}
+
+/// Manage caching for a single Module.
+class ModuleCacheEntry {
+ SmallString<128> EntryPath;
+
+public:
+ // Create a cache entry. This compute a unique hash for the Module considering
+ // the current list of export/import, and offer an interface to query to
+ // access the content in the cache.
+ ModuleCacheEntry(
+ StringRef CachePath, const ModuleSummaryIndex &Index, StringRef ModuleID,
+ const FunctionImporter::ImportMapTy &ImportList,
+ const FunctionImporter::ExportSetTy &ExportList,
+ const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
+ const GVSummaryMapTy &DefinedFunctions,
+ const DenseSet<GlobalValue::GUID> &PreservedSymbols) {
+ if (CachePath.empty())
+ return;
+
+ // Compute the unique hash for this entry
+ // This is based on the current compiler version, the module itself, the
+ // export list, the hash for every single module in the import list, the
+ // list of ResolvedODR for the module, and the list of preserved symbols.
+
+ SHA1 Hasher;
+
+ // Start with the compiler revision
+ Hasher.update(LLVM_VERSION_STRING);
+#ifdef HAVE_LLVM_REVISION
+ Hasher.update(LLVM_REVISION);
+#endif
+
+ // Include the hash for the current module
+ auto ModHash = Index.getModuleHash(ModuleID);
+ Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
+ for (auto F : ExportList)
+ // The export list can impact the internalization, be conservative here
+ Hasher.update(ArrayRef<uint8_t>((uint8_t *)&F, sizeof(F)));
+
+ // Include the hash for every module we import functions from
+ for (auto &Entry : ImportList) {
+ auto ModHash = Index.getModuleHash(Entry.first());
+ Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
+ }
+
+ // Include the hash for the resolved ODR.
+ for (auto &Entry : ResolvedODR) {
+ Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
+ sizeof(GlobalValue::GUID)));
+ Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
+ sizeof(GlobalValue::LinkageTypes)));
+ }
+
+ // Include the hash for the preserved symbols.
+ for (auto &Entry : PreservedSymbols) {
+ if (DefinedFunctions.count(Entry))
+ Hasher.update(
+ ArrayRef<uint8_t>((const uint8_t *)&Entry, sizeof(GlobalValue::GUID)));
+ }
+
+ sys::path::append(EntryPath, CachePath, toHex(Hasher.result()));
+ }
+
+ // Access the path to this entry in the cache.
+ StringRef getEntryPath() { return EntryPath; }
+
+ // Try loading the buffer for this cache entry.
+ ErrorOr<std::unique_ptr<MemoryBuffer>> tryLoadingBuffer() {
+ if (EntryPath.empty())
+ return std::error_code();
+ return MemoryBuffer::getFile(EntryPath);
+ }
+
+ // Cache the Produced object file
+ std::unique_ptr<MemoryBuffer>
+ write(std::unique_ptr<MemoryBuffer> OutputBuffer) {
+ if (EntryPath.empty())
+ return OutputBuffer;
+
+ // Write to a temporary to avoid race condition
+ SmallString<128> TempFilename;
+ int TempFD;
+ std::error_code EC =
+ sys::fs::createTemporaryFile("Thin", "tmp.o", TempFD, TempFilename);
+ if (EC) {
+ errs() << "Error: " << EC.message() << "\n";
+ report_fatal_error("ThinLTO: Can't get a temporary file");
+ }
+ {
+ raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
+ OS << OutputBuffer->getBuffer();
+ }
+ // Rename to final destination (hopefully race condition won't matter here)
+ EC = sys::fs::rename(TempFilename, EntryPath);
+ if (EC) {
+ sys::fs::remove(TempFilename);
+ raw_fd_ostream OS(EntryPath, EC, sys::fs::F_None);
+ if (EC)
+ report_fatal_error(Twine("Failed to open ") + EntryPath +
+ " to save cached entry\n");
+ OS << OutputBuffer->getBuffer();
+ }
+ auto ReloadedBufferOrErr = MemoryBuffer::getFile(EntryPath);
+ if (auto EC = ReloadedBufferOrErr.getError()) {
+ // FIXME diagnose
+ errs() << "error: can't reload cached file '" << EntryPath
+ << "': " << EC.message() << "\n";
+ return OutputBuffer;
+ }
+ return std::move(*ReloadedBufferOrErr);
+ }
+};
+
+static std::unique_ptr<MemoryBuffer>
+ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
+ StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
+ const FunctionImporter::ImportMapTy &ImportList,
+ const FunctionImporter::ExportSetTy &ExportList,
+ const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
+ const GVSummaryMapTy &DefinedGlobals,
+ const ThinLTOCodeGenerator::CachingOptions &CacheOptions,
+ bool DisableCodeGen, StringRef SaveTempsDir,
+ unsigned count) {
+
+ // "Benchmark"-like optimization: single-source case
+ bool SingleModule = (ModuleMap.size() == 1);
+
+ if (!SingleModule) {
+ promoteModule(TheModule, Index);
+
+ // Apply summary-based LinkOnce/Weak resolution decisions.
+ thinLTOResolveWeakForLinkerModule(TheModule, DefinedGlobals);
+
+ // Save temps: after promotion.
+ saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
+ }
+
+ // Be friendly and don't nuke totally the module when the client didn't
+ // supply anything to preserve.
+ if (!ExportList.empty() || !GUIDPreservedSymbols.empty()) {
+ // Apply summary-based internalization decisions.
+ thinLTOInternalizeModule(TheModule, DefinedGlobals);
+ }
+
+ // Save internalized bitcode
+ saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
+
+ if (!SingleModule) {
+ crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
+
+ // Save temps: after cross-module import.
+ saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
+ }
+
+ optimizeModule(TheModule, TM);
+
+ saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
+
+ if (DisableCodeGen) {
+ // Configured to stop before CodeGen, serialize the bitcode and return.
+ SmallVector<char, 128> OutputBuffer;
+ {
+ raw_svector_ostream OS(OutputBuffer);
+ ModuleSummaryIndexBuilder IndexBuilder(&TheModule);
+ WriteBitcodeToFile(&TheModule, OS, true, &IndexBuilder.getIndex());
+ }
+ return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
+ }
+
+ return codegenModule(TheModule, TM);
+}
+
+/// Resolve LinkOnce/Weak symbols. Record resolutions in the \p ResolvedODR map
+/// for caching, and in the \p Index for application during the ThinLTO
+/// backends. This is needed for correctness for exported symbols (ensure
+/// at least one copy kept) and a compile-time optimization (to drop duplicate
+/// copies when possible).
+static void resolveWeakForLinkerInIndex(
+ ModuleSummaryIndex &Index,
+ StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>>
+ &ResolvedODR) {
+
+ DenseMap<GlobalValue::GUID, const GlobalValueSummary *> PrevailingCopy;
+ computePrevailingCopies(Index, PrevailingCopy);
+
+ auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) {
+ const auto &Prevailing = PrevailingCopy.find(GUID);
+ // Not in map means that there was only one copy, which must be prevailing.
+ if (Prevailing == PrevailingCopy.end())
+ return true;
+ return Prevailing->second == S;
+ };
+
+ auto recordNewLinkage = [&](StringRef ModuleIdentifier,
+ GlobalValue::GUID GUID,
+ GlobalValue::LinkageTypes NewLinkage) {
+ ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
+ };
+
+ thinLTOResolveWeakForLinkerInIndex(Index, isPrevailing, recordNewLinkage);
+}
+
+// Initialize the TargetMachine builder for a given Triple
+static void initTMBuilder(TargetMachineBuilder &TMBuilder,
+ const Triple &TheTriple) {
+ // Set a default CPU for Darwin triples (copied from LTOCodeGenerator).
+ // FIXME this looks pretty terrible...
+ if (TMBuilder.MCpu.empty() && TheTriple.isOSDarwin()) {
+ if (TheTriple.getArch() == llvm::Triple::x86_64)
+ TMBuilder.MCpu = "core2";
+ else if (TheTriple.getArch() == llvm::Triple::x86)
+ TMBuilder.MCpu = "yonah";
+ else if (TheTriple.getArch() == llvm::Triple::aarch64)
+ TMBuilder.MCpu = "cyclone";
+ }
+ TMBuilder.TheTriple = std::move(TheTriple);
+}
+
+} // end anonymous namespace
+
+void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
+ MemoryBufferRef Buffer(Data, Identifier);
+ if (Modules.empty()) {
+ // First module added, so initialize the triple and some options
+ LLVMContext Context;
+ Triple TheTriple(getBitcodeTargetTriple(Buffer, Context));
+ initTMBuilder(TMBuilder, Triple(TheTriple));
+ }
+#ifndef NDEBUG
+ else {
+ LLVMContext Context;
+ assert(TMBuilder.TheTriple.str() ==
+ getBitcodeTargetTriple(Buffer, Context) &&
+ "ThinLTO modules with different triple not supported");
+ }
+#endif
+ Modules.push_back(Buffer);
+}
+
+void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
+ PreservedSymbols.insert(Name);
+}
+
+void ThinLTOCodeGenerator::crossReferenceSymbol(StringRef Name) {
+ // FIXME: At the moment, we don't take advantage of this extra information,
+ // we're conservatively considering cross-references as preserved.
+ // CrossReferencedSymbols.insert(Name);
+ PreservedSymbols.insert(Name);
+}
+
+// TargetMachine factory
+std::unique_ptr<TargetMachine> TargetMachineBuilder::create() const {
+ std::string ErrMsg;
+ const Target *TheTarget =
+ TargetRegistry::lookupTarget(TheTriple.str(), ErrMsg);
+ if (!TheTarget) {
+ report_fatal_error("Can't load target for this Triple: " + ErrMsg);
+ }
+
+ // Use MAttr as the default set of features.
+ SubtargetFeatures Features(MAttr);
+ Features.getDefaultSubtargetFeatures(TheTriple);
+ std::string FeatureStr = Features.getString();
+ return std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
+ TheTriple.str(), MCpu, FeatureStr, Options, RelocModel,
+ CodeModel::Default, CGOptLevel));
+}
+
+/**
+ * Produce the combined summary index from all the bitcode files:
+ * "thin-link".
+ */
+std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
+ std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
+ uint64_t NextModuleId = 0;
+ for (auto &ModuleBuffer : Modules) {
+ ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
+ object::ModuleSummaryIndexObjectFile::create(ModuleBuffer,
+ diagnosticHandler);
+ if (std::error_code EC = ObjOrErr.getError()) {
+ // FIXME diagnose
+ errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: "
+ << EC.message() << "\n";
+ return nullptr;
+ }
+ auto Index = (*ObjOrErr)->takeIndex();
+ if (CombinedIndex) {
+ CombinedIndex->mergeFrom(std::move(Index), ++NextModuleId);
+ } else {
+ CombinedIndex = std::move(Index);
+ }
+ }
+ return CombinedIndex;
+}
+
+/**
+ * Perform promotion and renaming of exported internal functions.
+ * Index is updated to reflect linkage changes from weak resolution.
+ */
+void ThinLTOCodeGenerator::promote(Module &TheModule,
+ ModuleSummaryIndex &Index) {
+ auto ModuleCount = Index.modulePaths().size();
+ auto ModuleIdentifier = TheModule.getModuleIdentifier();
+ // Collect for each module the list of function it defines (GUID -> Summary).
+ StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
+ Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
+
+ // Generate import/export list
+ StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
+ StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
+ ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
+ ExportLists);
+
+ // Resolve LinkOnce/Weak symbols.
+ StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
+ resolveWeakForLinkerInIndex(Index, ResolvedODR);
+
+ thinLTOResolveWeakForLinkerModule(
+ TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
+
+ promoteModule(TheModule, Index);
+}
+
+/**
+ * Perform cross-module importing for the module identified by ModuleIdentifier.
+ */
+void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
+ ModuleSummaryIndex &Index) {
+ auto ModuleMap = generateModuleMap(Modules);
+ auto ModuleCount = Index.modulePaths().size();
+
+ // Collect for each module the list of function it defines (GUID -> Summary).
+ StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
+ Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
+
+ // Generate import/export list
+ StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
+ StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
+ ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
+ ExportLists);
+ auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
+
+ crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
+}
+
+/**
+ * Compute the list of summaries needed for importing into module.
+ */
+void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
+ StringRef ModulePath, ModuleSummaryIndex &Index,
+ std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
+ auto ModuleCount = Index.modulePaths().size();
+
+ // Collect for each module the list of function it defines (GUID -> Summary).
+ StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
+ Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
+
+ // Generate import/export list
+ StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
+ StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
+ ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
+ ExportLists);
+
+ llvm::gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
+ ImportLists,
+ ModuleToSummariesForIndex);
+}
+
+/**
+ * Emit the list of files needed for importing into module.
+ */
+void ThinLTOCodeGenerator::emitImports(StringRef ModulePath,
+ StringRef OutputName,
+ ModuleSummaryIndex &Index) {
+ auto ModuleCount = Index.modulePaths().size();
+
+ // Collect for each module the list of function it defines (GUID -> Summary).
+ StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
+ Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
+
+ // Generate import/export list
+ StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
+ StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
+ ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
+ ExportLists);
+
+ std::error_code EC;
+ if ((EC = EmitImportsFiles(ModulePath, OutputName, ImportLists)))
+ report_fatal_error(Twine("Failed to open ") + OutputName +
+ " to save imports lists\n");
+}
+
+/**
+ * Perform internalization. Index is updated to reflect linkage changes.
+ */
+void ThinLTOCodeGenerator::internalize(Module &TheModule,
+ ModuleSummaryIndex &Index) {
+ initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
+ auto ModuleCount = Index.modulePaths().size();
+ auto ModuleIdentifier = TheModule.getModuleIdentifier();
+
+ // Convert the preserved symbols set from string to GUID
+ auto GUIDPreservedSymbols =
+ computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
+
+ // Collect for each module the list of function it defines (GUID -> Summary).
+ StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
+ Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
+
+ // Generate import/export list
+ StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
+ StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
+ ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
+ ExportLists);
+ auto &ExportList = ExportLists[ModuleIdentifier];
+
+ // Be friendly and don't nuke totally the module when the client didn't
+ // supply anything to preserve.
+ if (ExportList.empty() && GUIDPreservedSymbols.empty())
+ return;
+
+ // Internalization
+ auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
+ const auto &ExportList = ExportLists.find(ModuleIdentifier);
+ return (ExportList != ExportLists.end() &&
+ ExportList->second.count(GUID)) ||
+ GUIDPreservedSymbols.count(GUID);
+ };
+ thinLTOInternalizeAndPromoteInIndex(Index, isExported);
+ thinLTOInternalizeModule(TheModule,
+ ModuleToDefinedGVSummaries[ModuleIdentifier]);
+}
+
+/**
+ * Perform post-importing ThinLTO optimizations.
+ */
+void ThinLTOCodeGenerator::optimize(Module &TheModule) {
+ initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
+
+ // Optimize now
+ optimizeModule(TheModule, *TMBuilder.create());
+}
+
+/**
+ * Perform ThinLTO CodeGen.
+ */
+std::unique_ptr<MemoryBuffer> ThinLTOCodeGenerator::codegen(Module &TheModule) {
+ initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
+ return codegenModule(TheModule, *TMBuilder.create());
+}
+
+// Main entry point for the ThinLTO processing
+void ThinLTOCodeGenerator::run() {
+ if (CodeGenOnly) {
+ // Perform only parallel codegen and return.
+ ThreadPool Pool;
+ assert(ProducedBinaries.empty() && "The generator should not be reused");
+ ProducedBinaries.resize(Modules.size());
+ int count = 0;
+ for (auto &ModuleBuffer : Modules) {
+ Pool.async([&](int count) {
+ LLVMContext Context;
+ Context.setDiscardValueNames(LTODiscardValueNames);
+
+ // Parse module now
+ auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
+
+ // CodeGen
+ ProducedBinaries[count] = codegen(*TheModule);
+ }, count++);
+ }
+
+ return;
+ }
+
+ // Sequential linking phase
+ auto Index = linkCombinedIndex();
+
+ // Save temps: index.
+ if (!SaveTempsDir.empty()) {
+ auto SaveTempPath = SaveTempsDir + "index.bc";
+ std::error_code EC;
+ raw_fd_ostream OS(SaveTempPath, EC, sys::fs::F_None);
+ if (EC)
+ report_fatal_error(Twine("Failed to open ") + SaveTempPath +
+ " to save optimized bitcode\n");
+ WriteIndexToFile(*Index, OS);
+ }
+
+ // Prepare the resulting object vector
+ assert(ProducedBinaries.empty() && "The generator should not be reused");
+ ProducedBinaries.resize(Modules.size());
+
+ // Prepare the module map.
+ auto ModuleMap = generateModuleMap(Modules);
+ auto ModuleCount = Modules.size();
+
+ // Collect for each module the list of function it defines (GUID -> Summary).
+ StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
+ Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
+
+ // Collect the import/export lists for all modules from the call-graph in the
+ // combined index.
+ StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
+ StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
+ ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
+ ExportLists);
+
+ // Convert the preserved symbols set from string to GUID, this is needed for
+ // computing the caching hash and the internalization.
+ auto GUIDPreservedSymbols =
+ computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
+
+ // We use a std::map here to be able to have a defined ordering when
+ // producing a hash for the cache entry.
+ // FIXME: we should be able to compute the caching hash for the entry based
+ // on the index, and nuke this map.
+ StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
+
+ // Resolve LinkOnce/Weak symbols, this has to be computed early because it
+ // impacts the caching.
+ resolveWeakForLinkerInIndex(*Index, ResolvedODR);
+
+ auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
+ const auto &ExportList = ExportLists.find(ModuleIdentifier);
+ return (ExportList != ExportLists.end() &&
+ ExportList->second.count(GUID)) ||
+ GUIDPreservedSymbols.count(GUID);
+ };
+
+ // Use global summary-based analysis to identify symbols that can be
+ // internalized (because they aren't exported or preserved as per callback).
+ // Changes are made in the index, consumed in the ThinLTO backends.
+ thinLTOInternalizeAndPromoteInIndex(*Index, isExported);
+
+ // Make sure that every module has an entry in the ExportLists and
+ // ResolvedODR maps to enable threaded access to these maps below.
+ for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
+ ExportLists[DefinedGVSummaries.first()];
+ ResolvedODR[DefinedGVSummaries.first()];
+ }
+
+ // Compute the ordering we will process the inputs: the rough heuristic here
+ // is to sort them per size so that the largest module get schedule as soon as
+ // possible. This is purely a compile-time optimization.
+ std::vector<int> ModulesOrdering;
+ ModulesOrdering.resize(Modules.size());
+ std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
+ std::sort(ModulesOrdering.begin(), ModulesOrdering.end(),
+ [&](int LeftIndex, int RightIndex) {
+ auto LSize = Modules[LeftIndex].getBufferSize();
+ auto RSize = Modules[RightIndex].getBufferSize();
+ return LSize > RSize;
+ });
+
+ // Parallel optimizer + codegen
+ {
+ ThreadPool Pool(ThreadCount);
+ for (auto IndexCount : ModulesOrdering) {
+ auto &ModuleBuffer = Modules[IndexCount];
+ Pool.async([&](int count) {
+ auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier();
+ auto &ExportList = ExportLists[ModuleIdentifier];
+
+ auto &DefinedFunctions = ModuleToDefinedGVSummaries[ModuleIdentifier];
+
+ // The module may be cached, this helps handling it.
+ ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
+ ImportLists[ModuleIdentifier], ExportList,
+ ResolvedODR[ModuleIdentifier],
+ DefinedFunctions, GUIDPreservedSymbols);
+
+ {
+ auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
+ DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss") << " '"
+ << CacheEntry.getEntryPath() << "' for buffer " << count
+ << " " << ModuleIdentifier << "\n");
+
+ if (ErrOrBuffer) {
+ // Cache Hit!
+ ProducedBinaries[count] = std::move(ErrOrBuffer.get());
+ return;
+ }
+ }
+
+ LLVMContext Context;
+ Context.setDiscardValueNames(LTODiscardValueNames);
+ Context.enableDebugTypeODRUniquing();
+
+ // Parse module now
+ auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
+
+ // Save temps: original file.
+ saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
+
+ auto &ImportList = ImportLists[ModuleIdentifier];
+ // Run the main process now, and generates a binary
+ auto OutputBuffer = ProcessThinLTOModule(
+ *TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
+ ExportList, GUIDPreservedSymbols,
+ ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
+ DisableCodeGen, SaveTempsDir, count);
+
+ OutputBuffer = CacheEntry.write(std::move(OutputBuffer));
+ ProducedBinaries[count] = std::move(OutputBuffer);
+ }, IndexCount);
+ }
+ }
+
+ CachePruning(CacheOptions.Path)
+ .setPruningInterval(CacheOptions.PruningInterval)
+ .setEntryExpiration(CacheOptions.Expiration)
+ .setMaxSize(CacheOptions.MaxPercentageOfAvailableSpace)
+ .prune();
+
+ // If statistics were requested, print them out now.
+ if (llvm::AreStatisticsEnabled())
+ llvm::PrintStatistics();
+}
diff --git a/contrib/llvm/lib/LTO/UpdateCompilerUsed.cpp b/contrib/llvm/lib/LTO/UpdateCompilerUsed.cpp
new file mode 100644
index 0000000..a574db6
--- /dev/null
+++ b/contrib/llvm/lib/LTO/UpdateCompilerUsed.cpp
@@ -0,0 +1,146 @@
+//==-LTOInternalize.cpp - LLVM Link Time Optimizer Internalization Utility -==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines a helper to run the internalization part of LTO.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/LTO/legacy/UpdateCompilerUsed.h"
+#include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/Mangler.h"
+#include "llvm/Target/TargetLowering.h"
+#include "llvm/Target/TargetSubtargetInfo.h"
+#include "llvm/Transforms/IPO/Internalize.h"
+
+using namespace llvm;
+
+namespace {
+
+// Helper class that collects AsmUsed and user supplied libcalls.
+class PreserveLibCallsAndAsmUsed {
+public:
+ PreserveLibCallsAndAsmUsed(const StringSet<> &AsmUndefinedRefs,
+ const TargetMachine &TM,
+ SmallPtrSetImpl<const GlobalValue *> &LLVMUsed)
+ : AsmUndefinedRefs(AsmUndefinedRefs), TM(TM), LLVMUsed(LLVMUsed) {}
+
+ void findInModule(const Module &TheModule) {
+ initializeLibCalls(TheModule);
+ for (const Function &F : TheModule)
+ findLibCallsAndAsm(F);
+ for (const GlobalVariable &GV : TheModule.globals())
+ findLibCallsAndAsm(GV);
+ for (const GlobalAlias &GA : TheModule.aliases())
+ findLibCallsAndAsm(GA);
+ }
+
+private:
+ // Inputs
+ const StringSet<> &AsmUndefinedRefs;
+ const TargetMachine &TM;
+
+ // Temps
+ llvm::Mangler Mangler;
+ StringSet<> Libcalls;
+
+ // Output
+ SmallPtrSetImpl<const GlobalValue *> &LLVMUsed;
+
+ // Collect names of runtime library functions. User-defined functions with the
+ // same names are added to llvm.compiler.used to prevent them from being
+ // deleted by optimizations.
+ void initializeLibCalls(const Module &TheModule) {
+ TargetLibraryInfoImpl TLII(Triple(TM.getTargetTriple()));
+ TargetLibraryInfo TLI(TLII);
+
+ // 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.insert(TLI.getName(F));
+ }
+
+ SmallPtrSet<const TargetLowering *, 1> TLSet;
+
+ for (const Function &F : TheModule) {
+ const TargetLowering *Lowering =
+ TM.getSubtargetImpl(F)->getTargetLowering();
+
+ if (Lowering && TLSet.insert(Lowering).second)
+ // TargetLowering has info on library calls that CodeGen expects to be
+ // available, both from the C runtime and compiler-rt.
+ 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.insert(Name);
+ }
+ }
+
+ void findLibCallsAndAsm(const GlobalValue &GV) {
+ // There are no restrictions to apply to declarations.
+ if (GV.isDeclaration())
+ return;
+
+ // There is nothing more restrictive than private linkage.
+ if (GV.hasPrivateLinkage())
+ return;
+
+ // 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) && Libcalls.count(GV.getName()))
+ LLVMUsed.insert(&GV);
+
+ SmallString<64> Buffer;
+ TM.getNameWithPrefix(Buffer, &GV, Mangler);
+ if (AsmUndefinedRefs.count(Buffer))
+ LLVMUsed.insert(&GV);
+ }
+};
+
+} // namespace anonymous
+
+void llvm::updateCompilerUsed(Module &TheModule, const TargetMachine &TM,
+ const StringSet<> &AsmUndefinedRefs) {
+ SmallPtrSet<const GlobalValue *, 8> UsedValues;
+ PreserveLibCallsAndAsmUsed(AsmUndefinedRefs, TM, UsedValues)
+ .findInModule(TheModule);
+
+ if (UsedValues.empty())
+ return;
+
+ llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(TheModule.getContext());
+ std::vector<Constant *> UsedValuesList;
+ for (const auto *GV : UsedValues) {
+ Constant *c =
+ ConstantExpr::getBitCast(const_cast<GlobalValue *>(GV), i8PTy);
+ UsedValuesList.push_back(c);
+ }
+
+ GlobalVariable *LLVMUsed = TheModule.getGlobalVariable("llvm.compiler.used");
+ if (LLVMUsed) {
+ ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
+ for (auto &V : Inits->operands())
+ UsedValuesList.push_back(cast<Constant>(&V));
+ LLVMUsed->eraseFromParent();
+ }
+
+ llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, UsedValuesList.size());
+ LLVMUsed = new llvm::GlobalVariable(
+ TheModule, ATy, false, llvm::GlobalValue::AppendingLinkage,
+ llvm::ConstantArray::get(ATy, UsedValuesList), "llvm.compiler.used");
+
+ LLVMUsed->setSection("llvm.metadata");
+}
OpenPOWER on IntegriCloud