2016-05-24 06:54:06 +08:00
|
|
|
//===-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"
|
2016-08-11 22:58:12 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2016-11-11 13:34:58 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2016-08-11 22:58:12 +08:00
|
|
|
#include "llvm/CodeGen/Analysis.h"
|
|
|
|
#include "llvm/IR/AutoUpgrade.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
|
|
|
#include "llvm/LTO/LTOBackend.h"
|
|
|
|
#include "llvm/Linker/IRMover.h"
|
|
|
|
#include "llvm/Object/ModuleSummaryIndexObjectFile.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2016-05-24 06:54:06 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2016-08-11 22:58:12 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-08-24 05:30:12 +08:00
|
|
|
#include "llvm/Support/SHA1.h"
|
2016-05-24 06:54:06 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2016-08-11 22:58:12 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/ThreadPool.h"
|
2016-10-20 01:35:01 +08:00
|
|
|
#include "llvm/Support/Threading.h"
|
2016-05-24 06:54:06 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-08-11 22:58:12 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
|
|
|
#include "llvm/Transforms/Utils/SplitModule.h"
|
2016-05-24 06:54:06 +08:00
|
|
|
|
2016-08-11 22:58:12 +08:00
|
|
|
#include <set>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lto;
|
|
|
|
using namespace object;
|
2016-05-24 06:54:06 +08:00
|
|
|
|
2016-08-24 05:30:12 +08:00
|
|
|
#define DEBUG_TYPE "lto"
|
|
|
|
|
|
|
|
// Returns a unique hash for the Module considering the current list of
|
|
|
|
// export/import and other global analysis results.
|
|
|
|
// The hash is produced in \p Key.
|
|
|
|
static void computeCacheKey(
|
|
|
|
SmallString<40> &Key, const ModuleSummaryIndex &Index, StringRef ModuleID,
|
|
|
|
const FunctionImporter::ImportMapTy &ImportList,
|
|
|
|
const FunctionImporter::ExportSetTy &ExportList,
|
|
|
|
const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
|
|
|
|
const GVSummaryMapTy &DefinedGlobals) {
|
|
|
|
// 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 linkage type to reflect internalization and weak
|
|
|
|
// resolution.
|
|
|
|
for (auto &GS : DefinedGlobals) {
|
|
|
|
GlobalValue::LinkageTypes Linkage = GS.second->linkage();
|
|
|
|
Hasher.update(
|
|
|
|
ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Key = toHex(Hasher.result());
|
|
|
|
}
|
|
|
|
|
2016-05-24 06:54:06 +08:00
|
|
|
// Simple helper to load a module from bitcode
|
2016-08-11 22:58:12 +08:00
|
|
|
std::unique_ptr<Module>
|
|
|
|
llvm::loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context,
|
|
|
|
bool Lazy) {
|
2016-05-24 06:54:06 +08:00
|
|
|
SMDiagnostic Err;
|
2016-11-13 15:00:17 +08:00
|
|
|
Expected<std::unique_ptr<Module>> ModuleOrErr =
|
|
|
|
Lazy ? getLazyBitcodeModule(Buffer, Context,
|
|
|
|
/* ShouldLazyLoadMetadata */ true)
|
|
|
|
: parseBitcodeFile(Buffer, Context);
|
|
|
|
if (!ModuleOrErr) {
|
|
|
|
handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
|
|
|
|
SMDiagnostic Err = SMDiagnostic(Buffer.getBufferIdentifier(),
|
|
|
|
SourceMgr::DK_Error, EIB.message());
|
|
|
|
Err.print("ThinLTO", errs());
|
|
|
|
});
|
2016-05-24 06:54:06 +08:00
|
|
|
report_fatal_error("Can't load module, abort.");
|
|
|
|
}
|
|
|
|
return std::move(ModuleOrErr.get());
|
|
|
|
}
|
2016-05-25 22:03:11 +08:00
|
|
|
|
|
|
|
static void thinLTOResolveWeakForLinkerGUID(
|
|
|
|
GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
|
|
|
|
DenseSet<GlobalValueSummary *> &GlobalInvolvedWithAlias,
|
2016-06-13 00:13:55 +08:00
|
|
|
function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
|
2016-05-25 22:03:11 +08:00
|
|
|
isPrevailing,
|
2016-06-13 00:13:55 +08:00
|
|
|
function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
|
2016-05-25 22:03:11 +08:00
|
|
|
recordNewLinkage) {
|
|
|
|
for (auto &S : GVSummaryList) {
|
|
|
|
GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
|
|
|
|
if (!GlobalValue::isWeakForLinker(OriginalLinkage))
|
|
|
|
continue;
|
2016-07-08 02:31:51 +08:00
|
|
|
// We need to emit only one of these. The prevailing module will keep it,
|
2016-05-25 22:03:11 +08:00
|
|
|
// but turned into a weak, while the others will drop it when possible.
|
2016-10-30 13:15:23 +08:00
|
|
|
// This is both a compile-time optimization and a correctness
|
|
|
|
// transformation. This is necessary for correctness when we have exported
|
|
|
|
// a reference - we need to convert the linkonce to weak to
|
|
|
|
// ensure a copy is kept to satisfy the exported reference.
|
|
|
|
// FIXME: We may want to split the compile time and correctness
|
|
|
|
// aspects into separate routines.
|
2016-07-08 02:31:51 +08:00
|
|
|
if (isPrevailing(GUID, S.get())) {
|
2016-05-26 22:16:52 +08:00
|
|
|
if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
|
|
|
|
S->setLinkage(GlobalValue::getWeakLinkage(
|
|
|
|
GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
|
2016-05-25 22:03:11 +08:00
|
|
|
}
|
2016-10-30 13:15:23 +08:00
|
|
|
// Alias and aliasee can't be turned into available_externally.
|
2016-05-25 22:03:11 +08:00
|
|
|
else if (!isa<AliasSummary>(S.get()) &&
|
2016-10-30 13:15:23 +08:00
|
|
|
!GlobalInvolvedWithAlias.count(S.get()) &&
|
2016-05-25 22:03:11 +08:00
|
|
|
(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.
|
2016-08-11 22:58:12 +08:00
|
|
|
void llvm::thinLTOResolveWeakForLinkerInIndex(
|
2016-05-25 22:03:11 +08:00
|
|
|
ModuleSummaryIndex &Index,
|
2016-06-13 00:13:55 +08:00
|
|
|
function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
|
2016-05-25 22:03:11 +08:00
|
|
|
isPrevailing,
|
2016-06-13 00:13:55 +08:00
|
|
|
function_ref<void(StringRef, GlobalValue::GUID, GlobalValue::LinkageTypes)>
|
2016-05-25 22:03:11 +08:00
|
|
|
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,
|
2016-07-08 02:31:51 +08:00
|
|
|
isPrevailing, recordNewLinkage);
|
2016-05-25 22:03:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void thinLTOInternalizeAndPromoteGUID(
|
|
|
|
GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
|
2016-06-13 00:13:55 +08:00
|
|
|
function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
|
2016-05-25 22:03:11 +08:00
|
|
|
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.
|
2016-08-11 22:58:12 +08:00
|
|
|
void llvm::thinLTOInternalizeAndPromoteInIndex(
|
2016-05-25 22:03:11 +08:00
|
|
|
ModuleSummaryIndex &Index,
|
2016-06-13 00:13:55 +08:00
|
|
|
function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
|
2016-05-25 22:03:11 +08:00
|
|
|
for (auto &I : Index)
|
|
|
|
thinLTOInternalizeAndPromoteGUID(I.second, I.first, isExported);
|
|
|
|
}
|
2016-08-11 22:58:12 +08:00
|
|
|
|
|
|
|
Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) {
|
|
|
|
std::unique_ptr<InputFile> File(new InputFile);
|
2016-11-13 15:00:17 +08:00
|
|
|
|
|
|
|
Expected<std::unique_ptr<object::IRObjectFile>> IRObj =
|
2016-08-11 22:58:12 +08:00
|
|
|
IRObjectFile::create(Object, File->Ctx);
|
|
|
|
if (!IRObj)
|
2016-11-13 15:00:17 +08:00
|
|
|
return IRObj.takeError();
|
2016-08-11 22:58:12 +08:00
|
|
|
File->Obj = std::move(*IRObj);
|
|
|
|
|
2016-10-25 20:02:03 +08:00
|
|
|
for (const auto &C : File->Obj->getModule().getComdatSymbolTable()) {
|
|
|
|
auto P =
|
|
|
|
File->ComdatMap.insert(std::make_pair(&C.second, File->Comdats.size()));
|
|
|
|
assert(P.second);
|
2016-10-25 20:28:26 +08:00
|
|
|
(void)P;
|
2016-10-25 20:02:03 +08:00
|
|
|
File->Comdats.push_back(C.first());
|
|
|
|
}
|
|
|
|
|
2016-08-11 22:58:12 +08:00
|
|
|
return std::move(File);
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:02:03 +08:00
|
|
|
Expected<int> InputFile::Symbol::getComdatIndex() const {
|
|
|
|
if (!GV)
|
|
|
|
return -1;
|
|
|
|
const GlobalObject *GO;
|
|
|
|
if (auto *GA = dyn_cast<GlobalAlias>(GV)) {
|
|
|
|
GO = GA->getBaseObject();
|
|
|
|
if (!GO)
|
|
|
|
return make_error<StringError>("Unable to determine comdat of alias!",
|
|
|
|
inconvertibleErrorCode());
|
|
|
|
} else {
|
|
|
|
GO = cast<GlobalObject>(GV);
|
|
|
|
}
|
|
|
|
if (const Comdat *C = GO->getComdat()) {
|
|
|
|
auto I = File->ComdatMap.find(C);
|
|
|
|
assert(I != File->ComdatMap.end());
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-08-11 22:58:12 +08:00
|
|
|
LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
|
|
|
|
Config &Conf)
|
|
|
|
: ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
|
2016-08-24 02:39:12 +08:00
|
|
|
Ctx(Conf) {}
|
2016-08-11 22:58:12 +08:00
|
|
|
|
|
|
|
LTO::ThinLTOState::ThinLTOState(ThinBackend Backend) : Backend(Backend) {
|
|
|
|
if (!Backend)
|
2016-10-20 01:35:01 +08:00
|
|
|
this->Backend =
|
|
|
|
createInProcessThinBackend(llvm::heavyweight_hardware_concurrency());
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LTO::LTO(Config Conf, ThinBackend Backend,
|
|
|
|
unsigned ParallelCodeGenParallelismLevel)
|
|
|
|
: Conf(std::move(Conf)),
|
|
|
|
RegularLTO(ParallelCodeGenParallelismLevel, this->Conf),
|
2016-08-19 13:56:37 +08:00
|
|
|
ThinLTO(std::move(Backend)) {}
|
2016-08-11 22:58:12 +08:00
|
|
|
|
|
|
|
// Add the given symbol to the GlobalResolutions map, and resolve its partition.
|
|
|
|
void LTO::addSymbolToGlobalRes(IRObjectFile *Obj,
|
|
|
|
SmallPtrSet<GlobalValue *, 8> &Used,
|
|
|
|
const InputFile::Symbol &Sym,
|
2016-08-12 04:38:39 +08:00
|
|
|
SymbolResolution Res, unsigned Partition) {
|
2016-08-11 22:58:12 +08:00
|
|
|
GlobalValue *GV = Obj->getSymbolGV(Sym.I->getRawDataRefImpl());
|
|
|
|
|
|
|
|
auto &GlobalRes = GlobalResolutions[Sym.getName()];
|
|
|
|
if (GV) {
|
|
|
|
GlobalRes.UnnamedAddr &= GV->hasGlobalUnnamedAddr();
|
|
|
|
if (Res.Prevailing)
|
|
|
|
GlobalRes.IRName = GV->getName();
|
|
|
|
}
|
|
|
|
if (Res.VisibleToRegularObj || (GV && Used.count(GV)) ||
|
|
|
|
(GlobalRes.Partition != GlobalResolution::Unknown &&
|
|
|
|
GlobalRes.Partition != Partition))
|
|
|
|
GlobalRes.Partition = GlobalResolution::External;
|
|
|
|
else
|
|
|
|
GlobalRes.Partition = Partition;
|
|
|
|
}
|
|
|
|
|
2016-08-27 04:19:35 +08:00
|
|
|
static void writeToResolutionFile(raw_ostream &OS, InputFile *Input,
|
|
|
|
ArrayRef<SymbolResolution> Res) {
|
|
|
|
StringRef Path = Input->getMemoryBufferRef().getBufferIdentifier();
|
|
|
|
OS << Path << '\n';
|
2016-08-11 22:58:12 +08:00
|
|
|
auto ResI = Res.begin();
|
|
|
|
for (const InputFile::Symbol &Sym : Input->symbols()) {
|
|
|
|
assert(ResI != Res.end());
|
|
|
|
SymbolResolution Res = *ResI++;
|
|
|
|
|
2016-08-27 04:19:35 +08:00
|
|
|
OS << "-r=" << Path << ',' << Sym.getName() << ',';
|
2016-08-11 22:58:12 +08:00
|
|
|
if (Res.Prevailing)
|
2016-08-27 04:19:35 +08:00
|
|
|
OS << 'p';
|
2016-08-11 22:58:12 +08:00
|
|
|
if (Res.FinalDefinitionInLinkageUnit)
|
2016-08-27 04:19:35 +08:00
|
|
|
OS << 'l';
|
2016-08-11 22:58:12 +08:00
|
|
|
if (Res.VisibleToRegularObj)
|
2016-08-27 04:19:35 +08:00
|
|
|
OS << 'x';
|
|
|
|
OS << '\n';
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
assert(ResI == Res.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
Error LTO::add(std::unique_ptr<InputFile> Input,
|
|
|
|
ArrayRef<SymbolResolution> Res) {
|
|
|
|
assert(!CalledGetMaxTasks);
|
|
|
|
|
|
|
|
if (Conf.ResolutionFile)
|
2016-08-27 04:19:35 +08:00
|
|
|
writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res);
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2016-08-19 23:35:44 +08:00
|
|
|
// FIXME: move to backend
|
2016-08-11 22:58:12 +08:00
|
|
|
Module &M = Input->Obj->getModule();
|
|
|
|
if (!Conf.OverrideTriple.empty())
|
|
|
|
M.setTargetTriple(Conf.OverrideTriple);
|
|
|
|
else if (M.getTargetTriple().empty())
|
|
|
|
M.setTargetTriple(Conf.DefaultTriple);
|
|
|
|
|
|
|
|
MemoryBufferRef MBRef = Input->Obj->getMemoryBufferRef();
|
2016-11-12 03:50:24 +08:00
|
|
|
Expected<bool> HasThinLTOSummary = hasGlobalValueSummary(MBRef);
|
|
|
|
if (!HasThinLTOSummary)
|
|
|
|
return HasThinLTOSummary.takeError();
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2016-11-12 03:50:24 +08:00
|
|
|
if (*HasThinLTOSummary)
|
2016-08-11 22:58:12 +08:00
|
|
|
return addThinLTO(std::move(Input), Res);
|
|
|
|
else
|
|
|
|
return addRegularLTO(std::move(Input), Res);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a regular LTO object to the link.
|
|
|
|
Error LTO::addRegularLTO(std::unique_ptr<InputFile> Input,
|
|
|
|
ArrayRef<SymbolResolution> Res) {
|
2016-08-24 02:39:12 +08:00
|
|
|
if (!RegularLTO.CombinedModule) {
|
|
|
|
RegularLTO.CombinedModule =
|
|
|
|
llvm::make_unique<Module>("ld-temp.o", RegularLTO.Ctx);
|
|
|
|
RegularLTO.Mover = llvm::make_unique<IRMover>(*RegularLTO.CombinedModule);
|
|
|
|
}
|
2016-11-13 15:00:17 +08:00
|
|
|
Expected<std::unique_ptr<object::IRObjectFile>> ObjOrErr =
|
2016-08-11 22:58:12 +08:00
|
|
|
IRObjectFile::create(Input->Obj->getMemoryBufferRef(), RegularLTO.Ctx);
|
|
|
|
if (!ObjOrErr)
|
2016-11-13 15:00:17 +08:00
|
|
|
return ObjOrErr.takeError();
|
2016-08-11 22:58:12 +08:00
|
|
|
std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr);
|
|
|
|
|
|
|
|
Module &M = Obj->getModule();
|
2016-11-10 01:49:19 +08:00
|
|
|
if (Error Err = M.materializeMetadata())
|
|
|
|
return Err;
|
2016-08-11 22:58:12 +08:00
|
|
|
UpgradeDebugInfo(M);
|
|
|
|
|
|
|
|
SmallPtrSet<GlobalValue *, 8> Used;
|
|
|
|
collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
|
|
|
|
|
|
|
|
std::vector<GlobalValue *> Keep;
|
|
|
|
|
|
|
|
for (GlobalVariable &GV : M.globals())
|
|
|
|
if (GV.hasAppendingLinkage())
|
|
|
|
Keep.push_back(&GV);
|
|
|
|
|
|
|
|
auto ResI = Res.begin();
|
|
|
|
for (const InputFile::Symbol &Sym :
|
2016-10-25 20:02:03 +08:00
|
|
|
make_range(InputFile::symbol_iterator(Obj->symbol_begin(), nullptr),
|
|
|
|
InputFile::symbol_iterator(Obj->symbol_end(), nullptr))) {
|
2016-08-11 22:58:12 +08:00
|
|
|
assert(ResI != Res.end());
|
|
|
|
SymbolResolution Res = *ResI++;
|
|
|
|
addSymbolToGlobalRes(Obj.get(), Used, Sym, Res, 0);
|
|
|
|
|
|
|
|
GlobalValue *GV = Obj->getSymbolGV(Sym.I->getRawDataRefImpl());
|
2016-09-14 02:45:13 +08:00
|
|
|
if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined)
|
|
|
|
continue;
|
2016-08-11 22:58:12 +08:00
|
|
|
if (Res.Prevailing && GV) {
|
|
|
|
Keep.push_back(GV);
|
|
|
|
switch (GV->getLinkage()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case GlobalValue::LinkOnceAnyLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::WeakAnyLinkage);
|
|
|
|
break;
|
|
|
|
case GlobalValue::LinkOnceODRLinkage:
|
|
|
|
GV->setLinkage(GlobalValue::WeakODRLinkage);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 05:05:04 +08:00
|
|
|
// Common resolution: collect the maximum size/alignment over all commons.
|
|
|
|
// We also record if we see an instance of a common as prevailing, so that
|
|
|
|
// if none is prevailing we can ignore it later.
|
2016-08-22 14:25:46 +08:00
|
|
|
if (Sym.getFlags() & object::BasicSymbolRef::SF_Common) {
|
|
|
|
auto &CommonRes = RegularLTO.Commons[Sym.getIRName()];
|
|
|
|
CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize());
|
|
|
|
CommonRes.Align = std::max(CommonRes.Align, Sym.getCommonAlignment());
|
2016-09-15 05:05:04 +08:00
|
|
|
CommonRes.Prevailing |= Res.Prevailing;
|
2016-08-22 14:25:46 +08:00
|
|
|
}
|
2016-08-11 22:58:12 +08:00
|
|
|
|
|
|
|
// FIXME: use proposed local attribute for FinalDefinitionInLinkageUnit.
|
|
|
|
}
|
|
|
|
assert(ResI == Res.end());
|
|
|
|
|
2016-08-24 02:39:12 +08:00
|
|
|
return RegularLTO.Mover->move(Obj->takeModule(), Keep,
|
2016-10-13 02:39:29 +08:00
|
|
|
[](GlobalValue &, IRMover::ValueAdder) {},
|
|
|
|
/* LinkModuleInlineAsm */ true);
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a ThinLTO object to the link.
|
|
|
|
Error LTO::addThinLTO(std::unique_ptr<InputFile> Input,
|
|
|
|
ArrayRef<SymbolResolution> Res) {
|
|
|
|
Module &M = Input->Obj->getModule();
|
|
|
|
SmallPtrSet<GlobalValue *, 8> Used;
|
|
|
|
collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
|
|
|
|
|
|
|
|
MemoryBufferRef MBRef = Input->Obj->getMemoryBufferRef();
|
2016-11-12 03:50:39 +08:00
|
|
|
Expected<std::unique_ptr<object::ModuleSummaryIndexObjectFile>>
|
|
|
|
SummaryObjOrErr = object::ModuleSummaryIndexObjectFile::create(MBRef);
|
2016-08-11 22:58:12 +08:00
|
|
|
if (!SummaryObjOrErr)
|
2016-11-12 03:50:39 +08:00
|
|
|
return SummaryObjOrErr.takeError();
|
2016-08-11 22:58:12 +08:00
|
|
|
ThinLTO.CombinedIndex.mergeFrom((*SummaryObjOrErr)->takeIndex(),
|
|
|
|
ThinLTO.ModuleMap.size());
|
|
|
|
|
|
|
|
auto ResI = Res.begin();
|
|
|
|
for (const InputFile::Symbol &Sym : Input->symbols()) {
|
|
|
|
assert(ResI != Res.end());
|
|
|
|
SymbolResolution Res = *ResI++;
|
|
|
|
addSymbolToGlobalRes(Input->Obj.get(), Used, Sym, Res,
|
|
|
|
ThinLTO.ModuleMap.size() + 1);
|
|
|
|
|
|
|
|
GlobalValue *GV = Input->Obj->getSymbolGV(Sym.I->getRawDataRefImpl());
|
|
|
|
if (Res.Prevailing && GV)
|
|
|
|
ThinLTO.PrevailingModuleForGUID[GV->getGUID()] =
|
|
|
|
MBRef.getBufferIdentifier();
|
|
|
|
}
|
|
|
|
assert(ResI == Res.end());
|
|
|
|
|
|
|
|
ThinLTO.ModuleMap[MBRef.getBufferIdentifier()] = MBRef;
|
2016-11-11 12:28:40 +08:00
|
|
|
return Error::success();
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
|
2016-08-12 04:38:39 +08:00
|
|
|
unsigned LTO::getMaxTasks() const {
|
2016-08-11 22:58:12 +08:00
|
|
|
CalledGetMaxTasks = true;
|
|
|
|
return RegularLTO.ParallelCodeGenParallelismLevel + ThinLTO.ModuleMap.size();
|
|
|
|
}
|
|
|
|
|
2016-09-24 05:33:43 +08:00
|
|
|
Error LTO::run(AddStreamFn AddStream, NativeObjectCache Cache) {
|
2016-09-16 21:54:19 +08:00
|
|
|
// Save the status of having a regularLTO combined module, as
|
|
|
|
// this is needed for generating the ThinLTO Task ID, and
|
|
|
|
// the CombinedModule will be moved at the end of runRegularLTO.
|
|
|
|
bool HasRegularLTO = RegularLTO.CombinedModule != nullptr;
|
2016-08-24 02:39:12 +08:00
|
|
|
// Invoke regular LTO if there was a regular LTO module to start with.
|
2016-09-16 21:54:19 +08:00
|
|
|
if (HasRegularLTO)
|
2016-09-24 05:33:43 +08:00
|
|
|
if (auto E = runRegularLTO(AddStream))
|
2016-08-11 22:58:12 +08:00
|
|
|
return E;
|
2016-09-24 05:33:43 +08:00
|
|
|
return runThinLTO(AddStream, Cache, HasRegularLTO);
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
|
2016-09-24 05:33:43 +08:00
|
|
|
Error LTO::runRegularLTO(AddStreamFn AddStream) {
|
2016-08-22 14:25:46 +08:00
|
|
|
// Make sure commons have the right size/alignment: we kept the largest from
|
|
|
|
// all the prevailing when adding the inputs, and we apply it here.
|
2016-08-27 12:41:22 +08:00
|
|
|
const DataLayout &DL = RegularLTO.CombinedModule->getDataLayout();
|
2016-08-22 14:25:46 +08:00
|
|
|
for (auto &I : RegularLTO.Commons) {
|
2016-09-15 05:05:04 +08:00
|
|
|
if (!I.second.Prevailing)
|
|
|
|
// Don't do anything if no instance of this common was prevailing.
|
|
|
|
continue;
|
2016-08-22 14:25:46 +08:00
|
|
|
GlobalVariable *OldGV = RegularLTO.CombinedModule->getNamedGlobal(I.first);
|
2016-08-27 12:41:22 +08:00
|
|
|
if (OldGV && DL.getTypeAllocSize(OldGV->getValueType()) == I.second.Size) {
|
2016-08-22 14:25:46 +08:00
|
|
|
// Don't create a new global if the type is already correct, just make
|
|
|
|
// sure the alignment is correct.
|
|
|
|
OldGV->setAlignment(I.second.Align);
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-27 12:41:22 +08:00
|
|
|
ArrayType *Ty =
|
|
|
|
ArrayType::get(Type::getInt8Ty(RegularLTO.Ctx), I.second.Size);
|
2016-08-22 14:25:46 +08:00
|
|
|
auto *GV = new GlobalVariable(*RegularLTO.CombinedModule, Ty, false,
|
|
|
|
GlobalValue::CommonLinkage,
|
|
|
|
ConstantAggregateZero::get(Ty), "");
|
|
|
|
GV->setAlignment(I.second.Align);
|
|
|
|
if (OldGV) {
|
|
|
|
OldGV->replaceAllUsesWith(ConstantExpr::getBitCast(GV, OldGV->getType()));
|
|
|
|
GV->takeName(OldGV);
|
|
|
|
OldGV->eraseFromParent();
|
|
|
|
} else {
|
|
|
|
GV->setName(I.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-11 22:58:12 +08:00
|
|
|
if (Conf.PreOptModuleHook &&
|
|
|
|
!Conf.PreOptModuleHook(0, *RegularLTO.CombinedModule))
|
2016-11-11 12:28:40 +08:00
|
|
|
return Error::success();
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2016-08-22 14:25:41 +08:00
|
|
|
if (!Conf.CodeGenOnly) {
|
|
|
|
for (const auto &R : GlobalResolutions) {
|
|
|
|
if (R.second.IRName.empty())
|
|
|
|
continue;
|
|
|
|
if (R.second.Partition != 0 &&
|
|
|
|
R.second.Partition != GlobalResolution::External)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
GlobalValue *GV =
|
|
|
|
RegularLTO.CombinedModule->getNamedValue(R.second.IRName);
|
|
|
|
// Ignore symbols defined in other partitions.
|
|
|
|
if (!GV || GV->hasLocalLinkage())
|
|
|
|
continue;
|
|
|
|
GV->setUnnamedAddr(R.second.UnnamedAddr ? GlobalValue::UnnamedAddr::Global
|
|
|
|
: GlobalValue::UnnamedAddr::None);
|
|
|
|
if (R.second.Partition == 0)
|
|
|
|
GV->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
}
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2016-08-22 14:25:41 +08:00
|
|
|
if (Conf.PostInternalizeModuleHook &&
|
|
|
|
!Conf.PostInternalizeModuleHook(0, *RegularLTO.CombinedModule))
|
2016-11-11 12:28:40 +08:00
|
|
|
return Error::success();
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
2016-09-24 05:33:43 +08:00
|
|
|
return backend(Conf, AddStream, RegularLTO.ParallelCodeGenParallelismLevel,
|
2016-08-11 22:58:12 +08:00
|
|
|
std::move(RegularLTO.CombinedModule));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This class defines the interface to the ThinLTO backend.
|
|
|
|
class lto::ThinBackendProc {
|
|
|
|
protected:
|
|
|
|
Config &Conf;
|
|
|
|
ModuleSummaryIndex &CombinedIndex;
|
2016-09-06 11:23:45 +08:00
|
|
|
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries;
|
2016-08-11 22:58:12 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
ThinBackendProc(Config &Conf, ModuleSummaryIndex &CombinedIndex,
|
2016-09-06 11:23:45 +08:00
|
|
|
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries)
|
2016-08-19 14:10:03 +08:00
|
|
|
: Conf(Conf), CombinedIndex(CombinedIndex),
|
2016-08-11 22:58:12 +08:00
|
|
|
ModuleToDefinedGVSummaries(ModuleToDefinedGVSummaries) {}
|
|
|
|
|
|
|
|
virtual ~ThinBackendProc() {}
|
2016-08-24 05:30:12 +08:00
|
|
|
virtual Error start(
|
|
|
|
unsigned Task, MemoryBufferRef MBRef,
|
|
|
|
const FunctionImporter::ImportMapTy &ImportList,
|
|
|
|
const FunctionImporter::ExportSetTy &ExportList,
|
|
|
|
const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
|
|
|
|
MapVector<StringRef, MemoryBufferRef> &ModuleMap) = 0;
|
2016-08-11 22:58:12 +08:00
|
|
|
virtual Error wait() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class InProcessThinBackend : public ThinBackendProc {
|
|
|
|
ThreadPool BackendThreadPool;
|
2016-09-24 05:33:43 +08:00
|
|
|
AddStreamFn AddStream;
|
|
|
|
NativeObjectCache Cache;
|
2016-08-11 22:58:12 +08:00
|
|
|
|
|
|
|
Optional<Error> Err;
|
|
|
|
std::mutex ErrMu;
|
|
|
|
|
|
|
|
public:
|
2016-09-06 11:23:45 +08:00
|
|
|
InProcessThinBackend(
|
|
|
|
Config &Conf, ModuleSummaryIndex &CombinedIndex,
|
|
|
|
unsigned ThinLTOParallelismLevel,
|
|
|
|
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
|
2016-09-24 05:33:43 +08:00
|
|
|
AddStreamFn AddStream, NativeObjectCache Cache)
|
2016-08-19 14:10:03 +08:00
|
|
|
: ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries),
|
|
|
|
BackendThreadPool(ThinLTOParallelismLevel),
|
2016-09-24 05:33:43 +08:00
|
|
|
AddStream(std::move(AddStream)), Cache(std::move(Cache)) {}
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2016-08-24 05:30:12 +08:00
|
|
|
Error runThinLTOBackendThread(
|
2016-09-24 05:33:43 +08:00
|
|
|
AddStreamFn AddStream, NativeObjectCache Cache, unsigned Task,
|
|
|
|
MemoryBufferRef MBRef, ModuleSummaryIndex &CombinedIndex,
|
2016-08-24 05:30:12 +08:00
|
|
|
const FunctionImporter::ImportMapTy &ImportList,
|
|
|
|
const FunctionImporter::ExportSetTy &ExportList,
|
|
|
|
const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
|
|
|
|
const GVSummaryMapTy &DefinedGlobals,
|
|
|
|
MapVector<StringRef, MemoryBufferRef> &ModuleMap) {
|
2016-09-24 05:33:43 +08:00
|
|
|
auto RunThinBackend = [&](AddStreamFn AddStream) {
|
|
|
|
LTOLLVMContext BackendContext(Conf);
|
2016-11-13 15:00:17 +08:00
|
|
|
Expected<std::unique_ptr<Module>> MOrErr =
|
2016-09-24 05:33:43 +08:00
|
|
|
parseBitcodeFile(MBRef, BackendContext);
|
2016-11-13 15:00:17 +08:00
|
|
|
if (!MOrErr)
|
|
|
|
return MOrErr.takeError();
|
2016-09-24 05:33:43 +08:00
|
|
|
|
|
|
|
return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex,
|
|
|
|
ImportList, DefinedGlobals, ModuleMap);
|
|
|
|
};
|
2016-08-24 05:30:12 +08:00
|
|
|
|
2016-10-08 12:44:18 +08:00
|
|
|
auto ModuleID = MBRef.getBufferIdentifier();
|
2016-10-08 12:44:23 +08:00
|
|
|
|
|
|
|
if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) ||
|
|
|
|
all_of(CombinedIndex.getModuleHash(ModuleID),
|
|
|
|
[](uint32_t V) { return V == 0; }))
|
|
|
|
// Cache disabled or no entry for this module in the combined index or
|
|
|
|
// no module hash.
|
2016-09-24 05:33:43 +08:00
|
|
|
return RunThinBackend(AddStream);
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2016-09-24 05:33:43 +08:00
|
|
|
SmallString<40> Key;
|
|
|
|
// The module may be cached, this helps handling it.
|
2016-10-08 12:44:18 +08:00
|
|
|
computeCacheKey(Key, CombinedIndex, ModuleID, ImportList, ExportList,
|
|
|
|
ResolvedODR, DefinedGlobals);
|
2016-09-24 05:33:43 +08:00
|
|
|
if (AddStreamFn CacheAddStream = Cache(Task, Key))
|
|
|
|
return RunThinBackend(CacheAddStream);
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2016-11-11 12:28:40 +08:00
|
|
|
return Error::success();
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
|
2016-08-24 05:30:12 +08:00
|
|
|
Error start(
|
|
|
|
unsigned Task, MemoryBufferRef MBRef,
|
|
|
|
const FunctionImporter::ImportMapTy &ImportList,
|
|
|
|
const FunctionImporter::ExportSetTy &ExportList,
|
|
|
|
const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
|
|
|
|
MapVector<StringRef, MemoryBufferRef> &ModuleMap) override {
|
2016-08-11 22:58:12 +08:00
|
|
|
StringRef ModulePath = MBRef.getBufferIdentifier();
|
2016-09-06 11:23:45 +08:00
|
|
|
assert(ModuleToDefinedGVSummaries.count(ModulePath));
|
|
|
|
const GVSummaryMapTy &DefinedGlobals =
|
|
|
|
ModuleToDefinedGVSummaries.find(ModulePath)->second;
|
2016-08-11 22:58:12 +08:00
|
|
|
BackendThreadPool.async(
|
|
|
|
[=](MemoryBufferRef MBRef, ModuleSummaryIndex &CombinedIndex,
|
|
|
|
const FunctionImporter::ImportMapTy &ImportList,
|
2016-08-24 05:30:12 +08:00
|
|
|
const FunctionImporter::ExportSetTy &ExportList,
|
|
|
|
const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>
|
|
|
|
&ResolvedODR,
|
2016-09-06 11:23:45 +08:00
|
|
|
const GVSummaryMapTy &DefinedGlobals,
|
2016-08-11 22:58:12 +08:00
|
|
|
MapVector<StringRef, MemoryBufferRef> &ModuleMap) {
|
2016-08-24 05:30:12 +08:00
|
|
|
Error E = runThinLTOBackendThread(
|
2016-09-24 05:33:43 +08:00
|
|
|
AddStream, Cache, Task, MBRef, CombinedIndex, ImportList,
|
|
|
|
ExportList, ResolvedODR, DefinedGlobals, ModuleMap);
|
2016-08-11 22:58:12 +08:00
|
|
|
if (E) {
|
|
|
|
std::unique_lock<std::mutex> L(ErrMu);
|
|
|
|
if (Err)
|
|
|
|
Err = joinErrors(std::move(*Err), std::move(E));
|
|
|
|
else
|
|
|
|
Err = std::move(E);
|
|
|
|
}
|
|
|
|
},
|
2016-08-16 13:46:05 +08:00
|
|
|
MBRef, std::ref(CombinedIndex), std::ref(ImportList),
|
2016-09-06 11:23:45 +08:00
|
|
|
std::ref(ExportList), std::ref(ResolvedODR), std::ref(DefinedGlobals),
|
|
|
|
std::ref(ModuleMap));
|
2016-11-11 12:28:40 +08:00
|
|
|
return Error::success();
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Error wait() override {
|
|
|
|
BackendThreadPool.wait();
|
|
|
|
if (Err)
|
|
|
|
return std::move(*Err);
|
|
|
|
else
|
2016-11-11 12:28:40 +08:00
|
|
|
return Error::success();
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ThinBackend lto::createInProcessThinBackend(unsigned ParallelismLevel) {
|
|
|
|
return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex,
|
2016-09-06 11:23:45 +08:00
|
|
|
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
|
2016-09-24 05:33:43 +08:00
|
|
|
AddStreamFn AddStream, NativeObjectCache Cache) {
|
2016-08-11 22:58:12 +08:00
|
|
|
return llvm::make_unique<InProcessThinBackend>(
|
|
|
|
Conf, CombinedIndex, ParallelismLevel, ModuleToDefinedGVSummaries,
|
2016-09-24 05:33:43 +08:00
|
|
|
AddStream, Cache);
|
2016-08-11 22:58:12 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-22 03:12:05 +08:00
|
|
|
// Given the original \p Path to an output file, replace any path
|
|
|
|
// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
|
|
|
|
// resulting directory if it does not yet exist.
|
|
|
|
std::string lto::getThinLTOOutputFile(const std::string &Path,
|
|
|
|
const std::string &OldPrefix,
|
|
|
|
const std::string &NewPrefix) {
|
|
|
|
if (OldPrefix.empty() && NewPrefix.empty())
|
|
|
|
return Path;
|
|
|
|
SmallString<128> NewPath(Path);
|
|
|
|
llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
|
|
|
|
StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
|
|
|
|
if (!ParentPath.empty()) {
|
|
|
|
// Make sure the new directory exists, creating it if necessary.
|
|
|
|
if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
|
|
|
|
llvm::errs() << "warning: could not create directory '" << ParentPath
|
|
|
|
<< "': " << EC.message() << '\n';
|
|
|
|
}
|
|
|
|
return NewPath.str();
|
|
|
|
}
|
|
|
|
|
2016-08-11 22:58:12 +08:00
|
|
|
class WriteIndexesThinBackend : public ThinBackendProc {
|
|
|
|
std::string OldPrefix, NewPrefix;
|
|
|
|
bool ShouldEmitImportsFiles;
|
|
|
|
|
|
|
|
std::string LinkedObjectsFileName;
|
|
|
|
std::unique_ptr<llvm::raw_fd_ostream> LinkedObjectsFile;
|
|
|
|
|
|
|
|
public:
|
2016-09-06 11:23:45 +08:00
|
|
|
WriteIndexesThinBackend(
|
|
|
|
Config &Conf, ModuleSummaryIndex &CombinedIndex,
|
|
|
|
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
|
|
|
|
std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
|
|
|
|
std::string LinkedObjectsFileName)
|
2016-08-19 14:10:03 +08:00
|
|
|
: ThinBackendProc(Conf, CombinedIndex, ModuleToDefinedGVSummaries),
|
2016-08-11 22:58:12 +08:00
|
|
|
OldPrefix(OldPrefix), NewPrefix(NewPrefix),
|
|
|
|
ShouldEmitImportsFiles(ShouldEmitImportsFiles),
|
|
|
|
LinkedObjectsFileName(LinkedObjectsFileName) {}
|
|
|
|
|
2016-08-24 05:30:12 +08:00
|
|
|
Error start(
|
|
|
|
unsigned Task, MemoryBufferRef MBRef,
|
|
|
|
const FunctionImporter::ImportMapTy &ImportList,
|
|
|
|
const FunctionImporter::ExportSetTy &ExportList,
|
|
|
|
const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
|
|
|
|
MapVector<StringRef, MemoryBufferRef> &ModuleMap) override {
|
2016-08-11 22:58:12 +08:00
|
|
|
StringRef ModulePath = MBRef.getBufferIdentifier();
|
|
|
|
std::string NewModulePath =
|
|
|
|
getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
|
|
|
|
|
|
|
|
std::error_code EC;
|
|
|
|
if (!LinkedObjectsFileName.empty()) {
|
|
|
|
if (!LinkedObjectsFile) {
|
|
|
|
LinkedObjectsFile = llvm::make_unique<raw_fd_ostream>(
|
|
|
|
LinkedObjectsFileName, EC, sys::fs::OpenFlags::F_None);
|
|
|
|
if (EC)
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
}
|
|
|
|
*LinkedObjectsFile << NewModulePath << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
|
|
|
|
gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
|
2016-08-16 13:46:05 +08:00
|
|
|
ImportList, ModuleToSummariesForIndex);
|
2016-08-11 22:58:12 +08:00
|
|
|
|
|
|
|
raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
|
|
|
|
sys::fs::OpenFlags::F_None);
|
|
|
|
if (EC)
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
WriteIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex);
|
|
|
|
|
|
|
|
if (ShouldEmitImportsFiles)
|
2016-08-16 13:46:05 +08:00
|
|
|
return errorCodeToError(
|
|
|
|
EmitImportsFiles(ModulePath, NewModulePath + ".imports", ImportList));
|
2016-11-11 12:28:40 +08:00
|
|
|
return Error::success();
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
|
2016-11-11 12:28:40 +08:00
|
|
|
Error wait() override { return Error::success(); }
|
2016-08-11 22:58:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ThinBackend lto::createWriteIndexesThinBackend(std::string OldPrefix,
|
|
|
|
std::string NewPrefix,
|
|
|
|
bool ShouldEmitImportsFiles,
|
|
|
|
std::string LinkedObjectsFile) {
|
|
|
|
return [=](Config &Conf, ModuleSummaryIndex &CombinedIndex,
|
2016-09-06 11:23:45 +08:00
|
|
|
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
|
2016-09-24 05:33:43 +08:00
|
|
|
AddStreamFn AddStream, NativeObjectCache Cache) {
|
2016-08-11 22:58:12 +08:00
|
|
|
return llvm::make_unique<WriteIndexesThinBackend>(
|
2016-08-19 14:10:03 +08:00
|
|
|
Conf, CombinedIndex, ModuleToDefinedGVSummaries, OldPrefix, NewPrefix,
|
|
|
|
ShouldEmitImportsFiles, LinkedObjectsFile);
|
2016-08-11 22:58:12 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-24 05:33:43 +08:00
|
|
|
Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache,
|
|
|
|
bool HasRegularLTO) {
|
2016-08-11 22:58:12 +08:00
|
|
|
if (ThinLTO.ModuleMap.empty())
|
2016-11-11 12:28:40 +08:00
|
|
|
return Error::success();
|
2016-08-11 22:58:12 +08:00
|
|
|
|
|
|
|
if (Conf.CombinedIndexHook && !Conf.CombinedIndexHook(ThinLTO.CombinedIndex))
|
2016-11-11 12:28:40 +08:00
|
|
|
return Error::success();
|
2016-08-11 22:58:12 +08:00
|
|
|
|
|
|
|
// Collect for each module the list of function it defines (GUID ->
|
|
|
|
// Summary).
|
|
|
|
StringMap<std::map<GlobalValue::GUID, GlobalValueSummary *>>
|
|
|
|
ModuleToDefinedGVSummaries(ThinLTO.ModuleMap.size());
|
|
|
|
ThinLTO.CombinedIndex.collectDefinedGVSummariesPerModule(
|
|
|
|
ModuleToDefinedGVSummaries);
|
2016-09-21 07:07:17 +08:00
|
|
|
// Create entries for any modules that didn't have any GV summaries
|
|
|
|
// (either they didn't have any GVs to start with, or we suppressed
|
|
|
|
// generation of the summaries because they e.g. had inline assembly
|
|
|
|
// uses that couldn't be promoted/renamed on export). This is so
|
|
|
|
// InProcessThinBackend::start can still launch a backend thread, which
|
|
|
|
// is passed the map of summaries for the module, without any special
|
|
|
|
// handling for this case.
|
|
|
|
for (auto &Mod : ThinLTO.ModuleMap)
|
|
|
|
if (!ModuleToDefinedGVSummaries.count(Mod.first))
|
|
|
|
ModuleToDefinedGVSummaries.try_emplace(Mod.first);
|
2016-08-11 22:58:12 +08:00
|
|
|
|
|
|
|
StringMap<FunctionImporter::ImportMapTy> ImportLists(
|
|
|
|
ThinLTO.ModuleMap.size());
|
|
|
|
StringMap<FunctionImporter::ExportSetTy> ExportLists(
|
|
|
|
ThinLTO.ModuleMap.size());
|
2016-11-01 06:12:21 +08:00
|
|
|
StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2016-11-01 06:12:21 +08:00
|
|
|
if (Conf.OptLevel > 0) {
|
|
|
|
ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
|
|
|
|
ImportLists, ExportLists);
|
2016-08-24 05:30:12 +08:00
|
|
|
|
2016-11-01 06:12:21 +08:00
|
|
|
std::set<GlobalValue::GUID> ExportedGUIDs;
|
|
|
|
for (auto &Res : GlobalResolutions) {
|
|
|
|
if (!Res.second.IRName.empty() &&
|
|
|
|
Res.second.Partition == GlobalResolution::External)
|
|
|
|
ExportedGUIDs.insert(GlobalValue::getGUID(Res.second.IRName));
|
|
|
|
}
|
2016-08-24 05:30:12 +08:00
|
|
|
|
2016-11-01 06:12:21 +08:00
|
|
|
auto isPrevailing = [&](GlobalValue::GUID GUID,
|
|
|
|
const GlobalValueSummary *S) {
|
|
|
|
return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath();
|
|
|
|
};
|
|
|
|
auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) {
|
|
|
|
const auto &ExportList = ExportLists.find(ModuleIdentifier);
|
|
|
|
return (ExportList != ExportLists.end() &&
|
|
|
|
ExportList->second.count(GUID)) ||
|
|
|
|
ExportedGUIDs.count(GUID);
|
|
|
|
};
|
|
|
|
thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported);
|
|
|
|
|
|
|
|
auto recordNewLinkage = [&](StringRef ModuleIdentifier,
|
|
|
|
GlobalValue::GUID GUID,
|
|
|
|
GlobalValue::LinkageTypes NewLinkage) {
|
|
|
|
ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
|
|
|
|
};
|
|
|
|
|
|
|
|
thinLTOResolveWeakForLinkerInIndex(ThinLTO.CombinedIndex, isPrevailing,
|
|
|
|
recordNewLinkage);
|
|
|
|
}
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2016-09-24 05:33:43 +08:00
|
|
|
std::unique_ptr<ThinBackendProc> BackendProc =
|
|
|
|
ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
|
|
|
|
AddStream, Cache);
|
2016-08-11 22:58:12 +08:00
|
|
|
|
|
|
|
// Partition numbers for ThinLTO jobs start at 1 (see comments for
|
|
|
|
// GlobalResolution in LTO.h). Task numbers, however, start at
|
2016-08-24 05:30:12 +08:00
|
|
|
// ParallelCodeGenParallelismLevel if an LTO module is present, as tasks 0
|
|
|
|
// through ParallelCodeGenParallelismLevel-1 are reserved for parallel code
|
|
|
|
// generation partitions.
|
2016-09-16 21:54:19 +08:00
|
|
|
unsigned Task =
|
|
|
|
HasRegularLTO ? RegularLTO.ParallelCodeGenParallelismLevel : 0;
|
2016-08-12 04:38:39 +08:00
|
|
|
unsigned Partition = 1;
|
2016-08-11 22:58:12 +08:00
|
|
|
|
|
|
|
for (auto &Mod : ThinLTO.ModuleMap) {
|
2016-08-16 13:46:05 +08:00
|
|
|
if (Error E = BackendProc->start(Task, Mod.second, ImportLists[Mod.first],
|
2016-08-24 05:30:12 +08:00
|
|
|
ExportLists[Mod.first],
|
|
|
|
ResolvedODR[Mod.first], ThinLTO.ModuleMap))
|
2016-08-11 22:58:12 +08:00
|
|
|
return E;
|
|
|
|
|
|
|
|
++Task;
|
|
|
|
++Partition;
|
|
|
|
}
|
|
|
|
|
|
|
|
return BackendProc->wait();
|
2016-05-24 06:54:06 +08:00
|
|
|
}
|