LLVM  14.0.0git
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
llvm::sampleprof::SampleProfileReader Class Referenceabstract

Sample-based profile reader. More...

#include "llvm/ProfileData/SampleProfReader.h"

Inheritance diagram for llvm::sampleprof::SampleProfileReader:
Inheritance graph
[legend]
Collaboration diagram for llvm::sampleprof::SampleProfileReader:
Collaboration graph
[legend]

Public Member Functions

 SampleProfileReader (std::unique_ptr< MemoryBuffer > B, LLVMContext &C, SampleProfileFormat Format=SPF_None)
 
virtual ~SampleProfileReader ()=default
 
virtual std::error_code readHeader ()=0
 Read and validate the file header. More...
 
void setDiscriminatorMaskedBitFrom (FSDiscriminatorPass P)
 Set the bits for FS discriminators. More...
 
uint32_t getDiscriminatorMask () const
 Get the bitmask the discriminators: For FS profiles, return the bit mask for this pass. More...
 
std::error_code read ()
 The interface to read sample profiles from the associated file. More...
 
virtual std::error_code readImpl ()=0
 The implementaion to read sample profiles from the associated file. More...
 
void dumpFunctionProfile (SampleContext FContext, raw_ostream &OS=dbgs())
 Print the profile for FContext on stream OS. More...
 
virtual bool collectFuncsFromModule ()
 Collect functions with definitions in Module M. More...
 
void dump (raw_ostream &OS=dbgs())
 Print all the profiles on stream OS. More...
 
FunctionSamplesgetSamplesFor (const Function &F)
 Return the samples collected for function F. More...
 
FunctionSamplesgetOrCreateSamplesFor (const Function &F)
 Return the samples collected for function F, create empty FunctionSamples if it doesn't exist. More...
 
virtual FunctionSamplesgetSamplesFor (StringRef Fname)
 Return the samples collected for function F. More...
 
SampleProfileMapgetProfiles ()
 Return all the profiles. More...
 
void reportError (int64_t LineNumber, const Twine &Msg) const
 Report a parse error message. More...
 
ProfileSummarygetSummary () const
 Return the profile summary. More...
 
MemoryBuffergetBuffer () const
 
SampleProfileFormat getFormat () const
 Return the profile format. More...
 
bool profileIsProbeBased () const
 Whether input profile is based on pseudo probes. More...
 
bool profileIsCS () const
 Whether input profile is fully context-sensitive. More...
 
virtual std::unique_ptr< ProfileSymbolListgetProfileSymbolList ()
 
virtual std::vector< StringRef > * getNameTable ()
 It includes all the names that have samples either in outline instance or inline instance. More...
 
virtual bool dumpSectionInfo (raw_ostream &OS=dbgs())
 
virtual bool useMD5 ()
 Return whether names in the profile are all MD5 numbers. More...
 
virtual void setSkipFlatProf (bool Skip)
 Don't read profile without context if the flag is set. More...
 
virtual bool hasUniqSuffix ()
 Return whether any name in the profile contains ".__uniq." suffix. More...
 
SampleProfileReaderItaniumRemappergetRemapper ()
 
void setModule (const Module *Mod)
 

Static Public Member Functions

static ErrorOr< std::unique_ptr< SampleProfileReader > > create (const std::string Filename, LLVMContext &C, FSDiscriminatorPass P=FSDiscriminatorPass::Base, const std::string RemapFilename="")
 Create a sample profile reader appropriate to the file format. More...
 
static ErrorOr< std::unique_ptr< SampleProfileReader > > create (std::unique_ptr< MemoryBuffer > &B, LLVMContext &C, FSDiscriminatorPass P=FSDiscriminatorPass::Base, const std::string RemapFilename="")
 Create a sample profile reader from the supplied memory buffer. More...
 

Protected Member Functions

void computeSummary ()
 Compute summary for this profile. More...
 

Static Protected Member Functions

static std::unique_ptr< ProfileSummarytakeSummary (SampleProfileReader &Reader)
 Take ownership of the summary of this reader. More...
 

Protected Attributes

SampleProfileMap Profiles
 Map every function to its associated profile. More...
 
LLVMContextCtx
 LLVM context used to emit diagnostics. More...
 
