#ifndef LLVM_PROFILEDATA_INSTRPROFREADER_H
#define LLVM_PROFILEDATA_INSTRPROFREADER_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/OnDiskHashTable.h"
#include "llvm/Support/raw_ostream.h"
#include <iterator>
namespace llvm {
class InstrProfReader;
class InstrProfIterator : public std::iterator<std::input_iterator_tag,
InstrProfRecord> {
InstrProfReader *Reader;
InstrProfRecord Record;
void Increment();
public:
InstrProfIterator() : Reader(nullptr) {}
InstrProfIterator(InstrProfReader *Reader) : Reader(Reader) { Increment(); }
InstrProfIterator &operator++() { Increment(); return *this; }
bool operator==(const InstrProfIterator &RHS) { return Reader == RHS.Reader; }
bool operator!=(const InstrProfIterator &RHS) { return Reader != RHS.Reader; }
InstrProfRecord &operator*() { return Record; }
InstrProfRecord *operator->() { return &Record; }
};
class InstrProfReader {
std::error_code LastError;
public:
InstrProfReader() : LastError(instrprof_error::success), Symtab() {}
virtual ~InstrProfReader() {}
virtual std::error_code readHeader() = 0;
virtual std::error_code readNextRecord(InstrProfRecord &Record) = 0;
InstrProfIterator begin() { return InstrProfIterator(this); }
InstrProfIterator end() { return InstrProfIterator(); }
virtual bool isIRLevelProfile() const = 0;
virtual InstrProfSymtab &getSymtab() = 0;
protected:
std::unique_ptr<InstrProfSymtab> Symtab;
std::error_code error(std::error_code EC) {
LastError = EC;
return EC;
}
std::error_code success() { return error(instrprof_error::success); }
public:
bool isEOF() { return LastError == instrprof_error::eof; }
bool hasError() { return LastError && !isEOF(); }
std::error_code getError() { return LastError; }
static ErrorOr<std::unique_ptr<InstrProfReader>> create(std::string Path);
static ErrorOr<std::unique_ptr<InstrProfReader>>
create(std::unique_ptr<MemoryBuffer> Buffer);
};
class TextInstrProfReader : public InstrProfReader {
private:
std::unique_ptr<MemoryBuffer> DataBuffer;
line_iterator Line;
bool IsIRLevelProfile;
TextInstrProfReader(const TextInstrProfReader &) = delete;
TextInstrProfReader &operator=(const TextInstrProfReader &) = delete;
std::error_code readValueProfileData(InstrProfRecord &Record);
public:
TextInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer_)
: DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, true, '#'),
IsIRLevelProfile(false) {}
static bool hasFormat(const MemoryBuffer &Buffer);
bool isIRLevelProfile() const override { return IsIRLevelProfile; }
std::error_code readHeader() override;
std::error_code readNextRecord(InstrProfRecord &Record) override;
InstrProfSymtab &getSymtab() override {
assert(Symtab.get());
return *Symtab.get();
}
};
template <class IntPtrT>
class RawInstrProfReader : public InstrProfReader {
private:
std::unique_ptr<MemoryBuffer> DataBuffer;
bool ShouldSwapBytes;
uint64_t Version;
uint64_t CountersDelta;
uint64_t NamesDelta;
const RawInstrProf::ProfileData<IntPtrT> *Data;
const RawInstrProf::ProfileData<IntPtrT> *DataEnd;
const uint64_t *CountersStart;
const char *NamesStart;
uint64_t NamesSize;
const uint8_t *ValueDataStart;
const char *ProfileEnd;
uint32_t ValueKindLast;
uint32_t CurValueDataSize;
InstrProfRecord::ValueMapType FunctionPtrToNameMap;
RawInstrProfReader(const RawInstrProfReader &) = delete;
RawInstrProfReader &operator=(const RawInstrProfReader &) = delete;
public:
RawInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
: DataBuffer(std::move(DataBuffer)) { }
static bool hasFormat(const MemoryBuffer &DataBuffer);
std::error_code readHeader() override;
std::error_code readNextRecord(InstrProfRecord &Record) override;
bool isIRLevelProfile() const override {
return (Version & VARIANT_MASK_IR_PROF) != 0;
}
InstrProfSymtab &getSymtab() override {
assert(Symtab.get());
return *Symtab.get();
}
private:
std::error_code createSymtab(InstrProfSymtab &Symtab);
std::error_code readNextHeader(const char *CurrentPos);
std::error_code 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)
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++;
ValueDataStart += CurValueDataSize;
}
const uint64_t *getCounter(IntPtrT CounterPtr) const {
ptrdiff_t Offset = (swap(CounterPtr) - CountersDelta) / sizeof(uint64_t);
return CountersStart + Offset;
}
StringRef getName(uint64_t NameRef) const {
return Symtab->getFuncName(swap(NameRef));
}
};
typedef RawInstrProfReader<uint32_t> RawInstrProfReader32;
typedef RawInstrProfReader<uint64_t> RawInstrProfReader64;
namespace IndexedInstrProf {
enum class HashT : uint32_t;
}
class InstrProfLookupTrait {
std::vector<InstrProfRecord> DataBuffer;
IndexedInstrProf::HashT HashType;
unsigned FormatVersion;
support::endianness ValueProfDataEndianness;
public:
InstrProfLookupTrait(IndexedInstrProf::HashT HashType, unsigned FormatVersion)
: HashType(HashType), FormatVersion(FormatVersion),
ValueProfDataEndianness(support::little) {}
typedef ArrayRef<InstrProfRecord> data_type;
typedef StringRef internal_key_type;
typedef StringRef external_key_type;
typedef uint64_t hash_value_type;
typedef uint64_t offset_type;
static bool EqualKey(StringRef A, StringRef B) { return A == B; }
static StringRef GetInternalKey(StringRef K) { return K; }
static StringRef GetExternalKey(StringRef K) { return K; }
hash_value_type ComputeHash(StringRef K);
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);
}
StringRef ReadKey(const unsigned char *D, offset_type N) {
return StringRef((const char *)D, N);
}
bool readValueProfilingData(const unsigned char *&D,
const unsigned char *const End);
data_type ReadData(StringRef K, const unsigned char *D, offset_type N);
void setValueProfDataEndianness(support::endianness Endianness) {
ValueProfDataEndianness = Endianness;
}
};
struct InstrProfReaderIndexBase {
virtual std::error_code getRecords(ArrayRef<InstrProfRecord> &Data) = 0;
virtual std::error_code 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;
};
typedef OnDiskIterableChainedHashTable<InstrProfLookupTrait>
OnDiskHashTableImplV3;
template <typename HashTableImpl>
class InstrProfReaderIndex : public InstrProfReaderIndexBase {
private:
std::unique_ptr<HashTableImpl> HashTable;
typename HashTableImpl::data_iterator RecordIterator;
uint64_t FormatVersion;
public:
InstrProfReaderIndex(const unsigned char *Buckets,
const unsigned char *const Payload,
const unsigned char *const Base,
IndexedInstrProf::HashT HashType, uint64_t Version);
std::error_code getRecords(ArrayRef<InstrProfRecord> &Data) override;
std::error_code 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());
}
};
class IndexedInstrProfReader : public InstrProfReader {
private:
std::unique_ptr<MemoryBuffer> DataBuffer;
std::unique_ptr<InstrProfReaderIndexBase> Index;
std::unique_ptr<ProfileSummary> Summary;
IndexedInstrProfReader(const IndexedInstrProfReader &) = delete;
IndexedInstrProfReader &operator=(const IndexedInstrProfReader &) = delete;
const unsigned char *readSummary(IndexedInstrProf::ProfVersion Version,
const unsigned char *Cur);
public:
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) {}
static bool hasFormat(const MemoryBuffer &DataBuffer);
std::error_code readHeader() override;
std::error_code readNextRecord(InstrProfRecord &Record) override;
ErrorOr<InstrProfRecord> getInstrProfRecord(StringRef FuncName,
uint64_t FuncHash);
std::error_code getFunctionCounts(StringRef FuncName, uint64_t FuncHash,
std::vector<uint64_t> &Counts);
uint64_t getMaximumFunctionCount() { return Summary->getMaxFunctionCount(); }
static ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
create(std::string Path);
static ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
create(std::unique_ptr<MemoryBuffer> Buffer);
void setValueProfDataEndianness(support::endianness Endianness) {
Index->setValueProfDataEndianness(Endianness);
}
InstrProfSymtab &getSymtab() override;
ProfileSummary &getSummary() { return *(Summary.get()); }
};
}
#endif