summaryrefslogtreecommitdiff
path: root/llvm/lib/IR/ProfileSummary.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/IR/ProfileSummary.cpp')
-rw-r--r--llvm/lib/IR/ProfileSummary.cpp140
1 files changed, 111 insertions, 29 deletions
diff --git a/llvm/lib/IR/ProfileSummary.cpp b/llvm/lib/IR/ProfileSummary.cpp
index 11d95ac19be6a..ac6bcd9fe3afd 100644
--- a/llvm/lib/IR/ProfileSummary.cpp
+++ b/llvm/lib/IR/ProfileSummary.cpp
@@ -18,6 +18,7 @@
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/Casting.h"
+#include "llvm/Support/Format.h"
using namespace llvm;
@@ -31,6 +32,14 @@ static Metadata *getKeyValMD(LLVMContext &Context, const char *Key,
return MDTuple::get(Context, Ops);
}
+static Metadata *getKeyFPValMD(LLVMContext &Context, const char *Key,
+ double Val) {
+ Type *DoubleTy = Type::getDoubleTy(Context);
+ Metadata *Ops[2] = {MDString::get(Context, Key),
+ ConstantAsMetadata::get(ConstantFP::get(DoubleTy, Val))};
+ return MDTuple::get(Context, Ops);
+}
+
// Return an MDTuple with two elements. The first element is a string Key and
// the second is a string Value.
static Metadata *getKeyValMD(LLVMContext &Context, const char *Key,
@@ -65,35 +74,63 @@ Metadata *ProfileSummary::getDetailedSummaryMD(LLVMContext &Context) {
// "ProfileFormat" and a string representing the format ("InstrProf" or
// "SampleProfile"). The rest of the elements of the outer MDTuple are specific
// to the kind of profile summary as returned by getFormatSpecificMD.
-Metadata *ProfileSummary::getMD(LLVMContext &Context) {
+// IsPartialProfile is an optional field and \p AddPartialField will decide
+// whether to add a field for it.
+// PartialProfileRatio is an optional field and \p AddPartialProfileRatioField
+// will decide whether to add a field for it.
+Metadata *ProfileSummary::getMD(LLVMContext &Context, bool AddPartialField,
+ bool AddPartialProfileRatioField) {
const char *KindStr[3] = {"InstrProf", "CSInstrProf", "SampleProfile"};
- Metadata *Components[] = {
- getKeyValMD(Context, "ProfileFormat", KindStr[PSK]),
- getKeyValMD(Context, "TotalCount", getTotalCount()),
- getKeyValMD(Context, "MaxCount", getMaxCount()),
- getKeyValMD(Context, "MaxInternalCount", getMaxInternalCount()),
- getKeyValMD(Context, "MaxFunctionCount", getMaxFunctionCount()),
- getKeyValMD(Context, "NumCounts", getNumCounts()),
- getKeyValMD(Context, "NumFunctions", getNumFunctions()),
- getDetailedSummaryMD(Context),
- };
+ SmallVector<Metadata *, 16> Components;
+ Components.push_back(getKeyValMD(Context, "ProfileFormat", KindStr[PSK]));
+ Components.push_back(getKeyValMD(Context, "TotalCount", getTotalCount()));
+ Components.push_back(getKeyValMD(Context, "MaxCount", getMaxCount()));
+ Components.push_back(
+ getKeyValMD(Context, "MaxInternalCount", getMaxInternalCount()));
+ Components.push_back(
+ getKeyValMD(Context, "MaxFunctionCount", getMaxFunctionCount()));
+ Components.push_back(getKeyValMD(Context, "NumCounts", getNumCounts()));
+ Components.push_back(getKeyValMD(Context, "NumFunctions", getNumFunctions()));
+ if (AddPartialField)
+ Components.push_back(
+ getKeyValMD(Context, "IsPartialProfile", isPartialProfile()));
+ if (AddPartialProfileRatioField)
+ Components.push_back(getKeyFPValMD(Context, "PartialProfileRatio",
+ getPartialProfileRatio()));
+ Components.push_back(getDetailedSummaryMD(Context));
return MDTuple::get(Context, Components);
}
-// Parse an MDTuple representing (Key, Val) pair.
-static bool getVal(MDTuple *MD, const char *Key, uint64_t &Val) {
+// Get the value metadata for the input MD/Key.
+static ConstantAsMetadata *getValMD(MDTuple *MD, const char *Key) {
if (!MD)
- return false;
+ return nullptr;
if (MD->getNumOperands() != 2)
- return false;
+ return nullptr;
MDString *KeyMD = dyn_cast<MDString>(MD->getOperand(0));
ConstantAsMetadata *ValMD = dyn_cast<ConstantAsMetadata>(MD->getOperand(1));
if (!KeyMD || !ValMD)
- return false;
+ return nullptr;
if (!KeyMD->getString().equals(Key))
- return false;
- Val = cast<ConstantInt>(ValMD->getValue())->getZExtValue();
- return true;
+ return nullptr;
+ return ValMD;
+}
+
+// Parse an MDTuple representing (Key, Val) pair.
+static bool getVal(MDTuple *MD, const char *Key, uint64_t &Val) {
+ if (auto *ValMD = getValMD(MD, Key)) {
+ Val = cast<ConstantInt>(ValMD->getValue())->getZExtValue();
+ return true;
+ }
+ return false;
+}
+
+static bool getVal(MDTuple *MD, const char *Key, double &Val) {
+ if (auto *ValMD = getValMD(MD, Key)) {
+ Val = cast<ConstantFP>(ValMD->getValue())->getValueAPF().convertToDouble();
+ return true;
+ }
+ return false;
}
// Check if an MDTuple represents a (Key, Val) pair.
@@ -139,12 +176,29 @@ static bool getSummaryFromMD(MDTuple *MD, SummaryEntryVector &Summary) {
return true;
}
+// Get the value of an optional field. Increment 'Idx' if it was present. Return
+// true if we can move onto the next field.
+template <typename ValueType>
+static bool getOptionalVal(MDTuple *Tuple, unsigned &Idx, const char *Key,
+ ValueType &Value) {
+ if (getVal(dyn_cast<MDTuple>(Tuple->getOperand(Idx)), Key, Value)) {
+ Idx++;
+ // Need to make sure when the key is present, we won't step over the bound
+ // of Tuple operand array. Since (non-optional) DetailedSummary always comes
+ // last, the next entry in the tuple operand array must exist.
+ return Idx < Tuple->getNumOperands();
+ }
+ // It was absent, keep going.
+ return true;
+}
+
ProfileSummary *ProfileSummary::getFromMD(Metadata *MD) {
MDTuple *Tuple = dyn_cast_or_null<MDTuple>(MD);
- if (!Tuple || Tuple->getNumOperands() != 8)
+ if (!Tuple || Tuple->getNumOperands() < 8 || Tuple->getNumOperands() > 10)
return nullptr;
- auto &FormatMD = Tuple->getOperand(0);
+ unsigned I = 0;
+ auto &FormatMD = Tuple->getOperand(I++);
ProfileSummary::Kind SummaryKind;
if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",
"SampleProfile"))
@@ -160,27 +214,55 @@ ProfileSummary *ProfileSummary::getFromMD(Metadata *MD) {
uint64_t NumCounts, TotalCount, NumFunctions, MaxFunctionCount, MaxCount,
MaxInternalCount;
- if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(1)), "TotalCount",
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "TotalCount",
TotalCount))
return nullptr;
- if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(2)), "MaxCount", MaxCount))
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "MaxCount", MaxCount))
return nullptr;
- if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(3)), "MaxInternalCount",
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "MaxInternalCount",
MaxInternalCount))
return nullptr;
- if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(4)), "MaxFunctionCount",
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "MaxFunctionCount",
MaxFunctionCount))
return nullptr;
- if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(5)), "NumCounts", NumCounts))
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "NumCounts",
+ NumCounts))
return nullptr;
- if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(6)), "NumFunctions",
+ if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "NumFunctions",
NumFunctions))
return nullptr;
+ // Optional fields. Need to initialize because the fields are optional.
+ uint64_t IsPartialProfile = 0;
+ if (!getOptionalVal(Tuple, I, "IsPartialProfile", IsPartialProfile))
+ return nullptr;
+ double PartialProfileRatio = 0;
+ if (!getOptionalVal(Tuple, I, "PartialProfileRatio", PartialProfileRatio))
+ return nullptr;
+
SummaryEntryVector Summary;
- if (!getSummaryFromMD(dyn_cast<MDTuple>(Tuple->getOperand(7)), Summary))
+ if (!getSummaryFromMD(dyn_cast<MDTuple>(Tuple->getOperand(I++)), Summary))
return nullptr;
return new ProfileSummary(SummaryKind, std::move(Summary), TotalCount,
MaxCount, MaxInternalCount, MaxFunctionCount,
- NumCounts, NumFunctions);
+ NumCounts, NumFunctions, IsPartialProfile,
+ PartialProfileRatio);
+}
+
+void ProfileSummary::printSummary(raw_ostream &OS) {
+ OS << "Total functions: " << NumFunctions << "\n";
+ OS << "Maximum function count: " << MaxFunctionCount << "\n";
+ OS << "Maximum block count: " << MaxCount << "\n";
+ OS << "Total number of blocks: " << NumCounts << "\n";
+ OS << "Total count: " << TotalCount << "\n";
+}
+
+void ProfileSummary::printDetailedSummary(raw_ostream &OS) {
+ OS << "Detailed summary:\n";
+ for (auto Entry : DetailedSummary) {
+ OS << Entry.NumCounts << " blocks with count >= " << Entry.MinCount
+ << " account for "
+ << format("%0.6g", (float)Entry.Cutoff / Scale * 100)
+ << " percentage of the total counts.\n";
+ }
}