return Result;
}
-// Get a Module for \p FileName from the cache, or load it lazily.
-Module &ModuleLazyLoaderCache::operator()(StringRef FileName) {
- auto &Module = ModuleMap[FileName];
- if (!Module)
- Module = loadFile(FileName, Context);
- return *Module;
-}
-
/// Walk through the instructions in \p F looking for external
/// calls not already in the \p CalledFunctions set. If any are
/// found they are added to the \p Worklist for importing.
Module &DestModule, SmallVector<StringRef, 64> &Worklist,
StringSet<> &CalledFunctions, Linker &TheLinker,
const FunctionInfoIndex &Index,
- std::function<Module &(StringRef FileName)> &LazyModuleLoader) {
+ std::function<std::unique_ptr<Module>(StringRef FileName)> &
+ LazyModuleLoader) {
unsigned ImportCount = 0;
while (!Worklist.empty()) {
auto CalledFunctionName = Worklist.pop_back_val();
- DEBUG(dbgs() << "Process import for " << CalledFunctionName << "\n");
+ DEBUG(dbgs() << DestModule.getModuleIdentifier() << "Process import for "
+ << CalledFunctionName << "\n");
// Try to get a summary for this function call.
auto InfoList = Index.findFunctionInfoList(CalledFunctionName);
if (InfoList == Index.end()) {
- DEBUG(dbgs() << "No summary for " << CalledFunctionName
- << " Ignoring.\n");
+ DEBUG(dbgs() << DestModule.getModuleIdentifier() << "No summary for "
+ << CalledFunctionName << " Ignoring.\n");
continue;
}
assert(!InfoList->second.empty() && "No summary, error at import?");
auto *Summary = Info->functionSummary();
if (!Summary) {
// FIXME: in case we are lazyloading summaries, we can do it now.
- DEBUG(dbgs() << "Missing summary for " << CalledFunctionName
+ DEBUG(dbgs() << DestModule.getModuleIdentifier()
+ << " Missing summary for " << CalledFunctionName
<< ", error at import?\n");
llvm_unreachable("Missing summary");
}
if (Summary->instCount() > ImportInstrLimit) {
- DEBUG(dbgs() << "Skip import of " << CalledFunctionName << " with "
- << Summary->instCount() << " instructions (limit "
- << ImportInstrLimit << ")\n");
+ DEBUG(dbgs() << DestModule.getModuleIdentifier() << " Skip import of "
+ << CalledFunctionName << " with " << Summary->instCount()
+ << " instructions (limit " << ImportInstrLimit << ")\n");
continue;
}
DEBUG(dbgs() << "Importing " << CalledFunctionName << " from " << FileName
<< "\n");
- // Get the module for the import (potentially from the cache).
- auto &Module = LazyModuleLoader(FileName);
- assert(&Module.getContext() == &DestModule.getContext());
+ // Get the module for the import
+ auto SrcModule = LazyModuleLoader(FileName);
+ assert(&SrcModule->getContext() == &DestModule.getContext());
// The function that we will import!
- GlobalValue *SGV = Module.getNamedValue(CalledFunctionName);
+ GlobalValue *SGV = SrcModule->getNamedValue(CalledFunctionName);
StringRef ImportFunctionName = CalledFunctionName;
if (!SGV) {
// Might be local in source Module, promoted/renamed in DestModule.
std::pair<StringRef, StringRef> Split =
CalledFunctionName.split(".llvm.");
- SGV = Module.getNamedValue(Split.first);
+ SGV = SrcModule->getNamedValue(Split.first);
#ifndef NDEBUG
// Assert that Split.second is module id
uint64_t ModuleId;
// the order they are seen and selected by the linker, changing program
// semantics.
if (SGV->hasWeakAnyLinkage()) {
- DEBUG(dbgs() << "Ignoring import request for weak-any "
+ DEBUG(dbgs() << DestModule.getModuleIdentifier()
+ << " Ignoring import request for weak-any "
<< (isa<Function>(SGV) ? "function " : "alias ")
<< CalledFunctionName << " from " << FileName << "\n");
continue;
// Link in the specified function.
DenseSet<const GlobalValue *> FunctionsToImport;
FunctionsToImport.insert(F);
- if (TheLinker.linkInModule(Module, Linker::Flags::None, &Index,
+ if (TheLinker.linkInModule(*SrcModule, Linker::Flags::None, &Index,
&FunctionsToImport))
report_fatal_error("Function Import: link error");
// The current implementation imports every called functions that exists in the
// summaries index.
bool FunctionImporter::importFunctions(Module &DestModule) {
- DEBUG(errs() << "Starting import for Module "
+ DEBUG(dbgs() << "Starting import for Module "
<< DestModule.getModuleIdentifier() << "\n");
unsigned ImportedCount = 0;
Linker TheLinker(DestModule, DiagnosticHandler);
ImportedCount += ProcessImportWorklist(DestModule, Worklist, CalledFunctions,
- TheLinker, Index, getLazyModule);
+ TheLinker, Index, ModuleLoader);
DEBUG(errs() << "Imported " << ImportedCount << " functions for Module "
<< DestModule.getModuleIdentifier() << "\n");
}
// Perform the import now.
- ModuleLazyLoaderCache Loader(M.getContext());
- FunctionImporter Importer(*Index, diagnosticHandler,
- [&](StringRef Name)
- -> Module &{ return Loader(Name); });
+ auto ModuleLoader = [&M](StringRef Identifier) {
+ return loadFile(Identifier, M.getContext());
+ };
+ FunctionImporter Importer(*Index, diagnosticHandler, ModuleLoader);
return Importer.importFunctions(M);
return false;