1 //===- ProfileSummaryInfo.cpp - Global profile summary information --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a pass that provides access to the global profile summary
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/ProfileSummaryInfo.h"
16 #include "llvm/Analysis/BlockFrequencyInfo.h"
17 #include "llvm/IR/BasicBlock.h"
18 #include "llvm/IR/CallSite.h"
19 #include "llvm/IR/Metadata.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/ProfileSummary.h"
24 // The following two parameters determine the threshold for a count to be
25 // considered hot/cold. These two parameters are percentile values (multiplied
26 // by 10000). If the counts are sorted in descending order, the minimum count to
27 // reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
28 // Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
29 // threshold for determining cold count (everything <= this threshold is
32 static cl::opt
<int> ProfileSummaryCutoffHot(
33 "profile-summary-cutoff-hot", cl::Hidden
, cl::init(990000), cl::ZeroOrMore
,
34 cl::desc("A count is hot if it exceeds the minimum count to"
35 " reach this percentile of total counts."));
37 static cl::opt
<int> ProfileSummaryCutoffCold(
38 "profile-summary-cutoff-cold", cl::Hidden
, cl::init(999999), cl::ZeroOrMore
,
39 cl::desc("A count is cold if it is below the minimum count"
40 " to reach this percentile of total counts."));
42 static cl::opt
<bool> ProfileSampleAccurate(
43 "profile-sample-accurate", cl::Hidden
, cl::init(false),
44 cl::desc("If the sample profile is accurate, we will mark all un-sampled "
45 "callsite as cold. Otherwise, treat un-sampled callsites as if "
46 "we have no profile."));
47 static cl::opt
<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold(
48 "profile-summary-huge-working-set-size-threshold", cl::Hidden
,
49 cl::init(15000), cl::ZeroOrMore
,
50 cl::desc("The code working set size is considered huge if the number of"
51 " blocks required to reach the -profile-summary-cutoff-hot"
52 " percentile exceeds this count."));
54 // Find the summary entry for a desired percentile of counts.
55 static const ProfileSummaryEntry
&getEntryForPercentile(SummaryEntryVector
&DS
,
56 uint64_t Percentile
) {
57 auto Compare
= [](const ProfileSummaryEntry
&Entry
, uint64_t Percentile
) {
58 return Entry
.Cutoff
< Percentile
;
60 auto It
= std::lower_bound(DS
.begin(), DS
.end(), Percentile
, Compare
);
61 // The required percentile has to be <= one of the percentiles in the
64 report_fatal_error("Desired percentile exceeds the maximum cutoff");
68 // The profile summary metadata may be attached either by the frontend or by
69 // any backend passes (IR level instrumentation, for example). This method
70 // checks if the Summary is null and if so checks if the summary metadata is now
71 // available in the module and parses it to get the Summary object. Returns true
72 // if a valid Summary is available.
73 bool ProfileSummaryInfo::computeSummary() {
76 auto *SummaryMD
= M
.getProfileSummary();
79 Summary
.reset(ProfileSummary::getFromMD(SummaryMD
));
84 ProfileSummaryInfo::getProfileCount(const Instruction
*Inst
,
85 BlockFrequencyInfo
*BFI
) {
88 assert((isa
<CallInst
>(Inst
) || isa
<InvokeInst
>(Inst
)) &&
89 "We can only get profile count for call/invoke instruction.");
90 if (hasSampleProfile()) {
91 // In sample PGO mode, check if there is a profile metadata on the
92 // instruction. If it is present, determine hotness solely based on that,
93 // since the sampled entry count may not be accurate. If there is no
94 // annotated on the instruction, return None.
96 if (Inst
->extractProfTotalWeight(TotalCount
))
101 return BFI
->getBlockProfileCount(Inst
->getParent());
105 /// Returns true if the function's entry is hot. If it returns false, it
106 /// either means it is not hot or it is unknown whether it is hot or not (for
107 /// example, no profile data is available).
108 bool ProfileSummaryInfo::isFunctionEntryHot(const Function
*F
) {
109 if (!F
|| !computeSummary())
111 auto FunctionCount
= F
->getEntryCount();
112 // FIXME: The heuristic used below for determining hotness is based on
113 // preliminary SPEC tuning for inliner. This will eventually be a
114 // convenience method that calls isHotCount.
115 return FunctionCount
&& isHotCount(FunctionCount
.getCount());
118 /// Returns true if the function contains hot code. This can include a hot
119 /// function entry count, hot basic block, or (in the case of Sample PGO)
120 /// hot total call edge count.
121 /// If it returns false, it either means it is not hot or it is unknown
122 /// (for example, no profile data is available).
123 bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function
*F
,
124 BlockFrequencyInfo
&BFI
) {
125 if (!F
|| !computeSummary())
127 if (auto FunctionCount
= F
->getEntryCount())
128 if (isHotCount(FunctionCount
.getCount()))
131 if (hasSampleProfile()) {
132 uint64_t TotalCallCount
= 0;
133 for (const auto &BB
: *F
)
134 for (const auto &I
: BB
)
135 if (isa
<CallInst
>(I
) || isa
<InvokeInst
>(I
))
136 if (auto CallCount
= getProfileCount(&I
, nullptr))
137 TotalCallCount
+= CallCount
.getValue();
138 if (isHotCount(TotalCallCount
))
141 for (const auto &BB
: *F
)
142 if (isHotBB(&BB
, &BFI
))
147 /// Returns true if the function only contains cold code. This means that
148 /// the function entry and blocks are all cold, and (in the case of Sample PGO)
149 /// the total call edge count is cold.
150 /// If it returns false, it either means it is not cold or it is unknown
151 /// (for example, no profile data is available).
152 bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function
*F
,
153 BlockFrequencyInfo
&BFI
) {
154 if (!F
|| !computeSummary())
156 if (auto FunctionCount
= F
->getEntryCount())
157 if (!isColdCount(FunctionCount
.getCount()))
160 if (hasSampleProfile()) {
161 uint64_t TotalCallCount
= 0;
162 for (const auto &BB
: *F
)
163 for (const auto &I
: BB
)
164 if (isa
<CallInst
>(I
) || isa
<InvokeInst
>(I
))
165 if (auto CallCount
= getProfileCount(&I
, nullptr))
166 TotalCallCount
+= CallCount
.getValue();
167 if (!isColdCount(TotalCallCount
))
170 for (const auto &BB
: *F
)
171 if (!isColdBB(&BB
, &BFI
))
176 /// Returns true if the function's entry is a cold. If it returns false, it
177 /// either means it is not cold or it is unknown whether it is cold or not (for
178 /// example, no profile data is available).
179 bool ProfileSummaryInfo::isFunctionEntryCold(const Function
*F
) {
182 if (F
->hasFnAttribute(Attribute::Cold
))
184 if (!computeSummary())
186 auto FunctionCount
= F
->getEntryCount();
187 // FIXME: The heuristic used below for determining coldness is based on
188 // preliminary SPEC tuning for inliner. This will eventually be a
189 // convenience method that calls isHotCount.
190 return FunctionCount
&& isColdCount(FunctionCount
.getCount());
193 /// Compute the hot and cold thresholds.
194 void ProfileSummaryInfo::computeThresholds() {
195 if (!computeSummary())
197 auto &DetailedSummary
= Summary
->getDetailedSummary();
199 getEntryForPercentile(DetailedSummary
, ProfileSummaryCutoffHot
);
200 HotCountThreshold
= HotEntry
.MinCount
;
202 getEntryForPercentile(DetailedSummary
, ProfileSummaryCutoffCold
);
203 ColdCountThreshold
= ColdEntry
.MinCount
;
204 HasHugeWorkingSetSize
=
205 HotEntry
.NumCounts
> ProfileSummaryHugeWorkingSetSizeThreshold
;
208 bool ProfileSummaryInfo::hasHugeWorkingSetSize() {
209 if (!HasHugeWorkingSetSize
)
211 return HasHugeWorkingSetSize
&& HasHugeWorkingSetSize
.getValue();
214 bool ProfileSummaryInfo::isHotCount(uint64_t C
) {
215 if (!HotCountThreshold
)
217 return HotCountThreshold
&& C
>= HotCountThreshold
.getValue();
220 bool ProfileSummaryInfo::isColdCount(uint64_t C
) {
221 if (!ColdCountThreshold
)
223 return ColdCountThreshold
&& C
<= ColdCountThreshold
.getValue();
226 uint64_t ProfileSummaryInfo::getOrCompHotCountThreshold() {
227 if (!HotCountThreshold
)
229 return HotCountThreshold
? HotCountThreshold
.getValue() : UINT64_MAX
;
232 uint64_t ProfileSummaryInfo::getOrCompColdCountThreshold() {
233 if (!ColdCountThreshold
)
235 return ColdCountThreshold
? ColdCountThreshold
.getValue() : 0;
238 bool ProfileSummaryInfo::isHotBB(const BasicBlock
*B
, BlockFrequencyInfo
*BFI
) {
239 auto Count
= BFI
->getBlockProfileCount(B
);
240 return Count
&& isHotCount(*Count
);
243 bool ProfileSummaryInfo::isColdBB(const BasicBlock
*B
,
244 BlockFrequencyInfo
*BFI
) {
245 auto Count
= BFI
->getBlockProfileCount(B
);
246 return Count
&& isColdCount(*Count
);
249 bool ProfileSummaryInfo::isHotCallSite(const CallSite
&CS
,
250 BlockFrequencyInfo
*BFI
) {
251 auto C
= getProfileCount(CS
.getInstruction(), BFI
);
252 return C
&& isHotCount(*C
);
255 bool ProfileSummaryInfo::isColdCallSite(const CallSite
&CS
,
256 BlockFrequencyInfo
*BFI
) {
257 auto C
= getProfileCount(CS
.getInstruction(), BFI
);
259 return isColdCount(*C
);
261 // In SamplePGO, if the caller has been sampled, and there is no profile
262 // annotated on the callsite, we consider the callsite as cold.
263 // If there is no profile for the caller, and we know the profile is
264 // accurate, we consider the callsite as cold.
265 return (hasSampleProfile() &&
266 (CS
.getCaller()->hasProfileData() || ProfileSampleAccurate
||
267 CS
.getCaller()->hasFnAttribute("profile-sample-accurate")));
270 INITIALIZE_PASS(ProfileSummaryInfoWrapperPass
, "profile-summary-info",
271 "Profile summary info", false, true)
273 ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
274 : ImmutablePass(ID
) {
275 initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
278 bool ProfileSummaryInfoWrapperPass::doInitialization(Module
&M
) {
279 PSI
.reset(new ProfileSummaryInfo(M
));
283 bool ProfileSummaryInfoWrapperPass::doFinalization(Module
&M
) {
288 AnalysisKey
ProfileSummaryAnalysis::Key
;
289 ProfileSummaryInfo
ProfileSummaryAnalysis::run(Module
&M
,
290 ModuleAnalysisManager
&) {
291 return ProfileSummaryInfo(M
);
294 PreservedAnalyses
ProfileSummaryPrinterPass::run(Module
&M
,
295 ModuleAnalysisManager
&AM
) {
296 ProfileSummaryInfo
&PSI
= AM
.getResult
<ProfileSummaryAnalysis
>(M
);
298 OS
<< "Functions in " << M
.getName() << " with hot/cold annotations: \n";
301 if (PSI
.isFunctionEntryHot(&F
))
302 OS
<< " :hot entry ";
303 else if (PSI
.isFunctionEntryCold(&F
))
304 OS
<< " :cold entry ";
307 return PreservedAnalyses::all();
310 char ProfileSummaryInfoWrapperPass::ID
= 0;