std::unique_ptr< MemoryBufferBuffer
 Memory buffer holding the profile file. More...
 
std::unordered_set< std::string > MD5NameBuffer
 Extra name buffer holding names created on demand. More...
 
std::unique_ptr< ProfileSummarySummary
 Profile summary information. More...
 
std::unique_ptr< SampleProfileReaderItaniumRemapperRemapper
 
bool ProfileIsProbeBased = false
 Whether samples are collected based on pseudo probes. More...
 
bool ProfileIsCS = false
 Whether function profiles are context-sensitive. More...
 
uint32_t CSProfileCount = 0
 Number of context-sensitive profiles. More...
 
bool ProfileIsFS = false
 Whether the function profiles use FS discriminators. More...
 
SampleProfileFormat Format = SPF_None
 The format of sample. More...
 
const ModuleM = nullptr
 The current module being compiled if SampleProfileReader is used by compiler. More...
 
uint32_t MaskedBitFrom = 31
 Zero out the discriminator bits higher than bit MaskedBitFrom (0 based). More...
 

Detailed Description

Sample-based profile reader.

Each profile contains sample counts for all the functions executed. Inside each function, statements are annotated with the collected samples on all the instructions associated with that statement.

For this to produce meaningful data, the program needs to be compiled with some debug information (at minimum, line numbers: -gline-tables-only). Otherwise, it will be impossible to match IR instructions to the line numbers collected by the profiler.

From the profile file, we are interested in collecting the following information:

The reader supports two file formats: text and binary. The text format is useful for debugging and testing, while the binary format is more compact and I/O efficient. They can both be used interchangeably.

Definition at line 345 of file SampleProfReader.h.

Constructor & Destructor Documentation

◆ SampleProfileReader()

llvm::sampleprof::SampleProfileReader::SampleProfileReader ( std::unique_ptr< MemoryBuffer B,
LLVMContext C,
SampleProfileFormat  Format = SPF_None 
)
inline

Definition at line 347 of file SampleProfReader.h.

◆ ~SampleProfileReader()

virtual llvm::sampleprof::SampleProfileReader::~SampleProfileReader ( )
virtualdefault

Member Function Documentation

◆ collectFuncsFromModule()

virtual bool llvm::sampleprof::SampleProfileReader::collectFuncsFromModule ( )
inlinevirtual

Collect functions with definitions in Module M.

For reader which support loading function profiles on demand, return true when the reader has been given a module. Always return false for reader which doesn't support loading function profiles on demand.

Reimplemented in llvm::sampleprof::SampleProfileReaderCompactBinary, and llvm::sampleprof::SampleProfileReaderExtBinaryBase.

Definition at line 392 of file SampleProfReader.h.

Referenced by llvm::sampleprof::SampleProfileReaderExtBinaryBase::readFuncProfiles(), and llvm::sampleprof::SampleProfileReaderCompactBinary::readImpl().

◆ computeSummary()

void SampleProfileReader::computeSummary ( )
protected

◆ create() [1/2]

ErrorOr< std::unique_ptr< SampleProfileReader > > SampleProfileReader::create ( const std::string  Filename,
LLVMContext C,
FSDiscriminatorPass  P = FSDiscriminatorPass::Base,
const std::string  RemapFilename = "" 
)
static

Create a sample profile reader appropriate to the file format.

Create a sample profile reader based on the format of the input file.

Create a remapper underlying if RemapFilename is not empty. Parameter P specifies the FSDiscriminatorPass.

Parameters
FilenameThe file to open.
CThe LLVM context to use to emit diagnostics.
PThe FSDiscriminatorPass.
RemapFilenameThe file used for profile remapping.
Returns
an error code indicating the status of the created reader.

Definition at line 1725 of file SampleProfReader.cpp.

References P, and setupMemoryBuffer().

Referenced by llvm::sampleprof::SampleProfileReaderItaniumRemapper::create(), and llvm::MIRProfileLoader::doInitialization().

◆ create() [2/2]

ErrorOr< std::unique_ptr< SampleProfileReader > > SampleProfileReader::create ( std::unique_ptr< MemoryBuffer > &  B,
LLVMContext C,
FSDiscriminatorPass  P = FSDiscriminatorPass::Base,
const std::string  RemapFilename = "" 
)
static

Create a sample profile reader from the supplied memory buffer.

