Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 1 | //=-- Profilesummary.cpp - Profile summary support --------------------------=// |
Easwaran Raman | d68aae2 | 2016-02-04 23:34:31 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Easwaran Raman | d68aae2 | 2016-02-04 23:34:31 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 9 | // This file contains support for converting profile summary data from/to |
| 10 | // metadata. |
Easwaran Raman | d68aae2 | 2016-02-04 23:34:31 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 14 | #include "llvm/IR/ProfileSummary.h" |
Dehao Chen | f84b630 | 2016-02-23 03:39:24 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Attributes.h" |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Constants.h" |
Dehao Chen | f84b630 | 2016-02-23 03:39:24 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Function.h" |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Metadata.h" |
| 19 | #include "llvm/IR/Type.h" |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Casting.h" |
Easwaran Raman | d68aae2 | 2016-02-04 23:34:31 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 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), |
| 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. |
| 36 | static 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. |
| 47 | Metadata *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 Mi | 10b57ca | 2020-04-21 14:32:28 -0700 | [diff] [blame] | 68 | // IsPartialProfile is an optional field and \p AddPartialField will decide |
| 69 | // whether to add a field for it. |
| 70 | Metadata *ProfileSummary::getMD(LLVMContext &Context, bool AddPartialField) { |
Rong Xu | a6ff69f | 2019-02-28 19:55:07 +0000 | [diff] [blame] | 71 | const char *KindStr[3] = {"InstrProf", "CSInstrProf", "SampleProfile"}; |
Wei Mi | 10b57ca | 2020-04-21 14:32:28 -0700 | [diff] [blame] | 72 | 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 Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 86 | return MDTuple::get(Context, Components); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Parse an MDTuple representing (Key, Val) pair. |
| 90 | static 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. |
| 106 | static 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. |
| 119 | static 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 Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 148 | ProfileSummary *ProfileSummary::getFromMD(Metadata *MD) { |
George Burgess IV | 85fc4c3 | 2018-04-12 19:48:05 +0000 | [diff] [blame] | 149 | MDTuple *Tuple = dyn_cast_or_null<MDTuple>(MD); |
Wei Mi | 10b57ca | 2020-04-21 14:32:28 -0700 | [diff] [blame] | 150 | if (!Tuple && (Tuple->getNumOperands() < 8 || Tuple->getNumOperands() > 9)) |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 151 | return nullptr; |
| 152 | |
Wei Mi | 10b57ca | 2020-04-21 14:32:28 -0700 | [diff] [blame] | 153 | int i = 0; |
| 154 | auto &FormatMD = Tuple->getOperand(i++); |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 155 | ProfileSummary::Kind SummaryKind; |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 156 | if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat", |
| 157 | "SampleProfile")) |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 158 | SummaryKind = PSK_Sample; |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 159 | else if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat", |
| 160 | "InstrProf")) |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 161 | SummaryKind = PSK_Instr; |
Rong Xu | a6ff69f | 2019-02-28 19:55:07 +0000 | [diff] [blame] | 162 | else if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat", |
| 163 | "CSInstrProf")) |
| 164 | SummaryKind = PSK_CSInstr; |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 165 | else |
| 166 | return nullptr; |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 167 | |
| 168 | uint64_t NumCounts, TotalCount, NumFunctions, MaxFunctionCount, MaxCount, |
| 169 | MaxInternalCount; |
Wei Mi | 10b57ca | 2020-04-21 14:32:28 -0700 | [diff] [blame] | 170 | if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "TotalCount", |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 171 | TotalCount)) |
| 172 | return nullptr; |
Wei Mi | 10b57ca | 2020-04-21 14:32:28 -0700 | [diff] [blame] | 173 | if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "MaxCount", MaxCount)) |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 174 | return nullptr; |
Wei Mi | 10b57ca | 2020-04-21 14:32:28 -0700 | [diff] [blame] | 175 | if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "MaxInternalCount", |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 176 | MaxInternalCount)) |
| 177 | return nullptr; |
Wei Mi | 10b57ca | 2020-04-21 14:32:28 -0700 | [diff] [blame] | 178 | if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "MaxFunctionCount", |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 179 | MaxFunctionCount)) |
| 180 | return nullptr; |
Wei Mi | 10b57ca | 2020-04-21 14:32:28 -0700 | [diff] [blame] | 181 | if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "NumCounts", |
| 182 | NumCounts)) |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 183 | return nullptr; |
Wei Mi | 10b57ca | 2020-04-21 14:32:28 -0700 | [diff] [blame] | 184 | if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "NumFunctions", |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 185 | NumFunctions)) |
| 186 | return nullptr; |
Wei Mi | 10b57ca | 2020-04-21 14:32:28 -0700 | [diff] [blame] | 187 | // 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 Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 200 | |
| 201 | SummaryEntryVector Summary; |
Wei Mi | 10b57ca | 2020-04-21 14:32:28 -0700 | [diff] [blame] | 202 | if (!getSummaryFromMD(dyn_cast<MDTuple>(Tuple->getOperand(i++)), Summary)) |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 203 | return nullptr; |
George Burgess IV | 4933124 | 2018-04-12 20:54:05 +0000 | [diff] [blame] | 204 | return new ProfileSummary(SummaryKind, std::move(Summary), TotalCount, |
| 205 | MaxCount, MaxInternalCount, MaxFunctionCount, |
Wei Mi | 10b57ca | 2020-04-21 14:32:28 -0700 | [diff] [blame] | 206 | NumCounts, NumFunctions, IsPartialProfile); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 207 | } |
Wenlei He | 17fc651 | 2020-05-02 08:34:10 -0700 | [diff] [blame^] | 208 | |
| 209 | void ProfileSummary::printSummary(raw_ostream &OS) { |
| 210 | OS << "Total functions: " << NumFunctions << "\n"; |
| 211 | OS << "Maximum function count: " << MaxFunctionCount << "\n"; |
| 212 | OS << "Maximum block count: " << MaxCount << "\n"; |
| 213 | OS << "Total number of blocks: " << NumCounts << "\n"; |
| 214 | OS << "Total count: " << TotalCount << "\n"; |
| 215 | } |
| 216 | |
| 217 | void ProfileSummary::printDetailedSummary(raw_ostream &OS) { |
| 218 | OS << "Detailed summary:\n"; |
| 219 | for (auto Entry : DetailedSummary) { |
| 220 | OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount |
| 221 | << " account for " |
| 222 | << format("%0.6g", (float)Entry.Cutoff / Scale * 100) |
| 223 | << " percentage of the total counts.\n"; |
| 224 | } |
| 225 | } |