diff options
Diffstat (limited to 'include/llvm/ProfileData/InstrProfReader.h')
-rw-r--r-- | include/llvm/ProfileData/InstrProfReader.h | 88 |
1 files changed, 60 insertions, 28 deletions
diff --git a/include/llvm/ProfileData/InstrProfReader.h b/include/llvm/ProfileData/InstrProfReader.h index 65b11f61d10bc..1d85a7149afc8 100644 --- a/include/llvm/ProfileData/InstrProfReader.h +++ b/include/llvm/ProfileData/InstrProfReader.h @@ -1,4 +1,4 @@ -//=-- InstrProfReader.h - Instrumented profiling readers ----------*- C++ -*-=// +//===- InstrProfReader.h - Instrumented profiling readers -------*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -16,14 +16,23 @@ #define LLVM_PROFILEDATA_INSTRPROFREADER_H #include "llvm/ADT/ArrayRef.h" -#include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/IR/ProfileSummary.h" #include "llvm/ProfileData/InstrProf.h" -#include "llvm/Support/EndianStream.h" +#include "llvm/Support/Endian.h" +#include "llvm/Support/Error.h" #include "llvm/Support/LineIterator.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/OnDiskHashTable.h" -#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/SwapByteOrder.h" +#include <algorithm> +#include <cassert> +#include <cstddef> +#include <cstdint> #include <iterator> +#include <memory> +#include <utility> +#include <vector> namespace llvm { @@ -32,12 +41,13 @@ class InstrProfReader; /// A file format agnostic iterator over profiling data. class InstrProfIterator : public std::iterator<std::input_iterator_tag, InstrProfRecord> { - InstrProfReader *Reader; + InstrProfReader *Reader = nullptr; InstrProfRecord Record; void Increment(); + public: - InstrProfIterator() : Reader(nullptr) {} + InstrProfIterator() = default; InstrProfIterator(InstrProfReader *Reader) : Reader(Reader) { Increment(); } InstrProfIterator &operator++() { Increment(); return *this; } @@ -50,19 +60,22 @@ public: /// Base class and interface for reading profiling data of any known instrprof /// format. Provides an iterator over InstrProfRecords. class InstrProfReader { - instrprof_error LastError; + instrprof_error LastError = instrprof_error::success; public: - InstrProfReader() : LastError(instrprof_error::success), Symtab() {} - virtual ~InstrProfReader() {} + InstrProfReader() = default; + virtual ~InstrProfReader() = default; /// Read the header. Required before reading first record. virtual Error readHeader() = 0; + /// Read a single record. virtual Error readNextRecord(InstrProfRecord &Record) = 0; + /// Iterator over profile data. InstrProfIterator begin() { return InstrProfIterator(this); } InstrProfIterator end() { return InstrProfIterator(); } + virtual bool isIRLevelProfile() const = 0; /// Return the PGO symtab. There are three different readers: @@ -86,6 +99,7 @@ protected: return Error::success(); return make_error<InstrProfError>(Err); } + Error error(Error E) { return error(InstrProfError::take(std::move(E))); } /// Clear the current error and return a successful one. @@ -94,8 +108,10 @@ protected: public: /// Return true if the reader has finished reading the profile data. bool isEOF() { return LastError == instrprof_error::eof; } + /// Return true if the reader encountered an error reading profiling data. bool hasError() { return LastError != instrprof_error::success && !isEOF(); } + /// Get the current error. Error getError() { if (hasError()) @@ -125,16 +141,15 @@ private: std::unique_ptr<MemoryBuffer> DataBuffer; /// Iterator over the profile data. line_iterator Line; - bool IsIRLevelProfile; + bool IsIRLevelProfile = false; - TextInstrProfReader(const TextInstrProfReader &) = delete; - TextInstrProfReader &operator=(const TextInstrProfReader &) = delete; Error readValueProfileData(InstrProfRecord &Record); public: TextInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer_) - : DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, true, '#'), - IsIRLevelProfile(false) {} + : DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, true, '#') {} + TextInstrProfReader(const TextInstrProfReader &) = delete; + TextInstrProfReader &operator=(const TextInstrProfReader &) = delete; /// Return true if the given buffer is in text instrprof format. static bool hasFormat(const MemoryBuffer &Buffer); @@ -143,6 +158,7 @@ public: /// Read the header. Error readHeader() override; + /// Read a single record. Error readNextRecord(InstrProfRecord &Record) override; @@ -184,15 +200,16 @@ private: InstrProfRecord::ValueMapType FunctionPtrToNameMap; - RawInstrProfReader(const RawInstrProfReader &) = delete; - RawInstrProfReader &operator=(const RawInstrProfReader &) = delete; public: RawInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer) : DataBuffer(std::move(DataBuffer)) { } + RawInstrProfReader(const RawInstrProfReader &) = delete; + RawInstrProfReader &operator=(const RawInstrProfReader &) = delete; static bool hasFormat(const MemoryBuffer &DataBuffer); Error readHeader() override; Error readNextRecord(InstrProfRecord &Record) override; + bool isIRLevelProfile() const override { return (Version & VARIANT_MASK_IR_PROF) != 0; } @@ -206,9 +223,11 @@ private: Error createSymtab(InstrProfSymtab &Symtab); Error readNextHeader(const char *CurrentPos); Error readHeader(const RawInstrProf::Header &Header); + template <class IntT> IntT swap(IntT Int) const { return ShouldSwapBytes ? sys::getSwappedBytes(Int) : Int; } + support::endianness getDataEndianness() const { support::endianness HostEndian = getHostEndianness(); if (!ShouldSwapBytes) @@ -222,15 +241,18 @@ private: inline uint8_t getNumPaddingBytes(uint64_t SizeInBytes) { return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t)); } + Error readName(InstrProfRecord &Record); Error readFuncHash(InstrProfRecord &Record); Error readRawCounts(InstrProfRecord &Record); Error readValueProfilingData(InstrProfRecord &Record); bool atEnd() const { return Data == DataEnd; } + void advanceData() { Data++; ValueDataStart += CurValueDataSize; } + const char *getNextHeaderPos() const { assert(atEnd()); return (const char *)ValueDataStart; @@ -240,6 +262,7 @@ private: ptrdiff_t Offset = (swap(CounterPtr) - CountersDelta) / sizeof(uint64_t); return CountersStart + Offset; } + StringRef getName(uint64_t NameRef) const { return Symtab->getFuncName(swap(NameRef)); } @@ -249,8 +272,10 @@ typedef RawInstrProfReader<uint32_t> RawInstrProfReader32; typedef RawInstrProfReader<uint64_t> RawInstrProfReader64; namespace IndexedInstrProf { + enum class HashT : uint32_t; -} + +} // end namespace IndexedInstrProf /// Trait for lookups into the on-disk hash table for the binary instrprof /// format. @@ -261,12 +286,11 @@ class InstrProfLookupTrait { // Endianness of the input value profile data. // It should be LE by default, but can be changed // for testing purpose. - support::endianness ValueProfDataEndianness; + support::endianness ValueProfDataEndianness = support::little; public: InstrProfLookupTrait(IndexedInstrProf::HashT HashType, unsigned FormatVersion) - : HashType(HashType), FormatVersion(FormatVersion), - ValueProfDataEndianness(support::little) {} + : HashType(HashType), FormatVersion(FormatVersion) {} typedef ArrayRef<InstrProfRecord> data_type; @@ -284,6 +308,7 @@ public: static std::pair<offset_type, offset_type> ReadKeyDataLength(const unsigned char *&D) { using namespace support; + offset_type KeyLen = endian::readNext<offset_type, little, unaligned>(D); offset_type DataLen = endian::readNext<offset_type, little, unaligned>(D); return std::make_pair(KeyLen, DataLen); @@ -304,16 +329,18 @@ public: }; struct InstrProfReaderIndexBase { + virtual ~InstrProfReaderIndexBase() = default; + // Read all the profile records with the same key pointed to the current // iterator. virtual Error getRecords(ArrayRef<InstrProfRecord> &Data) = 0; + // Read all the profile records with the key equal to FuncName virtual Error getRecords(StringRef FuncName, ArrayRef<InstrProfRecord> &Data) = 0; virtual void advanceToNextKey() = 0; virtual bool atEnd() const = 0; virtual void setValueProfDataEndianness(support::endianness Endianness) = 0; - virtual ~InstrProfReaderIndexBase() {} virtual uint64_t getVersion() const = 0; virtual bool isIRLevelProfile() const = 0; virtual void populateSymtab(InstrProfSymtab &) = 0; @@ -335,22 +362,27 @@ public: const unsigned char *const Payload, const unsigned char *const Base, IndexedInstrProf::HashT HashType, uint64_t Version); + ~InstrProfReaderIndex() override = default; Error getRecords(ArrayRef<InstrProfRecord> &Data) override; Error getRecords(StringRef FuncName, ArrayRef<InstrProfRecord> &Data) override; void advanceToNextKey() override { RecordIterator++; } + bool atEnd() const override { return RecordIterator == HashTable->data_end(); } + void setValueProfDataEndianness(support::endianness Endianness) override { HashTable->getInfoObj().setValueProfDataEndianness(Endianness); } - ~InstrProfReaderIndex() override {} + uint64_t getVersion() const override { return GET_VERSION(FormatVersion); } + bool isIRLevelProfile() const override { return (FormatVersion & VARIANT_MASK_IR_PROF) != 0; } + void populateSymtab(InstrProfSymtab &Symtab) override { Symtab.create(HashTable->keys()); } @@ -366,20 +398,20 @@ private: /// Profile summary data. std::unique_ptr<ProfileSummary> Summary; - IndexedInstrProfReader(const IndexedInstrProfReader &) = delete; - IndexedInstrProfReader &operator=(const IndexedInstrProfReader &) = delete; - // Read the profile summary. Return a pointer pointing to one byte past the // end of the summary data if it exists or the input \c Cur. const unsigned char *readSummary(IndexedInstrProf::ProfVersion Version, const unsigned char *Cur); public: + IndexedInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer) + : DataBuffer(std::move(DataBuffer)) {} + IndexedInstrProfReader(const IndexedInstrProfReader &) = delete; + IndexedInstrProfReader &operator=(const IndexedInstrProfReader &) = delete; + /// Return the profile version. uint64_t getVersion() const { return Index->getVersion(); } bool isIRLevelProfile() const override { return Index->isIRLevelProfile(); } - IndexedInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer) - : DataBuffer(std::move(DataBuffer)), Index(nullptr) {} /// Return true if the given buffer is in an indexed instrprof format. static bool hasFormat(const MemoryBuffer &DataBuffer); @@ -422,4 +454,4 @@ public: } // end namespace llvm -#endif +#endif // LLVM_PROFILEDATA_INSTRPROFREADER_H |