X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=include%2Fllvm%2FProfileData%2FInstrProfReader.h;h=304606de67914879e45ff8bef277659457e50e62;hp=d0f5a57b1ae80e89c2100fd05e7d1e809c5628d3;hb=6f139ce20bc911456a8edcaabd408e09bd41c806;hpb=69c9ea3b396aa3b5dbfe1dea823bab81f7a704b9 diff --git a/include/llvm/ProfileData/InstrProfReader.h b/include/llvm/ProfileData/InstrProfReader.h index d0f5a57b1ae..304606de679 100644 --- a/include/llvm/ProfileData/InstrProfReader.h +++ b/include/llvm/ProfileData/InstrProfReader.h @@ -23,6 +23,7 @@ #include "llvm/Support/LineIterator.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/OnDiskHashTable.h" +#include "llvm/Support/raw_ostream.h" #include namespace llvm { @@ -111,6 +112,9 @@ public: TextInstrProfReader(std::unique_ptr DataBuffer_) : DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, true, '#') {} + /// Return true if the given buffer is in text instrprof format. + static bool hasFormat(const MemoryBuffer &Buffer); + /// Read the header. std::error_code readHeader() override { return success(); } /// Read a single record. @@ -129,7 +133,6 @@ class RawInstrProfReader : public InstrProfReader { private: /// The profile data file contents. std::unique_ptr DataBuffer; - bool ShouldSwapBytes; uint64_t CountersDelta; uint64_t NamesDelta; @@ -137,7 +140,14 @@ private: const RawInstrProf::ProfileData *DataEnd; const uint64_t *CountersStart; const char *NamesStart; + const uint8_t *ValueDataStart; const char *ProfileEnd; + uint32_t ValueKindLast; + uint32_t CurValueDataSize; + + // String table for holding a unique copy of all the strings in the profile. + InstrProfStringTable StringTable; + InstrProfRecord::ValueMapType FunctionPtrToNameMap; RawInstrProfReader(const RawInstrProfReader &) = delete; RawInstrProfReader &operator=(const RawInstrProfReader &) = delete; @@ -152,16 +162,31 @@ public: private: std::error_code readNextHeader(const char *CurrentPos); std::error_code readHeader(const RawInstrProf::Header &Header); - template - IntT swap(IntT Int) const { + template IntT swap(IntT Int) const { return ShouldSwapBytes ? sys::getSwappedBytes(Int) : Int; } + support::endianness getDataEndianness() const { + support::endianness HostEndian = getHostEndianness(); + if (!ShouldSwapBytes) + return HostEndian; + if (HostEndian == support::little) + return support::big; + else + return support::little; + } + inline uint8_t getNumPaddingBytes(uint64_t SizeInBytes) { + return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t)); + } std::error_code readName(InstrProfRecord &Record); std::error_code readFuncHash(InstrProfRecord &Record); std::error_code readRawCounts(InstrProfRecord &Record); + std::error_code readValueProfilingData(InstrProfRecord &Record); bool atEnd() const { return Data == DataEnd; } - void advanceData() { Data++; } + void advanceData() { + Data++; + ValueDataStart += CurValueDataSize; + } const uint64_t *getCounter(IntPtrT CounterPtr) const { ptrdiff_t Offset = (swap(CounterPtr) - CountersDelta) / sizeof(uint64_t); @@ -225,7 +250,7 @@ public: return StringRef((const char *)D, N); } - bool ReadValueProfilingData(const unsigned char *&D, + bool readValueProfilingData(const unsigned char *&D, const unsigned char *const End); data_type ReadData(StringRef K, const unsigned char *D, offset_type N); @@ -235,36 +260,52 @@ public: } }; -class InstrProfReaderIndex { - private: - typedef OnDiskIterableChainedHashTable IndexType; +struct InstrProfReaderIndexBase { + // Read all the profile records with the same key pointed to the current + // iterator. + virtual std::error_code getRecords(ArrayRef &Data) = 0; + // Read all the profile records with the key equal to FuncName + virtual std::error_code getRecords(StringRef FuncName, + ArrayRef &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; +}; + +typedef OnDiskIterableChainedHashTable + OnDiskHashTableImplV3; - std::unique_ptr Index; - IndexType::data_iterator RecordIterator; +template +class InstrProfReaderIndex : public InstrProfReaderIndexBase { + +private: + std::unique_ptr HashTable; + typename HashTableImpl::data_iterator RecordIterator; uint64_t FormatVersion; // String table for holding a unique copy of all the strings in the profile. InstrProfStringTable StringTable; - public: - InstrProfReaderIndex() : Index(nullptr) {} - void Init(const unsigned char *Buckets, const unsigned char *const Payload, - const unsigned char *const Base, IndexedInstrProf::HashT HashType, - uint64_t Version); +public: + InstrProfReaderIndex(const unsigned char *Buckets, + const unsigned char *const Payload, + const unsigned char *const Base, + IndexedInstrProf::HashT HashType, uint64_t Version); - // Read all the pofile records with the same key pointed to the current - // iterator. - std::error_code getRecords(ArrayRef &Data); - // Read all the profile records with the key equal to FuncName + std::error_code getRecords(ArrayRef &Data) override; std::error_code getRecords(StringRef FuncName, - ArrayRef &Data); - - void advanceToNextKey() { RecordIterator++; } - bool atEnd() const { return RecordIterator == Index->data_end(); } - // Used for testing purpose only. - void setValueProfDataEndianness(support::endianness Endianness) { - Index->getInfoObj().setValueProfDataEndianness(Endianness); + ArrayRef &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 FormatVersion; } }; /// Reader for the indexed binary instrprof format. @@ -273,16 +314,17 @@ private: /// The profile data file contents. std::unique_ptr DataBuffer; /// The index into the profile data. - InstrProfReaderIndex Index; + std::unique_ptr Index; /// The maximal execution count among all functions. uint64_t MaxFunctionCount; IndexedInstrProfReader(const IndexedInstrProfReader &) = delete; IndexedInstrProfReader &operator=(const IndexedInstrProfReader &) = delete; - public: +public: + uint64_t getVersion() const { return Index->getVersion(); } IndexedInstrProfReader(std::unique_ptr DataBuffer) - : DataBuffer(std::move(DataBuffer)), Index() {} + : DataBuffer(std::move(DataBuffer)), Index(nullptr) {} /// Return true if the given buffer is in an indexed instrprof format. static bool hasFormat(const MemoryBuffer &DataBuffer); @@ -300,6 +342,7 @@ private: /// Fill Counts with the profile data for the given function name. std::error_code getFunctionCounts(StringRef FuncName, uint64_t FuncHash, std::vector &Counts); + /// Return the maximum of all known function counts. uint64_t getMaximumFunctionCount() { return MaxFunctionCount; } @@ -312,7 +355,7 @@ private: // Used for testing purpose only. void setValueProfDataEndianness(support::endianness Endianness) { - Index.setValueProfDataEndianness(Endianness); + Index->setValueProfDataEndianness(Endianness); } };