15 #ifndef LLVM_PROFILEDATA_SAMPLEPROF_H 16 #define LLVM_PROFILEDATA_SAMPLEPROF_H 32 #include <system_error> 80 namespace sampleprof {
91 return uint64_t(
'S') << (64 - 8) | uint64_t(
'P') << (64 - 16) |
92 uint64_t(
'R') << (64 - 24) | uint64_t(
'O') << (64 - 32) |
93 uint64_t(
'F') << (64 - 40) | uint64_t(
'4') << (64 - 48) |
94 uint64_t(
'2') << (64 - 56) | uint64_t(
Format);
100 std::string &GUIDBuf) {
169 uint64_t Weight = 1) {
170 uint64_t &TargetSamples = CallTargets[
F];
179 bool hasCalls()
const {
return !CallTargets.empty(); }
189 MergeResult(Result, addCalledTarget(
I.first(),
I.second, Weight));
198 uint64_t NumSamples = 0;
241 uint64_t Num, uint64_t Weight = 1) {
242 return BodySamples[
LineLocation(LineOffset, Discriminator)].addSamples(
249 uint64_t Weight = 1) {
250 return BodySamples[
LineLocation(LineOffset, Discriminator)].addCalledTarget(
259 const auto &ret = BodySamples.find(
LineLocation(LineOffset, Discriminator));
260 if (ret == BodySamples.end())
261 return std::error_code();
263 return ret->second.getSamples();
271 const auto &ret = BodySamples.find(
LineLocation(LineOffset, Discriminator));
272 if (ret == BodySamples.end())
273 return std::error_code();
274 return ret->second.getCallTargets();
279 return CallsiteSamples[Loc];
285 auto iter = CallsiteSamples.find(Loc);
286 if (iter == CallsiteSamples.end())
288 return &iter->second;
297 std::string CalleeGUID;
300 auto iter = CallsiteSamples.find(Loc);
301 if (iter == CallsiteSamples.end())
303 auto FS = iter->second.find(CalleeName);
304 if (FS != iter->second.end())
308 uint64_t MaxTotalSamples = 0;
310 for (
const auto &NameFS : iter->second)
311 if (NameFS.second.getTotalSamples() >= MaxTotalSamples) {
312 MaxTotalSamples = NameFS.second.getTotalSamples();
318 bool empty()
const {
return TotalSamples == 0; }
335 if (!BodySamples.empty() &&
336 (CallsiteSamples.empty() ||
337 BodySamples.begin()->first < CallsiteSamples.begin()->first))
338 return BodySamples.begin()->second.getSamples();
339 if (!CallsiteSamples.empty()) {
343 for (
const auto &N_FS : CallsiteSamples.begin()->second)
344 T += N_FS.second.getEntrySamples();
355 return CallsiteSamples;
368 MergeResult(Result, BodySamples[Loc].merge(Rec, Weight));
373 for (
const auto &Rec :
I.second)
374 MergeResult(Result, FSMap[Rec.first].merge(Rec.second, Weight));
385 if (TotalSamples <= Threshold)
390 for (
const auto &BS : BodySamples)
391 for (
const auto &TS : BS.second.getCallTargets())
398 for (
const auto &CS : CallsiteSamples)
399 for (
const auto &NameFS : CS.second)
400 NameFS.second.findInlinedFunctions(S, M, Threshold);
411 return getNameInModule(
Name, M);
424 if (M != CurrentModule)
426 auto iter = GUIDToFuncNameMap.find(std::stoull(Name.
data()));
427 if (iter == GUIDToFuncNameMap.end())
459 for (
const auto &
F : M) {
469 auto pos = OrigName.
find(
'.');
482 GUIDToFuncNameMap.
clear();
483 CurrentModule =
nullptr;
503 uint64_t TotalSamples = 0;
508 uint64_t TotalHeadSamples = 0;
548 for (
const auto &
I : Samples)
550 std::stable_sort(V.begin(), V.end(),
552 return A->first <
B->first;
565 #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. ...
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.
LineLocation(uint32_t L, uint32_t D)
SampleSorter(const std::map< LocationT, SampleT > &Samples)
ErrorOr< uint64_t > findSamplesAt(uint32_t LineOffset, uint32_t Discriminator) const
Return the number of samples collected at the given location.
std::pair< const LocationT, SampleT > SamplesWithLoc
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
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Representation of the samples collected for a function.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
amdgpu Simplify well known AMD library false Value Value const Twine & Name
std::error_code make_error_code(BitcodeError E)
static DenseMap< uint64_t, StringRef > GUIDToFuncNameMap
GUIDToFuncNameMap saves the mapping from GUID to the symbol name, for all the function symbols define...
sampleprof_error addCalledTarget(StringRef F, uint64_t S, uint64_t Weight=1)
Add called function F with samples S.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Representation of a single sample record.
void setName(StringRef FunctionName)
Set the name of the function.
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
std::map< LineLocation, SampleRecord > BodySampleMap
static uint64_t SPVersion()
amdgpu Simplify well known AMD library false Value * Callee
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.
uint64_t getSamples() const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
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 ManagedStatic< _object_error_category > error_category
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#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)
Module.h This file contains the declarations for the Module class.
static Module * CurrentModule
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()
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
Represents the relative location of an instruction.
StringRef getNameInModule(StringRef Name, const Module *M) const
Translate Name into its original name in Module.
std::map< std::string, FunctionSamples > FunctionSamplesMap
StringRef getName() const
Return the function name.
const CallTargetMap & getCallTargets() const
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
static GlobalValue::GUID getGUID(GlobalValue::GUID G)
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.
GUIDToFuncNameMapper(Module &M)
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.
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.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
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)