14 #ifndef LLVM_PROFILEDATA_SAMPLEPROF_H 15 #define LLVM_PROFILEDATA_SAMPLEPROF_H 34 #include <system_error> 85 namespace sampleprof {
97 return uint64_t(
'S') << (64 - 8) | uint64_t(
'P') << (64 - 16) |
98 uint64_t(
'R') << (64 - 24) | uint64_t(
'O') << (64 - 32) |
99 uint64_t(
'F') << (64 - 40) | uint64_t(
'4') << (64 - 48) |
100 uint64_t(
'2') << (64 - 56) | uint64_t(
Format);
106 std::string &GUIDBuf) {
132 return "InvalidSection";
134 return "ProfileSummarySection";
136 return "NameTableSection";
138 return "ProfileSymbolListSection";
140 return "FuncOffsetTableSection";
142 return "LBRProfileSection";
159 Entry.
Flags |= Flags;
163 Entry.
Flags &= ~Flags;
211 if (LHS.second != RHS.second)
212 return LHS.second > RHS.second;
214 return LHS.first < RHS.first;
240 uint64_t Weight = 1) {
241 uint64_t &TargetSamples = CallTargets[
F];
250 bool hasCalls()
const {
return !CallTargets.empty(); }
255 return SortCallTargets(CallTargets);
261 for (
const auto &
I : Targets) {
262 SortedTargets.emplace(
I.first(),
I.second);
264 return SortedTargets;
272 MergeResult(Result, addCalledTarget(
I.first(),
I.second, Weight));
281 uint64_t NumSamples = 0;
324 uint64_t Num, uint64_t Weight = 1) {
325 return BodySamples[
LineLocation(LineOffset, Discriminator)].addSamples(
332 uint64_t Weight = 1) {
333 return BodySamples[
LineLocation(LineOffset, Discriminator)].addCalledTarget(
342 const auto &ret = BodySamples.find(
LineLocation(LineOffset, Discriminator));
343 if (ret == BodySamples.end())
344 return std::error_code();
346 return ret->second.getSamples();
354 const auto &ret = BodySamples.find(
LineLocation(LineOffset, Discriminator));
355 if (ret == BodySamples.end())
356 return std::error_code();
357 return ret->second.getCallTargets();
362 return CallsiteSamples[Loc];
368 auto iter = CallsiteSamples.find(Loc);
369 if (iter == CallsiteSamples.end())
371 return &iter->second;
380 std::string CalleeGUID;
383 auto iter = CallsiteSamples.find(Loc);
384 if (iter == CallsiteSamples.end())
386 auto FS = iter->second.find(CalleeName);
387 if (
FS != iter->second.end())
394 uint64_t MaxTotalSamples = 0;
396 for (
const auto &NameFS : iter->second)
397 if (NameFS.second.getTotalSamples() >= MaxTotalSamples) {
398 MaxTotalSamples = NameFS.second.getTotalSamples();
404 bool empty()
const {
return TotalSamples == 0; }
421 if (!BodySamples.empty() &&
422 (CallsiteSamples.empty() ||
423 BodySamples.begin()->first < CallsiteSamples.begin()->first))
424 return BodySamples.begin()->second.getSamples();
425 if (!CallsiteSamples.empty()) {
429 for (
const auto &N_FS : CallsiteSamples.begin()->second)
430 T += N_FS.second.getEntrySamples();
441 return CallsiteSamples;
454 MergeResult(Result, BodySamples[Loc].merge(Rec, Weight));
459 for (
const auto &Rec :
I.second)
460 MergeResult(Result, FSMap[Rec.first].merge(Rec.second, Weight));
471 if (TotalSamples <= Threshold)
476 for (
const auto &BS : BodySamples)
477 for (
const auto &TS : BS.second.getCallTargets())
482 S.
insert(getGUID(TS.getKey()));
484 for (
const auto &CS : CallsiteSamples)
485 for (
const auto &NameFS : CS.second)
486 NameFS.second.findInlinedFunctions(S, M, Threshold);
497 return getNameInModule(
Name, M);
503 static const char *knownSuffixes[] = {
".llvm.",
".part." };
504 auto AttrName =
"sample-profile-suffix-elision-policy";
506 if (Attr ==
"" || Attr ==
"all") {
508 }
else if (Attr ==
"selected") {
510 for (
const auto &Suf : knownSuffixes) {
512 auto It = Cand.rfind(Suffix);
515 auto Dit = Cand.rfind(
'.');
516 if (Dit == It + Suffix.
size() - 1)
517 Cand = Cand.substr(0, It);
520 }
else if (Attr ==
"none") {
523 assert(
false &&
"internal error: unknown suffix elision policy");
538 assert(GUIDToFuncNameMap &&
"GUIDToFuncNameMap needs to be popluated first");
539 auto iter = GUIDToFuncNameMap->find(std::stoull(Name.
data()));
540 if (iter == GUIDToFuncNameMap->end())
582 uint64_t TotalSamples = 0;
587 uint64_t TotalHeadSamples = 0;
627 for (
const auto &
I : Samples)
630 return A->first < B->first;
659 for (
auto Sym : List.Syms)
663 unsigned size() {
return Syms.size(); }
668 std::error_code
read(
const uint8_t *
Data, uint64_t ListSize);
676 bool ToCompress =
false;
684 #endif // LLVM_PROFILEDATA_SAMPLEPROF_H const FunctionSamplesMap * findFunctionSamplesMapAt(const LineLocation &Loc) const
Returns the FunctionSamplesMap at the given Loc.
static uint64_t getGUID(StringRef Name)
Represents either an error or a value T.
This class represents lattice values for constants.
static StringRef getRepInFormat(StringRef Name, SampleProfileFormat Format, std::string &GUIDBuf)
A Module instance is used to store all the information related to an LLVM module. ...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
Implements a dense probed hash-table based set.
sampleprof_error merge(const SampleRecord &Other, uint64_t Weight=1)
Merge the samples in Other into this record.
bool contains(StringRef Name)
LineLocation(uint32_t L, uint32_t D)
SampleSorter(const std::map< LocationT, SampleT > &Samples)
static void removeSecFlags(SecHdrTableEntry &Entry, uint64_t Flags)
ErrorOr< uint64_t > findSamplesAt(uint32_t LineOffset, uint32_t Discriminator) const
Return the number of samples collected at the given location.
static void addSecFlags(SecHdrTableEntry &Entry, uint64_t Flags)
std::pair< const LocationT, SampleT > SamplesWithLoc
void merge(const ProfileSymbolList &List)
void findInlinedFunctions(DenseSet< GlobalValue::GUID > &S, const Module *M, uint64_t Threshold) const
Recursively traverses all children, if the total sample count of the corresponding function is no les...
bool operator<(const LineLocation &O) const
static SampleProfileFormat Format
Representation of the samples collected for a function.
static const SortedCallTargetSet SortCallTargets(const CallTargetMap &Targets)
Sort call targets in descending order of call frequency.
static std::string getSecName(SecType Type)
LLVM_NODISCARD StringRef copy(Allocator &A) const
static bool hasSecFlag(SecHdrTableEntry &Entry, SecFlags Flag)
std::error_code make_error_code(BitcodeError E)
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
sampleprof_error addCalledTarget(StringRef F, uint64_t S, uint64_t Weight=1)
Add called function F with samples S.
Representation of a single sample record.
LLVM_NODISCARD size_t size() const
size - Get the string size.
void setName(StringRef FunctionName)
Set the name of the function.
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
std::map< LineLocation, SampleRecord > BodySampleMap
static uint64_t SPVersion()
Sort a LocationT->SampleT map by LocationT.
uint64_t getHeadSamples() const
Return the total number of branch samples that have the function as the branch target.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
uint64_t getSamples() const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
The instances of the Type class are immutable: once they are created, they are never changed...
Allocate memory in an ever growing pool, as if by bump-pointer.
DISubprogram * getSubprogram() const
Get the attached subprogram.
const BodySampleMap & getBodySamples() const
Return all the samples collected in the body of the function.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
sampleprof_error addBodySamples(uint32_t LineOffset, uint32_t Discriminator, uint64_t Num, uint64_t Weight=1)
sampleprof_error addSamples(uint64_t S, uint64_t Weight=1)
Increment the number of samples for this record by S.
std::pair< iterator, bool > insert(const ValueT &V)
static StringRef getCanonicalFnName(const Function &F)
Return the canonical name for a function, taking into account suffix elision policy attributes...
static ManagedStatic< _object_error_category > error_category
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static void write(bool isBE, void *P, T V)
std::map< std::string, FunctionSamples, std::less<> > FunctionSamplesMap
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
StringRef getFuncNameInModule(const Module *M) const
Return the original function name if it exists in Module M.
std::enable_if< std::is_unsigned< T >::value, T >::type SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed=nullptr)
Multiply two unsigned integers, X and Y, and add the unsigned integer, A to the product.
const FunctionSamples * findFunctionSamplesAt(const LineLocation &Loc, StringRef CalleeName) const
Returns a pointer to FunctionSamples at the given callsite location Loc with callee CalleeName...
sampleprof_error addTotalSamples(uint64_t Num, uint64_t Weight=1)
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
Module.h This file contains the declarations for the Module class.
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
FunctionSamplesMap & functionSamplesAt(const LineLocation &Loc)
Return the function samples at the given callsite location.
ErrorOr< SampleRecord::CallTargetMap > findCallTargetMapAt(uint32_t LineOffset, uint32_t Discriminator) const
Returns the call target map collected at a given location.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
const std::error_category & sampleprof_category()
amdgpu Simplify well known AMD library false FunctionCallee Callee
static cl::opt< unsigned > Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
bool hasCalls() const
Return true if this sample record contains function calls.
std::map< LineLocation, FunctionSamplesMap > CallsiteSampleMap
ProfileSymbolList records the list of function symbols shown up in the binary used to generate the pr...
Represents the relative location of an instruction.
void setToCompress(bool TC)
StringRef getNameInModule(StringRef Name, const Module *M) const
Translate Name into its original name in Module.
StringRef getValueAsString() const
Return the attribute's value as a string.
StringRef getName() const
Return a constant reference to the value's name.
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
StringRef getName() const
Return the function name.
const CallTargetMap & getCallTargets() const
std::set< CallTarget, CallTargetComparator > SortedCallTargetSet
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
static uint64_t SPMagic(SampleProfileFormat Format=SPF_Binary)
sampleprof_error addHeadSamples(uint64_t Num, uint64_t Weight=1)
const std::string to_string(const T &Value)
Provides ErrorOr<T> smart pointer.
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
std::pair< StringRef, uint64_t > CallTarget
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void stable_sort(R &&Range)
uint64_t getTotalSamples() const
Return the total number of samples collected inside the function.
const CallsiteSampleMap & getCallsiteSamples() const
Return all the callsite samples collected in the body of the function.
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
uint64_t getEntrySamples() const
Return the sample count of the first instruction of the function.
const SortedCallTargetSet getSortedCallTargets() const
bool operator()(const CallTarget &LHS, const CallTarget &RHS) const
OutputIt copy(R &&Range, OutputIt Out)
sampleprof_error merge(const FunctionSamples &Other, uint64_t Weight=1)
Merge the samples in Other into this one.
sampleprof_error addCalledTargetSamples(uint32_t LineOffset, uint32_t Discriminator, StringRef FName, uint64_t Num, uint64_t Weight=1)
sampleprof_error MergeResult(sampleprof_error &Accumulator, sampleprof_error Result)
void add(StringRef Name, bool copy=false)
copy indicates whether we need to copy the underlying memory for the input Name.