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"
|
|
|
|
|
2016-03-26 13:40:34 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-03-27 23:27:30 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2015-11-24 14:07:49 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2016-05-25 22:03:11 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2017-05-02 04:42:32 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
2015-11-24 14:07:49 +08:00
|
|
|
#include "llvm/IR/AutoUpgrade.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2016-12-24 02:04:51 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2015-11-24 14:07:49 +08:00
|
|
|
#include "llvm/IRReader/IRReader.h"
|
|
|
|
#include "llvm/Linker/Linker.h"
|
2016-05-25 22:03:11 +08:00
|
|
|
#include "llvm/Object/IRObjectFile.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-05-25 22:03:11 +08:00
|
|
|
#include "llvm/Transforms/IPO/Internalize.h"
|
2016-02-11 02:11:31 +08:00
|
|
|
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
|
2015-12-09 16:17:35 +08:00
|
|
|
|
2016-03-26 13:40:34 +08:00
|
|
|
#define DEBUG_TYPE "function-import"
|
2015-12-09 16:17:35 +08:00
|
|
|
|
2015-11-24 14:07:49 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-01-06 05:34:18 +08:00
|
|
|
STATISTIC(NumImportedFunctions, "Number of functions imported");
|
|
|
|
STATISTIC(NumImportedModules, "Number of modules imported from");
|
|
|
|
STATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index");
|
|
|
|
STATISTIC(NumLiveSymbols, "Number of live symbols in index");
|
2016-03-27 23:27:30 +08:00
|
|
|
|
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"));
|
2016-09-30 01:32:07 +08:00
|
|
|
|
2016-09-30 11:01:17 +08:00
|
|
|
static cl::opt<float> ImportHotInstrFactor(
|
|
|
|
"import-hot-evolution-factor", cl::init(1.0), cl::Hidden,
|
|
|
|
cl::value_desc("x"),
|
|
|
|
cl::desc("As we import functions called from hot callsite, multiply the "
|
|
|
|
"`import-instr-limit` threshold by this factor "
|
|
|
|
"before processing newly imported functions"));
|
|
|
|
|
[thinlto] Basic thinlto fdo heuristic
Summary:
This patch improves thinlto importer
by importing 3x larger functions that are called from hot block.
I compared performance with the trunk on spec, and there
were about 2% on povray and 3.33% on milc. These results seems
to be consistant and match the results Teresa got with her simple
heuristic. Some benchmarks got slower but I think they are just
noisy (mcf, xalancbmki, omnetpp)- running the benchmarks again with
more iterations to confirm. Geomean of all benchmarks including the noisy ones
were about +0.02%.
I see much better improvement on google branch with Easwaran patch
for pgo callsite inlining (the inliner actually inline those big functions)
Over all I see +0.5% improvement, and I get +8.65% on povray.
So I guess we will see much bigger change when Easwaran patch will land
(it depends on new pass manager), but it is still worth putting this to trunk
before it.
Implementation details changes:
- Removed CallsiteCount.
- ProfileCount got replaced by Hotness
- hot-import-multiplier is set to 3.0 for now,
didn't have time to tune it up, but I see that we get most of the interesting
functions with 3, so there is no much performance difference with higher, and
binary size doesn't grow as much as with 10.0.
Reviewers: eraman, mehdi_amini, tejohnson
Subscribers: mehdi_amini, llvm-commits
Differential Revision: https://reviews.llvm.org/D24638
llvm-svn: 282437
2016-09-27 04:37:32 +08:00
|
|
|
static cl::opt<float> ImportHotMultiplier(
|
|
|
|
"import-hot-multiplier", cl::init(3.0), cl::Hidden, cl::value_desc("x"),
|
2016-09-30 01:32:07 +08:00
|
|
|
cl::desc("Multiply the `import-instr-limit` threshold for hot callsites"));
|
|
|
|
|
2017-07-08 05:01:00 +08:00
|
|
|
static cl::opt<float> ImportCriticalMultiplier(
|
|
|
|
"import-critical-multiplier", cl::init(100.0), cl::Hidden,
|
|
|
|
cl::value_desc("x"),
|
|
|
|
cl::desc(
|
|
|
|
"Multiply the `import-instr-limit` threshold for critical callsites"));
|
|
|
|
|
2016-09-30 01:32:07 +08:00
|
|
|
// FIXME: This multiplier was not really tuned up.
|
|
|
|
static cl::opt<float> ImportColdMultiplier(
|
|
|
|
"import-cold-multiplier", cl::init(0), cl::Hidden, cl::value_desc("N"),
|
|
|
|
cl::desc("Multiply the `import-instr-limit` threshold for cold callsites"));
|
2016-02-11 07:31:45 +08:00
|
|
|
|
2016-03-27 23:27:30 +08:00
|
|
|
static cl::opt<bool> PrintImports("print-imports", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Print imported functions"));
|
|
|
|
|
2017-01-06 05:34:18 +08:00
|
|
|
static cl::opt<bool> ComputeDead("compute-dead", cl::init(true), cl::Hidden,
|
|
|
|
cl::desc("Compute dead symbols"));
|
|
|
|
|
2016-07-09 07:01:49 +08:00
|
|
|
static cl::opt<bool> EnableImportMetadata(
|
|
|
|
"enable-import-metadata", cl::init(
|
|
|
|
#if !defined(NDEBUG)
|
|
|
|
true /*Enabled with asserts.*/
|
|
|
|
#else
|
|
|
|
false
|
|
|
|
#endif
|
|
|
|
),
|
|
|
|
cl::Hidden, cl::desc("Enable import metadata like 'thinlto_src_module'"));
|
|
|
|
|
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());
|
2016-04-01 13:33:11 +08:00
|
|
|
report_fatal_error("Abort");
|
2015-11-24 14:07:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2015-12-09 16:17:35 +08:00
|
|
|
namespace {
|
2016-02-11 07:31:45 +08:00
|
|
|
|
2016-03-26 13:40:34 +08:00
|
|
|
/// Given a list of possible callee implementation for a call site, select one
|
|
|
|
/// that fits the \p Threshold.
|
|
|
|
///
|
|
|
|
/// FIXME: select "best" instead of first that fits. But what is "best"?
|
|
|
|
/// - The smallest: more likely to be inlined.
|
|
|
|
/// - The one with the least outgoing edges (already well optimized).
|
|
|
|
/// - One from a module already being imported from in order to reduce the
|
|
|
|
/// number of source modules parsed/linked.
|
|
|
|
/// - One that has PGO data attached.
|
|
|
|
/// - [insert you fancy metric here]
|
2016-04-16 14:56:44 +08:00
|
|
|
static const GlobalValueSummary *
|
2016-04-27 08:32:13 +08:00
|
|
|
selectCallee(const ModuleSummaryIndex &Index,
|
2017-05-05 02:03:25 +08:00
|
|
|
ArrayRef<std::unique_ptr<GlobalValueSummary>> CalleeSummaryList,
|
2017-01-13 06:04:45 +08:00
|
|
|
unsigned Threshold, StringRef CallerModulePath) {
|
2017-07-16 06:58:06 +08:00
|
|
|
// Find the first eligible callee (e.g. legality checks).
|
2016-03-26 13:40:34 +08:00
|
|
|
auto It = llvm::find_if(
|
2016-04-24 22:57:11 +08:00
|
|
|
CalleeSummaryList,
|
|
|
|
[&](const std::unique_ptr<GlobalValueSummary> &SummaryPtr) {
|
|
|
|
auto *GVSummary = SummaryPtr.get();
|
2016-05-11 09:26:06 +08:00
|
|
|
if (GlobalValue::isInterposableLinkage(GVSummary->linkage()))
|
2016-05-03 08:27:28 +08:00
|
|
|
// There is no point in importing these, we can't inline them
|
2016-04-20 12:17:36 +08:00
|
|
|
return false;
|
|
|
|
if (auto *AS = dyn_cast<AliasSummary>(GVSummary)) {
|
2016-04-16 14:56:44 +08:00
|
|
|
GVSummary = &AS->getAliasee();
|
2016-04-20 12:17:36 +08:00
|
|
|
// Alias can't point to "available_externally". However when we import
|
|
|
|
// linkOnceODR the linkage does not change. So we import the alias
|
|
|
|
// and aliasee only in this case.
|
|
|
|
// FIXME: we should import alias as available_externally *function*,
|
|
|
|
// the destination module does need to know it is an alias.
|
|
|
|
if (!GlobalValue::isLinkOnceODRLinkage(GVSummary->linkage()))
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-26 13:40:34 +08:00
|
|
|
|
2016-04-20 12:17:36 +08:00
|
|
|
auto *Summary = cast<FunctionSummary>(GVSummary);
|
2016-03-26 13:40:34 +08:00
|
|
|
|
2017-01-13 06:04:45 +08:00
|
|
|
// If this is a local function, make sure we import the copy
|
|
|
|
// in the caller's module. The only time a local function can
|
|
|
|
// share an entry in the index is if there is a local with the same name
|
|
|
|
// in another module that had the same source file name (in a different
|
|
|
|
// directory), where each was compiled in their own directory so there
|
|
|
|
// was not distinguishing path.
|
|
|
|
// However, do the import from another module if there is only one
|
|
|
|
// entry in the list - in that case this must be a reference due
|
|
|
|
// to indirect call profile data, since a function pointer can point to
|
|
|
|
// a local in another module.
|
|
|
|
if (GlobalValue::isLocalLinkage(Summary->linkage()) &&
|
|
|
|
CalleeSummaryList.size() > 1 &&
|
|
|
|
Summary->modulePath() != CallerModulePath)
|
|
|
|
return false;
|
|
|
|
|
2017-01-05 22:32:16 +08:00
|
|
|
if (Summary->notEligibleToImport())
|
2016-04-27 08:32:13 +08:00
|
|
|
return false;
|
|
|
|
|
2016-03-26 13:40:34 +08:00
|
|
|
return true;
|
|
|
|
});
|
2016-04-24 22:57:11 +08:00
|
|
|
if (It == CalleeSummaryList.end())
|
2016-03-26 13:40:34 +08:00
|
|
|
return nullptr;
|
2015-12-09 16:17:35 +08:00
|
|
|
|
2017-07-16 06:58:06 +08:00
|
|
|
// Now check if the first eligible callee is under the instruction
|
|
|
|
// threshold. Checking this on the first eligible callee ensures that
|
|
|
|
// we don't end up selecting different callees to import when we invoke
|
|
|
|
// this routine with different thresholds (when there are multiple copies,
|
|
|
|
// i.e. with weak or linkonce linkage).
|
|
|
|
auto *Summary = dyn_cast<FunctionSummary>(It->get());
|
|
|
|
if (auto *AS = dyn_cast<AliasSummary>(It->get()))
|
|
|
|
Summary = cast<FunctionSummary>(&AS->getAliasee());
|
|
|
|
assert(Summary && "Expected FunctionSummary, or alias to one");
|
|
|
|
if (Summary->instCount() > Threshold)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return Summary;
|
2016-03-26 13:40:34 +08:00
|
|
|
}
|
2015-12-09 16:17:35 +08:00
|
|
|
|
2016-12-16 04:48:19 +08:00
|
|
|
using EdgeInfo = std::tuple<const FunctionSummary *, unsigned /* Threshold */,
|
|
|
|
GlobalValue::GUID>;
|
2016-03-26 13:40:34 +08:00
|
|
|
|
|
|
|
/// Compute the list of functions to import for a given caller. Mark these
|
|
|
|
/// imported functions and the symbols they reference in their source module as
|
|
|
|
/// exported from their source module.
|
|
|
|
static void computeImportForFunction(
|
2016-04-10 23:17:26 +08:00
|
|
|
const FunctionSummary &Summary, const ModuleSummaryIndex &Index,
|
[thinlto] Basic thinlto fdo heuristic
Summary:
This patch improves thinlto importer
by importing 3x larger functions that are called from hot block.
I compared performance with the trunk on spec, and there
were about 2% on povray and 3.33% on milc. These results seems
to be consistant and match the results Teresa got with her simple
heuristic. Some benchmarks got slower but I think they are just
noisy (mcf, xalancbmki, omnetpp)- running the benchmarks again with
more iterations to confirm. Geomean of all benchmarks including the noisy ones
were about +0.02%.
I see much better improvement on google branch with Easwaran patch
for pgo callsite inlining (the inliner actually inline those big functions)
Over all I see +0.5% improvement, and I get +8.65% on povray.
So I guess we will see much bigger change when Easwaran patch will land
(it depends on new pass manager), but it is still worth putting this to trunk
before it.
Implementation details changes:
- Removed CallsiteCount.
- ProfileCount got replaced by Hotness
- hot-import-multiplier is set to 3.0 for now,
didn't have time to tune it up, but I see that we get most of the interesting
functions with 3, so there is no much performance difference with higher, and
binary size doesn't grow as much as with 10.0.
Reviewers: eraman, mehdi_amini, tejohnson
Subscribers: mehdi_amini, llvm-commits
Differential Revision: https://reviews.llvm.org/D24638
llvm-svn: 282437
2016-09-27 04:37:32 +08:00
|
|
|
const unsigned Threshold, const GVSummaryMapTy &DefinedGVSummaries,
|
2016-03-26 13:40:34 +08:00
|
|
|
SmallVectorImpl<EdgeInfo> &Worklist,
|
2016-08-16 13:47:12 +08:00
|
|
|
FunctionImporter::ImportMapTy &ImportList,
|
2016-04-13 05:13:11 +08:00
|
|
|
StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
|
2016-03-26 13:40:34 +08:00
|
|
|
for (auto &Edge : Summary.calls()) {
|
2017-05-05 02:03:25 +08:00
|
|
|
ValueInfo VI = Edge.first;
|
|
|
|
DEBUG(dbgs() << " edge -> " << VI.getGUID() << " Threshold:" << Threshold
|
|
|
|
<< "\n");
|
2016-03-26 13:40:34 +08:00
|
|
|
|
2017-05-05 02:03:25 +08:00
|
|
|
if (VI.getSummaryList().empty()) {
|
SamplePGO ThinLTO ICP fix for local functions.
Summary:
In SamplePGO, if the profile is collected from non-LTO binary, and used to drive ThinLTO, the indirect call promotion may fail because ThinLTO adjusts local function names to avoid conflicts. There are two places of where the mismatch can happen:
1. thin-link prepends SourceFileName to front of FuncName to build the GUID (GlobalValue::getGlobalIdentifier). Unlike instrumentation FDO, SamplePGO does not use the PGOFuncName scheme and therefore the indirect call target profile data contains a hash of the OriginalName.
2. backend compiler promotes some local functions to global and appends .llvm.{$ModuleHash} to the end of the FuncName to derive PromotedFunctionName
This patch tries at the best effort to find the GUID from the original local function name (in profile), and use that in ICP promotion, and in SamplePGO matching that happens in the backend after importing/inlining:
1. in thin-link, it builds the map from OriginalName to GUID so that when thin-link reads in indirect call target profile (represented by OriginalName), it knows which GUID to import.
2. in backend compiler, if sample profile reader cannot find a profile match for PromotedFunctionName, it will try to find if there is a match for OriginalFunctionName.
3. in backend compiler, we build symbol table entry for OriginalFunctionName and pointer to the same symbol of PromotedFunctionName, so that ICP can find the correct target to promote.
Reviewers: mehdi_amini, tejohnson
Reviewed By: tejohnson
Subscribers: llvm-commits, Prazek
Differential Revision: https://reviews.llvm.org/D30754
llvm-svn: 297757
2017-03-15 01:33:01 +08:00
|
|
|
// For SamplePGO, the indirect call targets for local functions will
|
|
|
|
// have its original name annotated in profile. We try to find the
|
|
|
|
// corresponding PGOFuncName as the GUID.
|
2017-05-05 02:03:25 +08:00
|
|
|
auto GUID = Index.getGUIDFromOriginalID(VI.getGUID());
|
SamplePGO ThinLTO ICP fix for local functions.
Summary:
In SamplePGO, if the profile is collected from non-LTO binary, and used to drive ThinLTO, the indirect call promotion may fail because ThinLTO adjusts local function names to avoid conflicts. There are two places of where the mismatch can happen:
1. thin-link prepends SourceFileName to front of FuncName to build the GUID (GlobalValue::getGlobalIdentifier). Unlike instrumentation FDO, SamplePGO does not use the PGOFuncName scheme and therefore the indirect call target profile data contains a hash of the OriginalName.
2. backend compiler promotes some local functions to global and appends .llvm.{$ModuleHash} to the end of the FuncName to derive PromotedFunctionName
This patch tries at the best effort to find the GUID from the original local function name (in profile), and use that in ICP promotion, and in SamplePGO matching that happens in the backend after importing/inlining:
1. in thin-link, it builds the map from OriginalName to GUID so that when thin-link reads in indirect call target profile (represented by OriginalName), it knows which GUID to import.
2. in backend compiler, if sample profile reader cannot find a profile match for PromotedFunctionName, it will try to find if there is a match for OriginalFunctionName.
3. in backend compiler, we build symbol table entry for OriginalFunctionName and pointer to the same symbol of PromotedFunctionName, so that ICP can find the correct target to promote.
Reviewers: mehdi_amini, tejohnson
Reviewed By: tejohnson
Subscribers: llvm-commits, Prazek
Differential Revision: https://reviews.llvm.org/D30754
llvm-svn: 297757
2017-03-15 01:33:01 +08:00
|
|
|
if (GUID == 0)
|
|
|
|
continue;
|
2017-05-05 02:03:25 +08:00
|
|
|
VI = Index.getValueInfo(GUID);
|
|
|
|
if (!VI)
|
|
|
|
continue;
|
SamplePGO ThinLTO ICP fix for local functions.
Summary:
In SamplePGO, if the profile is collected from non-LTO binary, and used to drive ThinLTO, the indirect call promotion may fail because ThinLTO adjusts local function names to avoid conflicts. There are two places of where the mismatch can happen:
1. thin-link prepends SourceFileName to front of FuncName to build the GUID (GlobalValue::getGlobalIdentifier). Unlike instrumentation FDO, SamplePGO does not use the PGOFuncName scheme and therefore the indirect call target profile data contains a hash of the OriginalName.
2. backend compiler promotes some local functions to global and appends .llvm.{$ModuleHash} to the end of the FuncName to derive PromotedFunctionName
This patch tries at the best effort to find the GUID from the original local function name (in profile), and use that in ICP promotion, and in SamplePGO matching that happens in the backend after importing/inlining:
1. in thin-link, it builds the map from OriginalName to GUID so that when thin-link reads in indirect call target profile (represented by OriginalName), it knows which GUID to import.
2. in backend compiler, if sample profile reader cannot find a profile match for PromotedFunctionName, it will try to find if there is a match for OriginalFunctionName.
3. in backend compiler, we build symbol table entry for OriginalFunctionName and pointer to the same symbol of PromotedFunctionName, so that ICP can find the correct target to promote.
Reviewers: mehdi_amini, tejohnson
Reviewed By: tejohnson
Subscribers: llvm-commits, Prazek
Differential Revision: https://reviews.llvm.org/D30754
llvm-svn: 297757
2017-03-15 01:33:01 +08:00
|
|
|
}
|
|
|
|
|
2017-05-05 02:03:25 +08:00
|
|
|
if (DefinedGVSummaries.count(VI.getGUID())) {
|
2016-03-26 13:40:34 +08:00
|
|
|
DEBUG(dbgs() << "ignored! Target already in destination module.\n");
|
2015-11-24 14:07:49 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-30 01:32:07 +08:00
|
|
|
auto GetBonusMultiplier = [](CalleeInfo::HotnessType Hotness) -> float {
|
|
|
|
if (Hotness == CalleeInfo::HotnessType::Hot)
|
|
|
|
return ImportHotMultiplier;
|
|
|
|
if (Hotness == CalleeInfo::HotnessType::Cold)
|
|
|
|
return ImportColdMultiplier;
|
2017-07-08 05:01:00 +08:00
|
|
|
if (Hotness == CalleeInfo::HotnessType::Critical)
|
|
|
|
return ImportCriticalMultiplier;
|
2016-09-30 01:32:07 +08:00
|
|
|
return 1.0;
|
|
|
|
};
|
|
|
|
|
[thinlto] Basic thinlto fdo heuristic
Summary:
This patch improves thinlto importer
by importing 3x larger functions that are called from hot block.
I compared performance with the trunk on spec, and there
were about 2% on povray and 3.33% on milc. These results seems
to be consistant and match the results Teresa got with her simple
heuristic. Some benchmarks got slower but I think they are just
noisy (mcf, xalancbmki, omnetpp)- running the benchmarks again with
more iterations to confirm. Geomean of all benchmarks including the noisy ones
were about +0.02%.
I see much better improvement on google branch with Easwaran patch
for pgo callsite inlining (the inliner actually inline those big functions)
Over all I see +0.5% improvement, and I get +8.65% on povray.
So I guess we will see much bigger change when Easwaran patch will land
(it depends on new pass manager), but it is still worth putting this to trunk
before it.
Implementation details changes:
- Removed CallsiteCount.
- ProfileCount got replaced by Hotness
- hot-import-multiplier is set to 3.0 for now,
didn't have time to tune it up, but I see that we get most of the interesting
functions with 3, so there is no much performance difference with higher, and
binary size doesn't grow as much as with 10.0.
Reviewers: eraman, mehdi_amini, tejohnson
Subscribers: mehdi_amini, llvm-commits
Differential Revision: https://reviews.llvm.org/D24638
llvm-svn: 282437
2016-09-27 04:37:32 +08:00
|
|
|
const auto NewThreshold =
|
2016-09-30 01:32:07 +08:00
|
|
|
Threshold * GetBonusMultiplier(Edge.second.Hotness);
|
2016-09-30 11:01:17 +08:00
|
|
|
|
2017-05-05 02:03:25 +08:00
|
|
|
auto *CalleeSummary = selectCallee(Index, VI.getSummaryList(), NewThreshold,
|
|
|
|
Summary.modulePath());
|
2016-03-26 13:40:34 +08:00
|
|
|
if (!CalleeSummary) {
|
|
|
|
DEBUG(dbgs() << "ignored! No qualifying callee with summary found.\n");
|
2015-11-25 06:55:46 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-04-16 14:56:44 +08:00
|
|
|
// "Resolve" the summary, traversing alias,
|
|
|
|
const FunctionSummary *ResolvedCalleeSummary;
|
2016-04-20 09:04:20 +08:00
|
|
|
if (isa<AliasSummary>(CalleeSummary)) {
|
2016-04-16 14:56:44 +08:00
|
|
|
ResolvedCalleeSummary = cast<FunctionSummary>(
|
|
|
|
&cast<AliasSummary>(CalleeSummary)->getAliasee());
|
2016-04-20 12:17:36 +08:00
|
|
|
assert(
|
|
|
|
GlobalValue::isLinkOnceODRLinkage(ResolvedCalleeSummary->linkage()) &&
|
|
|
|
"Unexpected alias to a non-linkonceODR in import list");
|
2016-04-20 09:04:20 +08:00
|
|
|
} else
|
2016-04-16 14:56:44 +08:00
|
|
|
ResolvedCalleeSummary = cast<FunctionSummary>(CalleeSummary);
|
|
|
|
|
[thinlto] Basic thinlto fdo heuristic
Summary:
This patch improves thinlto importer
by importing 3x larger functions that are called from hot block.
I compared performance with the trunk on spec, and there
were about 2% on povray and 3.33% on milc. These results seems
to be consistant and match the results Teresa got with her simple
heuristic. Some benchmarks got slower but I think they are just
noisy (mcf, xalancbmki, omnetpp)- running the benchmarks again with
more iterations to confirm. Geomean of all benchmarks including the noisy ones
were about +0.02%.
I see much better improvement on google branch with Easwaran patch
for pgo callsite inlining (the inliner actually inline those big functions)
Over all I see +0.5% improvement, and I get +8.65% on povray.
So I guess we will see much bigger change when Easwaran patch will land
(it depends on new pass manager), but it is still worth putting this to trunk
before it.
Implementation details changes:
- Removed CallsiteCount.
- ProfileCount got replaced by Hotness
- hot-import-multiplier is set to 3.0 for now,
didn't have time to tune it up, but I see that we get most of the interesting
functions with 3, so there is no much performance difference with higher, and
binary size doesn't grow as much as with 10.0.
Reviewers: eraman, mehdi_amini, tejohnson
Subscribers: mehdi_amini, llvm-commits
Differential Revision: https://reviews.llvm.org/D24638
llvm-svn: 282437
2016-09-27 04:37:32 +08:00
|
|
|
assert(ResolvedCalleeSummary->instCount() <= NewThreshold &&
|
2016-03-26 13:40:34 +08:00
|
|
|
"selectCallee() didn't honor the threshold");
|
|
|
|
|
2016-12-16 02:21:01 +08:00
|
|
|
auto GetAdjustedThreshold = [](unsigned Threshold, bool IsHotCallsite) {
|
|
|
|
// Adjust the threshold for next level of imported functions.
|
|
|
|
// The threshold is different for hot callsites because we can then
|
|
|
|
// inline chains of hot calls.
|
|
|
|
if (IsHotCallsite)
|
|
|
|
return Threshold * ImportHotInstrFactor;
|
|
|
|
return Threshold * ImportInstrFactor;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool IsHotCallsite = Edge.second.Hotness == CalleeInfo::HotnessType::Hot;
|
|
|
|
const auto AdjThreshold = GetAdjustedThreshold(Threshold, IsHotCallsite);
|
|
|
|
|
2016-04-16 14:56:44 +08:00
|
|
|
auto ExportModulePath = ResolvedCalleeSummary->modulePath();
|
2017-05-05 02:03:25 +08:00
|
|
|
auto &ProcessedThreshold = ImportList[ExportModulePath][VI.getGUID()];
|
2016-03-26 13:40:34 +08:00
|
|
|
/// Since the traversal of the call graph is DFS, we can revisit a function
|
|
|
|
/// a second time with a higher threshold. In this case, it is added back to
|
|
|
|
/// the worklist with the new threshold.
|
2016-12-16 02:21:01 +08:00
|
|
|
if (ProcessedThreshold && ProcessedThreshold >= AdjThreshold) {
|
2016-03-26 13:40:34 +08:00
|
|
|
DEBUG(dbgs() << "ignored! Target was already seen with Threshold "
|
|
|
|
<< ProcessedThreshold << "\n");
|
|
|
|
continue;
|
|
|
|
}
|
2016-12-16 07:50:06 +08:00
|
|
|
bool PreviouslyImported = ProcessedThreshold != 0;
|
2016-03-26 13:40:34 +08:00
|
|
|
// Mark this function as imported in this module, with the current Threshold
|
2016-12-16 02:21:01 +08:00
|
|
|
ProcessedThreshold = AdjThreshold;
|
2016-03-26 13:40:34 +08:00
|
|
|
|
|
|
|
// Make exports in the source module.
|
2016-04-13 05:13:11 +08:00
|
|
|
if (ExportLists) {
|
2016-04-13 09:52:32 +08:00
|
|
|
auto &ExportList = (*ExportLists)[ExportModulePath];
|
2017-05-05 02:03:25 +08:00
|
|
|
ExportList.insert(VI.getGUID());
|
2016-12-16 07:50:06 +08:00
|
|
|
if (!PreviouslyImported) {
|
|
|
|
// This is the first time this function was exported from its source
|
|
|
|
// module, so mark all functions and globals it references as exported
|
|
|
|
// to the outside if they are defined in the same source module.
|
2016-12-16 12:11:51 +08:00
|
|
|
// For efficiency, we unconditionally add all the referenced GUIDs
|
|
|
|
// to the ExportList for this module, and will prune out any not
|
|
|
|
// defined in the module later in a single pass.
|
2016-12-16 07:50:06 +08:00
|
|
|
for (auto &Edge : ResolvedCalleeSummary->calls()) {
|
|
|
|
auto CalleeGUID = Edge.first.getGUID();
|
2016-12-16 12:11:51 +08:00
|
|
|
ExportList.insert(CalleeGUID);
|
2016-12-16 07:50:06 +08:00
|
|
|
}
|
|
|
|
for (auto &Ref : ResolvedCalleeSummary->refs()) {
|
|
|
|
auto GUID = Ref.getGUID();
|
2016-12-16 12:11:51 +08:00
|
|
|
ExportList.insert(GUID);
|
2016-12-16 07:50:06 +08:00
|
|
|
}
|
2016-04-13 05:13:11 +08:00
|
|
|
}
|
2016-03-26 13:40:34 +08:00
|
|
|
}
|
2015-11-25 06:55:46 +08:00
|
|
|
|
2016-03-26 13:40:34 +08:00
|
|
|
// Insert the newly imported function to the worklist.
|
2017-05-05 02:03:25 +08:00
|
|
|
Worklist.emplace_back(ResolvedCalleeSummary, AdjThreshold, VI.getGUID());
|
2016-03-26 13:40:34 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-11 07:31:45 +08:00
|
|
|
|
2016-03-26 13:40:34 +08:00
|
|
|
/// Given the list of globals defined in a module, compute the list of imports
|
|
|
|
/// as well as the list of "exports", i.e. the list of symbols referenced from
|
|
|
|
/// another module (that may require promotion).
|
|
|
|
static void ComputeImportForModule(
|
2016-04-26 05:09:51 +08:00
|
|
|
const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index,
|
2016-08-16 13:47:12 +08:00
|
|
|
FunctionImporter::ImportMapTy &ImportList,
|
2017-06-02 04:30:06 +08:00
|
|
|
StringMap<FunctionImporter::ExportSetTy> *ExportLists = nullptr) {
|
2016-03-26 13:40:34 +08:00
|
|
|
// Worklist contains the list of function imported in this module, for which
|
|
|
|
// we will analyse the callees and may import further down the callgraph.
|
|
|
|
SmallVector<EdgeInfo, 128> Worklist;
|
|
|
|
|
|
|
|
// Populate the worklist with the import for the functions in the current
|
|
|
|
// module
|
2016-04-24 22:57:11 +08:00
|
|
|
for (auto &GVSummary : DefinedGVSummaries) {
|
2017-06-02 04:30:06 +08:00
|
|
|
if (!Index.isGlobalValueLive(GVSummary.second)) {
|
2017-01-06 05:34:18 +08:00
|
|
|
DEBUG(dbgs() << "Ignores Dead GUID: " << GVSummary.first << "\n");
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-24 22:57:11 +08:00
|
|
|
auto *Summary = GVSummary.second;
|
2016-04-16 14:56:44 +08:00
|
|
|
if (auto *AS = dyn_cast<AliasSummary>(Summary))
|
|
|
|
Summary = &AS->getAliasee();
|
2016-04-16 15:02:16 +08:00
|
|
|
auto *FuncSummary = dyn_cast<FunctionSummary>(Summary);
|
|
|
|
if (!FuncSummary)
|
|
|
|
// Skip import for global variables
|
|
|
|
continue;
|
2016-04-24 22:57:11 +08:00
|
|
|
DEBUG(dbgs() << "Initalize import for " << GVSummary.first << "\n");
|
2016-04-16 14:56:44 +08:00
|
|
|
computeImportForFunction(*FuncSummary, Index, ImportInstrLimit,
|
2016-08-16 13:47:12 +08:00
|
|
|
DefinedGVSummaries, Worklist, ImportList,
|
2016-03-26 13:40:34 +08:00
|
|
|
ExportLists);
|
|
|
|
}
|
2015-11-24 14:07:49 +08:00
|
|
|
|
2016-09-30 11:01:17 +08:00
|
|
|
// Process the newly imported functions and add callees to the worklist.
|
2016-03-26 13:40:34 +08:00
|
|
|
while (!Worklist.empty()) {
|
|
|
|
auto FuncInfo = Worklist.pop_back_val();
|
2016-12-16 04:48:19 +08:00
|
|
|
auto *Summary = std::get<0>(FuncInfo);
|
|
|
|
auto Threshold = std::get<1>(FuncInfo);
|
|
|
|
auto GUID = std::get<2>(FuncInfo);
|
|
|
|
|
|
|
|
// Check if we later added this summary with a higher threshold.
|
|
|
|
// If so, skip this entry.
|
|
|
|
auto ExportModulePath = Summary->modulePath();
|
|
|
|
auto &LatestProcessedThreshold = ImportList[ExportModulePath][GUID];
|
|
|
|
if (LatestProcessedThreshold > Threshold)
|
|
|
|
continue;
|
2015-11-24 14:07:49 +08:00
|
|
|
|
2016-04-16 15:02:16 +08:00
|
|
|
computeImportForFunction(*Summary, Index, Threshold, DefinedGVSummaries,
|
2016-08-16 13:47:12 +08:00
|
|
|
Worklist, ImportList, ExportLists);
|
2016-03-26 13:40:34 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-09 16:17:35 +08:00
|
|
|
|
2016-03-26 13:40:34 +08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-04-13 05:13:11 +08:00
|
|
|
/// Compute all the import and export for every module using the Index.
|
2016-03-26 13:40:34 +08:00
|
|
|
void llvm::ComputeCrossModuleImport(
|
|
|
|
const ModuleSummaryIndex &Index,
|
2016-04-26 05:09:51 +08:00
|
|
|
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
|
2016-03-26 13:40:34 +08:00
|
|
|
StringMap<FunctionImporter::ImportMapTy> &ImportLists,
|
2017-06-02 04:30:06 +08:00
|
|
|
StringMap<FunctionImporter::ExportSetTy> &ExportLists) {
|
2016-03-26 13:40:34 +08:00
|
|
|
// For each module that has function defined, compute the import/export lists.
|
2016-04-16 15:02:16 +08:00
|
|
|
for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
|
2016-08-16 13:47:12 +08:00
|
|
|
auto &ImportList = ImportLists[DefinedGVSummaries.first()];
|
2016-04-16 15:02:16 +08:00
|
|
|
DEBUG(dbgs() << "Computing import for Module '"
|
|
|
|
<< DefinedGVSummaries.first() << "'\n");
|
2016-08-16 13:47:12 +08:00
|
|
|
ComputeImportForModule(DefinedGVSummaries.second, Index, ImportList,
|
2017-06-02 04:30:06 +08:00
|
|
|
&ExportLists);
|
2016-03-26 13:40:34 +08:00
|
|
|
}
|
2015-11-24 14:07:49 +08:00
|
|
|
|
2016-12-16 12:11:51 +08:00
|
|
|
// When computing imports we added all GUIDs referenced by anything
|
|
|
|
// imported from the module to its ExportList. Now we prune each ExportList
|
|
|
|
// of any not defined in that module. This is more efficient than checking
|
|
|
|
// while computing imports because some of the summary lists may be long
|
|
|
|
// due to linkonce (comdat) copies.
|
|
|
|
for (auto &ELI : ExportLists) {
|
|
|
|
const auto &DefinedGVSummaries =
|
|
|
|
ModuleToDefinedGVSummaries.lookup(ELI.first());
|
|
|
|
for (auto EI = ELI.second.begin(); EI != ELI.second.end();) {
|
|
|
|
if (!DefinedGVSummaries.count(*EI))
|
|
|
|
EI = ELI.second.erase(EI);
|
|
|
|
else
|
|
|
|
++EI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 13:40:34 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
DEBUG(dbgs() << "Import/Export lists for " << ImportLists.size()
|
|
|
|
<< " modules:\n");
|
|
|
|
for (auto &ModuleImports : ImportLists) {
|
|
|
|
auto ModName = ModuleImports.first();
|
|
|
|
auto &Exports = ExportLists[ModName];
|
|
|
|
DEBUG(dbgs() << "* Module " << ModName << " exports " << Exports.size()
|
|
|
|
<< " functions. Imports from " << ModuleImports.second.size()
|
|
|
|
<< " modules.\n");
|
|
|
|
for (auto &Src : ModuleImports.second) {
|
|
|
|
auto SrcModName = Src.first();
|
|
|
|
DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
|
|
|
|
<< SrcModName << "\n");
|
|
|
|
}
|
2015-12-03 10:37:33 +08:00
|
|
|
}
|
2016-03-26 13:40:34 +08:00
|
|
|
#endif
|
2015-12-03 10:37:33 +08:00
|
|
|
}
|
|
|
|
|
2016-04-13 05:13:11 +08:00
|
|
|
/// Compute all the imports for the given module in the Index.
|
|
|
|
void llvm::ComputeCrossModuleImportForModule(
|
|
|
|
StringRef ModulePath, const ModuleSummaryIndex &Index,
|
|
|
|
FunctionImporter::ImportMapTy &ImportList) {
|
|
|
|
|
|
|
|
// Collect the list of functions this module defines.
|
|
|
|
// GUID -> Summary
|
2016-04-26 05:09:51 +08:00
|
|
|
GVSummaryMapTy FunctionSummaryMap;
|
2016-04-24 22:57:11 +08:00
|
|
|
Index.collectDefinedFunctionsForModule(ModulePath, FunctionSummaryMap);
|
2016-04-13 05:13:11 +08:00
|
|
|
|
|
|
|
// Compute the import list for this module.
|
|
|
|
DEBUG(dbgs() << "Computing import for Module '" << ModulePath << "'\n");
|
2016-04-24 22:57:11 +08:00
|
|
|
ComputeImportForModule(FunctionSummaryMap, Index, ImportList);
|
2016-04-13 05:13:11 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
|
|
|
|
<< ImportList.size() << " modules.\n");
|
|
|
|
for (auto &Src : ImportList) {
|
|
|
|
auto SrcModName = Src.first();
|
|
|
|
DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
|
|
|
|
<< SrcModName << "\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-06-02 04:30:06 +08:00
|
|
|
void llvm::computeDeadSymbols(
|
|
|
|
ModuleSummaryIndex &Index,
|
2017-01-06 05:34:18 +08:00
|
|
|
const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
|
2017-06-02 04:30:06 +08:00
|
|
|
assert(!Index.withGlobalValueDeadStripping());
|
2017-01-06 05:34:18 +08:00
|
|
|
if (!ComputeDead)
|
2017-06-02 04:30:06 +08:00
|
|
|
return;
|
2017-01-06 05:34:18 +08:00
|
|
|
if (GUIDPreservedSymbols.empty())
|
|
|
|
// Don't do anything when nothing is live, this is friendly with tests.
|
2017-06-02 04:30:06 +08:00
|
|
|
return;
|
|
|
|
unsigned LiveSymbols = 0;
|
2017-05-05 02:03:25 +08:00
|
|
|
SmallVector<ValueInfo, 128> Worklist;
|
|
|
|
Worklist.reserve(GUIDPreservedSymbols.size() * 2);
|
|
|
|
for (auto GUID : GUIDPreservedSymbols) {
|
|
|
|
ValueInfo VI = Index.getValueInfo(GUID);
|
|
|
|
if (!VI)
|
|
|
|
continue;
|
2017-06-02 04:30:06 +08:00
|
|
|
for (auto &S : VI.getSummaryList())
|
|
|
|
S->setLive(true);
|
2017-01-06 05:34:18 +08:00
|
|
|
}
|
2017-06-02 04:30:06 +08:00
|
|
|
|
2017-01-06 05:34:18 +08:00
|
|
|
// Add values flagged in the index as live roots to the worklist.
|
2017-06-02 04:30:06 +08:00
|
|
|
for (const auto &Entry : Index)
|
|
|
|
for (auto &S : Entry.second.SummaryList)
|
|
|
|
if (S->isLive()) {
|
|
|
|
DEBUG(dbgs() << "Live root: " << Entry.first << "\n");
|
|
|
|
Worklist.push_back(ValueInfo(&Entry));
|
|
|
|
++LiveSymbols;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make value live and add it to the worklist if it was not live before.
|
|
|
|
// FIXME: we should only make the prevailing copy live here
|
|
|
|
auto visit = [&](ValueInfo VI) {
|
|
|
|
for (auto &S : VI.getSummaryList())
|
|
|
|
if (S->isLive())
|
|
|
|
return;
|
|
|
|
for (auto &S : VI.getSummaryList())
|
|
|
|
S->setLive(true);
|
|
|
|
++LiveSymbols;
|
|
|
|
Worklist.push_back(VI);
|
|
|
|
};
|
2017-01-06 05:34:18 +08:00
|
|
|
|
|
|
|
while (!Worklist.empty()) {
|
2017-05-05 02:03:25 +08:00
|
|
|
auto VI = Worklist.pop_back_val();
|
|
|
|
for (auto &Summary : VI.getSummaryList()) {
|
2017-06-02 04:30:06 +08:00
|
|
|
for (auto Ref : Summary->refs())
|
|
|
|
visit(Ref);
|
|
|
|
if (auto *FS = dyn_cast<FunctionSummary>(Summary.get()))
|
|
|
|
for (auto Call : FS->calls())
|
|
|
|
visit(Call.first);
|
2017-01-06 05:34:18 +08:00
|
|
|
if (auto *AS = dyn_cast<AliasSummary>(Summary.get())) {
|
|
|
|
auto AliaseeGUID = AS->getAliasee().getOriginalName();
|
2017-05-05 02:03:25 +08:00
|
|
|
ValueInfo AliaseeVI = Index.getValueInfo(AliaseeGUID);
|
2017-06-02 04:30:06 +08:00
|
|
|
if (AliaseeVI)
|
|
|
|
visit(AliaseeVI);
|
2017-01-06 05:34:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-02 04:30:06 +08:00
|
|
|
Index.setWithGlobalValueDeadStripping();
|
|
|
|
|
|
|
|
unsigned DeadSymbols = Index.size() - LiveSymbols;
|
|
|
|
DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
|
|
|
|
<< " symbols Dead \n");
|
|
|
|
NumDeadSymbols += DeadSymbols;
|
|
|
|
NumLiveSymbols += LiveSymbols;
|
2017-01-06 05:34:18 +08:00
|
|
|
}
|
|
|
|
|
2016-05-10 21:48:23 +08:00
|
|
|
/// Compute the set of summaries needed for a ThinLTO backend compilation of
|
|
|
|
/// \p ModulePath.
|
|
|
|
void llvm::gatherImportedSummariesForModule(
|
|
|
|
StringRef ModulePath,
|
|
|
|
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
|
2016-08-16 13:46:05 +08:00
|
|
|
const FunctionImporter::ImportMapTy &ImportList,
|
2016-05-10 21:48:23 +08:00
|
|
|
std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
|
|
|
|
// Include all summaries from the importing module.
|
|
|
|
ModuleToSummariesForIndex[ModulePath] =
|
|
|
|
ModuleToDefinedGVSummaries.lookup(ModulePath);
|
2016-08-16 13:46:05 +08:00
|
|
|
// Include summaries for imports.
|
2016-08-16 13:49:12 +08:00
|
|
|
for (auto &ILI : ImportList) {
|
2016-08-16 13:46:05 +08:00
|
|
|
auto &SummariesForIndex = ModuleToSummariesForIndex[ILI.first()];
|
|
|
|
const auto &DefinedGVSummaries =
|
|
|
|
ModuleToDefinedGVSummaries.lookup(ILI.first());
|
|
|
|
for (auto &GI : ILI.second) {
|
|
|
|
const auto &DS = DefinedGVSummaries.find(GI.first);
|
|
|
|
assert(DS != DefinedGVSummaries.end() &&
|
|
|
|
"Expected a defined summary for imported global value");
|
|
|
|
SummariesForIndex[GI.first] = DS->second;
|
2016-05-10 21:48:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 23:54:09 +08:00
|
|
|
/// Emit the files \p ModulePath will import from into \p OutputFilename.
|
2016-08-16 13:46:05 +08:00
|
|
|
std::error_code
|
|
|
|
llvm::EmitImportsFiles(StringRef ModulePath, StringRef OutputFilename,
|
|
|
|
const FunctionImporter::ImportMapTy &ModuleImports) {
|
2016-05-10 23:54:09 +08:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream ImportsOS(OutputFilename, EC, sys::fs::OpenFlags::F_None);
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
2016-08-16 13:46:05 +08:00
|
|
|
for (auto &ILI : ModuleImports)
|
|
|
|
ImportsOS << ILI.first() << "\n";
|
2016-05-10 23:54:09 +08:00
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2016-05-25 22:03:11 +08:00
|
|
|
/// Fixup WeakForLinker linkages in \p TheModule based on summary analysis.
|
|
|
|
void llvm::thinLTOResolveWeakForLinkerModule(
|
|
|
|
Module &TheModule, const GVSummaryMapTy &DefinedGlobals) {
|
2017-01-21 05:54:58 +08:00
|
|
|
auto ConvertToDeclaration = [](GlobalValue &GV) {
|
|
|
|
DEBUG(dbgs() << "Converting to a declaration: `" << GV.getName() << "\n");
|
|
|
|
if (Function *F = dyn_cast<Function>(&GV)) {
|
|
|
|
F->deleteBody();
|
|
|
|
F->clearMetadata();
|
|
|
|
} else if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
|
|
|
|
V->setInitializer(nullptr);
|
|
|
|
V->setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
V->clearMetadata();
|
|
|
|
} else
|
|
|
|
// For now we don't resolve or drop aliases. Once we do we'll
|
|
|
|
// need to add support here for creating either a function or
|
|
|
|
// variable declaration, and return the new GlobalValue* for
|
|
|
|
// the caller to use.
|
2017-04-15 01:22:02 +08:00
|
|
|
llvm_unreachable("Expected function or variable");
|
2017-01-21 05:54:58 +08:00
|
|
|
};
|
|
|
|
|
2016-05-25 22:03:11 +08:00
|
|
|
auto updateLinkage = [&](GlobalValue &GV) {
|
|
|
|
// See if the global summary analysis computed a new resolved linkage.
|
|
|
|
const auto &GS = DefinedGlobals.find(GV.getGUID());
|
|
|
|
if (GS == DefinedGlobals.end())
|
|
|
|
return;
|
|
|
|
auto NewLinkage = GS->second->linkage();
|
|
|
|
if (NewLinkage == GV.getLinkage())
|
|
|
|
return;
|
2017-07-07 03:58:26 +08:00
|
|
|
|
|
|
|
// Switch the linkage to weakany if asked for, e.g. we do this for
|
|
|
|
// linker redefined symbols (via --wrap or --defsym).
|
2017-07-07 04:04:20 +08:00
|
|
|
// We record that the visibility should be changed here in `addThinLTO`
|
|
|
|
// as we need access to the resolution vectors for each input file in
|
|
|
|
// order to find which symbols have been redefined.
|
|
|
|
// We may consider reorganizing this code and moving the linkage recording
|
|
|
|
// somewhere else, e.g. in thinLTOResolveWeakForLinkerInIndex.
|
2017-07-07 03:58:26 +08:00
|
|
|
if (NewLinkage == GlobalValue::WeakAnyLinkage) {
|
|
|
|
GV.setLinkage(NewLinkage);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!GlobalValue::isWeakForLinker(GV.getLinkage()))
|
|
|
|
return;
|
2017-01-21 05:54:58 +08:00
|
|
|
// Check for a non-prevailing def that has interposable linkage
|
|
|
|
// (e.g. non-odr weak or linkonce). In that case we can't simply
|
|
|
|
// convert to available_externally, since it would lose the
|
|
|
|
// interposable property and possibly get inlined. Simply drop
|
|
|
|
// the definition in that case.
|
|
|
|
if (GlobalValue::isAvailableExternallyLinkage(NewLinkage) &&
|
|
|
|
GlobalValue::isInterposableLinkage(GV.getLinkage()))
|
|
|
|
ConvertToDeclaration(GV);
|
|
|
|
else {
|
|
|
|
DEBUG(dbgs() << "ODR fixing up linkage for `" << GV.getName() << "` from "
|
|
|
|
<< GV.getLinkage() << " to " << NewLinkage << "\n");
|
|
|
|
GV.setLinkage(NewLinkage);
|
|
|
|
}
|
|
|
|
// Remove declarations from comdats, including available_externally
|
2016-08-16 05:00:04 +08:00
|
|
|
// as this is a declaration for the linker, and will be dropped eventually.
|
|
|
|
// It is illegal for comdats to contain declarations.
|
|
|
|
auto *GO = dyn_cast_or_null<GlobalObject>(&GV);
|
2017-01-21 05:54:58 +08:00
|
|
|
if (GO && GO->isDeclarationForLinker() && GO->hasComdat())
|
2016-08-16 05:00:04 +08:00
|
|
|
GO->setComdat(nullptr);
|
2016-05-25 22:03:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Process functions and global now
|
|
|
|
for (auto &GV : TheModule)
|
|
|
|
updateLinkage(GV);
|
|
|
|
for (auto &GV : TheModule.globals())
|
|
|
|
updateLinkage(GV);
|
|
|
|
for (auto &GV : TheModule.aliases())
|
|
|
|
updateLinkage(GV);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Run internalization on \p TheModule based on symmary analysis.
|
|
|
|
void llvm::thinLTOInternalizeModule(Module &TheModule,
|
|
|
|
const GVSummaryMapTy &DefinedGlobals) {
|
|
|
|
// Parse inline ASM and collect the list of symbols that are not defined in
|
|
|
|
// the current module.
|
|
|
|
StringSet<> AsmUndefinedRefs;
|
2016-12-01 14:51:47 +08:00
|
|
|
ModuleSymbolTable::CollectAsmSymbols(
|
Perform symbol binding for .symver versioned symbols
Summary:
In a .symver assembler directive like:
.symver name, name2@@nodename
"name2@@nodename" should get the same symbol binding as "name".
While the ELF object writer is updating the symbol binding for .symver
aliases before emitting the object file, not doing so when the module
inline assembly is handled by the RecordStreamer is causing the wrong
behavior in *LTO mode.
E.g. when "name" is global, "name2@@nodename" must also be marked as
global. Otherwise, the symbol is skipped when iterating over the LTO
InputFile symbols (InputFile::Symbol::shouldSkip). So, for example,
when performing any *LTO via the gold-plugin, the versioned symbol
definition is not recorded by the plugin and passed back to the
linker. If the object was in an archive, and there were no other symbols
needed from that object, the object would not be included in the final
link and references to the versioned symbol are undefined.
The llvm-lto2 tests added will give an error about an unused symbol
resolution without the fix.
Reviewers: rafael, pcc
Reviewed By: pcc
Subscribers: mehdi_amini, llvm-commits
Differential Revision: https://reviews.llvm.org/D30485
llvm-svn: 297332
2017-03-09 08:19:49 +08:00
|
|
|
TheModule,
|
2016-05-25 22:03:11 +08:00
|
|
|
[&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) {
|
|
|
|
if (Flags & object::BasicSymbolRef::SF_Undefined)
|
|
|
|
AsmUndefinedRefs.insert(Name);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Declare a callback for the internalize pass that will ask for every
|
|
|
|
// candidate GlobalValue if it can be internalized or not.
|
|
|
|
auto MustPreserveGV = [&](const GlobalValue &GV) -> bool {
|
|
|
|
// Can't be internalized if referenced in inline asm.
|
|
|
|
if (AsmUndefinedRefs.count(GV.getName()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Lookup the linkage recorded in the summaries during global analysis.
|
2017-05-10 06:43:31 +08:00
|
|
|
auto GS = DefinedGlobals.find(GV.getGUID());
|
2016-05-25 22:03:11 +08:00
|
|
|
if (GS == DefinedGlobals.end()) {
|
|
|
|
// Must have been promoted (possibly conservatively). Find original
|
|
|
|
// name so that we can access the correct summary and see if it can
|
|
|
|
// be internalized again.
|
|
|
|
// FIXME: Eventually we should control promotion instead of promoting
|
|
|
|
// and internalizing again.
|
|
|
|
StringRef OrigName =
|
|
|
|
ModuleSummaryIndex::getOriginalNameBeforePromote(GV.getName());
|
|
|
|
std::string OrigId = GlobalValue::getGlobalIdentifier(
|
|
|
|
OrigName, GlobalValue::InternalLinkage,
|
|
|
|
TheModule.getSourceFileName());
|
2017-05-10 06:43:31 +08:00
|
|
|
GS = DefinedGlobals.find(GlobalValue::getGUID(OrigId));
|
2016-06-09 09:14:13 +08:00
|
|
|
if (GS == DefinedGlobals.end()) {
|
|
|
|
// Also check the original non-promoted non-globalized name. In some
|
|
|
|
// cases a preempted weak value is linked in as a local copy because
|
|
|
|
// it is referenced by an alias (IRLinker::linkGlobalValueProto).
|
|
|
|
// In that case, since it was originally not a local value, it was
|
|
|
|
// recorded in the index using the original name.
|
|
|
|
// FIXME: This may not be needed once PR27866 is fixed.
|
2017-05-10 06:43:31 +08:00
|
|
|
GS = DefinedGlobals.find(GlobalValue::getGUID(OrigName));
|
2016-06-09 09:14:13 +08:00
|
|
|
assert(GS != DefinedGlobals.end());
|
|
|
|
}
|
2017-05-10 06:43:31 +08:00
|
|
|
}
|
|
|
|
return !GlobalValue::isLocalLinkage(GS->second->linkage());
|
2016-05-25 22:03:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// FIXME: See if we can just internalize directly here via linkage changes
|
|
|
|
// based on the index, rather than invoking internalizeModule.
|
|
|
|
llvm::internalizeModule(TheModule, MustPreserveGV);
|
|
|
|
}
|
|
|
|
|
2015-12-03 10:37:33 +08:00
|
|
|
// Automatically import functions in Module \p DestModule based on the summaries
|
|
|
|
// index.
|
|
|
|
//
|
2016-11-10 01:49:19 +08:00
|
|
|
Expected<bool> FunctionImporter::importFunctions(
|
2017-05-20 07:32:21 +08:00
|
|
|
Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
|
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
|
|
|
|
2017-02-04 00:56:27 +08:00
|
|
|
IRMover Mover(DestModule);
|
2015-12-09 16:17:35 +08:00
|
|
|
// Do the actual import of functions now, one Module at a time
|
2016-03-26 13:40:34 +08:00
|
|
|
std::set<StringRef> ModuleNameOrderedList;
|
|
|
|
for (auto &FunctionsToImportPerModule : ImportList) {
|
|
|
|
ModuleNameOrderedList.insert(FunctionsToImportPerModule.first());
|
|
|
|
}
|
|
|
|
for (auto &Name : ModuleNameOrderedList) {
|
2015-12-09 16:17:35 +08:00
|
|
|
// Get the module for the import
|
2016-03-26 13:40:34 +08:00
|
|
|
const auto &FunctionsToImportPerModule = ImportList.find(Name);
|
|
|
|
assert(FunctionsToImportPerModule != ImportList.end());
|
2016-11-13 15:00:17 +08:00
|
|
|
Expected<std::unique_ptr<Module>> SrcModuleOrErr = ModuleLoader(Name);
|
|
|
|
if (!SrcModuleOrErr)
|
|
|
|
return SrcModuleOrErr.takeError();
|
|
|
|
std::unique_ptr<Module> SrcModule = std::move(*SrcModuleOrErr);
|
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).
|
2016-11-10 01:49:19 +08:00
|
|
|
if (Error Err = SrcModule->materializeMetadata())
|
|
|
|
return std::move(Err);
|
2015-12-18 01:14:09 +08:00
|
|
|
|
2016-03-26 13:40:34 +08:00
|
|
|
auto &ImportGUIDs = FunctionsToImportPerModule->second;
|
|
|
|
// Find the globals to import
|
2017-02-04 00:56:27 +08:00
|
|
|
SetVector<GlobalValue *> GlobalsToImport;
|
2016-07-07 02:12:23 +08:00
|
|
|
for (Function &F : *SrcModule) {
|
|
|
|
if (!F.hasName())
|
2016-04-05 02:52:23 +08:00
|
|
|
continue;
|
2016-07-07 02:12:23 +08:00
|
|
|
auto GUID = F.getGUID();
|
2016-04-05 02:52:23 +08:00
|
|
|
auto Import = ImportGUIDs.count(GUID);
|
2016-04-19 17:21:30 +08:00
|
|
|
DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing function " << GUID
|
2016-07-07 02:12:23 +08:00
|
|
|
<< " " << F.getName() << " from "
|
2016-04-19 17:21:30 +08:00
|
|
|
<< SrcModule->getSourceFileName() << "\n");
|
2016-04-05 02:52:23 +08:00
|
|
|
if (Import) {
|
2016-11-10 01:49:19 +08:00
|
|
|
if (Error Err = F.materialize())
|
|
|
|
return std::move(Err);
|
2016-07-09 07:01:49 +08:00
|
|
|
if (EnableImportMetadata) {
|
|
|
|
// Add 'thinlto_src_module' metadata for statistics and debugging.
|
|
|
|
F.setMetadata(
|
|
|
|
"thinlto_src_module",
|
|
|
|
llvm::MDNode::get(
|
|
|
|
DestModule.getContext(),
|
|
|
|
{llvm::MDString::get(DestModule.getContext(),
|
|
|
|
SrcModule->getSourceFileName())}));
|
|
|
|
}
|
2016-07-07 02:12:23 +08:00
|
|
|
GlobalsToImport.insert(&F);
|
2016-03-26 13:40:34 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-07 02:12:23 +08:00
|
|
|
for (GlobalVariable &GV : SrcModule->globals()) {
|
2016-03-26 13:40:34 +08:00
|
|
|
if (!GV.hasName())
|
|
|
|
continue;
|
|
|
|
auto GUID = GV.getGUID();
|
2016-04-05 02:52:23 +08:00
|
|
|
auto Import = ImportGUIDs.count(GUID);
|
2016-04-19 17:21:30 +08:00
|
|
|
DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing global " << GUID
|
|
|
|
<< " " << GV.getName() << " from "
|
|
|
|
<< SrcModule->getSourceFileName() << "\n");
|
2016-04-05 02:52:23 +08:00
|
|
|
if (Import) {
|
2016-11-10 01:49:19 +08:00
|
|
|
if (Error Err = GV.materialize())
|
|
|
|
return std::move(Err);
|
2016-03-27 23:01:11 +08:00
|
|
|
GlobalsToImport.insert(&GV);
|
2016-03-26 13:40:34 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-07 02:12:23 +08:00
|
|
|
for (GlobalAlias &GA : SrcModule->aliases()) {
|
2017-02-04 00:56:27 +08:00
|
|
|
// FIXME: This should eventually be controlled entirely by the summary.
|
|
|
|
if (FunctionImportGlobalProcessing::doImportAsDefinition(
|
|
|
|
&GA, &GlobalsToImport)) {
|
|
|
|
GlobalsToImport.insert(&GA);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-07 02:12:23 +08:00
|
|
|
if (!GA.hasName())
|
2016-03-26 13:40:34 +08:00
|
|
|
continue;
|
2016-07-07 02:12:23 +08:00
|
|
|
auto GUID = GA.getGUID();
|
2016-04-05 02:52:23 +08:00
|
|
|
auto Import = ImportGUIDs.count(GUID);
|
2016-04-19 17:21:30 +08:00
|
|
|
DEBUG(dbgs() << (Import ? "Is" : "Not") << " importing alias " << GUID
|
2016-07-07 02:12:23 +08:00
|
|
|
<< " " << GA.getName() << " from "
|
2016-04-19 17:21:30 +08:00
|
|
|
<< SrcModule->getSourceFileName() << "\n");
|
2016-04-05 02:52:23 +08:00
|
|
|
if (Import) {
|
2016-04-16 14:56:44 +08:00
|
|
|
// Alias can't point to "available_externally". However when we import
|
|
|
|
// linkOnceODR the linkage does not change. So we import the alias
|
2016-04-20 09:04:20 +08:00
|
|
|
// and aliasee only in this case. This has been handled by
|
|
|
|
// computeImportForFunction()
|
2016-07-07 02:12:23 +08:00
|
|
|
GlobalObject *GO = GA.getBaseObject();
|
2016-04-20 09:04:20 +08:00
|
|
|
assert(GO->hasLinkOnceODRLinkage() &&
|
|
|
|
"Unexpected alias to a non-linkonceODR in import list");
|
2016-04-16 14:56:44 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (!GlobalsToImport.count(GO))
|
|
|
|
DEBUG(dbgs() << " alias triggers importing aliasee " << GO->getGUID()
|
|
|
|
<< " " << GO->getName() << " from "
|
|
|
|
<< SrcModule->getSourceFileName() << "\n");
|
|
|
|
#endif
|
2016-11-10 01:49:19 +08:00
|
|
|
if (Error Err = GO->materialize())
|
|
|
|
return std::move(Err);
|
2016-04-16 14:56:44 +08:00
|
|
|
GlobalsToImport.insert(GO);
|
2016-11-10 01:49:19 +08:00
|
|
|
if (Error Err = GA.materialize())
|
|
|
|
return std::move(Err);
|
2016-07-07 02:12:23 +08:00
|
|
|
GlobalsToImport.insert(&GA);
|
2016-03-26 13:40:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 06:54:33 +08:00
|
|
|
// Upgrade debug info after we're done materializing all the globals and we
|
|
|
|
// have loaded all the required metadata!
|
|
|
|
UpgradeDebugInfo(*SrcModule);
|
|
|
|
|
2015-12-09 16:17:35 +08:00
|
|
|
// Link in the specified functions.
|
2016-03-26 13:40:34 +08:00
|
|
|
if (renameModuleForThinLTO(*SrcModule, Index, &GlobalsToImport))
|
2016-03-19 08:40:31 +08:00
|
|
|
return true;
|
|
|
|
|
2016-03-27 23:27:30 +08:00
|
|
|
if (PrintImports) {
|
|
|
|
for (const auto *GV : GlobalsToImport)
|
|
|
|
dbgs() << DestModule.getSourceFileName() << ": Import " << GV->getName()
|
|
|
|
<< " from " << SrcModule->getSourceFileName() << "\n";
|
|
|
|
}
|
|
|
|
|
2017-02-04 00:56:27 +08:00
|
|
|
if (Mover.move(std::move(SrcModule), GlobalsToImport.getArrayRef(),
|
|
|
|
[](GlobalValue &, IRMover::ValueAdder) {},
|
2017-02-04 01:01:14 +08:00
|
|
|
/*IsPerformingImport=*/true))
|
2015-12-09 16:17:35 +08:00
|
|
|
report_fatal_error("Function Import: link error");
|
|
|
|
|
2016-03-26 13:40:34 +08:00
|
|
|
ImportedCount += GlobalsToImport.size();
|
2017-01-06 05:34:18 +08:00
|
|
|
NumImportedModules++;
|
2015-12-09 16:17:35 +08:00
|
|
|
}
|
2015-12-18 01:14:09 +08:00
|
|
|
|
2017-01-06 05:34:18 +08:00
|
|
|
NumImportedFunctions += ImportedCount;
|
2016-03-27 23:27:30 +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."));
|
|
|
|
|
2016-12-21 08:50:12 +08:00
|
|
|
static bool doImportingForModule(Module &M) {
|
|
|
|
if (SummaryFile.empty())
|
|
|
|
report_fatal_error("error: -function-import requires -summary-file\n");
|
|
|
|
Expected<std::unique_ptr<ModuleSummaryIndex>> IndexPtrOrErr =
|
|
|
|
getModuleSummaryIndexForFile(SummaryFile);
|
|
|
|
if (!IndexPtrOrErr) {
|
|
|
|
logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(),
|
|
|
|
"Error loading file '" + SummaryFile + "': ");
|
|
|
|
return false;
|
2016-07-19 05:22:24 +08:00
|
|
|
}
|
2016-12-21 08:50:12 +08:00
|
|
|
std::unique_ptr<ModuleSummaryIndex> Index = std::move(*IndexPtrOrErr);
|
2016-07-19 05:22:24 +08:00
|
|
|
|
|
|
|
// First step is collecting the import list.
|
|
|
|
FunctionImporter::ImportMapTy ImportList;
|
|
|
|
ComputeCrossModuleImportForModule(M.getModuleIdentifier(), *Index,
|
|
|
|
ImportList);
|
|
|
|
|
2016-11-15 03:21:41 +08:00
|
|
|
// Conservatively mark all internal values as promoted. This interface is
|
|
|
|
// only used when doing importing via the function importing pass. The pass
|
|
|
|
// is only enabled when testing importing via the 'opt' tool, which does
|
|
|
|
// not do the ThinLink that would normally determine what values to promote.
|
|
|
|
for (auto &I : *Index) {
|
2017-05-05 02:03:25 +08:00
|
|
|
for (auto &S : I.second.SummaryList) {
|
2016-11-15 03:21:41 +08:00
|
|
|
if (GlobalValue::isLocalLinkage(S->linkage()))
|
|
|
|
S->setLinkage(GlobalValue::ExternalLinkage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 05:22:24 +08:00
|
|
|
// Next we need to promote to global scope and rename any local values that
|
|
|
|
// are potentially exported to other modules.
|
|
|
|
if (renameModuleForThinLTO(M, *Index, nullptr)) {
|
|
|
|
errs() << "Error renaming module\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the import now.
|
|
|
|
auto ModuleLoader = [&M](StringRef Identifier) {
|
|
|
|
return loadFile(Identifier, M.getContext());
|
|
|
|
};
|
|
|
|
FunctionImporter Importer(*Index, ModuleLoader);
|
2017-02-03 02:42:25 +08:00
|
|
|
Expected<bool> Result = Importer.importFunctions(M, ImportList);
|
2016-11-10 01:49:19 +08:00
|
|
|
|
|
|
|
// FIXME: Probably need to propagate Errors through the pass manager.
|
|
|
|
if (!Result) {
|
|
|
|
logAllUnhandledErrors(Result.takeError(), errs(),
|
|
|
|
"Error importing module: ");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *Result;
|
2016-07-19 05:22:24 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2016-07-19 05:22:24 +08:00
|
|
|
class FunctionImportLegacyPass : public ModulePass {
|
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
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override { return "Function Importing"; }
|
2015-12-08 03:21:11 +08:00
|
|
|
|
2016-12-21 08:50:12 +08:00
|
|
|
explicit FunctionImportLegacyPass() : ModulePass(ID) {}
|
2015-11-24 14:07:49 +08:00
|
|
|
|
|
|
|
bool runOnModule(Module &M) override {
|
2016-04-23 06:06:11 +08:00
|
|
|
if (skipModule(M))
|
|
|
|
return false;
|
|
|
|
|
2016-12-21 08:50:12 +08:00
|
|
|
return doImportingForModule(M);
|
2015-11-24 14:07:49 +08:00
|
|
|
}
|
|
|
|
};
|
2015-12-24 18:03:35 +08:00
|
|
|
} // anonymous namespace
|
2015-11-24 14:07:49 +08:00
|
|
|
|
2016-07-19 05:22:24 +08:00
|
|
|
PreservedAnalyses FunctionImportPass::run(Module &M,
|
2016-08-09 08:28:38 +08:00
|
|
|
ModuleAnalysisManager &AM) {
|
2016-12-21 08:50:12 +08:00
|
|
|
if (!doImportingForModule(M))
|
2016-07-19 05:22:24 +08:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
|
|
|
|
return PreservedAnalyses::none();
|
|
|
|
}
|
|
|
|
|
|
|
|
char FunctionImportLegacyPass::ID = 0;
|
|
|
|
INITIALIZE_PASS(FunctionImportLegacyPass, "function-import",
|
|
|
|
"Summary Based Function Import", false, false)
|
2015-11-24 14:07:49 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
2016-12-21 08:50:12 +08:00
|
|
|
Pass *createFunctionImportPass() {
|
|
|
|
return new FunctionImportLegacyPass();
|
2015-12-08 03:21:11 +08:00
|
|
|
}
|
2015-11-24 14:07:49 +08:00
|
|
|
}
|