[PGO] Add support for reading multiple versions of indexed profile format profile data
Profile readers using incompatible on-disk hash table format can now share the same implementation and interfaces. Differential Revision: http://reviews.llvm.org/D15100 llvm-svn: 254458
This commit is contained in:
parent
edf811d68f
commit
a28306db0c
|
@ -259,36 +259,50 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class InstrProfReaderIndex {
|
struct InstrProfReaderIndexBase {
|
||||||
private:
|
// Read all the profile records with the same key pointed to the current
|
||||||
typedef OnDiskIterableChainedHashTable<InstrProfLookupTrait> IndexType;
|
// iterator.
|
||||||
|
virtual std::error_code getRecords(ArrayRef<InstrProfRecord> &Data) = 0;
|
||||||
|
// Read all the profile records with the key equal to FuncName
|
||||||
|
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() {}
|
||||||
|
};
|
||||||
|
|
||||||
std::unique_ptr<IndexType> Index;
|
typedef OnDiskIterableChainedHashTable<InstrProfLookupTrait>
|
||||||
IndexType::data_iterator RecordIterator;
|
OnDiskHashTableImplV3;
|
||||||
|
|
||||||
|
template <typename HashTableImpl>
|
||||||
|
class InstrProfReaderIndex : public InstrProfReaderIndexBase {
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unique_ptr<HashTableImpl> HashTable;
|
||||||
|
typename HashTableImpl::data_iterator RecordIterator;
|
||||||
uint64_t FormatVersion;
|
uint64_t FormatVersion;
|
||||||
|
|
||||||
// String table for holding a unique copy of all the strings in the profile.
|
// String table for holding a unique copy of all the strings in the profile.
|
||||||
InstrProfStringTable StringTable;
|
InstrProfStringTable StringTable;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
InstrProfReaderIndex() : Index(nullptr) {}
|
InstrProfReaderIndex(const unsigned char *Buckets,
|
||||||
void Init(const unsigned char *Buckets, const unsigned char *const Payload,
|
const unsigned char *const Payload,
|
||||||
const unsigned char *const Base, IndexedInstrProf::HashT HashType,
|
const unsigned char *const Base,
|
||||||
uint64_t Version);
|
IndexedInstrProf::HashT HashType, uint64_t Version);
|
||||||
|
|
||||||
// Read all the pofile records with the same key pointed to the current
|
std::error_code getRecords(ArrayRef<InstrProfRecord> &Data) override;
|
||||||
// iterator.
|
|
||||||
std::error_code getRecords(ArrayRef<InstrProfRecord> &Data);
|
|
||||||
// Read all the profile records with the key equal to FuncName
|
|
||||||
std::error_code getRecords(StringRef FuncName,
|
std::error_code getRecords(StringRef FuncName,
|
||||||
ArrayRef<InstrProfRecord> &Data);
|
ArrayRef<InstrProfRecord> &Data) override;
|
||||||
|
void advanceToNextKey() override { RecordIterator++; }
|
||||||
void advanceToNextKey() { RecordIterator++; }
|
bool atEnd() const override {
|
||||||
bool atEnd() const { return RecordIterator == Index->data_end(); }
|
return RecordIterator == HashTable->data_end();
|
||||||
// Used for testing purpose only.
|
|
||||||
void setValueProfDataEndianness(support::endianness Endianness) {
|
|
||||||
Index->getInfoObj().setValueProfDataEndianness(Endianness);
|
|
||||||
}
|
}
|
||||||
|
void setValueProfDataEndianness(support::endianness Endianness) override {
|
||||||
|
HashTable->getInfoObj().setValueProfDataEndianness(Endianness);
|
||||||
|
}
|
||||||
|
~InstrProfReaderIndex() override {}
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Reader for the indexed binary instrprof format.
|
/// Reader for the indexed binary instrprof format.
|
||||||
|
@ -297,16 +311,16 @@ private:
|
||||||
/// The profile data file contents.
|
/// The profile data file contents.
|
||||||
std::unique_ptr<MemoryBuffer> DataBuffer;
|
std::unique_ptr<MemoryBuffer> DataBuffer;
|
||||||
/// The index into the profile data.
|
/// The index into the profile data.
|
||||||
InstrProfReaderIndex Index;
|
std::unique_ptr<InstrProfReaderIndexBase> Index;
|
||||||
/// The maximal execution count among all functions.
|
/// The maximal execution count among all functions.
|
||||||
uint64_t MaxFunctionCount;
|
uint64_t MaxFunctionCount;
|
||||||
|
|
||||||
IndexedInstrProfReader(const IndexedInstrProfReader &) = delete;
|
IndexedInstrProfReader(const IndexedInstrProfReader &) = delete;
|
||||||
IndexedInstrProfReader &operator=(const IndexedInstrProfReader &) = delete;
|
IndexedInstrProfReader &operator=(const IndexedInstrProfReader &) = delete;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
IndexedInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
|
IndexedInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
|
||||||
: DataBuffer(std::move(DataBuffer)), Index() {}
|
: DataBuffer(std::move(DataBuffer)), Index(nullptr) {}
|
||||||
|
|
||||||
/// Return true if the given buffer is in an indexed instrprof format.
|
/// Return true if the given buffer is in an indexed instrprof format.
|
||||||
static bool hasFormat(const MemoryBuffer &DataBuffer);
|
static bool hasFormat(const MemoryBuffer &DataBuffer);
|
||||||
|
@ -337,7 +351,7 @@ private:
|
||||||
|
|
||||||
// Used for testing purpose only.
|
// Used for testing purpose only.
|
||||||
void setValueProfDataEndianness(support::endianness Endianness) {
|
void setValueProfDataEndianness(support::endianness Endianness) {
|
||||||
Index.setValueProfDataEndianness(Endianness);
|
Index->setValueProfDataEndianness(Endianness);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -263,11 +263,12 @@ template <typename Info> class OnDiskChainedHashTable {
|
||||||
Info InfoObj;
|
Info InfoObj;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
typedef Info InfoType;
|
||||||
typedef typename Info::internal_key_type internal_key_type;
|
typedef typename Info::internal_key_type internal_key_type;
|
||||||
typedef typename Info::external_key_type external_key_type;
|
typedef typename Info::external_key_type external_key_type;
|
||||||
typedef typename Info::data_type data_type;
|
typedef typename Info::data_type data_type;
|
||||||
typedef typename Info::hash_value_type hash_value_type;
|
typedef typename Info::hash_value_type hash_value_type;
|
||||||
typedef typename Info::offset_type offset_type;
|
typedef typename Info::offset_type offset_type;
|
||||||
|
|
||||||
OnDiskChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
|
OnDiskChainedHashTable(offset_type NumBuckets, offset_type NumEntries,
|
||||||
const unsigned char *Buckets,
|
const unsigned char *Buckets,
|
||||||
|
|
|
@ -441,11 +441,11 @@ data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D,
|
||||||
return DataBuffer;
|
return DataBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::error_code
|
template <typename HashTableImpl>
|
||||||
InstrProfReaderIndex::getRecords(StringRef FuncName,
|
std::error_code InstrProfReaderIndex<HashTableImpl>::getRecords(
|
||||||
ArrayRef<InstrProfRecord> &Data) {
|
StringRef FuncName, ArrayRef<InstrProfRecord> &Data) {
|
||||||
auto Iter = Index->find(FuncName);
|
auto Iter = HashTable->find(FuncName);
|
||||||
if (Iter == Index->end())
|
if (Iter == HashTable->end())
|
||||||
return instrprof_error::unknown_function;
|
return instrprof_error::unknown_function;
|
||||||
|
|
||||||
Data = (*Iter);
|
Data = (*Iter);
|
||||||
|
@ -455,9 +455,11 @@ InstrProfReaderIndex::getRecords(StringRef FuncName,
|
||||||
return instrprof_error::success;
|
return instrprof_error::success;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::error_code InstrProfReaderIndex::getRecords(
|
template <typename HashTableImpl>
|
||||||
|
std::error_code InstrProfReaderIndex<HashTableImpl>::getRecords(
|
||||||
ArrayRef<InstrProfRecord> &Data) {
|
ArrayRef<InstrProfRecord> &Data) {
|
||||||
if (atEnd()) return instrprof_error::eof;
|
if (atEnd())
|
||||||
|
return instrprof_error::eof;
|
||||||
|
|
||||||
Data = *RecordIterator;
|
Data = *RecordIterator;
|
||||||
|
|
||||||
|
@ -466,25 +468,26 @@ std::error_code InstrProfReaderIndex::getRecords(
|
||||||
return instrprof_error::success;
|
return instrprof_error::success;
|
||||||
}
|
}
|
||||||
|
|
||||||
void InstrProfReaderIndex::Init(const unsigned char *Buckets,
|
template <typename HashTableImpl>
|
||||||
const unsigned char *const Payload,
|
InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex(
|
||||||
const unsigned char *const Base,
|
const unsigned char *Buckets, const unsigned char *const Payload,
|
||||||
IndexedInstrProf::HashT HashType,
|
const unsigned char *const Base, IndexedInstrProf::HashT HashType,
|
||||||
uint64_t Version) {
|
uint64_t Version) {
|
||||||
FormatVersion = Version;
|
FormatVersion = Version;
|
||||||
Index.reset(IndexType::Create(Buckets, Payload, Base,
|
HashTable.reset(HashTableImpl::Create(
|
||||||
InstrProfLookupTrait(HashType, Version)));
|
Buckets, Payload, Base,
|
||||||
|
typename HashTableImpl::InfoType(HashType, Version)));
|
||||||
// Form the map of hash values to const char* keys in profiling data.
|
// Form the map of hash values to const char* keys in profiling data.
|
||||||
std::vector<std::pair<uint64_t, const char *>> HashKeys;
|
std::vector<std::pair<uint64_t, const char *>> HashKeys;
|
||||||
for (auto Key : Index->keys()) {
|
for (auto Key : HashTable->keys()) {
|
||||||
const char *KeyTableRef = StringTable.insertString(Key);
|
const char *KeyTableRef = StringTable.insertString(Key);
|
||||||
HashKeys.push_back(std::make_pair(ComputeHash(HashType, Key), KeyTableRef));
|
HashKeys.push_back(std::make_pair(ComputeHash(HashType, Key), KeyTableRef));
|
||||||
}
|
}
|
||||||
std::sort(HashKeys.begin(), HashKeys.end(), less_first());
|
std::sort(HashKeys.begin(), HashKeys.end(), less_first());
|
||||||
HashKeys.erase(std::unique(HashKeys.begin(), HashKeys.end()), HashKeys.end());
|
HashKeys.erase(std::unique(HashKeys.begin(), HashKeys.end()), HashKeys.end());
|
||||||
// Set the hash key map for the InstrLookupTrait
|
// Set the hash key map for the InstrLookupTrait
|
||||||
Index->getInfoObj().setHashKeys(std::move(HashKeys));
|
HashTable->getInfoObj().setHashKeys(std::move(HashKeys));
|
||||||
RecordIterator = Index->data_begin();
|
RecordIterator = HashTable->data_begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
|
bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
|
||||||
|
@ -532,8 +535,10 @@ std::error_code IndexedInstrProfReader::readHeader() {
|
||||||
uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
|
uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
|
||||||
|
|
||||||
// The rest of the file is an on disk hash table.
|
// The rest of the file is an on disk hash table.
|
||||||
Index.Init(Start + HashOffset, Cur, Start, HashType, FormatVersion);
|
InstrProfReaderIndexBase *IndexPtr = nullptr;
|
||||||
|
IndexPtr = new InstrProfReaderIndex<OnDiskHashTableImplV3>(
|
||||||
|
Start + HashOffset, Cur, Start, HashType, FormatVersion);
|
||||||
|
Index.reset(IndexPtr);
|
||||||
return success();
|
return success();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -541,7 +546,7 @@ ErrorOr<InstrProfRecord>
|
||||||
IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
|
IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
|
||||||
uint64_t FuncHash) {
|
uint64_t FuncHash) {
|
||||||
ArrayRef<InstrProfRecord> Data;
|
ArrayRef<InstrProfRecord> Data;
|
||||||
std::error_code EC = Index.getRecords(FuncName, Data);
|
std::error_code EC = Index->getRecords(FuncName, Data);
|
||||||
if (EC != instrprof_error::success)
|
if (EC != instrprof_error::success)
|
||||||
return EC;
|
return EC;
|
||||||
// Found it. Look for counters with the right hash.
|
// Found it. Look for counters with the right hash.
|
||||||
|
@ -571,13 +576,13 @@ std::error_code IndexedInstrProfReader::readNextRecord(
|
||||||
|
|
||||||
ArrayRef<InstrProfRecord> Data;
|
ArrayRef<InstrProfRecord> Data;
|
||||||
|
|
||||||
std::error_code EC = Index.getRecords(Data);
|
std::error_code EC = Index->getRecords(Data);
|
||||||
if (EC != instrprof_error::success)
|
if (EC != instrprof_error::success)
|
||||||
return error(EC);
|
return error(EC);
|
||||||
|
|
||||||
Record = Data[RecordIndex++];
|
Record = Data[RecordIndex++];
|
||||||
if (RecordIndex >= Data.size()) {
|
if (RecordIndex >= Data.size()) {
|
||||||
Index.advanceToNextKey();
|
Index->advanceToNextKey();
|
||||||
RecordIndex = 0;
|
RecordIndex = 0;
|
||||||
}
|
}
|
||||||
return success();
|
return success();
|
||||||
|
|
Loading…
Reference in New Issue