diff options
Diffstat (limited to 'contrib/llvm/lib/ProfileData/InstrProfReader.cpp')
-rw-r--r-- | contrib/llvm/lib/ProfileData/InstrProfReader.cpp | 83 |
1 files changed, 55 insertions, 28 deletions
diff --git a/contrib/llvm/lib/ProfileData/InstrProfReader.cpp b/contrib/llvm/lib/ProfileData/InstrProfReader.cpp index ad407f0..1b39a06 100644 --- a/contrib/llvm/lib/ProfileData/InstrProfReader.cpp +++ b/contrib/llvm/lib/ProfileData/InstrProfReader.cpp @@ -1,4 +1,4 @@ -//=-- InstrProfReader.cpp - Instrumented profiling reader -------------------=// +//===- InstrProfReader.cpp - Instrumented profiling reader ----------------===// // // The LLVM Compiler Infrastructure // @@ -13,8 +13,26 @@ //===----------------------------------------------------------------------===// #include "llvm/ProfileData/InstrProfReader.h" +#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/STLExtras.h" -#include <cassert> +#include "llvm/ADT/StringRef.h" +#include "llvm/IR/ProfileSummary.h" +#include "llvm/ProfileData/InstrProf.h" +#include "llvm/ProfileData/ProfileCommon.h" +#include "llvm/Support/Endian.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/ErrorOr.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/SwapByteOrder.h" +#include <algorithm> +#include <cctype> +#include <cstddef> +#include <cstdint> +#include <limits> +#include <memory> +#include <system_error> +#include <utility> +#include <vector> using namespace llvm; @@ -78,7 +96,6 @@ IndexedInstrProfReader::create(const Twine &Path) { return IndexedInstrProfReader::create(std::move(BufferOrError.get())); } - Expected<std::unique_ptr<IndexedInstrProfReader>> IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) { // Sanity check the buffer. @@ -182,8 +199,9 @@ TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) { CHECK_LINE_END(Line); std::pair<StringRef, StringRef> VD = Line->rsplit(':'); uint64_t TakenCount, Value; - if (VK == IPVK_IndirectCallTarget) { - Symtab->addFuncName(VD.first); + if (ValueKind == IPVK_IndirectCallTarget) { + if (Error E = Symtab->addFuncName(VD.first)) + return E; Value = IndexedInstrProf::ComputeHash(VD.first); } else { READ_NUM(VD.first, Value); @@ -192,7 +210,8 @@ TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) { CurrentValues.push_back({Value, TakenCount}); Line++; } - Record.addValueData(VK, S, CurrentValues.data(), NumValueData, nullptr); + Record.addValueData(ValueKind, S, CurrentValues.data(), NumValueData, + nullptr); } } return success(); @@ -202,7 +221,7 @@ TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) { #undef VP_READ_ADVANCE } -Error TextInstrProfReader::readNextRecord(InstrProfRecord &Record) { +Error TextInstrProfReader::readNextRecord(NamedInstrProfRecord &Record) { // Skip empty lines and comments. while (!Line.is_at_end() && (Line->empty() || Line->startswith("#"))) ++Line; @@ -214,7 +233,8 @@ Error TextInstrProfReader::readNextRecord(InstrProfRecord &Record) { // Read the function name. Record.Name = *Line++; - Symtab->addFuncName(Record.Name); + if (Error E = Symtab->addFuncName(Record.Name)) + return E; // Read the function hash. if (Line.is_at_end()) @@ -232,7 +252,7 @@ Error TextInstrProfReader::readNextRecord(InstrProfRecord &Record) { return error(instrprof_error::malformed); // Read each counter and fill our internal storage with the values. - Record.Counts.clear(); + Record.Clear(); Record.Counts.reserve(NumCounters); for (uint64_t I = 0; I < NumCounters; ++I) { if (Line.is_at_end()) @@ -357,13 +377,13 @@ Error RawInstrProfReader<IntPtrT>::readHeader( } template <class IntPtrT> -Error RawInstrProfReader<IntPtrT>::readName(InstrProfRecord &Record) { +Error RawInstrProfReader<IntPtrT>::readName(NamedInstrProfRecord &Record) { Record.Name = getName(Data->NameRef); return success(); } template <class IntPtrT> -Error RawInstrProfReader<IntPtrT>::readFuncHash(InstrProfRecord &Record) { +Error RawInstrProfReader<IntPtrT>::readFuncHash(NamedInstrProfRecord &Record) { Record.Hash = swap(Data->FuncHash); return success(); } @@ -398,7 +418,6 @@ Error RawInstrProfReader<IntPtrT>::readRawCounts( template <class IntPtrT> Error RawInstrProfReader<IntPtrT>::readValueProfilingData( InstrProfRecord &Record) { - Record.clearValueData(); CurValueDataSize = 0; // Need to match the logic in value profile dumper code in compiler-rt: @@ -426,7 +445,7 @@ Error RawInstrProfReader<IntPtrT>::readValueProfilingData( } template <class IntPtrT> -Error RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) { +Error RawInstrProfReader<IntPtrT>::readNextRecord(NamedInstrProfRecord &Record) { if (atEnd()) // At this point, ValueDataStart field points to the next header. if (Error E = readNextHeader(getNextHeaderPos())) @@ -454,17 +473,19 @@ Error RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) { } namespace llvm { + template class RawInstrProfReader<uint32_t>; template class RawInstrProfReader<uint64_t>; -} + +} // end namespace llvm InstrProfLookupTrait::hash_value_type InstrProfLookupTrait::ComputeHash(StringRef K) { return IndexedInstrProf::ComputeHash(HashType, K); } -typedef InstrProfLookupTrait::data_type data_type; -typedef InstrProfLookupTrait::offset_type offset_type; +using data_type = InstrProfLookupTrait::data_type; +using offset_type = InstrProfLookupTrait::offset_type; bool InstrProfLookupTrait::readValueProfilingData( const unsigned char *&D, const unsigned char *const End) { @@ -482,6 +503,8 @@ bool InstrProfLookupTrait::readValueProfilingData( data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D, offset_type N) { + using namespace support; + // Check if the data is corrupt. If so, don't try to read it. if (N % sizeof(uint64_t)) return data_type(); @@ -489,7 +512,6 @@ data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D, DataBuffer.clear(); std::vector<uint64_t> CounterBuffer; - using namespace support; const unsigned char *End = D + N; while (D < End) { // Read hash. @@ -528,7 +550,7 @@ data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D, template <typename HashTableImpl> Error InstrProfReaderIndex<HashTableImpl>::getRecords( - StringRef FuncName, ArrayRef<InstrProfRecord> &Data) { + StringRef FuncName, ArrayRef<NamedInstrProfRecord> &Data) { auto Iter = HashTable->find(FuncName); if (Iter == HashTable->end()) return make_error<InstrProfError>(instrprof_error::unknown_function); @@ -542,7 +564,7 @@ Error InstrProfReaderIndex<HashTableImpl>::getRecords( template <typename HashTableImpl> Error InstrProfReaderIndex<HashTableImpl>::getRecords( - ArrayRef<InstrProfRecord> &Data) { + ArrayRef<NamedInstrProfRecord> &Data) { if (atEnd()) return make_error<InstrProfError>(instrprof_error::eof); @@ -567,9 +589,10 @@ InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex( } bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) { + using namespace support; + if (DataBuffer.getBufferSize() < 8) return false; - using namespace support; uint64_t Magic = endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart()); // Verify that it's magical. @@ -581,6 +604,7 @@ IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version, const unsigned char *Cur) { using namespace IndexedInstrProf; using namespace support; + if (Version >= IndexedInstrProf::Version4) { const IndexedInstrProf::Summary *SummaryInLE = reinterpret_cast<const IndexedInstrProf::Summary *>(Cur); @@ -598,7 +622,7 @@ IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version, for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++) Dst[I] = endian::byte_swap<uint64_t, little>(Src[I]); - llvm::SummaryEntryVector DetailedSummary; + SummaryEntryVector DetailedSummary; for (unsigned I = 0; I < SummaryData->NumCutoffEntries; I++) { const IndexedInstrProf::Summary::Entry &Ent = SummaryData->getEntry(I); DetailedSummary.emplace_back((uint32_t)Ent.Cutoff, Ent.MinBlockCount, @@ -617,23 +641,24 @@ IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version, } else { // For older version of profile data, we need to compute on the fly: using namespace IndexedInstrProf; + InstrProfSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs); // FIXME: This only computes an empty summary. Need to call addRecord for - // all InstrProfRecords to get the correct summary. + // all NamedInstrProfRecords to get the correct summary. this->Summary = Builder.getSummary(); return Cur; } } Error IndexedInstrProfReader::readHeader() { + using namespace support; + const unsigned char *Start = (const unsigned char *)DataBuffer->getBufferStart(); const unsigned char *Cur = Start; if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24) return error(instrprof_error::truncated); - using namespace support; - auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur); Cur += sizeof(IndexedInstrProf::Header); @@ -671,7 +696,9 @@ InstrProfSymtab &IndexedInstrProfReader::getSymtab() { return *Symtab.get(); std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>(); - Index->populateSymtab(*NewSymtab.get()); + if (Error E = Index->populateSymtab(*NewSymtab.get())) { + consumeError(error(InstrProfError::take(std::move(E)))); + } Symtab = std::move(NewSymtab); return *Symtab.get(); @@ -680,7 +707,7 @@ InstrProfSymtab &IndexedInstrProfReader::getSymtab() { Expected<InstrProfRecord> IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName, uint64_t FuncHash) { - ArrayRef<InstrProfRecord> Data; + ArrayRef<NamedInstrProfRecord> Data; Error Err = Index->getRecords(FuncName, Data); if (Err) return std::move(Err); @@ -705,10 +732,10 @@ Error IndexedInstrProfReader::getFunctionCounts(StringRef FuncName, return success(); } -Error IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) { +Error IndexedInstrProfReader::readNextRecord(NamedInstrProfRecord &Record) { static unsigned RecordIndex = 0; - ArrayRef<InstrProfRecord> Data; + ArrayRef<NamedInstrProfRecord> Data; Error E = Index->getRecords(Data); if (E) |