15 #ifndef LLVM_PROFILEDATA_INSTRPROFREADER_H
16 #define LLVM_PROFILEDATA_INSTRPROFREADER_H
30 class InstrProfReader;
81 std::unique_ptr<InstrProfSymtab>
Symtab;
87 return make_error<InstrProfError>(Err);
102 return make_error<InstrProfError>(LastError);
111 create(std::unique_ptr<MemoryBuffer> Buffer);
125 std::unique_ptr<MemoryBuffer> DataBuffer;
128 bool IsIRLevelProfile;
136 : DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer,
true,
'#'),
137 IsIRLevelProfile(
false) {}
162 template <
class IntPtrT>
166 std::unique_ptr<MemoryBuffer> DataBuffer;
167 bool ShouldSwapBytes;
172 uint64_t CountersDelta;
174 const RawInstrProf::ProfileData<IntPtrT> *Data;
175 const RawInstrProf::ProfileData<IntPtrT> *DataEnd;
176 const uint64_t *CountersStart;
177 const char *NamesStart;
181 const uint8_t *ValueDataStart;
191 : DataBuffer(std::move(DataBuffer)) { }
197 return (Version & VARIANT_MASK_IR_PROF) != 0;
207 Error readNextHeader(
const char *CurrentPos);
209 template <
class IntT> IntT swap(IntT Int)
const {
214 if (!ShouldSwapBytes)
222 inline uint8_t getNumPaddingBytes(uint64_t SizeInBytes) {
223 return 7 & (
sizeof(uint64_t) - SizeInBytes %
sizeof(uint64_t));
225 Error readName(InstrProfRecord &Record);
226 Error readFuncHash(InstrProfRecord &Record);
227 Error readRawCounts(InstrProfRecord &Record);
228 Error readValueProfilingData(InstrProfRecord &Record);
229 bool atEnd()
const {
return Data == DataEnd; }
232 ValueDataStart += CurValueDataSize;
234 const char *getNextHeaderPos()
const {
236 return (
const char *)ValueDataStart;
239 const uint64_t *getCounter(IntPtrT CounterPtr)
const {
240 ptrdiff_t
Offset = (swap(CounterPtr) - CountersDelta) /
sizeof(uint64_t);
241 return CountersStart +
Offset;
243 StringRef getName(uint64_t NameRef)
const {
244 return Symtab->getFuncName(swap(NameRef));
251 namespace IndexedInstrProf {
258 std::vector<InstrProfRecord> DataBuffer;
260 unsigned FormatVersion;
268 : HashType(HashType), FormatVersion(FormatVersion),
269 ValueProfDataEndianness(support::
little) {}
284 static std::pair<offset_type, offset_type>
286 using namespace support;
287 offset_type KeyLen = endian::readNext<offset_type, little, unaligned>(
D);
288 offset_type DataLen = endian::readNext<offset_type, little, unaligned>(
D);
289 return std::make_pair(KeyLen, DataLen);
296 bool readValueProfilingData(
const unsigned char *&
D,
297 const unsigned char *
const End);
302 ValueProfDataEndianness = Endianness;
313 virtual void advanceToNextKey() = 0;
314 virtual bool atEnd()
const = 0;
317 virtual uint64_t getVersion()
const = 0;
318 virtual bool isIRLevelProfile()
const = 0;
322 typedef OnDiskIterableChainedHashTable<InstrProfLookupTrait>
325 template <
typename HashTableImpl>
329 std::unique_ptr<HashTableImpl> HashTable;
330 typename HashTableImpl::data_iterator RecordIterator;
331 uint64_t FormatVersion;
335 const unsigned char *
const Payload,
336 const unsigned char *
const Base,
344 return RecordIterator == HashTable->data_end();
347 HashTable->getInfoObj().setValueProfDataEndianness(Endianness);
350 uint64_t
getVersion()
const override {
return GET_VERSION(FormatVersion); }
352 return (FormatVersion & VARIANT_MASK_IR_PROF) != 0;
355 Symtab.
create(HashTable->keys());
363 std::unique_ptr<MemoryBuffer> DataBuffer;
365 std::unique_ptr<InstrProfReaderIndexBase> Index;
367 std::unique_ptr<ProfileSummary> Summary;
375 const unsigned char *Cur);
382 : DataBuffer(std::move(DataBuffer)), Index(nullptr) {}
388 Error readHeader()
override;
399 std::vector<uint64_t> &Counts);
406 create(
const Twine &Path);
409 create(std::unique_ptr<MemoryBuffer> Buffer);
413 Index->setValueProfDataEndianness(Endianness);
Error readNextRecord(InstrProfRecord &Record) override
Read a single record.
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.
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
OnDiskIterableChainedHashTable< InstrProfLookupTrait > OnDiskHashTableImplV3
Error readHeader() override
Read the header.
Error create(object::SectionRef &Section)
Create InstrProfSymtab from an object file section which contains function PGO names.
bool operator!=(const InstrProfIterator &RHS)
bool isEOF()
Return true if the reader has finished reading the profile data.
virtual Error readNextRecord(InstrProfRecord &Record)=0
Read a single record.
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.
static GCRegistry::Add< StatepointGC > D("statepoint-example","an example strategy for statepoint")
InstrProfLookupTrait::data_type data_type
RawInstrProfReader< uint32_t > RawInstrProfReader32
Tagged union holding either a T or a Error.
void setValueProfDataEndianness(support::endianness Endianness)
bool isIRLevelProfile() const override
InstrProfLookupTrait::offset_type offset_type
RawInstrProfReader(std::unique_ptr< MemoryBuffer > DataBuffer)
Function Alias Analysis false
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.
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
Reader for the simple text based instrprof format.
static bool hasFormat(const MemoryBuffer &DataBuffer)
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
StringRef ReadKey(const unsigned char *D, offset_type N)
void populateSymtab(InstrProfSymtab &Symtab) override
uint64_t getVersion() const override
InstrProfSymtab & getSymtab() override
Return the PGO symtab.
Base class and interface for reading profiling data of any known instrprof format.
std::unique_ptr< InstrProfSymtab > Symtab
static const unsigned End
void advanceToNextKey() override
uint64_t ComputeHash(StringRef K)
bool atEnd() const override
InstrProfRecord * operator->()
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.
virtual ~InstrProfReaderIndexBase()
This interface provides simple read-only access to a block of memory, and provides simple methods for...
unsigned char getSwappedBytes(unsigned char C)
RawInstrProfReader< uint64_t > RawInstrProfReader64
TextInstrProfReader(std::unique_ptr< MemoryBuffer > DataBuffer_)
Error readNextRecord(InstrProfRecord &Record) override
Read a single record.
virtual Error readHeader()=0
Read the header. Required before reading first record.
Profiling information for a single function.
StringRef external_key_type
ArrayRef< InstrProfRecord > data_type
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)
StringRef internal_key_type
Error getError()
Get the current error.
~InstrProfReaderIndex() override
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)
std::vector< std::pair< uint64_t, uint64_t > > ValueMapType
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)
InstrProfRecord & operator*()
virtual ~InstrProfReader()
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
bool isIRLevelProfile() const override
Error success()
Clear the current error and return a successful one.