X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=lib%2FProfileData%2FInstrProfReader.cpp;h=10b8fa781d7369d12a7c78747809bc732c0c4501;hp=479b6bc61d95e706b1489b16bec22d829eab6a3f;hb=849df7927ee243d1bdce23cf5d5b91e0c465f18b;hpb=e34401de05668522e24e0faf54dc62d7ebf2f8fc diff --git a/lib/ProfileData/InstrProfReader.cpp b/lib/ProfileData/InstrProfReader.cpp index 479b6bc61d9..10b8fa781d7 100644 --- a/lib/ProfileData/InstrProfReader.cpp +++ b/lib/ProfileData/InstrProfReader.cpp @@ -206,15 +206,21 @@ std::error_code RawInstrProfReader::readHeader( CountersDelta = swap(Header.CountersDelta); NamesDelta = swap(Header.NamesDelta); + ValueDataDelta = swap(Header.ValueDataDelta); auto DataSize = swap(Header.DataSize); auto CountersSize = swap(Header.CountersSize); auto NamesSize = swap(Header.NamesSize); + auto ValueDataSize = swap(Header.ValueDataSize); + ValueKindLast = swap(Header.ValueKindLast); + + auto DataSizeInBytes = DataSize * sizeof(RawInstrProf::ProfileData); + auto PaddingSize = getNumPaddingBytes(NamesSize); ptrdiff_t DataOffset = sizeof(RawInstrProf::Header); - ptrdiff_t CountersOffset = - DataOffset + sizeof(RawInstrProf::ProfileData) * DataSize; + ptrdiff_t CountersOffset = DataOffset + DataSizeInBytes; ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize; - size_t ProfileSize = NamesOffset + sizeof(char) * NamesSize; + ptrdiff_t ValueDataOffset = NamesOffset + NamesSize + PaddingSize; + size_t ProfileSize = ValueDataOffset + ValueDataSize; auto *Start = reinterpret_cast(&Header); if (Start + ProfileSize > DataBuffer->getBufferEnd()) @@ -225,8 +231,23 @@ std::error_code RawInstrProfReader::readHeader( DataEnd = Data + DataSize; CountersStart = reinterpret_cast(Start + CountersOffset); NamesStart = Start + NamesOffset; + ValueDataStart = reinterpret_cast(Start + ValueDataOffset); ProfileEnd = Start + ProfileSize; + FunctionPtrToNameMap.clear(); + for (const RawInstrProf::ProfileData *I = Data; I != DataEnd; ++I) { + const IntPtrT FPtr = swap(I->FunctionPointer); + if (!FPtr) + continue; + StringRef FunctionName(getName(I->NamePtr), swap(I->NameSize)); + const char* NameEntryPtr = StringTable.insertString(FunctionName); + FunctionPtrToNameMap.push_back(std::pair + (FPtr, NameEntryPtr)); + } + std::sort(FunctionPtrToNameMap.begin(), FunctionPtrToNameMap.end(), less_first()); + FunctionPtrToNameMap.erase(std::unique(FunctionPtrToNameMap.begin(), + FunctionPtrToNameMap.end()), + FunctionPtrToNameMap.end()); return success(); } @@ -234,9 +255,9 @@ template std::error_code RawInstrProfReader::readName(InstrProfRecord &Record) { Record.Name = StringRef(getName(Data->NamePtr), swap(Data->NameSize)); if (Record.Name.data() < NamesStart || - Record.Name.data() + Record.Name.size() > DataBuffer->getBufferEnd()) + Record.Name.data() + Record.Name.size() > + reinterpret_cast(ValueDataStart)) return error(instrprof_error::malformed); - return success(); } @@ -274,6 +295,26 @@ std::error_code RawInstrProfReader::readRawCounts( return success(); } +template +std::error_code +RawInstrProfReader::readValueProfilingData(InstrProfRecord &Record) { + + Record.clearValueData(); + if (!Data->Values || (ValueDataDelta == 0)) + return success(); + + ErrorOr> VDataPtrOrErr = + ValueProfData::getValueProfData(getValueDataCounts(Data->Values), + (const unsigned char *)ProfileEnd, + getDataEndianness()); + + if (VDataPtrOrErr.getError()) + return VDataPtrOrErr.getError(); + + VDataPtrOrErr.get()->deserializeTo(Record, &FunctionPtrToNameMap); + return success(); +} + template std::error_code RawInstrProfReader::readNextRecord(InstrProfRecord &Record) { @@ -293,6 +334,10 @@ RawInstrProfReader::readNextRecord(InstrProfRecord &Record) { if (std::error_code EC = readRawCounts(Record)) return EC; + // Read value data and set Record. + if (std::error_code EC = readValueProfilingData(Record)) + return EC; + // Iterate. advanceData(); return success(); @@ -311,7 +356,7 @@ InstrProfLookupTrait::ComputeHash(StringRef K) { typedef InstrProfLookupTrait::data_type data_type; typedef InstrProfLookupTrait::offset_type offset_type; -bool InstrProfLookupTrait::ReadValueProfilingData( +bool InstrProfLookupTrait::readValueProfilingData( const unsigned char *&D, const unsigned char *const End) { ErrorOr> VDataPtrOrErr = ValueProfData::getValueProfData(D, End, ValueProfDataEndianness); @@ -362,7 +407,7 @@ data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D, DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer)); // Read value profiling data. - if (FormatVersion > 2 && !ReadValueProfilingData(D, End)) { + if (FormatVersion > 2 && !readValueProfilingData(D, End)) { DataBuffer.clear(); return data_type(); } @@ -370,11 +415,11 @@ data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D, return DataBuffer; } -std::error_code -InstrProfReaderIndex::getRecords(StringRef FuncName, - ArrayRef &Data) { - auto Iter = Index->find(FuncName); - if (Iter == Index->end()) +template +std::error_code InstrProfReaderIndex::getRecords( + StringRef FuncName, ArrayRef &Data) { + auto Iter = HashTable->find(FuncName); + if (Iter == HashTable->end()) return instrprof_error::unknown_function; Data = (*Iter); @@ -384,36 +429,40 @@ InstrProfReaderIndex::getRecords(StringRef FuncName, return instrprof_error::success; } -std::error_code InstrProfReaderIndex::getRecords( +template +std::error_code InstrProfReaderIndex::getRecords( ArrayRef &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 +InstrProfReaderIndex::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> 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) { @@ -461,8 +510,10 @@ std::error_code IndexedInstrProfReader::readHeader() { uint64_t HashOffset = endian::byte_swap(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( + Start + HashOffset, Cur, Start, HashType, FormatVersion); + Index.reset(IndexPtr); return success(); } @@ -470,7 +521,7 @@ ErrorOr IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName, uint64_t FuncHash) { ArrayRef 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. @@ -500,13 +551,13 @@ std::error_code IndexedInstrProfReader::readNextRecord( ArrayRef 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();