Revert [ThinLTO] Fix ThinLTOCodegenerator to export llvm.used symbols

This reverts r357931 (git commit 8b70a5c11e08116955a875b9085433f14737bcaf)

llvm-svn: 357932
This commit is contained in:
Steven Wu 2019-04-08 18:53:21 +00:00
parent 9ddbd54259
commit e757d7f8e8
7 changed files with 99 additions and 184 deletions

View File

@ -131,7 +131,6 @@ public:
using irsymtab::Symbol::isWeak; using irsymtab::Symbol::isWeak;
using irsymtab::Symbol::isIndirect; using irsymtab::Symbol::isIndirect;
using irsymtab::Symbol::getName; using irsymtab::Symbol::getName;
using irsymtab::Symbol::getIRName;
using irsymtab::Symbol::getVisibility; using irsymtab::Symbol::getVisibility;
using irsymtab::Symbol::canBeOmittedFromSymbolTable; using irsymtab::Symbol::canBeOmittedFromSymbolTable;
using irsymtab::Symbol::isTLS; using irsymtab::Symbol::isTLS;
@ -162,9 +161,6 @@ public:
// Returns a table with all the comdats used by this file. // Returns a table with all the comdats used by this file.
ArrayRef<StringRef> getComdatTable() const { return ComdatTable; } ArrayRef<StringRef> getComdatTable() const { return ComdatTable; }
// Returns the only BitcodeModule from InputFile.
BitcodeModule &getSingleBitcodeModule();
private: private:
ArrayRef<Symbol> module_symbols(unsigned I) const { ArrayRef<Symbol> module_symbols(unsigned I) const {
const auto &Indices = ModuleSymIndices[I]; const auto &Indices = ModuleSymIndices[I];

View File

@ -19,7 +19,6 @@
#include "llvm/ADT/StringSet.h" #include "llvm/ADT/StringSet.h"
#include "llvm/ADT/Triple.h" #include "llvm/ADT/Triple.h"
#include "llvm/IR/ModuleSummaryIndex.h" #include "llvm/IR/ModuleSummaryIndex.h"
#include "llvm/LTO/LTO.h"
#include "llvm/Support/CachePruning.h" #include "llvm/Support/CachePruning.h"
#include "llvm/Support/CodeGen.h" #include "llvm/Support/CodeGen.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
@ -32,6 +31,23 @@ class StringRef;
class LLVMContext; class LLVMContext;
class TargetMachine; class TargetMachine;
/// Wrapper around MemoryBufferRef, owning the identifier
class ThinLTOBuffer {
std::string OwnedIdentifier;
StringRef Buffer;
public:
ThinLTOBuffer(StringRef Buffer, StringRef Identifier)
: OwnedIdentifier(Identifier), Buffer(Buffer) {}
MemoryBufferRef getMemBuffer() const {
return MemoryBufferRef(Buffer,
{OwnedIdentifier.c_str(), OwnedIdentifier.size()});
}
StringRef getBuffer() const { return Buffer; }
StringRef getBufferIdentifier() const { return OwnedIdentifier; }
};
/// Helper to gather options relevant to the target machine creation /// Helper to gather options relevant to the target machine creation
struct TargetMachineBuilder { struct TargetMachineBuilder {
Triple TheTriple; Triple TheTriple;
@ -251,36 +267,31 @@ public:
* and additionally resolve weak and linkonce symbols. * and additionally resolve weak and linkonce symbols.
* Index is updated to reflect linkage changes from weak resolution. * Index is updated to reflect linkage changes from weak resolution.
*/ */
void promote(Module &Module, ModuleSummaryIndex &Index, void promote(Module &Module, ModuleSummaryIndex &Index);
const lto::InputFile &File);
/** /**
* Compute and emit the imported files for module at \p ModulePath. * Compute and emit the imported files for module at \p ModulePath.
*/ */
void emitImports(Module &Module, StringRef OutputName, void emitImports(Module &Module, StringRef OutputName,
ModuleSummaryIndex &Index, ModuleSummaryIndex &Index);
const lto::InputFile &File);
/** /**
* Perform cross-module importing for the module identified by * Perform cross-module importing for the module identified by
* ModuleIdentifier. * ModuleIdentifier.
*/ */
void crossModuleImport(Module &Module, ModuleSummaryIndex &Index, void crossModuleImport(Module &Module, ModuleSummaryIndex &Index);
const lto::InputFile &File);
/** /**
* Compute the list of summaries needed for importing into module. * Compute the list of summaries needed for importing into module.
*/ */
void gatherImportedSummariesForModule( void gatherImportedSummariesForModule(
Module &Module, ModuleSummaryIndex &Index, Module &Module, ModuleSummaryIndex &Index,
std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex, std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
const lto::InputFile &File);
/** /**
* Perform internalization. Index is updated to reflect linkage changes. * Perform internalization. Index is updated to reflect linkage changes.
*/ */
void internalize(Module &Module, ModuleSummaryIndex &Index, void internalize(Module &Module, ModuleSummaryIndex &Index);
const lto::InputFile &File);
/** /**
* Perform post-importing ThinLTO optimizations. * Perform post-importing ThinLTO optimizations.
@ -302,7 +313,7 @@ private:
/// Vector holding the input buffers containing the bitcode modules to /// Vector holding the input buffers containing the bitcode modules to
/// process. /// process.
std::vector<std::unique_ptr<lto::InputFile>> Modules; std::vector<ThinLTOBuffer> Modules;
/// Set of symbols that need to be preserved outside of the set of bitcode /// Set of symbols that need to be preserved outside of the set of bitcode
/// files. /// files.

View File

@ -420,11 +420,6 @@ StringRef InputFile::getName() const {
return Mods[0].getModuleIdentifier(); return Mods[0].getModuleIdentifier();
} }
BitcodeModule &InputFile::getSingleBitcodeModule() {
assert(Mods.size() == 1 && "Expect only one bitcode module");
return Mods[0];
}
LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel, LTO::RegularLTOState::RegularLTOState(unsigned ParallelCodeGenParallelismLevel,
Config &Conf) Config &Conf)
: ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel), : ParallelCodeGenParallelismLevel(ParallelCodeGenParallelismLevel),

View File

@ -135,13 +135,14 @@ static void computePrevailingCopies(
} }
} }
static StringMap<lto::InputFile *> static StringMap<MemoryBufferRef>
generateModuleMap(std::vector<std::unique_ptr<lto::InputFile>> &Modules) { generateModuleMap(const std::vector<ThinLTOBuffer> &Modules) {
StringMap<lto::InputFile *> ModuleMap; StringMap<MemoryBufferRef> ModuleMap;
for (auto &M : Modules) { for (auto &ModuleBuffer : Modules) {
assert(ModuleMap.find(M->getName()) == ModuleMap.end() && assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
ModuleMap.end() &&
"Expect unique Buffer Identifier"); "Expect unique Buffer Identifier");
ModuleMap[M->getName()] = M.get(); ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer.getMemBuffer();
} }
return ModuleMap; return ModuleMap;
} }
@ -174,19 +175,18 @@ static void verifyLoadedModule(Module &TheModule) {
} }
} }
static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile *Input, static std::unique_ptr<Module>
LLVMContext &Context, loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context,
bool Lazy, bool Lazy, bool IsImporting) {
bool IsImporting) {
auto &Mod = Input->getSingleBitcodeModule();
SMDiagnostic Err; SMDiagnostic Err;
Expected<std::unique_ptr<Module>> ModuleOrErr = Expected<std::unique_ptr<Module>> ModuleOrErr =
Lazy ? Mod.getLazyModule(Context, Lazy
/* ShouldLazyLoadMetadata */ true, IsImporting) ? getLazyBitcodeModule(Buffer, Context,
: Mod.parseModule(Context); /* ShouldLazyLoadMetadata */ true, IsImporting)
: parseBitcodeFile(Buffer, Context);
if (!ModuleOrErr) { if (!ModuleOrErr) {
handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) { handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
SMDiagnostic Err = SMDiagnostic(Mod.getModuleIdentifier(), SMDiagnostic Err = SMDiagnostic(Buffer.getBufferIdentifier(),
SourceMgr::DK_Error, EIB.message()); SourceMgr::DK_Error, EIB.message());
Err.print("ThinLTO", errs()); Err.print("ThinLTO", errs());
}); });
@ -194,17 +194,16 @@ static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile *Input,
} }
if (!Lazy) if (!Lazy)
verifyLoadedModule(*ModuleOrErr.get()); verifyLoadedModule(*ModuleOrErr.get());
return std::move(*ModuleOrErr); return std::move(ModuleOrErr.get());
} }
static void static void
crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index, crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
StringMap<lto::InputFile*> &ModuleMap, StringMap<MemoryBufferRef> &ModuleMap,
const FunctionImporter::ImportMapTy &ImportList) { const FunctionImporter::ImportMapTy &ImportList) {
auto Loader = [&](StringRef Identifier) { auto Loader = [&](StringRef Identifier) {
auto &Input = ModuleMap[Identifier]; return loadModuleFromBuffer(ModuleMap[Identifier], TheModule.getContext(),
return loadModuleFromInput(Input, TheModule.getContext(), /*Lazy=*/true, /*IsImporting*/ true);
/*Lazy=*/true, /*IsImporting*/ true);
}; };
FunctionImporter Importer(Index, Loader); FunctionImporter Importer(Index, Loader);
@ -249,15 +248,6 @@ static void optimizeModule(Module &TheModule, TargetMachine &TM,
PM.run(TheModule); PM.run(TheModule);
} }
static void
addUsedSymbolToPreservedGUID(const lto::InputFile &File,
DenseSet<GlobalValue::GUID> &PreservedGUID) {
for (const auto &Sym : File.symbols()) {
if (Sym.isUsed())
PreservedGUID.insert(GlobalValue::getGUID(Sym.getIRName()));
}
}
// Convert the PreservedSymbols map from "Name" based to "GUID" based. // Convert the PreservedSymbols map from "Name" based to "GUID" based.
static DenseSet<GlobalValue::GUID> static DenseSet<GlobalValue::GUID>
computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols, computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
@ -391,7 +381,7 @@ public:
static std::unique_ptr<MemoryBuffer> static std::unique_ptr<MemoryBuffer>
ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index, ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
StringMap<lto::InputFile *> &ModuleMap, TargetMachine &TM, StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
const FunctionImporter::ImportMapTy &ImportList, const FunctionImporter::ImportMapTy &ImportList,
const FunctionImporter::ExportSetTy &ExportList, const FunctionImporter::ExportSetTy &ExportList,
const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols, const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
@ -498,13 +488,15 @@ static void initTMBuilder(TargetMachineBuilder &TMBuilder,
} // end anonymous namespace } // end anonymous namespace
void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) { void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
MemoryBufferRef Buffer(Data, Identifier); ThinLTOBuffer Buffer(Data, Identifier);
LLVMContext Context;
StringRef TripleStr;
ErrorOr<std::string> TripleOrErr = expectedToErrorOrAndEmitErrors(
Context, getBitcodeTargetTriple(Buffer.getMemBuffer()));
auto InputOrError = lto::InputFile::create(Buffer); if (TripleOrErr)
if (!InputOrError) TripleStr = *TripleOrErr;
report_fatal_error("ThinLTO cannot create input file");
auto TripleStr = (*InputOrError)->getTargetTriple();
Triple TheTriple(TripleStr); Triple TheTriple(TripleStr);
if (Modules.empty()) if (Modules.empty())
@ -516,7 +508,7 @@ void ThinLTOCodeGenerator::addModule(StringRef Identifier, StringRef Data) {
initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple))); initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
} }
Modules.emplace_back(std::move(*InputOrError)); Modules.push_back(Buffer);
} }
void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) { void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
@ -557,10 +549,9 @@ std::unique_ptr<ModuleSummaryIndex> ThinLTOCodeGenerator::linkCombinedIndex() {
std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
llvm::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false); llvm::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
uint64_t NextModuleId = 0; uint64_t NextModuleId = 0;
for (auto &Mod : Modules) { for (auto &ModuleBuffer : Modules) {
auto &M = Mod->getSingleBitcodeModule(); if (Error Err = readModuleSummaryIndex(ModuleBuffer.getMemBuffer(),
if (Error Err = *CombinedIndex, NextModuleId++)) {
M.readSummary(*CombinedIndex, Mod->getName(), NextModuleId++)) {
// FIXME diagnose // FIXME diagnose
logAllUnhandledErrors( logAllUnhandledErrors(
std::move(Err), errs(), std::move(Err), errs(),
@ -602,8 +593,8 @@ static void computeDeadSymbolsInIndex(
* Perform promotion and renaming of exported internal functions. * Perform promotion and renaming of exported internal functions.
* Index is updated to reflect linkage changes from weak resolution. * Index is updated to reflect linkage changes from weak resolution.
*/ */
void ThinLTOCodeGenerator::promote(Module &TheModule, ModuleSummaryIndex &Index, void ThinLTOCodeGenerator::promote(Module &TheModule,
const lto::InputFile &File) { ModuleSummaryIndex &Index) {
auto ModuleCount = Index.modulePaths().size(); auto ModuleCount = Index.modulePaths().size();
auto ModuleIdentifier = TheModule.getModuleIdentifier(); auto ModuleIdentifier = TheModule.getModuleIdentifier();
@ -615,9 +606,6 @@ void ThinLTOCodeGenerator::promote(Module &TheModule, ModuleSummaryIndex &Index,
auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
PreservedSymbols, Triple(TheModule.getTargetTriple())); PreservedSymbols, Triple(TheModule.getTargetTriple()));
// Add used symbol to the preserved symbols.
addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
// Compute "dead" symbols, we don't want to import/export these! // Compute "dead" symbols, we don't want to import/export these!
computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
@ -631,22 +619,21 @@ void ThinLTOCodeGenerator::promote(Module &TheModule, ModuleSummaryIndex &Index,
StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR; StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
resolvePrevailingInIndex(Index, ResolvedODR); resolvePrevailingInIndex(Index, ResolvedODR);
thinLTOResolvePrevailingInModule(
TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
// Promote the exported values in the index, so that they are promoted // Promote the exported values in the index, so that they are promoted
// in the module. // in the module.
internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index); internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index);
promoteModule(TheModule, Index); promoteModule(TheModule, Index);
thinLTOResolvePrevailingInModule(
TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
} }
/** /**
* Perform cross-module importing for the module identified by ModuleIdentifier. * Perform cross-module importing for the module identified by ModuleIdentifier.
*/ */
void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule, void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
ModuleSummaryIndex &Index, ModuleSummaryIndex &Index) {
const lto::InputFile &File) {
auto ModuleMap = generateModuleMap(Modules); auto ModuleMap = generateModuleMap(Modules);
auto ModuleCount = Index.modulePaths().size(); auto ModuleCount = Index.modulePaths().size();
@ -658,8 +645,6 @@ void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
PreservedSymbols, Triple(TheModule.getTargetTriple())); PreservedSymbols, Triple(TheModule.getTargetTriple()));
addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
// Compute "dead" symbols, we don't want to import/export these! // Compute "dead" symbols, we don't want to import/export these!
computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
@ -678,8 +663,7 @@ void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
*/ */
void ThinLTOCodeGenerator::gatherImportedSummariesForModule( void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
Module &TheModule, ModuleSummaryIndex &Index, Module &TheModule, ModuleSummaryIndex &Index,
std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex, std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
const lto::InputFile &File) {
auto ModuleCount = Index.modulePaths().size(); auto ModuleCount = Index.modulePaths().size();
auto ModuleIdentifier = TheModule.getModuleIdentifier(); auto ModuleIdentifier = TheModule.getModuleIdentifier();
@ -691,8 +675,6 @@ void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
PreservedSymbols, Triple(TheModule.getTargetTriple())); PreservedSymbols, Triple(TheModule.getTargetTriple()));
addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
// Compute "dead" symbols, we don't want to import/export these! // Compute "dead" symbols, we don't want to import/export these!
computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
@ -711,8 +693,7 @@ void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
* Emit the list of files needed for importing into module. * Emit the list of files needed for importing into module.
*/ */
void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName, void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName,
ModuleSummaryIndex &Index, ModuleSummaryIndex &Index) {
const lto::InputFile &File) {
auto ModuleCount = Index.modulePaths().size(); auto ModuleCount = Index.modulePaths().size();
auto ModuleIdentifier = TheModule.getModuleIdentifier(); auto ModuleIdentifier = TheModule.getModuleIdentifier();
@ -724,8 +705,6 @@ void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName,
auto GUIDPreservedSymbols = computeGUIDPreservedSymbols( auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
PreservedSymbols, Triple(TheModule.getTargetTriple())); PreservedSymbols, Triple(TheModule.getTargetTriple()));
addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
// Compute "dead" symbols, we don't want to import/export these! // Compute "dead" symbols, we don't want to import/export these!
computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols); computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
@ -751,8 +730,7 @@ void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName,
* Perform internalization. Index is updated to reflect linkage changes. * Perform internalization. Index is updated to reflect linkage changes.
*/ */
void ThinLTOCodeGenerator::internalize(Module &TheModule, void ThinLTOCodeGenerator::internalize(Module &TheModule,
ModuleSummaryIndex &Index, ModuleSummaryIndex &Index) {
const lto::InputFile &File) {
initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple())); initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
auto ModuleCount = Index.modulePaths().size(); auto ModuleCount = Index.modulePaths().size();
auto ModuleIdentifier = TheModule.getModuleIdentifier(); auto ModuleIdentifier = TheModule.getModuleIdentifier();
@ -761,8 +739,6 @@ void ThinLTOCodeGenerator::internalize(Module &TheModule,
auto GUIDPreservedSymbols = auto GUIDPreservedSymbols =
computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple); computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
// Collect for each module the list of function it defines (GUID -> Summary). // Collect for each module the list of function it defines (GUID -> Summary).
StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount); StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries); Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
@ -854,14 +830,15 @@ void ThinLTOCodeGenerator::run() {
// Perform only parallel codegen and return. // Perform only parallel codegen and return.
ThreadPool Pool; ThreadPool Pool;
int count = 0; int count = 0;
for (auto &Mod : Modules) { for (auto &ModuleBuffer : Modules) {
Pool.async([&](int count) { Pool.async([&](int count) {
LLVMContext Context; LLVMContext Context;
Context.setDiscardValueNames(LTODiscardValueNames); Context.setDiscardValueNames(LTODiscardValueNames);
// Parse module now // Parse module now
auto TheModule = loadModuleFromInput(Mod.get(), Context, false, auto TheModule =
/*IsImporting*/ false); loadModuleFromBuffer(ModuleBuffer.getMemBuffer(), Context, false,
/*IsImporting*/ false);
// CodeGen // CodeGen
auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create()); auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
@ -904,10 +881,6 @@ void ThinLTOCodeGenerator::run() {
auto GUIDPreservedSymbols = auto GUIDPreservedSymbols =
computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple); computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
// Add used symbol from inputs to the preserved symbols.
for (const auto &M : Modules)
addUsedSymbolToPreservedGUID(*M, GUIDPreservedSymbols);
// Compute "dead" symbols, we don't want to import/export these! // Compute "dead" symbols, we don't want to import/export these!
computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols); computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
@ -940,7 +913,7 @@ void ThinLTOCodeGenerator::run() {
// GVSummary and ResolvedODR maps to enable threaded access to these maps // GVSummary and ResolvedODR maps to enable threaded access to these maps
// below. // below.
for (auto &Module : Modules) { for (auto &Module : Modules) {
auto ModuleIdentifier = Module->getName(); auto ModuleIdentifier = Module.getBufferIdentifier();
ExportLists[ModuleIdentifier]; ExportLists[ModuleIdentifier];
ImportLists[ModuleIdentifier]; ImportLists[ModuleIdentifier];
ResolvedODR[ModuleIdentifier]; ResolvedODR[ModuleIdentifier];
@ -954,10 +927,8 @@ void ThinLTOCodeGenerator::run() {
ModulesOrdering.resize(Modules.size()); ModulesOrdering.resize(Modules.size());
std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0); std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) { llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
auto LSize = auto LSize = Modules[LeftIndex].getBuffer().size();
Modules[LeftIndex]->getSingleBitcodeModule().getBuffer().size(); auto RSize = Modules[RightIndex].getBuffer().size();
auto RSize =
Modules[RightIndex]->getSingleBitcodeModule().getBuffer().size();
return LSize > RSize; return LSize > RSize;
}); });
@ -965,9 +936,9 @@ void ThinLTOCodeGenerator::run() {
{ {
ThreadPool Pool(ThreadCount); ThreadPool Pool(ThreadCount);
for (auto IndexCount : ModulesOrdering) { for (auto IndexCount : ModulesOrdering) {
auto &Mod = Modules[IndexCount]; auto &ModuleBuffer = Modules[IndexCount];
Pool.async([&](int count) { Pool.async([&](int count) {
auto ModuleIdentifier = Mod->getName(); auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier();
auto &ExportList = ExportLists[ModuleIdentifier]; auto &ExportList = ExportLists[ModuleIdentifier];
auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier]; auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
@ -1011,8 +982,9 @@ void ThinLTOCodeGenerator::run() {
} }
// Parse module now // Parse module now
auto TheModule = loadModuleFromInput(Mod.get(), Context, false, auto TheModule =
/*IsImporting*/ false); loadModuleFromBuffer(ModuleBuffer.getMemBuffer(), Context, false,
/*IsImporting*/ false);
// Save temps: original file. // Save temps: original file.
saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc"); saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");

View File

@ -1,10 +0,0 @@
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.15.0"
define i32 @main() {
entry:
%call = call i32 @bar()
ret i32 0
}
declare i32 @bar()

View File

@ -1,21 +0,0 @@
; RUN: opt -module-summary -o %t.bc %s
; RUN: opt -module-summary -o %t-main.bc %S/Inputs/thinlto-internalize-used2.ll
; RUN: llvm-lto -thinlto-action=thinlink %t.bc %t-main.bc -o %t-index.bc
; RUN: llvm-lto -thinlto-action=promote -thinlto-index %t-index.bc %t.bc -o %t.promote.bc
; RUN: llvm-dis %t.promote.bc -o - | FileCheck %s
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.15.0"
@llvm.used = appending global [1 x i8*] [i8* bitcast (i32 ()* @foo to i8*)], section "llvm.metadata"
; Make sure foo is not internalized.
; CHECK: define i32 @foo()
define i32 @foo() {
ret i32 0
}
define hidden i32 @bar() {
ret i32 0
}

View File

@ -449,37 +449,22 @@ std::unique_ptr<ModuleSummaryIndex> loadCombinedIndex() {
return ExitOnErr(getModuleSummaryIndexForFile(ThinLTOIndex)); return ExitOnErr(getModuleSummaryIndexForFile(ThinLTOIndex));
} }
static std::unique_ptr<MemoryBuffer> loadFile(StringRef Filename) { static std::unique_ptr<Module> loadModule(StringRef Filename,
ExitOnError ExitOnErr("llvm-lto: error loading file '" + Filename.str() + LLVMContext &Ctx) {
"': "); SMDiagnostic Err;
return ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Filename))); std::unique_ptr<Module> M(parseIRFile(Filename, Err, Ctx));
} if (!M) {
Err.print("llvm-lto", errs());
static std::unique_ptr<lto::InputFile> loadInputFile(MemoryBufferRef Buffer) { report_fatal_error("Can't load module for file " + Filename);
ExitOnError ExitOnErr("llvm-lto: error loading input '" +
Buffer.getBufferIdentifier().str() + "': ");
return ExitOnErr(lto::InputFile::create(Buffer));
}
static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile &File,
LLVMContext &CTX) {
auto &Mod = File.getSingleBitcodeModule();
auto ModuleOrErr = Mod.parseModule(CTX);
if (!ModuleOrErr) {
handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
SMDiagnostic Err = SMDiagnostic(Mod.getModuleIdentifier(),
SourceMgr::DK_Error, EIB.message());
Err.print("llvm-lto", errs());
});
report_fatal_error("Can't load module, abort.");
} }
maybeVerifyModule(**ModuleOrErr); maybeVerifyModule(*M);
if (ThinLTOModuleId.getNumOccurrences()) { if (ThinLTOModuleId.getNumOccurrences()) {
if (InputFilenames.size() != 1) if (InputFilenames.size() != 1)
report_fatal_error("Can't override the module id for multiple files"); report_fatal_error("Can't override the module id for multiple files");
(*ModuleOrErr)->setModuleIdentifier(ThinLTOModuleId); M->setModuleIdentifier(ThinLTOModuleId);
} }
return std::move(*ModuleOrErr); return M;
} }
static void writeModuleToFile(Module &TheModule, StringRef Filename) { static void writeModuleToFile(Module &TheModule, StringRef Filename) {
@ -577,15 +562,13 @@ private:
auto Index = loadCombinedIndex(); auto Index = loadCombinedIndex();
for (auto &Filename : InputFilenames) { for (auto &Filename : InputFilenames) {
LLVMContext Ctx; LLVMContext Ctx;
auto Buffer = loadFile(Filename); auto TheModule = loadModule(Filename, Ctx);
auto Input = loadInputFile(Buffer->getMemBufferRef());
auto TheModule = loadModuleFromInput(*Input, Ctx);
// Build a map of module to the GUIDs and summary objects that should // Build a map of module to the GUIDs and summary objects that should
// be written to its index. // be written to its index.
std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex; std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
ThinGenerator.gatherImportedSummariesForModule( ThinGenerator.gatherImportedSummariesForModule(*TheModule, *Index,
*TheModule, *Index, ModuleToSummariesForIndex, *Input); ModuleToSummariesForIndex);
std::string OutputName = OutputFilename; std::string OutputName = OutputFilename;
if (OutputName.empty()) { if (OutputName.empty()) {
@ -614,16 +597,13 @@ private:
auto Index = loadCombinedIndex(); auto Index = loadCombinedIndex();
for (auto &Filename : InputFilenames) { for (auto &Filename : InputFilenames) {
LLVMContext Ctx; LLVMContext Ctx;
auto Buffer = loadFile(Filename); auto TheModule = loadModule(Filename, Ctx);
auto Input = loadInputFile(Buffer->getMemBufferRef());
auto TheModule = loadModuleFromInput(*Input, Ctx);
std::string OutputName = OutputFilename; std::string OutputName = OutputFilename;
if (OutputName.empty()) { if (OutputName.empty()) {
OutputName = Filename + ".imports"; OutputName = Filename + ".imports";
} }
OutputName = OutputName = getThinLTOOutputFile(OutputName, OldPrefix, NewPrefix);
getThinLTOOutputFile(OutputName, OldPrefix, NewPrefix); ThinGenerator.emitImports(*TheModule, OutputName, *Index);
ThinGenerator.emitImports(*TheModule, OutputName, *Index, *Input);
} }
} }
@ -641,11 +621,9 @@ private:
auto Index = loadCombinedIndex(); auto Index = loadCombinedIndex();
for (auto &Filename : InputFilenames) { for (auto &Filename : InputFilenames) {
LLVMContext Ctx; LLVMContext Ctx;
auto Buffer = loadFile(Filename); auto TheModule = loadModule(Filename, Ctx);
auto Input = loadInputFile(Buffer->getMemBufferRef());
auto TheModule = loadModuleFromInput(*Input, Ctx);
ThinGenerator.promote(*TheModule, *Index, *Input); ThinGenerator.promote(*TheModule, *Index);
std::string OutputName = OutputFilename; std::string OutputName = OutputFilename;
if (OutputName.empty()) { if (OutputName.empty()) {
@ -674,11 +652,9 @@ private:
for (auto &Filename : InputFilenames) { for (auto &Filename : InputFilenames) {
LLVMContext Ctx; LLVMContext Ctx;
auto Buffer = loadFile(Filename); auto TheModule = loadModule(Filename, Ctx);
auto Input = loadInputFile(Buffer->getMemBufferRef());
auto TheModule = loadModuleFromInput(*Input, Ctx);
ThinGenerator.crossModuleImport(*TheModule, *Index, *Input); ThinGenerator.crossModuleImport(*TheModule, *Index);
std::string OutputName = OutputFilename; std::string OutputName = OutputFilename;
if (OutputName.empty()) { if (OutputName.empty()) {
@ -707,11 +683,9 @@ private:
for (auto &Filename : InputFilenames) { for (auto &Filename : InputFilenames) {
LLVMContext Ctx; LLVMContext Ctx;
auto Buffer = loadFile(Filename); auto TheModule = loadModule(Filename, Ctx);
auto Input = loadInputFile(Buffer->getMemBufferRef());
auto TheModule = loadModuleFromInput(*Input, Ctx);
ThinGenerator.internalize(*TheModule, *Index, *Input); ThinGenerator.internalize(*TheModule, *Index);
std::string OutputName = OutputFilename; std::string OutputName = OutputFilename;
if (OutputName.empty()) { if (OutputName.empty()) {
@ -732,9 +706,7 @@ private:
for (auto &Filename : InputFilenames) { for (auto &Filename : InputFilenames) {
LLVMContext Ctx; LLVMContext Ctx;
auto Buffer = loadFile(Filename); auto TheModule = loadModule(Filename, Ctx);
auto Input = loadInputFile(Buffer->getMemBufferRef());
auto TheModule = loadModuleFromInput(*Input, Ctx);
ThinGenerator.optimize(*TheModule); ThinGenerator.optimize(*TheModule);