blob: 453a278a7f3f7a49d2c8a21c178ba0d1452b5faa [file] [log] [blame]
Easwaran Ramane5a17e32016-05-19 21:07:12 +00001//=-- Profilesummary.cpp - Profile summary support --------------------------=//
Easwaran Ramand68aae22016-02-04 23:34:31 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Easwaran Ramand68aae22016-02-04 23:34:31 +00006//
7//===----------------------------------------------------------------------===//
8//
Easwaran Ramane5a17e32016-05-19 21:07:12 +00009// This file contains support for converting profile summary data from/to
10// metadata.
Easwaran Ramand68aae22016-02-04 23:34:31 +000011//
12//===----------------------------------------------------------------------===//
13
Easwaran Ramane5a17e32016-05-19 21:07:12 +000014#include "llvm/IR/ProfileSummary.h"
Dehao Chenf84b6302016-02-23 03:39:24 +000015#include "llvm/IR/Attributes.h"
Easwaran Raman7c4f25d2016-03-01 18:30:58 +000016#include "llvm/IR/Constants.h"
Dehao Chenf84b6302016-02-23 03:39:24 +000017#include "llvm/IR/Function.h"
Easwaran Raman7c4f25d2016-03-01 18:30:58 +000018#include "llvm/IR/Metadata.h"
19#include "llvm/IR/Type.h"
Easwaran Raman7c4f25d2016-03-01 18:30:58 +000020#include "llvm/Support/Casting.h"
Simon Pilgrim7a55d982020-06-23 09:43:09 +010021#include "llvm/Support/Format.h"
Easwaran Ramand68aae22016-02-04 23:34:31 +000022
23using namespace llvm;
24
Easwaran Raman7c4f25d2016-03-01 18:30:58 +000025// Return an MDTuple with two elements. The first element is a string Key and
26// the second is a uint64_t Value.
27static 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),
31 ConstantAsMetadata::get(ConstantInt::get(Int64Ty, Val))};
32 return MDTuple::get(Context, Ops);
33}
34
Hiroshi Yamauchib5c59d72020-05-14 10:21:53 -070035static Metadata *getKeyFPValMD(LLVMContext &Context, const char *Key,
36 double Val) {
37 Type *DoubleTy = Type::getDoubleTy(Context);
38 Metadata *Ops[2] = {MDString::get(Context, Key),
39 ConstantAsMetadata::get(ConstantFP::get(DoubleTy, Val))};
40 return MDTuple::get(Context, Ops);
41}
42
Easwaran Raman7c4f25d2016-03-01 18:30:58 +000043// Return an MDTuple with two elements. The first element is a string Key and
44// the second is a string Value.
45static Metadata *getKeyValMD(LLVMContext &Context, const char *Key,
46 const char *Val) {
47 Metadata *Ops[2] = {MDString::get(Context, Key), MDString::get(Context, 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.
56Metadata *ProfileSummary::getDetailedSummaryMD(LLVMContext &Context) {
57 std::vector<Metadata *> Entries;
58 Type *Int32Ty = Type::getInt32Ty(Context);
59 Type *Int64Ty = Type::getInt64Ty(Context);
60 for (auto &Entry : DetailedSummary) {
61 Metadata *EntryMD[3] = {
62 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Entry.Cutoff)),
63 ConstantAsMetadata::get(ConstantInt::get(Int64Ty, Entry.MinCount)),
64 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Entry.NumCounts))};
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.
Wei Mi10b57ca2020-04-21 14:32:28 -070077// IsPartialProfile is an optional field and \p AddPartialField will decide
78// whether to add a field for it.
Hiroshi Yamauchib5c59d72020-05-14 10:21:53 -070079// PartialProfileRatio is an optional field and \p AddPartialProfileRatioField
80// will decide whether to add a field for it.
81Metadata *ProfileSummary::getMD(LLVMContext &Context, bool AddPartialField,
82 bool AddPartialProfileRatioField) {
Rong Xua6ff69f2019-02-28 19:55:07 +000083 const char *KindStr[3] = {"InstrProf", "CSInstrProf", "SampleProfile"};
Wei Mi10b57ca2020-04-21 14:32:28 -070084 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()));
Hiroshi Yamauchib5c59d72020-05-14 10:21:53 -070097 if (AddPartialProfileRatioField)
98 Components.push_back(getKeyFPValMD(Context, "PartialProfileRatio",
99 getPartialProfileRatio()));
Wei Mi10b57ca2020-04-21 14:32:28 -0700100 Components.push_back(getDetailedSummaryMD(Context));
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000101 return MDTuple::get(Context, Components);
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000102}
103
Hiroshi Yamauchib5c59d72020-05-14 10:21:53 -0700104// Get the value metadata for the input MD/Key.
105static ConstantAsMetadata *getValMD(MDTuple *MD, const char *Key) {
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000106 if (!MD)
Hiroshi Yamauchib5c59d72020-05-14 10:21:53 -0700107 return nullptr;
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000108 if (MD->getNumOperands() != 2)
Hiroshi Yamauchib5c59d72020-05-14 10:21:53 -0700109 return nullptr;
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000110 MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0));
111 ConstantAsMetadata *ValMD = dyn_cast<ConstantAsMetadata>(MD->getOperand(1));
112 if (!KeyMD || !ValMD)
Hiroshi Yamauchib5c59d72020-05-14 10:21:53 -0700113 return nullptr;
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000114 if (!KeyMD->getString().equals(Key))
Hiroshi Yamauchib5c59d72020-05-14 10:21:53 -0700115 return nullptr;
116 return ValMD;
117}
118
119// Parse an MDTuple representing (Key, Val) pair.
120static 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
128static 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;
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000134}
135
136// Check if an MDTuple represents a (Key, Val) pair.
137static 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.
150static 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
Hiroshi Yamauchif9a61632020-05-19 12:12:26 -0700179// Get the value of an optional field. Increment 'Idx' if it was present. Return
180// true if we can move onto the next field.
181template <typename ValueType>
182static 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
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000195ProfileSummary *ProfileSummary::getFromMD(Metadata *MD) {
George Burgess IV85fc4c32018-04-12 19:48:05 +0000196 MDTuple *Tuple = dyn_cast_or_null<MDTuple>(MD);
Hiroshi Yamauchib5c59d72020-05-14 10:21:53 -0700197 if (!Tuple || Tuple->getNumOperands() < 8 || Tuple->getNumOperands() > 10)
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000198 return nullptr;
199
Hiroshi Yamauchif9a61632020-05-19 12:12:26 -0700200 unsigned I = 0;
201 auto &FormatMD = Tuple->getOperand(I++);
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000202 ProfileSummary::Kind SummaryKind;
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000203 if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",
204 "SampleProfile"))
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000205 SummaryKind = PSK_Sample;
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000206 else if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",
207 "InstrProf"))
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000208 SummaryKind = PSK_Instr;
Rong Xua6ff69f2019-02-28 19:55:07 +0000209 else if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",
210 "CSInstrProf"))
211 SummaryKind = PSK_CSInstr;
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000212 else
213 return nullptr;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000214
215 uint64_t NumCounts, TotalCount, NumFunctions, MaxFunctionCount, MaxCount,
216 MaxInternalCount;
Hiroshi Yamauchif9a61632020-05-19 12:12:26 -0700217 if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "TotalCount",
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000218 TotalCount))
219 return nullptr;
Hiroshi Yamauchif9a61632020-05-19 12:12:26 -0700220 if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "MaxCount", MaxCount))
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000221 return nullptr;
Hiroshi Yamauchif9a61632020-05-19 12:12:26 -0700222 if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "MaxInternalCount",
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000223 MaxInternalCount))
224 return nullptr;
Hiroshi Yamauchif9a61632020-05-19 12:12:26 -0700225 if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "MaxFunctionCount",
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000226 MaxFunctionCount))
227 return nullptr;
Hiroshi Yamauchif9a61632020-05-19 12:12:26 -0700228 if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "NumCounts",
Wei Mi10b57ca2020-04-21 14:32:28 -0700229 NumCounts))
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000230 return nullptr;
Hiroshi Yamauchif9a61632020-05-19 12:12:26 -0700231 if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "NumFunctions",
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000232 NumFunctions))
233 return nullptr;
Wei Mi10b57ca2020-04-21 14:32:28 -0700234
Hiroshi Yamauchif9a61632020-05-19 12:12:26 -0700235 // 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;
Hiroshi Yamauchib5c59d72020-05-14 10:21:53 -0700239 double PartialProfileRatio = 0;
240 if (!getOptionalVal(Tuple, I, "PartialProfileRatio", PartialProfileRatio))
241 return nullptr;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000242
243 SummaryEntryVector Summary;
Hiroshi Yamauchif9a61632020-05-19 12:12:26 -0700244 if (!getSummaryFromMD(dyn_cast<MDTuple>(Tuple->getOperand(I++)), Summary))
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000245 return nullptr;
George Burgess IV49331242018-04-12 20:54:05 +0000246 return new ProfileSummary(SummaryKind, std::move(Summary), TotalCount,
247 MaxCount, MaxInternalCount, MaxFunctionCount,
Hiroshi Yamauchib5c59d72020-05-14 10:21:53 -0700248 NumCounts, NumFunctions, IsPartialProfile,
249 PartialProfileRatio);
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000250}
Wenlei He17fc6512020-05-02 08:34:10 -0700251
252void ProfileSummary::printSummary(raw_ostream &OS) {
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
260void ProfileSummary::printDetailedSummary(raw_ostream &OS) {
261 OS << "Detailed summary:\n";
Simon Pilgrim53f17482020-09-21 16:28:10 +0100262 for (const auto &Entry : DetailedSummary) {
Wenlei He17fc6512020-05-02 08:34:10 -0700263 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}