comparison lib/IR/ProfileSummary.cpp @ 148:63bd29f05246

merged
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 19:46:37 +0900
parents c2174574ed3a
children
comparison
equal deleted inserted replaced
146:3fc4d5c3e21e 148:63bd29f05246
1 //=-- Profilesummary.cpp - Profile summary support --------------------------=// 1 //=-- Profilesummary.cpp - Profile summary support --------------------------=//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // 4 // See https://llvm.org/LICENSE.txt for license information.
5 // This file is distributed under the University of Illinois Open Source 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // License. See LICENSE.TXT for details.
7 // 6 //
8 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
9 // 8 //
10 // This file contains support for converting profile summary data from/to 9 // This file contains support for converting profile summary data from/to
11 // metadata. 10 // metadata.
19 #include "llvm/IR/Metadata.h" 18 #include "llvm/IR/Metadata.h"
20 #include "llvm/IR/Type.h" 19 #include "llvm/IR/Type.h"
21 #include "llvm/Support/Casting.h" 20 #include "llvm/Support/Casting.h"
22 21
23 using namespace llvm; 22 using namespace llvm;
24
25 const char *ProfileSummary::KindStr[2] = {"InstrProf", "SampleProfile"};
26 23
27 // Return an MDTuple with two elements. The first element is a string Key and 24 // Return an MDTuple with two elements. The first element is a string Key and
28 // the second is a uint64_t Value. 25 // the second is a uint64_t Value.
29 static Metadata *getKeyValMD(LLVMContext &Context, const char *Key, 26 static Metadata *getKeyValMD(LLVMContext &Context, const char *Key,
30 uint64_t Val) { 27 uint64_t Val) {
67 // entry of this tuple is another MDTuple of two elements: a string 64 // entry of this tuple is another MDTuple of two elements: a string
68 // "ProfileFormat" and a string representing the format ("InstrProf" or 65 // "ProfileFormat" and a string representing the format ("InstrProf" or
69 // "SampleProfile"). The rest of the elements of the outer MDTuple are specific 66 // "SampleProfile"). The rest of the elements of the outer MDTuple are specific
70 // to the kind of profile summary as returned by getFormatSpecificMD. 67 // to the kind of profile summary as returned by getFormatSpecificMD.
71 Metadata *ProfileSummary::getMD(LLVMContext &Context) { 68 Metadata *ProfileSummary::getMD(LLVMContext &Context) {
72 std::vector<Metadata *> Components; 69 const char *KindStr[3] = {"InstrProf", "CSInstrProf", "SampleProfile"};
73 Components.push_back(getKeyValMD(Context, "ProfileFormat", KindStr[PSK])); 70 Metadata *Components[] = {
74 71 getKeyValMD(Context, "ProfileFormat", KindStr[PSK]),
75 Components.push_back(getKeyValMD(Context, "TotalCount", getTotalCount())); 72 getKeyValMD(Context, "TotalCount", getTotalCount()),
76 Components.push_back(getKeyValMD(Context, "MaxCount", getMaxCount())); 73 getKeyValMD(Context, "MaxCount", getMaxCount()),
77 Components.push_back( 74 getKeyValMD(Context, "MaxInternalCount", getMaxInternalCount()),
78 getKeyValMD(Context, "MaxInternalCount", getMaxInternalCount())); 75 getKeyValMD(Context, "MaxFunctionCount", getMaxFunctionCount()),
79 Components.push_back( 76 getKeyValMD(Context, "NumCounts", getNumCounts()),
80 getKeyValMD(Context, "MaxFunctionCount", getMaxFunctionCount())); 77 getKeyValMD(Context, "NumFunctions", getNumFunctions()),
81 Components.push_back(getKeyValMD(Context, "NumCounts", getNumCounts())); 78 getDetailedSummaryMD(Context),
82 Components.push_back(getKeyValMD(Context, "NumFunctions", getNumFunctions())); 79 };
83 Components.push_back(getDetailedSummaryMD(Context));
84 return MDTuple::get(Context, Components); 80 return MDTuple::get(Context, Components);
85 } 81 }
86 82
87 // Parse an MDTuple representing (Key, Val) pair. 83 // Parse an MDTuple representing (Key, Val) pair.
88 static bool getVal(MDTuple *MD, const char *Key, uint64_t &Val) { 84 static bool getVal(MDTuple *MD, const char *Key, uint64_t &Val) {
142 } 138 }
143 return true; 139 return true;
144 } 140 }
145 141
146 ProfileSummary *ProfileSummary::getFromMD(Metadata *MD) { 142 ProfileSummary *ProfileSummary::getFromMD(Metadata *MD) {
147 if (!MD) 143 MDTuple *Tuple = dyn_cast_or_null<MDTuple>(MD);
148 return nullptr; 144 if (!Tuple || Tuple->getNumOperands() != 8)
149 if (!isa<MDTuple>(MD))
150 return nullptr;
151 MDTuple *Tuple = cast<MDTuple>(MD);
152 if (Tuple->getNumOperands() != 8)
153 return nullptr; 145 return nullptr;
154 146
155 auto &FormatMD = Tuple->getOperand(0); 147 auto &FormatMD = Tuple->getOperand(0);
156 ProfileSummary::Kind SummaryKind; 148 ProfileSummary::Kind SummaryKind;
157 if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat", 149 if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",
158 "SampleProfile")) 150 "SampleProfile"))
159 SummaryKind = PSK_Sample; 151 SummaryKind = PSK_Sample;
160 else if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat", 152 else if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",
161 "InstrProf")) 153 "InstrProf"))
162 SummaryKind = PSK_Instr; 154 SummaryKind = PSK_Instr;
155 else if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",
156 "CSInstrProf"))
157 SummaryKind = PSK_CSInstr;
163 else 158 else
164 return nullptr; 159 return nullptr;
165 160
166 uint64_t NumCounts, TotalCount, NumFunctions, MaxFunctionCount, MaxCount, 161 uint64_t NumCounts, TotalCount, NumFunctions, MaxFunctionCount, MaxCount,
167 MaxInternalCount; 162 MaxInternalCount;
183 return nullptr; 178 return nullptr;
184 179
185 SummaryEntryVector Summary; 180 SummaryEntryVector Summary;
186 if (!getSummaryFromMD(dyn_cast<MDTuple>(Tuple->getOperand(7)), Summary)) 181 if (!getSummaryFromMD(dyn_cast<MDTuple>(Tuple->getOperand(7)), Summary))
187 return nullptr; 182 return nullptr;
188 return new ProfileSummary(SummaryKind, Summary, TotalCount, MaxCount, 183 return new ProfileSummary(SummaryKind, std::move(Summary), TotalCount,
189 MaxInternalCount, MaxFunctionCount, NumCounts, 184 MaxCount, MaxInternalCount, MaxFunctionCount,
190 NumFunctions); 185 NumCounts, NumFunctions);
191 } 186 }