blob: aeaa5172b3e6bf848fc0b38eca093358a616e66c [file] [log] [blame]
Easwaran Raman019e0bf2016-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 Ramanb035f912017-01-13 01:34:00 +000016#include "llvm/Analysis/BlockFrequencyInfo.h"
Dehao Chen38a666d2016-11-09 23:36:02 +000017#include "llvm/IR/BasicBlock.h"
Easwaran Ramanb035f912017-01-13 01:34:00 +000018#include "llvm/IR/CallSite.h"
Easwaran Raman019e0bf2016-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 Chen63799512017-08-04 16:20:54 +000033 "profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore,
Easwaran Raman019e0bf2016-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
Dehao Chenf0e27e62017-08-24 21:37:04 +000042static cl::opt<bool> ProfileSampleAccurate(
43 "profile-sample-accurate", cl::Hidden, cl::init(false),
Dehao Chenf58df392017-08-03 17:11:41 +000044 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."));
Teresa Johnson8482e562017-08-03 23:42:58 +000047static 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."));
Dehao Chenf58df392017-08-03 17:11:41 +000053
Teresa Johnson8482e562017-08-03 23:42:58 +000054// Find the summary entry for a desired percentile of counts.
55static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS,
56 uint64_t Percentile) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +000057 auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) {
58 return Entry.Cutoff < Percentile;
59 };
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
62 // detailed summary.
63 if (It == DS.end())
64 report_fatal_error("Desired percentile exceeds the maximum cutoff");
Teresa Johnson8482e562017-08-03 23:42:58 +000065 return *It;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000066}
67
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
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000071// available in the module and parses it to get the Summary object. Returns true
72// if a valid Summary is available.
73bool ProfileSummaryInfo::computeSummary() {
Easwaran Raman019e0bf2016-06-03 22:54:26 +000074 if (Summary)
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000075 return true;
Dehao Chen5461d8b2016-09-28 21:00:58 +000076 auto *SummaryMD = M.getProfileSummary();
Easwaran Raman019e0bf2016-06-03 22:54:26 +000077 if (!SummaryMD)
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000078 return false;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000079 Summary.reset(ProfileSummary::getFromMD(SummaryMD));
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +000080 return true;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000081}
82
Dehao Chenc2048152017-03-10 19:45:16 +000083Optional<uint64_t>
84ProfileSummaryInfo::getProfileCount(const Instruction *Inst,
85 BlockFrequencyInfo *BFI) {
86 if (!Inst)
87 return None;
88 assert((isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) &&
89 "We can only get profile count for call/invoke instruction.");
Easwaran Ramandadc0f12017-05-16 20:14:39 +000090 if (hasSampleProfile()) {
Teresa Johnson2a6b7992017-05-11 23:18:05 +000091 // 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,
Dehao Chenf58df392017-08-03 17:11:41 +000093 // since the sampled entry count may not be accurate. If there is no
94 // annotated on the instruction, return None.
Teresa Johnson2a6b7992017-05-11 23:18:05 +000095 uint64_t TotalCount;
96 if (Inst->extractProfTotalWeight(TotalCount))
97 return TotalCount;
Dehao Chenf58df392017-08-03 17:11:41 +000098 return None;
Teresa Johnson2a6b7992017-05-11 23:18:05 +000099 }
Dehao Chenc2048152017-03-10 19:45:16 +0000100 if (BFI)
101 return BFI->getBlockProfileCount(Inst->getParent());
102 return None;
103}
104
Dehao Chen84287ab2016-10-10 21:47:28 +0000105/// 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
Piotr Padlewskif3d122c2016-09-30 21:05:49 +0000107/// example, no profile data is available).
Dehao Chen84287ab2016-10-10 21:47:28 +0000108bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000109 if (!F || !computeSummary())
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000110 return false;
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.
Easwaran Ramane5b8de22018-01-17 22:24:23 +0000115 return FunctionCount && isHotCount(FunctionCount.getCount());
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000116}
117
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000118/// 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.
Dehao Chen775341a2017-03-23 23:14:11 +0000121/// If it returns false, it either means it is not hot or it is unknown
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000122/// (for example, no profile data is available).
123bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F,
124 BlockFrequencyInfo &BFI) {
Dehao Chen775341a2017-03-23 23:14:11 +0000125 if (!F || !computeSummary())
126 return false;
127 if (auto FunctionCount = F->getEntryCount())
Easwaran Ramane5b8de22018-01-17 22:24:23 +0000128 if (isHotCount(FunctionCount.getCount()))
Dehao Chen775341a2017-03-23 23:14:11 +0000129 return true;
130
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000131 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))
139 return true;
140 }
Dehao Chen775341a2017-03-23 23:14:11 +0000141 for (const auto &BB : *F)
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000142 if (isHotBB(&BB, &BFI))
143 return true;
144 return false;
Dehao Chen775341a2017-03-23 23:14:11 +0000145}
146
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000147/// 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.
Dehao Chen775341a2017-03-23 23:14:11 +0000150/// If it returns false, it either means it is not cold or it is unknown
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000151/// (for example, no profile data is available).
152bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F,
153 BlockFrequencyInfo &BFI) {
Dehao Chen775341a2017-03-23 23:14:11 +0000154 if (!F || !computeSummary())
155 return false;
156 if (auto FunctionCount = F->getEntryCount())
Easwaran Ramane5b8de22018-01-17 22:24:23 +0000157 if (!isColdCount(FunctionCount.getCount()))
Dehao Chen775341a2017-03-23 23:14:11 +0000158 return false;
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000159
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))
168 return false;
169 }
Dehao Chen775341a2017-03-23 23:14:11 +0000170 for (const auto &BB : *F)
Teresa Johnsona4ce3bf2017-12-20 17:53:10 +0000171 if (!isColdBB(&BB, &BFI))
172 return false;
173 return true;
Dehao Chen775341a2017-03-23 23:14:11 +0000174}
175
Dehao Chen84287ab2016-10-10 21:47:28 +0000176/// 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
Piotr Padlewskif3d122c2016-09-30 21:05:49 +0000178/// example, no profile data is available).
Dehao Chen84287ab2016-10-10 21:47:28 +0000179bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
Richard Trieufae70b62016-06-10 01:42:05 +0000180 if (!F)
181 return false;
Davide Italiano574e5972017-03-10 20:50:51 +0000182 if (F->hasFnAttribute(Attribute::Cold))
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000183 return true;
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000184 if (!computeSummary())
Richard Trieufae70b62016-06-10 01:42:05 +0000185 return false;
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000186 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.
Easwaran Ramane5b8de22018-01-17 22:24:23 +0000190 return FunctionCount && isColdCount(FunctionCount.getCount());
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000191}
192
Piotr Padlewskif3d122c2016-09-30 21:05:49 +0000193/// Compute the hot and cold thresholds.
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000194void ProfileSummaryInfo::computeThresholds() {
Easwaran Ramana7bdb8a2017-01-14 00:32:37 +0000195 if (!computeSummary())
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000196 return;
197 auto &DetailedSummary = Summary->getDetailedSummary();
Teresa Johnson8482e562017-08-03 23:42:58 +0000198 auto &HotEntry =
199 getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
200 HotCountThreshold = HotEntry.MinCount;
201 auto &ColdEntry =
202 getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
203 ColdCountThreshold = ColdEntry.MinCount;
204 HasHugeWorkingSetSize =
205 HotEntry.NumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
206}
207
208bool ProfileSummaryInfo::hasHugeWorkingSetSize() {
209 if (!HasHugeWorkingSetSize)
210 computeThresholds();
211 return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue();
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000212}
213
214bool ProfileSummaryInfo::isHotCount(uint64_t C) {
215 if (!HotCountThreshold)
216 computeThresholds();
217 return HotCountThreshold && C >= HotCountThreshold.getValue();
218}
219
220bool ProfileSummaryInfo::isColdCount(uint64_t C) {
221 if (!ColdCountThreshold)
222 computeThresholds();
223 return ColdCountThreshold && C <= ColdCountThreshold.getValue();
224}
225
Wei Mi0c2f6be2018-05-10 23:02:27 +0000226uint64_t ProfileSummaryInfo::getOrCompHotCountThreshold() {
227 if (!HotCountThreshold)
228 computeThresholds();
Wei Mib1ef2cc2018-08-07 18:13:10 +0000229 return HotCountThreshold ? HotCountThreshold.getValue() : UINT64_MAX;
Wei Mi0c2f6be2018-05-10 23:02:27 +0000230}
231
232uint64_t ProfileSummaryInfo::getOrCompColdCountThreshold() {
233 if (!ColdCountThreshold)
234 computeThresholds();
Wei Mib1ef2cc2018-08-07 18:13:10 +0000235 return ColdCountThreshold ? ColdCountThreshold.getValue() : 0;
Wei Mi0c2f6be2018-05-10 23:02:27 +0000236}
237
Dehao Chen38a666d2016-11-09 23:36:02 +0000238bool ProfileSummaryInfo::isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI) {
239 auto Count = BFI->getBlockProfileCount(B);
Dehao Chen22645ee2017-03-10 01:44:37 +0000240 return Count && isHotCount(*Count);
Easwaran Ramanb035f912017-01-13 01:34:00 +0000241}
242
243bool ProfileSummaryInfo::isColdBB(const BasicBlock *B,
244 BlockFrequencyInfo *BFI) {
245 auto Count = BFI->getBlockProfileCount(B);
246 return Count && isColdCount(*Count);
247}
248
Easwaran Ramanb035f912017-01-13 01:34:00 +0000249bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS,
250 BlockFrequencyInfo *BFI) {
Dehao Chenc2048152017-03-10 19:45:16 +0000251 auto C = getProfileCount(CS.getInstruction(), BFI);
252 return C && isHotCount(*C);
Easwaran Ramanb035f912017-01-13 01:34:00 +0000253}
254
255bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS,
256 BlockFrequencyInfo *BFI) {
Dehao Chenc2048152017-03-10 19:45:16 +0000257 auto C = getProfileCount(CS.getInstruction(), BFI);
Dehao Chenf58df392017-08-03 17:11:41 +0000258 if (C)
259 return isColdCount(*C);
260
261 // In SamplePGO, if the caller has been sampled, and there is no profile
George Burgess IVceecd452018-04-12 18:36:01 +0000262 // annotated on the callsite, we consider the callsite as cold.
Dehao Chenf58df392017-08-03 17:11:41 +0000263 // 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() &&
Easwaran Ramana17f2202017-12-22 01:33:52 +0000266 (CS.getCaller()->hasProfileData() || ProfileSampleAccurate ||
Dehao Chenf0e27e62017-08-24 21:37:04 +0000267 CS.getCaller()->hasFnAttribute("profile-sample-accurate")));
Dehao Chen38a666d2016-11-09 23:36:02 +0000268}
269
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000270INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
271 "Profile summary info", false, true)
272
273ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
274 : ImmutablePass(ID) {
275 initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
276}
277
Dehao Chen5461d8b2016-09-28 21:00:58 +0000278bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
279 PSI.reset(new ProfileSummaryInfo(M));
280 return false;
281}
282
283bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
284 PSI.reset();
285 return false;
286}
287
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000288AnalysisKey ProfileSummaryAnalysis::Key;
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000289ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
290 ModuleAnalysisManager &) {
Dehao Chen5461d8b2016-09-28 21:00:58 +0000291 return ProfileSummaryInfo(M);
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000292}
293
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000294PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
Sean Silvafd03ac62016-08-09 00:28:38 +0000295 ModuleAnalysisManager &AM) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000296 ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
297
298 OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
299 for (auto &F : M) {
300 OS << F.getName();
Dehao Chen84287ab2016-10-10 21:47:28 +0000301 if (PSI.isFunctionEntryHot(&F))
302 OS << " :hot entry ";
303 else if (PSI.isFunctionEntryCold(&F))
304 OS << " :cold entry ";
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000305 OS << "\n";
306 }
307 return PreservedAnalyses::all();
308}
309
310char ProfileSummaryInfoWrapperPass::ID = 0;