blob: d6c3c5035dc85dca8900e56ecc48ea83c322998a [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"
Easwaran Ramand68aae22016-02-04 23:34:31 +000021
22using namespace llvm;
23
Easwaran Raman7c4f25d2016-03-01 18:30:58 +000024// Return an MDTuple with two elements. The first element is a string Key and
25// the second is a uint64_t Value.
26static 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),
30 ConstantAsMetadata::get(ConstantInt::get(Int64Ty, Val))};
31 return MDTuple::get(Context, Ops);
32}
33
34// Return an MDTuple with two elements. The first element is a string Key and
35// the second is a string Value.
36static Metadata *getKeyValMD(LLVMContext &Context, const char *Key,
37 const char *Val) {
38 Metadata *Ops[2] = {MDString::get(Context, Key), MDString::get(Context, Val)};
39 return MDTuple::get(Context, Ops);
40}
41
42// This returns an MDTuple representing the detiled summary. The tuple has two
43// elements: a string "DetailedSummary" and an MDTuple representing the value
44// of the detailed summary. Each element of this tuple is again an MDTuple whose
45// elements are the (Cutoff, MinCount, NumCounts) triplet of the
46// DetailedSummaryEntry.
47Metadata *ProfileSummary::getDetailedSummaryMD(LLVMContext &Context) {
48 std::vector<Metadata *> Entries;
49 Type *Int32Ty = Type::getInt32Ty(Context);
50 Type *Int64Ty = Type::getInt64Ty(Context);
51 for (auto &Entry : DetailedSummary) {
52 Metadata *EntryMD[3] = {
53 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Entry.Cutoff)),
54 ConstantAsMetadata::get(ConstantInt::get(Int64Ty, Entry.MinCount)),
55 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Entry.NumCounts))};
56 Entries.push_back(MDTuple::get(Context, EntryMD));
57 }
58 Metadata *Ops[2] = {MDString::get(Context, "DetailedSummary"),
59 MDTuple::get(Context, Entries)};
60 return MDTuple::get(Context, Ops);
61}
62
63// This returns an MDTuple representing this ProfileSummary object. The first
64// entry of this tuple is another MDTuple of two elements: a string
65// "ProfileFormat" and a string representing the format ("InstrProf" or
66// "SampleProfile"). The rest of the elements of the outer MDTuple are specific
67// to the kind of profile summary as returned by getFormatSpecificMD.
Wei Mi10b57ca2020-04-21 14:32:28 -070068// IsPartialProfile is an optional field and \p AddPartialField will decide
69// whether to add a field for it.
70Metadata *ProfileSummary::getMD(LLVMContext &Context, bool AddPartialField) {
Rong Xua6ff69f2019-02-28 19:55:07 +000071 const char *KindStr[3] = {"InstrProf", "CSInstrProf", "SampleProfile"};
Wei Mi10b57ca2020-04-21 14:32:28 -070072 SmallVector<Metadata *, 16> Components;
73 Components.push_back(getKeyValMD(Context, "ProfileFormat", KindStr[PSK]));
74 Components.push_back(getKeyValMD(Context, "TotalCount", getTotalCount()));
75 Components.push_back(getKeyValMD(Context, "MaxCount", getMaxCount()));
76 Components.push_back(
77 getKeyValMD(Context, "MaxInternalCount", getMaxInternalCount()));
78 Components.push_back(
79 getKeyValMD(Context, "MaxFunctionCount", getMaxFunctionCount()));
80 Components.push_back(getKeyValMD(Context, "NumCounts", getNumCounts()));
81 Components.push_back(getKeyValMD(Context, "NumFunctions", getNumFunctions()));
82 if (AddPartialField)
83 Components.push_back(
84 getKeyValMD(Context, "IsPartialProfile", isPartialProfile()));
85 Components.push_back(getDetailedSummaryMD(Context));
Easwaran Raman7cefdb82016-05-19 21:53:28 +000086 return MDTuple::get(Context, Components);
Easwaran Raman7c4f25d2016-03-01 18:30:58 +000087}
88
89// Parse an MDTuple representing (Key, Val) pair.
90static bool getVal(MDTuple *MD, const char *Key, uint64_t &Val) {
91 if (!MD)
92 return false;
93 if (MD->getNumOperands() != 2)
94 return false;
95 MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0));
96 ConstantAsMetadata *ValMD = dyn_cast<ConstantAsMetadata>(MD->getOperand(1));
97 if (!KeyMD || !ValMD)
98 return false;
99 if (!KeyMD->getString().equals(Key))
100 return false;
101 Val = cast<ConstantInt>(ValMD->getValue())->getZExtValue();
102 return true;
103}
104
105// Check if an MDTuple represents a (Key, Val) pair.
106static bool isKeyValuePair(MDTuple *MD, const char *Key, const char *Val) {
107 if (!MD || MD->getNumOperands() != 2)
108 return false;
109 MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0));
110 MDString *ValMD = dyn_cast<MDString>(MD->getOperand(1));
111 if (!KeyMD || !ValMD)
112 return false;
113 if (!KeyMD->getString().equals(Key) || !ValMD->getString().equals(Val))
114 return false;
115 return true;
116}
117
118// Parse an MDTuple representing detailed summary.
119static bool getSummaryFromMD(MDTuple *MD, SummaryEntryVector &Summary) {
120 if (!MD || MD->getNumOperands() != 2)
121 return false;
122 MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0));
123 if (!KeyMD || !KeyMD->getString().equals("DetailedSummary"))
124 return false;
125 MDTuple *EntriesMD = dyn_cast<MDTuple>(MD->getOperand(1));
126 if (!EntriesMD)
127 return false;
128 for (auto &&MDOp : EntriesMD->operands()) {
129 MDTuple *EntryMD = dyn_cast<MDTuple>(MDOp);
130 if (!EntryMD || EntryMD->getNumOperands() != 3)
131 return false;
132 ConstantAsMetadata *Op0 =
133 dyn_cast<ConstantAsMetadata>(EntryMD->getOperand(0));
134 ConstantAsMetadata *Op1 =
135 dyn_cast<ConstantAsMetadata>(EntryMD->getOperand(1));
136 ConstantAsMetadata *Op2 =
137 dyn_cast<ConstantAsMetadata>(EntryMD->getOperand(2));
138
139 if (!Op0 || !Op1 || !Op2)
140 return false;
141 Summary.emplace_back(cast<ConstantInt>(Op0->getValue())->getZExtValue(),
142 cast<ConstantInt>(Op1->getValue())->getZExtValue(),
143 cast<ConstantInt>(Op2->getValue())->getZExtValue());
144 }
145 return true;
146}
147
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000148ProfileSummary *ProfileSummary::getFromMD(Metadata *MD) {
George Burgess IV85fc4c32018-04-12 19:48:05 +0000149 MDTuple *Tuple = dyn_cast_or_null<MDTuple>(MD);
Wei Mi10b57ca2020-04-21 14:32:28 -0700150 if (!Tuple && (Tuple->getNumOperands() < 8 || Tuple->getNumOperands() > 9))
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000151 return nullptr;
152
Wei Mi10b57ca2020-04-21 14:32:28 -0700153 int i = 0;
154 auto &FormatMD = Tuple->getOperand(i++);
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000155 ProfileSummary::Kind SummaryKind;
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000156 if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",
157 "SampleProfile"))
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000158 SummaryKind = PSK_Sample;
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000159 else if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",
160 "InstrProf"))
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000161 SummaryKind = PSK_Instr;
Rong Xua6ff69f2019-02-28 19:55:07 +0000162 else if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",
163 "CSInstrProf"))
164 SummaryKind = PSK_CSInstr;
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000165 else
166 return nullptr;
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000167
168 uint64_t NumCounts, TotalCount, NumFunctions, MaxFunctionCount, MaxCount,
169 MaxInternalCount;
Wei Mi10b57ca2020-04-21 14:32:28 -0700170 if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "TotalCount",
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000171 TotalCount))
172 return nullptr;
Wei Mi10b57ca2020-04-21 14:32:28 -0700173 if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "MaxCount", MaxCount))
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000174 return nullptr;
Wei Mi10b57ca2020-04-21 14:32:28 -0700175 if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "MaxInternalCount",
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000176 MaxInternalCount))
177 return nullptr;
Wei Mi10b57ca2020-04-21 14:32:28 -0700178 if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "MaxFunctionCount",
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000179 MaxFunctionCount))
180 return nullptr;
Wei Mi10b57ca2020-04-21 14:32:28 -0700181 if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "NumCounts",
182 NumCounts))
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000183 return nullptr;
Wei Mi10b57ca2020-04-21 14:32:28 -0700184 if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "NumFunctions",
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000185 NumFunctions))
186 return nullptr;
Wei Mi10b57ca2020-04-21 14:32:28 -0700187 // Initialize IsPartialProfile because the field is optional.
188 uint64_t IsPartialProfile = 0;
189
190 // IsPartialProfile is optional so it doesn't matter even if the next val
191 // is not IsPartialProfile.
192 if (getVal(dyn_cast<MDTuple>(Tuple->getOperand(i)), "IsPartialProfile",
193 IsPartialProfile)) {
194 // Need to make sure when IsPartialProfile is presented, we won't step
195 // over the bound of Tuple operand array.
196 if (Tuple->getNumOperands() < 9)
197 return nullptr;
198 i++;
199 }
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000200
201 SummaryEntryVector Summary;
Wei Mi10b57ca2020-04-21 14:32:28 -0700202 if (!getSummaryFromMD(dyn_cast<MDTuple>(Tuple->getOperand(i++)), Summary))
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000203 return nullptr;
George Burgess IV49331242018-04-12 20:54:05 +0000204 return new ProfileSummary(SummaryKind, std::move(Summary), TotalCount,
205 MaxCount, MaxInternalCount, MaxFunctionCount,
Wei Mi10b57ca2020-04-21 14:32:28 -0700206 NumCounts, NumFunctions, IsPartialProfile);
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000207}