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"
|
|
|
|
#include "llvm/Object/FunctionIndexObjectFile.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
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"));
|
|
|
|
|
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");
|
|
|
|
std::unique_ptr<Module> Result = getLazyIRFileModule(FileName, Err, Context);
|
|
|
|
if (!Result) {
|
|
|
|
Err.print("function-import", errs());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result->materializeMetadata();
|
|
|
|
UpgradeDebugInfo(*Result);
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a Module for \p FileName from the cache, or load it lazily.
|
2015-12-02 10:00:29 +08:00
|
|
|
Module &ModuleLazyLoaderCache::operator()(StringRef FileName) {
|
2015-11-24 14:07:49 +08:00
|
|
|
auto &Module = ModuleMap[FileName];
|
|
|
|
if (!Module)
|
|
|
|
Module = loadFile(FileName, Context);
|
|
|
|
return *Module;
|
|
|
|
}
|
|
|
|
|
2015-11-25 05:15:19 +08:00
|
|
|
/// 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.
|
|
|
|
static void findExternalCalls(const Function &F, StringSet<> &CalledFunctions,
|
|
|
|
SmallVector<StringRef, 64> &Worklist) {
|
|
|
|
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.
|
|
|
|
if (CalledFunction && CalledFunction->hasName() &&
|
|
|
|
CalledFunction->isDeclaration() &&
|
|
|
|
!CalledFunctions.count(CalledFunction->getName())) {
|
|
|
|
CalledFunctions.insert(CalledFunction->getName());
|
|
|
|
Worklist.push_back(CalledFunction->getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 10:37:33 +08:00
|
|
|
// Helper function: given a worklist and an index, will process all the worklist
|
|
|
|
// and import them based on the summary information
|
2015-12-03 10:58:14 +08:00
|
|
|
static unsigned ProcessImportWorklist(
|
|
|
|
Module &DestModule, SmallVector<StringRef, 64> &Worklist,
|
|
|
|
StringSet<> &CalledFunctions, Linker &TheLinker,
|
|
|
|
const FunctionInfoIndex &Index,
|
|
|
|
std::function<Module &(StringRef FileName)> &LazyModuleLoader) {
|
2015-12-03 10:37:33 +08:00
|
|
|
unsigned ImportCount = 0;
|
2015-11-24 14:07:49 +08:00
|
|
|
while (!Worklist.empty()) {
|
|
|
|
auto CalledFunctionName = Worklist.pop_back_val();
|
|
|
|
DEBUG(dbgs() << "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");
|
|
|
|
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");
|
|
|
|
|
|
|
|
auto *Summary = Info->functionSummary();
|
|
|
|
if (!Summary) {
|
|
|
|
// FIXME: in case we are lazyloading summaries, we can do it now.
|
2015-12-02 01:12:10 +08:00
|
|
|
DEBUG(dbgs() << "Missing summary for " << CalledFunctionName
|
|
|
|
<< ", error at import?\n");
|
2015-11-24 14:07:49 +08:00
|
|
|
llvm_unreachable("Missing summary");
|
|
|
|
}
|
|
|
|
|
2015-11-25 06:55:46 +08:00
|
|
|
if (Summary->instCount() > ImportInstrLimit) {
|
2015-12-02 01:12:10 +08:00
|
|
|
DEBUG(dbgs() << "Skip import of " << CalledFunctionName << " with "
|
|
|
|
<< Summary->instCount() << " instructions (limit "
|
|
|
|
<< ImportInstrLimit << ")\n");
|
2015-11-25 06:55:46 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-11-24 14:07:49 +08:00
|
|
|
// Get the module path from the summary.
|
|
|
|
auto FileName = Summary->modulePath();
|
|
|
|
DEBUG(dbgs() << "Importing " << CalledFunctionName << " from " << FileName
|
|
|
|
<< "\n");
|
|
|
|
|
|
|
|
// Get the module for the import (potentially from the cache).
|
2015-12-03 10:37:33 +08:00
|
|
|
auto &Module = LazyModuleLoader(FileName);
|
|
|
|
assert(&Module.getContext() == &DestModule.getContext());
|
2015-11-24 14:07:49 +08:00
|
|
|
|
|
|
|
// The function that we will import!
|
|
|
|
GlobalValue *SGV = Module.getNamedValue(CalledFunctionName);
|
2015-11-25 03:55:04 +08:00
|
|
|
StringRef ImportFunctionName = CalledFunctionName;
|
|
|
|
if (!SGV) {
|
2015-12-03 10:37:33 +08:00
|
|
|
// Might be local in source Module, promoted/renamed in DestModule.
|
2015-11-25 03:55:04 +08:00
|
|
|
std::pair<StringRef, StringRef> Split =
|
|
|
|
CalledFunctionName.split(".llvm.");
|
|
|
|
SGV = Module.getNamedValue(Split.first);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Assert that Split.second is module id
|
|
|
|
uint64_t ModuleId;
|
|
|
|
assert(!Split.second.getAsInteger(10, ModuleId));
|
|
|
|
assert(ModuleId == Index.getModuleId(FileName));
|
|
|
|
#endif
|
|
|
|
}
|
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-11-25 03:55:04 +08:00
|
|
|
ImportFunctionName = F->getName();
|
2015-11-24 14:07:49 +08:00
|
|
|
}
|
|
|
|
if (!F) {
|
|
|
|
errs() << "Can't load function '" << CalledFunctionName << "' in Module '"
|
|
|
|
<< FileName << "', error in the summary?\n";
|
|
|
|
llvm_unreachable("Can't load function in Module");
|
|
|
|
}
|
|
|
|
|
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()) {
|
|
|
|
DEBUG(dbgs() << "Ignoring import request for weak-any "
|
|
|
|
<< (isa<Function>(SGV) ? "function " : "alias ")
|
2015-11-24 14:07:49 +08:00
|
|
|
<< CalledFunctionName << " from " << FileName << "\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link in the specified function.
|
2015-12-02 12:34:28 +08:00
|
|
|
DenseSet<const GlobalValue *> FunctionsToImport;
|
|
|
|
FunctionsToImport.insert(F);
|
2015-12-03 10:37:33 +08:00
|
|
|
if (TheLinker.linkInModule(Module, Linker::Flags::None, &Index,
|
2015-12-03 10:58:14 +08:00
|
|
|
&FunctionsToImport))
|
2015-11-24 14:07:49 +08:00
|
|
|
report_fatal_error("Function Import: link error");
|
|
|
|
|
2015-11-25 03:55:04 +08:00
|
|
|
// Process the newly imported function and add callees to the worklist.
|
2015-12-03 10:37:33 +08:00
|
|
|
GlobalValue *NewGV = DestModule.getNamedValue(ImportFunctionName);
|
2015-11-25 03:55:04 +08:00
|
|
|
assert(NewGV);
|
|
|
|
Function *NewF = dyn_cast<Function>(NewGV);
|
|
|
|
assert(NewF);
|
2015-11-25 05:15:19 +08:00
|
|
|
findExternalCalls(*NewF, CalledFunctions, Worklist);
|
2015-12-03 10:37:33 +08:00
|
|
|
++ImportCount;
|
|
|
|
}
|
|
|
|
return ImportCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-03 10:58:14 +08:00
|
|
|
DEBUG(errs() << "Starting import for Module "
|
|
|
|
<< DestModule.getModuleIdentifier() << "\n");
|
2015-12-03 10:37:33 +08:00
|
|
|
unsigned ImportedCount = 0;
|
2015-11-24 14:07:49 +08:00
|
|
|
|
2015-12-03 10:37:33 +08:00
|
|
|
/// First step is collecting the called external functions.
|
|
|
|
StringSet<> CalledFunctions;
|
|
|
|
SmallVector<StringRef, 64> Worklist;
|
|
|
|
for (auto &F : DestModule) {
|
|
|
|
if (F.isDeclaration() || F.hasFnAttribute(Attribute::OptimizeNone))
|
|
|
|
continue;
|
|
|
|
findExternalCalls(F, CalledFunctions, 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
|
|
|
|
Linker TheLinker(DestModule, DiagnosticHandler);
|
|
|
|
|
2015-12-03 10:58:14 +08:00
|
|
|
ImportedCount += ProcessImportWorklist(DestModule, Worklist, CalledFunctions,
|
|
|
|
TheLinker, Index, getLazyModule);
|
2015-12-02 12:34:28 +08:00
|
|
|
|
2015-12-03 10:37:33 +08:00
|
|
|
DEBUG(errs() << "Imported " << ImportedCount << " functions for Module "
|
|
|
|
<< 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';
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse the function index out of an IR file and return the function
|
|
|
|
/// index object if found, or nullptr if not.
|
|
|
|
static std::unique_ptr<FunctionInfoIndex>
|
|
|
|
getFunctionIndexForFile(StringRef Path, std::string &Error,
|
|
|
|
DiagnosticHandlerFunction DiagnosticHandler) {
|
|
|
|
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());
|
|
|
|
ErrorOr<std::unique_ptr<object::FunctionIndexObjectFile>> ObjOrErr =
|
|
|
|
object::FunctionIndexObjectFile::create(Buffer->getMemBufferRef(),
|
|
|
|
DiagnosticHandler);
|
|
|
|
if (std::error_code EC = ObjOrErr.getError()) {
|
|
|
|
Error = EC.message();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return (*ObjOrErr)->takeIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pass that performs cross-module function import provided a summary file.
|
|
|
|
class FunctionImportPass : public ModulePass {
|
2015-12-08 03:21:11 +08:00
|
|
|
/// Optional function summary index to use for importing, otherwise
|
|
|
|
/// the summary-file option must be specified.
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit FunctionImportPass(FunctionInfoIndex *Index = nullptr)
|
|
|
|
: 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");
|
|
|
|
std::unique_ptr<FunctionInfoIndex> IndexPtr;
|
|
|
|
if (!SummaryFile.empty()) {
|
|
|
|
if (Index)
|
|
|
|
report_fatal_error("error: -summary-file and index from frontend\n");
|
|
|
|
std::string Error;
|
|
|
|
IndexPtr = getFunctionIndexForFile(SummaryFile, Error, diagnosticHandler);
|
|
|
|
if (!IndexPtr) {
|
|
|
|
errs() << "Error loading file '" << SummaryFile << "': " << Error
|
|
|
|
<< "\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Index = IndexPtr.get();
|
2015-11-24 14:07:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the import now.
|
2015-12-02 10:00:29 +08:00
|
|
|
ModuleLazyLoaderCache Loader(M.getContext());
|
|
|
|
FunctionImporter Importer(*Index, diagnosticHandler,
|
|
|
|
[&](StringRef Name)
|
|
|
|
-> Module &{ return Loader(Name); });
|
2015-11-24 14:07:49 +08:00
|
|
|
return Importer.importFunctions(M);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
2015-12-08 03:21:11 +08:00
|
|
|
Pass *createFunctionImportPass(FunctionInfoIndex *Index = nullptr) {
|
|
|
|
return new FunctionImportPass(Index);
|
|
|
|
}
|
2015-11-24 14:07:49 +08:00
|
|
|
}
|