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 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 10 | // This file contains support for converting profile summary data from/to |
| 11 | // metadata. |
Easwaran Raman | d68aae2 | 2016-02-04 23:34:31 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Easwaran Raman | e5a17e3 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 15 | #include "llvm/IR/ProfileSummary.h" |
Dehao Chen | f84b630 | 2016-02-23 03:39:24 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Attributes.h" |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Constants.h" |
Dehao Chen | f84b630 | 2016-02-23 03:39:24 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Function.h" |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Metadata.h" |
| 20 | #include "llvm/IR/Type.h" |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Casting.h" |
Easwaran Raman | d68aae2 | 2016-02-04 23:34:31 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | |
Easwaran Raman | 8832e5e | 2016-03-01 18:59:11 +0000 | [diff] [blame] | 25 | const char *ProfileSummary::KindStr[2] = {"InstrProf", "SampleProfile"}; |
Easwaran Raman | 40ee23d | 2016-02-19 03:15:33 +0000 | [diff] [blame] | 26 | |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 27 | // Return an MDTuple with two elements. The first element is a string Key and |
| 28 | // the second is a uint64_t Value. |
| 29 | static Metadata *getKeyValMD(LLVMContext &Context, const char *Key, |
| 30 | uint64_t Val) { |
| 31 | Type *Int64Ty = Type::getInt64Ty(Context); |
| 32 | Metadata *Ops[2] = {MDString::get(Context, Key), |
| 33 | ConstantAsMetadata::get(ConstantInt::get(Int64Ty, Val))}; |
| 34 | return MDTuple::get(Context, Ops); |
| 35 | } |
| 36 | |
| 37 | // Return an MDTuple with two elements. The first element is a string Key and |
| 38 | // the second is a string Value. |
| 39 | static Metadata *getKeyValMD(LLVMContext &Context, const char *Key, |
| 40 | const char *Val) { |
| 41 | Metadata *Ops[2] = {MDString::get(Context, Key), MDString::get(Context, Val)}; |
| 42 | return MDTuple::get(Context, Ops); |
| 43 | } |
| 44 | |
| 45 | // This returns an MDTuple representing the detiled summary. The tuple has two |
| 46 | // elements: a string "DetailedSummary" and an MDTuple representing the value |
| 47 | // of the detailed summary. Each element of this tuple is again an MDTuple whose |
| 48 | // elements are the (Cutoff, MinCount, NumCounts) triplet of the |
| 49 | // DetailedSummaryEntry. |
| 50 | Metadata *ProfileSummary::getDetailedSummaryMD(LLVMContext &Context) { |
| 51 | std::vector<Metadata *> Entries; |
| 52 | Type *Int32Ty = Type::getInt32Ty(Context); |
| 53 | Type *Int64Ty = Type::getInt64Ty(Context); |
| 54 | for (auto &Entry : DetailedSummary) { |
| 55 | Metadata *EntryMD[3] = { |
| 56 | ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Entry.Cutoff)), |
| 57 | ConstantAsMetadata::get(ConstantInt::get(Int64Ty, Entry.MinCount)), |
| 58 | ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Entry.NumCounts))}; |
| 59 | Entries.push_back(MDTuple::get(Context, EntryMD)); |
| 60 | } |
| 61 | Metadata *Ops[2] = {MDString::get(Context, "DetailedSummary"), |
| 62 | MDTuple::get(Context, Entries)}; |
| 63 | return MDTuple::get(Context, Ops); |
| 64 | } |
| 65 | |
| 66 | // This returns an MDTuple representing this ProfileSummary object. The first |
| 67 | // entry of this tuple is another MDTuple of two elements: a string |
| 68 | // "ProfileFormat" and a string representing the format ("InstrProf" or |
| 69 | // "SampleProfile"). The rest of the elements of the outer MDTuple are specific |
| 70 | // to the kind of profile summary as returned by getFormatSpecificMD. |
| 71 | Metadata *ProfileSummary::getMD(LLVMContext &Context) { |
| 72 | std::vector<Metadata *> Components; |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame^] | 73 | Components.push_back(getKeyValMD(Context, "ProfileFormat", KindStr[PSK])); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 74 | |
| 75 | Components.push_back(getKeyValMD(Context, "TotalCount", getTotalCount())); |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame^] | 76 | Components.push_back(getKeyValMD(Context, "MaxCount", getMaxCount())); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 77 | Components.push_back( |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame^] | 78 | getKeyValMD(Context, "MaxInternalCount", getMaxInternalCount())); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 79 | Components.push_back( |
| 80 | getKeyValMD(Context, "MaxFunctionCount", getMaxFunctionCount())); |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame^] | 81 | Components.push_back(getKeyValMD(Context, "NumCounts", getNumCounts())); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 82 | Components.push_back(getKeyValMD(Context, "NumFunctions", getNumFunctions())); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 83 | Components.push_back(getDetailedSummaryMD(Context)); |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame^] | 84 | return MDTuple::get(Context, Components); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Parse an MDTuple representing (Key, Val) pair. |
| 88 | static bool getVal(MDTuple *MD, const char *Key, uint64_t &Val) { |
| 89 | if (!MD) |
| 90 | return false; |
| 91 | if (MD->getNumOperands() != 2) |
| 92 | return false; |
| 93 | MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0)); |
| 94 | ConstantAsMetadata *ValMD = dyn_cast<ConstantAsMetadata>(MD->getOperand(1)); |
| 95 | if (!KeyMD || !ValMD) |
| 96 | return false; |
| 97 | if (!KeyMD->getString().equals(Key)) |
| 98 | return false; |
| 99 | Val = cast<ConstantInt>(ValMD->getValue())->getZExtValue(); |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | // Check if an MDTuple represents a (Key, Val) pair. |
| 104 | static bool isKeyValuePair(MDTuple *MD, const char *Key, const char *Val) { |
| 105 | if (!MD || MD->getNumOperands() != 2) |
| 106 | return false; |
| 107 | MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0)); |
| 108 | MDString *ValMD = dyn_cast<MDString>(MD->getOperand(1)); |
| 109 | if (!KeyMD || !ValMD) |
| 110 | return false; |
| 111 | if (!KeyMD->getString().equals(Key) || !ValMD->getString().equals(Val)) |
| 112 | return false; |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | // Parse an MDTuple representing detailed summary. |
| 117 | static bool getSummaryFromMD(MDTuple *MD, SummaryEntryVector &Summary) { |
| 118 | if (!MD || MD->getNumOperands() != 2) |
| 119 | return false; |
| 120 | MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0)); |
| 121 | if (!KeyMD || !KeyMD->getString().equals("DetailedSummary")) |
| 122 | return false; |
| 123 | MDTuple *EntriesMD = dyn_cast<MDTuple>(MD->getOperand(1)); |
| 124 | if (!EntriesMD) |
| 125 | return false; |
| 126 | for (auto &&MDOp : EntriesMD->operands()) { |
| 127 | MDTuple *EntryMD = dyn_cast<MDTuple>(MDOp); |
| 128 | if (!EntryMD || EntryMD->getNumOperands() != 3) |
| 129 | return false; |
| 130 | ConstantAsMetadata *Op0 = |
| 131 | dyn_cast<ConstantAsMetadata>(EntryMD->getOperand(0)); |
| 132 | ConstantAsMetadata *Op1 = |
| 133 | dyn_cast<ConstantAsMetadata>(EntryMD->getOperand(1)); |
| 134 | ConstantAsMetadata *Op2 = |
| 135 | dyn_cast<ConstantAsMetadata>(EntryMD->getOperand(2)); |
| 136 | |
| 137 | if (!Op0 || !Op1 || !Op2) |
| 138 | return false; |
| 139 | Summary.emplace_back(cast<ConstantInt>(Op0->getValue())->getZExtValue(), |
| 140 | cast<ConstantInt>(Op1->getValue())->getZExtValue(), |
| 141 | cast<ConstantInt>(Op2->getValue())->getZExtValue()); |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 146 | ProfileSummary *ProfileSummary::getFromMD(Metadata *MD) { |
| 147 | if (!isa<MDTuple>(MD)) |
| 148 | return nullptr; |
| 149 | MDTuple *Tuple = cast<MDTuple>(MD); |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame^] | 150 | if (Tuple->getNumOperands() != 8) |
| 151 | return nullptr; |
| 152 | |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 153 | auto &FormatMD = Tuple->getOperand(0); |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame^] | 154 | ProfileSummary::Kind SummaryKind; |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 155 | if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat", |
| 156 | "SampleProfile")) |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame^] | 157 | SummaryKind = PSK_Sample; |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 158 | else if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat", |
| 159 | "InstrProf")) |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame^] | 160 | SummaryKind = PSK_Instr; |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 161 | else |
| 162 | return nullptr; |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame^] | 163 | |
| 164 | uint64_t NumCounts, TotalCount, NumFunctions, MaxFunctionCount, MaxCount, |
| 165 | MaxInternalCount; |
| 166 | if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(1)), "TotalCount", |
| 167 | TotalCount)) |
| 168 | return nullptr; |
| 169 | if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(2)), "MaxCount", MaxCount)) |
| 170 | return nullptr; |
| 171 | if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(3)), "MaxInternalCount", |
| 172 | MaxInternalCount)) |
| 173 | return nullptr; |
| 174 | if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(4)), "MaxFunctionCount", |
| 175 | MaxFunctionCount)) |
| 176 | return nullptr; |
| 177 | if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(5)), "NumCounts", NumCounts)) |
| 178 | return nullptr; |
| 179 | if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(6)), "NumFunctions", |
| 180 | NumFunctions)) |
| 181 | return nullptr; |
| 182 | |
| 183 | SummaryEntryVector Summary; |
| 184 | if (!getSummaryFromMD(dyn_cast<MDTuple>(Tuple->getOperand(7)), Summary)) |
| 185 | return nullptr; |
| 186 | return new ProfileSummary(SummaryKind, Summary, TotalCount, MaxCount, |
| 187 | MaxInternalCount, MaxFunctionCount, NumCounts, |
| 188 | NumFunctions); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 189 | } |