LLVM 18.0.0git
Go to the documentation of this file.
1//===- ProfileSummaryInfo.cpp - Global profile summary information --------===//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9// This file contains a pass that provides access to the global profile summary
10// information.
16#include "llvm/IR/BasicBlock.h"
18#include "llvm/IR/Module.h"
23#include <optional>
24using namespace llvm;
27 "partial-profile", cl::Hidden, cl::init(false),
28 cl::desc("Specify the current profile is used as a partial profile."));
31 "scale-partial-sample-profile-working-set-size", cl::Hidden, cl::init(true),
33 "If true, scale the working set size of the partial sample profile "
34 "by the partial profile ratio to reflect the size of the program "
35 "being compiled."));
38 "partial-sample-profile-working-set-size-scale-factor", cl::Hidden,
39 cl::init(0.008),
40 cl::desc("The scale factor used to scale the working set size of the "
41 "partial sample profile along with the partial profile ratio. "
42 "This includes the factor of the profile counter per block "
43 "and the factor to scale the working set size to use the same "
44 "shared thresholds as PGO."));
46// The profile summary metadata may be attached either by the frontend or by
47// any backend passes (IR level instrumentation, for example). This method
48// checks if the Summary is null and if so checks if the summary metadata is now
49// available in the module and parses it to get the Summary object.
52 return;
53 // First try to get context sensitive ProfileSummary.
54 auto *SummaryMD = M->getProfileSummary(/* IsCS */ true);
55 if (SummaryMD)
56 Summary.reset(ProfileSummary::getFromMD(SummaryMD));
58 if (!hasProfileSummary()) {
59 // This will actually return PSK_Instr or PSK_Sample summary.
60 SummaryMD = M->getProfileSummary(/* IsCS */ false);
61 if (SummaryMD)
62 Summary.reset(ProfileSummary::getFromMD(SummaryMD));
63 }
64 if (!hasProfileSummary())
65 return;
66 computeThresholds();
70 const CallBase &Call, BlockFrequencyInfo *BFI, bool AllowSynthetic) const {
71 assert((isa<CallInst>(Call) || isa<InvokeInst>(Call)) &&
72 "We can only get profile count for call/invoke instruction.");
73 if (hasSampleProfile()) {
74 // In sample PGO mode, check if there is a profile metadata on the
75 // instruction. If it is present, determine hotness solely based on that,
76 // since the sampled entry count may not be accurate. If there is no
77 // annotated on the instruction, return std::nullopt.
78 uint64_t TotalCount;
79 if (Call.extractProfTotalWeight(TotalCount))
80 return TotalCount;
81 return std::nullopt;
82 }
83 if (BFI)
84 return BFI->getBlockProfileCount(Call.getParent(), AllowSynthetic);
85 return std::nullopt;
89 assert(hasPartialSampleProfile() && "Expect partial sample profile");
90 return !F.getEntryCount();
93/// Returns true if the function's entry is a cold. If it returns false, it
94/// either means it is not cold or it is unknown whether it is cold or not (for
95/// example, no profile data is available).
97 if (!F)
98 return false;
99 if (F->hasFnAttribute(Attribute::Cold))
100 return true;
101 if (!hasProfileSummary())
102 return false;
103 auto FunctionCount = F->getEntryCount();
104 // FIXME: The heuristic used below for determining coldness is based on
105 // preliminary SPEC tuning for inliner. This will eventually be a
106 // convenience method that calls isHotCount.
107 return FunctionCount && isColdCount(FunctionCount->getCount());
110/// Compute the hot and cold thresholds.
111void ProfileSummaryInfo::computeThresholds() {
112 auto &DetailedSummary = Summary->getDetailedSummary();
114 DetailedSummary, ProfileSummaryCutoffHot);
115 HotCountThreshold =
117 ColdCountThreshold =
119 assert(ColdCountThreshold <= HotCountThreshold &&
120 "Cold count threshold cannot exceed hot count threshold!");
122 HasHugeWorkingSetSize =
124 HasLargeWorkingSetSize =
126 } else {
127 // Scale the working set size of the partial sample profile to reflect the
128 // size of the program being compiled.
129 double PartialProfileRatio = Summary->getPartialProfileRatio();
130 uint64_t ScaledHotEntryNumCounts =
131 static_cast<uint64_t>(HotEntry.NumCounts * PartialProfileRatio *
133 HasHugeWorkingSetSize =
134 ScaledHotEntryNumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
135 HasLargeWorkingSetSize =
136 ScaledHotEntryNumCounts > ProfileSummaryLargeWorkingSetSizeThreshold;
137 }
141ProfileSummaryInfo::computeThreshold(int PercentileCutoff) const {
142 if (!hasProfileSummary())
143 return std::nullopt;
144 auto iter = ThresholdCache.find(PercentileCutoff);
145 if (iter != ThresholdCache.end()) {
146 return iter->second;
147 }
148 auto &DetailedSummary = Summary->getDetailedSummary();
149 auto &Entry = ProfileSummaryBuilder::getEntryForPercentile(DetailedSummary,
151 uint64_t CountThreshold = Entry.MinCount;
152 ThresholdCache[PercentileCutoff] = CountThreshold;
153 return CountThreshold;
157 return HasHugeWorkingSetSize && *HasHugeWorkingSetSize;
161 return HasLargeWorkingSetSize && *HasLargeWorkingSetSize;
165 return HotCountThreshold && C >= *HotCountThreshold;
169 return ColdCountThreshold && C <= *ColdCountThreshold;
172template <bool isHot>
173bool ProfileSummaryInfo::isHotOrColdCountNthPercentile(int PercentileCutoff,
174 uint64_t C) const {
175 auto CountThreshold = computeThreshold(PercentileCutoff);
176 if (isHot)
177 return CountThreshold && C >= *CountThreshold;
178 else
179 return CountThreshold && C <= *CountThreshold;
183 uint64_t C) const {
184 return isHotOrColdCountNthPercentile<true>(PercentileCutoff, C);
188 uint64_t C) const {
189 return isHotOrColdCountNthPercentile<false>(PercentileCutoff, C);
193 return HotCountThreshold.value_or(UINT64_MAX);
197 return ColdCountThreshold.value_or(0);
201 BlockFrequencyInfo *BFI) const {
202 auto C = getProfileCount(CB, BFI);
203 return C && isHotCount(*C);
207 BlockFrequencyInfo *BFI) const {
208 auto C = getProfileCount(CB, BFI);
209 if (C)
210 return isColdCount(*C);
212 // In SamplePGO, if the caller has been sampled, and there is no profile
213 // annotated on the callsite, we consider the callsite as cold.
214 return hasSampleProfile() && CB.getCaller()->hasProfileData();
218 return hasProfileSummary() &&
219 Summary->getKind() == ProfileSummary::PSK_Sample &&
220 (PartialProfile || Summary->isPartialProfile());
224 "Profile summary info", false, true)
227 : ImmutablePass(ID) {
232 PSI.reset(new ProfileSummaryInfo(M));
233 return false;
237 PSI.reset();
238 return false;
241AnalysisKey ProfileSummaryAnalysis::Key;
244 return ProfileSummaryInfo(M);
251 OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
252 for (auto &F : M) {
253 OS << F.getName();
254 if (PSI.isFunctionEntryHot(&F))
255 OS << " :hot entry ";
256 else if (PSI.isFunctionEntryCold(&F))
257 OS << " :cold entry ";
258 OS << "\n";
259 }
260 return PreservedAnalyses::all();
static cl::opt< unsigned > CountThreshold("hexagon-cext-threshold", cl::init(3), cl::Hidden, cl::desc("Minimum number of extenders to trigger replacement"))
#define F(x, y, z)
Definition: MD5.cpp:55
static cl::opt< unsigned > PercentileCutoff("mfs-psi-cutoff", cl::desc("Percentile profile summary cutoff used to " "determine cold blocks. Unused if set to zero."), cl::init(999950), cl::Hidden)
cl::opt< bool > ScalePartialSampleProfileWorkingSetSize
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
static cl::opt< bool > PartialProfile("partial-profile", cl::Hidden, cl::init(false), cl::desc("Specify the current profile is used as a partial profile."))
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."))
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."))
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:649
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:803
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1227
Function * getCaller()
Helper to get the caller (the parent function).
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
iterator end()
Definition: DenseMap.h:84
bool hasProfileData(bool IncludeSynthetic=false) const
Return true if the function is annotated with profile data.
Definition: Function.h:314
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:282
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Metadata * getProfileSummary(bool IsCS) const
Returns profile summary metadata.
Definition: Module.cpp:646
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:172
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:178
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
Result run(Module &M, ModuleAnalysisManager &)
static const ProfileSummaryEntry & getEntryForPercentile(const SummaryEntryVector &DS, uint64_t Percentile)
Find the summary entry for a desired percentile of counts.
static uint64_t getHotCountThreshold(const SummaryEntryVector &DS)
static uint64_t getColdCountThreshold(const SummaryEntryVector &DS)
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Analysis providing profile information.
uint64_t getOrCompColdCountThreshold() const
Returns ColdCountThreshold if set.
bool hasProfileSummary() const
Returns true if profile summary is available.
bool isFunctionHotnessUnknown(const Function &F) const
Returns true if the hotness of F is unknown.
void refresh()
If no summary is present, attempt to refresh.
std::optional< uint64_t > getProfileCount(const CallBase &CallInst, BlockFrequencyInfo *BFI, bool AllowSynthetic=false) const
Returns the profile count for CallInst.
bool hasSampleProfile() const
Returns true if module M has sample profile.
bool isFunctionEntryHot(const FuncT *F) const
Returns true if F has hot function entry.
bool isColdCount(uint64_t C) const
Returns true if count C is considered cold.
bool isColdCountNthPercentile(int PercentileCutoff, uint64_t C) const
Returns true if count C is considered cold with regard to a given cold percentile cutoff value.
bool isHotCountNthPercentile(int PercentileCutoff, uint64_t C) const
Returns true if count C is considered hot with regard to a given hot percentile cutoff value.
bool hasPartialSampleProfile() const
Returns true if module M has partial-profile sample profile.
bool hasLargeWorkingSetSize() const
Returns true if the working set size of the code is considered large.
bool isColdCallSite(const CallBase &CB, BlockFrequencyInfo *BFI) const
Returns true if call site CB is considered cold.
bool isHotCallSite(const CallBase &CB, BlockFrequencyInfo *BFI) const
Returns true if the call site CB is considered hot.
bool isHotCount(uint64_t C) const
Returns true if count C is considered hot.
bool hasHugeWorkingSetSize() const
Returns true if the working set size of the code is considered huge.
uint64_t getOrCompHotCountThreshold() const
Returns HotCountThreshold if set.
bool isFunctionEntryCold(const Function *F) const
Returns true if F has cold function entry.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
static ProfileSummary * getFromMD(Metadata *MD)
Construct profile summary from metdata.
#define UINT64_MAX
Definition: DataTypes.h:77
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
cl::opt< unsigned > ProfileSummaryLargeWorkingSetSizeThreshold
void initializeProfileSummaryInfoWrapperPassPass(PassRegistry &)
cl::opt< int > ProfileSummaryCutoffHot
cl::opt< unsigned > ProfileSummaryHugeWorkingSetSizeThreshold
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:89