blob: 1d70c75f2e1c9d532edf6531589e155721874fe1 [file] [log] [blame]
Easwaran Raman7ef349f2016-06-03 22:54:26 +00001//===- ProfileSummaryInfo.cpp - Global profile summary information --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a pass that provides access to the global profile summary
11// information.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/ProfileSummaryInfo.h"
Easwaran Ramana2090aa2017-01-13 01:34:00 +000016#include "llvm/Analysis/BlockFrequencyInfo.h"
Dehao Chen35ce2db2016-11-09 23:36:02 +000017#include "llvm/IR/BasicBlock.h"
Easwaran Ramana2090aa2017-01-13 01:34:00 +000018#include "llvm/IR/CallSite.h"
Easwaran Raman7ef349f2016-06-03 22:54:26 +000019#include "llvm/IR/Metadata.h"
20#include "llvm/IR/Module.h"
21#include "llvm/IR/ProfileSummary.h"
22using namespace llvm;
23
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
30// considered cold).
31
32static cl::opt<int> ProfileSummaryCutoffHot(
Dehao Chen16768ce2017-08-04 16:20:54 +000033 "profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore,
Easwaran Raman7ef349f2016-06-03 22:54:26 +000034 cl::desc("A count is hot if it exceeds the minimum count to"
35 " reach this percentile of total counts."));
36
37static 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."));
41
Teresa Johnsondad922a2017-08-03 23:42:58 +000042static cl::opt<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold(
43 "profile-summary-huge-working-set-size-threshold", cl::Hidden,
44 cl::init(15000), cl::ZeroOrMore,
45 cl::desc("The code working set size is considered huge if the number of"
46 " blocks required to reach the -profile-summary-cutoff-hot"
47 " percentile exceeds this count."));
Dehao Chenb65c3a92017-08-03 17:11:41 +000048
Easwaran Raman6ed7d332018-11-02 17:39:31 +000049// The next two options override the counts derived from summary computation and
50// are useful for debugging purposes.
51static cl::opt<int> ProfileSummaryHotCount(
52 "profile-summary-hot-count", cl::ReallyHidden, cl::ZeroOrMore,
53 cl::desc("A fixed hot count that overrides the count derived from"
54 " profile-summary-cutoff-hot"));
55
56static cl::opt<int> ProfileSummaryColdCount(
57 "profile-summary-cold-count", cl::ReallyHidden, cl::ZeroOrMore,
58 cl::desc("A fixed cold count that overrides the count derived from"
59 " profile-summary-cutoff-cold"));
60
Teresa Johnsondad922a2017-08-03 23:42:58 +000061// Find the summary entry for a desired percentile of counts.
62static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS,
63 uint64_t Percentile) {
Easwaran Raman7ef349f2016-06-03 22:54:26 +000064 auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) {
65 return Entry.Cutoff < Percentile;
66 };
67 auto It = std::lower_bound(DS.begin(), DS.end(), Percentile, Compare);
68 // The required percentile has to be <= one of the percentiles in the
69 // detailed summary.
70 if (It == DS.end())
71 report_fatal_error("Desired percentile exceeds the maximum cutoff");
Teresa Johnsondad922a2017-08-03 23:42:58 +000072 return *It;
Easwaran Raman7ef349f2016-06-03 22:54:26 +000073}
74
75// The profile summary metadata may be attached either by the frontend or by
76// any backend passes (IR level instrumentation, for example). This method
77// checks if the Summary is null and if so checks if the summary metadata is now
Easwaran Raman65d7a8c2017-01-14 00:32:37 +000078// available in the module and parses it to get the Summary object. Returns true
79// if a valid Summary is available.
80bool ProfileSummaryInfo::computeSummary() {
Easwaran Raman7ef349f2016-06-03 22:54:26 +000081 if (Summary)
Easwaran Raman65d7a8c2017-01-14 00:32:37 +000082 return true;
Dehao Chenb393d822016-09-28 21:00:58 +000083 auto *SummaryMD = M.getProfileSummary();
Easwaran Raman7ef349f2016-06-03 22:54:26 +000084 if (!SummaryMD)
Easwaran Raman65d7a8c2017-01-14 00:32:37 +000085 return false;
Easwaran Raman7ef349f2016-06-03 22:54:26 +000086 Summary.reset(ProfileSummary::getFromMD(SummaryMD));
Easwaran Raman65d7a8c2017-01-14 00:32:37 +000087 return true;
Easwaran Raman7ef349f2016-06-03 22:54:26 +000088}
89
Dehao Chena328a462017-03-10 19:45:16 +000090Optional<uint64_t>
91ProfileSummaryInfo::getProfileCount(const Instruction *Inst,
92 BlockFrequencyInfo *BFI) {
93 if (!Inst)
94 return None;
95 assert((isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) &&
96 "We can only get profile count for call/invoke instruction.");
Easwaran Ramanb1649f12017-05-16 20:14:39 +000097 if (hasSampleProfile()) {
Teresa Johnson7b299662017-05-11 23:18:05 +000098 // In sample PGO mode, check if there is a profile metadata on the
99 // instruction. If it is present, determine hotness solely based on that,
Dehao Chenb65c3a92017-08-03 17:11:41 +0000100 // since the sampled entry count may not be accurate. If there is no
101 // annotated on the instruction, return None.
Teresa Johnson7b299662017-05-11 23:18:05 +0000102 uint64_t TotalCount;
103 if (Inst->extractProfTotalWeight(TotalCount))
104 return TotalCount;
Dehao Chenb65c3a92017-08-03 17:11:41 +0000105 return None;
Teresa Johnson7b299662017-05-11 23:18:05 +0000106 }
Dehao Chena328a462017-03-10 19:45:16 +0000107 if (BFI)
108 return BFI->getBlockProfileCount(Inst->getParent());
109 return None;
110}
111
Dehao Chend9a6f652016-10-10 21:47:28 +0000112/// Returns true if the function's entry is hot. If it returns false, it
113/// either means it is not hot or it is unknown whether it is hot or not (for
Piotr Padlewski8948c792016-09-30 21:05:49 +0000114/// example, no profile data is available).
Dehao Chend9a6f652016-10-10 21:47:28 +0000115bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
Easwaran Raman65d7a8c2017-01-14 00:32:37 +0000116 if (!F || !computeSummary())
Easwaran Raman7ef349f2016-06-03 22:54:26 +0000117 return false;
118 auto FunctionCount = F->getEntryCount();
119 // FIXME: The heuristic used below for determining hotness is based on
120 // preliminary SPEC tuning for inliner. This will eventually be a
121 // convenience method that calls isHotCount.
Easwaran Ramanfd335152018-01-17 22:24:23 +0000122 return FunctionCount && isHotCount(FunctionCount.getCount());
Easwaran Raman7ef349f2016-06-03 22:54:26 +0000123}
124
Teresa Johnson9274a9b2017-12-20 17:53:10 +0000125/// Returns true if the function contains hot code. This can include a hot
126/// function entry count, hot basic block, or (in the case of Sample PGO)
127/// hot total call edge count.
Dehao Chen261eb1f2017-03-23 23:14:11 +0000128/// If it returns false, it either means it is not hot or it is unknown
Teresa Johnson9274a9b2017-12-20 17:53:10 +0000129/// (for example, no profile data is available).
130bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F,
131 BlockFrequencyInfo &BFI) {
Dehao Chen261eb1f2017-03-23 23:14:11 +0000132 if (!F || !computeSummary())
133 return false;
134 if (auto FunctionCount = F->getEntryCount())
Easwaran Ramanfd335152018-01-17 22:24:23 +0000135 if (isHotCount(FunctionCount.getCount()))
Dehao Chen261eb1f2017-03-23 23:14:11 +0000136 return true;
137
Teresa Johnson9274a9b2017-12-20 17:53:10 +0000138 if (hasSampleProfile()) {
139 uint64_t TotalCallCount = 0;
140 for (const auto &BB : *F)
141 for (const auto &I : BB)
142 if (isa<CallInst>(I) || isa<InvokeInst>(I))
143 if (auto CallCount = getProfileCount(&I, nullptr))
144 TotalCallCount += CallCount.getValue();
145 if (isHotCount(TotalCallCount))
146 return true;
147 }
Dehao Chen261eb1f2017-03-23 23:14:11 +0000148 for (const auto &BB : *F)
Vedant Kumar85aede42018-11-19 05:23:16 +0000149 if (isHotBlock(&BB, &BFI))
Teresa Johnson9274a9b2017-12-20 17:53:10 +0000150 return true;
151 return false;
Dehao Chen261eb1f2017-03-23 23:14:11 +0000152}
153
Teresa Johnson9274a9b2017-12-20 17:53:10 +0000154/// Returns true if the function only contains cold code. This means that
155/// the function entry and blocks are all cold, and (in the case of Sample PGO)
156/// the total call edge count is cold.
Dehao Chen261eb1f2017-03-23 23:14:11 +0000157/// If it returns false, it either means it is not cold or it is unknown
Teresa Johnson9274a9b2017-12-20 17:53:10 +0000158/// (for example, no profile data is available).
159bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F,
160 BlockFrequencyInfo &BFI) {
Dehao Chen261eb1f2017-03-23 23:14:11 +0000161 if (!F || !computeSummary())
162 return false;
163 if (auto FunctionCount = F->getEntryCount())
Easwaran Ramanfd335152018-01-17 22:24:23 +0000164 if (!isColdCount(FunctionCount.getCount()))
Dehao Chen261eb1f2017-03-23 23:14:11 +0000165 return false;
Teresa Johnson9274a9b2017-12-20 17:53:10 +0000166
167 if (hasSampleProfile()) {
168 uint64_t TotalCallCount = 0;
169 for (const auto &BB : *F)
170 for (const auto &I : BB)
171 if (isa<CallInst>(I) || isa<InvokeInst>(I))
172 if (auto CallCount = getProfileCount(&I, nullptr))
173 TotalCallCount += CallCount.getValue();
174 if (!isColdCount(TotalCallCount))
175 return false;
176 }
Dehao Chen261eb1f2017-03-23 23:14:11 +0000177 for (const auto &BB : *F)
Vedant Kumar85aede42018-11-19 05:23:16 +0000178 if (!isColdBlock(&BB, &BFI))
Teresa Johnson9274a9b2017-12-20 17:53:10 +0000179 return false;
180 return true;
Dehao Chen261eb1f2017-03-23 23:14:11 +0000181}
182
Dehao Chend9a6f652016-10-10 21:47:28 +0000183/// Returns true if the function's entry is a cold. If it returns false, it
184/// either means it is not cold or it is unknown whether it is cold or not (for
Piotr Padlewski8948c792016-09-30 21:05:49 +0000185/// example, no profile data is available).
Dehao Chend9a6f652016-10-10 21:47:28 +0000186bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
Richard Trieufc34cc42016-06-10 01:42:05 +0000187 if (!F)
188 return false;
Davide Italiano9763d852017-03-10 20:50:51 +0000189 if (F->hasFnAttribute(Attribute::Cold))
Easwaran Raman7ef349f2016-06-03 22:54:26 +0000190 return true;
Easwaran Raman65d7a8c2017-01-14 00:32:37 +0000191 if (!computeSummary())
Richard Trieufc34cc42016-06-10 01:42:05 +0000192 return false;
Easwaran Raman7ef349f2016-06-03 22:54:26 +0000193 auto FunctionCount = F->getEntryCount();
194 // FIXME: The heuristic used below for determining coldness is based on
195 // preliminary SPEC tuning for inliner. This will eventually be a
196 // convenience method that calls isHotCount.
Easwaran Ramanfd335152018-01-17 22:24:23 +0000197 return FunctionCount && isColdCount(FunctionCount.getCount());
Easwaran Raman7ef349f2016-06-03 22:54:26 +0000198}
199
Piotr Padlewski8948c792016-09-30 21:05:49 +0000200/// Compute the hot and cold thresholds.
Easwaran Raman7ef349f2016-06-03 22:54:26 +0000201void ProfileSummaryInfo::computeThresholds() {
Easwaran Raman65d7a8c2017-01-14 00:32:37 +0000202 if (!computeSummary())
Easwaran Raman7ef349f2016-06-03 22:54:26 +0000203 return;
204 auto &DetailedSummary = Summary->getDetailedSummary();
Teresa Johnsondad922a2017-08-03 23:42:58 +0000205 auto &HotEntry =
206 getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
207 HotCountThreshold = HotEntry.MinCount;
Easwaran Raman6ed7d332018-11-02 17:39:31 +0000208 if (ProfileSummaryHotCount.getNumOccurrences() > 0)
209 HotCountThreshold = ProfileSummaryHotCount;
Teresa Johnsondad922a2017-08-03 23:42:58 +0000210 auto &ColdEntry =
211 getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
212 ColdCountThreshold = ColdEntry.MinCount;
Easwaran Raman6ed7d332018-11-02 17:39:31 +0000213 if (ProfileSummaryColdCount.getNumOccurrences() > 0)
214 ColdCountThreshold = ProfileSummaryColdCount;
215 assert(ColdCountThreshold <= HotCountThreshold &&
216 "Cold count threshold cannot exceed hot count threshold!");
Teresa Johnsondad922a2017-08-03 23:42:58 +0000217 HasHugeWorkingSetSize =
218 HotEntry.NumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
219}
220
221bool ProfileSummaryInfo::hasHugeWorkingSetSize() {
222 if (!HasHugeWorkingSetSize)
223 computeThresholds();
224 return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue();
Easwaran Raman7ef349f2016-06-03 22:54:26 +0000225}
226
227bool ProfileSummaryInfo::isHotCount(uint64_t C) {
228 if (!HotCountThreshold)
229 computeThresholds();
230 return HotCountThreshold && C >= HotCountThreshold.getValue();
231}
232
233bool ProfileSummaryInfo::isColdCount(uint64_t C) {
234 if (!ColdCountThreshold)
235 computeThresholds();
236 return ColdCountThreshold && C <= ColdCountThreshold.getValue();
237}
238
Wei Mif2885f72018-05-10 23:02:27 +0000239uint64_t ProfileSummaryInfo::getOrCompHotCountThreshold() {
240 if (!HotCountThreshold)
241 computeThresholds();
Wei Mi9ff4d232018-08-07 18:13:10 +0000242 return HotCountThreshold ? HotCountThreshold.getValue() : UINT64_MAX;
Wei Mif2885f72018-05-10 23:02:27 +0000243}
244
245uint64_t ProfileSummaryInfo::getOrCompColdCountThreshold() {
246 if (!ColdCountThreshold)
247 computeThresholds();
Wei Mi9ff4d232018-08-07 18:13:10 +0000248 return ColdCountThreshold ? ColdCountThreshold.getValue() : 0;
Wei Mif2885f72018-05-10 23:02:27 +0000249}
250
Vedant Kumar85aede42018-11-19 05:23:16 +0000251bool ProfileSummaryInfo::isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI) {
252 auto Count = BFI->getBlockProfileCount(BB);
Dehao Chene7c12602017-03-10 01:44:37 +0000253 return Count && isHotCount(*Count);
Easwaran Ramana2090aa2017-01-13 01:34:00 +0000254}
255
Vedant Kumar85aede42018-11-19 05:23:16 +0000256bool ProfileSummaryInfo::isColdBlock(const BasicBlock *BB,
Easwaran Ramana2090aa2017-01-13 01:34:00 +0000257 BlockFrequencyInfo *BFI) {
Vedant Kumar85aede42018-11-19 05:23:16 +0000258 auto Count = BFI->getBlockProfileCount(BB);
Wei Miacfb0462018-12-13 21:51:42 +0000259 return Count && isColdCount(*Count);
Easwaran Ramana2090aa2017-01-13 01:34:00 +0000260}
261
Easwaran Ramana2090aa2017-01-13 01:34:00 +0000262bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS,
263 BlockFrequencyInfo *BFI) {
Dehao Chena328a462017-03-10 19:45:16 +0000264 auto C = getProfileCount(CS.getInstruction(), BFI);
265 return C && isHotCount(*C);
Easwaran Ramana2090aa2017-01-13 01:34:00 +0000266}
267
268bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS,
269 BlockFrequencyInfo *BFI) {
Dehao Chena328a462017-03-10 19:45:16 +0000270 auto C = getProfileCount(CS.getInstruction(), BFI);
Dehao Chenb65c3a92017-08-03 17:11:41 +0000271 if (C)
272 return isColdCount(*C);
273
274 // In SamplePGO, if the caller has been sampled, and there is no profile
George Burgess IVe2da0552018-04-12 18:36:01 +0000275 // annotated on the callsite, we consider the callsite as cold.
Wei Miacfb0462018-12-13 21:51:42 +0000276 return hasSampleProfile() && CS.getCaller()->hasProfileData();
Dehao Chen35ce2db2016-11-09 23:36:02 +0000277}
278
Easwaran Raman7ef349f2016-06-03 22:54:26 +0000279INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
280 "Profile summary info", false, true)
281
282ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
283 : ImmutablePass(ID) {
284 initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
285}
286
Dehao Chenb393d822016-09-28 21:00:58 +0000287bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
288 PSI.reset(new ProfileSummaryInfo(M));
289 return false;
290}
291
292bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
293 PSI.reset();
294 return false;
295}
296
Chandler Carruth33d56812016-11-23 17:53:26 +0000297AnalysisKey ProfileSummaryAnalysis::Key;
Chandler Carruth04d0fe92016-06-17 00:11:01 +0000298ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
299 ModuleAnalysisManager &) {
Dehao Chenb393d822016-09-28 21:00:58 +0000300 return ProfileSummaryInfo(M);
Easwaran Raman7ef349f2016-06-03 22:54:26 +0000301}
302
Easwaran Raman7ef349f2016-06-03 22:54:26 +0000303PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
Sean Silva2fb9a982016-08-09 00:28:38 +0000304 ModuleAnalysisManager &AM) {
Easwaran Raman7ef349f2016-06-03 22:54:26 +0000305 ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
306
307 OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
308 for (auto &F : M) {
309 OS << F.getName();
Dehao Chend9a6f652016-10-10 21:47:28 +0000310 if (PSI.isFunctionEntryHot(&F))
311 OS << " :hot entry ";
312 else if (PSI.isFunctionEntryCold(&F))
313 OS << " :cold entry ";
Easwaran Raman7ef349f2016-06-03 22:54:26 +0000314 OS << "\n";
315 }
316 return PreservedAnalyses::all();
317}
318
319char ProfileSummaryInfoWrapperPass::ID = 0;