Create a sample profile reader based on the format of the input data.

Create a remapper underlying if RemapFilename is not empty. Parameter P specifies the FSDiscriminatorPass.

Parameters
BThe memory buffer to create the reader from (assumes ownership).
CThe LLVM context to use to emit diagnostics.
PThe FSDiscriminatorPass.
RemapFilenameThe file used for profile remapping.
Returns
an error code indicating the status of the created reader.

Definition at line 1795 of file SampleProfReader.cpp.

References B, llvm::sampleprof::SampleProfileReaderItaniumRemapper::create(), llvm::sampleprof::FunctionSamples::Format, llvm::sampleprof::SampleProfileReaderText::hasFormat(), llvm::sampleprof::SampleProfileReaderRawBinary::hasFormat(), llvm::sampleprof::SampleProfileReaderExtBinary::hasFormat(), llvm::sampleprof::SampleProfileReaderCompactBinary::hasFormat(), llvm::sampleprof::SampleProfileReaderGCC::hasFormat(), move, P, and llvm::unrecognized_format.

◆ dump()

void SampleProfileReader::dump ( raw_ostream OS = dbgs())

Print all the profiles on stream OS.

Dump all the function profiles found on stream OS.

Definition at line 68 of file SampleProfReader.cpp.

References dumpFunctionProfile(), I, Profiles, and llvm::sampleprof::sortFuncProfiles().

◆ dumpFunctionProfile()

void SampleProfileReader::dumpFunctionProfile ( SampleContext  FContext,
raw_ostream OS = dbgs() 
)

Print the profile for FContext on stream OS.

Dump the function profile for FName.

Parameters
FContextName + context of the function to print.
OSStream to emit the output to.

Definition at line 62 of file SampleProfReader.cpp.

References Profiles, and llvm::sampleprof::SampleContext::toString().

Referenced by dump().

◆ dumpSectionInfo()

virtual bool llvm::sampleprof::SampleProfileReader::dumpSectionInfo ( raw_ostream OS = dbgs())
inlinevirtual

Reimplemented in llvm::sampleprof::SampleProfileReaderExtBinaryBase.

Definition at line 486 of file SampleProfReader.h.

◆ getBuffer()

MemoryBuffer* llvm::sampleprof::SampleProfileReader::getBuffer ( ) const
inline

Definition at line 468 of file SampleProfReader.h.

References Buffer.

◆ getDiscriminatorMask()

uint32_t llvm::sampleprof::SampleProfileReader::getDiscriminatorMask ( ) const
inline

Get the bitmask the discriminators: For FS profiles, return the bit mask for this pass.

For non FS profiles, return (unsigned) -1.

Definition at line 365 of file SampleProfReader.h.

References assert(), llvm::getN1Bits(), MaskedBitFrom, and ProfileIsFS.

Referenced by llvm::sampleprof::SampleProfileReaderText::readImpl(), and llvm::sampleprof::SampleProfileReaderBinary::readProfile().

◆ getFormat()

SampleProfileFormat llvm::sampleprof::SampleProfileReader::getFormat ( ) const
inline

Return the profile format.

Definition at line 471 of file SampleProfReader.h.

References Format.

◆ getNameTable()

virtual std::vector<StringRef>* llvm::sampleprof::SampleProfileReader::getNameTable ( )
inlinevirtual

It includes all the names that have samples either in outline instance or inline instance.

Reimplemented in llvm::sampleprof::SampleProfileReaderBinary.

Definition at line 485 of file SampleProfReader.h.

◆ getOrCreateSamplesFor()

FunctionSamples* llvm::sampleprof::SampleProfileReader::getOrCreateSamplesFor ( const Function F)
inline

Return the samples collected for function F, create empty FunctionSamples if it doesn't exist.

Definition at line 408 of file SampleProfReader.h.

References assert(), F, llvm::sampleprof::FunctionSamples::getCanonicalFnName(), llvm::sampleprof::getRepInFormat(), MD5NameBuffer, Profiles, and useMD5().

◆ getProfiles()

SampleProfileMap& llvm::sampleprof::SampleProfileReader::getProfiles ( )
inline

Return all the profiles.

Definition at line 441 of file SampleProfReader.h.

References Profiles.

◆ getProfileSymbolList()

