LLVM: lib/IR/ProfileSummary.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
21
22using namespace llvm;
23
24
25
32}
33
35 double Val) {
40}
41
42
43
45 const char *Val) {
48}
49
50
51
52
53
54
56 std::vector<Metadata *> Entries;
59 for (auto &Entry : DetailedSummary) {
64 Entries.push_back(MDTuple::get(Context, EntryMD));
65 }
69}
70
71
72
73
74
75
76
77
78
79
81 bool AddPartialProfileRatioField) {
82 const char *KindStr[3] = {"InstrProf", "CSInstrProf", "SampleProfile"};
93 if (AddPartialField)
96 if (AddPartialProfileRatioField)
99 Components.push_back(getDetailedSummaryMD(Context));
101}
102
103
105 if (!MD)
106 return nullptr;
108 return nullptr;
111 if (!KeyMD || !ValMD)
112 return nullptr;
114 return nullptr;
115 return ValMD;
116}
117
118
120 if (auto *ValMD = getValMD(MD, Key)) {
121 Val = cast(ValMD->getValue())->getZExtValue();
122 return true;
123 }
124 return false;
125}
126
127static bool getVal(MDTuple *MD, const char *Key, double &Val) {
128 if (auto *ValMD = getValMD(MD, Key)) {
129 Val = cast(ValMD->getValue())->getValueAPF().convertToDouble();
130 return true;
131 }
132 return false;
133}
134
135
138 return false;
141 if (!KeyMD || !ValMD)
142 return false;
144 return false;
145 return true;
146}
147
148
151 return false;
153 if (!KeyMD || KeyMD->getString() != "DetailedSummary")
154 return false;
156 if (!EntriesMD)
157 return false;
158 for (auto &&MDOp : EntriesMD->operands()) {
159 MDTuple *EntryMD = dyn_cast(MDOp);
161 return false;
163 dyn_cast(EntryMD->getOperand(0));
165 dyn_cast(EntryMD->getOperand(1));
167 dyn_cast(EntryMD->getOperand(2));
168
169 if (!Op0 || !Op1 || !Op2)
170 return false;
171 Summary.emplace_back(cast(Op0->getValue())->getZExtValue(),
172 cast(Op1->getValue())->getZExtValue(),
173 cast(Op2->getValue())->getZExtValue());
174 }
175 return true;
176}
177
178
179
180template
185
186
187
189 }
190
191 return true;
192}
193
195 MDTuple *Tuple = dyn_cast_or_null(MD);
197 return nullptr;
198
199 unsigned I = 0;
202 if (isKeyValuePair(dyn_cast_or_null(FormatMD), "ProfileFormat",
203 "SampleProfile"))
205 else if (isKeyValuePair(dyn_cast_or_null(FormatMD), "ProfileFormat",
206 "InstrProf"))
208 else if (isKeyValuePair(dyn_cast_or_null(FormatMD), "ProfileFormat",
209 "CSInstrProf"))
211 else
212 return nullptr;
213
214 uint64_t NumCounts, TotalCount, NumFunctions, MaxFunctionCount, MaxCount,
215 MaxInternalCount;
216 if ((dyn_cast(Tuple->getOperand(I++)), "TotalCount",
217 TotalCount))
218 return nullptr;
219 if ((dyn_cast(Tuple->getOperand(I++)), "MaxCount", MaxCount))
220 return nullptr;
221 if ((dyn_cast(Tuple->getOperand(I++)), "MaxInternalCount",
222 MaxInternalCount))
223 return nullptr;
224 if ((dyn_cast(Tuple->getOperand(I++)), "MaxFunctionCount",
225 MaxFunctionCount))
226 return nullptr;
227 if ((dyn_cast(Tuple->getOperand(I++)), "NumCounts",
228 NumCounts))
229 return nullptr;
230 if ((dyn_cast(Tuple->getOperand(I++)), "NumFunctions",
231 NumFunctions))
232 return nullptr;
233
234
235 uint64_t IsPartialProfile = 0;
236 if ((Tuple, I, "IsPartialProfile", IsPartialProfile))
237 return nullptr;
238 double PartialProfileRatio = 0;
239 if ((Tuple, I, "PartialProfileRatio", PartialProfileRatio))
240 return nullptr;
241
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 internal block count: " << MaxInternalCount << "\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 << format("%lu blocks (%.2f%%) with count >= %lu account for %0.6g%% of "
263 "the total counts.\n",
264 Entry.NumCounts,
265 NumCounts ? (100.f * Entry.NumCounts / NumCounts) : 0,
266 Entry.MinCount, 100.f * Entry.Cutoff / Scale);
267 }
268}
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static ConstantAsMetadata * getValMD(MDTuple *MD, const char *Key)
static Metadata * getKeyFPValMD(LLVMContext &Context, const char *Key, double Val)
static bool isKeyValuePair(MDTuple *MD, const char *Key, const char *Val)
static bool getOptionalVal(MDTuple *Tuple, unsigned &Idx, const char *Key, ValueType &Value)
static bool getSummaryFromMD(MDTuple *MD, SummaryEntryVector &Summary)
static Metadata * getKeyValMD(LLVMContext &Context, const char *Key, uint64_t Val)
static bool getVal(MDTuple *MD, const char *Key, uint64_t &Val)
This is an important class for using LLVM in a threaded context.
const MDOperand & getOperand(unsigned I) const
ArrayRef< MDOperand > operands() const
unsigned getNumOperands() const
Return number of MDNode operands.
StringRef getString() const
static MDString * get(LLVMContext &Context, StringRef Str)
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
uint64_t getTotalCount() const
uint64_t getMaxCount() const
Metadata * getMD(LLVMContext &Context, bool AddPartialField=true, bool AddPartialProfileRatioField=true)
Return summary information as metadata.
uint32_t getNumCounts() const
void printDetailedSummary(raw_ostream &OS) const
uint64_t getMaxInternalCount() const
bool isPartialProfile() const
static ProfileSummary * getFromMD(Metadata *MD)
Construct profile summary from metdata.
uint64_t getMaxFunctionCount() const
void printSummary(raw_ostream &OS) const
double getPartialProfileRatio() const
uint32_t getNumFunctions() const
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
static Type * getDoubleTy(LLVMContext &C)
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
LLVM Value Representation.
This class implements an extremely fast bulk output stream that can only output to a stream.
This is an optimization pass for GlobalISel generic memory operations.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
std::vector< ProfileSummaryEntry > SummaryEntryVector