aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ProfileSummaryInfo.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Analysis/ProfileSummaryInfo.cpp')
-rw-r--r--llvm/lib/Analysis/ProfileSummaryInfo.cpp279
1 files changed, 173 insertions, 106 deletions
diff --git a/llvm/lib/Analysis/ProfileSummaryInfo.cpp b/llvm/lib/Analysis/ProfileSummaryInfo.cpp
index 911d39d9a263..c9671d4f5c2e 100644
--- a/llvm/lib/Analysis/ProfileSummaryInfo.cpp
+++ b/llvm/lib/Analysis/ProfileSummaryInfo.cpp
@@ -14,11 +14,12 @@
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Analysis/BlockFrequencyInfo.h"
#include "llvm/IR/BasicBlock.h"
-#include "llvm/IR/CallSite.h"
+#include "llvm/IR/Instructions.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ProfileSummary.h"
#include "llvm/InitializePasses.h"
+#include "llvm/ProfileData/ProfileCommon.h"
#include "llvm/Support/CommandLine.h"
using namespace llvm;
@@ -66,48 +67,52 @@ static cl::opt<int> ProfileSummaryColdCount(
cl::desc("A fixed cold count that overrides the count derived from"
" profile-summary-cutoff-cold"));
-// Find the summary entry for a desired percentile of counts.
-static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS,
- uint64_t Percentile) {
- auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) {
- return Entry.Cutoff < Percentile;
- });
- // 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");
- return *It;
-}
+static cl::opt<bool> PartialProfile(
+ "partial-profile", cl::Hidden, cl::init(false),
+ cl::desc("Specify the current profile is used as a partial profile."));
+
+cl::opt<bool> ScalePartialSampleProfileWorkingSetSize(
+ "scale-partial-sample-profile-working-set-size", cl::Hidden, cl::init(true),
+ cl::desc(
+ "If true, scale the working set size of the partial sample profile "
+ "by the partial profile ratio to reflect the size of the program "
+ "being compiled."));
+
+static cl::opt<double> PartialSampleProfileWorkingSetSizeScaleFactor(
+ "partial-sample-profile-working-set-size-scale-factor", cl::Hidden,
+ cl::init(0.008),
+ cl::desc("The scale factor used to scale the working set size of the "
+ "partial sample profile along with the partial profile ratio. "
+ "This includes the factor of the profile counter per block "
+ "and the factor to scale the working set size to use the same "
+ "shared thresholds as PGO."));
// 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
-// available in the module and parses it to get the Summary object. Returns true
-// if a valid Summary is available.
-bool ProfileSummaryInfo::computeSummary() {
- if (Summary)
- return true;
+// available in the module and parses it to get the Summary object.
+void ProfileSummaryInfo::refresh() {
+ if (hasProfileSummary())
+ return;
// First try to get context sensitive ProfileSummary.
auto *SummaryMD = M.getProfileSummary(/* IsCS */ true);
- if (SummaryMD) {
+ if (SummaryMD)
Summary.reset(ProfileSummary::getFromMD(SummaryMD));
- return true;
+
+ if (!hasProfileSummary()) {
+ // This will actually return PSK_Instr or PSK_Sample summary.
+ SummaryMD = M.getProfileSummary(/* IsCS */ false);
+ if (SummaryMD)
+ Summary.reset(ProfileSummary::getFromMD(SummaryMD));
}
- // This will actually return PSK_Instr or PSK_Sample summary.
- SummaryMD = M.getProfileSummary(/* IsCS */ false);
- if (!SummaryMD)
- return false;
- Summary.reset(ProfileSummary::getFromMD(SummaryMD));
- return true;
+ if (!hasProfileSummary())
+ return;
+ computeThresholds();
}
-Optional<uint64_t>
-ProfileSummaryInfo::getProfileCount(const Instruction *Inst,
- BlockFrequencyInfo *BFI,
- bool AllowSynthetic) {
- if (!Inst)
- return None;
- assert((isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) &&
+Optional<uint64_t> ProfileSummaryInfo::getProfileCount(
+ const CallBase &Call, BlockFrequencyInfo *BFI, bool AllowSynthetic) const {
+ assert((isa<CallInst>(Call) || isa<InvokeInst>(Call)) &&
"We can only get profile count for call/invoke instruction.");
if (hasSampleProfile()) {
// In sample PGO mode, check if there is a profile metadata on the
@@ -115,20 +120,20 @@ ProfileSummaryInfo::getProfileCount(const Instruction *Inst,
// since the sampled entry count may not be accurate. If there is no
// annotated on the instruction, return None.
uint64_t TotalCount;
- if (Inst->extractProfTotalWeight(TotalCount))
+ if (Call.extractProfTotalWeight(TotalCount))
return TotalCount;
return None;
}
if (BFI)
- return BFI->getBlockProfileCount(Inst->getParent(), AllowSynthetic);
+ return BFI->getBlockProfileCount(Call.getParent(), AllowSynthetic);
return None;
}
/// 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
/// example, no profile data is available).
-bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
- if (!F || !computeSummary())
+bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) const {
+ if (!F || !hasProfileSummary())
return false;
auto FunctionCount = F->getEntryCount();
// FIXME: The heuristic used below for determining hotness is based on
@@ -142,9 +147,9 @@ bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
/// hot total call edge count.
/// If it returns false, it either means it is not hot or it is unknown
/// (for example, no profile data is available).
-bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F,
- BlockFrequencyInfo &BFI) {
- if (!F || !computeSummary())
+bool ProfileSummaryInfo::isFunctionHotInCallGraph(
+ const Function *F, BlockFrequencyInfo &BFI) const {
+ if (!F || !hasProfileSummary())
return false;
if (auto FunctionCount = F->getEntryCount())
if (isHotCount(FunctionCount.getCount()))
@@ -155,7 +160,7 @@ bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F,
for (const auto &BB : *F)
for (const auto &I : BB)
if (isa<CallInst>(I) || isa<InvokeInst>(I))
- if (auto CallCount = getProfileCount(&I, nullptr))
+ if (auto CallCount = getProfileCount(cast<CallBase>(I), nullptr))
TotalCallCount += CallCount.getValue();
if (isHotCount(TotalCallCount))
return true;
@@ -171,9 +176,9 @@ bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F,
/// the total call edge count is cold.
/// If it returns false, it either means it is not cold or it is unknown
/// (for example, no profile data is available).
-bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F,
- BlockFrequencyInfo &BFI) {
- if (!F || !computeSummary())
+bool ProfileSummaryInfo::isFunctionColdInCallGraph(
+ const Function *F, BlockFrequencyInfo &BFI) const {
+ if (!F || !hasProfileSummary())
return false;
if (auto FunctionCount = F->getEntryCount())
if (!isColdCount(FunctionCount.getCount()))
@@ -184,7 +189,7 @@ bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F,
for (const auto &BB : *F)
for (const auto &I : BB)
if (isa<CallInst>(I) || isa<InvokeInst>(I))
- if (auto CallCount = getProfileCount(&I, nullptr))
+ if (auto CallCount = getProfileCount(cast<CallBase>(I), nullptr))
TotalCallCount += CallCount.getValue();
if (!isColdCount(TotalCallCount))
return false;
@@ -195,40 +200,67 @@ bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F,
return true;
}
-// Like isFunctionHotInCallGraph but for a given cutoff.
-bool ProfileSummaryInfo::isFunctionHotInCallGraphNthPercentile(
- int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) {
- if (!F || !computeSummary())
+bool ProfileSummaryInfo::isFunctionHotnessUnknown(const Function &F) const {
+ assert(hasPartialSampleProfile() && "Expect partial sample profile");
+ return !F.getEntryCount().hasValue();
+}
+
+template <bool isHot>
+bool ProfileSummaryInfo::isFunctionHotOrColdInCallGraphNthPercentile(
+ int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const {
+ if (!F || !hasProfileSummary())
return false;
- if (auto FunctionCount = F->getEntryCount())
- if (isHotCountNthPercentile(PercentileCutoff, FunctionCount.getCount()))
+ if (auto FunctionCount = F->getEntryCount()) {
+ if (isHot &&
+ isHotCountNthPercentile(PercentileCutoff, FunctionCount.getCount()))
return true;
-
+ if (!isHot &&
+ !isColdCountNthPercentile(PercentileCutoff, FunctionCount.getCount()))
+ return false;
+ }
if (hasSampleProfile()) {
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))
+ if (auto CallCount = getProfileCount(cast<CallBase>(I), nullptr))
TotalCallCount += CallCount.getValue();
- if (isHotCountNthPercentile(PercentileCutoff, TotalCallCount))
+ if (isHot && isHotCountNthPercentile(PercentileCutoff, TotalCallCount))
return true;
+ if (!isHot && !isColdCountNthPercentile(PercentileCutoff, TotalCallCount))
+ return false;
}
- for (const auto &BB : *F)
- if (isHotBlockNthPercentile(PercentileCutoff, &BB, &BFI))
+ for (const auto &BB : *F) {
+ if (isHot && isHotBlockNthPercentile(PercentileCutoff, &BB, &BFI))
return true;
- return false;
+ if (!isHot && !isColdBlockNthPercentile(PercentileCutoff, &BB, &BFI))
+ return false;
+ }
+ return !isHot;
+}
+
+// Like isFunctionHotInCallGraph but for a given cutoff.
+bool ProfileSummaryInfo::isFunctionHotInCallGraphNthPercentile(
+ int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const {
+ return isFunctionHotOrColdInCallGraphNthPercentile<true>(
+ PercentileCutoff, F, BFI);
+}
+
+bool ProfileSummaryInfo::isFunctionColdInCallGraphNthPercentile(
+ int PercentileCutoff, const Function *F, BlockFrequencyInfo &BFI) const {
+ return isFunctionHotOrColdInCallGraphNthPercentile<false>(
+ PercentileCutoff, F, BFI);
}
/// 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
/// example, no profile data is available).
-bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
+bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) const {
if (!F)
return false;
if (F->hasFnAttribute(Attribute::Cold))
return true;
- if (!computeSummary())
+ if (!hasProfileSummary())
return false;
auto FunctionCount = F->getEntryCount();
// FIXME: The heuristic used below for determining coldness is based on
@@ -239,116 +271,151 @@ bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
/// Compute the hot and cold thresholds.
void ProfileSummaryInfo::computeThresholds() {
- if (!computeSummary())
- return;
auto &DetailedSummary = Summary->getDetailedSummary();
- auto &HotEntry =
- getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
+ auto &HotEntry = ProfileSummaryBuilder::getEntryForPercentile(
+ DetailedSummary, ProfileSummaryCutoffHot);
HotCountThreshold = HotEntry.MinCount;
if (ProfileSummaryHotCount.getNumOccurrences() > 0)
HotCountThreshold = ProfileSummaryHotCount;
- auto &ColdEntry =
- getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
+ auto &ColdEntry = ProfileSummaryBuilder::getEntryForPercentile(
+ DetailedSummary, ProfileSummaryCutoffCold);
ColdCountThreshold = ColdEntry.MinCount;
if (ProfileSummaryColdCount.getNumOccurrences() > 0)
ColdCountThreshold = ProfileSummaryColdCount;
assert(ColdCountThreshold <= HotCountThreshold &&
"Cold count threshold cannot exceed hot count threshold!");
- HasHugeWorkingSetSize =
- HotEntry.NumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
- HasLargeWorkingSetSize =
- HotEntry.NumCounts > ProfileSummaryLargeWorkingSetSizeThreshold;
+ if (!hasPartialSampleProfile() || !ScalePartialSampleProfileWorkingSetSize) {
+ HasHugeWorkingSetSize =
+ HotEntry.NumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
+ HasLargeWorkingSetSize =
+ HotEntry.NumCounts > ProfileSummaryLargeWorkingSetSizeThreshold;
+ } else {
+ // Scale the working set size of the partial sample profile to reflect the
+ // size of the program being compiled.
+ double PartialProfileRatio = Summary->getPartialProfileRatio();
+ uint64_t ScaledHotEntryNumCounts =
+ static_cast<uint64_t>(HotEntry.NumCounts * PartialProfileRatio *
+ PartialSampleProfileWorkingSetSizeScaleFactor);
+ HasHugeWorkingSetSize =
+ ScaledHotEntryNumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
+ HasLargeWorkingSetSize =
+ ScaledHotEntryNumCounts > ProfileSummaryLargeWorkingSetSizeThreshold;
+ }
}
-Optional<uint64_t> ProfileSummaryInfo::computeThreshold(int PercentileCutoff) {
- if (!computeSummary())
+Optional<uint64_t>
+ProfileSummaryInfo::computeThreshold(int PercentileCutoff) const {
+ if (!hasProfileSummary())
return None;
auto iter = ThresholdCache.find(PercentileCutoff);
if (iter != ThresholdCache.end()) {
return iter->second;
}
auto &DetailedSummary = Summary->getDetailedSummary();
- auto &Entry =
- getEntryForPercentile(DetailedSummary, PercentileCutoff);
+ auto &Entry = ProfileSummaryBuilder::getEntryForPercentile(DetailedSummary,
+ PercentileCutoff);
uint64_t CountThreshold = Entry.MinCount;
ThresholdCache[PercentileCutoff] = CountThreshold;
return CountThreshold;
}
-bool ProfileSummaryInfo::hasHugeWorkingSetSize() {
- if (!HasHugeWorkingSetSize)
- computeThresholds();
+bool ProfileSummaryInfo::hasHugeWorkingSetSize() const {
return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue();
}
-bool ProfileSummaryInfo::hasLargeWorkingSetSize() {
- if (!HasLargeWorkingSetSize)
- computeThresholds();
+bool ProfileSummaryInfo::hasLargeWorkingSetSize() const {
return HasLargeWorkingSetSize && HasLargeWorkingSetSize.getValue();
}
-bool ProfileSummaryInfo::isHotCount(uint64_t C) {
- if (!HotCountThreshold)
- computeThresholds();
+bool ProfileSummaryInfo::isHotCount(uint64_t C) const {
return HotCountThreshold && C >= HotCountThreshold.getValue();
}
-bool ProfileSummaryInfo::isColdCount(uint64_t C) {
- if (!ColdCountThreshold)
- computeThresholds();
+bool ProfileSummaryInfo::isColdCount(uint64_t C) const {
return ColdCountThreshold && C <= ColdCountThreshold.getValue();
}
-bool ProfileSummaryInfo::isHotCountNthPercentile(int PercentileCutoff, uint64_t C) {
+template <bool isHot>
+bool ProfileSummaryInfo::isHotOrColdCountNthPercentile(int PercentileCutoff,
+ uint64_t C) const {
auto CountThreshold = computeThreshold(PercentileCutoff);
- return CountThreshold && C >= CountThreshold.getValue();
+ if (isHot)
+ return CountThreshold && C >= CountThreshold.getValue();
+ else
+ return CountThreshold && C <= CountThreshold.getValue();
}
-uint64_t ProfileSummaryInfo::getOrCompHotCountThreshold() {
- if (!HotCountThreshold)
- computeThresholds();
+bool ProfileSummaryInfo::isHotCountNthPercentile(int PercentileCutoff,
+ uint64_t C) const {
+ return isHotOrColdCountNthPercentile<true>(PercentileCutoff, C);
+}
+
+bool ProfileSummaryInfo::isColdCountNthPercentile(int PercentileCutoff,
+ uint64_t C) const {
+ return isHotOrColdCountNthPercentile<false>(PercentileCutoff, C);
+}
+
+uint64_t ProfileSummaryInfo::getOrCompHotCountThreshold() const {
return HotCountThreshold ? HotCountThreshold.getValue() : UINT64_MAX;
}
-uint64_t ProfileSummaryInfo::getOrCompColdCountThreshold() {
- if (!ColdCountThreshold)
- computeThresholds();
+uint64_t ProfileSummaryInfo::getOrCompColdCountThreshold() const {
return ColdCountThreshold ? ColdCountThreshold.getValue() : 0;
}
-bool ProfileSummaryInfo::isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) {
+bool ProfileSummaryInfo::isHotBlock(const BasicBlock *BB,
+ BlockFrequencyInfo *BFI) const {
auto Count = BFI->getBlockProfileCount(BB);
return Count && isHotCount(*Count);
}
bool ProfileSummaryInfo::isColdBlock(const BasicBlock *BB,
- BlockFrequencyInfo *BFI) {
+ BlockFrequencyInfo *BFI) const {
auto Count = BFI->getBlockProfileCount(BB);
return Count && isColdCount(*Count);
}
-bool ProfileSummaryInfo::isHotBlockNthPercentile(int PercentileCutoff,
- const BasicBlock *BB,
- BlockFrequencyInfo *BFI) {
+template <bool isHot>
+bool ProfileSummaryInfo::isHotOrColdBlockNthPercentile(
+ int PercentileCutoff, const BasicBlock *BB, BlockFrequencyInfo *BFI) const {
auto Count = BFI->getBlockProfileCount(BB);
- return Count && isHotCountNthPercentile(PercentileCutoff, *Count);
+ if (isHot)
+ return Count && isHotCountNthPercentile(PercentileCutoff, *Count);
+ else
+ return Count && isColdCountNthPercentile(PercentileCutoff, *Count);
+}
+
+bool ProfileSummaryInfo::isHotBlockNthPercentile(
+ int PercentileCutoff, const BasicBlock *BB, BlockFrequencyInfo *BFI) const {
+ return isHotOrColdBlockNthPercentile<true>(PercentileCutoff, BB, BFI);
+}
+
+bool ProfileSummaryInfo::isColdBlockNthPercentile(
+ int PercentileCutoff, const BasicBlock *BB, BlockFrequencyInfo *BFI) const {
+ return isHotOrColdBlockNthPercentile<false>(PercentileCutoff, BB, BFI);
}
-bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS,
- BlockFrequencyInfo *BFI) {
- auto C = getProfileCount(CS.getInstruction(), BFI);
+bool ProfileSummaryInfo::isHotCallSite(const CallBase &CB,
+ BlockFrequencyInfo *BFI) const {
+ auto C = getProfileCount(CB, BFI);
return C && isHotCount(*C);
}
-bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS,
- BlockFrequencyInfo *BFI) {
- auto C = getProfileCount(CS.getInstruction(), BFI);
+bool ProfileSummaryInfo::isColdCallSite(const CallBase &CB,
+ BlockFrequencyInfo *BFI) const {
+ auto C = getProfileCount(CB, BFI);
if (C)
return isColdCount(*C);
// In SamplePGO, if the caller has been sampled, and there is no profile
// annotated on the callsite, we consider the callsite as cold.
- return hasSampleProfile() && CS.getCaller()->hasProfileData();
+ return hasSampleProfile() && CB.getCaller()->hasProfileData();
+}
+
+bool ProfileSummaryInfo::hasPartialSampleProfile() const {
+ return hasProfileSummary() &&
+ Summary->getKind() == ProfileSummary::PSK_Sample &&
+ (PartialProfile || Summary->isPartialProfile());
}
INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",