LLVM  14.0.0git
ProfileCommon.h
Go to the documentation of this file.
1 //===- ProfileCommon.h - Common profiling APIs. -----------------*- 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 // This file contains data structures and functions common to both instrumented
10 // and sample profiling.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_PROFILEDATA_PROFILECOMMON_H
15 #define LLVM_PROFILEDATA_PROFILECOMMON_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/IR/ProfileSummary.h"
21 #include "llvm/Support/Error.h"
22 #include <algorithm>
23 #include <cstdint>
24 #include <functional>
25 #include <map>
26 #include <memory>
27 #include <vector>
28 
29 namespace llvm {
30 
31 namespace sampleprof {
32 
33 class FunctionSamples;
34 
35 } // end namespace sampleprof
36 
37 inline const char *getHotSectionPrefix() { return "hot"; }
38 inline const char *getUnlikelySectionPrefix() { return "unlikely"; }
39 
41 private:
42  /// We keep track of the number of times a count (block count or samples)
43  /// appears in the profile. The map is kept sorted in the descending order of
44  /// counts.
45  std::map<uint64_t, uint32_t, std::greater<uint64_t>> CountFrequencies;
46  std::vector<uint32_t> DetailedSummaryCutoffs;
47 
48 protected:
55 
56  ProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
57  : DetailedSummaryCutoffs(std::move(Cutoffs)) {}
58  ~ProfileSummaryBuilder() = default;
59 
60  inline void addCount(uint64_t Count);
62 
63 public:
64  /// A vector of useful cutoff values for detailed summary.
66 
67  /// Find the summary entry for a desired percentile of counts.
68  static const ProfileSummaryEntry &
72 };
73 
75  uint64_t MaxInternalBlockCount = 0;
76 
77  inline void addEntryCount(uint64_t Count);
78  inline void addInternalCount(uint64_t Count);
79 
80 public:
81  InstrProfSummaryBuilder(std::vector<uint32_t> Cutoffs)
82  : ProfileSummaryBuilder(std::move(Cutoffs)) {}
83 
84  void addRecord(const InstrProfRecord &);
85  std::unique_ptr<ProfileSummary> getSummary();
86 };
87 
89 public:
90  SampleProfileSummaryBuilder(std::vector<uint32_t> Cutoffs)
91  : ProfileSummaryBuilder(std::move(Cutoffs)) {}
92 
94  bool isCallsiteSample = false);
95  std::unique_ptr<ProfileSummary>
97  std::unique_ptr<ProfileSummary> getSummary();
98 };
99 
100 /// This is called when a count is seen in the profile.
102  TotalCount += Count;
103  if (Count > MaxCount)
104  MaxCount = Count;
105  NumCounts++;
106  CountFrequencies[Count]++;
107 }
108 
109 } // end namespace llvm
110 
111 #endif // LLVM_PROFILEDATA_PROFILECOMMON_H
llvm::ProfileSummaryBuilder::getEntryForPercentile
static const ProfileSummaryEntry & getEntryForPercentile(SummaryEntryVector &DS, uint64_t Percentile)
Find the summary entry for a desired percentile of counts.
Definition: ProfileSummaryBuilder.cpp:83
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::ProfileSummaryBuilder::getColdCountThreshold
static uint64_t getColdCountThreshold(SummaryEntryVector &DS)
Definition: ProfileSummaryBuilder.cpp:166
ProfileSummary.h
Error.h
llvm::sampleprof::SampleProfileMap
std::unordered_map< SampleContext, FunctionSamples, SampleContext::Hash > SampleProfileMap
Definition: SampleProf.h:1091
llvm::SampleProfileSummaryBuilder
Definition: ProfileCommon.h:88
llvm::InstrProfRecord
Profiling information for a single function.
Definition: InstrProf.h:691
llvm::ProfileSummaryBuilder::TotalCount
uint64_t TotalCount
Definition: ProfileCommon.h:50
llvm::ProfileSummaryBuilder::~ProfileSummaryBuilder
~ProfileSummaryBuilder()=default
llvm::ProfileSummaryBuilder::NumFunctions
uint32_t NumFunctions
Definition: ProfileCommon.h:54
llvm::ProfileSummaryBuilder::ProfileSummaryBuilder
ProfileSummaryBuilder(std::vector< uint32_t > Cutoffs)
Definition: ProfileCommon.h:56
SampleProf.h
llvm::InstrProfSummaryBuilder::getSummary
std::unique_ptr< ProfileSummary > getSummary()
Definition: ProfileSummaryBuilder.cpp:211
llvm::SIInstrFlags::DS
@ DS
Definition: SIDefines.h:59
InstrProf.h
llvm::ProfileSummaryBuilder::MaxFunctionCount
uint64_t MaxFunctionCount
Definition: ProfileCommon.h:52
llvm::ProfileSummaryBuilder::MaxCount
uint64_t MaxCount
Definition: ProfileCommon.h:51
llvm::ProfileSummaryBuilder::getHotCountThreshold
static uint64_t getHotCountThreshold(SummaryEntryVector &DS)
Definition: ProfileSummaryBuilder.cpp:157
llvm::SampleProfileSummaryBuilder::computeSummaryForProfiles
std::unique_ptr< ProfileSummary > computeSummaryForProfiles(const sampleprof::SampleProfileMap &Profiles)
Definition: ProfileSummaryBuilder.cpp:183
llvm::ProfileSummaryBuilder::computeDetailedSummary
void computeDetailedSummary()
Definition: ProfileSummaryBuilder.cpp:125
llvm::getHotSectionPrefix
const char * getHotSectionPrefix()
Definition: ProfileCommon.h:37
uint64_t
llvm::sampleprof::FunctionSamples
Representation of the samples collected for a function.
Definition: SampleProf.h:684
llvm::InstrProfSummaryBuilder
Definition: ProfileCommon.h:74
ArrayRef.h
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1605
llvm::InstrProfSummaryBuilder::InstrProfSummaryBuilder
InstrProfSummaryBuilder(std::vector< uint32_t > Cutoffs)
Definition: ProfileCommon.h:81
llvm::X86AS::FS
@ FS
Definition: X86.h:188
llvm::ProfileSummaryBuilder
Definition: ProfileCommon.h:40
llvm::ArrayRef< uint32_t >
uint32_t
llvm::ProfileSummaryEntry
Definition: ProfileSummary.h:33
llvm::SummaryEntryVector
std::vector< ProfileSummaryEntry > SummaryEntryVector
Definition: ProfileSummary.h:43
llvm::getUnlikelySectionPrefix
const char * getUnlikelySectionPrefix()
Definition: ProfileCommon.h:38
llvm::SampleProfileSummaryBuilder::getSummary
std::unique_ptr< ProfileSummary > getSummary()
Definition: ProfileSummaryBuilder.cpp:175
llvm::ProfileSummaryBuilder::NumCounts
uint32_t NumCounts
Definition: ProfileCommon.h:53
std
Definition: BitVector.h:838
llvm::SampleProfileSummaryBuilder::SampleProfileSummaryBuilder
SampleProfileSummaryBuilder(std::vector< uint32_t > Cutoffs)
Definition: ProfileCommon.h:90
llvm::SampleProfileSummaryBuilder::addRecord
void addRecord(const sampleprof::FunctionSamples &FS, bool isCallsiteSample=false)
Definition: ProfileSummaryBuilder.cpp:107
llvm::ProfileSummaryBuilder::addCount
void addCount(uint64_t Count)
This is called when a count is seen in the profile.
Definition: ProfileCommon.h:101
llvm::InstrProfSummaryBuilder::addRecord
void addRecord(const InstrProfRecord &)
Definition: ProfileSummaryBuilder.cpp:95
llvm::ProfileSummaryBuilder::DetailedSummary
SummaryEntryVector DetailedSummary
Definition: ProfileCommon.h:49
llvm::ProfileSummaryBuilder::DefaultCutoffs
static const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
Definition: ProfileCommon.h:65