LLVM  14.0.0git
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
llvm::sampleprof::FunctionSamples Class Reference

Representation of the samples collected for a function. More...

#include "llvm/ProfileData/SampleProf.h"

Collaboration diagram for llvm::sampleprof::FunctionSamples:
Collaboration graph
[legend]

Public Member Functions

 FunctionSamples ()=default
 
void print (raw_ostream &OS=dbgs(), unsigned Indent=0) const
 Print the samples collected for a function on stream OS. More...
 
void dump () const
 
sampleprof_error addTotalSamples (uint64_t Num, uint64_t Weight=1)
 
void setTotalSamples (uint64_t Num)
 
sampleprof_error addHeadSamples (uint64_t Num, uint64_t Weight=1)
 
sampleprof_error addBodySamples (uint32_t LineOffset, uint32_t Discriminator, uint64_t Num, uint64_t Weight=1)
 
sampleprof_error addCalledTargetSamples (uint32_t LineOffset, uint32_t Discriminator, StringRef FName, uint64_t Num, uint64_t Weight=1)
 
sampleprof_error addBodySamplesForProbe (uint32_t Index, uint64_t Num, uint64_t Weight=1)
 
ErrorOr< uint64_t > findSamplesAt (uint32_t LineOffset, uint32_t Discriminator) const
 Return the number of samples collected at the given location. More...
 
ErrorOr< SampleRecord::CallTargetMapfindCallTargetMapAt (uint32_t LineOffset, uint32_t Discriminator) const
 Returns the call target map collected at a given location. More...
 
ErrorOr< SampleRecord::CallTargetMapfindCallTargetMapAt (const LineLocation &CallSite) const
 Returns the call target map collected at a given location specified by CallSite. More...
 
FunctionSamplesMapfunctionSamplesAt (const LineLocation &Loc)
 Return the function samples at the given callsite location. More...
 
const FunctionSamplesMapfindFunctionSamplesMapAt (const LineLocation &Loc) const
 Returns the FunctionSamplesMap at the given Loc. More...
 
const FunctionSamplesfindFunctionSamplesAt (const LineLocation &Loc, StringRef CalleeName, SampleProfileReaderItaniumRemapper *Remapper) const
 Returns a pointer to FunctionSamples at the given callsite location Loc with callee CalleeName. More...
 
bool empty () const
 
uint64_t getTotalSamples () const
 Return the total number of samples collected inside the function. More...
 
uint64_t getHeadSamples () const
 Return the total number of branch samples that have the function as the branch target. More...
 
uint64_t getEntrySamples () const
 Return the sample count of the first instruction of the function. More...
 
const BodySampleMapgetBodySamples () const
 Return all the samples collected in the body of the function. More...
 
const CallsiteSampleMapgetCallsiteSamples () const
 Return all the callsite samples collected in the body of the function. More...
 
uint64_t getMaxCountInside () const
 Return the maximum of sample counts in a function body including functions inlined in it. More...
 
sampleprof_error merge (const FunctionSamples &Other, uint64_t Weight=1)
 Merge the samples in Other into this one. More...
 
void findInlinedFunctions (DenseSet< GlobalValue::GUID > &S, const StringMap< Function * > &SymbolMap, uint64_t Threshold) const
 Recursively traverses all children, if the total sample count of the corresponding function is no less than Threshold, add its corresponding GUID to S. More...
 
void setName (StringRef FunctionName)
 Set the name of the function. More...
 
StringRef getName () const
 Return the function name. More...
 
StringRef getNameWithContext () const
 Return function name with context. More...
 
StringRef getFuncName () const
 Return the original function name. More...
 
void setFunctionHash (uint64_t Hash)
 
uint64_t getFunctionHash () const
 
StringRef getFuncName (StringRef Name) const
 Translate Name into its original name. More...
 
const FunctionSamplesfindFunctionSamples (const DILocation *DIL, SampleProfileReaderItaniumRemapper *Remapper=nullptr) const
 Get the FunctionSamples of the inline instance where DIL originates from. More...
 
SampleContextgetContext () const
 
void setContext (const SampleContext &FContext)
 
void findAllNames (DenseSet< StringRef > &NameSet) const
 

Static Public Member Functions

static StringRef getCanonicalFnName (const Function &F)
 Return the canonical name for a function, taking into account suffix elision policy attributes. More...
 
