LLVM  8.0.0svn
ProfileSummaryInfo.cpp
Go to the documentation of this file.
1 //===- 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 
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"
22 using 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 
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."));
36 
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 
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."));
48 
49 // The next two options override the counts derived from summary computation and
50 // are useful for debugging purposes.
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 
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 
61 // Find the summary entry for a desired percentile of counts.
63  uint64_t Percentile) {
64  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");
72  return *It;
73 }
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
78 // available in the module and parses it to get the Summary object. Returns true
79 // if a valid Summary is available.
80 bool ProfileSummaryInfo::computeSummary() {
81  if (Summary)
82  return true;
83  auto *SummaryMD = M.getProfileSummary();
84  if (!SummaryMD)
85  return false;
86  Summary.reset(ProfileSummary::getFromMD(SummaryMD));
87  return true;
88 }
89 
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.");
97  if (hasSampleProfile()) {
98  // 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,
100  // since the sampled entry count may not be accurate. If there is no
101  // annotated on the instruction, return None.
102  uint64_t TotalCount;
103  if (Inst->extractProfTotalWeight(TotalCount))
104  return TotalCount;
105  return None;
106  }
107  if (BFI)
108  return BFI->getBlockProfileCount(Inst->getParent());
109  return None;
110 }
111 
112 /// 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
114 /// example, no profile data is available).
116  if (!F || !computeSummary())
117  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.
122  return FunctionCount && isHotCount(FunctionCount.getCount());
123 }
124 
125 /// 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.
128 /// If it returns false, it either means it is not hot or it is unknown
129 /// (for example, no profile data is available).
132  if (!F || !computeSummary())
133  return false;
134  if (auto FunctionCount = F->getEntryCount())
135  if (isHotCount(FunctionCount.getCount()))
136  return true;
137 
138  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  }
148  for (const auto &BB : *F)
149  if (isHotBlock(&BB, &BFI))
150  return true;
151  return false;
152 }
153 
154 /// 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.
157 /// If it returns false, it either means it is not cold or it is unknown
158 /// (for example, no profile data is available).
161  if (!F || !computeSummary())
162  return false;
163  if (auto FunctionCount = F->getEntryCount())
164  if (!isColdCount(FunctionCount.getCount()))
165  return false;
166 
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  }
177  for (const auto &BB : *F)
178  if (!isColdBlock(&BB, &BFI))
179  return false;
180  return true;
181 }
182 
183 /// 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
185 /// example, no profile data is available).
187  if (!F)
188  return false;
190  return true;
191  if (!computeSummary())
192  return false;
193  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.
197  return FunctionCount && isColdCount(FunctionCount.getCount());
198 }
199 
200 /// Compute the hot and cold thresholds.
201 void ProfileSummaryInfo::computeThresholds() {
202  if (!computeSummary())
203  return;
204  auto &DetailedSummary = Summary->getDetailedSummary();
205  auto &HotEntry =
207  HotCountThreshold = HotEntry.MinCount;
208  if (ProfileSummaryHotCount.getNumOccurrences() > 0)
209  HotCountThreshold = ProfileSummaryHotCount;
210  auto &ColdEntry =
212  ColdCountThreshold = ColdEntry.MinCount;
213  if (ProfileSummaryColdCount.getNumOccurrences() > 0)
214  ColdCountThreshold = ProfileSummaryColdCount;
215  assert(ColdCountThreshold <= HotCountThreshold &&
216  "Cold count threshold cannot exceed hot count threshold!");
217  HasHugeWorkingSetSize =
218  HotEntry.NumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
219 }
220 
222  if (!HasHugeWorkingSetSize)
223  computeThresholds();
224  return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue();
225 }
226 
228  if (!HotCountThreshold)
229  computeThresholds();
230  return HotCountThreshold && C >= HotCountThreshold.getValue();
231 }
232 
234  if (!ColdCountThreshold)
235  computeThresholds();
236  return ColdCountThreshold && C <= ColdCountThreshold.getValue();
237 }
238 
240  if (!HotCountThreshold)
241  computeThresholds();
242  return HotCountThreshold ? HotCountThreshold.getValue() : UINT64_MAX;
243 }
244 
246  if (!ColdCountThreshold)
247  computeThresholds();
248  return ColdCountThreshold ? ColdCountThreshold.getValue() : 0;
249 }
250 
252  auto Count = BFI->getBlockProfileCount(BB);
253  return Count && isHotCount(*Count);
254 }
255 
258  auto Count = BFI->getBlockProfileCount(BB);
259  return Count && isColdCount(*Count);
260 }
261 
264  auto C = getProfileCount(CS.getInstruction(), BFI);
265  return C && isHotCount(*C);
266 }
267 
270  auto C = getProfileCount(CS.getInstruction(), BFI);
271  if (C)
272  return isColdCount(*C);
273 
274  // In SamplePGO, if the caller has been sampled, and there is no profile
275  // annotated on the callsite, we consider the callsite as cold.
276  return hasSampleProfile() && CS.getCaller()->hasProfileData();
277 }
278 
280  "Profile summary info", false, true)
281 
283  : ImmutablePass(ID) {
285 }
286 
288  PSI.reset(new ProfileSummaryInfo(M));
289  return false;
290 }
291 
293  PSI.reset();
294  return false;
295 }
296 
297 AnalysisKey ProfileSummaryAnalysis::Key;
300  return ProfileSummaryInfo(M);
301 }
302 
304  ModuleAnalysisManager &AM) {
306 
307  OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
308  for (auto &F : M) {
309  OS << F.getName();
310  if (PSI.isFunctionEntryHot(&F))
311  OS << " :hot entry ";
312  else if (PSI.isFunctionEntryCold(&F))
313  OS << " :cold entry ";
314  OS << "\n";
315  }
316  return PreservedAnalyses::all();
317 }
318 
uint64_t CallInst * C
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
uint32_t Cutoff
The required percentile of counts.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:770
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
bool isColdCount(uint64_t C)
Returns true if count C is considered cold.
Analysis providing profile information.
This file contains the declarations for metadata subclasses.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:321
Optional< uint64_t > getProfileCount(const Instruction *CallInst, BlockFrequencyInfo *BFI)
Returns the profile count for CallInst.
StringRef getName() const
Get a short "name" for the module.
Definition: Module.h:227
F(f)
uint64_t getOrCompHotCountThreshold()
Returns HotCountThreshold if set.
static cl::opt< unsigned > ProfileSummaryHugeWorkingSetSizeThreshold("profile-summary-huge-working-set-size-threshold", cl::Hidden, cl::init(15000), cl::ZeroOrMore, cl::desc("The code working set size is considered huge if the number of" " blocks required to reach the -profile-summary-cutoff-hot" " percentile exceeds this count."))
bool isHotCount(uint64_t C)
Returns true if count C is considered hot.
bool isFunctionEntryCold(const Function *F)
Returns true if F has cold function entry.
bool isHotCallSite(const CallSite &CS, BlockFrequencyInfo *BFI)
Returns true if CallSite CS is considered hot.
static cl::opt< int > ProfileSummaryHotCount("profile-summary-hot-count", cl::ReallyHidden, cl::ZeroOrMore, cl::desc("A fixed hot count that overrides the count derived from" " profile-summary-cutoff-hot"))
uint64_t getOrCompColdCountThreshold()
Returns ColdCountThreshold if set.
Result run(Module &M, ModuleAnalysisManager &)
bool isFunctionHotInCallGraph(const Function *F, BlockFrequencyInfo &BFI)
Returns true if F contains hot code.
ProfileCount getEntryCount() const
Get the entry count for this function.
Definition: Function.cpp:1384
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
InstrTy * getInstruction() const
Definition: CallSite.h:92
Metadata * getProfileSummary()
Returns profile summary metadata.
Definition: Module.cpp:534
bool isColdBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI)
Returns true if BasicBlock BB is considered cold.
#define UINT64_MAX
Definition: DataTypes.h:83
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:161
auto lower_bound(R &&Range, ForwardIt I) -> decltype(adl_begin(Range))
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1276
bool extractProfTotalWeight(uint64_t &TotalVal) const
Retrieve total raw weight values of a branch.
Definition: Metadata.cpp:1340
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:419
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
bool hasSampleProfile()
Returns true if module M has sample profile.
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info", "Profile summary info", false, true) ProfileSummaryInfoWrapperPass
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:256
Module.h This file contains the declarations for the Module class.
void initializeProfileSummaryInfoWrapperPassPass(PassRegistry &)
bool isFunctionColdInCallGraph(const Function *F, BlockFrequencyInfo &BFI)
Returns true if F contains only cold code.
static ProfileSummary * getFromMD(Metadata *MD)
Construct profile summary from metdata.
static cl::opt< int > ProfileSummaryCutoffHot("profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore, cl::desc("A count is hot if it exceeds the minimum count to" " reach this percentile of total counts."))
FunTy * getCaller() const
Return the caller function for this call site.
Definition: CallSite.h:267
bool isFunctionEntryHot(const Function *F)
Returns true if F has hot function entry.
#define I(x, y, z)
Definition: MD5.cpp:58
bool isHotBlock(const BasicBlock *BB, BlockFrequencyInfo *BFI)
Returns true if BasicBlock BB is considered hot.
bool isColdCallSite(const CallSite &CS, BlockFrequencyInfo *BFI)
Returns true if Callsite CS is considered cold.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasHugeWorkingSetSize()
Returns true if the working set size of the code is considered huge.
static cl::opt< int > ProfileSummaryCutoffCold("profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore, cl::desc("A count is cold if it is below the minimum count" " to reach this percentile of total counts."))
A container for analyses that lazily runs them and caches their results.
std::vector< ProfileSummaryEntry > SummaryEntryVector
static cl::opt< int > ProfileSummaryColdCount("profile-summary-cold-count", cl::ReallyHidden, cl::ZeroOrMore, cl::desc("A fixed cold count that overrides the count derived from" " profile-summary-cutoff-cold"))
bool hasProfileData() const
Return true if the function is annotated with profile data.
Definition: Function.h:308
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:71
Optional< uint64_t > getBlockProfileCount(const BasicBlock *BB) const
Returns the estimated profile count of BB.
static const ProfileSummaryEntry & getEntryForPercentile(SummaryEntryVector &DS, uint64_t Percentile)
const BasicBlock * getParent() const
Definition: Instruction.h:67