virtual std::unique_ptr<ProfileSymbolList> llvm::sampleprof::SampleProfileReader::getProfileSymbolList ( )
inlinevirtual

Reimplemented in llvm::sampleprof::SampleProfileReaderExtBinaryBase.

Definition at line 479 of file SampleProfReader.h.

◆ getRemapper()

SampleProfileReaderItaniumRemapper* llvm::sampleprof::SampleProfileReader::getRemapper ( )
inline

Definition at line 497 of file SampleProfReader.h.

References Remapper.

◆ getSamplesFor() [1/2]

FunctionSamples* llvm::sampleprof::SampleProfileReader::getSamplesFor ( const Function F)
inline

Return the samples collected for function F.

Definition at line 398 of file SampleProfReader.h.

References F, and llvm::sampleprof::FunctionSamples::getCanonicalFnName().

◆ getSamplesFor() [2/2]

virtual FunctionSamples* llvm::sampleprof::SampleProfileReader::getSamplesFor ( StringRef  Fname)
inlinevirtual

Return the samples collected for function F.

Definition at line 423 of file SampleProfReader.h.

References llvm::sampleprof::getRepInFormat(), Profiles, Remapper, and useMD5().

◆ getSummary()

ProfileSummary& llvm::sampleprof::SampleProfileReader::getSummary ( ) const
inline

Return the profile summary.

Definition at line 466 of file SampleProfReader.h.

References Summary.

◆ hasUniqSuffix()

virtual bool llvm::sampleprof::SampleProfileReader::hasUniqSuffix ( )
inlinevirtual

Return whether any name in the profile contains ".__uniq." suffix.

Definition at line 495 of file SampleProfReader.h.

◆ profileIsCS()

bool llvm::sampleprof::SampleProfileReader::profileIsCS ( ) const
inline

Whether input profile is fully context-sensitive.

Definition at line 477 of file SampleProfReader.h.

References ProfileIsCS.

◆ profileIsProbeBased()

bool llvm::sampleprof::SampleProfileReader::profileIsProbeBased ( ) const
inline

Whether input profile is based on pseudo probes.

Definition at line 474 of file SampleProfReader.h.

References ProfileIsProbeBased.

◆ read()

std::error_code llvm::sampleprof::SampleProfileReader::read ( )
inline

The interface to read sample profiles from the associated file.

Definition at line 373 of file SampleProfReader.h.

References Ctx, readImpl(), Remapper, llvm::success, useMD5(), and llvm::sampleprof::FunctionSamples::UseMD5.

◆ readHeader()

virtual std::error_code llvm::sampleprof::SampleProfileReader::readHeader ( )
pure virtual

◆ readImpl()

virtual std::error_code llvm::sampleprof::SampleProfileReader::readImpl ( )
pure virtual

◆ reportError()

void llvm::sampleprof::SampleProfileReader::reportError ( int64_t  LineNumber,
const Twine Msg 
) const
inline

◆ setDiscriminatorMaskedBitFrom()

void llvm::sampleprof::SampleProfileReader::setDiscriminatorMaskedBitFrom ( FSDiscriminatorPass  P)
inline

Set the bits for FS discriminators.

Parameter Pass specify the sequence number, Pass == i is for the i-th round of adding FS discriminators. Pass == 0 is for using base discriminators.

Definition at line 359 of file SampleProfReader.h.

References llvm::getFSPassBitEnd(), MaskedBitFrom, and P.

◆ setModule()

void llvm::sampleprof::SampleProfileReader::setModule ( const Module Mod)
inline

Definition at line 499 of file SampleProfReader.h.

References M, and Mod.

◆ setSkipFlatProf()

virtual void llvm::sampleprof::SampleProfileReader::setSkipFlatProf ( bool  Skip)
inlinevirtual

Don't read profile without context if the flag is set.

This is only meaningful for ExtBinary format.

Reimplemented in llvm::sampleprof::SampleProfileReaderExtBinaryBase.

Definition at line 493 of file SampleProfReader.h.

◆ takeSummary()

static std::unique_ptr<ProfileSummary> llvm::sampleprof::SampleProfileReader::takeSummary ( SampleProfileReader Reader)
inlinestaticprotected

Take ownership of the summary of this reader.

Definition at line 524 of file SampleProfReader.h.

