15 #ifndef LLVM_PROFILEDATA_INSTRPROFREADER_H
16 #define LLVM_PROFILEDATA_INSTRPROFREADER_H
39 class InstrProfReader;
43 NamedInstrProfRecord> {
94 std::unique_ptr<InstrProfSymtab>
Symtab;
101 return make_error<InstrProfError>(Err);
119 return make_error<InstrProfError>(LastError);
128 create(std::unique_ptr<MemoryBuffer> Buffer);
142 std::unique_ptr<MemoryBuffer> DataBuffer;
145 bool IsIRLevelProfile =
false;
151 : DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer,
true,
'#') {}
179 template <
class IntPtrT>
183 std::unique_ptr<MemoryBuffer> DataBuffer;
184 bool ShouldSwapBytes;
189 uint64_t CountersDelta;
193 const uint64_t *CountersStart;
194 const char *NamesStart;
198 const uint8_t *ValueDataStart;
206 : DataBuffer(std::move(DataBuffer)) {}
215 return (Version & VARIANT_MASK_IR_PROF) != 0;
225 Error readNextHeader(
const char *CurrentPos);
228 template <
class IntT> IntT swap(IntT Int)
const {
234 if (!ShouldSwapBytes)
242 inline uint8_t getNumPaddingBytes(uint64_t SizeInBytes) {
243 return 7 & (
sizeof(uint64_t) - SizeInBytes %
sizeof(uint64_t));
246 Error readName(NamedInstrProfRecord &Record);
247 Error readFuncHash(NamedInstrProfRecord &Record);
248 Error readRawCounts(InstrProfRecord &Record);
249 Error readValueProfilingData(InstrProfRecord &Record);
250 bool atEnd()
const {
return Data == DataEnd; }
254 ValueDataStart += CurValueDataSize;
257 const char *getNextHeaderPos()
const {
259 return (
const char *)ValueDataStart;
262 const uint64_t *getCounter(IntPtrT CounterPtr)
const {
263 ptrdiff_t Offset = (swap(CounterPtr) - CountersDelta) /
sizeof(uint64_t);
264 return CountersStart +
Offset;
267 StringRef getName(uint64_t NameRef)
const {
268 return Symtab->getFuncName(swap(NameRef));
275 namespace IndexedInstrProf {
284 std::vector<NamedInstrProfRecord> DataBuffer;
286 unsigned FormatVersion;
294 : HashType(HashType), FormatVersion(FormatVersion) {}
309 static std::pair<offset_type, offset_type>
311 using namespace support;
313 offset_type KeyLen = endian::readNext<offset_type, little, unaligned>(
D);
314 offset_type DataLen = endian::readNext<offset_type, little, unaligned>(
D);
315 return std::make_pair(KeyLen, DataLen);
322 bool readValueProfilingData(
const unsigned char *&
D,
323 const unsigned char *
const End);
328 ValueProfDataEndianness = Endianness;
342 virtual void advanceToNextKey() = 0;
343 virtual bool atEnd()
const = 0;
345 virtual uint64_t getVersion()
const = 0;
346 virtual bool isIRLevelProfile()
const = 0;
353 template <
typename HashTableImpl>
356 std::unique_ptr<HashTableImpl> HashTable;
357 typename HashTableImpl::data_iterator RecordIterator;
358 uint64_t FormatVersion;
362 const unsigned char *
const Payload,
363 const unsigned char *
const Base,
373 return RecordIterator == HashTable->data_end();
377 HashTable->getInfoObj().setValueProfDataEndianness(Endianness);
380 uint64_t
getVersion()
const override {
return GET_VERSION(FormatVersion); }
383 return (FormatVersion & VARIANT_MASK_IR_PROF) != 0;
387 return Symtab.
create(HashTable->keys());
395 std::unique_ptr<MemoryBuffer> DataBuffer;
397 std::unique_ptr<InstrProfReaderIndexBase> Index;
399 std::unique_ptr<ProfileSummary> Summary;
404 const unsigned char *Cur);
408 : DataBuffer(std::move(DataBuffer)) {}
420 Error readHeader()
override;
430 std::vector<uint64_t> &Counts);
437 create(
const Twine &Path);
440 create(std::unique_ptr<MemoryBuffer> Buffer);
444 Index->setValueProfDataEndianness(Endianness);
456 #endif // LLVM_PROFILEDATA_INSTRPROFREADER_H
A symbol table used for function PGO name look-up with keys (such as pointers, md5hash values) to the...
Error readHeader() override
Read the header. Required before reading first record.
void setValueProfDataEndianness(support::endianness Endianness)
bool isIRLevelProfile() const override
Defines facilities for reading and writing on-disk hash tables.
static instrprof_error take(Error E)
Consume an Error and return the raw enum value contained within it.
uint64_t getVersion() const
Return the profile version.
TextInstrProfReader & operator=(const TextInstrProfReader &)=delete
Error error(instrprof_error Err)
Set the current error and return same.
uint64_t getMaximumFunctionCount()
Return the maximum of all known function counts.
A forward iterator which reads text lines from a buffer.
InstrProfIterator(InstrProfReader *Reader)
bool operator==(const InstrProfIterator &RHS)
bool isIRLevelProfile() const override
InstrProfLookupTrait::offset_type offset_type
Error readNextRecord(NamedInstrProfRecord &Record) override
Read a single record.
Error readHeader() override
Read the header.
bool operator!=(const InstrProfIterator &RHS)
bool isEOF()
Return true if the reader has finished reading the profile data.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static Expected< std::unique_ptr< InstrProfReader > > create(const Twine &Path)
Factory method to create an appropriately typed reader for the given instrprof file.
Error populateSymtab(InstrProfSymtab &Symtab) override
Tagged union holding either a T or a Error.
void setValueProfDataEndianness(support::endianness Endianness)
bool isIRLevelProfile() const override
virtual ~InstrProfReader()=default
InstrProfReader()=default
RawInstrProfReader(std::unique_ptr< MemoryBuffer > DataBuffer)
static bool hasFormat(const MemoryBuffer &Buffer)
Return true if the given buffer is in text instrprof format.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
static std::pair< offset_type, offset_type > ReadKeyDataLength(const unsigned char *&D)
virtual InstrProfSymtab & getSymtab()=0
Return the PGO symtab.
Reader for the simple text based instrprof format.
static bool hasFormat(const MemoryBuffer &DataBuffer)
StringRef ReadKey(const unsigned char *D, offset_type N)
uint64_t getVersion() const override
InstrProfLookupTrait::data_type data_type
InstrProfSymtab & getSymtab() override
Return the PGO symtab.
Base class and interface for reading profiling data of any known instrprof format.
std::vector< std::pair< uint64_t, uint64_t >> ValueMapType
std::unique_ptr< InstrProfSymtab > Symtab
static const unsigned End
value_type * operator->()
void advanceToNextKey() override
uint64_t ComputeHash(StringRef K)
bool atEnd() const override
Error create(object::SectionRef &Section)
Create InstrProfSymtab from an object file section which contains function PGO names.
static ErrorSuccess success()
Create a success value.
Trait for lookups into the on-disk hash table for the binary instrprof format.
A file format agnostic iterator over profiling data.
Reader for the raw instrprof binary format from runtime.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Error readNextRecord(NamedInstrProfRecord &Record) override
Read a single record.
unsigned char getSwappedBytes(unsigned char C)
TextInstrProfReader(std::unique_ptr< MemoryBuffer > DataBuffer_)
virtual Error readHeader()=0
Read the header. Required before reading first record.
Profiling information for a single function.
virtual Error readNextRecord(NamedInstrProfRecord &Record)=0
Read a single record.
RawInstrProfReader & operator=(const RawInstrProfReader &)=delete
InstrProfIterator begin()
Iterator over profile data.
InstrProfSymtab & getSymtab() override
Return the PGO symtab.
virtual bool isIRLevelProfile() const =0
ProfileSummary & getSummary()
InstrProfIterator & operator++()
bool hasError()
Return true if the reader encountered an error reading profiling data.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static StringRef GetInternalKey(StringRef K)
InstrProfLookupTrait(IndexedInstrProf::HashT HashType, unsigned FormatVersion)
Error getError()
Get the current error.
static bool EqualKey(StringRef A, StringRef B)
Lightweight error class with error context and mandatory checking.
support::endianness getHostEndianness()
IndexedInstrProfReader(std::unique_ptr< MemoryBuffer > DataBuffer)
StringRef - Represent a constant reference to a string, i.e.
Reader for the indexed binary instrprof format.
void setValueProfDataEndianness(support::endianness Endianness) override
static StringRef GetExternalKey(StringRef K)
InstrProfIterator()=default
Provides lookup and iteration over an on disk hash table.
bool isIRLevelProfile() const override
Error success()
Clear the current error and return a successful one.