Add function entry count metadata.

Summary:
This adds three Function methods to handle function entry counts:
setEntryCount() and getEntryCount().

Entry counts are stored under the MD_prof metadata node with the name
"function_entry_count". They are unsigned 64 bit values set by profilers
(instrumentation and sample profiler changes coming up).

Added documentation for new profile metadata and tests.

Reviewers: dexonsmith, bogner

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D9628

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@237260 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Diego Novillo
2015-05-13 15:13:45 +00:00
parent dcc57103b8
commit a3bccceda7
9 changed files with 146 additions and 0 deletions

View File

@@ -19,10 +19,13 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/CodeGen/ValueTypes.h"
#include "llvm/IR/CallSite.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/RWMutex.h"
@@ -986,3 +989,19 @@ void llvm::overrideFunctionAttribute(StringRef Kind, StringRef Value,
Attrs = Attrs.addAttribute(Ctx, AttributeSet::FunctionIndex, Kind, Value);
F.setAttributes(Attrs);
}
void Function::setEntryCount(uint64_t Count) {
MDBuilder MDB(getContext());
setMetadata(LLVMContext::MD_prof, MDB.createFunctionEntryCount(Count));
}
Optional<uint64_t> Function::getEntryCount() const {
MDNode *MD = getMetadata(LLVMContext::MD_prof);
if (MD && MD->getOperand(0))
if (MDString *MDS = dyn_cast<MDString>(MD->getOperand(0)))
if (MDS->getString().equals("function_entry_count")) {
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));
return CI->getValue().getZExtValue();
}
return None;
}

View File

@@ -53,6 +53,16 @@ MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
return MDNode::get(Context, Vals);
}
MDNode *MDBuilder::createFunctionEntryCount(uint64_t Count) {
SmallVector<Metadata *, 2> Vals(2);
Vals[0] = createString("function_entry_count");
Type *Int64Ty = Type::getInt64Ty(Context);
Vals[1] = createConstant(ConstantInt::get(Int64Ty, Count));
return MDNode::get(Context, Vals);
}
MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");

View File

@@ -400,6 +400,8 @@ private:
bool isReturnValue, const Value *V);
void VerifyFunctionAttrs(FunctionType *FT, AttributeSet Attrs,
const Value *V);
void VerifyFunctionMetadata(
const SmallVector<std::pair<unsigned, MDNode *>, 4> MDs);
void VerifyConstantExprBitcastType(const ConstantExpr *CE);
void VerifyStatepoint(ImmutableCallSite CS);
@@ -1463,6 +1465,36 @@ void Verifier::VerifyFunctionAttrs(FunctionType *FT, AttributeSet Attrs,
}
}
void Verifier::VerifyFunctionMetadata(
const SmallVector<std::pair<unsigned, MDNode *>, 4> MDs) {
if (MDs.empty())
return;
for (unsigned i = 0; i < MDs.size(); i++) {
if (MDs[i].first == LLVMContext::MD_prof) {
MDNode *MD = MDs[i].second;
Assert(MD->getNumOperands() == 2,
"!prof annotations should have exactly 2 operands", MD);
// Check first operand.
Assert(MD->getOperand(0) != nullptr, "first operand should not be null",
MD);
Assert(isa<MDString>(MD->getOperand(0)),
"expected string with name of the !prof annotation", MD);
MDString *MDS = cast<MDString>(MD->getOperand(0));
StringRef ProfName = MDS->getString();
Assert(ProfName.equals("function_entry_count"),
"first operand should be 'function_entry_count'", MD);
// Check second operand.
Assert(MD->getOperand(1) != nullptr, "second operand should not be null",
MD);
Assert(isa<ConstantAsMetadata>(MD->getOperand(1)),
"expected integer argument to function_entry_count", MD);
}
}
}
void Verifier::VerifyConstantExprBitcastType(const ConstantExpr *CE) {
if (CE->getOpcode() != Instruction::BitCast)
return;
@@ -1713,6 +1745,7 @@ void Verifier::visitFunction(const Function &F) {
SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
F.getAllMetadata(MDs);
assert(F.hasMetadata() != MDs.empty() && "Bit out-of-sync");
VerifyFunctionMetadata(MDs);
if (F.isMaterializable()) {
// Function has a body somewhere we can't see.