[ThinLTO] Perform profile-guided indirect call promotion
Summary: To enable profile-guided indirect call promotion in ThinLTO mode, we simply add call graph edges for each profitable target from the profile to the summaries, then the summary-guided importing will consider the callee for importing as usual. Also we need to enable the indirect call promotion pass creation in the PassManagerBuilder when PerformThinLTO=true (we are in the ThinLTO backend), so that the newly imported functions are considered for promotion in the backends. The IC promotion profiles refer to callees by GUID, which required adding GUIDs to the per-module VST in bitcode (and assigning them valueIds similar to how they are assigned valueIds in the combined index). Reviewers: mehdi_amini, xur Subscribers: mehdi_amini, davidxl, llvm-commits Differential Revision: http://reviews.llvm.org/D21932 llvm-svn: 275707
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include "llvm/Analysis/BlockFrequencyInfo.h"
|
||||
#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
|
||||
#include "llvm/Analysis/BranchProbabilityInfo.h"
|
||||
#include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
|
||||
#include "llvm/Analysis/LoopInfo.h"
|
||||
#include "llvm/IR/CallSite.h"
|
||||
#include "llvm/IR/Dominators.h"
|
||||
@@ -73,7 +74,9 @@ void ModuleSummaryIndexBuilder::computeFunctionSummary(
|
||||
// Map from callee ValueId to profile count. Used to accumulate profile
|
||||
// counts for all static calls to a given callee.
|
||||
DenseMap<const Value *, CalleeInfo> CallGraphEdges;
|
||||
DenseMap<GlobalValue::GUID, CalleeInfo> IndirectCallEdges;
|
||||
DenseSet<const Value *> RefEdges;
|
||||
ICallPromotionAnalysis ICallAnalysis;
|
||||
|
||||
SmallPtrSet<const User *, 8> Visited;
|
||||
for (const BasicBlock &BB : F)
|
||||
@@ -83,13 +86,29 @@ void ModuleSummaryIndexBuilder::computeFunctionSummary(
|
||||
|
||||
if (auto CS = ImmutableCallSite(&I)) {
|
||||
auto *CalledFunction = CS.getCalledFunction();
|
||||
if (CalledFunction && CalledFunction->hasName() &&
|
||||
!CalledFunction->isIntrinsic()) {
|
||||
auto ScaledCount = BFI ? BFI->getBlockProfileCount(&BB) : None;
|
||||
auto *CalleeId =
|
||||
M->getValueSymbolTable().lookup(CalledFunction->getName());
|
||||
CallGraphEdges[CalleeId] +=
|
||||
(ScaledCount ? ScaledCount.getValue() : 0);
|
||||
// Check if this is a direct call to a known function.
|
||||
if (CalledFunction) {
|
||||
if (CalledFunction->hasName() && !CalledFunction->isIntrinsic()) {
|
||||
auto ScaledCount = BFI ? BFI->getBlockProfileCount(&BB) : None;
|
||||
auto *CalleeId =
|
||||
M->getValueSymbolTable().lookup(CalledFunction->getName());
|
||||
CallGraphEdges[CalleeId] +=
|
||||
(ScaledCount ? ScaledCount.getValue() : 0);
|
||||
}
|
||||
} else {
|
||||
// Otherwise, check for an indirect call (call to a non-const value
|
||||
// that isn't an inline assembly call).
|
||||
const CallInst *CI = dyn_cast<CallInst>(&I);
|
||||
if (CS.getCalledValue() && !isa<Constant>(CS.getCalledValue()) &&
|
||||
!(CI && CI->isInlineAsm())) {
|
||||
uint32_t NumVals, NumCandidates;
|
||||
uint64_t TotalCount;
|
||||
auto CandidateProfileData =
|
||||
ICallAnalysis.getPromotionCandidatesForInstruction(
|
||||
&I, NumVals, TotalCount, NumCandidates);
|
||||
for (auto &Candidate : CandidateProfileData)
|
||||
IndirectCallEdges[Candidate.Value] += Candidate.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
findRefEdges(&I, RefEdges, Visited);
|
||||
@@ -99,6 +118,7 @@ void ModuleSummaryIndexBuilder::computeFunctionSummary(
|
||||
std::unique_ptr<FunctionSummary> FuncSummary =
|
||||
llvm::make_unique<FunctionSummary>(Flags, NumInsts);
|
||||
FuncSummary->addCallGraphEdges(CallGraphEdges);
|
||||
FuncSummary->addCallGraphEdges(IndirectCallEdges);
|
||||
FuncSummary->addRefEdges(RefEdges);
|
||||
Index->addGlobalValueSummary(F.getName(), std::move(FuncSummary));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user