2016-06-04 00:54:26 +02:00
|
|
|
//===- ProfileSummaryInfo.cpp - Global profile summary information --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a pass that provides access to the global profile summary
|
|
|
|
// information.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/ProfileSummaryInfo.h"
|
2017-01-13 02:34:00 +01:00
|
|
|
#include "llvm/Analysis/BlockFrequencyInfo.h"
|
2016-11-10 00:36:02 +01:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2017-01-13 02:34:00 +01:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2016-06-04 00:54:26 +02:00
|
|
|
#include "llvm/IR/Metadata.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/ProfileSummary.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
// The following two parameters determine the threshold for a count to be
|
|
|
|
// considered hot/cold. These two parameters are percentile values (multiplied
|
|
|
|
// by 10000). If the counts are sorted in descending order, the minimum count to
|
|
|
|
// reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
|
|
|
|
// Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
|
|
|
|
// threshold for determining cold count (everything <= this threshold is
|
|
|
|
// considered cold).
|
|
|
|
|
|
|
|
static cl::opt<int> ProfileSummaryCutoffHot(
|
2017-08-04 18:20:54 +02:00
|
|
|
"profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore,
|
2016-06-04 00:54:26 +02:00
|
|
|
cl::desc("A count is hot if it exceeds the minimum count to"
|
|
|
|
" reach this percentile of total counts."));
|
|
|
|
|
|
|
|
static cl::opt<int> ProfileSummaryCutoffCold(
|
|
|
|
"profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore,
|
|
|
|
cl::desc("A count is cold if it is below the minimum count"
|
|
|
|
" to reach this percentile of total counts."));
|
|
|
|
|
2017-08-03 19:11:41 +02:00
|
|
|
static cl::opt<bool> AccurateSampleProfile(
|
|
|
|
"accurate-sample-profile", cl::Hidden, cl::init(false),
|
|
|
|
cl::desc("If the sample profile is accurate, we will mark all un-sampled "
|
|
|
|
"callsite as cold. Otherwise, treat un-sampled callsites as if "
|
|
|
|
"we have no profile."));
|
2017-08-04 01:42:58 +02:00
|
|
|
static cl::opt<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold(
|
|
|
|
"profile-summary-huge-working-set-size-threshold", cl::Hidden,
|
|
|
|
cl::init(15000), cl::ZeroOrMore,
|
|
|
|
cl::desc("The code working set size is considered huge if the number of"
|
|
|
|
" blocks required to reach the -profile-summary-cutoff-hot"
|
|
|
|
" percentile exceeds this count."));
|
|
|
|
|
|
|
|
// Find the summary entry for a desired percentile of counts.
|
|
|
|
static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS,
|
|
|
|
uint64_t Percentile) {
|
2016-06-04 00:54:26 +02:00
|
|
|
auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) {
|
|
|
|
return Entry.Cutoff < Percentile;
|
|
|
|
};
|
|
|
|
auto It = std::lower_bound(DS.begin(), DS.end(), Percentile, Compare);
|
|
|
|
// The required percentile has to be <= one of the percentiles in the
|
|
|
|
// detailed summary.
|
|
|
|
if (It == DS.end())
|
|
|
|
report_fatal_error("Desired percentile exceeds the maximum cutoff");
|
2017-08-04 01:42:58 +02:00
|
|
|
return *It;
|
2016-06-04 00:54:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// The profile summary metadata may be attached either by the frontend or by
|
|
|
|
// any backend passes (IR level instrumentation, for example). This method
|
|
|
|
// checks if the Summary is null and if so checks if the summary metadata is now
|
2017-01-14 01:32:37 +01:00
|
|
|
// available in the module and parses it to get the Summary object. Returns true
|
|
|
|
// if a valid Summary is available.
|
|
|
|
bool ProfileSummaryInfo::computeSummary() {
|
2016-06-04 00:54:26 +02:00
|
|
|
if (Summary)
|
2017-01-14 01:32:37 +01:00
|
|
|
return true;
|
2016-09-28 23:00:58 +02:00
|
|
|
auto *SummaryMD = M.getProfileSummary();
|
2016-06-04 00:54:26 +02:00
|
|
|
if (!SummaryMD)
|
2017-01-14 01:32:37 +01:00
|
|
|
return false;
|
2016-06-04 00:54:26 +02:00
|
|
|
Summary.reset(ProfileSummary::getFromMD(SummaryMD));
|
2017-01-14 01:32:37 +01:00
|
|
|
return true;
|
2016-06-04 00:54:26 +02:00
|
|
|
}
|
|
|
|
|
2017-03-10 20:45:16 +01:00
|
|
|
Optional<uint64_t>
|
|
|
|
ProfileSummaryInfo::getProfileCount(const Instruction *Inst,
|
|
|
|
BlockFrequencyInfo *BFI) {
|
|
|
|
if (!Inst)
|
|
|
|
return None;
|
|
|
|
assert((isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) &&
|
|
|
|
"We can only get profile count for call/invoke instruction.");
|
2017-05-16 22:14:39 +02:00
|
|
|
if (hasSampleProfile()) {
|
2017-05-12 01:18:05 +02:00
|
|
|
// In sample PGO mode, check if there is a profile metadata on the
|
|
|
|
// instruction. If it is present, determine hotness solely based on that,
|
2017-08-03 19:11:41 +02:00
|
|
|
// since the sampled entry count may not be accurate. If there is no
|
|
|
|
// annotated on the instruction, return None.
|
2017-05-12 01:18:05 +02:00
|
|
|
uint64_t TotalCount;
|
|
|
|
if (Inst->extractProfTotalWeight(TotalCount))
|
|
|
|
return TotalCount;
|
2017-08-03 19:11:41 +02:00
|
|
|
return None;
|
2017-05-12 01:18:05 +02:00
|
|
|
}
|
2017-03-10 20:45:16 +01:00
|
|
|
if (BFI)
|
|
|
|
return BFI->getBlockProfileCount(Inst->getParent());
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2016-10-10 23:47:28 +02:00
|
|
|
/// Returns true if the function's entry is hot. If it returns false, it
|
|
|
|
/// either means it is not hot or it is unknown whether it is hot or not (for
|
2016-09-30 23:05:49 +02:00
|
|
|
/// example, no profile data is available).
|
2016-10-10 23:47:28 +02:00
|
|
|
bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
|
2017-01-14 01:32:37 +01:00
|
|
|
if (!F || !computeSummary())
|
2016-06-04 00:54:26 +02:00
|
|
|
return false;
|
|
|
|
auto FunctionCount = F->getEntryCount();
|
|
|
|
// FIXME: The heuristic used below for determining hotness is based on
|
|
|
|
// preliminary SPEC tuning for inliner. This will eventually be a
|
|
|
|
// convenience method that calls isHotCount.
|
2016-10-11 07:19:00 +02:00
|
|
|
return FunctionCount && isHotCount(FunctionCount.getValue());
|
2016-06-04 00:54:26 +02:00
|
|
|
}
|
|
|
|
|
2017-03-24 00:14:11 +01:00
|
|
|
/// Returns true if the function's entry or total call edge count is hot.
|
|
|
|
/// If it returns false, it either means it is not hot or it is unknown
|
|
|
|
/// whether it is hot or not (for example, no profile data is available).
|
|
|
|
bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F) {
|
|
|
|
if (!F || !computeSummary())
|
|
|
|
return false;
|
|
|
|
if (auto FunctionCount = F->getEntryCount())
|
|
|
|
if (isHotCount(FunctionCount.getValue()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
uint64_t TotalCallCount = 0;
|
|
|
|
for (const auto &BB : *F)
|
|
|
|
for (const auto &I : BB)
|
|
|
|
if (isa<CallInst>(I) || isa<InvokeInst>(I))
|
|
|
|
if (auto CallCount = getProfileCount(&I, nullptr))
|
|
|
|
TotalCallCount += CallCount.getValue();
|
|
|
|
return isHotCount(TotalCallCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the function's entry and total call edge count is cold.
|
|
|
|
/// If it returns false, it either means it is not cold or it is unknown
|
|
|
|
/// whether it is cold or not (for example, no profile data is available).
|
|
|
|
bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F) {
|
|
|
|
if (!F || !computeSummary())
|
|
|
|
return false;
|
|
|
|
if (auto FunctionCount = F->getEntryCount())
|
|
|
|
if (!isColdCount(FunctionCount.getValue()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint64_t TotalCallCount = 0;
|
|
|
|
for (const auto &BB : *F)
|
|
|
|
for (const auto &I : BB)
|
|
|
|
if (isa<CallInst>(I) || isa<InvokeInst>(I))
|
|
|
|
if (auto CallCount = getProfileCount(&I, nullptr))
|
|
|
|
TotalCallCount += CallCount.getValue();
|
|
|
|
return isColdCount(TotalCallCount);
|
|
|
|
}
|
|
|
|
|
2016-10-10 23:47:28 +02:00
|
|
|
/// Returns true if the function's entry is a cold. If it returns false, it
|
|
|
|
/// either means it is not cold or it is unknown whether it is cold or not (for
|
2016-09-30 23:05:49 +02:00
|
|
|
/// example, no profile data is available).
|
2016-10-10 23:47:28 +02:00
|
|
|
bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
|
2016-06-10 03:42:05 +02:00
|
|
|
if (!F)
|
|
|
|
return false;
|
2017-03-10 21:50:51 +01:00
|
|
|
if (F->hasFnAttribute(Attribute::Cold))
|
2016-06-04 00:54:26 +02:00
|
|
|
return true;
|
2017-01-14 01:32:37 +01:00
|
|
|
if (!computeSummary())
|
2016-06-10 03:42:05 +02:00
|
|
|
return false;
|
2016-06-04 00:54:26 +02:00
|
|
|
auto FunctionCount = F->getEntryCount();
|
|
|
|
// FIXME: The heuristic used below for determining coldness is based on
|
|
|
|
// preliminary SPEC tuning for inliner. This will eventually be a
|
|
|
|
// convenience method that calls isHotCount.
|
2016-10-11 07:19:00 +02:00
|
|
|
return FunctionCount && isColdCount(FunctionCount.getValue());
|
2016-06-04 00:54:26 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 23:05:49 +02:00
|
|
|
/// Compute the hot and cold thresholds.
|
2016-06-04 00:54:26 +02:00
|
|
|
void ProfileSummaryInfo::computeThresholds() {
|
2017-01-14 01:32:37 +01:00
|
|
|
if (!computeSummary())
|
2016-06-04 00:54:26 +02:00
|
|
|
return;
|
|
|
|
auto &DetailedSummary = Summary->getDetailedSummary();
|
2017-08-04 01:42:58 +02:00
|
|
|
auto &HotEntry =
|
|
|
|
getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
|
|
|
|
HotCountThreshold = HotEntry.MinCount;
|
|
|
|
auto &ColdEntry =
|
|
|
|
getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
|
|
|
|
ColdCountThreshold = ColdEntry.MinCount;
|
|
|
|
HasHugeWorkingSetSize =
|
|
|
|
HotEntry.NumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileSummaryInfo::hasHugeWorkingSetSize() {
|
|
|
|
if (!HasHugeWorkingSetSize)
|
|
|
|
computeThresholds();
|
|
|
|
return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue();
|
2016-06-04 00:54:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileSummaryInfo::isHotCount(uint64_t C) {
|
|
|
|
if (!HotCountThreshold)
|
|
|
|
computeThresholds();
|
|
|
|
return HotCountThreshold && C >= HotCountThreshold.getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileSummaryInfo::isColdCount(uint64_t C) {
|
|
|
|
if (!ColdCountThreshold)
|
|
|
|
computeThresholds();
|
|
|
|
return ColdCountThreshold && C <= ColdCountThreshold.getValue();
|
|
|
|
}
|
|
|
|
|
2016-11-10 00:36:02 +01:00
|
|
|
bool ProfileSummaryInfo::isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI) {
|
|
|
|
auto Count = BFI->getBlockProfileCount(B);
|
2017-03-10 02:44:37 +01:00
|
|
|
return Count && isHotCount(*Count);
|
2017-01-13 02:34:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileSummaryInfo::isColdBB(const BasicBlock *B,
|
|
|
|
BlockFrequencyInfo *BFI) {
|
|
|
|
auto Count = BFI->getBlockProfileCount(B);
|
|
|
|
return Count && isColdCount(*Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS,
|
|
|
|
BlockFrequencyInfo *BFI) {
|
2017-03-10 20:45:16 +01:00
|
|
|
auto C = getProfileCount(CS.getInstruction(), BFI);
|
|
|
|
return C && isHotCount(*C);
|
2017-01-13 02:34:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS,
|
|
|
|
BlockFrequencyInfo *BFI) {
|
2017-03-10 20:45:16 +01:00
|
|
|
auto C = getProfileCount(CS.getInstruction(), BFI);
|
2017-08-03 19:11:41 +02:00
|
|
|
if (C)
|
|
|
|
return isColdCount(*C);
|
|
|
|
|
|
|
|
// In SamplePGO, if the caller has been sampled, and there is no profile
|
|
|
|
// annotatedon the callsite, we consider the callsite as cold.
|
|
|
|
// If there is no profile for the caller, and we know the profile is
|
|
|
|
// accurate, we consider the callsite as cold.
|
|
|
|
return (hasSampleProfile() &&
|
|
|
|
(CS.getCaller()->getEntryCount() || AccurateSampleProfile));
|
2016-11-10 00:36:02 +01:00
|
|
|
}
|
|
|
|
|
2016-06-04 00:54:26 +02:00
|
|
|
INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
|
|
|
|
"Profile summary info", false, true)
|
|
|
|
|
|
|
|
ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
|
|
|
|
: ImmutablePass(ID) {
|
|
|
|
initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2016-09-28 23:00:58 +02:00
|
|
|
bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
|
|
|
|
PSI.reset(new ProfileSummaryInfo(M));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
|
|
|
|
PSI.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-23 18:53:26 +01:00
|
|
|
AnalysisKey ProfileSummaryAnalysis::Key;
|
2016-06-17 02:11:01 +02:00
|
|
|
ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
|
|
|
|
ModuleAnalysisManager &) {
|
2016-09-28 23:00:58 +02:00
|
|
|
return ProfileSummaryInfo(M);
|
2016-06-04 00:54:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
|
2016-08-09 02:28:38 +02:00
|
|
|
ModuleAnalysisManager &AM) {
|
2016-06-04 00:54:26 +02:00
|
|
|
ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
|
|
|
|
|
|
|
|
OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
|
|
|
|
for (auto &F : M) {
|
|
|
|
OS << F.getName();
|
2016-10-10 23:47:28 +02:00
|
|
|
if (PSI.isFunctionEntryHot(&F))
|
|
|
|
OS << " :hot entry ";
|
|
|
|
else if (PSI.isFunctionEntryCold(&F))
|
|
|
|
OS << " :cold entry ";
|
2016-06-04 00:54:26 +02:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
|
|
|
char ProfileSummaryInfoWrapperPass::ID = 0;
|