#include "llvm/Analysis/MemoryProfileInfo.h"
#include "llvm/Support/CommandLine.h"
using namespace llvm;
using namespace llvm::memprof;
#define DEBUG_TYPE "memory-profile-info"
cl::opt<float> MemProfAccessesPerByteColdThreshold(
"memprof-accesses-per-byte-cold-threshold", cl::init(10.0), cl::Hidden,
cl::desc("The threshold the accesses per byte must be under to consider "
"an allocation cold"));
cl::opt<unsigned> MemProfMinLifetimeColdThreshold(
"memprof-min-lifetime-cold-threshold", cl::init(200), cl::Hidden,
cl::desc("The minimum lifetime (s) for an allocation to be considered "
"cold"));
AllocationType llvm::memprof::getAllocType(uint64_t MaxAccessCount,
uint64_t MinSize,
uint64_t MinLifetime) {
if (((float)MaxAccessCount) / MinSize < MemProfAccessesPerByteColdThreshold &&
MinLifetime >= MemProfMinLifetimeColdThreshold * 1000)
return AllocationType::Cold;
return AllocationType::NotCold;
}
MDNode *llvm::memprof::buildCallstackMetadata(ArrayRef<uint64_t> CallStack,
LLVMContext &Ctx) {
std::vector<Metadata *> StackVals;
for (auto Id : CallStack) {
auto *StackValMD =
ValueAsMetadata::get(ConstantInt::get(Type::getInt64Ty(Ctx), Id));
StackVals.push_back(StackValMD);
}
return MDNode::get(Ctx, StackVals);
}
MDNode *llvm::memprof::getMIBStackNode(const MDNode *MIB) {
assert(MIB->getNumOperands() == 2);
return cast<MDNode>(MIB->getOperand(0));
}
AllocationType llvm::memprof::getMIBAllocType(const MDNode *MIB) {
assert(MIB->getNumOperands() == 2);
auto *MDS = dyn_cast<MDString>(MIB->getOperand(1));
assert(MDS);
if (MDS->getString().equals("cold"))
return AllocationType::Cold;
return AllocationType::NotCold;
}
static std::string getAllocTypeAttributeString(AllocationType Type) {
switch (Type) {
case AllocationType::NotCold:
return "notcold";
break;
case AllocationType::Cold:
return "cold";
break;
default:
assert(false && "Unexpected alloc type");
}
llvm_unreachable("invalid alloc type");
}
static void addAllocTypeAttribute(LLVMContext &Ctx, CallBase *CI,
AllocationType AllocType) {
auto AllocTypeString = getAllocTypeAttributeString(AllocType);
auto A = llvm::Attribute::get(Ctx, "memprof", AllocTypeString);
CI->addFnAttr(A);
}
static bool hasSingleAllocType(uint8_t AllocTypes) {
const unsigned NumAllocTypes = countPopulation(AllocTypes);
assert(NumAllocTypes != 0);
return NumAllocTypes == 1;
}
void CallStackTrie::addCallStack(AllocationType AllocType,
ArrayRef<uint64_t> StackIds) {
bool First = true;
CallStackTrieNode *Curr = nullptr;
for (auto StackId : StackIds) {
if (First) {
First = false;
if (Alloc) {
assert(AllocStackId == StackId);
Alloc->AllocTypes |= static_cast<uint8_t>(AllocType);
} else {
AllocStackId = StackId;
Alloc = new CallStackTrieNode(AllocType);
}
Curr = Alloc;
continue;
}
auto Next = Curr->Callers.find(StackId);
if (Next != Curr->Callers.end()) {
Curr = Next->second;
Curr->AllocTypes |= static_cast<uint8_t>(AllocType);
continue;
}
auto *New = new CallStackTrieNode(AllocType);
Curr->Callers[StackId] = New;
Curr = New;
}
assert(Curr);
}
void CallStackTrie::addCallStack(MDNode *MIB) {
MDNode *StackMD = getMIBStackNode(MIB);
assert(StackMD);
std::vector<uint64_t> CallStack;
CallStack.reserve(StackMD->getNumOperands());
for (auto &MIBStackIter : StackMD->operands()) {
auto *StackId = mdconst::dyn_extract<ConstantInt>(MIBStackIter);
assert(StackId);
CallStack.push_back(StackId->getZExtValue());
}
addCallStack(getMIBAllocType(MIB), CallStack);
}
static MDNode *createMIBNode(LLVMContext &Ctx,
std::vector<uint64_t> &MIBCallStack,
AllocationType AllocType) {
std::vector<Metadata *> MIBPayload(
{buildCallstackMetadata(MIBCallStack, Ctx)});
MIBPayload.push_back(
MDString::get(Ctx, getAllocTypeAttributeString(AllocType)));
return MDNode::get(Ctx, MIBPayload);
}
bool CallStackTrie::buildMIBNodes(CallStackTrieNode *Node, LLVMContext &Ctx,
std::vector<uint64_t> &MIBCallStack,
std::vector<Metadata *> &MIBNodes,
bool CalleeHasAmbiguousCallerContext) {
if (hasSingleAllocType(Node->AllocTypes)) {
MIBNodes.push_back(
createMIBNode(Ctx, MIBCallStack, (AllocationType)Node->AllocTypes));
return true;
}
if (!Node->Callers.empty()) {
bool NodeHasAmbiguousCallerContext = Node->Callers.size() > 1;
bool AddedMIBNodesForAllCallerContexts = true;
for (auto &Caller : Node->Callers) {
MIBCallStack.push_back(Caller.first);
AddedMIBNodesForAllCallerContexts &=
buildMIBNodes(Caller.second, Ctx, MIBCallStack, MIBNodes,
NodeHasAmbiguousCallerContext);
MIBCallStack.pop_back();
}
if (AddedMIBNodesForAllCallerContexts)
return true;
assert(!NodeHasAmbiguousCallerContext);
}
if (!CalleeHasAmbiguousCallerContext)
return false;
MIBNodes.push_back(createMIBNode(Ctx, MIBCallStack, AllocationType::NotCold));
return true;
}
bool CallStackTrie::buildAndAttachMIBMetadata(CallBase *CI) {
auto &Ctx = CI->getContext();
if (hasSingleAllocType(Alloc->AllocTypes)) {
addAllocTypeAttribute(Ctx, CI, (AllocationType)Alloc->AllocTypes);
return false;
}
std::vector<uint64_t> MIBCallStack;
MIBCallStack.push_back(AllocStackId);
std::vector<Metadata *> MIBNodes;
assert(!Alloc->Callers.empty() && "addCallStack has not been called yet");
buildMIBNodes(Alloc, Ctx, MIBCallStack, MIBNodes,
true);
assert(MIBCallStack.size() == 1 &&
"Should only be left with Alloc's location in stack");
CI->setMetadata(LLVMContext::MD_memprof, MDNode::get(Ctx, MIBNodes));
return true;
}