1 ////===- SampleProfileLoadBaseUtil.h - Profile loader util func --*- C++-*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 /// \file 10 /// This file provides the utility functions for the sampled PGO loader base 11 /// implementation. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H 16 #define LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H 17 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/Analysis/ProfileSummaryInfo.h" 20 #include "llvm/IR/BasicBlock.h" 21 #include "llvm/IR/CFG.h" 22 #include "llvm/IR/DebugLoc.h" 23 #include "llvm/IR/Function.h" 24 #include "llvm/ProfileData/SampleProf.h" 25 #include "llvm/Support/CommandLine.h" 26 27 namespace llvm { 28 using namespace sampleprof; 29 30 class ProfileSummaryInfo; 31 32 extern cl::opt<unsigned> SampleProfileMaxPropagateIterations; 33 extern cl::opt<unsigned> SampleProfileRecordCoverage; 34 extern cl::opt<unsigned> SampleProfileSampleCoverage; 35 extern cl::opt<bool> NoWarnSampleUnused; 36 37 namespace sampleprofutil { 38 39 class SampleCoverageTracker { 40 public: 41 bool markSamplesUsed(const FunctionSamples *FS, uint32_t LineOffset, 42 uint32_t Discriminator, uint64_t Samples); 43 unsigned computeCoverage(unsigned Used, unsigned Total) const; 44 unsigned countUsedRecords(const FunctionSamples *FS, 45 ProfileSummaryInfo *PSI) const; 46 unsigned countBodyRecords(const FunctionSamples *FS, 47 ProfileSummaryInfo *PSI) const; getTotalUsedSamples()48 uint64_t getTotalUsedSamples() const { return TotalUsedSamples; } 49 uint64_t countBodySamples(const FunctionSamples *FS, 50 ProfileSummaryInfo *PSI) const; 51 clear()52 void clear() { 53 SampleCoverage.clear(); 54 TotalUsedSamples = 0; 55 } setProfAccForSymsInList(bool V)56 void setProfAccForSymsInList(bool V) { ProfAccForSymsInList = V; } 57 58 private: 59 using BodySampleCoverageMap = std::map<LineLocation, unsigned>; 60 using FunctionSamplesCoverageMap = 61 DenseMap<const FunctionSamples *, BodySampleCoverageMap>; 62 63 /// Coverage map for sampling records. 64 /// 65 /// This map keeps a record of sampling records that have been matched to 66 /// an IR instruction. This is used to detect some form of staleness in 67 /// profiles (see flag -sample-profile-check-coverage). 68 /// 69 /// Each entry in the map corresponds to a FunctionSamples instance. This is 70 /// another map that counts how many times the sample record at the 71 /// given location has been used. 72 FunctionSamplesCoverageMap SampleCoverage; 73 74 /// Number of samples used from the profile. 75 /// 76 /// When a sampling record is used for the first time, the samples from 77 /// that record are added to this accumulator. Coverage is later computed 78 /// based on the total number of samples available in this function and 79 /// its callsites. 80 /// 81 /// Note that this accumulator tracks samples used from a single function 82 /// and all the inlined callsites. Strictly, we should have a map of counters 83 /// keyed by FunctionSamples pointers, but these stats are cleared after 84 /// every function, so we just need to keep a single counter. 85 uint64_t TotalUsedSamples = 0; 86 87 // For symbol in profile symbol list, whether to regard their profiles 88 // to be accurate. This is passed from the SampleLoader instance. 89 bool ProfAccForSymsInList = false; 90 }; 91 92 /// Return true if the given callsite is hot wrt to hot cutoff threshold. 93 bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI, 94 bool ProfAccForSymsInList); 95 } // end of namespace sampleprofutil 96 } // end of namespace llvm 97 98 #endif // LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H 99