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"
|
2017-02-03 07:00:49 +08:00
|
|
|
#include "llvm/IR/Mangler.h"
|
|
|
|
#include "llvm/IR/Metadata.h"
|
2016-08-11 22:58:12 +08:00
|
|
|
#include "llvm/LTO/LTOBackend.h"
|
|
|
|
#include "llvm/Linker/IRMover.h"
|
2017-03-29 07:35:34 +08:00
|
|
|
#include "llvm/Object/IRObjectFile.h"
|
2017-02-03 07:00:49 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2016-08-11 22:58:12 +08:00
|
|
|
#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"
|
2017-04-13 09:26:12 +08:00
|
|
|
#include "llvm/Support/VCSRevision.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"
|
|
|
|
|
2018-02-19 23:14:50 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
DumpThinCGSCCs("dump-thin-cg-sccs", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Dump the SCCs in the ThinLTO index's callgraph"));
|
|
|
|
|
2017-03-11 05:35:17 +08:00
|
|
|
// The values are (type identifier, summary) pairs.
|
|
|
|
typedef DenseMap<
|
|
|
|
GlobalValue::GUID,
|
|
|
|
TinyPtrVector<const std::pair<const std::string, TypeIdSummary> *>>
|
|
|
|
TypeIdSummariesByGuidTy;
|
|
|
|
|
2016-08-24 05:30:12 +08:00
|
|
|
// 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(
|
2016-12-08 13:28:30 +08:00
|
|
|
SmallString<40> &Key, const Config &Conf, const ModuleSummaryIndex &Index,
|
|
|
|
StringRef ModuleID, const FunctionImporter::ImportMapTy &ImportList,
|
2016-08-24 05:30:12 +08:00
|
|
|
const FunctionImporter::ExportSetTy &ExportList,
|
|
|
|
const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
|
2017-03-11 05:35:17 +08:00
|
|
|
const GVSummaryMapTy &DefinedGlobals,
|
2017-08-10 07:24:07 +08:00
|
|
|
const TypeIdSummariesByGuidTy &TypeIdSummariesByGuid,
|
|
|
|
const std::set<GlobalValue::GUID> &CfiFunctionDefs,
|
|
|
|
const std::set<GlobalValue::GUID> &CfiFunctionDecls) {
|
2016-08-24 05:30:12 +08:00
|
|
|
// 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);
|
2017-04-13 09:26:12 +08:00
|
|
|
#ifdef LLVM_REVISION
|
2016-08-24 05:30:12 +08:00
|
|
|
Hasher.update(LLVM_REVISION);
|
|
|
|
#endif
|
|
|
|
|
2016-12-08 13:28:30 +08:00
|
|
|
// Include the parts of the LTO configuration that affect code generation.
|
|
|
|
auto AddString = [&](StringRef Str) {
|
|
|
|
Hasher.update(Str);
|
|
|
|
Hasher.update(ArrayRef<uint8_t>{0});
|
|
|
|
};
|
|
|
|
auto AddUnsigned = [&](unsigned I) {
|
|
|
|
uint8_t Data[4];
|
|
|
|
Data[0] = I;
|
|
|
|
Data[1] = I >> 8;
|
|
|
|
Data[2] = I >> 16;
|
|
|
|
Data[3] = I >> 24;
|
|
|
|
Hasher.update(ArrayRef<uint8_t>{Data, 4});
|
|
|
|
};
|
2017-03-04 04:25:30 +08:00
|
|
|
auto AddUint64 = [&](uint64_t I) {
|
|
|
|
uint8_t Data[8];
|
|
|
|
Data[0] = I;
|
|
|
|
Data[1] = I >> 8;
|
|
|
|
Data[2] = I >> 16;
|
|
|
|
Data[3] = I >> 24;
|
|
|
|
Data[4] = I >> 32;
|
|
|
|
Data[5] = I >> 40;
|
|
|
|
Data[6] = I >> 48;
|
|
|
|
Data[7] = I >> 56;
|
|
|
|
Hasher.update(ArrayRef<uint8_t>{Data, 8});
|
|
|
|
};
|
2016-12-08 13:28:30 +08:00
|
|
|
AddString(Conf.CPU);
|
|
|
|
// FIXME: Hash more of Options. For now all clients initialize Options from
|
|
|
|
// command-line flags (which is unsupported in production), but may set
|
|
|
|
// RelaxELFRelocations. The clang driver can also pass FunctionSections,
|
|
|
|
// DataSections and DebuggerTuning via command line flags.
|
|
|
|
AddUnsigned(Conf.Options.RelaxELFRelocations);
|
|
|
|
AddUnsigned(Conf.Options.FunctionSections);
|
|
|
|
AddUnsigned(Conf.Options.DataSections);
|
|
|
|
AddUnsigned((unsigned)Conf.Options.DebuggerTuning);
|
|
|
|
for (auto &A : Conf.MAttrs)
|
|
|
|
AddString(A);
|
2017-05-23 05:11:35 +08:00
|
|
|
if (Conf.RelocModel)
|
|
|
|
AddUnsigned(*Conf.RelocModel);
|
|
|
|
else
|
|
|
|
AddUnsigned(-1);
|
2017-08-03 10:16:21 +08:00
|
|
|
if (Conf.CodeModel)
|
|
|
|
AddUnsigned(*Conf.CodeModel);
|
|
|
|
else
|
|
|
|
AddUnsigned(-1);
|
2016-12-08 13:28:30 +08:00
|
|
|
AddUnsigned(Conf.CGOptLevel);
|
2017-02-16 04:36:36 +08:00
|
|
|
AddUnsigned(Conf.CGFileType);
|
2016-12-08 13:28:30 +08:00
|
|
|
AddUnsigned(Conf.OptLevel);
|
[ThinLTO] Move -lto-use-new-pm to llvm-lto2, and change it to -use-new-pm.
Summary:
As we teach Clang to use ThinkLTO + new PM, it's good for the users to
inject through Config, instead of setting a flag in the LTOBackend
library. Move the flag to llvm-lto2.
As it moves to llvm-lto2, a new name -use-new-pm seems simpler and as
clear.
Reviewers: davide, tejohnson
Subscribers: mehdi_amini, Prazek, inglorion, eraman, chandlerc, llvm-commits
Differential Revision: https://reviews.llvm.org/D33799
llvm-svn: 304492
2017-06-02 07:13:44 +08:00
|
|
|
AddUnsigned(Conf.UseNewPM);
|
2016-12-08 13:28:30 +08:00
|
|
|
AddString(Conf.OptPipeline);
|
|
|
|
AddString(Conf.AAPipeline);
|
|
|
|
AddString(Conf.OverrideTriple);
|
|
|
|
AddString(Conf.DefaultTriple);
|
|
|
|
|
2016-08-24 05:30:12 +08:00
|
|
|
// 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)));
|
|
|
|
|
2017-03-04 04:25:30 +08:00
|
|
|
// Include the hash for every module we import functions from. The set of
|
|
|
|
// imported symbols for each module may affect code generation and is
|
|
|
|
// sensitive to link order, so include that as well.
|
2016-08-24 05:30:12 +08:00
|
|
|
for (auto &Entry : ImportList) {
|
|
|
|
auto ModHash = Index.getModuleHash(Entry.first());
|
|
|
|
Hasher.update(ArrayRef<uint8_t>((uint8_t *)&ModHash[0], sizeof(ModHash)));
|
2017-03-04 04:25:30 +08:00
|
|
|
|
|
|
|
AddUint64(Entry.second.size());
|
|
|
|
for (auto &Fn : Entry.second)
|
|
|
|
AddUint64(Fn.first);
|
2016-08-24 05:30:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)));
|
|
|
|
}
|
|
|
|
|
2017-08-10 07:24:07 +08:00
|
|
|
// Members of CfiFunctionDefs and CfiFunctionDecls that are referenced or
|
|
|
|
// defined in this module.
|
|
|
|
std::set<GlobalValue::GUID> UsedCfiDefs;
|
|
|
|
std::set<GlobalValue::GUID> UsedCfiDecls;
|
|
|
|
|
|
|
|
// Typeids used in this module.
|
2017-03-11 05:35:17 +08:00
|
|
|
std::set<GlobalValue::GUID> UsedTypeIds;
|
|
|
|
|
2017-08-10 07:24:07 +08:00
|
|
|
auto AddUsedCfiGlobal = [&](GlobalValue::GUID ValueGUID) {
|
|
|
|
if (CfiFunctionDefs.count(ValueGUID))
|
|
|
|
UsedCfiDefs.insert(ValueGUID);
|
|
|
|
if (CfiFunctionDecls.count(ValueGUID))
|
|
|
|
UsedCfiDecls.insert(ValueGUID);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto AddUsedThings = [&](GlobalValueSummary *GS) {
|
|
|
|
if (!GS) return;
|
2018-02-09 13:58:55 +08:00
|
|
|
AddUnsigned(GS->isLive());
|
2018-02-06 01:17:51 +08:00
|
|
|
for (const ValueInfo &VI : GS->refs()) {
|
|
|
|
AddUnsigned(VI.isDSOLocal());
|
2017-08-10 07:24:07 +08:00
|
|
|
AddUsedCfiGlobal(VI.getGUID());
|
2018-02-06 01:17:51 +08:00
|
|
|
}
|
2017-08-10 07:24:07 +08:00
|
|
|
if (auto *FS = dyn_cast<FunctionSummary>(GS)) {
|
|
|
|
for (auto &TT : FS->type_tests())
|
|
|
|
UsedTypeIds.insert(TT);
|
|
|
|
for (auto &TT : FS->type_test_assume_vcalls())
|
|
|
|
UsedTypeIds.insert(TT.GUID);
|
|
|
|
for (auto &TT : FS->type_checked_load_vcalls())
|
|
|
|
UsedTypeIds.insert(TT.GUID);
|
|
|
|
for (auto &TT : FS->type_test_assume_const_vcalls())
|
|
|
|
UsedTypeIds.insert(TT.VFunc.GUID);
|
|
|
|
for (auto &TT : FS->type_checked_load_const_vcalls())
|
|
|
|
UsedTypeIds.insert(TT.VFunc.GUID);
|
2018-02-06 07:46:32 +08:00
|
|
|
for (auto &ET : FS->calls()) {
|
|
|
|
AddUnsigned(ET.first.isDSOLocal());
|
2017-08-10 07:24:07 +08:00
|
|
|
AddUsedCfiGlobal(ET.first.getGUID());
|
2018-02-06 07:46:32 +08:00
|
|
|
}
|
2017-08-10 07:24:07 +08:00
|
|
|
}
|
2017-03-11 05:35:17 +08:00
|
|
|
};
|
|
|
|
|
2016-08-24 05:30:12 +08:00
|
|
|
// Include the hash for the linkage type to reflect internalization and weak
|
2017-03-11 05:35:17 +08:00
|
|
|
// resolution, and collect any used type identifier resolutions.
|
2016-08-24 05:30:12 +08:00
|
|
|
for (auto &GS : DefinedGlobals) {
|
|
|
|
GlobalValue::LinkageTypes Linkage = GS.second->linkage();
|
|
|
|
Hasher.update(
|
|
|
|
ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
|
2017-08-10 07:24:07 +08:00
|
|
|
AddUsedCfiGlobal(GS.first);
|
|
|
|
AddUsedThings(GS.second);
|
2017-03-11 05:35:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Imported functions may introduce new uses of type identifier resolutions,
|
|
|
|
// so we need to collect their used resolutions as well.
|
|
|
|
for (auto &ImpM : ImportList)
|
|
|
|
for (auto &ImpF : ImpM.second)
|
2017-08-10 07:24:07 +08:00
|
|
|
AddUsedThings(Index.findSummaryInModule(ImpF.first, ImpM.first()));
|
2017-03-11 05:35:17 +08:00
|
|
|
|
|
|
|
auto AddTypeIdSummary = [&](StringRef TId, const TypeIdSummary &S) {
|
|
|
|
AddString(TId);
|
|
|
|
|
|
|
|
AddUnsigned(S.TTRes.TheKind);
|
|
|
|
AddUnsigned(S.TTRes.SizeM1BitWidth);
|
2017-03-11 05:37:10 +08:00
|
|
|
|
2017-09-12 06:49:10 +08:00
|
|
|
AddUint64(S.TTRes.AlignLog2);
|
|
|
|
AddUint64(S.TTRes.SizeM1);
|
|
|
|
AddUint64(S.TTRes.BitMask);
|
|
|
|
AddUint64(S.TTRes.InlineBits);
|
|
|
|
|
2017-03-11 05:37:10 +08:00
|
|
|
AddUint64(S.WPDRes.size());
|
|
|
|
for (auto &WPD : S.WPDRes) {
|
|
|
|
AddUnsigned(WPD.first);
|
|
|
|
AddUnsigned(WPD.second.TheKind);
|
|
|
|
AddString(WPD.second.SingleImplName);
|
|
|
|
|
|
|
|
AddUint64(WPD.second.ResByArg.size());
|
|
|
|
for (auto &ByArg : WPD.second.ResByArg) {
|
|
|
|
AddUint64(ByArg.first.size());
|
|
|
|
for (uint64_t Arg : ByArg.first)
|
|
|
|
AddUint64(Arg);
|
|
|
|
AddUnsigned(ByArg.second.TheKind);
|
|
|
|
AddUint64(ByArg.second.Info);
|
2017-09-12 06:34:42 +08:00
|
|
|
AddUnsigned(ByArg.second.Byte);
|
|
|
|
AddUnsigned(ByArg.second.Bit);
|
2017-03-11 05:37:10 +08:00
|
|
|
}
|
|
|
|
}
|
2017-03-11 05:35:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Include the hash for all type identifiers used by this module.
|
|
|
|
for (GlobalValue::GUID TId : UsedTypeIds) {
|
|
|
|
auto SummariesI = TypeIdSummariesByGuid.find(TId);
|
|
|
|
if (SummariesI != TypeIdSummariesByGuid.end())
|
|
|
|
for (auto *Summary : SummariesI->second)
|
|
|
|
AddTypeIdSummary(Summary->first, Summary->second);
|
2016-08-24 05:30:12 +08:00
|
|
|
}
|
|
|
|
|
2017-08-10 07:24:07 +08:00
|
|
|
AddUnsigned(UsedCfiDefs.size());
|
|
|
|
for (auto &V : UsedCfiDefs)
|
|
|
|
AddUint64(V);
|
|
|
|
|
|
|
|
AddUnsigned(UsedCfiDecls.size());
|
|
|
|
for (auto &V : UsedCfiDecls)
|
|
|
|
AddUint64(V);
|
|
|
|
|
2016-12-17 00:48:46 +08:00
|
|
|
if (!Conf.SampleProfile.empty()) {
|
|
|
|
auto FileOrErr = MemoryBuffer::getFile(Conf.SampleProfile);
|
|
|
|
if (FileOrErr)
|
|
|
|
Hasher.update(FileOrErr.get()->getBuffer());
|
|
|
|
}
|
|
|
|
|
2016-08-24 05:30:12 +08:00
|
|
|
Key = toHex(Hasher.result());
|
|
|
|
}
|
|
|
|
|
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()) &&
|
2017-01-21 05:54:58 +08:00
|
|
|
!GlobalInvolvedWithAlias.count(S.get()))
|
2016-05-25 22:03:11 +08:00
|
|
|
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)
|
2017-05-05 02:03:25 +08:00
|
|
|
for (auto &S : I.second.SummaryList)
|
2016-05-25 22:03:11 +08:00
|
|
|
if (auto AS = dyn_cast<AliasSummary>(S.get()))
|
|
|
|
GlobalInvolvedWithAlias.insert(&AS->getAliasee());
|
|
|
|
|
|
|
|
for (auto &I : Index)
|
2017-05-05 02:03:25 +08:00
|
|
|
thinLTOResolveWeakForLinkerGUID(I.second.SummaryList, I.first,
|
|
|
|
GlobalInvolvedWithAlias, isPrevailing,
|
|
|
|
recordNewLinkage);
|
2016-05-25 22:03:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void thinLTOInternalizeAndPromoteGUID(
|
|
|
|
GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
|
2017-02-03 15:41:43 +08:00
|
|
|
function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
|
2016-05-25 22:03:11 +08:00
|
|
|
for (auto &S : GVSummaryList) {
|
2017-02-03 15:41:43 +08:00
|
|
|
if (isExported(S->modulePath(), GUID)) {
|
2016-05-25 22:03:11 +08:00
|
|
|
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,
|
2017-02-03 15:41:43 +08:00
|
|
|
function_ref<bool(StringRef, GlobalValue::GUID)> isExported) {
|
2016-05-25 22:03:11 +08:00
|
|
|
for (auto &I : Index)
|
2017-05-05 02:03:25 +08:00
|
|
|
thinLTOInternalizeAndPromoteGUID(I.second.SummaryList, I.first, isExported);
|
2016-05-25 22:03:11 +08:00
|
|
|
}
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2016-12-14 09:17:59 +08:00
|
|
|
// Requires a destructor for std::vector<InputModule>.
|
|
|
|
InputFile::~InputFile() = default;
|
|
|
|
|
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
|
|
|
|
2017-06-08 09:26:14 +08:00
|
|
|
Expected<IRSymtabFile> FOrErr = readIRSymtab(Object);
|
|
|
|
if (!FOrErr)
|
|
|
|
return FOrErr.takeError();
|
2017-03-31 12:47:07 +08:00
|
|
|
|
2017-06-08 09:26:14 +08:00
|
|
|
File->TargetTriple = FOrErr->TheReader.getTargetTriple();
|
|
|
|
File->SourceFileName = FOrErr->TheReader.getSourceFileName();
|
|
|
|
File->COFFLinkerOpts = FOrErr->TheReader.getCOFFLinkerOpts();
|
|
|
|
File->ComdatTable = FOrErr->TheReader.getComdatTable();
|
2017-03-31 12:47:07 +08:00
|
|
|
|
2017-06-08 09:26:14 +08:00
|
|
|
for (unsigned I = 0; I != FOrErr->Mods.size(); ++I) {
|
2017-03-31 12:47:07 +08:00
|
|
|
size_t Begin = File->Symbols.size();
|
2017-06-08 09:26:14 +08:00
|
|
|
for (const irsymtab::Reader::SymbolRef &Sym :
|
|
|
|
FOrErr->TheReader.module_symbols(I))
|
2017-03-31 12:47:07 +08:00
|
|
|
// Skip symbols that are irrelevant to LTO. Note that this condition needs
|
|
|
|
// to match the one in Skip() in LTO::addRegularLTO().
|
|
|
|
if (Sym.isGlobal() && !Sym.isFormatSpecific())
|
|
|
|
File->Symbols.push_back(Sym);
|
|
|
|
File->ModuleSymIndices.push_back({Begin, File->Symbols.size()});
|
2017-03-31 10:44:50 +08:00
|
|
|
}
|
|
|
|
|
2017-06-08 09:26:14 +08:00
|
|
|
File->Mods = FOrErr->Mods;
|
|
|
|
File->Strtab = std::move(FOrErr->Strtab);
|
2017-03-31 12:47:07 +08:00
|
|
|
return std::move(File);
|
2017-03-31 10:44:50 +08:00
|
|
|
}
|
|
|
|
|
2016-12-14 09:17:59 +08:00
|
|
|
StringRef InputFile::getName() const {
|
2017-03-31 12:47:07 +08:00
|
|
|
return Mods[0].getModuleIdentifier();
|
2016-12-14 09:17:59 +08:00
|
|
|
}
|
|
|
|
|
2016-08-11 22:58:12 +08:00
|
|
|
LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
|
|
|
|
Config &Conf)
|
|
|
|
: ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),
|
2017-12-16 10:10:00 +08:00
|
|
|
Ctx(Conf), CombinedModule(llvm::make_unique<Module>("ld-temp.o", Ctx)),
|
|
|
|
Mover(llvm::make_unique<IRMover>(*CombinedModule)) {}
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2018-01-22 21:35:40 +08:00
|
|
|
LTO::ThinLTOState::ThinLTOState(ThinBackend Backend)
|
|
|
|
: Backend(Backend), CombinedIndex(/*IsPeformingAnalysis*/ false) {
|
2016-08-11 22:58:12 +08:00
|
|
|
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
|
|
|
|
2016-12-14 09:17:59 +08:00
|
|
|
// Requires a destructor for MapVector<BitcodeModule>.
|
|
|
|
LTO::~LTO() = default;
|
|
|
|
|
2017-06-16 01:26:13 +08:00
|
|
|
// Add the symbols in the given module to the GlobalResolutions map, and resolve
|
|
|
|
// their partitions.
|
|
|
|
void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
|
|
|
|
ArrayRef<SymbolResolution> Res,
|
|
|
|
unsigned Partition, bool InSummary) {
|
|
|
|
auto *ResI = Res.begin();
|
|
|
|
auto *ResE = Res.end();
|
2017-06-16 01:41:32 +08:00
|
|
|
(void)ResE;
|
2017-06-16 01:26:13 +08:00
|
|
|
for (const InputFile::Symbol &Sym : Syms) {
|
|
|
|
assert(ResI != ResE);
|
|
|
|
SymbolResolution Res = *ResI++;
|
|
|
|
|
|
|
|
auto &GlobalRes = GlobalResolutions[Sym.getName()];
|
|
|
|
GlobalRes.UnnamedAddr &= Sym.isUnnamedAddr();
|
2017-12-15 17:18:21 +08:00
|
|
|
if (Res.Prevailing) {
|
2018-01-26 01:23:27 +08:00
|
|
|
assert(!GlobalRes.Prevailing &&
|
2017-12-16 00:27:33 +08:00
|
|
|
"Multiple prevailing defs are not allowed");
|
2018-01-26 01:23:27 +08:00
|
|
|
GlobalRes.Prevailing = true;
|
2017-06-16 01:26:13 +08:00
|
|
|
GlobalRes.IRName = Sym.getIRName();
|
2018-01-29 16:03:30 +08:00
|
|
|
} else if (!GlobalRes.Prevailing && GlobalRes.IRName.empty()) {
|
|
|
|
// Sometimes it can be two copies of symbol in a module and prevailing
|
|
|
|
// symbol can have no IR name. That might happen if symbol is defined in
|
|
|
|
// module level inline asm block. In case we have multiple modules with
|
|
|
|
// the same symbol we want to use IR name of the prevailing symbol.
|
|
|
|
// Otherwise, if we haven't seen a prevailing symbol, set the name so that
|
|
|
|
// we can later use it to check if there is any prevailing copy in IR.
|
|
|
|
GlobalRes.IRName = Sym.getIRName();
|
2017-12-15 17:18:21 +08:00
|
|
|
}
|
2017-06-16 01:26:13 +08:00
|
|
|
|
|
|
|
// Set the partition to external if we know it is re-defined by the linker
|
|
|
|
// with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
|
|
|
|
// regular object, is referenced from llvm.compiler_used, or was already
|
|
|
|
// recorded as being referenced from a different partition.
|
|
|
|
if (Res.LinkerRedefined || Res.VisibleToRegularObj || Sym.isUsed() ||
|
|
|
|
(GlobalRes.Partition != GlobalResolution::Unknown &&
|
|
|
|
GlobalRes.Partition != Partition)) {
|
|
|
|
GlobalRes.Partition = GlobalResolution::External;
|
|
|
|
} else
|
|
|
|
// First recorded reference, save the current partition.
|
|
|
|
GlobalRes.Partition = Partition;
|
|
|
|
|
|
|
|
// Flag as visible outside of summary if visible from a regular object or
|
|
|
|
// from a module that does not have a summary.
|
|
|
|
GlobalRes.VisibleOutsideSummary |=
|
|
|
|
(Res.VisibleToRegularObj || Sym.isUsed() || !InSummary);
|
|
|
|
}
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
|
2016-08-27 04:19:35 +08:00
|
|
|
static void writeToResolutionFile(raw_ostream &OS, InputFile *Input,
|
|
|
|
ArrayRef<SymbolResolution> Res) {
|
2016-12-14 09:17:59 +08:00
|
|
|
StringRef Path = Input->getName();
|
2016-08-27 04:19:35 +08:00
|
|
|
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';
|
2017-06-06 00:24:25 +08:00
|
|
|
if (Res.LinkerRedefined)
|
|
|
|
OS << 'r';
|
2016-08-27 04:19:35 +08:00
|
|
|
OS << '\n';
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
2017-01-20 07:10:14 +08:00
|
|
|
OS.flush();
|
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
|
|
|
|
2017-12-16 10:10:00 +08:00
|
|
|
if (RegularLTO.CombinedModule->getTargetTriple().empty())
|
|
|
|
RegularLTO.CombinedModule->setTargetTriple(Input->getTargetTriple());
|
|
|
|
|
2016-12-14 09:17:59 +08:00
|
|
|
const SymbolResolution *ResI = Res.begin();
|
2017-03-31 12:47:07 +08:00
|
|
|
for (unsigned I = 0; I != Input->Mods.size(); ++I)
|
|
|
|
if (Error Err = addModule(*Input, I, ResI, Res.end()))
|
2016-12-14 09:17:59 +08:00
|
|
|
return Err;
|
|
|
|
|
|
|
|
assert(ResI == Res.end());
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-03-31 12:47:07 +08:00
|
|
|
Error LTO::addModule(InputFile &Input, unsigned ModI,
|
2016-12-14 09:17:59 +08:00
|
|
|
const SymbolResolution *&ResI,
|
|
|
|
const SymbolResolution *ResE) {
|
2017-06-16 01:26:13 +08:00
|
|
|
Expected<BitcodeLTOInfo> LTOInfo = Input.Mods[ModI].getLTOInfo();
|
|
|
|
if (!LTOInfo)
|
|
|
|
return LTOInfo.takeError();
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2017-06-16 01:26:13 +08:00
|
|
|
BitcodeModule BM = Input.Mods[ModI];
|
2017-03-31 12:47:07 +08:00
|
|
|
auto ModSyms = Input.module_symbols(ModI);
|
2017-06-16 01:26:13 +08:00
|
|
|
addModuleToGlobalRes(ModSyms, {ResI, ResE},
|
|
|
|
LTOInfo->IsThinLTO ? ThinLTO.ModuleMap.size() + 1 : 0,
|
|
|
|
LTOInfo->HasSummary);
|
|
|
|
|
|
|
|
if (LTOInfo->IsThinLTO)
|
|
|
|
return addThinLTO(BM, ModSyms, ResI, ResE);
|
|
|
|
|
|
|
|
Expected<RegularLTOState::AddedModule> ModOrErr =
|
|
|
|
addRegularLTO(BM, ModSyms, ResI, ResE);
|
|
|
|
if (!ModOrErr)
|
|
|
|
return ModOrErr.takeError();
|
|
|
|
|
|
|
|
if (!LTOInfo->HasSummary)
|
|
|
|
return linkRegularLTO(std::move(*ModOrErr), /*LivenessFromIndex=*/false);
|
|
|
|
|
|
|
|
// Regular LTO module summaries are added to a dummy module that represents
|
|
|
|
// the combined regular LTO module.
|
|
|
|
if (Error Err = BM.readSummary(ThinLTO.CombinedIndex, "", -1ull))
|
|
|
|
return Err;
|
|
|
|
RegularLTO.ModsWithSummaries.push_back(std::move(*ModOrErr));
|
|
|
|
return Error::success();
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
|
2017-06-30 22:03:24 +08:00
|
|
|
// Checks whether the given global value is in a non-prevailing comdat
|
|
|
|
// (comdat containing values the linker indicated were not prevailing,
|
|
|
|
// which we then dropped to available_externally), and if so, removes
|
|
|
|
// it from the comdat. This is called for all global values to ensure the
|
|
|
|
// comdat is empty rather than leaving an incomplete comdat. It is needed for
|
|
|
|
// regular LTO modules, in case we are in a mixed-LTO mode (both regular
|
|
|
|
// and thin LTO modules) compilation. Since the regular LTO module will be
|
|
|
|
// linked first in the final native link, we want to make sure the linker
|
|
|
|
// doesn't select any of these incomplete comdats that would be left
|
|
|
|
// in the regular LTO module without this cleanup.
|
|
|
|
static void
|
|
|
|
handleNonPrevailingComdat(GlobalValue &GV,
|
|
|
|
std::set<const Comdat *> &NonPrevailingComdats) {
|
|
|
|
Comdat *C = GV.getComdat();
|
|
|
|
if (!C)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!NonPrevailingComdats.count(C))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Additionally need to drop externally visible global values from the comdat
|
|
|
|
// to available_externally, so that there aren't multiply defined linker
|
|
|
|
// errors.
|
|
|
|
if (!GV.hasLocalLinkage())
|
|
|
|
GV.setLinkage(GlobalValue::AvailableExternallyLinkage);
|
|
|
|
|
|
|
|
if (auto GO = dyn_cast<GlobalObject>(&GV))
|
|
|
|
GO->setComdat(nullptr);
|
|
|
|
}
|
|
|
|
|
2016-08-11 22:58:12 +08:00
|
|
|
// Add a regular LTO object to the link.
|
2017-06-16 01:26:13 +08:00
|
|
|
// The resulting module needs to be linked into the combined LTO module with
|
|
|
|
// linkRegularLTO.
|
|
|
|
Expected<LTO::RegularLTOState::AddedModule>
|
|
|
|
LTO::addRegularLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
|
|
|
|
const SymbolResolution *&ResI,
|
|
|
|
const SymbolResolution *ResE) {
|
|
|
|
RegularLTOState::AddedModule Mod;
|
2016-12-14 03:43:49 +08:00
|
|
|
Expected<std::unique_ptr<Module>> MOrErr =
|
2016-12-17 05:25:01 +08:00
|
|
|
BM.getLazyModule(RegularLTO.Ctx, /*ShouldLazyLoadMetadata*/ true,
|
|
|
|
/*IsImporting*/ false);
|
2016-12-14 03:43:49 +08:00
|
|
|
if (!MOrErr)
|
|
|
|
return MOrErr.takeError();
|
|
|
|
Module &M = **MOrErr;
|
2017-06-16 01:26:13 +08:00
|
|
|
Mod.M = std::move(*MOrErr);
|
|
|
|
|
2016-11-10 01:49:19 +08:00
|
|
|
if (Error Err = M.materializeMetadata())
|
2017-06-16 01:26:13 +08:00
|
|
|
return std::move(Err);
|
2016-08-11 22:58:12 +08:00
|
|
|
UpgradeDebugInfo(M);
|
|
|
|
|
2016-12-14 03:43:49 +08:00
|
|
|
ModuleSymbolTable SymTab;
|
|
|
|
SymTab.addModule(&M);
|
|
|
|
|
2016-08-11 22:58:12 +08:00
|
|
|
for (GlobalVariable &GV : M.globals())
|
|
|
|
if (GV.hasAppendingLinkage())
|
2017-06-16 01:26:13 +08:00
|
|
|
Mod.Keep.push_back(&GV);
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2017-02-02 13:22:42 +08:00
|
|
|
DenseSet<GlobalObject *> AliasedGlobals;
|
|
|
|
for (auto &GA : M.aliases())
|
|
|
|
if (GlobalObject *GO = GA.getBaseObject())
|
|
|
|
AliasedGlobals.insert(GO);
|
|
|
|
|
2017-03-31 12:47:07 +08:00
|
|
|
// In this function we need IR GlobalValues matching the symbols in Syms
|
|
|
|
// (which is not backed by a module), so we need to enumerate them in the same
|
|
|
|
// order. The symbol enumeration order of a ModuleSymbolTable intentionally
|
|
|
|
// matches the order of an irsymtab, but when we read the irsymtab in
|
|
|
|
// InputFile::create we omit some symbols that are irrelevant to LTO. The
|
|
|
|
// Skip() function skips the same symbols from the module as InputFile does
|
|
|
|
// from the symbol table.
|
|
|
|
auto MsymI = SymTab.symbols().begin(), MsymE = SymTab.symbols().end();
|
|
|
|
auto Skip = [&]() {
|
|
|
|
while (MsymI != MsymE) {
|
|
|
|
auto Flags = SymTab.getSymbolFlags(*MsymI);
|
|
|
|
if ((Flags & object::BasicSymbolRef::SF_Global) &&
|
|
|
|
!(Flags & object::BasicSymbolRef::SF_FormatSpecific))
|
|
|
|
return;
|
|
|
|
++MsymI;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Skip();
|
|
|
|
|
2017-06-30 22:03:24 +08:00
|
|
|
std::set<const Comdat *> NonPrevailingComdats;
|
2017-03-31 12:47:07 +08:00
|
|
|
for (const InputFile::Symbol &Sym : Syms) {
|
2016-12-14 09:17:59 +08:00
|
|
|
assert(ResI != ResE);
|
2016-08-11 22:58:12 +08:00
|
|
|
SymbolResolution Res = *ResI++;
|
|
|
|
|
2017-03-31 12:47:07 +08:00
|
|
|
assert(MsymI != MsymE);
|
|
|
|
ModuleSymbolTable::Symbol Msym = *MsymI++;
|
|
|
|
Skip();
|
|
|
|
|
|
|
|
if (GlobalValue *GV = Msym.dyn_cast<GlobalValue *>()) {
|
2017-02-02 13:12:15 +08:00
|
|
|
if (Res.Prevailing) {
|
2017-03-29 06:31:35 +08:00
|
|
|
if (Sym.isUndefined())
|
2017-02-02 13:12:15 +08:00
|
|
|
continue;
|
2017-06-16 01:26:13 +08:00
|
|
|
Mod.Keep.push_back(GV);
|
2017-06-06 00:24:25 +08:00
|
|
|
// For symbols re-defined with linker -wrap and -defsym options,
|
|
|
|
// set the linkage to weak to inhibit IPO. The linkage will be
|
|
|
|
// restored by the linker.
|
|
|
|
if (Res.LinkerRedefined)
|
|
|
|
GV->setLinkage(GlobalValue::WeakAnyLinkage);
|
|
|
|
|
2017-05-27 05:56:14 +08:00
|
|
|
GlobalValue::LinkageTypes OriginalLinkage = GV->getLinkage();
|
|
|
|
if (GlobalValue::isLinkOnceLinkage(OriginalLinkage))
|
|
|
|
GV->setLinkage(GlobalValue::getWeakLinkage(
|
|
|
|
GlobalValue::isLinkOnceODRLinkage(OriginalLinkage)));
|
2017-02-02 13:22:42 +08:00
|
|
|
} else if (isa<GlobalObject>(GV) &&
|
|
|
|
(GV->hasLinkOnceODRLinkage() || GV->hasWeakODRLinkage() ||
|
|
|
|
GV->hasAvailableExternallyLinkage()) &&
|
|
|
|
!AliasedGlobals.count(cast<GlobalObject>(GV))) {
|
2017-06-16 01:26:13 +08:00
|
|
|
// Any of the above three types of linkage indicates that the
|
2017-02-02 13:22:42 +08:00
|
|
|
// chosen prevailing symbol will have the same semantics as this copy of
|
2017-06-16 01:26:13 +08:00
|
|
|
// the symbol, so we may be able to link it with available_externally
|
|
|
|
// linkage. We will decide later whether to do that when we link this
|
|
|
|
// module (in linkRegularLTO), based on whether it is undefined.
|
|
|
|
Mod.Keep.push_back(GV);
|
|
|
|
GV->setLinkage(GlobalValue::AvailableExternallyLinkage);
|
2017-06-30 22:03:24 +08:00
|
|
|
if (GV->hasComdat())
|
|
|
|
NonPrevailingComdats.insert(GV->getComdat());
|
2017-06-16 01:26:13 +08:00
|
|
|
cast<GlobalObject>(GV)->setComdat(nullptr);
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
2017-11-05 01:04:39 +08:00
|
|
|
|
|
|
|
// Set the 'local' flag based on the linker resolution for this symbol.
|
2018-01-18 13:38:43 +08:00
|
|
|
if (Res.FinalDefinitionInLinkageUnit)
|
2018-01-25 03:11:24 +08:00
|
|
|
GV->setDSOLocal(true);
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
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.
|
2017-03-29 06:31:35 +08:00
|
|
|
if (Sym.isCommon()) {
|
2016-12-01 10:51:12 +08:00
|
|
|
// FIXME: We should figure out what to do about commons defined by asm.
|
|
|
|
// For now they aren't reported correctly by ModuleSymbolTable.
|
2017-03-31 12:47:07 +08:00
|
|
|
auto &CommonRes = RegularLTO.Commons[Sym.getIRName()];
|
2016-08-22 14:25:46 +08:00
|
|
|
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
|
|
|
|
|
|
|
}
|
2017-06-30 22:03:24 +08:00
|
|
|
if (!M.getComdatSymbolTable().empty())
|
|
|
|
for (GlobalValue &GV : M.global_values())
|
|
|
|
handleNonPrevailingComdat(GV, NonPrevailingComdats);
|
2017-03-31 12:47:07 +08:00
|
|
|
assert(MsymI == MsymE);
|
2017-06-16 01:26:13 +08:00
|
|
|
return std::move(Mod);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
|
|
|
|
bool LivenessFromIndex) {
|
|
|
|
std::vector<GlobalValue *> Keep;
|
|
|
|
for (GlobalValue *GV : Mod.Keep) {
|
|
|
|
if (LivenessFromIndex && !ThinLTO.CombinedIndex.isGUIDLive(GV->getGUID()))
|
|
|
|
continue;
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2017-06-16 01:26:13 +08:00
|
|
|
if (!GV->hasAvailableExternallyLinkage()) {
|
|
|
|
Keep.push_back(GV);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only link available_externally definitions if we don't already have a
|
|
|
|
// definition.
|
|
|
|
GlobalValue *CombinedGV =
|
|
|
|
RegularLTO.CombinedModule->getNamedValue(GV->getName());
|
|
|
|
if (CombinedGV && !CombinedGV->isDeclaration())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Keep.push_back(GV);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RegularLTO.Mover->move(std::move(Mod.M), Keep,
|
2016-10-13 02:39:29 +08:00
|
|
|
[](GlobalValue &, IRMover::ValueAdder) {},
|
2016-12-13 00:09:30 +08:00
|
|
|
/* IsPerformingImport */ false);
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
|
2017-06-16 01:26:13 +08:00
|
|
|
// Add a ThinLTO module to the link.
|
|
|
|
Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
|
2016-12-14 09:17:59 +08:00
|
|
|
const SymbolResolution *&ResI,
|
|
|
|
const SymbolResolution *ResE) {
|
2017-05-02 06:04:36 +08:00
|
|
|
if (Error Err =
|
2017-06-16 01:26:13 +08:00
|
|
|
BM.readSummary(ThinLTO.CombinedIndex, BM.getModuleIdentifier(),
|
|
|
|
ThinLTO.ModuleMap.size()))
|
2017-05-02 06:04:36 +08:00
|
|
|
return Err;
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2016-12-14 09:17:59 +08:00
|
|
|
for (const InputFile::Symbol &Sym : Syms) {
|
|
|
|
assert(ResI != ResE);
|
2016-08-11 22:58:12 +08:00
|
|
|
SymbolResolution Res = *ResI++;
|
|
|
|
|
2017-11-05 01:04:39 +08:00
|
|
|
if (!Sym.getIRName().empty()) {
|
|
|
|
auto GUID = GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
|
|
|
|
Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
|
|
|
|
if (Res.Prevailing) {
|
2017-03-31 12:47:07 +08:00
|
|
|
ThinLTO.PrevailingModuleForGUID[GUID] = BM.getModuleIdentifier();
|
2017-07-07 03:58:26 +08:00
|
|
|
|
|
|
|
// For linker redefined symbols (via --wrap or --defsym) we want to
|
|
|
|
// switch the linkage to `weak` to prevent IPOs from happening.
|
|
|
|
// Find the summary in the module for this very GV and record the new
|
|
|
|
// linkage so that we can switch it when we import the GV.
|
|
|
|
if (Res.LinkerRedefined)
|
|
|
|
if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
|
|
|
|
GUID, BM.getModuleIdentifier()))
|
|
|
|
S->setLinkage(GlobalValue::WeakAnyLinkage);
|
2017-03-31 12:47:07 +08:00
|
|
|
}
|
2017-11-05 01:04:39 +08:00
|
|
|
|
|
|
|
// If the linker resolved the symbol to a local definition then mark it
|
|
|
|
// as local in the summary for the module we are adding.
|
|
|
|
if (Res.FinalDefinitionInLinkageUnit) {
|
|
|
|
if (auto S = ThinLTO.CombinedIndex.findSummaryInModule(
|
|
|
|
GUID, BM.getModuleIdentifier())) {
|
|
|
|
S->setDSOLocal(true);
|
|
|
|
}
|
|
|
|
}
|
2017-03-31 12:47:07 +08:00
|
|
|
}
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
|
2016-12-14 09:17:59 +08:00
|
|
|
if (!ThinLTO.ModuleMap.insert({BM.getModuleIdentifier(), BM}).second)
|
|
|
|
return make_error<StringError>(
|
|
|
|
"Expected at most one ThinLTO module per bitcode file",
|
|
|
|
inconvertibleErrorCode());
|
|
|
|
|
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) {
|
2017-06-03 02:24:17 +08:00
|
|
|
// Compute "dead" symbols, we don't want to import/export these!
|
|
|
|
DenseSet<GlobalValue::GUID> GUIDPreservedSymbols;
|
2018-01-29 16:03:30 +08:00
|
|
|
DenseMap<GlobalValue::GUID, PrevailingType> GUIDPrevailingResolutions;
|
|
|
|
for (auto &Res : GlobalResolutions) {
|
|
|
|
// Normally resolution have IR name of symbol. We can do nothing here
|
|
|
|
// otherwise. See comments in GlobalResolution struct for more details.
|
|
|
|
if (Res.second.IRName.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
GlobalValue::GUID GUID = GlobalValue::getGUID(
|
|
|
|
GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
|
|
|
|
|
2018-01-26 01:23:27 +08:00
|
|
|
if (Res.second.VisibleOutsideSummary && Res.second.Prevailing)
|
2017-06-03 02:24:17 +08:00
|
|
|
GUIDPreservedSymbols.insert(GlobalValue::getGUID(
|
|
|
|
GlobalValue::dropLLVMManglingEscape(Res.second.IRName)));
|
|
|
|
|
2018-01-29 16:03:30 +08:00
|
|
|
GUIDPrevailingResolutions[GUID] =
|
|
|
|
Res.second.Prevailing ? PrevailingType::Yes : PrevailingType::No;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto isPrevailing = [&](GlobalValue::GUID G) {
|
|
|
|
auto It = GUIDPrevailingResolutions.find(G);
|
|
|
|
if (It == GUIDPrevailingResolutions.end())
|
|
|
|
return PrevailingType::Unknown;
|
|
|
|
return It->second;
|
|
|
|
};
|
|
|
|
computeDeadSymbols(ThinLTO.CombinedIndex, GUIDPreservedSymbols, isPrevailing);
|
2017-06-03 02:24:17 +08:00
|
|
|
|
2017-12-16 10:10:00 +08:00
|
|
|
if (auto E = runRegularLTO(AddStream))
|
|
|
|
return E;
|
|
|
|
return runThinLTO(AddStream, Cache);
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
|
2016-09-24 05:33:43 +08:00
|
|
|
Error LTO::runRegularLTO(AddStreamFn AddStream) {
|
2017-06-16 01:26:13 +08:00
|
|
|
for (auto &M : RegularLTO.ModsWithSummaries)
|
|
|
|
if (Error Err = linkRegularLTO(std::move(M),
|
|
|
|
/*LivenessFromIndex=*/true))
|
|
|
|
return Err;
|
|
|
|
|
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) {
|
2018-01-29 16:03:30 +08:00
|
|
|
if (!R.second.isPrevailingIRSymbol())
|
2016-08-22 14:25:41 +08:00
|
|
|
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,
|
2017-01-21 06:18:52 +08:00
|
|
|
std::move(RegularLTO.CombinedModule), ThinLTO.CombinedIndex);
|
2016-08-11 22:58:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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(
|
2016-12-14 09:17:59 +08:00
|
|
|
unsigned Task, BitcodeModule BM,
|
2016-08-24 05:30:12 +08:00
|
|
|
const FunctionImporter::ImportMapTy &ImportList,
|
|
|
|
const FunctionImporter::ExportSetTy &ExportList,
|
|
|
|
const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
|
2016-12-14 09:17:59 +08:00
|
|
|
MapVector<StringRef, BitcodeModule> &ModuleMap) = 0;
|
2016-08-11 22:58:12 +08:00
|
|
|
virtual Error wait() = 0;
|
|
|
|
};
|
|
|
|
|
2016-11-20 04:44:26 +08:00
|
|
|
namespace {
|
2016-08-11 22:58:12 +08:00
|
|
|
class InProcessThinBackend : public ThinBackendProc {
|
|
|
|
ThreadPool BackendThreadPool;
|
2016-09-24 05:33:43 +08:00
|
|
|
AddStreamFn AddStream;
|
|
|
|
NativeObjectCache Cache;
|
2017-03-11 05:35:17 +08:00
|
|
|
TypeIdSummariesByGuidTy TypeIdSummariesByGuid;
|
2017-08-10 07:24:07 +08:00
|
|
|
std::set<GlobalValue::GUID> CfiFunctionDefs;
|
|
|
|
std::set<GlobalValue::GUID> CfiFunctionDecls;
|
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),
|
2017-03-11 05:35:17 +08:00
|
|
|
AddStream(std::move(AddStream)), Cache(std::move(Cache)) {
|
|
|
|
// Create a mapping from type identifier GUIDs to type identifier summaries.
|
|
|
|
// This allows backends to use the type identifier GUIDs stored in the
|
|
|
|
// function summaries to determine which type identifier summaries affect
|
|
|
|
// each function without needing to compute GUIDs in each backend.
|
|
|
|
for (auto &TId : CombinedIndex.typeIds())
|
|
|
|
TypeIdSummariesByGuid[GlobalValue::getGUID(TId.first)].push_back(&TId);
|
2017-08-10 07:24:07 +08:00
|
|
|
for (auto &Name : CombinedIndex.cfiFunctionDefs())
|
|
|
|
CfiFunctionDefs.insert(
|
|
|
|
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Name)));
|
|
|
|
for (auto &Name : CombinedIndex.cfiFunctionDecls())
|
|
|
|
CfiFunctionDecls.insert(
|
|
|
|
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Name)));
|
2017-03-11 05:35:17 +08:00
|
|
|
}
|
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,
|
2016-12-14 09:17:59 +08:00
|
|
|
BitcodeModule BM, 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,
|
2017-03-11 05:35:17 +08:00
|
|
|
MapVector<StringRef, BitcodeModule> &ModuleMap,
|
|
|
|
const TypeIdSummariesByGuidTy &TypeIdSummariesByGuid) {
|
2016-09-24 05:33:43 +08:00
|
|
|
auto RunThinBackend = [&](AddStreamFn AddStream) {
|
|
|
|
LTOLLVMContext BackendContext(Conf);
|
2016-12-14 09:17:59 +08:00
|
|
|
Expected<std::unique_ptr<Module>> MOrErr = BM.parseModule(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-12-14 09:17:59 +08:00
|
|
|
auto ModuleID = BM.getModuleIdentifier();
|
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-12-08 13:28:30 +08:00
|
|
|
computeCacheKey(Key, Conf, CombinedIndex, ModuleID, ImportList, ExportList,
|
2017-08-10 07:24:07 +08:00
|
|
|
ResolvedODR, DefinedGlobals, TypeIdSummariesByGuid,
|
|
|
|
CfiFunctionDefs, CfiFunctionDecls);
|
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(
|
2016-12-14 09:17:59 +08:00
|
|
|
unsigned Task, BitcodeModule BM,
|
2016-08-24 05:30:12 +08:00
|
|
|
const FunctionImporter::ImportMapTy &ImportList,
|
|
|
|
const FunctionImporter::ExportSetTy &ExportList,
|
|
|
|
const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
|
2016-12-14 09:17:59 +08:00
|
|
|
MapVector<StringRef, BitcodeModule> &ModuleMap) override {
|
|
|
|
StringRef ModulePath = BM.getModuleIdentifier();
|
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(
|
2016-12-14 09:17:59 +08:00
|
|
|
[=](BitcodeModule BM, ModuleSummaryIndex &CombinedIndex,
|
2016-08-11 22:58:12 +08:00
|
|
|
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,
|
2017-03-11 05:35:17 +08:00
|
|
|
MapVector<StringRef, BitcodeModule> &ModuleMap,
|
|
|
|
const TypeIdSummariesByGuidTy &TypeIdSummariesByGuid) {
|
2016-08-24 05:30:12 +08:00
|
|
|
Error E = runThinLTOBackendThread(
|
2017-03-11 05:35:17 +08:00
|
|
|
AddStream, Cache, Task, BM, CombinedIndex, ImportList, ExportList,
|
|
|
|
ResolvedODR, DefinedGlobals, ModuleMap, TypeIdSummariesByGuid);
|
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);
|
|
|
|
}
|
|
|
|
},
|
2017-03-11 05:35:17 +08:00
|
|
|
BM, std::ref(CombinedIndex), std::ref(ImportList), std::ref(ExportList),
|
|
|
|
std::ref(ResolvedODR), std::ref(DefinedGlobals), std::ref(ModuleMap),
|
|
|
|
std::ref(TypeIdSummariesByGuid));
|
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
|
|
|
}
|
|
|
|
};
|
2016-11-20 04:44:26 +08:00
|
|
|
} // end anonymous namespace
|
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-11-20 04:44:26 +08:00
|
|
|
namespace {
|
2016-08-11 22:58:12 +08:00
|
|
|
class WriteIndexesThinBackend : public ThinBackendProc {
|
|
|
|
std::string OldPrefix, NewPrefix;
|
|
|
|
bool ShouldEmitImportsFiles;
|
2018-02-23 03:06:15 +08:00
|
|
|
raw_fd_ostream *LinkedObjectsFile;
|
2018-01-31 05:19:26 +08:00
|
|
|
lto::IndexWriteCallback OnWrite;
|
|
|
|
|
2016-08-11 22:58:12 +08:00
|
|
|
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,
|
2018-02-23 03:06:15 +08:00
|
|
|
raw_fd_ostream *LinkedObjectsFile, lto::IndexWriteCallback OnWrite)
|
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),
|
2018-02-23 03:06:15 +08:00
|
|
|
LinkedObjectsFile(LinkedObjectsFile), OnWrite(OnWrite) {}
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2016-08-24 05:30:12 +08:00
|
|
|
Error start(
|
2016-12-14 09:17:59 +08:00
|
|
|
unsigned Task, BitcodeModule BM,
|
2016-08-24 05:30:12 +08:00
|
|
|
const FunctionImporter::ImportMapTy &ImportList,
|
|
|
|
const FunctionImporter::ExportSetTy &ExportList,
|
|
|
|
const std::map<GlobalValue::GUID, GlobalValue::LinkageTypes> &ResolvedODR,
|
2016-12-14 09:17:59 +08:00
|
|
|
MapVector<StringRef, BitcodeModule> &ModuleMap) override {
|
|
|
|
StringRef ModulePath = BM.getModuleIdentifier();
|
2016-08-11 22:58:12 +08:00
|
|
|
std::string NewModulePath =
|
|
|
|
getThinLTOOutputFile(ModulePath, OldPrefix, NewPrefix);
|
|
|
|
|
2018-02-23 03:06:15 +08:00
|
|
|
if (LinkedObjectsFile)
|
2016-08-11 22:58:12 +08:00
|
|
|
*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
|
|
|
|
2018-02-23 03:06:15 +08:00
|
|
|
std::error_code EC;
|
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);
|
|
|
|
|
2018-01-31 05:19:26 +08:00
|
|
|
if (ShouldEmitImportsFiles) {
|
|
|
|
EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports", ImportList);
|
|
|
|
if (EC)
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OnWrite)
|
|
|
|
OnWrite(ModulePath);
|
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
|
|
|
};
|
2016-11-20 04:44:26 +08:00
|
|
|
} // end anonymous namespace
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2018-02-23 03:06:15 +08:00
|
|
|
ThinBackend lto::createWriteIndexesThinBackend(
|
|
|
|
std::string OldPrefix, std::string NewPrefix, bool ShouldEmitImportsFiles,
|
|
|
|
raw_fd_ostream *LinkedObjectsFile, IndexWriteCallback OnWrite) {
|
2016-08-11 22:58:12 +08:00
|
|
|
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,
|
2018-01-31 05:19:26 +08:00
|
|
|
ShouldEmitImportsFiles, LinkedObjectsFile, OnWrite);
|
2016-08-11 22:58:12 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-16 10:10:00 +08:00
|
|
|
Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache) {
|
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).
|
2017-07-11 04:12:54 +08:00
|
|
|
StringMap<GVSummaryMapTy>
|
2016-08-11 22:58:12 +08:00
|
|
|
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
|
|
|
|
2018-02-19 23:14:50 +08:00
|
|
|
if (DumpThinCGSCCs)
|
|
|
|
ThinLTO.CombinedIndex.dumpSCCs(outs());
|
|
|
|
|
2017-11-02 01:58:39 +08:00
|
|
|
if (Conf.OptLevel > 0)
|
2016-11-01 06:12:21 +08:00
|
|
|
ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
|
2017-06-02 04:30:06 +08:00
|
|
|
ImportLists, ExportLists);
|
2016-08-24 05:30:12 +08:00
|
|
|
|
2017-11-02 01:58:39 +08:00
|
|
|
// Figure out which symbols need to be internalized. This also needs to happen
|
|
|
|
// at -O0 because summary-based DCE is implemented using internalization, and
|
|
|
|
// we must apply DCE consistently with the full LTO module in order to avoid
|
|
|
|
// undefined references during the final link.
|
|
|
|
std::set<GlobalValue::GUID> ExportedGUIDs;
|
|
|
|
for (auto &Res : GlobalResolutions) {
|
2018-01-26 01:23:27 +08:00
|
|
|
// If the symbol does not have external references or it is not prevailing,
|
|
|
|
// then not need to mark it as exported from a ThinLTO partition.
|
|
|
|
if (Res.second.Partition != GlobalResolution::External ||
|
2018-01-29 16:03:30 +08:00
|
|
|
!Res.second.isPrevailingIRSymbol())
|
2017-11-02 01:58:39 +08:00
|
|
|
continue;
|
|
|
|
auto GUID = GlobalValue::getGUID(
|
|
|
|
GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
|
|
|
|
// Mark exported unless index-based analysis determined it to be dead.
|
|
|
|
if (ThinLTO.CombinedIndex.isGUIDLive(GUID))
|
|
|
|
ExportedGUIDs.insert(GUID);
|
2016-11-01 06:12:21 +08:00
|
|
|
}
|
2016-08-11 22:58:12 +08:00
|
|
|
|
2017-11-02 01:58:39 +08:00
|
|
|
// Any functions referenced by the jump table in the regular LTO object must
|
|
|
|
// be exported.
|
|
|
|
for (auto &Def : ThinLTO.CombinedIndex.cfiFunctionDefs())
|
|
|
|
ExportedGUIDs.insert(
|
|
|
|
GlobalValue::getGUID(GlobalValue::dropLLVMManglingEscape(Def)));
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2017-05-26 07:40:11 +08:00
|
|
|
auto isPrevailing = [&](GlobalValue::GUID GUID,
|
|
|
|
const GlobalValueSummary *S) {
|
|
|
|
return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath();
|
|
|
|
};
|
|
|
|
auto recordNewLinkage = [&](StringRef ModuleIdentifier,
|
|
|
|
GlobalValue::GUID GUID,
|
|
|
|
GlobalValue::LinkageTypes NewLinkage) {
|
|
|
|
ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
|
|
|
|
};
|
|
|
|
thinLTOResolveWeakForLinkerInIndex(ThinLTO.CombinedIndex, isPrevailing,
|
|
|
|
recordNewLinkage);
|
|
|
|
|
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
|
|
|
|
2017-12-16 10:10:00 +08:00
|
|
|
// Tasks 0 through ParallelCodeGenParallelismLevel-1 are reserved for combined
|
|
|
|
// module and parallel code generation partitions.
|
|
|
|
unsigned Task = RegularLTO.ParallelCodeGenParallelismLevel;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return BackendProc->wait();
|
2016-05-24 06:54:06 +08:00
|
|
|
}
|
2017-02-11 07:49:38 +08:00
|
|
|
|
2018-03-08 09:13:10 +08:00
|
|
|
Expected<std::unique_ptr<ToolOutputFile>>
|
|
|
|
lto::setupOptimizationRemarks(LLVMContext &Context,
|
|
|
|
StringRef LTORemarksFilename,
|
|
|
|
bool LTOPassRemarksWithHotness, int Count) {
|
2017-02-11 07:49:38 +08:00
|
|
|
if (LTORemarksFilename.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
std::string Filename = LTORemarksFilename;
|
|
|
|
if (Count != -1)
|
|
|
|
Filename += ".thin." + llvm::utostr(Count) + ".yaml";
|
|
|
|
|
|
|
|
std::error_code EC;
|
|
|
|
auto DiagnosticFile =
|
2017-09-23 09:03:17 +08:00
|
|
|
llvm::make_unique<ToolOutputFile>(Filename, EC, sys::fs::F_None);
|
2017-02-11 07:49:38 +08:00
|
|
|
if (EC)
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
Context.setDiagnosticsOutputFile(
|
|
|
|
llvm::make_unique<yaml::Output>(DiagnosticFile->os()));
|
|
|
|
if (LTOPassRemarksWithHotness)
|
[ORE] Unify spelling as "diagnostics hotness"
Summary:
To enable profile hotness information in diagnostics output, Clang takes
the option `-fdiagnostics-show-hotness` -- that's "diagnostics", with an
"s" at the end. Clang also defines `CodeGenOptions::DiagnosticsWithHotness`.
LLVM, on the other hand, defines
`LLVMContext::getDiagnosticHotnessRequested` -- that's "diagnostic", not
"diagnostics". It's a small difference, but it's confusing, typo-inducing, and
frustrating.
Add a new method with the spelling "diagnostics", and "deprecate" the
old spelling.
Reviewers: anemet, davidxl
Reviewed By: anemet
Subscribers: llvm-commits, mehdi_amini
Differential Revision: https://reviews.llvm.org/D34864
llvm-svn: 306848
2017-07-01 02:13:59 +08:00
|
|
|
Context.setDiagnosticsHotnessRequested(true);
|
2017-02-11 07:49:38 +08:00
|
|
|
DiagnosticFile->keep();
|
|
|
|
return std::move(DiagnosticFile);
|
|
|
|
}
|