<div dir="ltr">Thanks for catching this and thanks Reid for the patch fixing it.<div><br></div><div>- Easwaran</div><div> </div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 1, 2016 at 11:39 AM, David Majnemer <span dir="ltr"><<a href="mailto:david.majnemer@gmail.com" target="_blank">david.majnemer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">You added a virtual method, <span style="font-size:12.8px">getFormatSpecificMD, but no virtual destructor. Is this intentional?</span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 1, 2016 at 10:30 AM, Easwaran Raman via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: eraman<br>
Date: Tue Mar 1 12:30:58 2016<br>
New Revision: 262360<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=262360&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=262360&view=rev</a><br>
Log:<br>
Metadata support for profile summary.<br>
<br>
This adds support to convert ProfileSummary object to Metadata and create a<br>
ProfileSummary object from metadata. This would allow attaching profile summary<br>
information to Module allowing optimization passes to use it.<br>
<br>
<br>
Modified:<br>
llvm/trunk/include/llvm/ProfileData/ProfileCommon.h<br>
llvm/trunk/lib/ProfileData/ProfileSummary.cpp<br>
llvm/trunk/unittests/ProfileData/InstrProfTest.cpp<br>
llvm/trunk/unittests/ProfileData/SampleProfTest.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/ProfileData/ProfileCommon.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/ProfileCommon.h?rev=262360&r1=262359&r2=262360&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/ProfileCommon.h?rev=262360&r1=262359&r2=262360&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ProfileData/ProfileCommon.h (original)<br>
+++ llvm/trunk/include/llvm/ProfileData/ProfileCommon.h Tue Mar 1 12:30:58 2016<br>
@@ -12,13 +12,15 @@<br>
//<br>
//===----------------------------------------------------------------------===//<br>
<br>
+#ifndef LLVM_PROFILEDATA_PROFILE_COMMON_H<br>
+#define LLVM_PROFILEDATA_PROFILE_COMMON_H<br>
+<br>
#include <cstdint><br>
#include <functional><br>
#include <map><br>
#include <vector><br>
<br>
-#ifndef LLVM_PROFILEDATA_PROFILE_COMMON_H<br>
-#define LLVM_PROFILEDATA_PROFILE_COMMON_H<br>
+#include "llvm/Support/Casting.h"<br>
<br>
namespace llvm {<br>
class Function;<br>
@@ -29,8 +31,14 @@ namespace sampleprof {<br>
class FunctionSamples;<br>
}<br>
struct InstrProfRecord;<br>
+class LLVMContext;<br>
+class Metadata;<br>
+class MDTuple;<br>
+class MDNode;<br>
+<br>
inline const char *getHotSectionPrefix() { return ".hot"; }<br>
inline const char *getUnlikelySectionPrefix() { return ".unlikely"; }<br>
+<br>
// The profile summary is one or more (Cutoff, MinCount, NumCounts) triplets.<br>
// The semantics of counts depend on the type of profile. For instrumentation<br>
// profile, counts are block counts and for sample profile, counts are<br>
@@ -46,37 +54,55 @@ struct ProfileSummaryEntry {<br>
: Cutoff(TheCutoff), MinCount(TheMinCount), NumCounts(TheNumCounts) {}<br>
};<br>
<br>
+typedef std::vector<ProfileSummaryEntry> SummaryEntryVector;<br>
+<br>
class ProfileSummary {<br>
+public:<br>
+ enum Kind { PSK_Instr, PSK_Sample };<br>
+<br>
+private:<br>
+ const Kind PSK;<br>
+ const char *KindStr[2] = {"InstrProf", "SampleProfile"};<br>
// We keep track of the number of times a count (block count or samples)<br>
// appears in the profile. The map is kept sorted in the descending order of<br>
// counts.<br>
std::map<uint64_t, uint32_t, std::greater<uint64_t>> CountFrequencies;<br>
-<br>
protected:<br>
- std::vector<ProfileSummaryEntry> DetailedSummary;<br>
+ SummaryEntryVector DetailedSummary;<br>
std::vector<uint32_t> DetailedSummaryCutoffs;<br>
uint64_t TotalCount, MaxCount;<br>
uint32_t NumCounts;<br>
- ProfileSummary(std::vector<uint32_t> Cutoffs)<br>
- : DetailedSummaryCutoffs(Cutoffs), TotalCount(0), MaxCount(0),<br>
+ ProfileSummary(Kind K, std::vector<uint32_t> Cutoffs)<br>
+ : PSK(K), DetailedSummaryCutoffs(Cutoffs), TotalCount(0), MaxCount(0),<br>
NumCounts(0) {}<br>
- ProfileSummary() : TotalCount(0), MaxCount(0), NumCounts(0) {}<br>
- ProfileSummary(std::vector<ProfileSummaryEntry> DetailedSummary,<br>
+ ProfileSummary(Kind K) : PSK(K), TotalCount(0), MaxCount(0), NumCounts(0) {}<br>
+ ProfileSummary(Kind K, SummaryEntryVector DetailedSummary,<br>
uint64_t TotalCount, uint64_t MaxCount, uint32_t NumCounts)<br>
- : DetailedSummary(DetailedSummary), TotalCount(TotalCount),<br>
+ : PSK(K), DetailedSummary(DetailedSummary), TotalCount(TotalCount),<br>
MaxCount(MaxCount), NumCounts(NumCounts) {}<br>
inline void addCount(uint64_t Count);<br>
+ /// \brief Return metadata specific to the profile format.<br>
+ /// Derived classes implement this method to return a vector of Metadata.<br>
+ virtual std::vector<Metadata *> getFormatSpecificMD(LLVMContext &Context) = 0;<br>
+ /// \brief Return detailed summary as metadata.<br>
+ Metadata *getDetailedSummaryMD(LLVMContext &Context);<br>
<br>
public:<br>
static const int Scale = 1000000;<br>
+ Kind getKind() const { return PSK; }<br>
+ const char *getKindStr() const { return KindStr[PSK]; }<br>
// \brief Returns true if F is a hot function.<br>
static bool isFunctionHot(const Function *F);<br>
// \brief Returns true if F is unlikley executed.<br>
static bool isFunctionUnlikely(const Function *F);<br>
- inline std::vector<ProfileSummaryEntry> &getDetailedSummary();<br>
+ inline SummaryEntryVector &getDetailedSummary();<br>
void computeDetailedSummary();<br>
/// \brief A vector of useful cutoff values for detailed summary.<br>
static const std::vector<uint32_t> DefaultCutoffs;<br>
+ /// \brief Return summary information as metadata.<br>
+ Metadata *getMD(LLVMContext &Context);<br>
+ /// \brief Construct profile summary from metdata.<br>
+ static ProfileSummary *getFromMD(Metadata *MD);<br>
};<br>
<br>
class InstrProfSummary : public ProfileSummary {<br>
@@ -85,11 +111,25 @@ class InstrProfSummary : public ProfileS<br>
inline void addEntryCount(uint64_t Count);<br>
inline void addInternalCount(uint64_t Count);<br>
<br>
+protected:<br>
+ std::vector<Metadata *> getFormatSpecificMD(LLVMContext &Context);<br>
+<br>
public:<br>
InstrProfSummary(std::vector<uint32_t> Cutoffs)<br>
- : ProfileSummary(Cutoffs), MaxInternalBlockCount(0), MaxFunctionCount(0),<br>
- NumFunctions(0) {}<br>
+ : ProfileSummary(PSK_Instr, Cutoffs), MaxInternalBlockCount(0),<br>
+ MaxFunctionCount(0), NumFunctions(0) {}<br>
InstrProfSummary(const IndexedInstrProf::Summary &S);<br>
+ InstrProfSummary(uint64_t TotalCount, uint64_t MaxBlockCount,<br>
+ uint64_t MaxInternalBlockCount, uint64_t MaxFunctionCount,<br>
+ uint32_t NumBlocks, uint32_t NumFunctions,<br>
+ SummaryEntryVector Summary)<br>
+ : ProfileSummary(PSK_Instr, Summary, TotalCount, MaxBlockCount,<br>
+ NumBlocks),<br>
+ MaxInternalBlockCount(MaxInternalBlockCount),<br>
+ MaxFunctionCount(MaxFunctionCount), NumFunctions(NumFunctions) {}<br>
+ static bool classof(const ProfileSummary *PS) {<br>
+ return PS->getKind() == PSK_Instr;<br>
+ }<br>
void addRecord(const InstrProfRecord &);<br>
uint32_t getNumBlocks() { return NumCounts; }<br>
uint64_t getTotalCount() { return TotalCount; }<br>
@@ -103,6 +143,9 @@ class SampleProfileSummary : public Prof<br>
uint64_t MaxHeadSamples;<br>
uint32_t NumFunctions;<br>
<br>
+protected:<br>
+ std::vector<Metadata *> getFormatSpecificMD(LLVMContext &Context);<br>
+<br>
public:<br>
uint32_t getNumLinesWithSamples() { return NumCounts; }<br>
uint64_t getTotalSamples() { return TotalCount; }<br>
@@ -111,14 +154,18 @@ public:<br>
uint64_t getMaxSamplesPerLine() { return MaxCount; }<br>
void addRecord(const sampleprof::FunctionSamples &FS);<br>
SampleProfileSummary(std::vector<uint32_t> Cutoffs)<br>
- : ProfileSummary(Cutoffs), MaxHeadSamples(0), NumFunctions(0) {}<br>
+ : ProfileSummary(PSK_Sample, Cutoffs), MaxHeadSamples(0),<br>
+ NumFunctions(0) {}<br>
SampleProfileSummary(uint64_t TotalSamples, uint64_t MaxSamplesPerLine,<br>
uint64_t MaxHeadSamples, int32_t NumLinesWithSamples,<br>
uint32_t NumFunctions,<br>
- std::vector<ProfileSummaryEntry> DetailedSummary)<br>
- : ProfileSummary(DetailedSummary, TotalSamples, MaxSamplesPerLine,<br>
- NumLinesWithSamples),<br>
+ SummaryEntryVector DetailedSummary)<br>
+ : ProfileSummary(PSK_Sample, DetailedSummary, TotalSamples,<br>
+ MaxSamplesPerLine, NumLinesWithSamples),<br>
MaxHeadSamples(MaxHeadSamples), NumFunctions(NumFunctions) {}<br>
+ static bool classof(const ProfileSummary *PS) {<br>
+ return PS->getKind() == PSK_Sample;<br>
+ }<br>
};<br>
<br>
// This is called when a count is seen in the profile.<br>
@@ -130,7 +177,7 @@ void ProfileSummary::addCount(uint64_t C<br>
CountFrequencies[Count]++;<br>
}<br>
<br>
-std::vector<ProfileSummaryEntry> &ProfileSummary::getDetailedSummary() {<br>
+SummaryEntryVector &ProfileSummary::getDetailedSummary() {<br>
if (!DetailedSummaryCutoffs.empty() && DetailedSummary.empty())<br>
computeDetailedSummary();<br>
return DetailedSummary;<br>
<br>
Modified: llvm/trunk/lib/ProfileData/ProfileSummary.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/ProfileSummary.cpp?rev=262360&r1=262359&r2=262360&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/ProfileSummary.cpp?rev=262360&r1=262359&r2=262360&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/ProfileData/ProfileSummary.cpp (original)<br>
+++ llvm/trunk/lib/ProfileData/ProfileSummary.cpp Tue Mar 1 12:30:58 2016<br>
@@ -12,10 +12,14 @@<br>
//===----------------------------------------------------------------------===//<br>
<br>
#include "llvm/IR/Attributes.h"<br>
+#include "llvm/IR/Constants.h"<br>
#include "llvm/IR/Function.h"<br>
+#include "llvm/IR/Metadata.h"<br>
+#include "llvm/IR/Type.h"<br>
#include "llvm/ProfileData/InstrProf.h"<br>
#include "llvm/ProfileData/ProfileCommon.h"<br>
#include "llvm/ProfileData/SampleProf.h"<br>
+#include "llvm/Support/Casting.h"<br>
<br>
using namespace llvm;<br>
<br>
@@ -96,8 +100,9 @@ bool ProfileSummary::isFunctionUnlikely(<br>
}<br>
<br>
InstrProfSummary::InstrProfSummary(const IndexedInstrProf::Summary &S)<br>
- : ProfileSummary(), MaxInternalBlockCount(S.get(<br>
- IndexedInstrProf::Summary::MaxInternalBlockCount)),<br>
+ : ProfileSummary(PSK_Instr),<br>
+ MaxInternalBlockCount(<br>
+ S.get(IndexedInstrProf::Summary::MaxInternalBlockCount)),<br>
MaxFunctionCount(S.get(IndexedInstrProf::Summary::MaxFunctionCount)),<br>
NumFunctions(S.get(IndexedInstrProf::Summary::TotalNumFunctions)) {<br>
<br>
@@ -123,3 +128,235 @@ void InstrProfSummary::addInternalCount(<br>
if (Count > MaxInternalBlockCount)<br>
MaxInternalBlockCount = Count;<br>
}<br>
+<br>
+// Return an MDTuple with two elements. The first element is a string Key and<br>
+// the second is a uint64_t Value.<br>
+static Metadata *getKeyValMD(LLVMContext &Context, const char *Key,<br>
+ uint64_t Val) {<br>
+ Type *Int64Ty = Type::getInt64Ty(Context);<br>
+ Metadata *Ops[2] = {MDString::get(Context, Key),<br>
+ ConstantAsMetadata::get(ConstantInt::get(Int64Ty, Val))};<br>
+ return MDTuple::get(Context, Ops);<br>
+}<br>
+<br>
+// Return an MDTuple with two elements. The first element is a string Key and<br>
+// the second is a string Value.<br>
+static Metadata *getKeyValMD(LLVMContext &Context, const char *Key,<br>
+ const char *Val) {<br>
+ Metadata *Ops[2] = {MDString::get(Context, Key), MDString::get(Context, Val)};<br>
+ return MDTuple::get(Context, Ops);<br>
+}<br>
+<br>
+// This returns an MDTuple representing the detiled summary. The tuple has two<br>
+// elements: a string "DetailedSummary" and an MDTuple representing the value<br>
+// of the detailed summary. Each element of this tuple is again an MDTuple whose<br>
+// elements are the (Cutoff, MinCount, NumCounts) triplet of the<br>
+// DetailedSummaryEntry.<br>
+Metadata *ProfileSummary::getDetailedSummaryMD(LLVMContext &Context) {<br>
+ std::vector<Metadata *> Entries;<br>
+ Type *Int32Ty = Type::getInt32Ty(Context);<br>
+ Type *Int64Ty = Type::getInt64Ty(Context);<br>
+ for (auto &Entry : DetailedSummary) {<br>
+ Metadata *EntryMD[3] = {<br>
+ ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Entry.Cutoff)),<br>
+ ConstantAsMetadata::get(ConstantInt::get(Int64Ty, Entry.MinCount)),<br>
+ ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Entry.NumCounts))};<br>
+ Entries.push_back(MDTuple::get(Context, EntryMD));<br>
+ }<br>
+ Metadata *Ops[2] = {MDString::get(Context, "DetailedSummary"),<br>
+ MDTuple::get(Context, Entries)};<br>
+ return MDTuple::get(Context, Ops);<br>
+}<br>
+<br>
+// This returns an MDTuple representing this ProfileSummary object. The first<br>
+// entry of this tuple is another MDTuple of two elements: a string<br>
+// "ProfileFormat" and a string representing the format ("InstrProf" or<br>
+// "SampleProfile"). The rest of the elements of the outer MDTuple are specific<br>
+// to the kind of profile summary as returned by getFormatSpecificMD.<br>
+Metadata *ProfileSummary::getMD(LLVMContext &Context) {<br>
+ std::vector<Metadata *> Components;<br>
+ Components.push_back(getKeyValMD(Context, "ProfileFormat", getKindStr()));<br>
+ std::vector<Metadata *> Res = getFormatSpecificMD(Context);<br>
+ Components.insert(Components.end(), Res.begin(), Res.end());<br>
+ return MDTuple::get(Context, Components);<br>
+}<br>
+<br>
+// Returns a vector of MDTuples specific to InstrProfSummary. The first six<br>
+// elements of this vector are (Key, Val) pairs of the six scalar fields of<br>
+// InstrProfSummary (TotalCount, MaxBlockCount, MaxInternalBlockCount,<br>
+// MaxFunctionCount, NumBlocks, NumFunctions). The last element of this vector<br>
+// is an MDTuple returned by getDetailedSummaryMD.<br>
+std::vector<Metadata *><br>
+InstrProfSummary::getFormatSpecificMD(LLVMContext &Context) {<br>
+ std::vector<Metadata *> Components;<br>
+<br>
+ Components.push_back(getKeyValMD(Context, "TotalCount", getTotalCount()));<br>
+ Components.push_back(<br>
+ getKeyValMD(Context, "MaxBlockCount", getMaxBlockCount()));<br>
+ Components.push_back(getKeyValMD(Context, "MaxInternalBlockCount",<br>
+ getMaxInternalBlockCount()));<br>
+ Components.push_back(<br>
+ getKeyValMD(Context, "MaxFunctionCount", getMaxFunctionCount()));<br>
+ Components.push_back(getKeyValMD(Context, "NumBlocks", getNumBlocks()));<br>
+ Components.push_back(getKeyValMD(Context, "NumFunctions", getNumFunctions()));<br>
+<br>
+ Components.push_back(getDetailedSummaryMD(Context));<br>
+ return Components;<br>
+}<br>
+<br>
+std::vector<Metadata *><br>
+SampleProfileSummary::getFormatSpecificMD(LLVMContext &Context) {<br>
+ std::vector<Metadata *> Components;<br>
+<br>
+ Components.push_back(getKeyValMD(Context, "TotalSamples", getTotalSamples()));<br>
+ Components.push_back(<br>
+ getKeyValMD(Context, "MaxSamplesPerLine", getMaxSamplesPerLine()));<br>
+ Components.push_back(<br>
+ getKeyValMD(Context, "MaxHeadSamples", getMaxHeadSamples()));<br>
+ Components.push_back(<br>
+ getKeyValMD(Context, "NumLinesWithSamples", getNumLinesWithSamples()));<br>
+ Components.push_back(getKeyValMD(Context, "NumFunctions", NumFunctions));<br>
+<br>
+ Components.push_back(getDetailedSummaryMD(Context));<br>
+ return Components;<br>
+}<br>
+<br>
+// Parse an MDTuple representing (Key, Val) pair.<br>
+static bool getVal(MDTuple *MD, const char *Key, uint64_t &Val) {<br>
+ if (!MD)<br>
+ return false;<br>
+ if (MD->getNumOperands() != 2)<br>
+ return false;<br>
+ MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0));<br>
+ ConstantAsMetadata *ValMD = dyn_cast<ConstantAsMetadata>(MD->getOperand(1));<br>
+ if (!KeyMD || !ValMD)<br>
+ return false;<br>
+ if (!KeyMD->getString().equals(Key))<br>
+ return false;<br>
+ Val = cast<ConstantInt>(ValMD->getValue())->getZExtValue();<br>
+ return true;<br>
+}<br>
+<br>
+// Check if an MDTuple represents a (Key, Val) pair.<br>
+static bool isKeyValuePair(MDTuple *MD, const char *Key, const char *Val) {<br>
+ if (!MD || MD->getNumOperands() != 2)<br>
+ return false;<br>
+ MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0));<br>
+ MDString *ValMD = dyn_cast<MDString>(MD->getOperand(1));<br>
+ if (!KeyMD || !ValMD)<br>
+ return false;<br>
+ if (!KeyMD->getString().equals(Key) || !ValMD->getString().equals(Val))<br>
+ return false;<br>
+ return true;<br>
+}<br>
+<br>
+// Parse an MDTuple representing detailed summary.<br>
+static bool getSummaryFromMD(MDTuple *MD, SummaryEntryVector &Summary) {<br>
+ if (!MD || MD->getNumOperands() != 2)<br>
+ return false;<br>
+ MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0));<br>
+ if (!KeyMD || !KeyMD->getString().equals("DetailedSummary"))<br>
+ return false;<br>
+ MDTuple *EntriesMD = dyn_cast<MDTuple>(MD->getOperand(1));<br>
+ if (!EntriesMD)<br>
+ return false;<br>
+ for (auto &&MDOp : EntriesMD->operands()) {<br>
+ MDTuple *EntryMD = dyn_cast<MDTuple>(MDOp);<br>
+ if (!EntryMD || EntryMD->getNumOperands() != 3)<br>
+ return false;<br>
+ ConstantAsMetadata *Op0 =<br>
+ dyn_cast<ConstantAsMetadata>(EntryMD->getOperand(0));<br>
+ ConstantAsMetadata *Op1 =<br>
+ dyn_cast<ConstantAsMetadata>(EntryMD->getOperand(1));<br>
+ ConstantAsMetadata *Op2 =<br>
+ dyn_cast<ConstantAsMetadata>(EntryMD->getOperand(2));<br>
+<br>
+ if (!Op0 || !Op1 || !Op2)<br>
+ return false;<br>
+ Summary.emplace_back(cast<ConstantInt>(Op0->getValue())->getZExtValue(),<br>
+ cast<ConstantInt>(Op1->getValue())->getZExtValue(),<br>
+ cast<ConstantInt>(Op2->getValue())->getZExtValue());<br>
+ }<br>
+ return true;<br>
+}<br>
+<br>
+// Parse an MDTuple representing an InstrProfSummary object.<br>
+static ProfileSummary *getInstrProfSummaryFromMD(MDTuple *Tuple) {<br>
+ uint64_t NumBlocks, TotalCount, NumFunctions, MaxFunctionCount, MaxBlockCount,<br>
+ MaxInternalBlockCount;<br>
+ SummaryEntryVector Summary;<br>
+<br>
+ if (Tuple->getNumOperands() != 8)<br>
+ return nullptr;<br>
+<br>
+ // Skip operand 0 which has been already parsed in the caller<br>
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(1)), "TotalCount",<br>
+ TotalCount))<br>
+ return nullptr;<br>
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(2)), "MaxBlockCount",<br>
+ MaxBlockCount))<br>
+ return nullptr;<br>
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(3)), "MaxInternalBlockCount",<br>
+ MaxInternalBlockCount))<br>
+ return nullptr;<br>
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(4)), "MaxFunctionCount",<br>
+ MaxFunctionCount))<br>
+ return nullptr;<br>
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(5)), "NumBlocks", NumBlocks))<br>
+ return nullptr;<br>
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(6)), "NumFunctions",<br>
+ NumFunctions))<br>
+ return nullptr;<br>
+ if (!getSummaryFromMD(dyn_cast<MDTuple>(Tuple->getOperand(7)), Summary))<br>
+ return nullptr;<br>
+ return new InstrProfSummary(TotalCount, MaxBlockCount, MaxInternalBlockCount,<br>
+ MaxFunctionCount, NumBlocks, NumFunctions,<br>
+ Summary);<br>
+}<br>
+<br>
+// Parse an MDTuple representing a SampleProfileSummary object.<br>
+static ProfileSummary *getSampleProfileSummaryFromMD(MDTuple *Tuple) {<br>
+ uint64_t TotalSamples, MaxSamplesPerLine, MaxHeadSamples, NumLinesWithSamples,<br>
+ NumFunctions;<br>
+ SummaryEntryVector Summary;<br>
+<br>
+ if (Tuple->getNumOperands() != 7)<br>
+ return nullptr;<br>
+<br>
+ // Skip operand 0 which has been already parsed in the caller<br>
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(1)), "TotalSamples",<br>
+ TotalSamples))<br>
+ return nullptr;<br>
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(2)), "MaxSamplesPerLine",<br>
+ MaxSamplesPerLine))<br>
+ return nullptr;<br>
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(3)), "MaxHeadSamples",<br>
+ MaxHeadSamples))<br>
+ return nullptr;<br>
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(4)), "NumLinesWithSamples",<br>
+ NumLinesWithSamples))<br>
+ return nullptr;<br>
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(5)), "NumFunctions",<br>
+ NumFunctions))<br>
+ return nullptr;<br>
+ if (!getSummaryFromMD(dyn_cast<MDTuple>(Tuple->getOperand(6)), Summary))<br>
+ return nullptr;<br>
+ return new SampleProfileSummary(TotalSamples, MaxSamplesPerLine,<br>
+ MaxHeadSamples, NumLinesWithSamples,<br>
+ NumFunctions, Summary);<br>
+}<br>
+<br>
+ProfileSummary *ProfileSummary::getFromMD(Metadata *MD) {<br>
+ if (!isa<MDTuple>(MD))<br>
+ return nullptr;<br>
+ MDTuple *Tuple = cast<MDTuple>(MD);<br>
+ auto &FormatMD = Tuple->getOperand(0);<br>
+ if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",<br>
+ "SampleProfile"))<br>
+ return getSampleProfileSummaryFromMD(Tuple);<br>
+ else if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",<br>
+ "InstrProf"))<br>
+ return getInstrProfSummaryFromMD(Tuple);<br>
+ else<br>
+ return nullptr;<br>
+}<br>
<br>
Modified: llvm/trunk/unittests/ProfileData/InstrProfTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ProfileData/InstrProfTest.cpp?rev=262360&r1=262359&r2=262360&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ProfileData/InstrProfTest.cpp?rev=262360&r1=262359&r2=262360&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/ProfileData/InstrProfTest.cpp (original)<br>
+++ llvm/trunk/unittests/ProfileData/InstrProfTest.cpp Tue Mar 1 12:30:58 2016<br>
@@ -154,27 +154,39 @@ TEST_F(InstrProfTest, get_profile_summar<br>
auto Profile = Writer.writeBuffer();<br>
readProfile(std::move(Profile));<br>
<br>
- InstrProfSummary &PS = Reader->getSummary();<br>
- ASSERT_EQ(2305843009213693952U, PS.getMaxFunctionCount());<br>
- ASSERT_EQ(2305843009213693952U, PS.getMaxBlockCount());<br>
- ASSERT_EQ(10U, PS.getNumBlocks());<br>
- ASSERT_EQ(4539628424389557499U, PS.getTotalCount());<br>
- std::vector<ProfileSummaryEntry> &Details = PS.getDetailedSummary();<br>
- uint32_t Cutoff = 800000;<br>
- auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) {<br>
- return PE.Cutoff == Cutoff;<br>
+ auto VerifySummary = [](InstrProfSummary &IPS, bool dummy) mutable {<br>
+ ASSERT_EQ(2305843009213693952U, IPS.getMaxFunctionCount());<br>
+ ASSERT_EQ(2305843009213693952U, IPS.getMaxBlockCount());<br>
+ ASSERT_EQ(10U, IPS.getNumBlocks());<br>
+ ASSERT_EQ(4539628424389557499U, IPS.getTotalCount());<br>
+ std::vector<ProfileSummaryEntry> &Details = IPS.getDetailedSummary();<br>
+ uint32_t Cutoff = 800000;<br>
+ auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) {<br>
+ return PE.Cutoff == Cutoff;<br>
+ };<br>
+ auto EightyPerc = std::find_if(Details.begin(), Details.end(), Predicate);<br>
+ Cutoff = 900000;<br>
+ auto NinetyPerc = std::find_if(Details.begin(), Details.end(), Predicate);<br>
+ Cutoff = 950000;<br>
+ auto NinetyFivePerc =<br>
+ std::find_if(Details.begin(), Details.end(), Predicate);<br>
+ Cutoff = 990000;<br>
+ auto NinetyNinePerc =<br>
+ std::find_if(Details.begin(), Details.end(), Predicate);<br>
+ ASSERT_EQ(576460752303423488U, EightyPerc->MinCount);<br>
+ ASSERT_EQ(288230376151711744U, NinetyPerc->MinCount);<br>
+ ASSERT_EQ(288230376151711744U, NinetyFivePerc->MinCount);<br>
+ ASSERT_EQ(72057594037927936U, NinetyNinePerc->MinCount);<br>
};<br>
- auto EightyPerc = std::find_if(Details.begin(), Details.end(), Predicate);<br>
- Cutoff = 900000;<br>
- auto NinetyPerc = std::find_if(Details.begin(), Details.end(), Predicate);<br>
- Cutoff = 950000;<br>
- auto NinetyFivePerc = std::find_if(Details.begin(), Details.end(), Predicate);<br>
- Cutoff = 990000;<br>
- auto NinetyNinePerc = std::find_if(Details.begin(), Details.end(), Predicate);<br>
- ASSERT_EQ(576460752303423488U, EightyPerc->MinCount);<br>
- ASSERT_EQ(288230376151711744U, NinetyPerc->MinCount);<br>
- ASSERT_EQ(288230376151711744U, NinetyFivePerc->MinCount);<br>
- ASSERT_EQ(72057594037927936U, NinetyNinePerc->MinCount);<br>
+ InstrProfSummary &PS = Reader->getSummary();<br>
+ VerifySummary(PS, true);<br>
+ Metadata *MD = PS.getMD(getGlobalContext());<br>
+ ASSERT_TRUE(MD);<br>
+ ProfileSummary *PSFromMD = ProfileSummary::getFromMD(MD);<br>
+ ASSERT_TRUE(PSFromMD);<br>
+ ASSERT_TRUE(isa<InstrProfSummary>(PSFromMD));<br>
+ InstrProfSummary *IPS = cast<InstrProfSummary>(PSFromMD);<br>
+ VerifySummary(*IPS, false);<br>
}<br>
<br>
TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write) {<br>
<br>
Modified: llvm/trunk/unittests/ProfileData/SampleProfTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ProfileData/SampleProfTest.cpp?rev=262360&r1=262359&r2=262360&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ProfileData/SampleProfTest.cpp?rev=262360&r1=262359&r2=262360&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/unittests/ProfileData/SampleProfTest.cpp (original)<br>
+++ llvm/trunk/unittests/ProfileData/SampleProfTest.cpp Tue Mar 1 12:30:58 2016<br>
@@ -8,6 +8,7 @@<br>
//<br>
//===----------------------------------------------------------------------===//<br>
<br>
+#include "llvm/IR/Metadata.h"<br>
#include "llvm/ProfileData/SampleProfReader.h"<br>
#include "llvm/ProfileData/SampleProfWriter.h"<br>
#include "gtest/gtest.h"<br>
@@ -93,31 +94,43 @@ struct SampleProfTest : ::testing::Test<br>
ASSERT_EQ(20301u, ReadBarSamples.getTotalSamples());<br>
ASSERT_EQ(1437u, ReadBarSamples.getHeadSamples());<br>
<br>
- SampleProfileSummary &Summary = Reader->getSummary();<br>
- ASSERT_EQ(123603u, Summary.getTotalSamples());<br>
- ASSERT_EQ(6u, Summary.getNumLinesWithSamples());<br>
- ASSERT_EQ(2u, Summary.getNumFunctions());<br>
- ASSERT_EQ(1437u, Summary.getMaxHeadSamples());<br>
- ASSERT_EQ(60351u, Summary.getMaxSamplesPerLine());<br>
-<br>
- std::vector<ProfileSummaryEntry> &Details = Summary.getDetailedSummary();<br>
- uint32_t Cutoff = 800000;<br>
- auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) {<br>
- return PE.Cutoff == Cutoff;<br>
+ auto VerifySummary = [](SampleProfileSummary &Summary) mutable {<br>
+ ASSERT_EQ(123603u, Summary.getTotalSamples());<br>
+ ASSERT_EQ(6u, Summary.getNumLinesWithSamples());<br>
+ ASSERT_EQ(2u, Summary.getNumFunctions());<br>
+ ASSERT_EQ(1437u, Summary.getMaxHeadSamples());<br>
+ ASSERT_EQ(60351u, Summary.getMaxSamplesPerLine());<br>
+<br>
+ uint32_t Cutoff = 800000;<br>
+ auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) {<br>
+ return PE.Cutoff == Cutoff;<br>
+ };<br>
+ std::vector<ProfileSummaryEntry> &Details = Summary.getDetailedSummary();<br>
+ auto EightyPerc = std::find_if(Details.begin(), Details.end(), Predicate);<br>
+ Cutoff = 900000;<br>
+ auto NinetyPerc = std::find_if(Details.begin(), Details.end(), Predicate);<br>
+ Cutoff = 950000;<br>
+ auto NinetyFivePerc =<br>
+ std::find_if(Details.begin(), Details.end(), Predicate);<br>
+ Cutoff = 990000;<br>
+ auto NinetyNinePerc =<br>
+ std::find_if(Details.begin(), Details.end(), Predicate);<br>
+ ASSERT_EQ(60000u, EightyPerc->MinCount);<br>
+ ASSERT_EQ(60000u, NinetyPerc->MinCount);<br>
+ ASSERT_EQ(60000u, NinetyFivePerc->MinCount);<br>
+ ASSERT_EQ(610u, NinetyNinePerc->MinCount);<br>
};<br>
- auto EightyPerc = std::find_if(Details.begin(), Details.end(), Predicate);<br>
- Cutoff = 900000;<br>
- auto NinetyPerc = std::find_if(Details.begin(), Details.end(), Predicate);<br>
- Cutoff = 950000;<br>
- auto NinetyFivePerc =<br>
- std::find_if(Details.begin(), Details.end(), Predicate);<br>
- Cutoff = 990000;<br>
- auto NinetyNinePerc =<br>
- std::find_if(Details.begin(), Details.end(), Predicate);<br>
- ASSERT_EQ(60000u, EightyPerc->MinCount);<br>
- ASSERT_EQ(60000u, NinetyPerc->MinCount);<br>
- ASSERT_EQ(60000u, NinetyFivePerc->MinCount);<br>
- ASSERT_EQ(610u, NinetyNinePerc->MinCount);<br>
+<br>
+ SampleProfileSummary &Summary = Reader->getSummary();<br>
+ VerifySummary(Summary);<br>
+<br>
+ Metadata *MD = Summary.getMD(getGlobalContext());<br>
+ ASSERT_TRUE(MD);<br>
+ ProfileSummary *PS = ProfileSummary::getFromMD(MD);<br>
+ ASSERT_TRUE(PS);<br>
+ ASSERT_TRUE(isa<SampleProfileSummary>(PS));<br>
+ SampleProfileSummary *SPS = cast<SampleProfileSummary>(PS);<br>
+ VerifySummary(*SPS);<br>
}<br>
};<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>