#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/Triple.h"
#include "llvm/IR/ModuleSummaryIndex.h"
-#include "llvm/LTO/LTO.h"
#include "llvm/Support/CachePruning.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/MemoryBuffer.h"
class LLVMContext;
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
struct TargetMachineBuilder {
Triple TheTriple;
* and additionally resolve weak and linkonce symbols.
* Index is updated to reflect linkage changes from weak resolution.
*/
- void promote(Module &Module, ModuleSummaryIndex &Index,
- const lto::InputFile &File);
+ void promote(Module &Module, ModuleSummaryIndex &Index);
/**
* Compute and emit the imported files for module at \p ModulePath.
*/
void emitImports(Module &Module, StringRef OutputName,
- ModuleSummaryIndex &Index,
- const lto::InputFile &File);
+ ModuleSummaryIndex &Index);
/**
* Perform cross-module importing for the module identified by
* ModuleIdentifier.
*/
- void crossModuleImport(Module &Module, ModuleSummaryIndex &Index,
- const lto::InputFile &File);
+ void crossModuleImport(Module &Module, ModuleSummaryIndex &Index);
/**
* Compute the list of summaries needed for importing into module.
*/
void gatherImportedSummariesForModule(
Module &Module, ModuleSummaryIndex &Index,
- std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
- const lto::InputFile &File);
+ std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
/**
* Perform internalization. Index is updated to reflect linkage changes.
*/
- void internalize(Module &Module, ModuleSummaryIndex &Index,
- const lto::InputFile &File);
+ void internalize(Module &Module, ModuleSummaryIndex &Index);
/**
* Perform post-importing ThinLTO optimizations.
/// Vector holding the input buffers containing the bitcode modules to
/// 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
/// files.
}
}
-static StringMap<lto::InputFile *>
-generateModuleMap(std::vector<std::unique_ptr<lto::InputFile>> &Modules) {
- StringMap<lto::InputFile *> ModuleMap;
- for (auto &M : Modules) {
- assert(ModuleMap.find(M->getName()) == ModuleMap.end() &&
+static StringMap<MemoryBufferRef>
+generateModuleMap(const std::vector<ThinLTOBuffer> &Modules) {
+ StringMap<MemoryBufferRef> ModuleMap;
+ for (auto &ModuleBuffer : Modules) {
+ assert(ModuleMap.find(ModuleBuffer.getBufferIdentifier()) ==
+ ModuleMap.end() &&
"Expect unique Buffer Identifier");
- ModuleMap[M->getName()] = M.get();
+ ModuleMap[ModuleBuffer.getBufferIdentifier()] = ModuleBuffer.getMemBuffer();
}
return ModuleMap;
}
}
}
-static std::unique_ptr<Module> loadModuleFromInput(lto::InputFile *Input,
- LLVMContext &Context,
- bool Lazy,
- bool IsImporting) {
- auto &Mod = Input->getSingleBitcodeModule();
+static std::unique_ptr<Module>
+loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context,
+ bool Lazy, bool IsImporting) {
SMDiagnostic Err;
Expected<std::unique_ptr<Module>> ModuleOrErr =
- Lazy ? Mod.getLazyModule(Context,
- /* ShouldLazyLoadMetadata */ true, IsImporting)
- : Mod.parseModule(Context);
+ Lazy
+ ? getLazyBitcodeModule(Buffer, Context,
+ /* ShouldLazyLoadMetadata */ true, IsImporting)
+ : parseBitcodeFile(Buffer, Context);
if (!ModuleOrErr) {
handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
- SMDiagnostic Err = SMDiagnostic(Mod.getModuleIdentifier(),
+ SMDiagnostic Err = SMDiagnostic(Buffer.getBufferIdentifier(),
SourceMgr::DK_Error, EIB.message());
Err.print("ThinLTO", errs());
});
}
if (!Lazy)
verifyLoadedModule(*ModuleOrErr.get());
- return std::move(*ModuleOrErr);
+ return std::move(ModuleOrErr.get());
}
static void
crossImportIntoModule(Module &TheModule, const ModuleSummaryIndex &Index,
- StringMap<lto::InputFile*> &ModuleMap,
+ StringMap<MemoryBufferRef> &ModuleMap,
const FunctionImporter::ImportMapTy &ImportList) {
auto Loader = [&](StringRef Identifier) {
- auto &Input = ModuleMap[Identifier];
- return loadModuleFromInput(Input, TheModule.getContext(),
- /*Lazy=*/true, /*IsImporting*/ true);
+ return loadModuleFromBuffer(ModuleMap[Identifier], TheModule.getContext(),
+ /*Lazy=*/true, /*IsImporting*/ true);
};
FunctionImporter Importer(Index, Loader);
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.
static DenseSet<GlobalValue::GUID>
computeGUIDPreservedSymbols(const StringSet<> &PreservedSymbols,
static std::unique_ptr<MemoryBuffer>
ProcessThinLTOModule(Module &TheModule, ModuleSummaryIndex &Index,
- StringMap<lto::InputFile *> &ModuleMap, TargetMachine &TM,
+ StringMap<MemoryBufferRef> &ModuleMap, TargetMachine &TM,
const FunctionImporter::ImportMapTy &ImportList,
const FunctionImporter::ExportSetTy &ExportList,
const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols,
} // end anonymous namespace
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 (!InputOrError)
- report_fatal_error("ThinLTO cannot create input file");
+ if (TripleOrErr)
+ TripleStr = *TripleOrErr;
- auto TripleStr = (*InputOrError)->getTargetTriple();
Triple TheTriple(TripleStr);
if (Modules.empty())
initTMBuilder(TMBuilder, Triple(TMBuilder.TheTriple.merge(TheTriple)));
}
- Modules.emplace_back(std::move(*InputOrError));
+ Modules.push_back(Buffer);
}
void ThinLTOCodeGenerator::preserveSymbol(StringRef Name) {
std::unique_ptr<ModuleSummaryIndex> CombinedIndex =
llvm::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
uint64_t NextModuleId = 0;
- for (auto &Mod : Modules) {
- auto &M = Mod->getSingleBitcodeModule();
- if (Error Err =
- M.readSummary(*CombinedIndex, Mod->getName(), NextModuleId++)) {
+ for (auto &ModuleBuffer : Modules) {
+ if (Error Err = readModuleSummaryIndex(ModuleBuffer.getMemBuffer(),
+ *CombinedIndex, NextModuleId++)) {
// FIXME diagnose
logAllUnhandledErrors(
std::move(Err), errs(),
* Perform promotion and renaming of exported internal functions.
* Index is updated to reflect linkage changes from weak resolution.
*/
-void ThinLTOCodeGenerator::promote(Module &TheModule, ModuleSummaryIndex &Index,
- const lto::InputFile &File) {
+void ThinLTOCodeGenerator::promote(Module &TheModule,
+ ModuleSummaryIndex &Index) {
auto ModuleCount = Index.modulePaths().size();
auto ModuleIdentifier = TheModule.getModuleIdentifier();
auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
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!
computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
resolvePrevailingInIndex(Index, ResolvedODR);
+ thinLTOResolvePrevailingInModule(
+ TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
+
// Promote the exported values in the index, so that they are promoted
// in the module.
internalizeAndPromoteInIndex(ExportLists, GUIDPreservedSymbols, Index);
promoteModule(TheModule, Index);
-
- thinLTOResolvePrevailingInModule(
- TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
}
/**
* Perform cross-module importing for the module identified by ModuleIdentifier.
*/
void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
- ModuleSummaryIndex &Index,
- const lto::InputFile &File) {
+ ModuleSummaryIndex &Index) {
auto ModuleMap = generateModuleMap(Modules);
auto ModuleCount = Index.modulePaths().size();
auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
PreservedSymbols, Triple(TheModule.getTargetTriple()));
- addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
-
// Compute "dead" symbols, we don't want to import/export these!
computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
*/
void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
Module &TheModule, ModuleSummaryIndex &Index,
- std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
- const lto::InputFile &File) {
+ std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
auto ModuleCount = Index.modulePaths().size();
auto ModuleIdentifier = TheModule.getModuleIdentifier();
auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
PreservedSymbols, Triple(TheModule.getTargetTriple()));
- addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
-
// Compute "dead" symbols, we don't want to import/export these!
computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
* Emit the list of files needed for importing into module.
*/
void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName,
- ModuleSummaryIndex &Index,
- const lto::InputFile &File) {
+ ModuleSummaryIndex &Index) {
auto ModuleCount = Index.modulePaths().size();
auto ModuleIdentifier = TheModule.getModuleIdentifier();
auto GUIDPreservedSymbols = computeGUIDPreservedSymbols(
PreservedSymbols, Triple(TheModule.getTargetTriple()));
- addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
-
// Compute "dead" symbols, we don't want to import/export these!
computeDeadSymbolsInIndex(Index, GUIDPreservedSymbols);
* Perform internalization. Index is updated to reflect linkage changes.
*/
void ThinLTOCodeGenerator::internalize(Module &TheModule,
- ModuleSummaryIndex &Index,
- const lto::InputFile &File) {
+ ModuleSummaryIndex &Index) {
initTMBuilder(TMBuilder, Triple(TheModule.getTargetTriple()));
auto ModuleCount = Index.modulePaths().size();
auto ModuleIdentifier = TheModule.getModuleIdentifier();
auto GUIDPreservedSymbols =
computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
- addUsedSymbolToPreservedGUID(File, GUIDPreservedSymbols);
-
// Collect for each module the list of function it defines (GUID -> Summary).
StringMap<GVSummaryMapTy> ModuleToDefinedGVSummaries(ModuleCount);
Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
// Perform only parallel codegen and return.
ThreadPool Pool;
int count = 0;
- for (auto &Mod : Modules) {
+ for (auto &ModuleBuffer : Modules) {
Pool.async([&](int count) {
LLVMContext Context;
Context.setDiscardValueNames(LTODiscardValueNames);
// Parse module now
- auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
- /*IsImporting*/ false);
+ auto TheModule =
+ loadModuleFromBuffer(ModuleBuffer.getMemBuffer(), Context, false,
+ /*IsImporting*/ false);
// CodeGen
auto OutputBuffer = codegenModule(*TheModule, *TMBuilder.create());
auto GUIDPreservedSymbols =
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!
computeDeadSymbolsInIndex(*Index, GUIDPreservedSymbols);
// GVSummary and ResolvedODR maps to enable threaded access to these maps
// below.
for (auto &Module : Modules) {
- auto ModuleIdentifier = Module->getName();
+ auto ModuleIdentifier = Module.getBufferIdentifier();
ExportLists[ModuleIdentifier];
ImportLists[ModuleIdentifier];
ResolvedODR[ModuleIdentifier];
ModulesOrdering.resize(Modules.size());
std::iota(ModulesOrdering.begin(), ModulesOrdering.end(), 0);
llvm::sort(ModulesOrdering, [&](int LeftIndex, int RightIndex) {
- auto LSize =
- Modules[LeftIndex]->getSingleBitcodeModule().getBuffer().size();
- auto RSize =
- Modules[RightIndex]->getSingleBitcodeModule().getBuffer().size();
+ auto LSize = Modules[LeftIndex].getBuffer().size();
+ auto RSize = Modules[RightIndex].getBuffer().size();
return LSize > RSize;
});
{
ThreadPool Pool(ThreadCount);
for (auto IndexCount : ModulesOrdering) {
- auto &Mod = Modules[IndexCount];
+ auto &ModuleBuffer = Modules[IndexCount];
Pool.async([&](int count) {
- auto ModuleIdentifier = Mod->getName();
+ auto ModuleIdentifier = ModuleBuffer.getBufferIdentifier();
auto &ExportList = ExportLists[ModuleIdentifier];
auto &DefinedGVSummaries = ModuleToDefinedGVSummaries[ModuleIdentifier];
}
// Parse module now
- auto TheModule = loadModuleFromInput(Mod.get(), Context, false,
- /*IsImporting*/ false);
+ auto TheModule =
+ loadModuleFromBuffer(ModuleBuffer.getMemBuffer(), Context, false,
+ /*IsImporting*/ false);
// Save temps: original file.
saveTempBitcode(*TheModule, SaveTempsDir, count, ".0.original.bc");
return ExitOnErr(getModuleSummaryIndexForFile(ThinLTOIndex));
}
-static std::unique_ptr<MemoryBuffer> loadFile(StringRef Filename) {
- ExitOnError ExitOnErr("llvm-lto: error loading file '" + Filename.str() +
- "': ");
- return ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Filename)));
-}
-
-static std::unique_ptr<lto::InputFile> loadInputFile(MemoryBufferRef Buffer) {
- 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.");
+static std::unique_ptr<Module> loadModule(StringRef Filename,
+ LLVMContext &Ctx) {
+ SMDiagnostic Err;
+ std::unique_ptr<Module> M(parseIRFile(Filename, Err, Ctx));
+ if (!M) {
+ Err.print("llvm-lto", errs());
+ report_fatal_error("Can't load module for file " + Filename);
}
- maybeVerifyModule(**ModuleOrErr);
+ maybeVerifyModule(*M);
+
if (ThinLTOModuleId.getNumOccurrences()) {
if (InputFilenames.size() != 1)
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) {
auto Index = loadCombinedIndex();
for (auto &Filename : InputFilenames) {
LLVMContext Ctx;
- auto Buffer = loadFile(Filename);
- auto Input = loadInputFile(Buffer->getMemBufferRef());
- auto TheModule = loadModuleFromInput(*Input, Ctx);
+ auto TheModule = loadModule(Filename, Ctx);
// Build a map of module to the GUIDs and summary objects that should
// be written to its index.
std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
- ThinGenerator.gatherImportedSummariesForModule(
- *TheModule, *Index, ModuleToSummariesForIndex, *Input);
+ ThinGenerator.gatherImportedSummariesForModule(*TheModule, *Index,
+ ModuleToSummariesForIndex);
std::string OutputName = OutputFilename;
if (OutputName.empty()) {
auto Index = loadCombinedIndex();
for (auto &Filename : InputFilenames) {
LLVMContext Ctx;
- auto Buffer = loadFile(Filename);
- auto Input = loadInputFile(Buffer->getMemBufferRef());
- auto TheModule = loadModuleFromInput(*Input, Ctx);
+ auto TheModule = loadModule(Filename, Ctx);
std::string OutputName = OutputFilename;
if (OutputName.empty()) {
OutputName = Filename + ".imports";
}
- OutputName =
- getThinLTOOutputFile(OutputName, OldPrefix, NewPrefix);
- ThinGenerator.emitImports(*TheModule, OutputName, *Index, *Input);
+ OutputName = getThinLTOOutputFile(OutputName, OldPrefix, NewPrefix);
+ ThinGenerator.emitImports(*TheModule, OutputName, *Index);
}
}
auto Index = loadCombinedIndex();
for (auto &Filename : InputFilenames) {
LLVMContext Ctx;
- auto Buffer = loadFile(Filename);
- auto Input = loadInputFile(Buffer->getMemBufferRef());
- auto TheModule = loadModuleFromInput(*Input, Ctx);
+ auto TheModule = loadModule(Filename, Ctx);
- ThinGenerator.promote(*TheModule, *Index, *Input);
+ ThinGenerator.promote(*TheModule, *Index);
std::string OutputName = OutputFilename;
if (OutputName.empty()) {
for (auto &Filename : InputFilenames) {
LLVMContext Ctx;
- auto Buffer = loadFile(Filename);
- auto Input = loadInputFile(Buffer->getMemBufferRef());
- auto TheModule = loadModuleFromInput(*Input, Ctx);
+ auto TheModule = loadModule(Filename, Ctx);
- ThinGenerator.crossModuleImport(*TheModule, *Index, *Input);
+ ThinGenerator.crossModuleImport(*TheModule, *Index);
std::string OutputName = OutputFilename;
if (OutputName.empty()) {
for (auto &Filename : InputFilenames) {
LLVMContext Ctx;
- auto Buffer = loadFile(Filename);
- auto Input = loadInputFile(Buffer->getMemBufferRef());
- auto TheModule = loadModuleFromInput(*Input, Ctx);
+ auto TheModule = loadModule(Filename, Ctx);
- ThinGenerator.internalize(*TheModule, *Index, *Input);
+ ThinGenerator.internalize(*TheModule, *Index);
std::string OutputName = OutputFilename;
if (OutputName.empty()) {
for (auto &Filename : InputFilenames) {
LLVMContext Ctx;
- auto Buffer = loadFile(Filename);
- auto Input = loadInputFile(Buffer->getMemBufferRef());
- auto TheModule = loadModuleFromInput(*Input, Ctx);
+ auto TheModule = loadModule(Filename, Ctx);
ThinGenerator.optimize(*TheModule);