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