#ifndef LLVM_PROFILEDATA_SAMPLEPROF_H
#define LLVM_PROFILEDATA_SAMPLEPROF_H
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MathExtras.h"
#include <algorithm>
#include <cstdint>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <system_error>
#include <unordered_map>
#include <utility>
namespace llvm {
class DILocation;
class raw_ostream;
const std::error_category &sampleprof_category();
enum class sampleprof_error {
success = 0,
bad_magic,
unsupported_version,
too_large,
truncated,
malformed,
unrecognized_format,
unsupported_writing_format,
truncated_name_table,
not_implemented,
counter_overflow,
ostream_seek_unsupported,
uncompress_failed,
zlib_unavailable,
hash_mismatch
};
inline std::error_code make_error_code(sampleprof_error E) {
return std::error_code(static_cast<int>(E), sampleprof_category());
}
inline sampleprof_error MergeResult(sampleprof_error &Accumulator,
sampleprof_error Result) {
if (Accumulator == sampleprof_error::success &&
Result != sampleprof_error::success)
Accumulator = Result;
return Accumulator;
}
}
namespace std {
template <>
struct is_error_code_enum<llvm::sampleprof_error> : std::true_type {};
}
namespace llvm {
namespace sampleprof {
enum SampleProfileFormat {
SPF_None = 0,
SPF_Text = 0x1,
SPF_Compact_Binary = 0x2,
SPF_GCC = 0x3,
SPF_Ext_Binary = 0x4,
SPF_Binary = 0xff
};
static inline uint64_t SPMagic(SampleProfileFormat Format = SPF_Binary) {
return uint64_t('S') << (64 - 8) | uint64_t('P') << (64 - 16) |
uint64_t('R') << (64 - 24) | uint64_t('O') << (64 - 32) |
uint64_t('F') << (64 - 40) | uint64_t('4') << (64 - 48) |
uint64_t('2') << (64 - 56) | uint64_t(Format);
}
static inline StringRef getRepInFormat(StringRef Name, bool UseMD5,
std::string &GUIDBuf) {
if (Name.empty() || !UseMD5)
return Name;
GUIDBuf = std::to_string(Function::getGUID(Name));
return GUIDBuf;
}
static inline uint64_t SPVersion() { return 103; }
enum SecType {
SecInValid = 0,
SecProfSummary = 1,
SecNameTable = 2,
SecProfileSymbolList = 3,
SecFuncOffsetTable = 4,
SecFuncMetadata = 5,
SecCSNameTable = 6,
SecFuncProfileFirst = 32,
SecLBRProfile = SecFuncProfileFirst
};
static inline std::string getSecName(SecType Type) {
switch ((int)Type) { case SecInValid:
return "InvalidSection";
case SecProfSummary:
return "ProfileSummarySection";
case SecNameTable:
return "NameTableSection";
case SecProfileSymbolList:
return "ProfileSymbolListSection";
case SecFuncOffsetTable:
return "FuncOffsetTableSection";
case SecFuncMetadata:
return "FunctionMetadata";
case SecCSNameTable:
return "CSNameTableSection";
case SecLBRProfile:
return "LBRProfileSection";
default:
return "UnknownSection";
}
}
struct SecHdrTableEntry {
SecType Type;
uint64_t Flags;
uint64_t Offset;
uint64_t Size;
uint32_t LayoutIndex;
};
enum class SecCommonFlags : uint32_t {
SecFlagInValid = 0,
SecFlagCompress = (1 << 0),
SecFlagFlat = (1 << 1)
};
enum class SecNameTableFlags : uint32_t {
SecFlagInValid = 0,
SecFlagMD5Name = (1 << 0),
SecFlagFixedLengthMD5 = (1 << 1),
SecFlagUniqSuffix = (1 << 2)
};
enum class SecProfSummaryFlags : uint32_t {
SecFlagInValid = 0,
SecFlagPartial = (1 << 0),
SecFlagFullContext = (1 << 1),
SecFlagFSDiscriminator = (1 << 2),
SecFlagIsPreInlined = (1 << 4),
};
enum class SecFuncMetadataFlags : uint32_t {
SecFlagInvalid = 0,
SecFlagIsProbeBased = (1 << 0),
SecFlagHasAttribute = (1 << 1),
};
enum class SecFuncOffsetFlags : uint32_t {
SecFlagInvalid = 0,
SecFlagOrdered = (1 << 0),
};
template <class SecFlagType>
static inline void verifySecFlag(SecType Type, SecFlagType Flag) {
if (std::is_same<SecCommonFlags, SecFlagType>())
return;
bool IsFlagLegal = false;
switch (Type) {
case SecNameTable:
IsFlagLegal = std::is_same<SecNameTableFlags, SecFlagType>();
break;
case SecProfSummary:
IsFlagLegal = std::is_same<SecProfSummaryFlags, SecFlagType>();
break;
case SecFuncMetadata:
IsFlagLegal = std::is_same<SecFuncMetadataFlags, SecFlagType>();
break;
default:
case SecFuncOffsetTable:
IsFlagLegal = std::is_same<SecFuncOffsetFlags, SecFlagType>();
break;
}
if (!IsFlagLegal)
llvm_unreachable("Misuse of a flag in an incompatible section");
}
template <class SecFlagType>
static inline void addSecFlag(SecHdrTableEntry &Entry, SecFlagType Flag) {
verifySecFlag(Entry.Type, Flag);
auto FVal = static_cast<uint64_t>(Flag);
bool IsCommon = std::is_same<SecCommonFlags, SecFlagType>();
Entry.Flags |= IsCommon ? FVal : (FVal << 32);
}
template <class SecFlagType>
static inline void removeSecFlag(SecHdrTableEntry &Entry, SecFlagType Flag) {
verifySecFlag(Entry.Type, Flag);
auto FVal = static_cast<uint64_t>(Flag);
bool IsCommon = std::is_same<SecCommonFlags, SecFlagType>();
Entry.Flags &= ~(IsCommon ? FVal : (FVal << 32));
}
template <class SecFlagType>
static inline bool hasSecFlag(const SecHdrTableEntry &Entry, SecFlagType Flag) {
verifySecFlag(Entry.Type, Flag);
auto FVal = static_cast<uint64_t>(Flag);
bool IsCommon = std::is_same<SecCommonFlags, SecFlagType>();
return Entry.Flags & (IsCommon ? FVal : (FVal << 32));
}
struct LineLocation {
LineLocation(uint32_t L, uint32_t D) : LineOffset(L), Discriminator(D) {}
void print(raw_ostream &OS) const;
void dump() const;
bool operator<(const LineLocation &O) const {
return LineOffset < O.LineOffset ||
(LineOffset == O.LineOffset && Discriminator < O.Discriminator);
}
bool operator==(const LineLocation &O) const {
return LineOffset == O.LineOffset && Discriminator == O.Discriminator;
}
bool operator!=(const LineLocation &O) const {
return LineOffset != O.LineOffset || Discriminator != O.Discriminator;
}
uint32_t LineOffset;
uint32_t Discriminator;
};
raw_ostream &operator<<(raw_ostream &OS, const LineLocation &Loc);
class SampleRecord {
public:
using CallTarget = std::pair<StringRef, uint64_t>;
struct CallTargetComparator {
bool operator()(const CallTarget &LHS, const CallTarget &RHS) const {
if (LHS.second != RHS.second)
return LHS.second > RHS.second;
return LHS.first < RHS.first;
}
};
using SortedCallTargetSet = std::set<CallTarget, CallTargetComparator>;
using CallTargetMap = StringMap<uint64_t>;
SampleRecord() = default;
sampleprof_error addSamples(uint64_t S, uint64_t Weight = 1) {
bool Overflowed;
NumSamples = SaturatingMultiplyAdd(S, Weight, NumSamples, &Overflowed);
return Overflowed ? sampleprof_error::counter_overflow
: sampleprof_error::success;
}
uint64_t removeSamples(uint64_t S) {
if (S > NumSamples)
S = NumSamples;
NumSamples -= S;
return S;
}
sampleprof_error addCalledTarget(StringRef F, uint64_t S,
uint64_t Weight = 1) {
uint64_t &TargetSamples = CallTargets[F];
bool Overflowed;
TargetSamples =
SaturatingMultiplyAdd(S, Weight, TargetSamples, &Overflowed);
return Overflowed ? sampleprof_error::counter_overflow
: sampleprof_error::success;
}
uint64_t removeCalledTarget(StringRef F) {
uint64_t Count = 0;
auto I = CallTargets.find(F);
if (I != CallTargets.end()) {
Count = I->second;
CallTargets.erase(I);
}
return Count;
}
bool hasCalls() const { return !CallTargets.empty(); }
uint64_t getSamples() const { return NumSamples; }
const CallTargetMap &getCallTargets() const { return CallTargets; }
const SortedCallTargetSet getSortedCallTargets() const {
return SortCallTargets(CallTargets);
}
uint64_t getCallTargetSum() const {
uint64_t Sum = 0;
for (const auto &I : CallTargets)
Sum += I.second;
return Sum;
}
static const SortedCallTargetSet SortCallTargets(const CallTargetMap &Targets) {
SortedCallTargetSet SortedTargets;
for (const auto &I : Targets) {
SortedTargets.emplace(I.first(), I.second);
}
return SortedTargets;
}
static const CallTargetMap adjustCallTargets(const CallTargetMap &Targets,
float DistributionFactor) {
CallTargetMap AdjustedTargets;
for (const auto &I : Targets) {
AdjustedTargets[I.first()] = I.second * DistributionFactor;
}
return AdjustedTargets;
}
sampleprof_error merge(const SampleRecord &Other, uint64_t Weight = 1);
void print(raw_ostream &OS, unsigned Indent) const;
void dump() const;
private:
uint64_t NumSamples = 0;
CallTargetMap CallTargets;
};
raw_ostream &operator<<(raw_ostream &OS, const SampleRecord &Sample);
enum ContextStateMask {
UnknownContext = 0x0, RawContext = 0x1, SyntheticContext = 0x2, InlinedContext = 0x4, MergedContext = 0x8 };
enum ContextAttributeMask {
ContextNone = 0x0,
ContextWasInlined = 0x1, ContextShouldBeInlined = 0x2, ContextDuplicatedIntoBase =
0x4, };
struct SampleContextFrame {
StringRef FuncName;
LineLocation Location;
SampleContextFrame() : Location(0, 0) {}
SampleContextFrame(StringRef FuncName, LineLocation Location)
: FuncName(FuncName), Location(Location) {}
bool operator==(const SampleContextFrame &That) const {
return Location == That.Location && FuncName == That.FuncName;
}
bool operator!=(const SampleContextFrame &That) const {
return !(*this == That);
}
std::string toString(bool OutputLineLocation) const {
std::ostringstream OContextStr;
OContextStr << FuncName.str();
if (OutputLineLocation) {
OContextStr << ":" << Location.LineOffset;
if (Location.Discriminator)
OContextStr << "." << Location.Discriminator;
}
return OContextStr.str();
}
};
static inline hash_code hash_value(const SampleContextFrame &arg) {
return hash_combine(arg.FuncName, arg.Location.LineOffset,
arg.Location.Discriminator);
}
using SampleContextFrameVector = SmallVector<SampleContextFrame, 1>;
using SampleContextFrames = ArrayRef<SampleContextFrame>;
struct SampleContextFrameHash {
uint64_t operator()(const SampleContextFrameVector &S) const {
return hash_combine_range(S.begin(), S.end());
}
};
class SampleContext {
public:
SampleContext() : State(UnknownContext), Attributes(ContextNone) {}
SampleContext(StringRef Name)
: Name(Name), State(UnknownContext), Attributes(ContextNone) {}
SampleContext(SampleContextFrames Context,
ContextStateMask CState = RawContext)
: Attributes(ContextNone) {
assert(!Context.empty() && "Context is empty");
setContext(Context, CState);
}
SampleContext(StringRef ContextStr,
std::list<SampleContextFrameVector> &CSNameTable,
ContextStateMask CState = RawContext)
: Attributes(ContextNone) {
assert(!ContextStr.empty());
bool HasContext = ContextStr.startswith("[");
if (!HasContext) {
State = UnknownContext;
Name = ContextStr;
} else {
CSNameTable.emplace_back();
SampleContextFrameVector &Context = CSNameTable.back();
createCtxVectorFromStr(ContextStr, Context);
setContext(Context, CState);
}
}
static void createCtxVectorFromStr(StringRef ContextStr,
SampleContextFrameVector &Context) {
ContextStr = ContextStr.substr(1, ContextStr.size() - 2);
StringRef ContextRemain = ContextStr;
StringRef ChildContext;
StringRef CalleeName;
while (!ContextRemain.empty()) {
auto ContextSplit = ContextRemain.split(" @ ");
ChildContext = ContextSplit.first;
ContextRemain = ContextSplit.second;
LineLocation CallSiteLoc(0, 0);
decodeContextString(ChildContext, CalleeName, CallSiteLoc);
Context.emplace_back(CalleeName, CallSiteLoc);
}
}
static void decodeContextString(StringRef ContextStr, StringRef &FName,
LineLocation &LineLoc) {
auto EntrySplit = ContextStr.split(':');
FName = EntrySplit.first;
LineLoc = {0, 0};
if (!EntrySplit.second.empty()) {
int LineOffset = 0;
auto LocSplit = EntrySplit.second.split('.');
LocSplit.first.getAsInteger(10, LineOffset);
LineLoc.LineOffset = LineOffset;
if (!LocSplit.second.empty())
LocSplit.second.getAsInteger(10, LineLoc.Discriminator);
}
}
operator SampleContextFrames() const { return FullContext; }
bool hasAttribute(ContextAttributeMask A) { return Attributes & (uint32_t)A; }
void setAttribute(ContextAttributeMask A) { Attributes |= (uint32_t)A; }
uint32_t getAllAttributes() { return Attributes; }
void setAllAttributes(uint32_t A) { Attributes = A; }
bool hasState(ContextStateMask S) { return State & (uint32_t)S; }
void setState(ContextStateMask S) { State |= (uint32_t)S; }
void clearState(ContextStateMask S) { State &= (uint32_t)~S; }
bool hasContext() const { return State != UnknownContext; }
bool isBaseContext() const { return FullContext.size() == 1; }
StringRef getName() const { return Name; }
SampleContextFrames getContextFrames() const { return FullContext; }
static std::string getContextString(SampleContextFrames Context,
bool IncludeLeafLineLocation = false) {
std::ostringstream OContextStr;
for (uint32_t I = 0; I < Context.size(); I++) {
if (OContextStr.str().size()) {
OContextStr << " @ ";
}
OContextStr << Context[I].toString(I != Context.size() - 1 ||
IncludeLeafLineLocation);
}
return OContextStr.str();
}
std::string toString() const {
if (!hasContext())
return Name.str();
return getContextString(FullContext, false);
}
uint64_t getHashCode() const {
return hasContext() ? hash_value(getContextFrames())
: hash_value(getName());
}
void setName(StringRef FunctionName) {
Name = FunctionName;
FullContext = SampleContextFrames();
State = UnknownContext;
}
void setContext(SampleContextFrames Context,
ContextStateMask CState = RawContext) {
assert(CState != UnknownContext);
FullContext = Context;
Name = Context.back().FuncName;
State = CState;
}
bool operator==(const SampleContext &That) const {
return State == That.State && Name == That.Name &&
FullContext == That.FullContext;
}
bool operator!=(const SampleContext &That) const { return !(*this == That); }
bool operator<(const SampleContext &That) const {
if (State != That.State)
return State < That.State;
if (!hasContext()) {
return (Name.compare(That.Name)) == -1;
}
uint64_t I = 0;
while (I < std::min(FullContext.size(), That.FullContext.size())) {
auto &Context1 = FullContext[I];
auto &Context2 = That.FullContext[I];
auto V = Context1.FuncName.compare(Context2.FuncName);
if (V)
return V == -1;
if (Context1.Location != Context2.Location)
return Context1.Location < Context2.Location;
I++;
}
return FullContext.size() < That.FullContext.size();
}
struct Hash {
uint64_t operator()(const SampleContext &Context) const {
return Context.getHashCode();
}
};
bool IsPrefixOf(const SampleContext &That) const {
auto ThisContext = FullContext;
auto ThatContext = That.FullContext;
if (ThatContext.size() < ThisContext.size())
return false;
ThatContext = ThatContext.take_front(ThisContext.size());
if (ThisContext.back().FuncName != ThatContext.back().FuncName)
return false;
return ThisContext.drop_back() == ThatContext.drop_back();
}
private:
StringRef Name;
SampleContextFrames FullContext;
uint32_t State;
uint32_t Attributes;
};
static inline hash_code hash_value(const SampleContext &arg) {
return arg.hasContext() ? hash_value(arg.getContextFrames())
: hash_value(arg.getName());
}
class FunctionSamples;
class SampleProfileReaderItaniumRemapper;
using BodySampleMap = std::map<LineLocation, SampleRecord>;
using FunctionSamplesMap = std::map<std::string, FunctionSamples, std::less<>>;
using CallsiteSampleMap = std::map<LineLocation, FunctionSamplesMap>;
class FunctionSamples {
public:
FunctionSamples() = default;
void print(raw_ostream &OS = dbgs(), unsigned Indent = 0) const;
void dump() const;
sampleprof_error addTotalSamples(uint64_t Num, uint64_t Weight = 1) {
bool Overflowed;
TotalSamples =
SaturatingMultiplyAdd(Num, Weight, TotalSamples, &Overflowed);
return Overflowed ? sampleprof_error::counter_overflow
: sampleprof_error::success;
}
void removeTotalSamples(uint64_t Num) {
if (TotalSamples < Num)
TotalSamples = 0;
else
TotalSamples -= Num;
}
void setTotalSamples(uint64_t Num) { TotalSamples = Num; }
sampleprof_error addHeadSamples(uint64_t Num, uint64_t Weight = 1) {
bool Overflowed;
TotalHeadSamples =
SaturatingMultiplyAdd(Num, Weight, TotalHeadSamples, &Overflowed);
return Overflowed ? sampleprof_error::counter_overflow
: sampleprof_error::success;
}
sampleprof_error addBodySamples(uint32_t LineOffset, uint32_t Discriminator,
uint64_t Num, uint64_t Weight = 1) {
return BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(
Num, Weight);
}
sampleprof_error addCalledTargetSamples(uint32_t LineOffset,
uint32_t Discriminator,
StringRef FName, uint64_t Num,
uint64_t Weight = 1) {
return BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(
FName, Num, Weight);
}
uint64_t removeCalledTargetAndBodySample(uint32_t LineOffset,
uint32_t Discriminator,
StringRef FName) {
uint64_t Count = 0;
auto I = BodySamples.find(LineLocation(LineOffset, Discriminator));
if (I != BodySamples.end()) {
Count = I->second.removeCalledTarget(FName);
Count = I->second.removeSamples(Count);
if (!I->second.getSamples())
BodySamples.erase(I);
}
return Count;
}
sampleprof_error addBodySamplesForProbe(uint32_t Index, uint64_t Num,
uint64_t Weight = 1) {
SampleRecord S;
S.addSamples(Num, Weight);
return BodySamples[LineLocation(Index, 0)].merge(S, Weight);
}
void updateCallsiteSamples() {
for (auto &I : BodySamples) {
uint64_t TargetSamples = I.second.getCallTargetSum();
if (TargetSamples > I.second.getSamples())
I.second.addSamples(TargetSamples - I.second.getSamples());
}
}
void updateTotalSamples() {
setTotalSamples(0);
for (const auto &I : BodySamples)
addTotalSamples(I.second.getSamples());
for (auto &I : CallsiteSamples) {
for (auto &CS : I.second) {
CS.second.updateTotalSamples();
addTotalSamples(CS.second.getTotalSamples());
}
}
}
void SetContextSynthetic() {
Context.setState(SyntheticContext);
for (auto &I : CallsiteSamples) {
for (auto &CS : I.second) {
CS.second.SetContextSynthetic();
}
}
}
ErrorOr<uint64_t> findSamplesAt(uint32_t LineOffset,
uint32_t Discriminator) const {
const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
if (ret == BodySamples.end())
return std::error_code();
return ret->second.getSamples();
}
ErrorOr<SampleRecord::CallTargetMap>
findCallTargetMapAt(uint32_t LineOffset, uint32_t Discriminator) const {
const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
if (ret == BodySamples.end())
return std::error_code();
return ret->second.getCallTargets();
}
ErrorOr<SampleRecord::CallTargetMap>
findCallTargetMapAt(const LineLocation &CallSite) const {
const auto &Ret = BodySamples.find(CallSite);
if (Ret == BodySamples.end())
return std::error_code();
return Ret->second.getCallTargets();
}
FunctionSamplesMap &functionSamplesAt(const LineLocation &Loc) {
return CallsiteSamples[Loc];
}
const FunctionSamplesMap *
findFunctionSamplesMapAt(const LineLocation &Loc) const {
auto iter = CallsiteSamples.find(Loc);
if (iter == CallsiteSamples.end())
return nullptr;
return &iter->second;
}
const FunctionSamples *
findFunctionSamplesAt(const LineLocation &Loc, StringRef CalleeName,
SampleProfileReaderItaniumRemapper *Remapper) const;
bool empty() const { return TotalSamples == 0; }
uint64_t getTotalSamples() const { return TotalSamples; }
uint64_t getHeadSamples() const { return TotalHeadSamples; }
uint64_t getHeadSamplesEstimate() const {
if (FunctionSamples::ProfileIsCS && getHeadSamples()) {
return getHeadSamples();
}
uint64_t Count = 0;
if (!BodySamples.empty() &&
(CallsiteSamples.empty() ||
BodySamples.begin()->first < CallsiteSamples.begin()->first))
Count = BodySamples.begin()->second.getSamples();
else if (!CallsiteSamples.empty()) {
for (const auto &N_FS : CallsiteSamples.begin()->second)
Count += N_FS.second.getHeadSamplesEstimate();
}
return Count ? Count : TotalSamples > 0;
}
const BodySampleMap &getBodySamples() const { return BodySamples; }
const CallsiteSampleMap &getCallsiteSamples() const {
return CallsiteSamples;
}
uint64_t getMaxCountInside() const {
uint64_t MaxCount = 0;
for (const auto &L : getBodySamples())
MaxCount = std::max(MaxCount, L.second.getSamples());
for (const auto &C : getCallsiteSamples())
for (const FunctionSamplesMap::value_type &F : C.second)
MaxCount = std::max(MaxCount, F.second.getMaxCountInside());
return MaxCount;
}
sampleprof_error merge(const FunctionSamples &Other, uint64_t Weight = 1) {
sampleprof_error Result = sampleprof_error::success;
if (!GUIDToFuncNameMap)
GUIDToFuncNameMap = Other.GUIDToFuncNameMap;
if (Context.getName().empty())
Context = Other.getContext();
if (FunctionHash == 0) {
FunctionHash = Other.getFunctionHash();
} else if (FunctionHash != Other.getFunctionHash()) {
return sampleprof_error::hash_mismatch;
}
MergeResult(Result, addTotalSamples(Other.getTotalSamples(), Weight));
MergeResult(Result, addHeadSamples(Other.getHeadSamples(), Weight));
for (const auto &I : Other.getBodySamples()) {
const LineLocation &Loc = I.first;
const SampleRecord &Rec = I.second;
MergeResult(Result, BodySamples[Loc].merge(Rec, Weight));
}
for (const auto &I : Other.getCallsiteSamples()) {
const LineLocation &Loc = I.first;
FunctionSamplesMap &FSMap = functionSamplesAt(Loc);
for (const auto &Rec : I.second)
MergeResult(Result, FSMap[Rec.first].merge(Rec.second, Weight));
}
return Result;
}
void findInlinedFunctions(DenseSet<GlobalValue::GUID> &S,
const StringMap<Function *> &SymbolMap,
uint64_t Threshold) const {
if (TotalSamples <= Threshold)
return;
auto isDeclaration = [](const Function *F) {
return !F || F->isDeclaration();
};
if (isDeclaration(SymbolMap.lookup(getFuncName()))) {
S.insert(getGUID(getName()));
}
for (const auto &BS : BodySamples)
for (const auto &TS : BS.second.getCallTargets())
if (TS.getValue() > Threshold) {
const Function *Callee = SymbolMap.lookup(getFuncName(TS.getKey()));
if (isDeclaration(Callee))
S.insert(getGUID(TS.getKey()));
}
for (const auto &CS : CallsiteSamples)
for (const auto &NameFS : CS.second)
NameFS.second.findInlinedFunctions(S, SymbolMap, Threshold);
}
void setName(StringRef FunctionName) { Context.setName(FunctionName); }
StringRef getName() const { return Context.getName(); }
StringRef getFuncName() const { return getFuncName(getName()); }
void setFunctionHash(uint64_t Hash) { FunctionHash = Hash; }
uint64_t getFunctionHash() const { return FunctionHash; }
static StringRef getCanonicalFnName(const Function &F) {
auto AttrName = "sample-profile-suffix-elision-policy";
auto Attr = F.getFnAttribute(AttrName).getValueAsString();
return getCanonicalFnName(F.getName(), Attr);
}
static constexpr const char *LLVMSuffix = ".llvm.";
static constexpr const char *PartSuffix = ".part.";
static constexpr const char *UniqSuffix = ".__uniq.";
static StringRef getCanonicalFnName(StringRef FnName,
StringRef Attr = "selected") {
const char *knownSuffixes[] = {LLVMSuffix, PartSuffix, UniqSuffix};
if (Attr == "" || Attr == "all") {
return FnName.split('.').first;
} else if (Attr == "selected") {
StringRef Cand(FnName);
for (const auto &Suf : knownSuffixes) {
StringRef Suffix(Suf);
if (Suffix == UniqSuffix && FunctionSamples::HasUniqSuffix)
continue;
auto It = Cand.rfind(Suffix);
if (It == StringRef::npos)
continue;
auto Dit = Cand.rfind('.');
if (Dit == It + Suffix.size() - 1)
Cand = Cand.substr(0, It);
}
return Cand;
} else if (Attr == "none") {
return FnName;
} else {
assert(false && "internal error: unknown suffix elision policy");
}
return FnName;
}
StringRef getFuncName(StringRef Name) const {
if (!UseMD5)
return Name;
assert(GUIDToFuncNameMap && "GUIDToFuncNameMap needs to be populated first");
return GUIDToFuncNameMap->lookup(std::stoull(Name.data()));
}
static unsigned getOffset(const DILocation *DIL);
static LineLocation getCallSiteIdentifier(const DILocation *DIL,
bool ProfileIsFS = false);
static uint64_t getCallSiteHash(StringRef CalleeName,
const LineLocation &Callsite);
const FunctionSamples *findFunctionSamples(
const DILocation *DIL,
SampleProfileReaderItaniumRemapper *Remapper = nullptr) const;
static bool ProfileIsProbeBased;
static bool ProfileIsCS;
static bool ProfileIsPreInlined;
SampleContext &getContext() const { return Context; }
void setContext(const SampleContext &FContext) { Context = FContext; }
static bool UseMD5;
static bool HasUniqSuffix;
static bool ProfileIsFS;
DenseMap<uint64_t, StringRef> *GUIDToFuncNameMap = nullptr;
static uint64_t getGUID(StringRef Name) {
return UseMD5 ? std::stoull(Name.data()) : Function::getGUID(Name);
}
void findAllNames(DenseSet<StringRef> &NameSet) const;
private:
uint64_t FunctionHash = 0;
mutable SampleContext Context;
uint64_t TotalSamples = 0;
uint64_t TotalHeadSamples = 0;
BodySampleMap BodySamples;
CallsiteSampleMap CallsiteSamples;
};
raw_ostream &operator<<(raw_ostream &OS, const FunctionSamples &FS);
using SampleProfileMap =
std::unordered_map<SampleContext, FunctionSamples, SampleContext::Hash>;
using NameFunctionSamples = std::pair<SampleContext, const FunctionSamples *>;
void sortFuncProfiles(const SampleProfileMap &ProfileMap,
std::vector<NameFunctionSamples> &SortedProfiles);
template <class LocationT, class SampleT> class SampleSorter {
public:
using SamplesWithLoc = std::pair<const LocationT, SampleT>;
using SamplesWithLocList = SmallVector<const SamplesWithLoc *, 20>;
SampleSorter(const std::map<LocationT, SampleT> &Samples) {
for (const auto &I : Samples)
V.push_back(&I);
llvm::stable_sort(V, [](const SamplesWithLoc *A, const SamplesWithLoc *B) {
return A->first < B->first;
});
}
const SamplesWithLocList &get() const { return V; }
private:
SamplesWithLocList V;
};
class SampleContextTrimmer {
public:
SampleContextTrimmer(SampleProfileMap &Profiles) : ProfileMap(Profiles){};
void trimAndMergeColdContextProfiles(uint64_t ColdCountThreshold,
bool TrimColdContext,
bool MergeColdContext,
uint32_t ColdContextFrameLength,
bool TrimBaseProfileOnly);
void canonicalizeContextProfiles();
private:
SampleProfileMap &ProfileMap;
};
class CSProfileConverter {
public:
CSProfileConverter(SampleProfileMap &Profiles);
void convertProfiles();
struct FrameNode {
FrameNode(StringRef FName = StringRef(),
FunctionSamples *FSamples = nullptr,
LineLocation CallLoc = {0, 0})
: FuncName(FName), FuncSamples(FSamples), CallSiteLoc(CallLoc){};
std::map<uint64_t, FrameNode> AllChildFrames;
StringRef FuncName;
FunctionSamples *FuncSamples;
LineLocation CallSiteLoc;
FrameNode *getOrCreateChildFrame(const LineLocation &CallSite,
StringRef CalleeName);
};
private:
void convertProfiles(FrameNode &Node);
FrameNode *getOrCreateContextPath(const SampleContext &Context);
SampleProfileMap &ProfileMap;
FrameNode RootFrame;
};
class ProfileSymbolList {
public:
void add(StringRef Name, bool copy = false) {
if (!copy) {
Syms.insert(Name);
return;
}
Syms.insert(Name.copy(Allocator));
}
bool contains(StringRef Name) { return Syms.count(Name); }
void merge(const ProfileSymbolList &List) {
for (auto Sym : List.Syms)
add(Sym, true);
}
unsigned size() { return Syms.size(); }
void setToCompress(bool TC) { ToCompress = TC; }
bool toCompress() { return ToCompress; }
std::error_code read(const uint8_t *Data, uint64_t ListSize);
std::error_code write(raw_ostream &OS);
void dump(raw_ostream &OS = dbgs()) const;
private:
bool ToCompress = false;
DenseSet<StringRef> Syms;
BumpPtrAllocator Allocator;
};
}
using namespace sampleprof;
template <> struct DenseMapInfo<SampleContext> {
static inline SampleContext getEmptyKey() { return SampleContext(); }
static inline SampleContext getTombstoneKey() { return SampleContext("@"); }
static unsigned getHashValue(const SampleContext &Val) {
return Val.getHashCode();
}
static bool isEqual(const SampleContext &LHS, const SampleContext &RHS) {
return LHS == RHS;
}
};
}
#endif