2016-03-09 09:37:22 +08:00
|
|
|
//===-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/ThinLTOCodeGenerator.h"
|
|
|
|
|
2016-04-21 13:54:23 +08:00
|
|
|
#ifdef HAVE_LLVM_REVISION
|
|
|
|
#include "LLVMLTORevision.h"
|
|
|
|
#endif
|
2016-04-24 11:18:01 +08:00
|
|
|
|
|
|
|
#include "UpdateCompilerUsed.h"
|
2016-03-15 05:18:10 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2016-03-15 08:04:37 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2016-04-11 21:58:45 +08:00
|
|
|
#include "llvm/Analysis/ModuleSummaryAnalysis.h"
|
2016-03-09 09:37:22 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2016-03-15 05:18:10 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeWriterPass.h"
|
2016-03-15 08:04:37 +08:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2016-03-09 09:37:22 +08:00
|
|
|
#include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
|
2016-03-15 05:18:10 +08:00
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2016-03-15 08:04:37 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2016-03-09 09:37:22 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
|
|
|
#include "llvm/IR/Mangler.h"
|
|
|
|
#include "llvm/IRReader/IRReader.h"
|
2016-05-24 06:54:06 +08:00
|
|
|
#include "llvm/LTO/LTO.h"
|
2016-03-09 09:37:22 +08:00
|
|
|
#include "llvm/Linker/Linker.h"
|
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
2016-04-24 11:18:01 +08:00
|
|
|
#include "llvm/Object/IRObjectFile.h"
|
2016-03-15 08:04:37 +08:00
|
|
|
#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
|
2016-04-21 13:54:23 +08:00
|
|
|
#include "llvm/Support/CachePruning.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/SHA1.h"
|
2016-03-09 09:37:22 +08:00
|
|
|
#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"
|
2016-04-24 11:18:01 +08:00
|
|
|
#include "llvm/Transforms/IPO/Internalize.h"
|
2016-03-09 09:37:22 +08:00
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
|
|
|
#include "llvm/Transforms/ObjCARC.h"
|
|
|
|
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
|
|
|
|
|
2016-05-17 03:33:07 +08:00
|
|
|
#include <numeric>
|
|
|
|
|
2016-03-09 09:37:22 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2016-04-16 15:02:16 +08:00
|
|
|
#define DEBUG_TYPE "thinlto"
|
|
|
|
|
Add a flag to the LLVMContext to disable name for Value other than GlobalValue
Summary:
This is intended to be a performance flag, on the same level as clang
cc1 option "--disable-free". LLVM will never initialize it by default,
it will be up to the client creating the LLVMContext to request this
behavior. Clang will do it by default in Release build (just like
--disable-free).
"opt" and "llc" can opt-in using -disable-named-value command line
option.
When performing LTO on llvm-tblgen, the initial merging of IR peaks
at 92MB without this patch, and 86MB after this patch,setNameImpl()
drops from 6.5MB to 0.5MB.
The total link time goes from ~29.5s to ~27.8s.
Compared to a compile-time flag (like the IRBuilder one), it performs
very close. I profiled on SROA and obtain these results:
420ms with IRBuilder that preserve name
372ms with IRBuilder that strip name
375ms with IRBuilder that preserve name, and a runtime flag to strip
Reviewers: chandlerc, dexonsmith, bogner
Subscribers: joker.eph, llvm-commits
Differential Revision: http://reviews.llvm.org/D17946
From: Mehdi Amini <mehdi.amini@apple.com>
llvm-svn: 263086
2016-03-10 09:28:54 +08:00
|
|
|
namespace llvm {
|
|
|
|
// Flags -discard-value-names, defined in LTOCodeGenerator.cpp
|
|
|
|
extern cl::opt<bool> LTODiscardValueNames;
|
|
|
|
}
|
|
|
|
|
2016-03-09 09:37:22 +08:00
|
|
|
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");
|
2016-04-05 05:19:31 +08:00
|
|
|
WriteBitcodeToFile(&TheModule, OS, /* ShouldPreserveUseListOrder */ true);
|
2016-03-09 09:37:22 +08:00
|
|
|
}
|
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
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();
|
2016-04-02 05:53:50 +08:00
|
|
|
// Get the first *linker visible* definition for this global in the summary
|
|
|
|
// list.
|
|
|
|
auto FirstDefForLinker = llvm::find_if(
|
2016-04-24 22:57:11 +08:00
|
|
|
GVSummaryList, [](const std::unique_ptr<GlobalValueSummary> &Summary) {
|
|
|
|
auto Linkage = Summary->linkage();
|
2016-04-02 05:53:50 +08:00
|
|
|
return !GlobalValue::isAvailableExternallyLinkage(Linkage);
|
|
|
|
});
|
2016-05-25 01:24:25 +08:00
|
|
|
// 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) {
|
2016-04-24 22:57:11 +08:00
|
|
|
auto HasMultipleCopies = [&](const GlobalValueSummaryList &GVSummaryList) {
|
|
|
|
return GVSummaryList.size() > 1;
|
|
|
|
};
|
2016-04-02 05:53:50 +08:00
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
for (auto &I : Index) {
|
|
|
|
if (HasMultipleCopies(I.second))
|
|
|
|
PrevailingCopy[I.first] = getFirstDefinitionForLinker(I.second);
|
2016-04-02 05:53:50 +08:00
|
|
|
}
|
2016-05-25 01:24:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void thinLTOResolveWeakForLinkerGUID(
|
|
|
|
GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
|
|
|
|
DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
|
|
|
|
std::function<bool(GlobalValue::GUID, const GlobalValueSummary *)>
|
|
|
|
isPrevailing,
|
|
|
|
std::function<bool(StringRef, GlobalValue::GUID)> isExported,
|
|
|
|
std::function<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
|
|
|
|
recordNewLinkage) {
|
|
|
|
auto HasMultipleCopies = GVSummaryList.size() > 1;
|
|
|
|
|
|
|
|
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 first module will keep it,
|
|
|
|
// but turned into a weak, while the others will drop it when possible.
|
|
|
|
if (!HasMultipleCopies) {
|
|
|
|
// Exported Linkonce needs to be promoted to not be discarded.
|
|
|
|
// FIXME: This should handle LinkOnceAny as well, but that should be a
|
|
|
|
// follow-on to the NFC restructuring:
|
|
|
|
// if (GlobalValue::isLinkOnceLinkage(OriginalLinkage) &&
|
|
|
|
// isExported(S->modulePath(), GUID))
|
|
|
|
// S->setLinkage(GlobalValue::getWeakLinkage(
|
|
|
|
// GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
|
|
|
|
if (GlobalValue::isLinkOnceODRLinkage(OriginalLinkage) &&
|
|
|
|
isExported(S->modulePath(), GUID))
|
|
|
|
S->setLinkage(GlobalValue::WeakODRLinkage);
|
|
|
|
} else if (isPrevailing(GUID, S.get())) {
|
|
|
|
// FIXME: This should handle LinkOnceAny as well, but that should be a
|
|
|
|
// follow-on to the NFC restructuring:
|
|
|
|
// if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
|
|
|
|
// S->setLinkage(GlobalValue::getWeakLinkage(
|
|
|
|
// GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
|
|
|
|
if (GlobalValue::isLinkOnceODRLinkage(OriginalLinkage))
|
|
|
|
S->setLinkage(GlobalValue::WeakODRLinkage);
|
|
|
|
}
|
|
|
|
// 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());
|
2016-04-02 05:53:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
// 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,
|
|
|
|
std::function<bool(GlobalValue::GUID, const GlobalValueSummary *)>
|
|
|
|
isPrevailing,
|
|
|
|
std::function<bool(StringRef, GlobalValue::GUID)> isExported,
|
|
|
|
std::function<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
|
|
|
|
recordNewLinkage) {
|
2016-04-14 16:46:22 +08:00
|
|
|
if (Index.modulePaths().size() == 1)
|
|
|
|
// Nothing to do if we don't have multiple modules
|
|
|
|
return;
|
|
|
|
|
2016-04-02 05:53:50 +08:00
|
|
|
// 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.
|
2016-04-16 15:02:16 +08:00
|
|
|
DenseSet<GlobalValueSummary *> GlobalInvolvedWithAlias;
|
2016-05-25 01:24:25 +08:00
|
|
|
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, isExported, recordNewLinkage);
|
2016-04-16 15:02:16 +08:00
|
|
|
}
|
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
/// Fixup WeakForLinker linkages in \p TheModule based on summary analysis.
|
|
|
|
void thinLTOResolveWeakForLinkerModule(Module &TheModule,
|
|
|
|
const GVSummaryMapTy &DefinedGlobals) {
|
|
|
|
auto updateLinkage = [&](GlobalValue &GV) {
|
|
|
|
if (!GlobalValue::isWeakForLinker(GV.getLinkage()))
|
|
|
|
return;
|
|
|
|
// See if the global summary analysis computed a new resolved linkage.
|
|
|
|
const auto &GS = DefinedGlobals.find(GV.getGUID());
|
|
|
|
if (GS == DefinedGlobals.end())
|
|
|
|
return;
|
|
|
|
auto NewLinkage = GS->second->linkage();
|
|
|
|
if (NewLinkage == GV.getLinkage())
|
|
|
|
return;
|
2016-04-21 13:47:17 +08:00
|
|
|
DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
|
2016-05-25 01:24:25 +08:00
|
|
|
<< GV.getLinkage() << " to " << NewLinkage << "\n");
|
|
|
|
GV.setLinkage(NewLinkage);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Process functions and global now
|
|
|
|
for (auto &GV : TheModule)
|
|
|
|
updateLinkage(GV);
|
|
|
|
for (auto &GV : TheModule.globals())
|
|
|
|
updateLinkage(GV);
|
|
|
|
for (auto &GV : TheModule.aliases())
|
|
|
|
updateLinkage(GV);
|
2016-04-02 05:53:50 +08:00
|
|
|
}
|
|
|
|
|
2016-03-09 09:37:22 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-15 08:04:37 +08:00
|
|
|
static void promoteModule(Module &TheModule, const ModuleSummaryIndex &Index) {
|
2016-03-09 09:37:22 +08:00
|
|
|
if (renameModuleForThinLTO(TheModule, Index))
|
|
|
|
report_fatal_error("renameModuleForThinLTO failed");
|
|
|
|
}
|
|
|
|
|
2016-03-26 13:40:34 +08:00
|
|
|
static void
|
|
|
|
crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
|
|
|
|
StringMap<MemoryBufferRef> &ModuleMap,
|
|
|
|
const FunctionImporter::ImportMapTy &ImportList) {
|
2016-03-09 09:37:22 +08:00
|
|
|
ModuleLoader Loader(TheModule.getContext(), ModuleMap);
|
|
|
|
FunctionImporter Importer(Index, Loader);
|
2016-03-26 13:40:34 +08:00
|
|
|
Importer.importFunctions(TheModule, ImportList);
|
2016-03-09 09:37:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
static void thinLTOInternalizeAndPromoteGUID(
|
|
|
|
GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
|
|
|
|
std::function<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);
|
|
|
|
}
|
2016-04-24 11:18:01 +08:00
|
|
|
}
|
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
// Update the linkages in the given \p Index to mark exported values
|
|
|
|
// as external and non-exported values as internal.
|
|
|
|
void thinLTOInternalizeAndPromoteInIndex(
|
|
|
|
ModuleSummaryIndex &Index,
|
|
|
|
std::function<bool(StringRef, GlobalValue::GUID)> isExported) {
|
|
|
|
for (auto &I : Index)
|
|
|
|
thinLTOInternalizeAndPromoteGUID(I.second, I.first, isExported);
|
|
|
|
}
|
2016-04-24 11:18:01 +08:00
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
// Run internalization on \p TheModule based on symmary analysis.
|
|
|
|
void thinLTOInternalizeModule(Module &TheModule,
|
|
|
|
const GVSummaryMapTy &DefinedGlobals) {
|
2016-04-24 11:18:01 +08:00
|
|
|
// Parse inline ASM and collect the list of symbols that are not defined in
|
|
|
|
// the current module.
|
|
|
|
StringSet<> AsmUndefinedRefs;
|
|
|
|
object::IRObjectFile::CollectAsmUndefinedRefs(
|
|
|
|
Triple(TheModule.getTargetTriple()), TheModule.getModuleInlineAsm(),
|
|
|
|
[&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) {
|
|
|
|
if (Flags & object::BasicSymbolRef::SF_Undefined)
|
|
|
|
AsmUndefinedRefs.insert(Name);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Declare a callback for the internalize pass that will ask for every
|
|
|
|
// candidate GlobalValue if it can be internalized or not.
|
2016-05-25 01:24:25 +08:00
|
|
|
auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
|
|
|
|
// Can't be internalized if referenced in inline asm.
|
|
|
|
if (AsmUndefinedRefs.count(GV.getName()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Lookup the linkage recorded in the summaries during global analysis.
|
|
|
|
const auto &GS = DefinedGlobals.find(GV.getGUID());
|
|
|
|
GlobalValue::LinkageTypes Linkage;
|
|
|
|
if (GS == DefinedGlobals.end()) {
|
|
|
|
// Must have been promoted (possibly conservatively). Find original
|
|
|
|
// name so that we can access the correct summary and see if it can
|
|
|
|
// be internalized again.
|
|
|
|
// FIXME: Eventually we should control promotion instead of promoting
|
|
|
|
// and internalizing again.
|
|
|
|
StringRef OrigName =
|
|
|
|
ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName());
|
|
|
|
std::string OrigId = GlobalValue::getGlobalIdentifier(
|
|
|
|
OrigName, GlobalValue::InternalLinkage,
|
|
|
|
TheModule.getSourceFileName());
|
|
|
|
const auto &GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
|
|
|
|
assert(GS != DefinedGlobals.end());
|
|
|
|
Linkage = GS->second->linkage();
|
|
|
|
} else
|
|
|
|
Linkage = GS->second->linkage();
|
|
|
|
return !GlobalValue::isLocalLinkage(Linkage);
|
|
|
|
};
|
2016-04-24 11:18:01 +08:00
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
// FIXME: See if we can just internalize directly here via linkage changes
|
|
|
|
// based on the index, rather than invoking internalizeModule.
|
2016-04-24 11:18:01 +08:00
|
|
|
llvm::internalizeModule(TheModule, MustPreserveGV);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2016-03-09 09:37:22 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
|
|
|
|
TargetMachine &TM) {
|
|
|
|
SmallVector<char, 128> OutputBuffer;
|
|
|
|
|
|
|
|
// CodeGen
|
|
|
|
{
|
|
|
|
raw_svector_ostream OS(OutputBuffer);
|
|
|
|
legacy::PassManager PM;
|
2016-04-01 16:22:59 +08:00
|
|
|
|
|
|
|
// 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.
|
2016-03-09 09:37:22 +08:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2016-04-21 13:54:23 +08:00
|
|
|
/// 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,
|
2016-04-26 05:09:51 +08:00
|
|
|
const GVSummaryMapTy &DefinedFunctions,
|
2016-04-21 13:54:23 +08:00
|
|
|
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) {
|
2016-04-28 02:35:02 +08:00
|
|
|
Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.first,
|
2016-04-21 13:54:23 +08:00
|
|
|
sizeof(GlobalValue::GUID)));
|
2016-04-28 02:35:02 +08:00
|
|
|
Hasher.update(ArrayRef<uint8_t>((const uint8_t *)&Entry.second,
|
2016-04-21 13:54:23 +08:00
|
|
|
sizeof(GlobalValue::LinkageTypes)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include the hash for the preserved symbols.
|
|
|
|
for (auto &Entry : PreservedSymbols) {
|
|
|
|
if (DefinedFunctions.count(Entry))
|
|
|
|
Hasher.update(
|
2016-04-28 02:35:02 +08:00
|
|
|
ArrayRef<uint8_t>((const uint8_t *)&Entry, sizeof(GlobalValue::GUID)));
|
2016-04-21 13:54:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sys::path::append(EntryPath, CachePath, toHex(Hasher.result()));
|
|
|
|
}
|
|
|
|
|
2016-04-24 11:18:01 +08:00
|
|
|
// Access the path to this entry in the cache.
|
|
|
|
StringRef getEntryPath() { return EntryPath; }
|
|
|
|
|
2016-04-21 13:54:23 +08:00
|
|
|
// 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
|
2016-05-17 03:11:59 +08:00
|
|
|
std::unique_ptr<MemoryBuffer>
|
|
|
|
write(std::unique_ptr<MemoryBuffer> OutputBuffer) {
|
2016-04-21 13:54:23 +08:00
|
|
|
if (EntryPath.empty())
|
2016-05-17 03:11:59 +08:00
|
|
|
return OutputBuffer;
|
2016-04-21 13:54:23 +08:00
|
|
|
|
|
|
|
// 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);
|
2016-05-17 03:11:59 +08:00
|
|
|
OS << OutputBuffer->getBuffer();
|
2016-04-21 13:54:23 +08:00
|
|
|
}
|
|
|
|
// Rename to final destination (hopefully race condition won't matter here)
|
2016-05-14 12:58:38 +08:00
|
|
|
EC = sys::fs::rename(TempFilename, EntryPath);
|
|
|
|
if (EC) {
|
2016-05-14 13:16:35 +08:00
|
|
|
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");
|
2016-05-17 03:11:59 +08:00
|
|
|
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;
|
2016-05-14 12:58:38 +08:00
|
|
|
}
|
2016-05-17 03:11:59 +08:00
|
|
|
return std::move(*ReloadedBufferOrErr);
|
2016-04-21 13:54:23 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
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,
|
|
|
|
ThinLTOCodeGenerator::CachingOptions CacheOptions,
|
|
|
|
bool DisableCodeGen, StringRef SaveTempsDir,
|
|
|
|
unsigned count) {
|
2016-04-24 11:18:01 +08:00
|
|
|
|
2016-03-09 09:37:22 +08:00
|
|
|
// "Benchmark"-like optimization: single-source case
|
|
|
|
bool SingleModule = (ModuleMap.size() == 1);
|
|
|
|
|
|
|
|
if (!SingleModule) {
|
|
|
|
promoteModule(TheModule, Index);
|
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
// Apply summary-based LinkOnce/Weak resolution decisions.
|
|
|
|
thinLTOResolveWeakForLinkerModule(TheModule, DefinedGlobals);
|
2016-04-02 05:53:50 +08:00
|
|
|
|
2016-03-09 09:37:22 +08:00
|
|
|
// Save temps: after promotion.
|
2016-05-05 13:14:16 +08:00
|
|
|
saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
|
2016-04-24 11:18:01 +08:00
|
|
|
}
|
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
// 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);
|
|
|
|
}
|
2016-03-09 09:37:22 +08:00
|
|
|
|
2016-04-24 11:18:01 +08:00
|
|
|
// Save internalized bitcode
|
2016-05-05 13:14:16 +08:00
|
|
|
saveTempBitcode(TheModule, SaveTempsDir, count, ".2.internalized.bc");
|
2016-04-24 11:18:01 +08:00
|
|
|
|
|
|
|
if (!SingleModule) {
|
2016-03-26 13:40:34 +08:00
|
|
|
crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
|
2016-03-09 09:37:22 +08:00
|
|
|
|
|
|
|
// Save temps: after cross-module import.
|
2016-05-05 13:14:16 +08:00
|
|
|
saveTempBitcode(TheModule, SaveTempsDir, count, ".3.imported.bc");
|
2016-03-09 09:37:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
optimizeModule(TheModule, TM);
|
|
|
|
|
2016-05-05 13:14:16 +08:00
|
|
|
saveTempBitcode(TheModule, SaveTempsDir, count, ".4.opt.bc");
|
2016-03-09 09:37:22 +08:00
|
|
|
|
2016-04-01 14:47:02 +08:00
|
|
|
if (DisableCodeGen) {
|
|
|
|
// Configured to stop before CodeGen, serialize the bitcode and return.
|
|
|
|
SmallVector<char, 128> OutputBuffer;
|
|
|
|
{
|
|
|
|
raw_svector_ostream OS(OutputBuffer);
|
2016-04-11 21:58:45 +08:00
|
|
|
ModuleSummaryIndexBuilder IndexBuilder(&TheModule);
|
|
|
|
WriteBitcodeToFile(&TheModule, OS, true, &IndexBuilder.getIndex());
|
2016-04-01 14:47:02 +08:00
|
|
|
}
|
|
|
|
return make_unique<ObjectMemoryBuffer>(std::move(OutputBuffer));
|
|
|
|
}
|
|
|
|
|
2016-03-09 09:37:22 +08:00
|
|
|
return codegenModule(TheModule, TM);
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
/// 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,
|
|
|
|
const StringMap<FunctionImporter::ExportSetTy> &ExportLists,
|
|
|
|
const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
|
|
|
|
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 isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
|
|
|
|
const auto &ExportList = ExportLists.find(ModuleIdentifier);
|
|
|
|
assert(ExportList != ExportLists.end() && "Missing export list for module");
|
|
|
|
return ExportList->second.count(GUID) || GUIDPreservedSymbols.count(GUID);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto recordNewLinkage = [&](StringRef ModuleIdentifier,
|
|
|
|
GlobalValue::GUID GUID,
|
|
|
|
GlobalValue::LinkageTypes NewLinkage) {
|
|
|
|
ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
|
|
|
|
};
|
|
|
|
|
|
|
|
thinLTOResolveWeakForLinkerInIndex(Index, isPrevailing, isExported,
|
|
|
|
recordNewLinkage);
|
|
|
|
}
|
|
|
|
|
2016-03-09 09:37:22 +08:00
|
|
|
// 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) {
|
2016-04-24 11:18:01 +08:00
|
|
|
// 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);
|
2016-03-09 09:37:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-15 08:04:37 +08:00
|
|
|
* Produce the combined summary index from all the bitcode files:
|
2016-03-09 09:37:22 +08:00
|
|
|
* "thin-link".
|
|
|
|
*/
|
2016-03-15 08:04:37 +08:00
|
|
|
std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
|
|
|
|
std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
|
2016-03-09 09:37:22 +08:00
|
|
|
uint64_t NextModuleId = 0;
|
|
|
|
for (auto &ModuleBuffer : Modules) {
|
2016-03-15 08:04:37 +08:00
|
|
|
ErrorOr<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr =
|
|
|
|
object::ModuleSummaryIndexObjectFile::create(ModuleBuffer,
|
2016-04-22 09:52:00 +08:00
|
|
|
diagnosticHandler);
|
2016-03-09 09:37:22 +08:00
|
|
|
if (std::error_code EC = ObjOrErr.getError()) {
|
|
|
|
// FIXME diagnose
|
2016-03-15 08:04:37 +08:00
|
|
|
errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: "
|
2016-03-09 09:37:22 +08:00
|
|
|
<< 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.
|
2016-05-25 01:24:25 +08:00
|
|
|
* Index is updated to reflect linkage changes from weak resolution.
|
2016-03-09 09:37:22 +08:00
|
|
|
*/
|
|
|
|
void ThinLTOCodeGenerator::promote(Module &TheModule,
|
2016-03-15 08:04:37 +08:00
|
|
|
ModuleSummaryIndex &Index) {
|
2016-04-21 13:47:17 +08:00
|
|
|
auto ModuleCount = Index.modulePaths().size();
|
2016-04-16 15:02:16 +08:00
|
|
|
auto ModuleIdentifier = TheModule.getModuleIdentifier();
|
|
|
|
// Collect for each module the list of function it defines (GUID -> Summary).
|
2016-04-26 05:09:51 +08:00
|
|
|
StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries;
|
2016-04-16 15:02:16 +08:00
|
|
|
Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
|
2016-04-02 05:53:50 +08:00
|
|
|
|
2016-04-21 13:47:17 +08:00
|
|
|
// Generate import/export list
|
|
|
|
StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
|
|
|
|
StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
|
|
|
|
ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
|
|
|
|
ExportLists);
|
|
|
|
|
2016-04-26 18:35:01 +08:00
|
|
|
// Convert the preserved symbols set from string to GUID
|
|
|
|
auto GUIDPreservedSymbols =
|
|
|
|
computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
|
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
// Resolve LinkOnce/Weak symbols.
|
|
|
|
StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
|
|
|
|
resolveWeakForLinkerInIndex(Index, ExportLists, GUIDPreservedSymbols,
|
|
|
|
ResolvedODR);
|
|
|
|
|
|
|
|
thinLTOResolveWeakForLinkerModule(
|
|
|
|
TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
|
2016-04-02 05:53:50 +08:00
|
|
|
|
2016-03-09 09:37:22 +08:00
|
|
|
promoteModule(TheModule, Index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform cross-module importing for the module identified by ModuleIdentifier.
|
|
|
|
*/
|
|
|
|
void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
|
2016-03-15 08:04:37 +08:00
|
|
|
ModuleSummaryIndex &Index) {
|
2016-03-09 09:37:22 +08:00
|
|
|
auto ModuleMap = generateModuleMap(Modules);
|
2016-04-16 15:02:16 +08:00
|
|
|
auto ModuleCount = Index.modulePaths().size();
|
|
|
|
|
|
|
|
// Collect for each module the list of function it defines (GUID -> Summary).
|
2016-04-26 05:09:51 +08:00
|
|
|
StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
|
2016-04-16 15:02:16 +08:00
|
|
|
Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
|
2016-03-26 13:40:34 +08:00
|
|
|
|
|
|
|
// Generate import/export list
|
|
|
|
StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
|
|
|
|
StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
|
2016-04-16 15:02:16 +08:00
|
|
|
ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
|
|
|
|
ExportLists);
|
2016-03-26 13:40:34 +08:00
|
|
|
auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
|
|
|
|
|
|
|
|
crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
|
2016-03-09 09:37:22 +08:00
|
|
|
}
|
|
|
|
|
2016-05-10 21:48:23 +08:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2016-05-10 23:54:09 +08:00
|
|
|
/**
|
|
|
|
* 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");
|
|
|
|
}
|
|
|
|
|
2016-04-24 11:18:01 +08:00
|
|
|
/**
|
2016-05-25 01:24:25 +08:00
|
|
|
* Perform internalization. Index is updated to reflect linkage changes.
|
2016-04-24 11:18:01 +08:00
|
|
|
*/
|
|
|
|
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).
|
2016-04-26 05:09:51 +08:00
|
|
|
StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
|
2016-04-24 11:18:01 +08:00
|
|
|
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];
|
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
// Be friendly and don't nuke totally the module when the client didn't
|
|
|
|
// supply anything to preserve.
|
|
|
|
if (ExportList.empty() && GUIDPreservedSymbols.empty())
|
|
|
|
return;
|
|
|
|
|
2016-04-24 11:18:01 +08:00
|
|
|
// Internalization
|
2016-05-25 01:24:25 +08:00
|
|
|
auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
|
|
|
|
const auto &ExportList = ExportLists.find(ModuleIdentifier);
|
|
|
|
assert(ExportList != ExportLists.end() && "Missing export list for module");
|
|
|
|
return ExportList->second.count(GUID) || GUIDPreservedSymbols.count(GUID);
|
|
|
|
};
|
|
|
|
thinLTOInternalizeAndPromoteInIndex(Index, isExported);
|
|
|
|
thinLTOInternalizeModule(TheModule,
|
|
|
|
ModuleToDefinedGVSummaries[ModuleIdentifier]);
|
2016-04-24 11:18:01 +08:00
|
|
|
}
|
|
|
|
|
2016-03-09 09:37:22 +08:00
|
|
|
/**
|
|
|
|
* Perform post-importing ThinLTO optimizations.
|
|
|
|
*/
|
|
|
|
void ThinLTOCodeGenerator::optimize(Module &TheModule) {
|
|
|
|
initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
|
2016-04-24 11:18:01 +08:00
|
|
|
|
|
|
|
// Optimize now
|
2016-03-09 09:37:22 +08:00
|
|
|
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() {
|
2016-04-01 14:47:02 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-09 09:37:22 +08:00
|
|
|
// 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");
|
[ThinLTO] Support for reference graph in per-module and combined summary.
Summary:
This patch adds support for including a full reference graph including
call graph edges and other GV references in the summary.
The reference graph edges can be used to make importing decisions
without materializing any source modules, can be used in the plugin
to make file staging decisions for distributed build systems, and is
expected to have other uses.
The call graph edges are recorded in each function summary in the
bitcode via a list of <CalleeValueIds, StaticCount> tuples when no PGO
data exists, or <CalleeValueId, StaticCount, ProfileCount> pairs when
there is PGO, where the ValueId can be mapped to the function GUID via
the ValueSymbolTable. In the function index in memory, the call graph
edges reference the target via the CalleeGUID instead of the
CalleeValueId.
The reference graph edges are recorded in each summary record with a
list of referenced value IDs, which can be mapped to value GUID via the
ValueSymbolTable.
Addtionally, a new summary record type is added to record references
from global variable initializers. A number of bitcode records and data
structures have been renamed to reflect the newly expanded scope of the
summary beyond functions. More cleanup will follow.
Reviewers: joker.eph, davidxl
Subscribers: joker.eph, llvm-commits
Differential Revision: http://reviews.llvm.org/D17212
llvm-svn: 263275
2016-03-12 02:52:24 +08:00
|
|
|
WriteIndexToFile(*Index, OS);
|
2016-03-09 09:37:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2016-03-26 13:40:34 +08:00
|
|
|
auto ModuleCount = Modules.size();
|
|
|
|
|
2016-04-16 15:02:16 +08:00
|
|
|
// Collect for each module the list of function it defines (GUID -> Summary).
|
2016-04-26 05:09:51 +08:00
|
|
|
StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
|
2016-04-16 15:02:16 +08:00
|
|
|
Index->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
|
|
|
|
|
2016-03-26 13:40:34 +08:00
|
|
|
// 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);
|
2016-04-16 15:02:16 +08:00
|
|
|
ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
|
|
|
|
ExportLists);
|
2016-03-09 09:37:22 +08:00
|
|
|
|
2016-04-21 13:54:23 +08:00
|
|
|
// Convert the preserved symbols set from string to GUID, this is needed for
|
2016-04-24 11:18:01 +08:00
|
|
|
// computing the caching hash and the internalization.
|
|
|
|
auto GUIDPreservedSymbols =
|
|
|
|
computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
|
2016-04-21 13:54:23 +08:00
|
|
|
|
2016-05-25 01:24:25 +08:00
|
|
|
// 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, ExportLists, GUIDPreservedSymbols,
|
|
|
|
ResolvedODR);
|
|
|
|
|
|
|
|
auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
|
|
|
|
const auto &ExportList = ExportLists.find(ModuleIdentifier);
|
|
|
|
assert(ExportList != ExportLists.end() && "Missing export list for module");
|
|
|
|
return 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);
|
|
|
|
|
2016-05-25 02:44:01 +08:00
|
|
|
// 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) {
|
2016-05-15 13:49:47 +08:00
|
|
|
ExportLists[DefinedGVSummaries.first()];
|
2016-05-25 02:44:01 +08:00
|
|
|
ResolvedODR[DefinedGVSummaries.first()];
|
|
|
|
}
|
2016-05-15 13:49:47 +08:00
|
|
|
|
2016-05-17 03:33:07 +08:00
|
|
|
// 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;
|
|
|
|
});
|
|
|
|
|
2016-03-09 09:37:22 +08:00
|
|
|
// Parallel optimizer + codegen
|
|
|
|
{
|
|
|
|
ThreadPool Pool(ThreadCount);
|
2016-05-17 03:33:07 +08:00
|
|
|
for (auto IndexCount : ModulesOrdering) {
|
|
|
|
auto &ModuleBuffer = Modules[IndexCount];
|
2016-03-09 09:37:22 +08:00
|
|
|
Pool.async([&](int count) {
|
2016-04-16 15:02:16 +08:00
|
|
|
auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier();
|
2016-04-21 13:47:17 +08:00
|
|
|
auto &ExportList = ExportLists[ModuleIdentifier];
|
2016-04-16 15:02:16 +08:00
|
|
|
|
2016-04-21 13:54:23 +08:00
|
|
|
auto &DefinedFunctions = ModuleToDefinedGVSummaries[ModuleIdentifier];
|
|
|
|
|
|
|
|
// The module may be cached, this helps handling it.
|
2016-04-24 11:18:01 +08:00
|
|
|
ModuleCacheEntry CacheEntry(CacheOptions.Path, *Index, ModuleIdentifier,
|
|
|
|
ImportLists[ModuleIdentifier], ExportList,
|
2016-05-25 01:24:25 +08:00
|
|
|
ResolvedODR[ModuleIdentifier],
|
|
|
|
DefinedFunctions, GUIDPreservedSymbols);
|
2016-04-21 13:54:23 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
auto ErrOrBuffer = CacheEntry.tryLoadingBuffer();
|
2016-04-24 11:18:01 +08:00
|
|
|
DEBUG(dbgs() << "Cache " << (ErrOrBuffer ? "hit" : "miss") << " '"
|
|
|
|
<< CacheEntry.getEntryPath() << "' for buffer " << count
|
|
|
|
<< " " << ModuleIdentifier << "\n");
|
|
|
|
|
2016-04-21 13:54:23 +08:00
|
|
|
if (ErrOrBuffer) {
|
|
|
|
// Cache Hit!
|
|
|
|
ProducedBinaries[count] = std::move(ErrOrBuffer.get());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMContext Context;
|
|
|
|
Context.setDiscardValueNames(LTODiscardValueNames);
|
|
|
|
Context.enableDebugTypeODRUniquing();
|
|
|
|
|
2016-03-09 09:37:22 +08:00
|
|
|
// Parse module now
|
|
|
|
auto TheModule = loadModuleFromBuffer(ModuleBuffer, Context, false);
|
|
|
|
|
|
|
|
// Save temps: original file.
|
2016-04-24 11:18:01 +08:00
|
|
|
saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
|
2016-03-09 09:37:22 +08:00
|
|
|
|
2016-04-16 15:02:16 +08:00
|
|
|
auto &ImportList = ImportLists[ModuleIdentifier];
|
2016-04-24 11:18:01 +08:00
|
|
|
// Run the main process now, and generates a binary
|
2016-04-21 13:54:23 +08:00
|
|
|
auto OutputBuffer = ProcessThinLTOModule(
|
2016-03-26 13:40:34 +08:00
|
|
|
*TheModule, *Index, ModuleMap, *TMBuilder.create(), ImportList,
|
2016-05-25 01:24:25 +08:00
|
|
|
ExportList, GUIDPreservedSymbols,
|
|
|
|
ModuleToDefinedGVSummaries[ModuleIdentifier], CacheOptions,
|
2016-04-24 11:18:01 +08:00
|
|
|
DisableCodeGen, SaveTempsDir, count);
|
2016-04-21 13:54:23 +08:00
|
|
|
|
2016-05-17 03:11:59 +08:00
|
|
|
OutputBuffer = CacheEntry.write(std::move(OutputBuffer));
|
2016-04-21 13:54:23 +08:00
|
|
|
ProducedBinaries[count] = std::move(OutputBuffer);
|
2016-05-17 03:33:07 +08:00
|
|
|
}, IndexCount);
|
2016-03-09 09:37:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 13:54:23 +08:00
|
|
|
CachePruning(CacheOptions.Path)
|
|
|
|
.setPruningInterval(CacheOptions.PruningInterval)
|
|
|
|
.setEntryExpiration(CacheOptions.Expiration)
|
|
|
|
.setMaxSize(CacheOptions.MaxPercentageOfAvailableSpace)
|
|
|
|
.prune();
|
|
|
|
|
2016-03-09 09:37:22 +08:00
|
|
|
// If statistics were requested, print them out now.
|
|
|
|
if (llvm::AreStatisticsEnabled())
|
|
|
|
llvm::PrintStatistics();
|
|
|
|
}
|