static StringRef getCanonicalFnName (StringRef FnName, StringRef Attr="selected")
 
static unsigned getOffset (const DILocation *DIL)
 Returns the line offset to the start line of the subprogram. More...
 
static LineLocation getCallSiteIdentifier (const DILocation *DIL)
 Returns a unique call site identifier for a given debug location of a call instruction. More...
 
static uint64_t getGUID (StringRef Name)
 

Public Attributes

DenseMap< uint64_t, StringRef > * GUIDToFuncNameMap = nullptr
 GUIDToFuncNameMap saves the mapping from GUID to the symbol name, for all the function symbols defined or declared in current module. More...
 

Static Public Attributes

static constexpr const char * LLVMSuffix = ".llvm."
 Name suffixes which canonicalization should handle to avoid profile mismatch. More...
 
static constexpr const char * PartSuffix = ".part."
 
static constexpr const char * UniqSuffix = ".__uniq."
 
static bool ProfileIsProbeBased = false
 
static bool ProfileIsCS = false
 
static SampleProfileFormat Format
 
static bool UseMD5 = false
 Whether the profile uses MD5 to represent string. More...
 
static bool HasUniqSuffix = true
 Whether the profile contains any ".__uniq." suffix in a name. More...
 
static bool ProfileIsFS = false
 If this profile uses flow sensitive discriminators. More...
 

Detailed Description

Representation of the samples collected for a function.

This data structure contains all the collected samples for the body of a function. Each sample corresponds to a LineLocation instance within the body of the function.

Definition at line 549 of file SampleProf.h.

Constructor & Destructor Documentation

◆ FunctionSamples()

llvm::sampleprof::FunctionSamples::FunctionSamples ( )
default

Member Function Documentation

◆ addBodySamples()

sampleprof_error llvm::sampleprof::FunctionSamples::addBodySamples ( uint32_t  LineOffset,
uint32_t  Discriminator,
uint64_t  Num,
uint64_t  Weight = 1 
)
inline

◆ addBodySamplesForProbe()

sampleprof_error llvm::sampleprof::FunctionSamples::addBodySamplesForProbe ( uint32_t  Index,
uint64_t  Num,
uint64_t  Weight = 1 
)
inline

Definition at line 588 of file SampleProf.h.

References Index, and S.

◆ addCalledTargetSamples()

sampleprof_error llvm::sampleprof::FunctionSamples::addCalledTargetSamples ( uint32_t  LineOffset,
uint32_t  Discriminator,
StringRef  FName,
uint64_t  Num,
uint64_t  Weight = 1 
)
inline

◆ addHeadSamples()

sampleprof_error llvm::sampleprof::FunctionSamples::addHeadSamples ( uint64_t  Num,
uint64_t  Weight = 1 
)
inline

◆ addTotalSamples()

sampleprof_error llvm::sampleprof::FunctionSamples::addTotalSamples ( uint64_t  Num,
uint64_t  Weight = 1 
)
inline

◆ dump()

LLVM_DUMP_METHOD void FunctionSamples::dump ( ) const

Definition at line 298 of file SampleProf.cpp.

References llvm::dbgs(), and print().

◆ empty()

bool llvm::sampleprof::FunctionSamples::empty ( ) const
inline

Definition at line 651 of file SampleProf.h.

◆ findAllNames()

void FunctionSamples::findAllNames ( DenseSet< StringRef > &  NameSet) const

◆ findCallTargetMapAt() [1/2]

ErrorOr<SampleRecord::CallTargetMap> llvm::sampleprof::FunctionSamples::findCallTargetMapAt ( const LineLocation CallSite) const
inline

Returns the call target map collected at a given location specified by CallSite.

If the location is not found in profile, return error.

Definition at line 620 of file SampleProf.h.

References llvm::MipsISD::Ret.

◆ findCallTargetMapAt() [2/2]

ErrorOr<SampleRecord::CallTargetMap> llvm::sampleprof::FunctionSamples::findCallTargetMapAt ( uint32_t  LineOffset,
uint32_t  Discriminator 
) const
inline

Returns the call target map collected at a given location.

Each location is specified by LineOffset and Discriminator. If the location is not found in profile, return error.

Definition at line 610 of file SampleProf.h.

References ret().

◆ findFunctionSamples()

