LLVM  14.0.0git
SampleProfileLoaderBaseUtil.h
Go to the documentation of this file.
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"
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"
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 
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;
50  uint64_t getTotalUsedSamples() const { return TotalUsedSamples; }
51  uint64_t countBodySamples(const FunctionSamples *FS,
52  ProfileSummaryInfo *PSI) const;
53 
54  void clear() {
55  SampleCoverage.clear();
56  TotalUsedSamples = 0;
57  }
58  void setProfAccForSymsInList(bool V) { ProfAccForSymsInList = V; }
59 
60 private:
61  using BodySampleCoverageMap = std::map<LineLocation, unsigned>;
62  using FunctionSamplesCoverageMap =
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.
100 
101 } // end of namespace sampleprofutil
102 } // end of namespace llvm
103 
104 #endif // LLVM_TRANSFORMS_UTILS_SAMPLEPROFILELOADERBASEUTIL_H
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
DenseMap.h
llvm::X86AS::FS
@ FS
Definition: X86.h:188
CommandLine.h
Constants.h
llvm::sampleprofutil::SampleCoverageTracker::setProfAccForSymsInList
void setProfAccForSymsInList(bool V)
Definition: SampleProfileLoaderBaseUtil.h:58
SampleProf.h
llvm::sampleprofutil::createFSDiscriminatorVariable
void createFSDiscriminatorVariable(Module *M)
Create a global variable to flag FSDiscriminators are used.
Definition: SampleProfileLoaderBaseUtil.cpp:163
DebugLoc.h
llvm::sampleprofutil::SampleCoverageTracker::getTotalUsedSamples
uint64_t getTotalUsedSamples() const
Definition: SampleProfileLoaderBaseUtil.h:50
CFG.h
llvm::ProfileSummaryInfo
Analysis providing profile information.
Definition: ProfileSummaryInfo.h:39
BasicBlock.h
uint64_t
ProfileSummaryInfo.h
llvm::sampleprof::FunctionSamples
Representation of the samples collected for a function.
Definition: SampleProf.h:688
llvm::DenseMap< const FunctionSamples *, BodySampleCoverageMap >
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::sampleprofutil::SampleCoverageTracker::clear
void clear()
Definition: SampleProfileLoaderBaseUtil.h:54
llvm::SampleProfileSampleCoverage
cl::opt< unsigned > SampleProfileSampleCoverage
uint32_t
llvm::sampleprofutil::callsiteIsHot
bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI, bool ProfAccForSymsInList)
Return true if the given callsite is hot wrt to hot cutoff threshold.
Definition: SampleProfileLoaderBaseUtil.cpp:56
Function.h
llvm::sampleprofutil::SampleCoverageTracker
Definition: SampleProfileLoaderBaseUtil.h:41
ModuleUtils.h
llvm::SampleProfileRecordCoverage
cl::opt< unsigned > SampleProfileRecordCoverage
llvm::SampleProfileMaxPropagateIterations
cl::opt< unsigned > SampleProfileMaxPropagateIterations
llvm::NoWarnSampleUnused
cl::opt< bool > NoWarnSampleUnused