14#ifndef LLVM_PROFILEDATA_INSTRPROFREADER_H
15#define LLVM_PROFILEDATA_INSTRPROFREADER_H
49template <
class record_type = NamedInstrProfRecord,
50 class reader_type = InstrProfReader>
60 reader_type *Reader =
nullptr;
80 return Reader ==
RHS.Reader;
83 return Reader !=
RHS.Reader;
93 std::string LastErrorMsg;
163 LastErrorMsg = ErrMsg;
166 return make_error<InstrProfError>(Err, ErrMsg);
171 LastError = IPE.
get();
174 return make_error<InstrProfError>(LastError, LastErrorMsg);
190 return make_error<InstrProfError>(LastError, LastErrorMsg);
201 create(std::unique_ptr<MemoryBuffer> Buffer,
216 std::unique_ptr<MemoryBuffer> DataBuffer;
226 : DataBuffer(
std::
move(DataBuffer_)), Line(*DataBuffer,
true,
'#') {}
245 return static_cast<bool>(ProfileKind &
283template <
class IntPtrT>
287 std::unique_ptr<MemoryBuffer> DataBuffer;
291 bool ShouldSwapBytes;
300 const char *CountersStart;
301 const char *CountersEnd;
302 const char *NamesStart;
303 const char *NamesEnd;
306 const uint8_t *ValueDataStart;
313 const uint8_t *BinaryIdsStart;
318 : DataBuffer(
std::
move(DataBuffer)),
333 return (Version & VARIANT_MASK_IR_PROF) != 0;
337 return (Version & VARIANT_MASK_CSIR_PROF) != 0;
341 return (Version & VARIANT_MASK_INSTR_ENTRY) != 0;
345 return (Version & VARIANT_MASK_DBG_CORRELATE) != 0;
349 return (Version & VARIANT_MASK_BYTE_COVERAGE) != 0;
353 return (Version & VARIANT_MASK_FUNCTION_ENTRY_ONLY) != 0;
358 assert(!(Version & VARIANT_MASK_MEMPROF));
372 Error readNextHeader(
const char *CurrentPos);
375 template <
class IntT> IntT swap(IntT
Int)
const {
381 if (!ShouldSwapBytes)
389 inline uint8_t getNumPaddingBytes(
uint64_t SizeInBytes) {
393 Error readName(NamedInstrProfRecord &Record);
394 Error readFuncHash(NamedInstrProfRecord &Record);
395 Error readRawCounts(InstrProfRecord &Record);
396 Error readValueProfilingData(InstrProfRecord &Record);
397 bool atEnd()
const {
return Data == DataEnd; }
407 CountersDelta -=
sizeof(*Data);
410 ValueDataStart += CurValueDataSize;
413 const char *getNextHeaderPos()
const {
415 return (
const char *)ValueDataStart;
418 StringRef getName(
uint64_t NameRef)
const {
419 return Symtab->getFuncName(swap(NameRef));
422 int getCounterTypeSize()
const {
430namespace IndexedInstrProf {
439 std::vector<NamedInstrProfRecord> DataBuffer;
441 unsigned FormatVersion;
449 : HashType(HashType), FormatVersion(FormatVersion) {}
464 static std::pair<offset_type, offset_type>
466 using namespace support;
468 offset_type KeyLen = endian::readNext<offset_type, little, unaligned>(
D);
469 offset_type DataLen = endian::readNext<offset_type, little, unaligned>(
D);
470 return std::make_pair(KeyLen, DataLen);
478 const unsigned char *
const End);
483 ValueProfDataEndianness = Endianness;
519template <
typename HashTableImpl>
522template <
typename HashTableImpl>
525 std::unique_ptr<HashTableImpl> HashTable;
526 typename HashTableImpl::data_iterator RecordIterator;
533 const unsigned char *
const Payload,
534 const unsigned char *
const Base,
544 return RecordIterator == HashTable->data_end();
548 HashTable->getInfoObj().setValueProfDataEndianness(Endianness);
554 return (FormatVersion & VARIANT_MASK_IR_PROF) != 0;
558 return (FormatVersion & VARIANT_MASK_CSIR_PROF) != 0;
562 return (FormatVersion & VARIANT_MASK_INSTR_ENTRY) != 0;
566 return (FormatVersion & VARIANT_MASK_BYTE_COVERAGE) != 0;
570 return (FormatVersion & VARIANT_MASK_FUNCTION_ENTRY_ONLY) != 0;
574 return (FormatVersion & VARIANT_MASK_MEMPROF) != 0;
580 return Symtab.
create(HashTable->keys());
597 std::unique_ptr<MemoryBuffer> DataBuffer;
599 std::unique_ptr<MemoryBuffer> RemappingBuffer;
601 std::unique_ptr<InstrProfReaderIndexBase>
Index;
603 std::unique_ptr<InstrProfReaderRemapper> Remapper;
605 std::unique_ptr<ProfileSummary> Summary;
607 std::unique_ptr<ProfileSummary> CS_Summary;
611 std::unique_ptr<MemProfRecordHashTable> MemProfRecordTable;
613 std::unique_ptr<MemProfFrameHashTable> MemProfFrameTable;
617 const uint8_t *BinaryIdsStart =
nullptr;
620 unsigned RecordIndex;
626 const unsigned char *Cur,
bool UseCS);
630 std::unique_ptr<MemoryBuffer> DataBuffer,
631 std::unique_ptr<MemoryBuffer> RemappingBuffer =
nullptr)
632 : DataBuffer(
std::
move(DataBuffer)),
633 RemappingBuffer(
std::
move(RemappingBuffer)), RecordIndex(0) {}
641 return Index->hasCSIRLevelProfile();
645 return Index->instrEntryBBEnabled();
649 return Index->hasSingleByteCoverage();
659 return Index->getProfileKind();
677 uint64_t *MismatchedFuncSum =
nullptr);
685 std::vector<uint64_t> &Counts);
691 assert(CS_Summary &&
"No context sensitive profile summary");
692 return CS_Summary->getMaxFunctionCount();
694 assert(Summary &&
"No profile summary");
695 return Summary->getMaxFunctionCount();
702 const Twine &RemappingPath =
"");
705 create(std::unique_ptr<MemoryBuffer> Buffer,
706 std::unique_ptr<MemoryBuffer> RemappingBuffer =
nullptr);
710 Index->setValueProfDataEndianness(Endianness);
722 assert(CS_Summary &&
"No context sensitive summary");
725 assert(Summary &&
"No profile summary");
This file declares a library for handling Build IDs and using them to find debug info.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
InstrProfLookupTrait::offset_type offset_type
InstrProfLookupTrait::data_type data_type
Defines facilities for reading and writing on-disk hash tables.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Reader for the indexed binary instrprof format.
uint64_t getVersion() const override
Return the profile version.
Error readNextRecord(NamedInstrProfRecord &Record) override
Read a single record.
static Expected< std::unique_ptr< IndexedInstrProfReader > > create(const Twine &Path, vfs::FileSystem &FS, const Twine &RemappingPath="")
Factory method to create an indexed reader.
IndexedInstrProfReader(const IndexedInstrProfReader &)=delete
Expected< memprof::MemProfRecord > getMemProfRecord(uint64_t FuncNameHash)
Return the memprof record for the function identified by llvm::md5(Name).
Error readHeader() override
Read the file header.
Error printBinaryIds(raw_ostream &OS) override
Print binary ids.
void setValueProfDataEndianness(support::endianness Endianness)
bool hasSingleByteCoverage() const override
Return true if the profile has single byte counters representing coverage.
Expected< InstrProfRecord > getInstrProfRecord(StringRef FuncName, uint64_t FuncHash, uint64_t *MismatchedFuncSum=nullptr)
Return the NamedInstrProfRecord associated with FuncName and FuncHash.
InstrProfSymtab & getSymtab() override
Return the PGO symtab.
ProfileSummary & getSummary(bool UseCS)
Return the profile summary.
static bool hasFormat(const MemoryBuffer &DataBuffer)
Return true if the given buffer is in an indexed instrprof format.
bool hasMemoryProfile() const override
Return true if profile includes a memory profile.
bool functionEntryOnly() const override
Return true if the profile only instruments function entries.
uint64_t getMaximumFunctionCount(bool UseCS)
Return the maximum of all known function counts.
Error getFunctionCounts(StringRef FuncName, uint64_t FuncHash, std::vector< uint64_t > &Counts)
Fill Counts with the profile data for the given function name.
InstrProfKind getProfileKind() const override
Returns a BitsetEnum describing the attributes of the indexed instr profile.
IndexedInstrProfReader(std::unique_ptr< MemoryBuffer > DataBuffer, std::unique_ptr< MemoryBuffer > RemappingBuffer=nullptr)
Error readBinaryIds(std::vector< llvm::object::BuildID > &BinaryIds) override
Read a list of binary ids.
bool hasCSIRLevelProfile() const override
IndexedInstrProfReader & operator=(const IndexedInstrProfReader &)=delete
bool instrEntryBBEnabled() const override
bool isIRLevelProfile() const override
InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template pointer type so that the Pro...
InstrProfCorrelator - A base class used to create raw instrumentation data to their functions.
const std::string & getMessage() const
instrprof_error get() const
static instrprof_error take(Error E)
Consume an Error and return the raw enum value contained within it.
A file format agnostic iterator over profiling data.
bool operator==(const InstrProfIterator &RHS) const
InstrProfIterator(reader_type *Reader)
InstrProfIterator & operator++()
bool operator!=(const InstrProfIterator &RHS) const
value_type * operator->()
std::ptrdiff_t difference_type
std::input_iterator_tag iterator_category
InstrProfIterator()=default
Trait for lookups into the on-disk hash table for the binary instrprof format.
InstrProfLookupTrait(IndexedInstrProf::HashT HashType, unsigned FormatVersion)
void setValueProfDataEndianness(support::endianness Endianness)
StringRef ReadKey(const unsigned char *D, offset_type N)
static std::pair< offset_type, offset_type > ReadKeyDataLength(const unsigned char *&D)
static StringRef GetExternalKey(StringRef K)
data_type ReadData(StringRef K, const unsigned char *D, offset_type N)
bool readValueProfilingData(const unsigned char *&D, const unsigned char *const End)
hash_value_type ComputeHash(StringRef K)
static bool EqualKey(StringRef A, StringRef B)
static StringRef GetInternalKey(StringRef K)
Error populateSymtab(InstrProfSymtab &Symtab) override
bool hasSingleByteCoverage() const override
void advanceToNextKey() override
bool hasCSIRLevelProfile() const override
InstrProfKind getProfileKind() const override
Error getRecords(ArrayRef< NamedInstrProfRecord > &Data) override
bool functionEntryOnly() const override
~InstrProfReaderIndex() override=default
uint64_t getVersion() const override
bool isIRLevelProfile() const override
bool hasMemoryProfile() const override
bool instrEntryBBEnabled() const override
void setValueProfDataEndianness(support::endianness Endianness) override
bool atEnd() const override
A remapper that applies remappings based on a symbol remapping file.
Name matcher supporting fuzzy matching of symbol names to names in profiles.
virtual Error getRecords(StringRef FuncName, ArrayRef< NamedInstrProfRecord > &Data)=0
virtual ~InstrProfReaderRemapper()=default
virtual Error populateRemappings()
Base class and interface for reading profiling data of any known instrprof format.
InstrProfIterator begin()
Iterator over profile data.
virtual bool instrEntryBBEnabled() const =0
virtual Error readNextRecord(NamedInstrProfRecord &Record)=0
Read a single record.
static Expected< std::unique_ptr< InstrProfReader > > create(const Twine &Path, vfs::FileSystem &FS, const InstrProfCorrelator *Correlator=nullptr)
Factory method to create an appropriately typed reader for the given instrprof file.
virtual Error readBinaryIds(std::vector< llvm::object::BuildID > &BinaryIds)
Read a list of binary ids.
virtual bool functionEntryOnly() const =0
Return true if the profile only instruments function entries.
std::unique_ptr< InstrProfSymtab > Symtab
Error getError()
Get the current error.
virtual InstrProfSymtab & getSymtab()=0
Return the PGO symtab.
InstrProfReader()=default
virtual bool hasSingleByteCoverage() const =0
Return true if the profile has single byte counters representing coverage.
Error success()
Clear the current error and return a successful one.
bool hasError()
Return true if the reader encountered an error reading profiling data.
virtual InstrProfKind getProfileKind() const =0
Returns a BitsetEnum describing the attributes of the profile.
virtual Error printBinaryIds(raw_ostream &OS)
Print binary ids.
virtual bool useDebugInfoCorrelate() const
Return true if we must provide debug info to create PGO profiles.
virtual uint64_t getVersion() const =0
Return the profile version.
virtual bool hasMemoryProfile() const =0
Return true if profile includes a memory profile.
virtual bool hasCSIRLevelProfile() const =0
virtual bool isIRLevelProfile() const =0
virtual ~InstrProfReader()=default
virtual Error readHeader()=0
Read the header. Required before reading first record.
Error error(instrprof_error Err, const std::string &ErrMsg="")
Set the current error and return same.
void accumulateCounts(CountSumOrPercent &Sum, bool IsCS)
Compute the sum of counts and return in Sum.
bool isEOF()
Return true if the reader has finished reading the profile data.
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Error create(object::SectionRef &Section)
Create InstrProfSymtab from an object file section which contains function PGO names.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Provides lookup and iteration over an on disk hash table.
Reader for the raw instrprof binary format from runtime.
bool functionEntryOnly() const override
Return true if the profile only instruments function entries.
Error readHeader() override
Read the header. Required before reading first record.
Error readNextRecord(NamedInstrProfRecord &Record) override
Read a single record.
Error printBinaryIds(raw_ostream &OS) override
Print binary ids.
static bool hasFormat(const MemoryBuffer &DataBuffer)
RawInstrProfReader & operator=(const RawInstrProfReader &)=delete
bool hasSingleByteCoverage() const override
Return true if the profile has single byte counters representing coverage.
bool useDebugInfoCorrelate() const override
Return true if we must provide debug info to create PGO profiles.
bool isIRLevelProfile() const override
InstrProfKind getProfileKind() const override
Returns a BitsetEnum describing the attributes of the raw instr profile.
bool hasMemoryProfile() const override
Return true if profile includes a memory profile.
InstrProfSymtab & getSymtab() override
Return the PGO symtab.
Error readBinaryIds(std::vector< llvm::object::BuildID > &BinaryIds) override
Read a list of binary ids.
bool instrEntryBBEnabled() const override
RawInstrProfReader(std::unique_ptr< MemoryBuffer > DataBuffer, const InstrProfCorrelator *Correlator)
uint64_t getVersion() const override
Return the profile version.
RawInstrProfReader(const RawInstrProfReader &)=delete
bool hasCSIRLevelProfile() const override
StringRef - Represent a constant reference to a string, i.e.
Reader for the simple text based instrprof format.
bool isIRLevelProfile() const override
uint64_t getVersion() const override
Return the profile version.
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if the given buffer is in text instrprof format.
TextInstrProfReader(std::unique_ptr< MemoryBuffer > DataBuffer_)
bool hasSingleByteCoverage() const override
Return true if the profile has single byte counters representing coverage.
Error readNextRecord(NamedInstrProfRecord &Record) override
Read a single record.
TextInstrProfReader(const TextInstrProfReader &)=delete
bool hasMemoryProfile() const override
Return true if profile includes a memory profile.
bool hasCSIRLevelProfile() const override
InstrProfSymtab & getSymtab() override
Return the PGO symtab.
bool instrEntryBBEnabled() const override
bool functionEntryOnly() const override
Return true if the profile only instruments function entries.
InstrProfKind getProfileKind() const override
Returns a BitsetEnum describing the attributes of the profile.
Error readHeader() override
Read the header.
TextInstrProfReader & operator=(const TextInstrProfReader &)=delete
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
A forward iterator which reads text lines from a buffer.
This class implements an extremely fast bulk output stream that can only output to a stream.
The virtual file system interface.
unsigned char getSwappedBytes(unsigned char C)
This is an optimization pass for GlobalISel generic memory operations.
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
auto dyn_cast_or_null(const Y &Val)
support::endianness getHostEndianness()
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
InstrProfKind
An enum describing the attributes of an instrumented profile.
@ FunctionEntryInstrumentation
virtual Error populateSymtab(InstrProfSymtab &)=0
virtual Error getRecords(ArrayRef< NamedInstrProfRecord > &Data)=0
virtual ~InstrProfReaderIndexBase()=default
virtual InstrProfKind getProfileKind() const =0
virtual bool isIRLevelProfile() const =0
virtual void advanceToNextKey()=0
virtual bool hasMemoryProfile() const =0
virtual bool hasCSIRLevelProfile() const =0
virtual uint64_t getVersion() const =0
virtual bool atEnd() const =0
virtual bool instrEntryBBEnabled() const =0
virtual Error getRecords(StringRef FuncName, ArrayRef< NamedInstrProfRecord > &Data)=0
virtual void setValueProfDataEndianness(support::endianness Endianness)=0
virtual bool functionEntryOnly() const =0
virtual bool hasSingleByteCoverage() const =0
Profiling information for a single function.