const FunctionSamples * FunctionSamples::findFunctionSamples ( const DILocation DIL,
SampleProfileReaderItaniumRemapper Remapper = nullptr 
) const

Get the FunctionSamples of the inline instance where DIL originates from.

The FunctionSamples of the instruction (Machine or IR) associated to DIL is the inlined instance in which that instruction is coming from. We traverse the inline stack of that instruction, and match it with the tree nodes in the profile.

Returns
the FunctionSamples pointer to the inlined instance. If Remapper is not nullptr, it will be used to find matching FunctionSamples with not exactly the same but equivalent name.

Definition at line 220 of file SampleProf.cpp.

References assert(), llvm::X86AS::FS, llvm::DILocation::getBaseDiscriminator(), getOffset(), i, ProfileIsFS, and S.

◆ findFunctionSamplesAt()

const FunctionSamples * FunctionSamples::findFunctionSamplesAt ( const LineLocation Loc,
StringRef  CalleeName,
SampleProfileReaderItaniumRemapper Remapper 
) const

Returns a pointer to FunctionSamples at the given callsite location Loc with callee CalleeName.

If no callsite can be found, relax the restriction to return the FunctionSamples at callsite location Loc with the maximum total sample count. If Remapper is not nullptr, use Remapper to find FunctionSamples with equivalent name as CalleeName.

Definition at line 261 of file SampleProf.cpp.

References llvm::StringRef::empty(), llvm::X86AS::FS, getCanonicalFnName(), llvm::sampleprof::getRepInFormat(), llvm::sampleprof::SampleProfileReaderItaniumRemapper::lookUpNameInProfile(), and UseMD5.

◆ findFunctionSamplesMapAt()

const FunctionSamplesMap* llvm::sampleprof::FunctionSamples::findFunctionSamplesMapAt ( const LineLocation Loc) const
inline

Returns the FunctionSamplesMap at the given Loc.

Definition at line 634 of file SampleProf.h.

◆ findInlinedFunctions()

void llvm::sampleprof::FunctionSamples::findInlinedFunctions ( DenseSet< GlobalValue::GUID > &  S,
const StringMap< Function * > &  SymbolMap,
uint64_t  Threshold 
) const
inline

Recursively traverses all children, if the total sample count of the corresponding function is no less than Threshold, add its corresponding GUID to S.

Also traverse the BodySamples to add hot CallTarget's GUID to S.

Definition at line 751 of file SampleProf.h.

