Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 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 | |
| 15 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
Dehao Chen | 38a666d | 2016-11-09 23:36:02 +0000 | [diff] [blame] | 17 | #include "llvm/IR/BasicBlock.h" |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 18 | #include "llvm/IR/CallSite.h" |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 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 | |
| 32 | static 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 | |
| 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.")); |
| 41 | |
| 42 | // Find the minimum count to reach a desired percentile of counts. |
| 43 | static 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 |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 59 | // available in the module and parses it to get the Summary object. Returns true |
| 60 | // if a valid Summary is available. |
| 61 | bool ProfileSummaryInfo::computeSummary() { |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 62 | if (Summary) |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 63 | return true; |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 64 | auto *SummaryMD = M.getProfileSummary(); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 65 | if (!SummaryMD) |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 66 | return false; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 67 | Summary.reset(ProfileSummary::getFromMD(SummaryMD)); |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 68 | return true; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Dehao Chen | c204815 | 2017-03-10 19:45:16 +0000 | [diff] [blame] | 71 | Optional<uint64_t> |
| 72 | ProfileSummaryInfo::getProfileCount(const Instruction *Inst, |
| 73 | BlockFrequencyInfo *BFI) { |
| 74 | if (!Inst) |
| 75 | return None; |
| 76 | assert((isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) && |
| 77 | "We can only get profile count for call/invoke instruction."); |
| 78 | // Check if there is a profile metadata on the instruction. If it is present, |
| 79 | // determine hotness solely based on that. |
| 80 | uint64_t TotalCount; |
| 81 | if (Inst->extractProfTotalWeight(TotalCount)) |
| 82 | return TotalCount; |
| 83 | if (BFI) |
| 84 | return BFI->getBlockProfileCount(Inst->getParent()); |
| 85 | return None; |
| 86 | } |
| 87 | |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 88 | /// Returns true if the function's entry is hot. If it returns false, it |
| 89 | /// either means it is not hot or it is unknown whether it is hot or not (for |
Piotr Padlewski | f3d122c | 2016-09-30 21:05:49 +0000 | [diff] [blame] | 90 | /// example, no profile data is available). |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 91 | bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) { |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 92 | if (!F || !computeSummary()) |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 93 | return false; |
| 94 | auto FunctionCount = F->getEntryCount(); |
| 95 | // FIXME: The heuristic used below for determining hotness is based on |
| 96 | // preliminary SPEC tuning for inliner. This will eventually be a |
| 97 | // convenience method that calls isHotCount. |
Dehao Chen | c87bc79 | 2016-10-11 05:19:00 +0000 | [diff] [blame] | 98 | return FunctionCount && isHotCount(FunctionCount.getValue()); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 101 | /// Returns true if the function's entry is a cold. If it returns false, it |
| 102 | /// either means it is not cold or it is unknown whether it is cold or not (for |
Piotr Padlewski | f3d122c | 2016-09-30 21:05:49 +0000 | [diff] [blame] | 103 | /// example, no profile data is available). |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 104 | bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) { |
Richard Trieu | fae70b6 | 2016-06-10 01:42:05 +0000 | [diff] [blame] | 105 | if (!F) |
| 106 | return false; |
Davide Italiano | 574e597 | 2017-03-10 20:50:51 +0000 | [diff] [blame^] | 107 | if (F->hasFnAttribute(Attribute::Cold)) |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 108 | return true; |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 109 | if (!computeSummary()) |
Richard Trieu | fae70b6 | 2016-06-10 01:42:05 +0000 | [diff] [blame] | 110 | return false; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 111 | auto FunctionCount = F->getEntryCount(); |
| 112 | // FIXME: The heuristic used below for determining coldness is based on |
| 113 | // preliminary SPEC tuning for inliner. This will eventually be a |
| 114 | // convenience method that calls isHotCount. |
Dehao Chen | c87bc79 | 2016-10-11 05:19:00 +0000 | [diff] [blame] | 115 | return FunctionCount && isColdCount(FunctionCount.getValue()); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Piotr Padlewski | f3d122c | 2016-09-30 21:05:49 +0000 | [diff] [blame] | 118 | /// Compute the hot and cold thresholds. |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 119 | void ProfileSummaryInfo::computeThresholds() { |
Easwaran Raman | a7bdb8a | 2017-01-14 00:32:37 +0000 | [diff] [blame] | 120 | if (!computeSummary()) |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 121 | return; |
| 122 | auto &DetailedSummary = Summary->getDetailedSummary(); |
| 123 | HotCountThreshold = |
| 124 | getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffHot); |
| 125 | ColdCountThreshold = |
| 126 | getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffCold); |
| 127 | } |
| 128 | |
| 129 | bool ProfileSummaryInfo::isHotCount(uint64_t C) { |
| 130 | if (!HotCountThreshold) |
| 131 | computeThresholds(); |
| 132 | return HotCountThreshold && C >= HotCountThreshold.getValue(); |
| 133 | } |
| 134 | |
| 135 | bool ProfileSummaryInfo::isColdCount(uint64_t C) { |
| 136 | if (!ColdCountThreshold) |
| 137 | computeThresholds(); |
| 138 | return ColdCountThreshold && C <= ColdCountThreshold.getValue(); |
| 139 | } |
| 140 | |
Dehao Chen | 38a666d | 2016-11-09 23:36:02 +0000 | [diff] [blame] | 141 | bool ProfileSummaryInfo::isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI) { |
| 142 | auto Count = BFI->getBlockProfileCount(B); |
Dehao Chen | 22645ee | 2017-03-10 01:44:37 +0000 | [diff] [blame] | 143 | return Count && isHotCount(*Count); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | bool ProfileSummaryInfo::isColdBB(const BasicBlock *B, |
| 147 | BlockFrequencyInfo *BFI) { |
| 148 | auto Count = BFI->getBlockProfileCount(B); |
| 149 | return Count && isColdCount(*Count); |
| 150 | } |
| 151 | |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 152 | bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS, |
| 153 | BlockFrequencyInfo *BFI) { |
Dehao Chen | c204815 | 2017-03-10 19:45:16 +0000 | [diff] [blame] | 154 | auto C = getProfileCount(CS.getInstruction(), BFI); |
| 155 | return C && isHotCount(*C); |
Easwaran Raman | b035f91 | 2017-01-13 01:34:00 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS, |
| 159 | BlockFrequencyInfo *BFI) { |
Dehao Chen | c204815 | 2017-03-10 19:45:16 +0000 | [diff] [blame] | 160 | auto C = getProfileCount(CS.getInstruction(), BFI); |
| 161 | return C && isColdCount(*C); |
Dehao Chen | 38a666d | 2016-11-09 23:36:02 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 164 | INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info", |
| 165 | "Profile summary info", false, true) |
| 166 | |
| 167 | ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass() |
| 168 | : ImmutablePass(ID) { |
| 169 | initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 170 | } |
| 171 | |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 172 | bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) { |
| 173 | PSI.reset(new ProfileSummaryInfo(M)); |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) { |
| 178 | PSI.reset(); |
| 179 | return false; |
| 180 | } |
| 181 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 182 | AnalysisKey ProfileSummaryAnalysis::Key; |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 183 | ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M, |
| 184 | ModuleAnalysisManager &) { |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 185 | return ProfileSummaryInfo(M); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 188 | PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M, |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 189 | ModuleAnalysisManager &AM) { |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 190 | ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M); |
| 191 | |
| 192 | OS << "Functions in " << M.getName() << " with hot/cold annotations: \n"; |
| 193 | for (auto &F : M) { |
| 194 | OS << F.getName(); |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame] | 195 | if (PSI.isFunctionEntryHot(&F)) |
| 196 | OS << " :hot entry "; |
| 197 | else if (PSI.isFunctionEntryCold(&F)) |
| 198 | OS << " :cold entry "; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 199 | OS << "\n"; |
| 200 | } |
| 201 | return PreservedAnalyses::all(); |
| 202 | } |
| 203 | |
| 204 | char ProfileSummaryInfoWrapperPass::ID = 0; |