References move, and Summary.

◆ useMD5()

virtual bool llvm::sampleprof::SampleProfileReader::useMD5 ( )
inlinevirtual

Return whether names in the profile are all MD5 numbers.

Reimplemented in llvm::sampleprof::SampleProfileReaderCompactBinary, and llvm::sampleprof::SampleProfileReaderExtBinaryBase.

Definition at line 489 of file SampleProfReader.h.

Referenced by getOrCreateSamplesFor(), getSamplesFor(), and read().

Member Data Documentation

◆ Buffer

std::unique_ptr<MemoryBuffer> llvm::sampleprof::SampleProfileReader::Buffer
protected

◆ CSProfileCount

uint32_t llvm::sampleprof::SampleProfileReader::CSProfileCount = 0
protected

Number of context-sensitive profiles.

Definition at line 540 of file SampleProfReader.h.

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

◆ Ctx

LLVMContext& llvm::sampleprof::SampleProfileReader::Ctx
protected

LLVM context used to emit diagnostics.

Definition at line 510 of file SampleProfReader.h.

Referenced by llvm::sampleprof::SampleProfileReaderItaniumRemapper::applyRemapping(), read(), and reportError().

◆ Format

SampleProfileFormat llvm::sampleprof::SampleProfileReader::Format = SPF_None
protected

The format of sample.

Definition at line 546 of file SampleProfReader.h.

Referenced by getFormat().

◆ M

const Module* llvm::sampleprof::SampleProfileReader::M = nullptr
protected

The current module being compiled if SampleProfileReader is used by compiler.

If SampleProfileReader is used by other tools which are not compiler, M is usually nullptr.

Definition at line 551 of file SampleProfReader.h.

Referenced by llvm::sampleprof::SampleProfileReaderExtBinaryBase::collectFuncsFromModule(), llvm::sampleprof::SampleProfileReaderCompactBinary::collectFuncsFromModule(), and setModule().

◆ MaskedBitFrom

uint32_t llvm::sampleprof::SampleProfileReader::MaskedBitFrom = 31
protected

Zero out the discriminator bits higher than bit MaskedBitFrom (0 based).

The default is to keep all the bits.

Definition at line 555 of file SampleProfReader.h.

Referenced by getDiscriminatorMask(), and setDiscriminatorMaskedBitFrom().

◆ MD5NameBuffer

std::unordered_set<std::string> llvm::sampleprof::SampleProfileReader::MD5NameBuffer
protected

Extra name buffer holding names created on demand.

This should only be needed for md5 profiles.

Definition at line 517 of file SampleProfReader.h.

Referenced by getOrCreateSamplesFor().

◆ ProfileIsCS

bool llvm::sampleprof::SampleProfileReader::ProfileIsCS = false
protected

◆ ProfileIsFS

bool llvm::sampleprof::SampleProfileReader::ProfileIsFS = false
protected

◆ ProfileIsProbeBased

bool llvm::sampleprof::SampleProfileReader::ProfileIsProbeBased = false
protected

◆ Profiles

SampleProfileMap llvm::sampleprof::SampleProfileReader::Profiles
protected

Map every function to its associated profile.

The profile of every function executed at runtime is collected in the structure FunctionSamples. This maps function objects to their corresponding profiles.

Definition at line 507 of file SampleProfReader.h.

Referenced by computeSummary(), dump(), dumpFunctionProfile(), getOrCreateSamplesFor(), getProfiles(), getSamplesFor(), llvm::sampleprof::SampleProfileReaderExtBinaryBase::readFuncMetadata(), llvm::sampleprof::SampleProfileReaderText::readImpl(), and llvm::sampleprof::SampleProfileReaderGCC::readOneFunctionProfile().

◆ Remapper

std::unique_ptr<SampleProfileReaderItaniumRemapper> llvm::sampleprof::SampleProfileReader::Remapper
protected

Definition at line 531 of file SampleProfReader.h.

Referenced by getRemapper(), getSamplesFor(), and read().

◆ Summary

std::unique_ptr<ProfileSummary> llvm::sampleprof::SampleProfileReader::Summary
protected

Profile summary information.

Definition at line 520 of file SampleProfReader.h.

Referenced by computeSummary(), getSummary(), and takeSummary().


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