References Callee, F, getFuncName(), getGUID(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::lookup(), S, and Threshold.

◆ findSamplesAt()

ErrorOr<uint64_t> llvm::sampleprof::FunctionSamples::findSamplesAt ( uint32_t  LineOffset,
uint32_t  Discriminator 
) const
inline

Return the number of samples collected at the given location.

Each location is specified by LineOffset and Discriminator. If the location is not found in profile, return error.

Definition at line 598 of file SampleProf.h.

References ret().

◆ functionSamplesAt()

FunctionSamplesMap& llvm::sampleprof::FunctionSamples::functionSamplesAt ( const LineLocation Loc)
inline

Return the function samples at the given callsite location.

Definition at line 628 of file SampleProf.h.

Referenced by merge(), llvm::sampleprof::SampleProfileReaderGCC::readOneFunctionProfile(), and llvm::sampleprof::SampleProfileReaderBinary::readProfile().

◆ getBodySamples()

const BodySampleMap& llvm::sampleprof::FunctionSamples::getBodySamples ( ) const
inline

Return all the samples collected in the body of the function.

Definition at line 689 of file SampleProf.h.

Referenced by llvm::sampleprof::ProfiledCallGraph::addProfiledCalls(), and getMaxCountInside().

◆ getCallSiteIdentifier()

LineLocation FunctionSamples::getCallSiteIdentifier ( const DILocation DIL)
static

Returns a unique call site identifier for a given debug location of a call instruction.

This is wrapper of two scenarios, the probe-based profile and regular profile, to hide implementation details from the sample loader and the context tracker.

Definition at line 206 of file SampleProf.cpp.

References llvm::PseudoProbeDwarfDiscriminator::extractProbeIndex(), llvm::DILocation::getBaseDiscriminator(), getOffset(), and ProfileIsProbeBased.

Referenced by llvm::SampleContextTracker::getIndirectCalleeContextSamplesFor(), and llvm::SampleContextTracker::promoteMergeContextSamplesTree().

◆ getCallsiteSamples()

const CallsiteSampleMap& llvm::sampleprof::FunctionSamples::getCallsiteSamples ( ) const
inline

Return all the callsite samples collected in the body of the function.

Definition at line 692 of file SampleProf.h.

Referenced by llvm::sampleprof::ProfiledCallGraph::addProfiledCalls(), and getMaxCountInside().

◆ getCanonicalFnName() [1/2]

static StringRef llvm::sampleprof::FunctionSamples::getCanonicalFnName ( const Function F)
inlinestatic

◆ getCanonicalFnName() [2/2]

static StringRef llvm::sampleprof::FunctionSamples::getCanonicalFnName ( StringRef  FnName,
StringRef  Attr = "selected" 
)
inlinestatic

◆ getContext()

SampleContext& llvm::sampleprof::FunctionSamples::getContext ( ) const
inline

◆ getEntrySamples()

uint64_t llvm::sampleprof::FunctionSamples::getEntrySamples ( ) const
inline

Return the sample count of the first instruction of the function.

The function can be either a standalone symbol or an inlined function.

Definition at line 665 of file SampleProf.h.

References getHeadSamples(), and ProfileIsCS.

◆ getFuncName() [1/2]

StringRef llvm::sampleprof::FunctionSamples::getFuncName ( ) const
inline

Return the original function name.

Definition at line 789 of file SampleProf.h.

References getFuncName().

Referenced by llvm::sampleprof::ProfiledCallGraph::addProfiledCalls(), findInlinedFunctions(), and getFuncName().

◆ getFuncName() [2/2]

StringRef llvm::sampleprof::FunctionSamples::getFuncName ( StringRef  Name) const
inline

Translate Name into its original name.

When profile doesn't use MD5, Name needs no translation. When profile uses MD5, Name in current FunctionSamples is actually GUID of the original function name. getFuncName will translate Name in current FunctionSamples into its original name by looking up in the function map GUIDToFuncNameMap. If the original name doesn't exist in the map, return empty StringRef.

Definition at line 848 of file SampleProf.h.

References assert(), GUIDToFuncNameMap, and UseMD5.

◆ getFunctionHash()

uint64_t llvm::sampleprof::FunctionSamples::getFunctionHash ( ) const
inline

Definition at line 793 of file SampleProf.h.

Referenced by print(), and llvm::PseudoProbeManager::profileIsValid().

◆ getGUID()

static uint64_t llvm::sampleprof::FunctionSamples::getGUID ( StringRef  Name)
inlinestatic

Definition at line 907 of file SampleProf.h.

References llvm::GlobalValue::getGUID(), and UseMD5.

Referenced by findInlinedFunctions(), and GetSortedValueDataFromCallTargets().

◆ getHeadSamples()

uint64_t llvm::sampleprof::FunctionSamples::getHeadSamples ( ) const
inline

Return the total number of branch samples that have the function as the branch target.

This should be equivalent to the sample of the first instruction of the symbol. But as we directly get this info for raw profile without referring to potentially inaccurate debug info, this gives more accurate profile data and is preferred for standalone symbols.

Definition at line 661 of file SampleProf.h.

Referenced by getEntrySamples().

◆ getMaxCountInside()

uint64_t llvm::sampleprof::FunctionSamples::getMaxCountInside ( ) const
inline

Return the maximum of sample counts in a function body including functions inlined in it.

Definition at line 698 of file SampleProf.h.

References F, getBodySamples(), getCallsiteSamples(), and llvm::max().

◆ getName()

StringRef llvm::sampleprof::FunctionSamples::getName ( ) const
inline

Return the function name.

Definition at line 781 of file SampleProf.h.

Referenced by llvm::sampleprof::SampleProfileWriterBinary::addNames().

◆ getNameWithContext()

StringRef llvm::sampleprof::FunctionSamples::getNameWithContext ( ) const
inline

Return function name with context.

Definition at line 784 of file SampleProf.h.

References llvm::sampleprof::SampleContext::getNameWithContext(), and ProfileIsCS.

Referenced by llvm::sampleprof::SampleContextTrimmer::canonicalizeContextProfiles().

◆ getOffset()

unsigned FunctionSamples::getOffset ( const DILocation DIL)
static

Returns the line offset to the start line of the subprogram.

We assume that a single function will not exceed 65535 LOC.

Definition at line 201 of file SampleProf.cpp.

Referenced by findFunctionSamples(), getCallSiteIdentifier(), and llvm::SampleProfileLoaderBaseImpl< BT >::getInstWeightImpl().

◆ getTotalSamples()

uint64_t llvm::sampleprof::FunctionSamples::getTotalSamples ( ) const
inline

◆ merge()

sampleprof_error llvm::sampleprof::FunctionSamples::merge ( const FunctionSamples Other,
uint64_t  Weight = 1 
)
inline

◆ print()

void FunctionSamples::print ( raw_ostream OS = dbgs(),
unsigned  Indent = 0 
) const

Print the samples collected for a function on stream OS.

Definition at line 155 of file SampleProf.cpp.

References llvm::X86AS::FS, llvm::sampleprof::SampleSorter< LocationT, SampleT >::get(), getFunctionHash(), llvm::raw_ostream::indent(), and SI.

Referenced by dump().

◆ setContext()

void llvm::sampleprof::FunctionSamples::setContext ( const SampleContext FContext)
inline

◆ setFunctionHash()

void llvm::sampleprof::FunctionSamples::setFunctionHash ( uint64_t  Hash)
inline

Definition at line 791 of file SampleProf.h.

Referenced by llvm::sampleprof::SampleProfileReaderText::readImpl().

◆ setName()

void llvm::sampleprof::FunctionSamples::setName ( StringRef  FunctionName)
inline

◆ setTotalSamples()

void llvm::sampleprof::FunctionSamples::setTotalSamples ( uint64_t  Num)
inline

Definition at line 564 of file SampleProf.h.

Member Data Documentation

◆ Format

SampleProfileFormat llvm::sampleprof::FunctionSamples::Format
static

Definition at line 889 of file SampleProf.h.

Referenced by llvm::sampleprof::SampleProfileReader::create().

◆ GUIDToFuncNameMap

DenseMap<uint64_t, StringRef>* llvm::sampleprof::FunctionSamples::GUIDToFuncNameMap = nullptr

GUIDToFuncNameMap saves the mapping from GUID to the symbol name, for all the function symbols defined or declared in current module.

Definition at line 902 of file SampleProf.h.

Referenced by getFuncName(), and merge().

◆ HasUniqSuffix

bool llvm::sampleprof::FunctionSamples::HasUniqSuffix = true
static

Whether the profile contains any ".__uniq." suffix in a name.

Definition at line 895 of file SampleProf.h.

Referenced by getCanonicalFnName().

◆ LLVMSuffix

constexpr const char* llvm::sampleprof::FunctionSamples::LLVMSuffix = ".llvm."
staticconstexpr

Name suffixes which canonicalization should handle to avoid profile mismatch.

Definition at line 805 of file SampleProf.h.

Referenced by getCanonicalFnName().

◆ PartSuffix

constexpr const char* llvm::sampleprof::FunctionSamples::PartSuffix = ".part."
staticconstexpr

Definition at line 806 of file SampleProf.h.

Referenced by getCanonicalFnName().

◆ ProfileIsCS

bool llvm::sampleprof::FunctionSamples::ProfileIsCS = false
static

◆ ProfileIsFS

bool llvm::sampleprof::FunctionSamples::ProfileIsFS = false
static

If this profile uses flow sensitive discriminators.

Definition at line 898 of file SampleProf.h.

Referenced by findFunctionSamples(), and llvm::sampleprof::SampleProfileWriterExtBinaryBase::writeOneSection().

◆ ProfileIsProbeBased

bool llvm::sampleprof::FunctionSamples::ProfileIsProbeBased = false
static

◆ UniqSuffix

constexpr const char* llvm::sampleprof::FunctionSamples::UniqSuffix = ".__uniq."
staticconstexpr

◆ UseMD5

bool llvm::sampleprof::FunctionSamples::UseMD5 = false
static

Whether the profile uses MD5 to represent string.

Definition at line 892 of file SampleProf.h.

Referenced by findFunctionSamplesAt(), getFuncName(), getGUID(), and llvm::sampleprof::SampleProfileReader::read().


The documentation for this class was generated from the following files: