LLVM  13.0.0git
ProfileSummary.cpp
Go to the documentation of this file.
1 //=-- Profilesummary.cpp - Profile summary support --------------------------=//
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 support for converting profile summary data from/to
10 // metadata.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/IR/ProfileSummary.h"
15 #include "llvm/IR/Attributes.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Metadata.h"
19 #include "llvm/IR/Type.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/Format.h"
22 
23 using namespace llvm;
24 
25 // Return an MDTuple with two elements. The first element is a string Key and
26 // the second is a uint64_t Value.
27 static Metadata *getKeyValMD(LLVMContext &Context, const char *Key,
28  uint64_t Val) {
29  Type *Int64Ty = Type::getInt64Ty(Context);
30  Metadata *Ops[2] = {MDString::get(Context, Key),
32  return MDTuple::get(Context, Ops);
33 }
34 
36  double Val) {
37  Type *DoubleTy = Type::getDoubleTy(Context);
38  Metadata *Ops[2] = {MDString::get(Context, Key),
40  return MDTuple::get(Context, Ops);
41 }
42 
43 // Return an MDTuple with two elements. The first element is a string Key and
44 // the second is a string Value.
45 static Metadata *getKeyValMD(LLVMContext &Context, const char *Key,
46  const char *Val) {
48  return MDTuple::get(Context, Ops);
49 }
50 
51 // This returns an MDTuple representing the detiled summary. The tuple has two
52 // elements: a string "DetailedSummary" and an MDTuple representing the value
53 // of the detailed summary. Each element of this tuple is again an MDTuple whose
54 // elements are the (Cutoff, MinCount, NumCounts) triplet of the
55 // DetailedSummaryEntry.
56 Metadata *ProfileSummary::getDetailedSummaryMD(LLVMContext &Context) {
57  std::vector<Metadata *> Entries;
59  Type *Int64Ty = Type::getInt64Ty(Context);
60  for (auto &Entry : DetailedSummary) {
61  Metadata *EntryMD[3] = {
63  ConstantAsMetadata::get(ConstantInt::get(Int64Ty, Entry.MinCount)),
65  Entries.push_back(MDTuple::get(Context, EntryMD));
66  }
67  Metadata *Ops[2] = {MDString::get(Context, "DetailedSummary"),
68  MDTuple::get(Context, Entries)};
69  return MDTuple::get(Context, Ops);
70 }
71 
72 // This returns an MDTuple representing this ProfileSummary object. The first
73 // entry of this tuple is another MDTuple of two elements: a string
74 // "ProfileFormat" and a string representing the format ("InstrProf" or
75 // "SampleProfile"). The rest of the elements of the outer MDTuple are specific
76 // to the kind of profile summary as returned by getFormatSpecificMD.
77 // IsPartialProfile is an optional field and \p AddPartialField will decide
78 // whether to add a field for it.
79 // PartialProfileRatio is an optional field and \p AddPartialProfileRatioField
80 // will decide whether to add a field for it.
82  bool AddPartialProfileRatioField) {
83  const char *KindStr[3] = {"InstrProf", "CSInstrProf", "SampleProfile"};
84  SmallVector<Metadata *, 16> Components;
85  Components.push_back(getKeyValMD(Context, "ProfileFormat", KindStr[PSK]));
86  Components.push_back(getKeyValMD(Context, "TotalCount", getTotalCount()));
87  Components.push_back(getKeyValMD(Context, "MaxCount", getMaxCount()));
88  Components.push_back(
89  getKeyValMD(Context, "MaxInternalCount", getMaxInternalCount()));
90  Components.push_back(
91  getKeyValMD(Context, "MaxFunctionCount", getMaxFunctionCount()));
92  Components.push_back(getKeyValMD(Context, "NumCounts", getNumCounts()));
93  Components.push_back(getKeyValMD(Context, "NumFunctions", getNumFunctions()));
94  if (AddPartialField)
95  Components.push_back(
96  getKeyValMD(Context, "IsPartialProfile", isPartialProfile()));
97  if (AddPartialProfileRatioField)
98  Components.push_back(getKeyFPValMD(Context, "PartialProfileRatio",
100  Components.push_back(getDetailedSummaryMD(Context));
101  return MDTuple::get(Context, Components);
102 }
103 
104 // Get the value metadata for the input MD/Key.
105 static ConstantAsMetadata *getValMD(MDTuple *MD, const char *Key) {
106  if (!MD)
107  return nullptr;
108  if (MD->getNumOperands() != 2)
109  return nullptr;
110  MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0));
111  ConstantAsMetadata *ValMD = dyn_cast<ConstantAsMetadata>(MD->getOperand(1));
112  if (!KeyMD || !ValMD)
113  return nullptr;
114  if (!KeyMD->getString().equals(Key))
115  return nullptr;
116  return ValMD;
117 }
118 
119 // Parse an MDTuple representing (Key, Val) pair.
120 static bool getVal(MDTuple *MD, const char *Key, uint64_t &Val) {
121  if (auto *ValMD = getValMD(MD, Key)) {
122  Val = cast<ConstantInt>(ValMD->getValue())->getZExtValue();
123  return true;
124  }
125  return false;
126 }
127 
128 static bool getVal(MDTuple *MD, const char *Key, double &Val) {
129  if (auto *ValMD = getValMD(MD, Key)) {
130  Val = cast<ConstantFP>(ValMD->getValue())->getValueAPF().convertToDouble();
131  return true;
132  }
133  return false;
134 }
135 
136 // Check if an MDTuple represents a (Key, Val) pair.
137 static bool isKeyValuePair(MDTuple *MD, const char *Key, const char *Val) {
138  if (!MD || MD->getNumOperands() != 2)
139  return false;
140  MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0));
141  MDString *ValMD = dyn_cast<MDString>(MD->getOperand(1));
142  if (!KeyMD || !ValMD)
143  return false;
144  if (!KeyMD->getString().equals(Key) || !ValMD->getString().equals(Val))
145  return false;
146  return true;
147 }
148 
149 // Parse an MDTuple representing detailed summary.
150 static bool getSummaryFromMD(MDTuple *MD, SummaryEntryVector &Summary) {
151  if (!MD || MD->getNumOperands() != 2)
152  return false;
153  MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0));
154  if (!KeyMD || !KeyMD->getString().equals("DetailedSummary"))
155  return false;
156  MDTuple *EntriesMD = dyn_cast<MDTuple>(MD->getOperand(1));
157  if (!EntriesMD)
158  return false;
159  for (auto &&MDOp : EntriesMD->operands()) {
160  MDTuple *EntryMD = dyn_cast<MDTuple>(MDOp);
161  if (!EntryMD || EntryMD->getNumOperands() != 3)
162  return false;
163  ConstantAsMetadata *Op0 =
164  dyn_cast<ConstantAsMetadata>(EntryMD->getOperand(0));
165  ConstantAsMetadata *Op1 =
166  dyn_cast<ConstantAsMetadata>(EntryMD->getOperand(1));
167  ConstantAsMetadata *Op2 =
168  dyn_cast<ConstantAsMetadata>(EntryMD->getOperand(2));
169 
170  if (!Op0 || !Op1 || !Op2)
171  return false;
172  Summary.emplace_back(cast<ConstantInt>(Op0->getValue())->getZExtValue(),
173  cast<ConstantInt>(Op1->getValue())->getZExtValue(),
174  cast<ConstantInt>(Op2->getValue())->getZExtValue());
175  }
176  return true;
177 }
178 
179 // Get the value of an optional field. Increment 'Idx' if it was present. Return
180 // true if we can move onto the next field.
181 template <typename ValueType>
182 static bool getOptionalVal(MDTuple *Tuple, unsigned &Idx, const char *Key,
183  ValueType &Value) {
184  if (getVal(dyn_cast<MDTuple>(Tuple->getOperand(Idx)), Key, Value)) {
185  Idx++;
186  // Need to make sure when the key is present, we won't step over the bound
187  // of Tuple operand array. Since (non-optional) DetailedSummary always comes
188  // last, the next entry in the tuple operand array must exist.
189  return Idx < Tuple->getNumOperands();
190  }
191  // It was absent, keep going.
192  return true;
193 }
194 
196  MDTuple *Tuple = dyn_cast_or_null<MDTuple>(MD);
197  if (!Tuple || Tuple->getNumOperands() < 8 || Tuple->getNumOperands() > 10)
198  return nullptr;
199 
200  unsigned I = 0;
201  auto &FormatMD = Tuple->getOperand(I++);
202  ProfileSummary::Kind SummaryKind;
203  if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",
204  "SampleProfile"))
205  SummaryKind = PSK_Sample;
206  else if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",
207  "InstrProf"))
208  SummaryKind = PSK_Instr;
209  else if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",
210  "CSInstrProf"))
211  SummaryKind = PSK_CSInstr;
212  else
213  return nullptr;
214 
215  uint64_t NumCounts, TotalCount, NumFunctions, MaxFunctionCount, MaxCount,
216  MaxInternalCount;
217  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "TotalCount",
218  TotalCount))
219  return nullptr;
220  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "MaxCount", MaxCount))
221  return nullptr;
222  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "MaxInternalCount",
223  MaxInternalCount))
224  return nullptr;
225  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "MaxFunctionCount",
226  MaxFunctionCount))
227  return nullptr;
228  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "NumCounts",
229  NumCounts))
230  return nullptr;
231  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "NumFunctions",
232  NumFunctions))
233  return nullptr;
234 
235  // Optional fields. Need to initialize because the fields are optional.
236  uint64_t IsPartialProfile = 0;
237  if (!getOptionalVal(Tuple, I, "IsPartialProfile", IsPartialProfile))
238  return nullptr;
239  double PartialProfileRatio = 0;
240  if (!getOptionalVal(Tuple, I, "PartialProfileRatio", PartialProfileRatio))
241  return nullptr;
242 
243  SummaryEntryVector Summary;
244  if (!getSummaryFromMD(dyn_cast<MDTuple>(Tuple->getOperand(I++)), Summary))
245  return nullptr;
246  return new ProfileSummary(SummaryKind, std::move(Summary), TotalCount,
247  MaxCount, MaxInternalCount, MaxFunctionCount,
248  NumCounts, NumFunctions, IsPartialProfile,
249  PartialProfileRatio);
250 }
251 
253  OS << "Total functions: " << NumFunctions << "\n";
254  OS << "Maximum function count: " << MaxFunctionCount << "\n";
255  OS << "Maximum block count: " << MaxCount << "\n";
256  OS << "Total number of blocks: " << NumCounts << "\n";
257  OS << "Total count: " << TotalCount << "\n";
258 }
259 
261  OS << "Detailed summary:\n";
262  for (const auto &Entry : DetailedSummary) {
263  OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount
264  << " account for "
265  << format("%0.6g", (float)Entry.Cutoff / Scale * 100)
266  << " percentage of the total counts.\n";
267  }
268 }
Int32Ty
IntegerType * Int32Ty
Definition: NVVMIntrRange.cpp:67
llvm
Definition: AllocatorList.h:23
llvm::ProfileSummary::Kind
Kind
Definition: ProfileSummary.h:47
Metadata.h
ProfileSummary.h
llvm::ProfileSummary::getFromMD
static ProfileSummary * getFromMD(Metadata *MD)
Construct profile summary from metdata.
Definition: ProfileSummary.cpp:195
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
llvm::ProfileSummary::printSummary
void printSummary(raw_ostream &OS)
Definition: ProfileSummary.cpp:252
llvm::ProfileSummary::getTotalCount
uint64_t getTotalCount()
Definition: ProfileSummary.h:92
llvm::ConstantAsMetadata::get
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:419
Format.h
llvm::Type::getInt32Ty
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:204
llvm::MDNode::operands
op_range operands() const
Definition: Metadata.h:1100
llvm::ConstantAsMetadata::getValue
Constant * getValue() const
Definition: Metadata.h:427
llvm::MDNode::getNumOperands
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1108
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::ProfileSummary::getPartialProfileRatio
double getPartialProfileRatio()
Definition: ProfileSummary.h:97
getKeyValMD
static Metadata * getKeyValMD(LLVMContext &Context, const char *Key, uint64_t Val)
Definition: ProfileSummary.cpp:27
Constants.h
llvm::ProfileSummary::getMaxFunctionCount
uint64_t getMaxFunctionCount()
Definition: ProfileSummary.h:90
llvm::ProfileSummary::getNumFunctions
uint32_t getNumFunctions()
Definition: ProfileSummary.h:89
isKeyValuePair
static bool isKeyValuePair(MDTuple *MD, const char *Key, const char *Val)
Definition: ProfileSummary.cpp:137
llvm::Type::getDoubleTy
static Type * getDoubleTy(LLVMContext &C)
Definition: Type.cpp:192
llvm::ProfileSummary::getMD
Metadata * getMD(LLVMContext &Context, bool AddPartialField=true, bool AddPartialProfileRatioField=true)
Return summary information as metadata.
Definition: ProfileSummary.cpp:81
llvm::MDTuple
Tuple of metadata.
Definition: Metadata.h:1139
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:481
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::ConstantInt::get
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:885
getSummaryFromMD
static bool getSummaryFromMD(MDTuple *MD, SummaryEntryVector &Summary)
Definition: ProfileSummary.cpp:150
llvm::ProfileSummary::PSK_Sample
@ PSK_Sample
Definition: ProfileSummary.h:47
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
getValMD
static ConstantAsMetadata * getValMD(MDTuple *MD, const char *Key)
Definition: ProfileSummary.cpp:105
Type.h
llvm::StringRef::equals
LLVM_NODISCARD bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:190
llvm::MDNode::getOperand
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1102
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::ProfileSummary::getMaxCount
uint64_t getMaxCount()
Definition: ProfileSummary.h:93
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::ProfileSummary
Definition: ProfileSummary.h:45
llvm::MDString::get
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:467
llvm::MDTuple::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1165
getOptionalVal
static bool getOptionalVal(MDTuple *Tuple, unsigned &Idx, const char *Key, ValueType &Value)
Definition: ProfileSummary.cpp:182
llvm::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
llvm::ProfileSummary::PSK_CSInstr
@ PSK_CSInstr
Definition: ProfileSummary.h:47
llvm::SummaryEntryVector
std::vector< ProfileSummaryEntry > SummaryEntryVector
Definition: ProfileSummary.h:43
llvm::PointerUnion< const Value *, const PseudoSourceValue * >
Attributes.h
llvm::ConstantAsMetadata
Definition: Metadata.h:412
getKeyFPValMD
static Metadata * getKeyFPValMD(LLVMContext &Context, const char *Key, double Val)
Definition: ProfileSummary.cpp:35
llvm::Type::getInt64Ty
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:205
llvm::ConstantFP::get
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
Definition: Constants.cpp:932
Casting.h
Function.h
llvm::ProfileSummary::getMaxInternalCount
uint64_t getMaxInternalCount()
Definition: ProfileSummary.h:94
llvm::ProfileSummary::getNumCounts
uint32_t getNumCounts()
Definition: ProfileSummary.h:91
getVal
static bool getVal(MDTuple *MD, const char *Key, uint64_t &Val)
Definition: ProfileSummary.cpp:120
llvm::ProfileSummary::PSK_Instr
@ PSK_Instr
Definition: ProfileSummary.h:47
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::isPartialProfile
bool isPartialProfile()
Definition: ProfileSummary.h:96
llvm::ProfileSummary::printDetailedSummary
void printDetailedSummary(raw_ostream &OS)
Definition: ProfileSummary.cpp:260
llvm::MDString::getString
StringRef getString() const
Definition: Metadata.cpp:477
llvm::ProfileSummary::Scale
static const int Scale
Definition: ProfileSummary.h:69
llvm::MDString
A single uniqued string.
Definition: Metadata.h:611
llvm::Value
LLVM Value Representation.
Definition: Value.h:75