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