Go to the documentation of this file.
14 #ifndef LLVM_PROFILEDATA_INSTRPROFREADER_H
15 #define LLVM_PROFILEDATA_INSTRPROFREADER_H
40 class InstrProfReader;
43 template <
class record_type = NamedInstrProfRecord,
44 class reader_type = InstrProfReader>
54 reader_type *Reader =
nullptr;
74 return Reader ==
RHS.Reader;
77 return Reader !=
RHS.Reader;
87 std::string LastErrorMsg;
146 LastErrorMsg = ErrMsg;
149 return make_error<InstrProfError>(Err, ErrMsg);
154 LastError = IPE.
get();
157 return make_error<InstrProfError>(LastError, LastErrorMsg);
173 return make_error<InstrProfError>(LastError, LastErrorMsg);
183 create(std::unique_ptr<MemoryBuffer> Buffer,
198 std::unique_ptr<MemoryBuffer> DataBuffer;
208 : DataBuffer(
std::
move(DataBuffer_)), Line(*DataBuffer,
true,
'#') {}
224 return static_cast<bool>(ProfileKind &
257 template <
class IntPtrT>
261 std::unique_ptr<MemoryBuffer> DataBuffer;
265 bool ShouldSwapBytes;
274 const char *CountersStart;
275 const char *CountersEnd;
276 const char *NamesStart;
277 const char *NamesEnd;
280 const uint8_t *ValueDataStart;
285 const uint8_t *BinaryIdsStart;
290 : DataBuffer(
std::
move(DataBuffer)),
302 return (Version & VARIANT_MASK_IR_PROF) != 0;
306 return (Version & VARIANT_MASK_CSIR_PROF) != 0;
310 return (Version & VARIANT_MASK_INSTR_ENTRY) != 0;
314 return (Version & VARIANT_MASK_DBG_CORRELATE) != 0;
318 return (Version & VARIANT_MASK_BYTE_COVERAGE) != 0;
322 return (Version & VARIANT_MASK_FUNCTION_ENTRY_ONLY) != 0;
335 Error readNextHeader(
const char *CurrentPos);
338 template <
class IntT> IntT swap(IntT Int)
const {
344 if (!ShouldSwapBytes)
352 inline uint8_t getNumPaddingBytes(
uint64_t SizeInBytes) {
356 Error readName(NamedInstrProfRecord &Record);
357 Error readFuncHash(NamedInstrProfRecord &Record);
358 Error readRawCounts(InstrProfRecord &Record);
359 Error readValueProfilingData(InstrProfRecord &Record);
360 bool atEnd()
const {
return Data == DataEnd; }
370 CountersDelta -=
sizeof(*Data);
373 ValueDataStart += CurValueDataSize;
376 const char *getNextHeaderPos()
const {
378 return (
const char *)ValueDataStart;
381 StringRef getName(
uint64_t NameRef)
const {
382 return Symtab->getFuncName(swap(NameRef));
385 int getCounterTypeSize()
const {
393 namespace IndexedInstrProf {
402 std::vector<NamedInstrProfRecord> DataBuffer;
404 unsigned FormatVersion;
427 static std::pair<offset_type, offset_type>
431 offset_type KeyLen = endian::readNext<offset_type, little, unaligned>(
D);
432 offset_type DataLen = endian::readNext<offset_type, little, unaligned>(
D);
433 return std::make_pair(KeyLen, DataLen);
440 bool readValueProfilingData(
const unsigned char *&
D,
441 const unsigned char *
const End);
460 virtual void advanceToNextKey() = 0;
461 virtual bool atEnd()
const = 0;
463 virtual uint64_t getVersion()
const = 0;
464 virtual bool isIRLevelProfile()
const = 0;
465 virtual bool hasCSIRLevelProfile()
const = 0;
466 virtual bool instrEntryBBEnabled()
const = 0;
467 virtual bool hasSingleByteCoverage()
const = 0;
468 virtual bool functionEntryOnly()
const = 0;
481 template <
typename HashTableImpl>
484 template <
typename HashTableImpl>
487 std::unique_ptr<HashTableImpl> HashTable;
488 typename HashTableImpl::data_iterator RecordIterator;
495 const unsigned char *
const Payload,
496 const unsigned char *
const Base,
506 return RecordIterator == HashTable->data_end();
510 HashTable->getInfoObj().setValueProfDataEndianness(
Endianness);
516 return (FormatVersion & VARIANT_MASK_IR_PROF) != 0;
520 return (FormatVersion & VARIANT_MASK_CSIR_PROF) != 0;
524 return (FormatVersion & VARIANT_MASK_INSTR_ENTRY) != 0;
528 return (FormatVersion & VARIANT_MASK_BYTE_COVERAGE) != 0;
532 return (FormatVersion & VARIANT_MASK_FUNCTION_ENTRY_ONLY) != 0;
538 return Symtab.
create(HashTable->keys());
555 std::unique_ptr<MemoryBuffer> DataBuffer;
557 std::unique_ptr<MemoryBuffer> RemappingBuffer;
559 std::unique_ptr<InstrProfReaderIndexBase> Index;
561 std::unique_ptr<InstrProfReaderRemapper> Remapper;
563 std::unique_ptr<ProfileSummary> Summary;
565 std::unique_ptr<ProfileSummary> CS_Summary;
569 std::unique_ptr<MemProfRecordHashTable> MemProfRecordTable;
571 std::unique_ptr<MemProfFrameHashTable> MemProfFrameTable;
574 unsigned RecordIndex;
580 const unsigned char *Cur,
bool UseCS);
584 std::unique_ptr<MemoryBuffer> DataBuffer,
585 std::unique_ptr<MemoryBuffer> RemappingBuffer =
nullptr)
586 : DataBuffer(
std::
move(DataBuffer)),
587 RemappingBuffer(
std::
move(RemappingBuffer)), RecordIndex(0) {}
595 return Index->hasCSIRLevelProfile();
599 return Index->instrEntryBBEnabled();
603 return Index->hasSingleByteCoverage();
611 return Index->getProfileKind();
618 Error readHeader()
override;
632 std::vector<uint64_t> &Counts);
638 assert(CS_Summary &&
"No context sensitive profile summary");
639 return CS_Summary->getMaxFunctionCount();
641 assert(Summary &&
"No profile summary");
642 return Summary->getMaxFunctionCount();
648 create(
const Twine &Path,
const Twine &RemappingPath =
"");
651 create(std::unique_ptr<MemoryBuffer> Buffer,
652 std::unique_ptr<MemoryBuffer> RemappingBuffer =
nullptr);
656 Index->setValueProfDataEndianness(
Endianness);
668 assert(CS_Summary &&
"No context sensitive summary");
669 return *(CS_Summary.get());
671 assert(Summary &&
"No profile summary");
672 return *(Summary.get());
679 #endif // LLVM_PROFILEDATA_INSTRPROFREADER_H
bool functionEntryOnly() const override
Return true if the profile only instruments function entries.
Reader for the raw instrprof binary format from runtime.
ProfileSummary & getSummary(bool UseCS)
Return the profile summary.
Error success()
Clear the current error and return a successful one.
This is an optimization pass for GlobalISel generic memory operations.
bool atEnd() const override
bool instrEntryBBEnabled() const override
constexpr support::endianness Endianness
The endianness of all multi-byte encoded values in MessagePack.
@ FunctionEntryInstrumentation
InstrProfLookupTrait::offset_type offset_type
bool operator==(const InstrProfIterator &RHS) const
InstrProfReader()=default
bool hasSingleByteCoverage() const override
Return true if the profile has single byte counters representing coverage.
A forward iterator which reads text lines from a buffer.
bool instrEntryBBEnabled() const override
InstrProfKind getProfileKind() const override
Returns a BitsetEnum describing the attributes of the raw instr profile.
bool functionEntryOnly() const override
Return true if the profile only instruments function entries.
void advanceToNextKey() override
bool hasSingleByteCoverage() const override
Return true if the profile has single byte counters representing coverage.
virtual Error readNextRecord(NamedInstrProfRecord &Record)=0
Read a single record.
uint64_t ComputeHash(StringRef K)
Error create(object::SectionRef &Section)
Create InstrProfSymtab from an object file section which contains function PGO names.
instrprof_error get() const
static ErrorSuccess success()
Create a success value.
bool isEOF()
Return true if the reader has finished reading the profile data.
virtual bool functionEntryOnly() const =0
Return true if the profile only instruments function entries.
Tagged union holding either a T or a Error.
static StringRef GetInternalKey(StringRef K)
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Profiling information for a single function.
bool isIRLevelProfile() const override
void setValueProfDataEndianness(support::endianness Endianness)
TextInstrProfReader & operator=(const TextInstrProfReader &)=delete
InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template pointer type so that the Pro...
Error readHeader() override
Read the header.
bool hasSingleByteCoverage() const override
Return true if the profile has single byte counters representing coverage.
InstrProfKind getProfileKind() const override
Returns a BitsetEnum describing the attributes of the indexed instr profile.
bool hasError()
Return true if the reader encountered an error reading profiling data.
Trait for lookups into the on-disk hash table for the binary instrprof format.
InstrProfSymtab & getSymtab() override
Return the PGO symtab.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void setValueProfDataEndianness(support::endianness Endianness)
InstrProfIterator & operator++()
static bool EqualKey(StringRef A, StringRef B)
const std::string & getMessage() const
bool functionEntryOnly() const override
RawInstrProfReader(std::unique_ptr< MemoryBuffer > DataBuffer, const InstrProfCorrelator *Correlator)
virtual bool hasCSIRLevelProfile() const =0
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
value_type * operator->()
InstrProfIterator(reader_type *Reader)
void setValueProfDataEndianness(support::endianness Endianness) override
This class implements an extremely fast bulk output stream that can only output to a stream.
bool hasCSIRLevelProfile() const override
Error readNextRecord(NamedInstrProfRecord &Record) override
Read a single record.
static StringRef GetExternalKey(StringRef K)
std::unique_ptr< InstrProfSymtab > Symtab
static instrprof_error take(Error E)
Consume an Error and return the raw enum value contained within it.
virtual Error populateRemappings()
unsigned char getSwappedBytes(unsigned char C)
support::endianness getHostEndianness()
bool isIRLevelProfile() const override
virtual ~InstrProfReader()=default
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
uint64_t getVersion() const
Return the profile version.
Reader for the indexed binary instrprof format.
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Reader for the simple text based instrprof format.
bool hasSingleByteCoverage() const override
virtual bool isIRLevelProfile() const =0
bool hasCSIRLevelProfile() const override
InstrProfLookupTrait(IndexedInstrProf::HashT HashType, unsigned FormatVersion)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
IndexedInstrProfReader(std::unique_ptr< MemoryBuffer > DataBuffer, std::unique_ptr< MemoryBuffer > RemappingBuffer=nullptr)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
bool isIRLevelProfile() const override
bool hasCSIRLevelProfile() const override
virtual Error printBinaryIds(raw_ostream &OS)
Print binary ids on stream OS.
Provides lookup and iteration over an on disk hash table.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
virtual bool useDebugInfoCorrelate() const
Return true if we must provide debug info to create PGO profiles.
StringRef - Represent a constant reference to a string, i.e.
RawInstrProfReader & operator=(const RawInstrProfReader &)=delete
virtual bool instrEntryBBEnabled() const =0
bool hasCSIRLevelProfile() const override
Error readHeader() override
Read the header. Required before reading first record.
static bool hasFormat(const MemoryBuffer &DataBuffer)
InstrProfKind getProfileKind() const override
Returns a BitsetEnum describing the attributes of the profile.
A remapper that applies remappings based on a symbol remapping file.
std::ptrdiff_t difference_type
virtual bool hasSingleByteCoverage() const =0
Return true if the profile has single byte counters representing coverage.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
void accumulateCounts(CountSumOrPercent &Sum, bool IsCS)
Compute the sum of counts and return in Sum.
Lightweight error class with error context and mandatory checking.
InstrProfSymtab & getSymtab() override
Return the PGO symtab.
Error populateSymtab(InstrProfSymtab &Symtab) override
Name matcher supporting fuzzy matching of symbol names to names in profiles.
static Expected< std::unique_ptr< InstrProfReader > > create(const Twine &Path, const InstrProfCorrelator *Correlator=nullptr)
Factory method to create an appropriately typed reader for the given instrprof file.
virtual Error readHeader()=0
Read the header. Required before reading first record.
Error readNextRecord(NamedInstrProfRecord &Record) override
Read a single record.
bool instrEntryBBEnabled() const override
bool instrEntryBBEnabled() const override
Error getError()
Get the current error.
TextInstrProfReader(std::unique_ptr< MemoryBuffer > DataBuffer_)
Error printBinaryIds(raw_ostream &OS) override
Print binary ids on stream OS.
bool functionEntryOnly() const override
Return true if the profile only instruments function entries.
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if the given buffer is in text instrprof format.
bool isIRLevelProfile() const override
InstrProfLookupTrait::data_type data_type
InstrProfKind
An enum describing the attributes of an instrumented profile.
Reimplement select in terms of SEL *We would really like to support but we need to prove that the add doesn t need to overflow between the two bit chunks *Implement pre post increment support(e.g. PR935) *Implement smarter const ant generation for binops with large immediates. A few ARMv6T2 ops should be pattern matched
StringRef ReadKey(const unsigned char *D, offset_type N)
InstrProfCorrelator - A base class used to create raw instrumentation data to their functions.
uint64_t getVersion() const override
static std::pair< offset_type, offset_type > ReadKeyDataLength(const unsigned char *&D)
A file format agnostic iterator over profiling data.
virtual InstrProfSymtab & getSymtab()=0
Return the PGO symtab.
uint64_t getMaximumFunctionCount(bool UseCS)
Return the maximum of all known function counts.
std::input_iterator_tag iterator_category
virtual InstrProfKind getProfileKind() const =0
Returns a BitsetEnum describing the attributes of the profile.
InstrProfIterator begin()
Iterator over profile data.
bool operator!=(const InstrProfIterator &RHS) const
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
InstrProfIterator()=default
Base class and interface for reading profiling data of any known instrprof format.
Error error(instrprof_error Err, const std::string &ErrMsg="")
Set the current error and return same.
bool useDebugInfoCorrelate() const override
Return true if we must provide debug info to create PGO profiles.
auto dyn_cast_or_null(const Y &Val)