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/Constants.h" 23 #include "llvm/IR/DebugLoc.h" 24 #include "llvm/IR/Function.h" 25 #include "llvm/ProfileData/SampleProf.h" 26 #include "llvm/Support/CommandLine.h" 27 #include "llvm/Transforms/Utils/ModuleUtils.h" 28 29 namespace llvm { 30 using namespace sampleprof; 31 32 class ProfileSummaryInfo; 33 34 extern cl::opt<unsigned> SampleProfileMaxPropagateIterations; 35 extern cl::opt<unsigned> SampleProfileRecordCoverage; 36 extern cl::opt<unsigned> SampleProfileSampleCoverage; 37 extern cl::opt<bool> NoWarnSampleUnused; 38 39 namespace sampleprofutil { 40 41 class SampleCoverageTracker { 42 public: 43 bool markSamplesUsed(const FunctionSamples *FS, uint32_t LineOffset, 44 uint32_t Discriminator, uint64_t Samples); 45 unsigned computeCoverage(unsigned Used, unsigned Total) const; 46 unsigned countUsedRecords(const FunctionSamples *FS, 47 ProfileSummaryInfo *PSI) const; 48 unsigned countBodyRecords(const FunctionSamples *FS, 49 ProfileSummaryInfo *PSI) const; getTotalUsedSamples()50 uint64_t getTotalUsedSamples() const { return TotalUsedSamples; } 51 uint64_t countBodySamples(const FunctionSamples *FS, 52 ProfileSummaryInfo *PSI) const; 53 clear()54 void clear() { 55 SampleCoverage.clear(); 56 TotalUsedSamples = 0; 57 } setProfAccForSymsInList(bool V)58 void setProfAccForSymsInList(bool V) { ProfAccForSymsInList = V; } 59 60 private: 61 using BodySampleCoverageMap = std::map<LineLocation, unsigned>; 62 using FunctionSamplesCoverageMap = 63 DenseMap<const FunctionSamples *, BodySampleCoverageMap>; 64 65 /// Coverage map for sampling records. 66 /// 67 /// This map keeps a record of sampling records that have been matched to 68 /// an IR instruction. This is used to detect some form of staleness in 69 /// profiles (see flag -sample-profile-check-coverage). 70 /// 71 /// Each entry in the map corresponds to a FunctionSamples instance. This is 72 /// another map that counts how many times the sample record at the 73 /// given location has been used. 74 FunctionSamplesCoverageMap SampleCoverage; 75 76 /// Number of samples used from the profile. 77 /// 78 /// When a sampling record is used for the first time, the samples from 79 /// that record are added to this accumulator. Coverage is later computed 80 /// based on the total number of samples available in this function and 81 /// its callsites. 82 /// 83 /// Note that this accumulator tracks samples used from a single function 84 /// and all the inlined callsites. Strictly, we should have a map of counters 85 /// keyed by FunctionSamples pointers, but these stats are cleared after 86 /// every function, so we just need to keep a single counter. 87 uint64_t TotalUsedSamples = 0; 88 89 // For symbol in profile symbol list, whether to regard their profiles 90 // to be accurate. This is passed from the SampleLoader instance. 91 bool ProfAccForSymsInList = false; 92 }; 93 94 /// Return true if the given callsite is hot wrt to hot cutoff threshold. 95 bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI, 96 bool ProfAccForSymsInList); 97 98 /// Create a global variable to flag FSDiscriminators are used. 99 void createFSDiscriminatorVariable(Module *M); 100 101 } // end of namespace sampleprofutil 102 } // end of namespace llvm 103 104 #endif // LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H 105