LLVM  14.0.0git
ProfileSummary.h
Go to the documentation of this file.
1 //===- ProfileSummary.h - Profile summary data structure. -------*- 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 defines the profile summary data structure.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_IR_PROFILESUMMARY_H
14 #define LLVM_IR_PROFILESUMMARY_H
15 
16 #include <algorithm>
17 #include <cassert>
18 #include <cstdint>
19 #include <vector>
20 
21 namespace llvm {
22 
23 class LLVMContext;
24 class Metadata;
25 class raw_ostream;
26 
27 // The profile summary is one or more (Cutoff, MinCount, NumCounts) triplets.
28 // The semantics of counts depend on the type of profile. For instrumentation
29 // profile, counts are block counts and for sample profile, counts are
30 // per-line samples. Given a target counts percentile, we compute the minimum
31 // number of counts needed to reach this target and the minimum among these
32 // counts.
34  const uint32_t Cutoff; ///< The required percentile of counts.
35  const uint64_t MinCount; ///< The minimum count for this percentile.
36  const uint64_t NumCounts; ///< Number of counts >= the minimum count.
37 
38  ProfileSummaryEntry(uint32_t TheCutoff, uint64_t TheMinCount,
39  uint64_t TheNumCounts)
40  : Cutoff(TheCutoff), MinCount(TheMinCount), NumCounts(TheNumCounts) {}
41 };
42 
43 using SummaryEntryVector = std::vector<ProfileSummaryEntry>;
44 
46 public:
48 
49 private:
50  const Kind PSK;
51  SummaryEntryVector DetailedSummary;
52  uint64_t TotalCount, MaxCount, MaxInternalCount, MaxFunctionCount;
53  uint32_t NumCounts, NumFunctions;
54  /// If 'Partial' is false, it means the profile being used to optimize
55  /// a target is collected from the same target.
56  /// If 'Partial' is true, it means the profile is for common/shared
57  /// code. The common profile is usually merged from profiles collected
58  /// from running other targets.
59  bool Partial = false;
60  /// This approximately represents the ratio of the number of profile counters
61  /// of the program being built to the number of profile counters in the
62  /// partial sample profile. When 'Partial' is false, it is undefined. This is
63  /// currently only available under thin LTO mode.
64  double PartialProfileRatio = 0.0;
65  /// Return detailed summary as metadata.
66  Metadata *getDetailedSummaryMD(LLVMContext &Context);
67 
68 public:
69  static const int Scale = 1000000;
70 
72  uint64_t TotalCount, uint64_t MaxCount,
73  uint64_t MaxInternalCount, uint64_t MaxFunctionCount,
74  uint32_t NumCounts, uint32_t NumFunctions,
75  bool Partial = false, double PartialProfileRatio = 0)
76  : PSK(K), DetailedSummary(std::move(DetailedSummary)),
77  TotalCount(TotalCount), MaxCount(MaxCount),
78  MaxInternalCount(MaxInternalCount), MaxFunctionCount(MaxFunctionCount),
79  NumCounts(NumCounts), NumFunctions(NumFunctions), Partial(Partial),
80  PartialProfileRatio(PartialProfileRatio) {}
81 
82  Kind getKind() const { return PSK; }
83  /// Return summary information as metadata.
84  Metadata *getMD(LLVMContext &Context, bool AddPartialField = true,
85  bool AddPartialProfileRatioField = true);
86  /// Construct profile summary from metdata.
87  static ProfileSummary *getFromMD(Metadata *MD);
88  SummaryEntryVector &getDetailedSummary() { return DetailedSummary; }
89  uint32_t getNumFunctions() const { return NumFunctions; }
90  uint64_t getMaxFunctionCount() const { return MaxFunctionCount; }
91  uint32_t getNumCounts() const { return NumCounts; }
92  uint64_t getTotalCount() const { return TotalCount; }
93  uint64_t getMaxCount() const { return MaxCount; }
94  uint64_t getMaxInternalCount() const { return MaxInternalCount; }
95  void setPartialProfile(bool PP) { Partial = PP; }
96  bool isPartialProfile() const { return Partial; }
97  double getPartialProfileRatio() const { return PartialProfileRatio; }
98  void setPartialProfileRatio(double R) {
99  assert(isPartialProfile() && "Unexpected when not partial profile");
100  PartialProfileRatio = R;
101  }
102  void printSummary(raw_ostream &OS) const;
103  void printDetailedSummary(raw_ostream &OS) const;
104 };
105 
106 } // end namespace llvm
107 
108 #endif // LLVM_IR_PROFILESUMMARY_H
llvm::ProfileSummaryEntry::MinCount
const uint64_t MinCount
The minimum count for this percentile.
Definition: ProfileSummary.h:35
llvm::ProfileSummaryEntry::ProfileSummaryEntry
ProfileSummaryEntry(uint32_t TheCutoff, uint64_t TheMinCount, uint64_t TheNumCounts)
Definition: ProfileSummary.h:38
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::ProfileSummary::Kind
Kind
Definition: ProfileSummary.h:47
llvm::ProfileSummary::getFromMD
static ProfileSummary * getFromMD(Metadata *MD)
Construct profile summary from metdata.
Definition: ProfileSummary.cpp:195
llvm::ProfileSummary::setPartialProfile
void setPartialProfile(bool PP)
Definition: ProfileSummary.h:95
llvm::ProfileSummary::isPartialProfile
bool isPartialProfile() const
Definition: ProfileSummary.h:96
llvm::ProfileSummary::printDetailedSummary
void printDetailedSummary(raw_ostream &OS) const
Definition: ProfileSummary.cpp:260
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::ProfileSummary::getNumCounts
uint32_t getNumCounts() const
Definition: ProfileSummary.h:91
llvm::ProfileSummary::getPartialProfileRatio
double getPartialProfileRatio() const
Definition: ProfileSummary.h:97
llvm::ProfileSummary::getMD
Metadata * getMD(LLVMContext &Context, bool AddPartialField=true, bool AddPartialProfileRatioField=true)
Return summary information as metadata.
Definition: ProfileSummary.cpp:81
llvm::ProfileSummary::getNumFunctions
uint32_t getNumFunctions() const
Definition: ProfileSummary.h:89
llvm::ProfileSummary::getMaxInternalCount
uint64_t getMaxInternalCount() const
Definition: ProfileSummary.h:94
llvm::ProfileSummary::getTotalCount
uint64_t getTotalCount() const
Definition: ProfileSummary.h:92
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::ProfileSummary::PSK_Sample
@ PSK_Sample
Definition: ProfileSummary.h:47
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::ProfileSummary::getMaxFunctionCount
uint64_t getMaxFunctionCount() const
Definition: ProfileSummary.h:90
llvm::ProfileSummary::getKind
Kind getKind() const
Definition: ProfileSummary.h:82
llvm::ProfileSummary::printSummary
void printSummary(raw_ostream &OS) const
Definition: ProfileSummary.cpp:252
llvm::ProfileSummary::getDetailedSummary
SummaryEntryVector & getDetailedSummary()
Definition: ProfileSummary.h:88
uint64_t
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::ProfileSummary
Definition: ProfileSummary.h:45
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:1609
llvm::ProfileSummaryEntry::Cutoff
const uint32_t Cutoff
The required percentile of counts.
Definition: ProfileSummary.h:34
llvm::ProfileSummaryEntry::NumCounts
const uint64_t NumCounts
Number of counts >= the minimum count.
Definition: ProfileSummary.h:36
uint32_t
llvm::ProfileSummaryEntry
Definition: ProfileSummary.h:33
llvm::ProfileSummary::PSK_CSInstr
@ PSK_CSInstr
Definition: ProfileSummary.h:47
llvm::SummaryEntryVector
std::vector< ProfileSummaryEntry > SummaryEntryVector
Definition: ProfileSummary.h:43
std
Definition: BitVector.h:838
llvm::ProfileSummary::setPartialProfileRatio
void setPartialProfileRatio(double R)
Definition: ProfileSummary.h:98
llvm::ProfileSummary::PSK_Instr
@ PSK_Instr
Definition: ProfileSummary.h:47
LineType::Metadata
@ Metadata
llvm::ProfileSummary::ProfileSummary
ProfileSummary(Kind K, SummaryEntryVector DetailedSummary, uint64_t TotalCount, uint64_t MaxCount, uint64_t MaxInternalCount, uint64_t MaxFunctionCount, uint32_t NumCounts, uint32_t NumFunctions, bool Partial=false, double PartialProfileRatio=0)
Definition: ProfileSummary.h:71
llvm::ProfileSummary::Scale
static const int Scale
Definition: ProfileSummary.h:69
llvm::ProfileSummary::getMaxCount
uint64_t getMaxCount() const
Definition: ProfileSummary.h:93