LCOV - code coverage report
Current view: top level - lib/Analysis - ProfileSummaryInfo.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 122 124 98.4 %
Date: 2018-02-20 16:54:40 Functions: 24 24 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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/Analysis/BlockFrequencyInfo.h"
      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             : 
      32       97302 : static cl::opt<int> ProfileSummaryCutoffHot(
      33      194604 :     "profile-summary-cutoff-hot", cl::Hidden, cl::init(990000), cl::ZeroOrMore,
      34       97302 :     cl::desc("A count is hot if it exceeds the minimum count to"
      35      291906 :              " reach this percentile of total counts."));
      36             : 
      37       97302 : static cl::opt<int> ProfileSummaryCutoffCold(
      38      194604 :     "profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore,
      39       97302 :     cl::desc("A count is cold if it is below the minimum count"
      40      291906 :              " to reach this percentile of total counts."));
      41             : 
      42       97302 : static cl::opt<bool> ProfileSampleAccurate(
      43      194604 :     "profile-sample-accurate", cl::Hidden, cl::init(false),
      44       97302 :     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       97302 :              "we have no profile."));
      47       97302 : static cl::opt<unsigned> ProfileSummaryHugeWorkingSetSizeThreshold(
      48             :     "profile-summary-huge-working-set-size-threshold", cl::Hidden,
      49      194604 :     cl::init(15000), cl::ZeroOrMore,
      50       97302 :     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      291906 :              " percentile exceeds this count."));
      53             : 
      54             : // Find the summary entry for a desired percentile of counts.
      55         116 : static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS,
      56             :                                                         uint64_t Percentile) {
      57             :   auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) {
      58         360 :     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         116 :   if (It == DS.end())
      64           0 :     report_fatal_error("Desired percentile exceeds the maximum cutoff");
      65         116 :   return *It;
      66             : }
      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
      71             : // available in the module and parses it to get the Summary object. Returns true
      72             : // if a valid Summary is available.
      73     1420892 : bool ProfileSummaryInfo::computeSummary() {
      74     1420892 :   if (Summary)
      75             :     return true;
      76     1420434 :   auto *SummaryMD = M.getProfileSummary();
      77     1420434 :   if (!SummaryMD)
      78             :     return false;
      79          67 :   Summary.reset(ProfileSummary::getFromMD(SummaryMD));
      80             :   return true;
      81             : }
      82             : 
      83             : Optional<uint64_t>
      84         476 : ProfileSummaryInfo::getProfileCount(const Instruction *Inst,
      85             :                                     BlockFrequencyInfo *BFI) {
      86         476 :   if (!Inst)
      87             :     return None;
      88             :   assert((isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) &&
      89             :          "We can only get profile count for call/invoke instruction.");
      90             :   if (hasSampleProfile()) {
      91             :     // 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,
      93             :     // since the sampled entry count may not be accurate. If there is no
      94             :     // annotated on the instruction, return None.
      95             :     uint64_t TotalCount;
      96          77 :     if (Inst->extractProfTotalWeight(TotalCount))
      97             :       return TotalCount;
      98             :     return None;
      99             :   }
     100         399 :   if (BFI)
     101         335 :     return BFI->getBlockProfileCount(Inst->getParent());
     102             :   return None;
     103             : }
     104             : 
     105             : /// 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
     107             : /// example, no profile data is available).
     108      236189 : bool ProfileSummaryInfo::isFunctionEntryHot(const Function *F) {
     109      236189 :   if (!F || !computeSummary())
     110             :     return false;
     111          19 :   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.
     115          19 :   return FunctionCount && isHotCount(FunctionCount.getCount());
     116             : }
     117             : 
     118             : /// 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.
     121             : /// If it returns false, it either means it is not hot or it is unknown
     122             : /// (for example, no profile data is available).
     123      160255 : bool ProfileSummaryInfo::isFunctionHotInCallGraph(const Function *F,
     124             :                                                   BlockFrequencyInfo &BFI) {
     125      160255 :   if (!F || !computeSummary())
     126             :     return false;
     127          92 :   if (auto FunctionCount = F->getEntryCount())
     128          18 :     if (isHotCount(FunctionCount.getCount()))
     129           4 :       return true;
     130             : 
     131             :   if (hasSampleProfile()) {
     132             :     uint64_t TotalCallCount = 0;
     133          42 :     for (const auto &BB : *F)
     134          86 :       for (const auto &I : BB)
     135          57 :         if (isa<CallInst>(I) || isa<InvokeInst>(I))
     136          17 :           if (auto CallCount = getProfileCount(&I, nullptr))
     137           9 :             TotalCallCount += CallCount.getValue();
     138          13 :     if (isHotCount(TotalCallCount))
     139             :       return true;
     140             :   }
     141          84 :   for (const auto &BB : *F)
     142          49 :     if (isHotBB(&BB, &BFI))
     143             :       return true;
     144             :   return false;
     145             : }
     146             : 
     147             : /// 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.
     150             : /// If it returns false, it either means it is not cold or it is unknown
     151             : /// (for example, no profile data is available).
     152      160244 : bool ProfileSummaryInfo::isFunctionColdInCallGraph(const Function *F,
     153             :                                                    BlockFrequencyInfo &BFI) {
     154      160244 :   if (!F || !computeSummary())
     155             :     return false;
     156          70 :   if (auto FunctionCount = F->getEntryCount())
     157           7 :     if (!isColdCount(FunctionCount.getCount()))
     158           0 :       return false;
     159             : 
     160             :   if (hasSampleProfile()) {
     161             :     uint64_t TotalCallCount = 0;
     162          19 :     for (const auto &BB : *F)
     163          36 :       for (const auto &I : BB)
     164          24 :         if (isa<CallInst>(I) || isa<InvokeInst>(I))
     165           8 :           if (auto CallCount = getProfileCount(&I, nullptr))
     166           4 :             TotalCallCount += CallCount.getValue();
     167           7 :     if (!isColdCount(TotalCallCount))
     168             :       return false;
     169             :   }
     170          45 :   for (const auto &BB : *F)
     171          39 :     if (!isColdBB(&BB, &BFI))
     172             :       return false;
     173             :   return true;
     174             : }
     175             : 
     176             : /// 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
     178             : /// example, no profile data is available).
     179      236358 : bool ProfileSummaryInfo::isFunctionEntryCold(const Function *F) {
     180      236358 :   if (!F)
     181             :     return false;
     182      236358 :   if (F->hasFnAttribute(Attribute::Cold))
     183             :     return true;
     184      236341 :   if (!computeSummary())
     185             :     return false;
     186          16 :   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.
     190          16 :   return FunctionCount && isColdCount(FunctionCount.getCount());
     191             : }
     192             : 
     193             : /// Compute the hot and cold thresholds.
     194      154673 : void ProfileSummaryInfo::computeThresholds() {
     195      154673 :   if (!computeSummary())
     196             :     return;
     197             :   auto &DetailedSummary = Summary->getDetailedSummary();
     198             :   auto &HotEntry =
     199          58 :       getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
     200          58 :   HotCountThreshold = HotEntry.MinCount;
     201             :   auto &ColdEntry =
     202          58 :       getEntryForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
     203          58 :   ColdCountThreshold = ColdEntry.MinCount;
     204             :   HasHugeWorkingSetSize =
     205         116 :       HotEntry.NumCounts > ProfileSummaryHugeWorkingSetSizeThreshold;
     206             : }
     207             : 
     208      154634 : bool ProfileSummaryInfo::hasHugeWorkingSetSize() {
     209      154634 :   if (!HasHugeWorkingSetSize)
     210      154601 :     computeThresholds();
     211      154634 :   return HasHugeWorkingSetSize && HasHugeWorkingSetSize.getValue();
     212             : }
     213             : 
     214         167 : bool ProfileSummaryInfo::isHotCount(uint64_t C) {
     215         167 :   if (!HotCountThreshold)
     216          59 :     computeThresholds();
     217         167 :   return HotCountThreshold && C >= HotCountThreshold.getValue();
     218             : }
     219             : 
     220         124 : bool ProfileSummaryInfo::isColdCount(uint64_t C) {
     221         124 :   if (!ColdCountThreshold)
     222          13 :     computeThresholds();
     223         124 :   return ColdCountThreshold && C <= ColdCountThreshold.getValue();
     224             : }
     225             : 
     226          58 : bool ProfileSummaryInfo::isHotBB(const BasicBlock *B, BlockFrequencyInfo *BFI) {
     227          58 :   auto Count = BFI->getBlockProfileCount(B);
     228          58 :   return Count && isHotCount(*Count);
     229             : }
     230             : 
     231          84 : bool ProfileSummaryInfo::isColdBB(const BasicBlock *B,
     232             :                                   BlockFrequencyInfo *BFI) {
     233          84 :   auto Count = BFI->getBlockProfileCount(B);
     234          84 :   return Count && isColdCount(*Count);
     235             : }
     236             : 
     237          43 : bool ProfileSummaryInfo::isHotCallSite(const CallSite &CS,
     238             :                                        BlockFrequencyInfo *BFI) {
     239          43 :   auto C = getProfileCount(CS.getInstruction(), BFI);
     240          43 :   return C && isHotCount(*C);
     241             : }
     242             : 
     243          25 : bool ProfileSummaryInfo::isColdCallSite(const CallSite &CS,
     244             :                                         BlockFrequencyInfo *BFI) {
     245          25 :   auto C = getProfileCount(CS.getInstruction(), BFI);
     246          25 :   if (C)
     247          11 :     return isColdCount(*C);
     248             : 
     249             :   // In SamplePGO, if the caller has been sampled, and there is no profile
     250             :   // annotatedon the callsite, we consider the callsite as cold.
     251             :   // If there is no profile for the caller, and we know the profile is
     252             :   // accurate, we consider the callsite as cold.
     253           8 :   return (hasSampleProfile() &&
     254           6 :           (CS.getCaller()->hasProfileData() || ProfileSampleAccurate ||
     255             :            CS.getCaller()->hasFnAttribute("profile-sample-accurate")));
     256             : }
     257             : 
     258      544932 : INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
     259             :                 "Profile summary info", false, true)
     260             : 
     261       28786 : ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
     262       28786 :     : ImmutablePass(ID) {
     263       28786 :   initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
     264       28786 : }
     265             : 
     266       28790 : bool ProfileSummaryInfoWrapperPass::doInitialization(Module &M) {
     267       28790 :   PSI.reset(new ProfileSummaryInfo(M));
     268       28790 :   return false;
     269             : }
     270             : 
     271       28689 : bool ProfileSummaryInfoWrapperPass::doFinalization(Module &M) {
     272             :   PSI.reset();
     273       28689 :   return false;
     274             : }
     275             : 
     276             : AnalysisKey ProfileSummaryAnalysis::Key;
     277         133 : ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M,
     278             :                                                ModuleAnalysisManager &) {
     279         133 :   return ProfileSummaryInfo(M);
     280             : }
     281             : 
     282           1 : PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
     283             :                                                  ModuleAnalysisManager &AM) {
     284             :   ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
     285             : 
     286           2 :   OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
     287           4 :   for (auto &F : M) {
     288           3 :     OS << F.getName();
     289           3 :     if (PSI.isFunctionEntryHot(&F))
     290           2 :       OS << " :hot entry ";
     291           1 :     else if (PSI.isFunctionEntryCold(&F))
     292           1 :       OS << " :cold entry ";
     293           3 :     OS << "\n";
     294             :   }
     295           1 :   return PreservedAnalyses::all();
     296             : }
     297             : 
     298      291906 : char ProfileSummaryInfoWrapperPass::ID = 0;

Generated by: LCOV version 1.13