LLVM  13.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/DebugLoc.h"
23 #include "llvm/IR/Function.h"
26 
27 namespace llvm {
28 using namespace sampleprof;
29 
31 
36 
37 namespace sampleprofutil {
38 
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;
48  uint64_t getTotalUsedSamples() const { return TotalUsedSamples; }
49  uint64_t countBodySamples(const FunctionSamples *FS,
50  ProfileSummaryInfo *PSI) const;
51 
52  void clear() {
53  SampleCoverage.clear();
54  TotalUsedSamples = 0;
55  }
56  void setProfAccForSymsInList(bool V) { ProfAccForSymsInList = V; }
57 
58 private:
59  using BodySampleCoverageMap = std::map<LineLocation, unsigned>;
60  using FunctionSamplesCoverageMap =
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
llvm
Definition: AllocatorList.h:23
DenseMap.h
llvm::NoWarnSampleUnused
cl::opt< bool > NoWarnSampleUnused
Definition: SampleProfileLoaderBaseUtil.h:35
CommandLine.h
llvm::sampleprofutil::SampleCoverageTracker::setProfAccForSymsInList
void setProfAccForSymsInList(bool V)
Definition: SampleProfileLoaderBaseUtil.h:56
SampleProf.h
DebugLoc.h
llvm::sampleprofutil::SampleCoverageTracker::getTotalUsedSamples
uint64_t getTotalUsedSamples() const
Definition: SampleProfileLoaderBaseUtil.h:48
CFG.h
llvm::ProfileSummaryInfo
Analysis providing profile information.
Definition: ProfileSummaryInfo.h:39
BasicBlock.h
llvm::cl::opt
Definition: CommandLine.h:1422
ProfileSummaryInfo.h
llvm::sampleprof::FunctionSamples
Representation of the samples collected for a function.
Definition: SampleProf.h:533
llvm::SampleProfileRecordCoverage
cl::opt< unsigned > SampleProfileRecordCoverage
Definition: SampleProfileLoaderBaseUtil.h:33
llvm::DenseMap< const FunctionSamples *, BodySampleCoverageMap >
llvm::sampleprofutil::SampleCoverageTracker::clear
void clear()
Definition: SampleProfileLoaderBaseUtil.h:52
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:39
llvm::SampleProfileSampleCoverage
cl::opt< unsigned > SampleProfileSampleCoverage
Definition: SampleProfileLoaderBaseUtil.h:34
llvm::X86AS::FS
@ FS
Definition: X86.h:188
llvm::SampleProfileMaxPropagateIterations
cl::opt< unsigned > SampleProfileMaxPropagateIterations
Definition: SampleProfileLoaderBaseUtil.h:30