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