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" |
| 16 | #include "llvm/IR/Metadata.h" |
| 17 | #include "llvm/IR/Module.h" |
| 18 | #include "llvm/IR/ProfileSummary.h" |
| 19 | using namespace llvm; |
| 20 | |
| 21 | // The following two parameters determine the threshold for a count to be |
| 22 | // considered hot/cold. These two parameters are percentile values (multiplied |
| 23 | // by 10000). If the counts are sorted in descending order, the minimum count to |
| 24 | // reach ProfileSummaryCutoffHot gives the threshold to determine a hot count. |
| 25 | // Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the |
| 26 | // threshold for determining cold count (everything <= this threshold is |
| 27 | // considered cold). |
| 28 | |
| 29 | static cl::opt<int> ProfileSummaryCutoffHot( |
| 30 | "profile-summary-cutoff-hot", cl::Hidden, cl::init(999000), cl::ZeroOrMore, |
| 31 | cl::desc("A count is hot if it exceeds the minimum count to" |
| 32 | " reach this percentile of total counts.")); |
| 33 | |
| 34 | static cl::opt<int> ProfileSummaryCutoffCold( |
| 35 | "profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore, |
| 36 | cl::desc("A count is cold if it is below the minimum count" |
| 37 | " to reach this percentile of total counts.")); |
| 38 | |
| 39 | // Find the minimum count to reach a desired percentile of counts. |
| 40 | static uint64_t getMinCountForPercentile(SummaryEntryVector &DS, |
| 41 | uint64_t Percentile) { |
| 42 | auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) { |
| 43 | return Entry.Cutoff < Percentile; |
| 44 | }; |
| 45 | auto It = std::lower_bound(DS.begin(), DS.end(), Percentile, Compare); |
| 46 | // The required percentile has to be <= one of the percentiles in the |
| 47 | // detailed summary. |
| 48 | if (It == DS.end()) |
| 49 | report_fatal_error("Desired percentile exceeds the maximum cutoff"); |
| 50 | return It->MinCount; |
| 51 | } |
| 52 | |
| 53 | // The profile summary metadata may be attached either by the frontend or by |
| 54 | // any backend passes (IR level instrumentation, for example). This method |
| 55 | // checks if the Summary is null and if so checks if the summary metadata is now |
| 56 | // available in the module and parses it to get the Summary object. |
| 57 | void ProfileSummaryInfo::computeSummary() { |
| 58 | if (Summary) |
| 59 | return; |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 60 | auto *SummaryMD = M.getProfileSummary(); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 61 | if (!SummaryMD) |
| 62 | return; |
| 63 | Summary.reset(ProfileSummary::getFromMD(SummaryMD)); |
| 64 | } |
| 65 | |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame^] | 66 | /// Returns true if the function's entry is hot. If it returns false, it |
| 67 | /// 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] | 68 | /// example, no profile data is available). |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame^] | 69 | bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) { |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 70 | computeSummary(); |
| 71 | if (!F || !Summary) |
| 72 | return false; |
| 73 | auto FunctionCount = F->getEntryCount(); |
| 74 | // FIXME: The heuristic used below for determining hotness is based on |
| 75 | // preliminary SPEC tuning for inliner. This will eventually be a |
| 76 | // convenience method that calls isHotCount. |
| 77 | return (FunctionCount && |
| 78 | FunctionCount.getValue() >= |
| 79 | (uint64_t)(0.3 * (double)Summary->getMaxFunctionCount())); |
| 80 | } |
| 81 | |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame^] | 82 | /// Returns true if the function's entry is a cold. If it returns false, it |
| 83 | /// 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] | 84 | /// example, no profile data is available). |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame^] | 85 | bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) { |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 86 | computeSummary(); |
Richard Trieu | fae70b6 | 2016-06-10 01:42:05 +0000 | [diff] [blame] | 87 | if (!F) |
| 88 | return false; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 89 | if (F->hasFnAttribute(Attribute::Cold)) { |
| 90 | return true; |
| 91 | } |
Richard Trieu | fae70b6 | 2016-06-10 01:42:05 +0000 | [diff] [blame] | 92 | if (!Summary) |
| 93 | return false; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 94 | auto FunctionCount = F->getEntryCount(); |
| 95 | // FIXME: The heuristic used below for determining coldness is based on |
| 96 | // preliminary SPEC tuning for inliner. This will eventually be a |
| 97 | // convenience method that calls isHotCount. |
| 98 | return (FunctionCount && |
| 99 | FunctionCount.getValue() <= |
| 100 | (uint64_t)(0.01 * (double)Summary->getMaxFunctionCount())); |
| 101 | } |
| 102 | |
Piotr Padlewski | f3d122c | 2016-09-30 21:05:49 +0000 | [diff] [blame] | 103 | /// Compute the hot and cold thresholds. |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 104 | void ProfileSummaryInfo::computeThresholds() { |
| 105 | if (!Summary) |
| 106 | computeSummary(); |
| 107 | if (!Summary) |
| 108 | return; |
| 109 | auto &DetailedSummary = Summary->getDetailedSummary(); |
| 110 | HotCountThreshold = |
| 111 | getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffHot); |
| 112 | ColdCountThreshold = |
| 113 | getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffCold); |
| 114 | } |
| 115 | |
| 116 | bool ProfileSummaryInfo::isHotCount(uint64_t C) { |
| 117 | if (!HotCountThreshold) |
| 118 | computeThresholds(); |
| 119 | return HotCountThreshold && C >= HotCountThreshold.getValue(); |
| 120 | } |
| 121 | |
| 122 | bool ProfileSummaryInfo::isColdCount(uint64_t C) { |
| 123 | if (!ColdCountThreshold) |
| 124 | computeThresholds(); |
| 125 | return ColdCountThreshold && C <= ColdCountThreshold.getValue(); |
| 126 | } |
| 127 | |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 128 | INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info", |
| 129 | "Profile summary info", false, true) |
| 130 | |
| 131 | ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass() |
| 132 | : ImmutablePass(ID) { |
| 133 | initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry()); |
| 134 | } |
| 135 | |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 136 | bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) { |
| 137 | PSI.reset(new ProfileSummaryInfo(M)); |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) { |
| 142 | PSI.reset(); |
| 143 | return false; |
| 144 | } |
| 145 | |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 146 | char ProfileSummaryAnalysis::PassID; |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 147 | ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M, |
| 148 | ModuleAnalysisManager &) { |
Dehao Chen | 5461d8b | 2016-09-28 21:00:58 +0000 | [diff] [blame] | 149 | return ProfileSummaryInfo(M); |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 152 | PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M, |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 153 | ModuleAnalysisManager &AM) { |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 154 | ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M); |
| 155 | |
| 156 | OS << "Functions in " << M.getName() << " with hot/cold annotations: \n"; |
| 157 | for (auto &F : M) { |
| 158 | OS << F.getName(); |
Dehao Chen | 84287ab | 2016-10-10 21:47:28 +0000 | [diff] [blame^] | 159 | if (PSI.isFunctionEntryHot(&F)) |
| 160 | OS << " :hot entry "; |
| 161 | else if (PSI.isFunctionEntryCold(&F)) |
| 162 | OS << " :cold entry "; |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 163 | OS << "\n"; |
| 164 | } |
| 165 | return PreservedAnalyses::all(); |
| 166 | } |
| 167 | |
| 168 | char ProfileSummaryInfoWrapperPass::ID = 0; |