1 //=-- InstrProfReader.cpp - Instrumented profiling reader -------------------=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for reading profiling data for clang's
11 // instrumentation based PGO and coverage.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ProfileData/InstrProfReader.h"
16 #include "llvm/ADT/STLExtras.h"
21 static Expected<std::unique_ptr<MemoryBuffer>>
22 setupMemoryBuffer(const Twine &Path) {
23 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
24 MemoryBuffer::getFileOrSTDIN(Path);
25 if (std::error_code EC = BufferOrErr.getError())
26 return errorCodeToError(EC);
27 return std::move(BufferOrErr.get());
30 static Error initializeReader(InstrProfReader &Reader) {
31 return Reader.readHeader();
34 Expected<std::unique_ptr<InstrProfReader>>
35 InstrProfReader::create(const Twine &Path) {
36 // Set up the buffer to read.
37 auto BufferOrError = setupMemoryBuffer(Path);
38 if (Error E = BufferOrError.takeError())
40 return InstrProfReader::create(std::move(BufferOrError.get()));
43 Expected<std::unique_ptr<InstrProfReader>>
44 InstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
45 // Sanity check the buffer.
46 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
47 return make_error<InstrProfError>(instrprof_error::too_large);
49 if (Buffer->getBufferSize() == 0)
50 return make_error<InstrProfError>(instrprof_error::empty_raw_profile);
52 std::unique_ptr<InstrProfReader> Result;
54 if (IndexedInstrProfReader::hasFormat(*Buffer))
55 Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
56 else if (RawInstrProfReader64::hasFormat(*Buffer))
57 Result.reset(new RawInstrProfReader64(std::move(Buffer)));
58 else if (RawInstrProfReader32::hasFormat(*Buffer))
59 Result.reset(new RawInstrProfReader32(std::move(Buffer)));
60 else if (TextInstrProfReader::hasFormat(*Buffer))
61 Result.reset(new TextInstrProfReader(std::move(Buffer)));
63 return make_error<InstrProfError>(instrprof_error::unrecognized_format);
65 // Initialize the reader and return the result.
66 if (Error E = initializeReader(*Result))
69 return std::move(Result);
72 Expected<std::unique_ptr<IndexedInstrProfReader>>
73 IndexedInstrProfReader::create(const Twine &Path) {
74 // Set up the buffer to read.
75 auto BufferOrError = setupMemoryBuffer(Path);
76 if (Error E = BufferOrError.takeError())
78 return IndexedInstrProfReader::create(std::move(BufferOrError.get()));
82 Expected<std::unique_ptr<IndexedInstrProfReader>>
83 IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
84 // Sanity check the buffer.
85 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
86 return make_error<InstrProfError>(instrprof_error::too_large);
89 if (!IndexedInstrProfReader::hasFormat(*Buffer))
90 return make_error<InstrProfError>(instrprof_error::bad_magic);
91 auto Result = llvm::make_unique<IndexedInstrProfReader>(std::move(Buffer));
93 // Initialize the reader and return the result.
94 if (Error E = initializeReader(*Result))
97 return std::move(Result);
100 void InstrProfIterator::Increment() {
101 if (auto E = Reader->readNextRecord(Record)) {
102 // Handle errors in the reader.
103 InstrProfError::take(std::move(E));
104 *this = InstrProfIterator();
108 bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) {
109 // Verify that this really looks like plain ASCII text by checking a
110 // 'reasonable' number of characters (up to profile magic size).
111 size_t count = std::min(Buffer.getBufferSize(), sizeof(uint64_t));
112 StringRef buffer = Buffer.getBufferStart();
114 std::all_of(buffer.begin(), buffer.begin() + count,
115 [](char c) { return ::isprint(c) || ::isspace(c); });
118 // Read the profile variant flag from the header: ":FE" means this is a FE
119 // generated profile. ":IR" means this is an IR level profile. Other strings
120 // with a leading ':' will be reported an error format.
121 Error TextInstrProfReader::readHeader() {
122 Symtab.reset(new InstrProfSymtab());
123 bool IsIRInstr = false;
124 if (!Line->startswith(":")) {
125 IsIRLevelProfile = false;
128 StringRef Str = (Line)->substr(1);
129 if (Str.equals_lower("ir"))
131 else if (Str.equals_lower("fe"))
134 return error(instrprof_error::bad_header);
137 IsIRLevelProfile = IsIRInstr;
142 TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) {
144 #define CHECK_LINE_END(Line) \
145 if (Line.is_at_end()) \
146 return error(instrprof_error::truncated);
147 #define READ_NUM(Str, Dst) \
148 if ((Str).getAsInteger(10, (Dst))) \
149 return error(instrprof_error::malformed);
150 #define VP_READ_ADVANCE(Val) \
151 CHECK_LINE_END(Line); \
153 READ_NUM((*Line), (Val)); \
156 if (Line.is_at_end())
159 uint32_t NumValueKinds;
160 if (Line->getAsInteger(10, NumValueKinds)) {
161 // No value profile data
164 if (NumValueKinds == 0 || NumValueKinds > IPVK_Last + 1)
165 return error(instrprof_error::malformed);
168 for (uint32_t VK = 0; VK < NumValueKinds; VK++) {
169 VP_READ_ADVANCE(ValueKind);
170 if (ValueKind > IPVK_Last)
171 return error(instrprof_error::malformed);
172 VP_READ_ADVANCE(NumValueSites);
176 Record.reserveSites(VK, NumValueSites);
177 for (uint32_t S = 0; S < NumValueSites; S++) {
178 VP_READ_ADVANCE(NumValueData);
180 std::vector<InstrProfValueData> CurrentValues;
181 for (uint32_t V = 0; V < NumValueData; V++) {
182 CHECK_LINE_END(Line);
183 std::pair<StringRef, StringRef> VD = Line->rsplit(':');
184 uint64_t TakenCount, Value;
185 if (VK == IPVK_IndirectCallTarget) {
186 Symtab->addFuncName(VD.first);
187 Value = IndexedInstrProf::ComputeHash(VD.first);
189 READ_NUM(VD.first, Value);
191 READ_NUM(VD.second, TakenCount);
192 CurrentValues.push_back({Value, TakenCount});
195 Record.addValueData(VK, S, CurrentValues.data(), NumValueData, nullptr);
200 #undef CHECK_LINE_END
202 #undef VP_READ_ADVANCE
205 Error TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
206 // Skip empty lines and comments.
207 while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
209 // If we hit EOF while looking for a name, we're done.
210 if (Line.is_at_end()) {
211 Symtab->finalizeSymtab();
212 return error(instrprof_error::eof);
215 // Read the function name.
216 Record.Name = *Line++;
217 Symtab->addFuncName(Record.Name);
219 // Read the function hash.
220 if (Line.is_at_end())
221 return error(instrprof_error::truncated);
222 if ((Line++)->getAsInteger(0, Record.Hash))
223 return error(instrprof_error::malformed);
225 // Read the number of counters.
226 uint64_t NumCounters;
227 if (Line.is_at_end())
228 return error(instrprof_error::truncated);
229 if ((Line++)->getAsInteger(10, NumCounters))
230 return error(instrprof_error::malformed);
231 if (NumCounters == 0)
232 return error(instrprof_error::malformed);
234 // Read each counter and fill our internal storage with the values.
235 Record.Counts.clear();
236 Record.Counts.reserve(NumCounters);
237 for (uint64_t I = 0; I < NumCounters; ++I) {
238 if (Line.is_at_end())
239 return error(instrprof_error::truncated);
241 if ((Line++)->getAsInteger(10, Count))
242 return error(instrprof_error::malformed);
243 Record.Counts.push_back(Count);
246 // Check if value profile data exists and read it if so.
247 if (Error E = readValueProfileData(Record))
250 // This is needed to avoid two pass parsing because llvm-profdata
251 // does dumping while reading.
252 Symtab->finalizeSymtab();
256 template <class IntPtrT>
257 bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
258 if (DataBuffer.getBufferSize() < sizeof(uint64_t))
261 *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
262 return RawInstrProf::getMagic<IntPtrT>() == Magic ||
263 sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic;
266 template <class IntPtrT>
267 Error RawInstrProfReader<IntPtrT>::readHeader() {
268 if (!hasFormat(*DataBuffer))
269 return error(instrprof_error::bad_magic);
270 if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header))
271 return error(instrprof_error::bad_header);
272 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(
273 DataBuffer->getBufferStart());
274 ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>();
275 return readHeader(*Header);
278 template <class IntPtrT>
279 Error RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
280 const char *End = DataBuffer->getBufferEnd();
281 // Skip zero padding between profiles.
282 while (CurrentPos != End && *CurrentPos == 0)
284 // If there's nothing left, we're done.
285 if (CurrentPos == End)
286 return make_error<InstrProfError>(instrprof_error::eof);
287 // If there isn't enough space for another header, this is probably just
288 // garbage at the end of the file.
289 if (CurrentPos + sizeof(RawInstrProf::Header) > End)
290 return make_error<InstrProfError>(instrprof_error::malformed);
291 // The writer ensures each profile is padded to start at an aligned address.
292 if (reinterpret_cast<size_t>(CurrentPos) % alignof(uint64_t))
293 return make_error<InstrProfError>(instrprof_error::malformed);
294 // The magic should have the same byte order as in the previous header.
295 uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
296 if (Magic != swap(RawInstrProf::getMagic<IntPtrT>()))
297 return make_error<InstrProfError>(instrprof_error::bad_magic);
299 // There's another profile to read, so we need to process the header.
300 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos);
301 return readHeader(*Header);
304 template <class IntPtrT>
305 Error RawInstrProfReader<IntPtrT>::createSymtab(InstrProfSymtab &Symtab) {
306 if (Error E = Symtab.create(StringRef(NamesStart, NamesSize)))
307 return error(std::move(E));
308 for (const RawInstrProf::ProfileData<IntPtrT> *I = Data; I != DataEnd; ++I) {
309 const IntPtrT FPtr = swap(I->FunctionPointer);
312 Symtab.mapAddress(FPtr, I->NameRef);
314 Symtab.finalizeSymtab();
318 template <class IntPtrT>
319 Error RawInstrProfReader<IntPtrT>::readHeader(
320 const RawInstrProf::Header &Header) {
321 Version = swap(Header.Version);
322 if (GET_VERSION(Version) != RawInstrProf::Version)
323 return error(instrprof_error::unsupported_version);
325 CountersDelta = swap(Header.CountersDelta);
326 NamesDelta = swap(Header.NamesDelta);
327 auto DataSize = swap(Header.DataSize);
328 auto CountersSize = swap(Header.CountersSize);
329 NamesSize = swap(Header.NamesSize);
330 ValueKindLast = swap(Header.ValueKindLast);
332 auto DataSizeInBytes = DataSize * sizeof(RawInstrProf::ProfileData<IntPtrT>);
333 auto PaddingSize = getNumPaddingBytes(NamesSize);
335 ptrdiff_t DataOffset = sizeof(RawInstrProf::Header);
336 ptrdiff_t CountersOffset = DataOffset + DataSizeInBytes;
337 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
338 ptrdiff_t ValueDataOffset = NamesOffset + NamesSize + PaddingSize;
340 auto *Start = reinterpret_cast<const char *>(&Header);
341 if (Start + ValueDataOffset > DataBuffer->getBufferEnd())
342 return error(instrprof_error::bad_header);
344 Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>(
346 DataEnd = Data + DataSize;
347 CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
348 NamesStart = Start + NamesOffset;
349 ValueDataStart = reinterpret_cast<const uint8_t *>(Start + ValueDataOffset);
351 std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
352 if (Error E = createSymtab(*NewSymtab.get()))
355 Symtab = std::move(NewSymtab);
359 template <class IntPtrT>
360 Error RawInstrProfReader<IntPtrT>::readName(InstrProfRecord &Record) {
361 Record.Name = getName(Data->NameRef);
365 template <class IntPtrT>
366 Error RawInstrProfReader<IntPtrT>::readFuncHash(InstrProfRecord &Record) {
367 Record.Hash = swap(Data->FuncHash);
371 template <class IntPtrT>
372 Error RawInstrProfReader<IntPtrT>::readRawCounts(
373 InstrProfRecord &Record) {
374 uint32_t NumCounters = swap(Data->NumCounters);
375 IntPtrT CounterPtr = Data->CounterPtr;
376 if (NumCounters == 0)
377 return error(instrprof_error::malformed);
379 auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters);
380 auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
383 if (RawCounts.data() < CountersStart ||
384 RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
385 return error(instrprof_error::malformed);
387 if (ShouldSwapBytes) {
388 Record.Counts.clear();
389 Record.Counts.reserve(RawCounts.size());
390 for (uint64_t Count : RawCounts)
391 Record.Counts.push_back(swap(Count));
393 Record.Counts = RawCounts;
398 template <class IntPtrT>
399 Error RawInstrProfReader<IntPtrT>::readValueProfilingData(
400 InstrProfRecord &Record) {
402 Record.clearValueData();
403 CurValueDataSize = 0;
404 // Need to match the logic in value profile dumper code in compiler-rt:
405 uint32_t NumValueKinds = 0;
406 for (uint32_t I = 0; I < IPVK_Last + 1; I++)
407 NumValueKinds += (Data->NumValueSites[I] != 0);
412 Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
413 ValueProfData::getValueProfData(
414 ValueDataStart, (const unsigned char *)DataBuffer->getBufferEnd(),
415 getDataEndianness());
417 if (Error E = VDataPtrOrErr.takeError())
420 // Note that besides deserialization, this also performs the conversion for
421 // indirect call targets. The function pointers from the raw profile are
422 // remapped into function name hashes.
423 VDataPtrOrErr.get()->deserializeTo(Record, &Symtab->getAddrHashMap());
424 CurValueDataSize = VDataPtrOrErr.get()->getSize();
428 template <class IntPtrT>
429 Error RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) {
431 // At this point, ValueDataStart field points to the next header.
432 if (Error E = readNextHeader(getNextHeaderPos()))
435 // Read name ad set it in Record.
436 if (Error E = readName(Record))
439 // Read FuncHash and set it in Record.
440 if (Error E = readFuncHash(Record))
443 // Read raw counts and set Record.
444 if (Error E = readRawCounts(Record))
447 // Read value data and set Record.
448 if (Error E = readValueProfilingData(Record))
457 template class RawInstrProfReader<uint32_t>;
458 template class RawInstrProfReader<uint64_t>;
461 InstrProfLookupTrait::hash_value_type
462 InstrProfLookupTrait::ComputeHash(StringRef K) {
463 return IndexedInstrProf::ComputeHash(HashType, K);
466 typedef InstrProfLookupTrait::data_type data_type;
467 typedef InstrProfLookupTrait::offset_type offset_type;
469 bool InstrProfLookupTrait::readValueProfilingData(
470 const unsigned char *&D, const unsigned char *const End) {
471 Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
472 ValueProfData::getValueProfData(D, End, ValueProfDataEndianness);
474 if (VDataPtrOrErr.takeError())
477 VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr);
478 D += VDataPtrOrErr.get()->TotalSize;
483 data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D,
485 // Check if the data is corrupt. If so, don't try to read it.
486 if (N % sizeof(uint64_t))
490 std::vector<uint64_t> CounterBuffer;
492 using namespace support;
493 const unsigned char *End = D + N;
496 if (D + sizeof(uint64_t) >= End)
498 uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
500 // Initialize number of counters for GET_VERSION(FormatVersion) == 1.
501 uint64_t CountsSize = N / sizeof(uint64_t) - 1;
502 // If format version is different then read the number of counters.
503 if (GET_VERSION(FormatVersion) != IndexedInstrProf::ProfVersion::Version1) {
504 if (D + sizeof(uint64_t) > End)
506 CountsSize = endian::readNext<uint64_t, little, unaligned>(D);
508 // Read counter values.
509 if (D + CountsSize * sizeof(uint64_t) > End)
512 CounterBuffer.clear();
513 CounterBuffer.reserve(CountsSize);
514 for (uint64_t J = 0; J < CountsSize; ++J)
515 CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
517 DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer));
519 // Read value profiling data.
520 if (GET_VERSION(FormatVersion) > IndexedInstrProf::ProfVersion::Version2 &&
521 !readValueProfilingData(D, End)) {
529 template <typename HashTableImpl>
530 Error InstrProfReaderIndex<HashTableImpl>::getRecords(
531 StringRef FuncName, ArrayRef<InstrProfRecord> &Data) {
532 auto Iter = HashTable->find(FuncName);
533 if (Iter == HashTable->end())
534 return make_error<InstrProfError>(instrprof_error::unknown_function);
538 return make_error<InstrProfError>(instrprof_error::malformed);
540 return Error::success();
543 template <typename HashTableImpl>
544 Error InstrProfReaderIndex<HashTableImpl>::getRecords(
545 ArrayRef<InstrProfRecord> &Data) {
547 return make_error<InstrProfError>(instrprof_error::eof);
549 Data = *RecordIterator;
552 return make_error<InstrProfError>(instrprof_error::malformed);
554 return Error::success();
557 template <typename HashTableImpl>
558 InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex(
559 const unsigned char *Buckets, const unsigned char *const Payload,
560 const unsigned char *const Base, IndexedInstrProf::HashT HashType,
562 FormatVersion = Version;
563 HashTable.reset(HashTableImpl::Create(
564 Buckets, Payload, Base,
565 typename HashTableImpl::InfoType(HashType, Version)));
566 RecordIterator = HashTable->data_begin();
569 bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
570 if (DataBuffer.getBufferSize() < 8)
572 using namespace support;
574 endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
575 // Verify that it's magical.
576 return Magic == IndexedInstrProf::Magic;
579 const unsigned char *
580 IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version,
581 const unsigned char *Cur) {
582 using namespace IndexedInstrProf;
583 using namespace support;
584 if (Version >= IndexedInstrProf::Version4) {
585 const IndexedInstrProf::Summary *SummaryInLE =
586 reinterpret_cast<const IndexedInstrProf::Summary *>(Cur);
588 endian::byte_swap<uint64_t, little>(SummaryInLE->NumSummaryFields);
590 endian::byte_swap<uint64_t, little>(SummaryInLE->NumCutoffEntries);
591 uint32_t SummarySize =
592 IndexedInstrProf::Summary::getSize(NFields, NEntries);
593 std::unique_ptr<IndexedInstrProf::Summary> SummaryData =
594 IndexedInstrProf::allocSummary(SummarySize);
596 const uint64_t *Src = reinterpret_cast<const uint64_t *>(SummaryInLE);
597 uint64_t *Dst = reinterpret_cast<uint64_t *>(SummaryData.get());
598 for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
599 Dst[I] = endian::byte_swap<uint64_t, little>(Src[I]);
601 llvm::SummaryEntryVector DetailedSummary;
602 for (unsigned I = 0; I < SummaryData->NumCutoffEntries; I++) {
603 const IndexedInstrProf::Summary::Entry &Ent = SummaryData->getEntry(I);
604 DetailedSummary.emplace_back((uint32_t)Ent.Cutoff, Ent.MinBlockCount,
607 // initialize InstrProfSummary using the SummaryData from disk.
608 this->Summary = llvm::make_unique<ProfileSummary>(
609 ProfileSummary::PSK_Instr, DetailedSummary,
610 SummaryData->get(Summary::TotalBlockCount),
611 SummaryData->get(Summary::MaxBlockCount),
612 SummaryData->get(Summary::MaxInternalBlockCount),
613 SummaryData->get(Summary::MaxFunctionCount),
614 SummaryData->get(Summary::TotalNumBlocks),
615 SummaryData->get(Summary::TotalNumFunctions));
616 return Cur + SummarySize;
618 // For older version of profile data, we need to compute on the fly:
619 using namespace IndexedInstrProf;
620 InstrProfSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
621 // FIXME: This only computes an empty summary. Need to call addRecord for
622 // all InstrProfRecords to get the correct summary.
623 this->Summary = Builder.getSummary();
628 Error IndexedInstrProfReader::readHeader() {
629 const unsigned char *Start =
630 (const unsigned char *)DataBuffer->getBufferStart();
631 const unsigned char *Cur = Start;
632 if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
633 return error(instrprof_error::truncated);
635 using namespace support;
637 auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur);
638 Cur += sizeof(IndexedInstrProf::Header);
640 // Check the magic number.
641 uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic);
642 if (Magic != IndexedInstrProf::Magic)
643 return error(instrprof_error::bad_magic);
646 uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version);
647 if (GET_VERSION(FormatVersion) >
648 IndexedInstrProf::ProfVersion::CurrentVersion)
649 return error(instrprof_error::unsupported_version);
651 Cur = readSummary((IndexedInstrProf::ProfVersion)FormatVersion, Cur);
653 // Read the hash type and start offset.
654 IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
655 endian::byte_swap<uint64_t, little>(Header->HashType));
656 if (HashType > IndexedInstrProf::HashT::Last)
657 return error(instrprof_error::unsupported_hash_type);
659 uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
661 // The rest of the file is an on disk hash table.
662 InstrProfReaderIndexBase *IndexPtr = nullptr;
663 IndexPtr = new InstrProfReaderIndex<OnDiskHashTableImplV3>(
664 Start + HashOffset, Cur, Start, HashType, FormatVersion);
665 Index.reset(IndexPtr);
669 InstrProfSymtab &IndexedInstrProfReader::getSymtab() {
671 return *Symtab.get();
673 std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
674 Index->populateSymtab(*NewSymtab.get());
676 Symtab = std::move(NewSymtab);
677 return *Symtab.get();
680 Expected<InstrProfRecord>
681 IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
683 ArrayRef<InstrProfRecord> Data;
684 Error Err = Index->getRecords(FuncName, Data);
686 return std::move(Err);
687 // Found it. Look for counters with the right hash.
688 for (unsigned I = 0, E = Data.size(); I < E; ++I) {
689 // Check for a match and fill the vector if there is one.
690 if (Data[I].Hash == FuncHash) {
691 return std::move(Data[I]);
694 return error(instrprof_error::hash_mismatch);
697 Error IndexedInstrProfReader::getFunctionCounts(StringRef FuncName,
699 std::vector<uint64_t> &Counts) {
700 Expected<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash);
701 if (Error E = Record.takeError())
702 return error(std::move(E));
704 Counts = Record.get().Counts;
708 Error IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) {
709 static unsigned RecordIndex = 0;
711 ArrayRef<InstrProfRecord> Data;
713 Error E = Index->getRecords(Data);
715 return error(std::move(E));
717 Record = Data[RecordIndex++];
718 if (RecordIndex >= Data.size()) {
719 Index->advanceToNextKey();