1 //=-- InstrProfReader.cpp - Instrumented profiling reader -------------------=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for reading profiling data for clang's
11 // instrumentation based PGO and coverage.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ProfileData/InstrProfReader.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include <cassert>
18 
19 using namespace llvm;
20 
21 static ErrorOr<std::unique_ptr<MemoryBuffer>>
22 setupMemoryBuffer(std::string Path) {
23   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
24       MemoryBuffer::getFileOrSTDIN(Path);
25   if (std::error_code EC = BufferOrErr.getError())
26     return EC;
27   return std::move(BufferOrErr.get());
28 }
29 
30 static std::error_code initializeReader(InstrProfReader &Reader) {
31   return Reader.readHeader();
32 }
33 
34 ErrorOr<std::unique_ptr<InstrProfReader>>
35 InstrProfReader::create(std::string Path) {
36   // Set up the buffer to read.
37   auto BufferOrError = setupMemoryBuffer(Path);
38   if (std::error_code EC = BufferOrError.getError())
39     return EC;
40   return InstrProfReader::create(std::move(BufferOrError.get()));
41 }
42 
43 ErrorOr<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 instrprof_error::too_large;
48 
49   std::unique_ptr<InstrProfReader> Result;
50   // Create the reader.
51   if (IndexedInstrProfReader::hasFormat(*Buffer))
52     Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
53   else if (RawInstrProfReader64::hasFormat(*Buffer))
54     Result.reset(new RawInstrProfReader64(std::move(Buffer)));
55   else if (RawInstrProfReader32::hasFormat(*Buffer))
56     Result.reset(new RawInstrProfReader32(std::move(Buffer)));
57   else if (TextInstrProfReader::hasFormat(*Buffer))
58     Result.reset(new TextInstrProfReader(std::move(Buffer)));
59   else
60     return instrprof_error::unrecognized_format;
61 
62   // Initialize the reader and return the result.
63   if (std::error_code EC = initializeReader(*Result))
64     return EC;
65 
66   return std::move(Result);
67 }
68 
69 ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
70 IndexedInstrProfReader::create(std::string Path) {
71   // Set up the buffer to read.
72   auto BufferOrError = setupMemoryBuffer(Path);
73   if (std::error_code EC = BufferOrError.getError())
74     return EC;
75   return IndexedInstrProfReader::create(std::move(BufferOrError.get()));
76 }
77 
78 
79 ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
80 IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
81   // Sanity check the buffer.
82   if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
83     return instrprof_error::too_large;
84 
85   // Create the reader.
86   if (!IndexedInstrProfReader::hasFormat(*Buffer))
87     return instrprof_error::bad_magic;
88   auto Result = llvm::make_unique<IndexedInstrProfReader>(std::move(Buffer));
89 
90   // Initialize the reader and return the result.
91   if (std::error_code EC = initializeReader(*Result))
92     return EC;
93 
94   return std::move(Result);
95 }
96 
97 void InstrProfIterator::Increment() {
98   if (Reader->readNextRecord(Record))
99     *this = InstrProfIterator();
100 }
101 
102 bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) {
103   // Verify that this really looks like plain ASCII text by checking a
104   // 'reasonable' number of characters (up to profile magic size).
105   size_t count = std::min(Buffer.getBufferSize(), sizeof(uint64_t));
106   StringRef buffer = Buffer.getBufferStart();
107   return count == 0 ||
108          std::all_of(buffer.begin(), buffer.begin() + count,
109                      [](char c) { return ::isprint(c) || ::isspace(c); });
110 }
111 
112 // Read the profile variant flag from the header: ":FE" means this is a FE
113 // generated profile. ":IR" means this is an IR level profile. Other strings
114 // with a leading ':' will be reported an error format.
115 std::error_code TextInstrProfReader::readHeader() {
116   Symtab.reset(new InstrProfSymtab());
117   bool IsIRInstr = false;
118   if (!Line->startswith(":")) {
119     IsIRLevelProfile = false;
120     return success();
121   }
122   StringRef Str = (Line)->substr(1);
123   if (Str.equals_lower("ir"))
124     IsIRInstr = true;
125   else if (Str.equals_lower("fe"))
126     IsIRInstr = false;
127   else
128     return instrprof_error::bad_header;
129 
130   ++Line;
131   IsIRLevelProfile = IsIRInstr;
132   return success();
133 }
134 
135 std::error_code
136 TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) {
137 
138 #define CHECK_LINE_END(Line)                                                   \
139   if (Line.is_at_end())                                                        \
140     return error(instrprof_error::truncated);
141 #define READ_NUM(Str, Dst)                                                     \
142   if ((Str).getAsInteger(10, (Dst)))                                           \
143     return error(instrprof_error::malformed);
144 #define VP_READ_ADVANCE(Val)                                                   \
145   CHECK_LINE_END(Line);                                                        \
146   uint32_t Val;                                                                \
147   READ_NUM((*Line), (Val));                                                    \
148   Line++;
149 
150   if (Line.is_at_end())
151     return success();
152 
153   uint32_t NumValueKinds;
154   if (Line->getAsInteger(10, NumValueKinds)) {
155     // No value profile data
156     return success();
157   }
158   if (NumValueKinds == 0 || NumValueKinds > IPVK_Last + 1)
159     return error(instrprof_error::malformed);
160   Line++;
161 
162   for (uint32_t VK = 0; VK < NumValueKinds; VK++) {
163     VP_READ_ADVANCE(ValueKind);
164     if (ValueKind > IPVK_Last)
165       return error(instrprof_error::malformed);
166     VP_READ_ADVANCE(NumValueSites);
167     if (!NumValueSites)
168       continue;
169 
170     Record.reserveSites(VK, NumValueSites);
171     for (uint32_t S = 0; S < NumValueSites; S++) {
172       VP_READ_ADVANCE(NumValueData);
173 
174       std::vector<InstrProfValueData> CurrentValues;
175       for (uint32_t V = 0; V < NumValueData; V++) {
176         CHECK_LINE_END(Line);
177         std::pair<StringRef, StringRef> VD = Line->split(':');
178         uint64_t TakenCount, Value;
179         if (VK == IPVK_IndirectCallTarget) {
180           Symtab->addFuncName(VD.first);
181           Value = IndexedInstrProf::ComputeHash(VD.first);
182         } else {
183           READ_NUM(VD.first, Value);
184         }
185         READ_NUM(VD.second, TakenCount);
186         CurrentValues.push_back({Value, TakenCount});
187         Line++;
188       }
189       Record.addValueData(VK, S, CurrentValues.data(), NumValueData, nullptr);
190     }
191   }
192   return success();
193 
194 #undef CHECK_LINE_END
195 #undef READ_NUM
196 #undef VP_READ_ADVANCE
197 }
198 
199 std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
200   // Skip empty lines and comments.
201   while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
202     ++Line;
203   // If we hit EOF while looking for a name, we're done.
204   if (Line.is_at_end()) {
205     Symtab->finalizeSymtab();
206     return error(instrprof_error::eof);
207   }
208 
209   // Read the function name.
210   Record.Name = *Line++;
211   Symtab->addFuncName(Record.Name);
212 
213   // Read the function hash.
214   if (Line.is_at_end())
215     return error(instrprof_error::truncated);
216   if ((Line++)->getAsInteger(0, Record.Hash))
217     return error(instrprof_error::malformed);
218 
219   // Read the number of counters.
220   uint64_t NumCounters;
221   if (Line.is_at_end())
222     return error(instrprof_error::truncated);
223   if ((Line++)->getAsInteger(10, NumCounters))
224     return error(instrprof_error::malformed);
225   if (NumCounters == 0)
226     return error(instrprof_error::malformed);
227 
228   // Read each counter and fill our internal storage with the values.
229   Record.Counts.clear();
230   Record.Counts.reserve(NumCounters);
231   for (uint64_t I = 0; I < NumCounters; ++I) {
232     if (Line.is_at_end())
233       return error(instrprof_error::truncated);
234     uint64_t Count;
235     if ((Line++)->getAsInteger(10, Count))
236       return error(instrprof_error::malformed);
237     Record.Counts.push_back(Count);
238   }
239 
240   // Check if value profile data exists and read it if so.
241   if (std::error_code EC = readValueProfileData(Record))
242     return EC;
243 
244   // This is needed to avoid two pass parsing because llvm-profdata
245   // does dumping while reading.
246   Symtab->finalizeSymtab();
247   return success();
248 }
249 
250 template <class IntPtrT>
251 bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
252   if (DataBuffer.getBufferSize() < sizeof(uint64_t))
253     return false;
254   uint64_t Magic =
255     *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
256   return RawInstrProf::getMagic<IntPtrT>() == Magic ||
257          sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic;
258 }
259 
260 template <class IntPtrT>
261 std::error_code RawInstrProfReader<IntPtrT>::readHeader() {
262   if (!hasFormat(*DataBuffer))
263     return error(instrprof_error::bad_magic);
264   if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header))
265     return error(instrprof_error::bad_header);
266   auto *Header = reinterpret_cast<const RawInstrProf::Header *>(
267       DataBuffer->getBufferStart());
268   ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>();
269   return readHeader(*Header);
270 }
271 
272 template <class IntPtrT>
273 std::error_code
274 RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
275   const char *End = DataBuffer->getBufferEnd();
276   // Skip zero padding between profiles.
277   while (CurrentPos != End && *CurrentPos == 0)
278     ++CurrentPos;
279   // If there's nothing left, we're done.
280   if (CurrentPos == End)
281     return instrprof_error::eof;
282   // If there isn't enough space for another header, this is probably just
283   // garbage at the end of the file.
284   if (CurrentPos + sizeof(RawInstrProf::Header) > End)
285     return instrprof_error::malformed;
286   // The writer ensures each profile is padded to start at an aligned address.
287   if (reinterpret_cast<size_t>(CurrentPos) % alignOf<uint64_t>())
288     return instrprof_error::malformed;
289   // The magic should have the same byte order as in the previous header.
290   uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
291   if (Magic != swap(RawInstrProf::getMagic<IntPtrT>()))
292     return instrprof_error::bad_magic;
293 
294   // There's another profile to read, so we need to process the header.
295   auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos);
296   return readHeader(*Header);
297 }
298 
299 template <class IntPtrT>
300 void RawInstrProfReader<IntPtrT>::createSymtab(InstrProfSymtab &Symtab) {
301   Symtab.create(StringRef(NamesStart, NamesSize));
302   for (const RawInstrProf::ProfileData<IntPtrT> *I = Data; I != DataEnd; ++I) {
303     const IntPtrT FPtr = swap(I->FunctionPointer);
304     if (!FPtr)
305       continue;
306     Symtab.mapAddress(FPtr, I->NameRef);
307   }
308   Symtab.finalizeSymtab();
309 }
310 
311 template <class IntPtrT>
312 std::error_code
313 RawInstrProfReader<IntPtrT>::readHeader(const RawInstrProf::Header &Header) {
314   Version = swap(Header.Version);
315   if (GET_VERSION(Version) != RawInstrProf::Version)
316     return error(instrprof_error::unsupported_version);
317 
318   CountersDelta = swap(Header.CountersDelta);
319   NamesDelta = swap(Header.NamesDelta);
320   auto DataSize = swap(Header.DataSize);
321   auto CountersSize = swap(Header.CountersSize);
322   NamesSize = swap(Header.NamesSize);
323   auto ValueDataSize = swap(Header.ValueDataSize);
324   ValueKindLast = swap(Header.ValueKindLast);
325 
326   auto DataSizeInBytes = DataSize * sizeof(RawInstrProf::ProfileData<IntPtrT>);
327   auto PaddingSize = getNumPaddingBytes(NamesSize);
328 
329   ptrdiff_t DataOffset = sizeof(RawInstrProf::Header);
330   ptrdiff_t CountersOffset = DataOffset + DataSizeInBytes;
331   ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
332   ptrdiff_t ValueDataOffset = NamesOffset + NamesSize + PaddingSize;
333   size_t ProfileSize = ValueDataOffset + ValueDataSize;
334 
335   auto *Start = reinterpret_cast<const char *>(&Header);
336   if (Start + ProfileSize > DataBuffer->getBufferEnd())
337     return error(instrprof_error::bad_header);
338 
339   Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>(
340       Start + DataOffset);
341   DataEnd = Data + DataSize;
342   CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
343   NamesStart = Start + NamesOffset;
344   ValueDataStart = reinterpret_cast<const uint8_t *>(Start + ValueDataOffset);
345   ProfileEnd = Start + ProfileSize;
346 
347   std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
348   createSymtab(*NewSymtab.get());
349   Symtab = std::move(NewSymtab);
350   return success();
351 }
352 
353 template <class IntPtrT>
354 std::error_code RawInstrProfReader<IntPtrT>::readName(InstrProfRecord &Record) {
355   Record.Name = getName(Data->NameRef);
356   return success();
357 }
358 
359 template <class IntPtrT>
360 std::error_code RawInstrProfReader<IntPtrT>::readFuncHash(
361     InstrProfRecord &Record) {
362   Record.Hash = swap(Data->FuncHash);
363   return success();
364 }
365 
366 template <class IntPtrT>
367 std::error_code RawInstrProfReader<IntPtrT>::readRawCounts(
368     InstrProfRecord &Record) {
369   uint32_t NumCounters = swap(Data->NumCounters);
370   IntPtrT CounterPtr = Data->CounterPtr;
371   if (NumCounters == 0)
372     return error(instrprof_error::malformed);
373 
374   auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters);
375   auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
376 
377   // Check bounds.
378   if (RawCounts.data() < CountersStart ||
379       RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
380     return error(instrprof_error::malformed);
381 
382   if (ShouldSwapBytes) {
383     Record.Counts.clear();
384     Record.Counts.reserve(RawCounts.size());
385     for (uint64_t Count : RawCounts)
386       Record.Counts.push_back(swap(Count));
387   } else
388     Record.Counts = RawCounts;
389 
390   return success();
391 }
392 
393 template <class IntPtrT>
394 std::error_code
395 RawInstrProfReader<IntPtrT>::readValueProfilingData(InstrProfRecord &Record) {
396 
397   Record.clearValueData();
398   CurValueDataSize = 0;
399   // Need to match the logic in value profile dumper code in compiler-rt:
400   uint32_t NumValueKinds = 0;
401   for (uint32_t I = 0; I < IPVK_Last + 1; I++)
402     NumValueKinds += (Data->NumValueSites[I] != 0);
403 
404   if (!NumValueKinds)
405     return success();
406 
407   ErrorOr<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
408       ValueProfData::getValueProfData(ValueDataStart,
409                                       (const unsigned char *)ProfileEnd,
410                                       getDataEndianness());
411 
412   if (VDataPtrOrErr.getError())
413     return VDataPtrOrErr.getError();
414 
415   VDataPtrOrErr.get()->deserializeTo(Record, &Symtab->getAddrHashMap());
416   CurValueDataSize = VDataPtrOrErr.get()->getSize();
417   return success();
418 }
419 
420 template <class IntPtrT>
421 std::error_code
422 RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) {
423   if (atEnd())
424     if (std::error_code EC = readNextHeader(ProfileEnd))
425       return EC;
426 
427   // Read name ad set it in Record.
428   if (std::error_code EC = readName(Record))
429     return EC;
430 
431   // Read FuncHash and set it in Record.
432   if (std::error_code EC = readFuncHash(Record))
433     return EC;
434 
435   // Read raw counts and set Record.
436   if (std::error_code EC = readRawCounts(Record))
437     return EC;
438 
439   // Read value data and set Record.
440   if (std::error_code EC = readValueProfilingData(Record))
441     return EC;
442 
443   // Iterate.
444   advanceData();
445   return success();
446 }
447 
448 namespace llvm {
449 template class RawInstrProfReader<uint32_t>;
450 template class RawInstrProfReader<uint64_t>;
451 }
452 
453 InstrProfLookupTrait::hash_value_type
454 InstrProfLookupTrait::ComputeHash(StringRef K) {
455   return IndexedInstrProf::ComputeHash(HashType, K);
456 }
457 
458 typedef InstrProfLookupTrait::data_type data_type;
459 typedef InstrProfLookupTrait::offset_type offset_type;
460 
461 bool InstrProfLookupTrait::readValueProfilingData(
462     const unsigned char *&D, const unsigned char *const End) {
463   ErrorOr<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
464       ValueProfData::getValueProfData(D, End, ValueProfDataEndianness);
465 
466   if (VDataPtrOrErr.getError())
467     return false;
468 
469   VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr);
470   D += VDataPtrOrErr.get()->TotalSize;
471 
472   return true;
473 }
474 
475 data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D,
476                                          offset_type N) {
477   // Check if the data is corrupt. If so, don't try to read it.
478   if (N % sizeof(uint64_t))
479     return data_type();
480 
481   DataBuffer.clear();
482   std::vector<uint64_t> CounterBuffer;
483 
484   using namespace support;
485   const unsigned char *End = D + N;
486   while (D < End) {
487     // Read hash.
488     if (D + sizeof(uint64_t) >= End)
489       return data_type();
490     uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
491 
492     // Initialize number of counters for GET_VERSION(FormatVersion) == 1.
493     uint64_t CountsSize = N / sizeof(uint64_t) - 1;
494     // If format version is different then read the number of counters.
495     if (GET_VERSION(FormatVersion) != IndexedInstrProf::ProfVersion::Version1) {
496       if (D + sizeof(uint64_t) > End)
497         return data_type();
498       CountsSize = endian::readNext<uint64_t, little, unaligned>(D);
499     }
500     // Read counter values.
501     if (D + CountsSize * sizeof(uint64_t) > End)
502       return data_type();
503 
504     CounterBuffer.clear();
505     CounterBuffer.reserve(CountsSize);
506     for (uint64_t J = 0; J < CountsSize; ++J)
507       CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
508 
509     DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer));
510 
511     // Read value profiling data.
512     if (GET_VERSION(FormatVersion) > IndexedInstrProf::ProfVersion::Version2 &&
513         !readValueProfilingData(D, End)) {
514       DataBuffer.clear();
515       return data_type();
516     }
517   }
518   return DataBuffer;
519 }
520 
521 template <typename HashTableImpl>
522 std::error_code InstrProfReaderIndex<HashTableImpl>::getRecords(
523     StringRef FuncName, ArrayRef<InstrProfRecord> &Data) {
524   auto Iter = HashTable->find(FuncName);
525   if (Iter == HashTable->end())
526     return instrprof_error::unknown_function;
527 
528   Data = (*Iter);
529   if (Data.empty())
530     return instrprof_error::malformed;
531 
532   return instrprof_error::success;
533 }
534 
535 template <typename HashTableImpl>
536 std::error_code InstrProfReaderIndex<HashTableImpl>::getRecords(
537     ArrayRef<InstrProfRecord> &Data) {
538   if (atEnd())
539     return instrprof_error::eof;
540 
541   Data = *RecordIterator;
542 
543   if (Data.empty())
544     return instrprof_error::malformed;
545 
546   return instrprof_error::success;
547 }
548 
549 template <typename HashTableImpl>
550 InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex(
551     const unsigned char *Buckets, const unsigned char *const Payload,
552     const unsigned char *const Base, IndexedInstrProf::HashT HashType,
553     uint64_t Version) {
554   FormatVersion = Version;
555   HashTable.reset(HashTableImpl::Create(
556       Buckets, Payload, Base,
557       typename HashTableImpl::InfoType(HashType, Version)));
558   RecordIterator = HashTable->data_begin();
559 }
560 
561 bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
562   if (DataBuffer.getBufferSize() < 8)
563     return false;
564   using namespace support;
565   uint64_t Magic =
566       endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
567   // Verify that it's magical.
568   return Magic == IndexedInstrProf::Magic;
569 }
570 
571 const unsigned char *
572 IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version,
573                                     const unsigned char *Cur) {
574   using namespace support;
575   if (Version >= IndexedInstrProf::Version4) {
576     const IndexedInstrProf::Summary *SummaryInLE =
577         reinterpret_cast<const IndexedInstrProf::Summary *>(Cur);
578     uint64_t NFields =
579         endian::byte_swap<uint64_t, little>(SummaryInLE->NumSummaryFields);
580     uint64_t NEntries =
581         endian::byte_swap<uint64_t, little>(SummaryInLE->NumCutoffEntries);
582     uint32_t SummarySize =
583         IndexedInstrProf::Summary::getSize(NFields, NEntries);
584     std::unique_ptr<IndexedInstrProf::Summary> SummaryData =
585         IndexedInstrProf::allocSummary(SummarySize);
586 
587     const uint64_t *Src = reinterpret_cast<const uint64_t *>(SummaryInLE);
588     uint64_t *Dst = reinterpret_cast<uint64_t *>(SummaryData.get());
589     for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
590       Dst[I] = endian::byte_swap<uint64_t, little>(Src[I]);
591 
592     // initialize InstrProfSummary using the SummaryData from disk.
593     this->Summary = llvm::make_unique<InstrProfSummary>(*(SummaryData.get()));
594     return Cur + SummarySize;
595   } else {
596     // For older version of profile data, we need to compute on the fly:
597     using namespace IndexedInstrProf;
598     this->Summary =
599         llvm::make_unique<InstrProfSummary>(ProfileSummary::DefaultCutoffs);
600     this->Summary->computeDetailedSummary();
601     return Cur;
602   }
603 }
604 
605 std::error_code IndexedInstrProfReader::readHeader() {
606   const unsigned char *Start =
607       (const unsigned char *)DataBuffer->getBufferStart();
608   const unsigned char *Cur = Start;
609   if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
610     return error(instrprof_error::truncated);
611 
612   using namespace support;
613 
614   auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur);
615   Cur += sizeof(IndexedInstrProf::Header);
616 
617   // Check the magic number.
618   uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic);
619   if (Magic != IndexedInstrProf::Magic)
620     return error(instrprof_error::bad_magic);
621 
622   // Read the version.
623   uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version);
624   if (GET_VERSION(FormatVersion) >
625       IndexedInstrProf::ProfVersion::CurrentVersion)
626     return error(instrprof_error::unsupported_version);
627 
628   Cur = readSummary((IndexedInstrProf::ProfVersion)FormatVersion, Cur);
629 
630   // Read the hash type and start offset.
631   IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
632       endian::byte_swap<uint64_t, little>(Header->HashType));
633   if (HashType > IndexedInstrProf::HashT::Last)
634     return error(instrprof_error::unsupported_hash_type);
635 
636   uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
637 
638   // The rest of the file is an on disk hash table.
639   InstrProfReaderIndexBase *IndexPtr = nullptr;
640   IndexPtr = new InstrProfReaderIndex<OnDiskHashTableImplV3>(
641       Start + HashOffset, Cur, Start, HashType, FormatVersion);
642   Index.reset(IndexPtr);
643   return success();
644 }
645 
646 InstrProfSymtab &IndexedInstrProfReader::getSymtab() {
647   if (Symtab.get())
648     return *Symtab.get();
649 
650   std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
651   Index->populateSymtab(*NewSymtab.get());
652 
653   Symtab = std::move(NewSymtab);
654   return *Symtab.get();
655 }
656 
657 ErrorOr<InstrProfRecord>
658 IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
659                                            uint64_t FuncHash) {
660   ArrayRef<InstrProfRecord> Data;
661   std::error_code EC = Index->getRecords(FuncName, Data);
662   if (EC != instrprof_error::success)
663     return EC;
664   // Found it. Look for counters with the right hash.
665   for (unsigned I = 0, E = Data.size(); I < E; ++I) {
666     // Check for a match and fill the vector if there is one.
667     if (Data[I].Hash == FuncHash) {
668       return std::move(Data[I]);
669     }
670   }
671   return error(instrprof_error::hash_mismatch);
672 }
673 
674 std::error_code
675 IndexedInstrProfReader::getFunctionCounts(StringRef FuncName, uint64_t FuncHash,
676                                           std::vector<uint64_t> &Counts) {
677   ErrorOr<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash);
678   if (std::error_code EC = Record.getError())
679     return EC;
680 
681   Counts = Record.get().Counts;
682   return success();
683 }
684 
685 std::error_code IndexedInstrProfReader::readNextRecord(
686     InstrProfRecord &Record) {
687   static unsigned RecordIndex = 0;
688 
689   ArrayRef<InstrProfRecord> Data;
690 
691   std::error_code EC = Index->getRecords(Data);
692   if (EC != instrprof_error::success)
693     return error(EC);
694 
695   Record = Data[RecordIndex++];
696   if (RecordIndex >= Data.size()) {
697     Index->advanceToNextKey();
698     RecordIndex = 0;
699   }
700   return success();
701 }
702