2015-11-24 14:07:49 +08:00
|
|
|
//===- FunctionImport.cpp - ThinLTO Summary-based Function Import ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements Function import based on summaries.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO/FunctionImport.h"
|
|
|
|
|
|
|
|
#include "llvm/ADT/StringSet.h"
|
|
|
|
#include "llvm/IR/AutoUpgrade.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IRReader/IRReader.h"
|
|
|
|
#include "llvm/Linker/Linker.h"
|
2016-03-15 05:18:10 +08:00
|
|
|
#include "llvm/Object/FunctionIndexObjectFile.h"
|
2015-11-24 14:07:49 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2016-02-11 02:11:31 +08:00
|
|
|
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
|
2015-12-09 16:17:35 +08:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2015-11-24 14:07:49 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "function-import"
|
|
|
|
|
2015-11-25 06:55:46 +08:00
|
|
|
/// Limit on instruction count of imported functions.
|
|
|
|
static cl::opt<unsigned> ImportInstrLimit(
|
|
|
|
"import-instr-limit", cl::init(100), cl::Hidden, cl::value_desc("N"),
|
|
|
|
cl::desc("Only import functions with less than N instructions"));
|
|
|
|
|
2016-02-11 07:31:45 +08:00
|
|
|
static cl::opt<float>
|
|
|
|
ImportInstrFactor("import-instr-evolution-factor", cl::init(0.7),
|
|
|
|
cl::Hidden, cl::value_desc("x"),
|
|
|
|
cl::desc("As we import functions, multiply the "
|
|
|
|
"`import-instr-limit` threshold by this factor "
|
|
|
|
"before processing newly imported functions"));
|
|
|
|
|
2015-11-24 14:07:49 +08:00
|
|
|
// Load lazily a module from \p FileName in \p Context.
|
|
|
|
static std::unique_ptr<Module> loadFile(const std::string &FileName,
|
|
|
|
LLVMContext &Context) {
|
|
|
|
SMDiagnostic Err;
|
|
|
|
DEBUG(dbgs() << "Loading '" << FileName << "'\n");
|
2016-01-22 08:15:53 +08:00
|
|
|
// Metadata isn't loaded until functions are imported, to minimize
|
|
|
|
// the memory overhead.
|
2016-01-08 22:17:41 +08:00
|
|
|
std::unique_ptr<Module> Result =
|
|
|
|
getLazyIRFileModule(FileName, Err, Context,
|
|
|
|
/* ShouldLazyLoadMetadata = */ true);
|
2015-11-24 14:07:49 +08:00
|
|
|
if (!Result) {
|
|
|
|
Err.print("function-import", errs());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2015-12-09 16:17:35 +08:00
|
|
|
namespace {
|
2016-02-11 07:31:45 +08:00
|
|
|
|
|
|
|
/// Track functions already seen using a map that record the current
|
|
|
|
/// Threshold and the importing decision. Since the traversal of the call graph
|
|
|
|
/// is DFS, we can revisit a function a second time with a higher threshold. In
|
|
|
|
/// this case and if the function was not imported the first time, it is added
|
|
|
|
/// back to the worklist with the new threshold
|
|
|
|
using VisitedFunctionTrackerTy = StringMap<std::pair<unsigned, bool>>;
|
|
|
|
|
2015-12-09 16:17:35 +08:00
|
|
|
/// Helper to load on demand a Module from file and cache it for subsequent
|
|
|
|
/// queries. It can be used with the FunctionImporter.
|
|
|
|
class ModuleLazyLoaderCache {
|
|
|
|
/// Cache of lazily loaded module for import.
|
|
|
|
StringMap<std::unique_ptr<Module>> ModuleMap;
|
|
|
|
|
|
|
|
/// Retrieve a Module from the cache or lazily load it on demand.
|
|
|
|
std::function<std::unique_ptr<Module>(StringRef FileName)> createLazyModule;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Create the loader, Module will be initialized in \p Context.
|
|
|
|
ModuleLazyLoaderCache(std::function<
|
|
|
|
std::unique_ptr<Module>(StringRef FileName)> createLazyModule)
|
|
|
|
: createLazyModule(createLazyModule) {}
|
|
|
|
|
|
|
|
/// Retrieve a Module from the cache or lazily load it on demand.
|
|
|
|
Module &operator()(StringRef FileName);
|
2015-12-17 07:16:33 +08:00
|
|
|
|
|
|
|
std::unique_ptr<Module> takeModule(StringRef FileName) {
|
|
|
|
auto I = ModuleMap.find(FileName);
|
|
|
|
assert(I != ModuleMap.end());
|
|
|
|
std::unique_ptr<Module> Ret = std::move(I->second);
|
|
|
|
ModuleMap.erase(I);
|
|
|
|
return Ret;
|
|
|
|
}
|
2015-12-09 16:17:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Get a Module for \p FileName from the cache, or load it lazily.
|
|
|
|
Module &ModuleLazyLoaderCache::operator()(StringRef Identifier) {
|
|
|
|
auto &Module = ModuleMap[Identifier];
|
|
|
|
if (!Module)
|
|
|
|
Module = createLazyModule(Identifier);
|
|
|
|
return *Module;
|
|
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-11-25 05:15:19 +08:00
|
|
|
/// Walk through the instructions in \p F looking for external
|
2016-02-11 07:31:45 +08:00
|
|
|
/// calls not already in the \p VisitedFunctions map. If any are
|
2015-11-25 05:15:19 +08:00
|
|
|
/// found they are added to the \p Worklist for importing.
|
2016-02-11 07:31:45 +08:00
|
|
|
static void findExternalCalls(
|
2016-03-15 05:18:10 +08:00
|
|
|
const Module &DestModule, Function &F, const FunctionInfoIndex &Index,
|
2016-02-11 07:31:45 +08:00
|
|
|
VisitedFunctionTrackerTy &VisitedFunctions, unsigned Threshold,
|
|
|
|
SmallVectorImpl<std::pair<StringRef, unsigned>> &Worklist) {
|
2015-12-09 16:17:35 +08:00
|
|
|
// We need to suffix internal function calls imported from other modules,
|
|
|
|
// prepare the suffix ahead of time.
|
2015-12-10 04:41:10 +08:00
|
|
|
std::string Suffix;
|
2015-12-09 16:17:35 +08:00
|
|
|
if (F.getParent() != &DestModule)
|
|
|
|
Suffix =
|
|
|
|
(Twine(".llvm.") +
|
|
|
|
Twine(Index.getModuleId(F.getParent()->getModuleIdentifier()))).str();
|
|
|
|
|
2015-11-25 05:15:19 +08:00
|
|
|
for (auto &BB : F) {
|
|
|
|
for (auto &I : BB) {
|
|
|
|
if (isa<CallInst>(I)) {
|
|
|
|
auto CalledFunction = cast<CallInst>(I).getCalledFunction();
|
|
|
|
// Insert any new external calls that have not already been
|
|
|
|
// added to set/worklist.
|
2015-12-09 16:17:35 +08:00
|
|
|
if (!CalledFunction || !CalledFunction->hasName())
|
|
|
|
continue;
|
|
|
|
// Ignore intrinsics early
|
|
|
|
if (CalledFunction->isIntrinsic()) {
|
|
|
|
assert(CalledFunction->getIntrinsicID() != 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto ImportedName = CalledFunction->getName();
|
|
|
|
auto Renamed = (ImportedName + Suffix).str();
|
|
|
|
// Rename internal functions
|
|
|
|
if (CalledFunction->hasInternalLinkage()) {
|
|
|
|
ImportedName = Renamed;
|
|
|
|
}
|
2016-03-15 05:18:10 +08:00
|
|
|
// Compute the global identifier used in the function index.
|
2016-02-11 05:55:02 +08:00
|
|
|
auto CalledFunctionGlobalID = Function::getGlobalIdentifier(
|
|
|
|
CalledFunction->getName(), CalledFunction->getLinkage(),
|
|
|
|
CalledFunction->getParent()->getSourceFileName());
|
2016-02-11 07:31:45 +08:00
|
|
|
|
|
|
|
auto CalledFunctionInfo = std::make_pair(Threshold, false);
|
|
|
|
auto It = VisitedFunctions.insert(
|
|
|
|
std::make_pair(CalledFunctionGlobalID, CalledFunctionInfo));
|
2015-12-09 16:17:35 +08:00
|
|
|
if (!It.second) {
|
2016-02-11 07:31:45 +08:00
|
|
|
// This is a call to a function we already considered, if the function
|
|
|
|
// has been imported the first time, or if the current threshold is
|
|
|
|
// not higher, skip it.
|
|
|
|
auto &FunctionInfo = It.first->second;
|
|
|
|
if (FunctionInfo.second || FunctionInfo.first >= Threshold)
|
|
|
|
continue;
|
|
|
|
It.first->second = CalledFunctionInfo;
|
2015-12-09 16:17:35 +08:00
|
|
|
}
|
|
|
|
// Ignore functions already present in the destination module
|
|
|
|
auto *SrcGV = DestModule.getNamedValue(ImportedName);
|
|
|
|
if (SrcGV) {
|
2016-01-13 01:48:44 +08:00
|
|
|
if (GlobalAlias *SGA = dyn_cast<GlobalAlias>(SrcGV))
|
|
|
|
SrcGV = SGA->getBaseObject();
|
2015-12-09 16:17:35 +08:00
|
|
|
assert(isa<Function>(SrcGV) && "Name collision during import");
|
|
|
|
if (!cast<Function>(SrcGV)->isDeclaration()) {
|
2015-12-11 00:39:07 +08:00
|
|
|
DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Ignoring "
|
2015-12-09 16:17:35 +08:00
|
|
|
<< ImportedName << " already in DestinationModule\n");
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-25 05:15:19 +08:00
|
|
|
}
|
2015-12-09 16:17:35 +08:00
|
|
|
|
2016-02-11 07:31:45 +08:00
|
|
|
Worklist.push_back(std::make_pair(It.first->getKey(), Threshold));
|
2015-12-09 16:17:35 +08:00
|
|
|
DEBUG(dbgs() << DestModule.getModuleIdentifier()
|
2015-12-11 00:39:07 +08:00
|
|
|
<< ": Adding callee for : " << ImportedName << " : "
|
2015-12-09 16:17:35 +08:00
|
|
|
<< F.getName() << "\n");
|
2015-11-25 05:15:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 10:37:33 +08:00
|
|
|
// Helper function: given a worklist and an index, will process all the worklist
|
2015-12-09 16:17:35 +08:00
|
|
|
// and decide what to import based on the summary information.
|
|
|
|
//
|
|
|
|
// Nothing is actually imported, functions are materialized in their source
|
|
|
|
// module and analyzed there.
|
|
|
|
//
|
|
|
|
// \p ModuleToFunctionsToImportMap is filled with the set of Function to import
|
|
|
|
// per Module.
|
2016-02-11 07:31:45 +08:00
|
|
|
static void
|
|
|
|
GetImportList(Module &DestModule,
|
|
|
|
SmallVectorImpl<std::pair<StringRef, unsigned>> &Worklist,
|
|
|
|
VisitedFunctionTrackerTy &VisitedFunctions,
|
2016-03-15 05:18:10 +08:00
|
|
|
std::map<StringRef, DenseSet<const GlobalValue *>> &
|
|
|
|
ModuleToFunctionsToImportMap,
|
|
|
|
const FunctionInfoIndex &Index,
|
2016-02-11 07:31:45 +08:00
|
|
|
ModuleLazyLoaderCache &ModuleLoaderCache) {
|
2015-11-24 14:07:49 +08:00
|
|
|
while (!Worklist.empty()) {
|
2016-02-11 07:31:45 +08:00
|
|
|
StringRef CalledFunctionName;
|
|
|
|
unsigned Threshold;
|
|
|
|
std::tie(CalledFunctionName, Threshold) = Worklist.pop_back_val();
|
2015-12-11 00:39:07 +08:00
|
|
|
DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Process import for "
|
2016-02-11 07:31:45 +08:00
|
|
|
<< CalledFunctionName << " with Threshold " << Threshold
|
|
|
|
<< "\n");
|
2015-11-24 14:07:49 +08:00
|
|
|
|
|
|
|
// Try to get a summary for this function call.
|
[ThinLTO] Support for reference graph in per-module and combined summary.
Summary:
This patch adds support for including a full reference graph including
call graph edges and other GV references in the summary.
The reference graph edges can be used to make importing decisions
without materializing any source modules, can be used in the plugin
to make file staging decisions for distributed build systems, and is
expected to have other uses.
The call graph edges are recorded in each function summary in the
bitcode via a list of <CalleeValueIds, StaticCount> tuples when no PGO
data exists, or <CalleeValueId, StaticCount, ProfileCount> pairs when
there is PGO, where the ValueId can be mapped to the function GUID via
the ValueSymbolTable. In the function index in memory, the call graph
edges reference the target via the CalleeGUID instead of the
CalleeValueId.
The reference graph edges are recorded in each summary record with a
list of referenced value IDs, which can be mapped to value GUID via the
ValueSymbolTable.
Addtionally, a new summary record type is added to record references
from global variable initializers. A number of bitcode records and data
structures have been renamed to reflect the newly expanded scope of the
summary beyond functions. More cleanup will follow.
Reviewers: joker.eph, davidxl
Subscribers: joker.eph, llvm-commits
Differential Revision: http://reviews.llvm.org/D17212
llvm-svn: 263275
2016-03-12 02:52:24 +08:00
|
|
|
auto InfoList = Index.findGlobalValueInfoList(CalledFunctionName);
|
2015-11-24 14:07:49 +08:00
|
|
|
if (InfoList == Index.end()) {
|
2015-12-11 00:39:07 +08:00
|
|
|
DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": No summary for "
|
2015-12-09 07:04:19 +08:00
|
|
|
<< CalledFunctionName << " Ignoring.\n");
|
2015-11-24 14:07:49 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
assert(!InfoList->second.empty() && "No summary, error at import?");
|
|
|
|
|
|
|
|
// Comdat can have multiple entries, FIXME: what do we do with them?
|
|
|
|
auto &Info = InfoList->second[0];
|
|
|
|
assert(Info && "Nullptr in list, error importing summaries?\n");
|
|
|
|
|
[ThinLTO] Support for reference graph in per-module and combined summary.
Summary:
This patch adds support for including a full reference graph including
call graph edges and other GV references in the summary.
The reference graph edges can be used to make importing decisions
without materializing any source modules, can be used in the plugin
to make file staging decisions for distributed build systems, and is
expected to have other uses.
The call graph edges are recorded in each function summary in the
bitcode via a list of <CalleeValueIds, StaticCount> tuples when no PGO
data exists, or <CalleeValueId, StaticCount, ProfileCount> pairs when
there is PGO, where the ValueId can be mapped to the function GUID via
the ValueSymbolTable. In the function index in memory, the call graph
edges reference the target via the CalleeGUID instead of the
CalleeValueId.
The reference graph edges are recorded in each summary record with a
list of referenced value IDs, which can be mapped to value GUID via the
ValueSymbolTable.
Addtionally, a new summary record type is added to record references
from global variable initializers. A number of bitcode records and data
structures have been renamed to reflect the newly expanded scope of the
summary beyond functions. More cleanup will follow.
Reviewers: joker.eph, davidxl
Subscribers: joker.eph, llvm-commits
Differential Revision: http://reviews.llvm.org/D17212
llvm-svn: 263275
2016-03-12 02:52:24 +08:00
|
|
|
auto *Summary = dyn_cast<FunctionSummary>(Info->summary());
|
2015-11-24 14:07:49 +08:00
|
|
|
if (!Summary) {
|
|
|
|
// FIXME: in case we are lazyloading summaries, we can do it now.
|
2015-12-09 07:04:19 +08:00
|
|
|
DEBUG(dbgs() << DestModule.getModuleIdentifier()
|
2015-12-11 00:39:07 +08:00
|
|
|
<< ": Missing summary for " << CalledFunctionName
|
2015-12-02 01:12:10 +08:00
|
|
|
<< ", error at import?\n");
|
2015-11-24 14:07:49 +08:00
|
|
|
llvm_unreachable("Missing summary");
|
|
|
|
}
|
|
|
|
|
2016-02-11 07:31:45 +08:00
|
|
|
if (Summary->instCount() > Threshold) {
|
2015-12-11 00:39:07 +08:00
|
|
|
DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Skip import of "
|
2015-12-09 07:04:19 +08:00
|
|
|
<< CalledFunctionName << " with " << Summary->instCount()
|
2016-02-11 07:31:45 +08:00
|
|
|
<< " instructions (limit " << Threshold << ")\n");
|
2015-11-25 06:55:46 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-11 07:31:45 +08:00
|
|
|
// Mark the function as imported in the VisitedFunctions tracker
|
|
|
|
assert(VisitedFunctions.count(CalledFunctionName));
|
|
|
|
VisitedFunctions[CalledFunctionName].second = true;
|
|
|
|
|
2015-11-24 14:07:49 +08:00
|
|
|
// Get the module path from the summary.
|
2015-12-09 16:17:35 +08:00
|
|
|
auto ModuleIdentifier = Summary->modulePath();
|
2015-12-11 00:39:07 +08:00
|
|
|
DEBUG(dbgs() << DestModule.getModuleIdentifier() << ": Importing "
|
2015-12-09 16:17:35 +08:00
|
|
|
<< CalledFunctionName << " from " << ModuleIdentifier << "\n");
|
2015-11-24 14:07:49 +08:00
|
|
|
|
2015-12-09 16:17:35 +08:00
|
|
|
auto &SrcModule = ModuleLoaderCache(ModuleIdentifier);
|
2015-11-24 14:07:49 +08:00
|
|
|
|
|
|
|
// The function that we will import!
|
2015-12-09 16:17:35 +08:00
|
|
|
GlobalValue *SGV = SrcModule.getNamedValue(CalledFunctionName);
|
|
|
|
|
2015-11-25 03:55:04 +08:00
|
|
|
if (!SGV) {
|
2016-02-11 05:55:02 +08:00
|
|
|
// The function is referenced by a global identifier, which has the
|
|
|
|
// source file name prepended for functions that were originally local
|
|
|
|
// in the source module. Strip any prepended name to recover the original
|
2015-12-09 16:17:35 +08:00
|
|
|
// name in the source module.
|
2016-02-11 07:47:38 +08:00
|
|
|
std::pair<StringRef, StringRef> Split = CalledFunctionName.rsplit(':');
|
2016-02-11 05:55:02 +08:00
|
|
|
SGV = SrcModule.getNamedValue(Split.second);
|
2015-12-09 16:17:35 +08:00
|
|
|
assert(SGV && "Can't find function to import in source module");
|
|
|
|
}
|
|
|
|
if (!SGV) {
|
|
|
|
report_fatal_error(Twine("Can't load function '") + CalledFunctionName +
|
|
|
|
"' in Module '" + SrcModule.getModuleIdentifier() +
|
|
|
|
"', error in the summary?\n");
|
2015-11-25 03:55:04 +08:00
|
|
|
}
|
2015-12-09 16:17:35 +08:00
|
|
|
|
2015-11-24 14:07:49 +08:00
|
|
|
Function *F = dyn_cast<Function>(SGV);
|
|
|
|
if (!F && isa<GlobalAlias>(SGV)) {
|
|
|
|
auto *SGA = dyn_cast<GlobalAlias>(SGV);
|
|
|
|
F = dyn_cast<Function>(SGA->getBaseObject());
|
2015-12-09 16:17:35 +08:00
|
|
|
CalledFunctionName = F->getName();
|
2015-11-24 14:07:49 +08:00
|
|
|
}
|
2015-12-09 16:17:35 +08:00
|
|
|
assert(F && "Imported Function is ... not a Function");
|
2015-11-24 14:07:49 +08:00
|
|
|
|
2015-11-25 00:10:43 +08:00
|
|
|
// We cannot import weak_any functions/aliases without possibly affecting
|
|
|
|
// the order they are seen and selected by the linker, changing program
|
2015-11-24 14:07:49 +08:00
|
|
|
// semantics.
|
2015-11-25 00:10:43 +08:00
|
|
|
if (SGV->hasWeakAnyLinkage()) {
|
2015-12-09 07:04:19 +08:00
|
|
|
DEBUG(dbgs() << DestModule.getModuleIdentifier()
|
2015-12-11 00:39:07 +08:00
|
|
|
<< ": Ignoring import request for weak-any "
|
2015-11-25 00:10:43 +08:00
|
|
|
<< (isa<Function>(SGV) ? "function " : "alias ")
|
2015-12-09 16:17:35 +08:00
|
|
|
<< CalledFunctionName << " from "
|
|
|
|
<< SrcModule.getModuleIdentifier() << "\n");
|
2015-11-24 14:07:49 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-12-09 16:17:35 +08:00
|
|
|
// Add the function to the import list
|
|
|
|
auto &Entry = ModuleToFunctionsToImportMap[SrcModule.getModuleIdentifier()];
|
2015-12-17 07:16:33 +08:00
|
|
|
Entry.insert(F);
|
2015-11-24 14:07:49 +08:00
|
|
|
|
2015-12-09 16:17:35 +08:00
|
|
|
// Process the newly imported functions and add callees to the worklist.
|
2016-02-11 07:31:45 +08:00
|
|
|
// Adjust the threshold
|
|
|
|
Threshold = Threshold * ImportInstrFactor;
|
2015-12-09 16:17:35 +08:00
|
|
|
F->materialize();
|
2016-02-11 07:31:45 +08:00
|
|
|
findExternalCalls(DestModule, *F, Index, VisitedFunctions, Threshold,
|
|
|
|
Worklist);
|
2015-12-03 10:37:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Automatically import functions in Module \p DestModule based on the summaries
|
|
|
|
// index.
|
|
|
|
//
|
|
|
|
// The current implementation imports every called functions that exists in the
|
|
|
|
// summaries index.
|
|
|
|
bool FunctionImporter::importFunctions(Module &DestModule) {
|
2015-12-09 07:04:19 +08:00
|
|
|
DEBUG(dbgs() << "Starting import for Module "
|
2015-12-03 10:58:14 +08:00
|
|
|
<< DestModule.getModuleIdentifier() << "\n");
|
2015-12-03 10:37:33 +08:00
|
|
|
unsigned ImportedCount = 0;
|
2015-11-24 14:07:49 +08:00
|
|
|
|
2016-02-11 07:31:45 +08:00
|
|
|
// First step is collecting the called external functions.
|
|
|
|
// We keep the function name as well as the import threshold for its callees.
|
|
|
|
VisitedFunctionTrackerTy VisitedFunctions;
|
|
|
|
SmallVector<std::pair<StringRef, unsigned>, 64> Worklist;
|
2015-12-03 10:37:33 +08:00
|
|
|
for (auto &F : DestModule) {
|
|
|
|
if (F.isDeclaration() || F.hasFnAttribute(Attribute::OptimizeNone))
|
|
|
|
continue;
|
2016-02-11 07:31:45 +08:00
|
|
|
findExternalCalls(DestModule, F, Index, VisitedFunctions, ImportInstrLimit,
|
|
|
|
Worklist);
|
2015-11-24 14:07:49 +08:00
|
|
|
}
|
2015-12-03 10:37:33 +08:00
|
|
|
if (Worklist.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/// Second step: for every call to an external function, try to import it.
|
|
|
|
|
|
|
|
// Linker that will be used for importing function
|
2015-12-15 07:17:03 +08:00
|
|
|
Linker TheLinker(DestModule);
|
2015-12-03 10:37:33 +08:00
|
|
|
|
2015-12-09 16:17:35 +08:00
|
|
|
// Map of Module -> List of Function to import from the Module
|
2015-12-17 07:16:33 +08:00
|
|
|
std::map<StringRef, DenseSet<const GlobalValue *>>
|
2015-12-09 16:17:35 +08:00
|
|
|
ModuleToFunctionsToImportMap;
|
2015-12-02 12:34:28 +08:00
|
|
|
|
2015-12-09 16:17:35 +08:00
|
|
|
// Analyze the summaries and get the list of functions to import by
|
|
|
|
// populating ModuleToFunctionsToImportMap
|
|
|
|
ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
|
2016-02-11 07:31:45 +08:00
|
|
|
GetImportList(DestModule, Worklist, VisitedFunctions,
|
2015-12-09 16:17:35 +08:00
|
|
|
ModuleToFunctionsToImportMap, Index, ModuleLoaderCache);
|
|
|
|
assert(Worklist.empty() && "Worklist hasn't been flushed in GetImportList");
|
|
|
|
|
|
|
|
// Do the actual import of functions now, one Module at a time
|
|
|
|
for (auto &FunctionsToImportPerModule : ModuleToFunctionsToImportMap) {
|
|
|
|
// Get the module for the import
|
2015-12-17 07:16:33 +08:00
|
|
|
auto &FunctionsToImport = FunctionsToImportPerModule.second;
|
|
|
|
std::unique_ptr<Module> SrcModule =
|
|
|
|
ModuleLoaderCache.takeModule(FunctionsToImportPerModule.first);
|
2015-12-09 16:17:35 +08:00
|
|
|
assert(&DestModule.getContext() == &SrcModule->getContext() &&
|
|
|
|
"Context mismatch");
|
|
|
|
|
2016-01-22 08:15:53 +08:00
|
|
|
// If modules were created with lazy metadata loading, materialize it
|
|
|
|
// now, before linking it (otherwise this will be a noop).
|
|
|
|
SrcModule->materializeMetadata();
|
|
|
|
UpgradeDebugInfo(*SrcModule);
|
2015-12-18 01:14:09 +08:00
|
|
|
|
2015-12-09 16:17:35 +08:00
|
|
|
// Link in the specified functions.
|
2015-12-17 07:16:33 +08:00
|
|
|
if (TheLinker.linkInModule(std::move(SrcModule), Linker::Flags::None,
|
2016-01-22 08:15:53 +08:00
|
|
|
&Index, &FunctionsToImport))
|
2015-12-09 16:17:35 +08:00
|
|
|
report_fatal_error("Function Import: link error");
|
|
|
|
|
|
|
|
ImportedCount += FunctionsToImport.size();
|
|
|
|
}
|
2015-12-18 01:14:09 +08:00
|
|
|
|
2015-12-09 16:17:35 +08:00
|
|
|
DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module "
|
2015-12-03 10:37:33 +08:00
|
|
|
<< DestModule.getModuleIdentifier() << "\n");
|
|
|
|
return ImportedCount;
|
2015-11-24 14:07:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Summary file to use for function importing when using -function-import from
|
|
|
|
/// the command line.
|
|
|
|
static cl::opt<std::string>
|
|
|
|
SummaryFile("summary-file",
|
|
|
|
cl::desc("The summary file to use for function importing."));
|
|
|
|
|
|
|
|
static void diagnosticHandler(const DiagnosticInfo &DI) {
|
|
|
|
raw_ostream &OS = errs();
|
|
|
|
DiagnosticPrinterRawOStream DP(OS);
|
|
|
|
DI.print(DP);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2016-03-15 05:18:10 +08:00
|
|
|
/// Parse the function index out of an IR file and return the function
|
2015-11-24 14:07:49 +08:00
|
|
|
/// index object if found, or nullptr if not.
|
2016-03-15 05:18:10 +08:00
|
|
|
static std::unique_ptr<FunctionInfoIndex>
|
|
|
|
getFunctionIndexForFile(StringRef Path, std::string &Error,
|
|
|
|
DiagnosticHandlerFunction DiagnosticHandler) {
|
2015-11-24 14:07:49 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> Buffer;
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
|
|
|
|
MemoryBuffer::getFile(Path);
|
|
|
|
if (std::error_code EC = BufferOrErr.getError()) {
|
|
|
|
Error = EC.message();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
Buffer = std::move(BufferOrErr.get());
|
2016-03-15 05:18:10 +08:00
|
|
|
ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr =
|
|
|
|
object::FunctionIndexObjectFile::create(Buffer->getMemBufferRef(),
|
|
|
|
DiagnosticHandler);
|
2015-11-24 14:07:49 +08:00
|
|
|
if (std::error_code EC = ObjOrErr.getError()) {
|
|
|
|
Error = EC.message();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return (*ObjOrErr)->takeIndex();
|
|
|
|
}
|
|
|
|
|
2015-12-24 18:03:35 +08:00
|
|
|
namespace {
|
2015-11-24 14:07:49 +08:00
|
|
|
/// Pass that performs cross-module function import provided a summary file.
|
|
|
|
class FunctionImportPass : public ModulePass {
|
2016-03-15 05:18:10 +08:00
|
|
|
/// Optional function summary index to use for importing, otherwise
|
2015-12-08 03:21:11 +08:00
|
|
|
/// the summary-file option must be specified.
|
2016-03-15 05:18:10 +08:00
|
|
|
const FunctionInfoIndex *Index;
|
2015-11-24 14:07:49 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// Pass identification, replacement for typeid
|
|
|
|
static char ID;
|
|
|
|
|
2015-12-08 03:21:11 +08:00
|
|
|
/// Specify pass name for debug output
|
|
|
|
const char *getPassName() const override {
|
|
|
|
return "Function Importing";
|
|
|
|
}
|
|
|
|
|
2016-03-15 05:18:10 +08:00
|
|
|
explicit FunctionImportPass(const FunctionInfoIndex *Index = nullptr)
|
2015-12-08 03:21:11 +08:00
|
|
|
: ModulePass(ID), Index(Index) {}
|
2015-11-24 14:07:49 +08:00
|
|
|
|
|
|
|
bool runOnModule(Module &M) override {
|
2015-12-08 03:21:11 +08:00
|
|
|
if (SummaryFile.empty() && !Index)
|
|
|
|
report_fatal_error("error: -function-import requires -summary-file or "
|
|
|
|
"file from frontend\n");
|
2016-03-15 05:18:10 +08:00
|
|
|
std::unique_ptr<FunctionInfoIndex> IndexPtr;
|
2015-12-08 03:21:11 +08:00
|
|
|
if (!SummaryFile.empty()) {
|
|
|
|
if (Index)
|
|
|
|
report_fatal_error("error: -summary-file and index from frontend\n");
|
|
|
|
std::string Error;
|
2016-03-15 05:18:10 +08:00
|
|
|
IndexPtr = getFunctionIndexForFile(SummaryFile, Error, diagnosticHandler);
|
2015-12-08 03:21:11 +08:00
|
|
|
if (!IndexPtr) {
|
|
|
|
errs() << "Error loading file '" << SummaryFile << "': " << Error
|
|
|
|
<< "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Index = IndexPtr.get();
|
2015-11-24 14:07:49 +08:00
|
|
|
}
|
|
|
|
|
2016-01-09 01:06:29 +08:00
|
|
|
// First we need to promote to global scope and rename any local values that
|
|
|
|
// are potentially exported to other modules.
|
2016-03-09 09:37:14 +08:00
|
|
|
if (renameModuleForThinLTO(M, *Index)) {
|
2016-01-09 01:06:29 +08:00
|
|
|
errs() << "Error renaming module\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-24 14:07:49 +08:00
|
|
|
// Perform the import now.
|
2015-12-09 06:39:40 +08:00
|
|
|
auto ModuleLoader = [&M](StringRef Identifier) {
|
|
|
|
return loadFile(Identifier, M.getContext());
|
|
|
|
};
|
2015-12-15 07:17:03 +08:00
|
|
|
FunctionImporter Importer(*Index, ModuleLoader);
|
2015-11-24 14:07:49 +08:00
|
|
|
return Importer.importFunctions(M);
|
|
|
|
}
|
|
|
|
};
|
2015-12-24 18:03:35 +08:00
|
|
|
} // anonymous namespace
|
2015-11-24 14:07:49 +08:00
|
|
|
|
|
|
|
char FunctionImportPass::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(FunctionImportPass, "function-import",
|
|
|
|
"Summary Based Function Import", false, false)
|
|
|
|
INITIALIZE_PASS_END(FunctionImportPass, "function-import",
|
|
|
|
"Summary Based Function Import", false, false)
|
|
|
|
|
|
|
|
namespace llvm {
|
2016-03-15 05:18:10 +08:00
|
|
|
Pass *createFunctionImportPass(const FunctionInfoIndex *Index = nullptr) {
|
2015-12-08 03:21:11 +08:00
|
|
|
return new FunctionImportPass(Index);
|
|
|
|
}
|
2015-11-24 14:07:49 +08:00
|
|
|
}
|