}
template <class IntPtrT>
-std::error_code RawInstrProfReader<IntPtrT>::readValueProfilingData(
- InstrProfRecord &Record) {
+std::error_code
+RawInstrProfReader<IntPtrT>::readValueProfilingData(InstrProfRecord &Record) {
Record.clearValueData();
if (!Data->Values || (ValueDataDelta == 0))
return success();
- // Read value data.
- uint64_t NumVSites = 0;
- for (uint32_t Kind = IPVK_First; Kind <= ValueKindLast; ++Kind)
- NumVSites += swap(Data->NumValueSites[Kind]);
- NumVSites += getNumPaddingBytes(NumVSites);
+ ErrorOr<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
+ ValueProfData::getValueProfData(getValueDataCounts(Data->Values),
+ (const unsigned char *)ProfileEnd,
+ getDataEndianness());
- auto VDataCounts = makeArrayRef(getValueDataCounts(Data->Values), NumVSites);
- // Check bounds.
- if (VDataCounts.data() < ValueDataStart ||
- VDataCounts.data() + VDataCounts.size() >
- reinterpret_cast<const uint8_t *>(ProfileEnd))
- return error(instrprof_error::malformed);
+ if (VDataPtrOrErr.getError())
+ return VDataPtrOrErr.getError();
- const InstrProfValueData *VDataPtr =
- getValueData(swap(Data->Values) + NumVSites);
- for (uint32_t Kind = IPVK_First; Kind <= ValueKindLast; ++Kind) {
- NumVSites = swap(Data->NumValueSites[Kind]);
- Record.reserveSites(Kind, NumVSites);
- for (uint32_t VSite = 0; VSite < NumVSites; ++VSite) {
-
- uint32_t VDataCount = VDataCounts[VSite];
- if ((const char *)(VDataPtr + VDataCount) > ProfileEnd)
- return error(instrprof_error::malformed);
-
- std::vector<InstrProfValueData> CurrentValues;
- CurrentValues.reserve(VDataCount);
- for (uint32_t VIndex = 0; VIndex < VDataCount; ++VIndex) {
- uint64_t TargetValue = swap(VDataPtr->Value);
- uint64_t Count = swap(VDataPtr->Count);
- CurrentValues.push_back({TargetValue, Count});
- ++VDataPtr;
- }
- Record.addValueData(Kind, VSite, CurrentValues.data(),
- VDataCount, &FunctionPtrToNameMap);
- }
- }
+ VDataPtrOrErr.get()->deserializeTo(Record, &FunctionPtrToNameMap);
return success();
}
template <class IntPtrT>
-std::error_code RawInstrProfReader<IntPtrT>::readNextRecord(
- InstrProfRecord &Record) {
+std::error_code
+RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) {
if (atEnd())
if (std::error_code EC = readNextHeader(ProfileEnd))
return EC;
return EC;
// Read value data and set Record.
- if (std::error_code EC = readValueProfilingData(Record)) return EC;
+ if (std::error_code EC = readValueProfilingData(Record))
+ return EC;
// Iterate.
advanceData();
return DataBuffer;
}
-std::error_code
-InstrProfReaderIndex::getRecords(StringRef FuncName,
- ArrayRef<InstrProfRecord> &Data) {
- auto Iter = Index->find(FuncName);
- if (Iter == Index->end())
+template <typename HashTableImpl>
+std::error_code InstrProfReaderIndex<HashTableImpl>::getRecords(
+ StringRef FuncName, ArrayRef<InstrProfRecord> &Data) {
+ auto Iter = HashTable->find(FuncName);
+ if (Iter == HashTable->end())
return instrprof_error::unknown_function;
Data = (*Iter);
return instrprof_error::success;
}
-std::error_code InstrProfReaderIndex::getRecords(
+template <typename HashTableImpl>
+std::error_code InstrProfReaderIndex<HashTableImpl>::getRecords(
ArrayRef<InstrProfRecord> &Data) {
- if (atEnd()) return instrprof_error::eof;
+ if (atEnd())
+ return instrprof_error::eof;
Data = *RecordIterator;
- if (Data.empty()) return instrprof_error::malformed;
+ if (Data.empty())
+ return instrprof_error::malformed;
return instrprof_error::success;
}
-void InstrProfReaderIndex::Init(const unsigned char *Buckets,
- const unsigned char *const Payload,
- const unsigned char *const Base,
- IndexedInstrProf::HashT HashType,
- uint64_t Version) {
+template <typename HashTableImpl>
+InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex(
+ const unsigned char *Buckets, const unsigned char *const Payload,
+ const unsigned char *const Base, IndexedInstrProf::HashT HashType,
+ uint64_t Version) {
FormatVersion = Version;
- Index.reset(IndexType::Create(Buckets, Payload, Base,
- InstrProfLookupTrait(HashType, Version)));
+ HashTable.reset(HashTableImpl::Create(
+ Buckets, Payload, Base,
+ typename HashTableImpl::InfoType(HashType, Version)));
// Form the map of hash values to const char* keys in profiling data.
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);
HashKeys.push_back(std::make_pair(ComputeHash(HashType, Key), KeyTableRef));
}
std::sort(HashKeys.begin(), HashKeys.end(), less_first());
HashKeys.erase(std::unique(HashKeys.begin(), HashKeys.end()), HashKeys.end());
// Set the hash key map for the InstrLookupTrait
- Index->getInfoObj().setHashKeys(std::move(HashKeys));
- RecordIterator = Index->data_begin();
+ HashTable->getInfoObj().setHashKeys(std::move(HashKeys));
+ RecordIterator = HashTable->data_begin();
}
bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
// 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();
}
IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
uint64_t FuncHash) {
ArrayRef<InstrProfRecord> Data;
- std::error_code EC = Index.getRecords(FuncName, Data);
+ std::error_code EC = Index->getRecords(FuncName, Data);
if (EC != instrprof_error::success)
return EC;
// Found it. Look for counters with the right hash.
ArrayRef<InstrProfRecord> Data;
- std::error_code EC = Index.getRecords(Data);
+ std::error_code EC = Index->getRecords(Data);
if (EC != instrprof_error::success)
return error(EC);
Record = Data[RecordIndex++];
if (RecordIndex >= Data.size()) {
- Index.advanceToNextKey();
+ Index->advanceToNextKey();
RecordIndex = 0;
}
return success();