blob: d36464e7d474529f8bd34168a04dfe9d99b102a9 [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(
33 "profile-summary-cutoff-hot", cl::Hidden, cl::init(999000), cl::ZeroOrMore,
34 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
42// Find the minimum count to reach a desired percentile of counts.
43static uint64_t getMinCountForPercentile(SummaryEntryVector &DS,
44 uint64_t Percentile) {
45 auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) {
46 return Entry.Cutoff < Percentile;
47 };
48 auto It = std::lower_bound(DS.begin(), DS.end(), Percentile, Compare);
49 // The required percentile has to be <= one of the percentiles in the
50 // detailed summary.
51 if (It == DS.end())
52 report_fatal_error("Desired percentile exceeds the maximum cutoff");
53 return It->MinCount;
54}
55
56// The profile summary metadata may be attached either by the frontend or by
57// any backend passes (IR level instrumentation, for example). This method
58// checks if the Summary is null and if so checks if the summary metadata is now
59// available in the module and parses it to get the Summary object.
60void ProfileSummaryInfo::computeSummary() {
61 if (Summary)
62 return;
Dehao Chen5461d8b2016-09-28 21:00:58 +000063 auto *SummaryMD = M.getProfileSummary();
Easwaran Raman019e0bf2016-06-03 22:54:26 +000064 if (!SummaryMD)
65 return;
66 Summary.reset(ProfileSummary::getFromMD(SummaryMD));
67}
68
Dehao Chen84287ab2016-10-10 21:47:28 +000069/// Returns true if the function's entry is hot. If it returns false, it
70/// either means it is not hot or it is unknown whether it is hot or not (for
Piotr Padlewskif3d122c2016-09-30 21:05:49 +000071/// example, no profile data is available).
Dehao Chen84287ab2016-10-10 21:47:28 +000072bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +000073 computeSummary();
74 if (!F || !Summary)
75 return false;
76 auto FunctionCount = F->getEntryCount();
77 // FIXME: The heuristic used below for determining hotness is based on
78 // preliminary SPEC tuning for inliner. This will eventually be a
79 // convenience method that calls isHotCount.
Dehao Chenc87bc792016-10-11 05:19:00 +000080 return FunctionCount && isHotCount(FunctionCount.getValue());
Easwaran Raman019e0bf2016-06-03 22:54:26 +000081}
82
Dehao Chen84287ab2016-10-10 21:47:28 +000083/// Returns true if the function's entry is a cold. If it returns false, it
84/// either means it is not cold or it is unknown whether it is cold or not (for
Piotr Padlewskif3d122c2016-09-30 21:05:49 +000085/// example, no profile data is available).
Dehao Chen84287ab2016-10-10 21:47:28 +000086bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +000087 computeSummary();
Richard Trieufae70b62016-06-10 01:42:05 +000088 if (!F)
89 return false;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000090 if (F->hasFnAttribute(Attribute::Cold)) {
91 return true;
92 }
Richard Trieufae70b62016-06-10 01:42:05 +000093 if (!Summary)
94 return false;
Easwaran Raman019e0bf2016-06-03 22:54:26 +000095 auto FunctionCount = F->getEntryCount();
96 // FIXME: The heuristic used below for determining coldness is based on
97 // preliminary SPEC tuning for inliner. This will eventually be a
98 // convenience method that calls isHotCount.
Dehao Chenc87bc792016-10-11 05:19:00 +000099 return FunctionCount && isColdCount(FunctionCount.getValue());
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000100}
101
Piotr Padlewskif3d122c2016-09-30 21:05:49 +0000102/// Compute the hot and cold thresholds.
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000103void ProfileSummaryInfo::computeThresholds() {
104 if (!Summary)
105 computeSummary();
106 if (!Summary)
107 return;
108 auto &DetailedSummary = Summary->getDetailedSummary();
109 HotCountThreshold =
110 getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
111 ColdCountThreshold =
112 getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
113}
114
115bool ProfileSummaryInfo::isHotCount(uint64_t C) {
116 if (!HotCountThreshold)
117 computeThresholds();
118 return HotCountThreshold && C >= HotCountThreshold.getValue();
119}
120
121bool ProfileSummaryInfo::isColdCount(uint64_t C) {
122 if (!ColdCountThreshold)
123 computeThresholds();
124 return ColdCountThreshold && C <= ColdCountThreshold.getValue();
125}
126
Dehao Chen38a666d2016-11-09 23:36:02 +0000127bool ProfileSummaryInfo::isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI) {
128 auto Count = BFI->getBlockProfileCount(B);
129 if (Count && isHotCount(*Count))
130 return true;
131 // Use extractProfTotalWeight to get BB count.
132 // For Sample PGO, BFI may not provide accurate BB count due to errors
133 // magnified during sample count propagation. This serves as a backup plan
134 // to ensure all hot BB will not be missed.
135 // The query currently has false positives as branch instruction cloning does
136 // not update/scale branch weights. Unlike false negatives, this will not cause
137 // performance problem.
138 uint64_t TotalCount;
Easwaran Ramanb035f912017-01-13 01:34:00 +0000139 auto *TI = B->getTerminator();
140 return extractProfTotalWeight(TI, TotalCount) && isHotCount(TotalCount);
141}
142
143bool ProfileSummaryInfo::isColdBB(const BasicBlock *B,
144 BlockFrequencyInfo *BFI) {
145 auto Count = BFI->getBlockProfileCount(B);
146 return Count && isColdCount(*Count);
147}
148
149bool ProfileSummaryInfo::extractProfTotalWeight(const Instruction *I,
150 uint64_t &TotalCount) {
151 // Use profile weight on metadata only for sample profiling where block counts
152 // could differ from the count of an instruction within the block.
153 if (Summary.get()->getKind() != ProfileSummary::PSK_Sample)
154 return false;
155
156 return (isa<CallInst>(I) ||
157 (isa<TerminatorInst>(I) && !isa<ReturnInst>(I))) &&
158 I->extractProfTotalWeight(TotalCount);
159}
160
161bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS,
162 BlockFrequencyInfo *BFI) {
163 auto *CallInst = CS.getInstruction();
164 if (!CS)
165 return false;
166 // Check if there is a profile metadata on the instruction. If it is present,
167 // determine hotness solely based on that.
168 uint64_t TotalCount;
169 if (extractProfTotalWeight(CallInst, TotalCount))
170 return isHotCount(TotalCount);
171 return BFI && isHotBB(CallInst->getParent(), BFI);
172}
173
174bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS,
175 BlockFrequencyInfo *BFI) {
176 auto *CallInst = CS.getInstruction();
177 if (!CS)
178 return false;
179 // Check if there is a profile metadata on the instruction. If it is present,
180 // and tells that the callsite is not cold, then return false;
181 uint64_t TotalCount;
182 if (extractProfTotalWeight(CallInst, TotalCount) && !isColdCount(TotalCount))
183 return false;
184 return BFI && isColdBB(CallInst->getParent(), BFI);
Dehao Chen38a666d2016-11-09 23:36:02 +0000185}
186
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000187INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
188 "Profile summary info", false, true)
189
190ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
191 : ImmutablePass(ID) {
192 initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
193}
194
Dehao Chen5461d8b2016-09-28 21:00:58 +0000195bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
196 PSI.reset(new ProfileSummaryInfo(M));
197 return false;
198}
199
200bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
201 PSI.reset();
202 return false;
203}
204
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000205AnalysisKey ProfileSummaryAnalysis::Key;
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000206ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
207 ModuleAnalysisManager &) {
Dehao Chen5461d8b2016-09-28 21:00:58 +0000208 return ProfileSummaryInfo(M);
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000209}
210
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000211PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
Sean Silvafd03ac62016-08-09 00:28:38 +0000212 ModuleAnalysisManager &AM) {
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000213 ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
214
215 OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
216 for (auto &F : M) {
217 OS << F.getName();
Dehao Chen84287ab2016-10-10 21:47:28 +0000218 if (PSI.isFunctionEntryHot(&F))
219 OS << " :hot entry ";
220 else if (PSI.isFunctionEntryCold(&F))
221 OS << " :cold entry ";
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000222 OS << "\n";
223 }
224 return PreservedAnalyses::all();
225}
226
227char ProfileSummaryInfoWrapperPass::ID = 0;