1 //===- InstrProfReader.cpp - Instrumented profiling reader ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for reading profiling data for clang's
10 // instrumentation based PGO and coverage.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ProfileData/InstrProfReader.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/IR/ProfileSummary.h"
21 #include "llvm/ProfileData/InstrProf.h"
22 #include "llvm/ProfileData/ProfileCommon.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/Error.h"
25 #include "llvm/Support/ErrorOr.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/SymbolRemappingReader.h"
28 #include "llvm/Support/SwapByteOrder.h"
29 #include <algorithm>
30 #include <cctype>
31 #include <cstddef>
32 #include <cstdint>
33 #include <limits>
34 #include <memory>
35 #include <system_error>
36 #include <utility>
37 #include <vector>
38 
39 using namespace llvm;
40 
41 static Expected<std::unique_ptr<MemoryBuffer>>
setupMemoryBuffer(const Twine & Path)42 setupMemoryBuffer(const Twine &Path) {
43   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
44       MemoryBuffer::getFileOrSTDIN(Path);
45   if (std::error_code EC = BufferOrErr.getError())
46     return errorCodeToError(EC);
47   return std::move(BufferOrErr.get());
48 }
49 
initializeReader(InstrProfReader & Reader)50 static Error initializeReader(InstrProfReader &Reader) {
51   return Reader.readHeader();
52 }
53 
54 Expected<std::unique_ptr<InstrProfReader>>
create(const Twine & Path)55 InstrProfReader::create(const Twine &Path) {
56   // Set up the buffer to read.
57   auto BufferOrError = setupMemoryBuffer(Path);
58   if (Error E = BufferOrError.takeError())
59     return std::move(E);
60   return InstrProfReader::create(std::move(BufferOrError.get()));
61 }
62 
63 Expected<std::unique_ptr<InstrProfReader>>
create(std::unique_ptr<MemoryBuffer> Buffer)64 InstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
65   // Sanity check the buffer.
66   if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint64_t>::max())
67     return make_error<InstrProfError>(instrprof_error::too_large);
68 
69   if (Buffer->getBufferSize() == 0)
70     return make_error<InstrProfError>(instrprof_error::empty_raw_profile);
71 
72   std::unique_ptr<InstrProfReader> Result;
73   // Create the reader.
74   if (IndexedInstrProfReader::hasFormat(*Buffer))
75     Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
76   else if (RawInstrProfReader64::hasFormat(*Buffer))
77     Result.reset(new RawInstrProfReader64(std::move(Buffer)));
78   else if (RawInstrProfReader32::hasFormat(*Buffer))
79     Result.reset(new RawInstrProfReader32(std::move(Buffer)));
80   else if (TextInstrProfReader::hasFormat(*Buffer))
81     Result.reset(new TextInstrProfReader(std::move(Buffer)));
82   else
83     return make_error<InstrProfError>(instrprof_error::unrecognized_format);
84 
85   // Initialize the reader and return the result.
86   if (Error E = initializeReader(*Result))
87     return std::move(E);
88 
89   return std::move(Result);
90 }
91 
92 Expected<std::unique_ptr<IndexedInstrProfReader>>
create(const Twine & Path,const Twine & RemappingPath)93 IndexedInstrProfReader::create(const Twine &Path, const Twine &RemappingPath) {
94   // Set up the buffer to read.
95   auto BufferOrError = setupMemoryBuffer(Path);
96   if (Error E = BufferOrError.takeError())
97     return std::move(E);
98 
99   // Set up the remapping buffer if requested.
100   std::unique_ptr<MemoryBuffer> RemappingBuffer;
101   std::string RemappingPathStr = RemappingPath.str();
102   if (!RemappingPathStr.empty()) {
103     auto RemappingBufferOrError = setupMemoryBuffer(RemappingPathStr);
104     if (Error E = RemappingBufferOrError.takeError())
105       return std::move(E);
106     RemappingBuffer = std::move(RemappingBufferOrError.get());
107   }
108 
109   return IndexedInstrProfReader::create(std::move(BufferOrError.get()),
110                                         std::move(RemappingBuffer));
111 }
112 
113 Expected<std::unique_ptr<IndexedInstrProfReader>>
create(std::unique_ptr<MemoryBuffer> Buffer,std::unique_ptr<MemoryBuffer> RemappingBuffer)114 IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer,
115                                std::unique_ptr<MemoryBuffer> RemappingBuffer) {
116   // Sanity check the buffer.
117   if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<uint64_t>::max())
118     return make_error<InstrProfError>(instrprof_error::too_large);
119 
120   // Create the reader.
121   if (!IndexedInstrProfReader::hasFormat(*Buffer))
122     return make_error<InstrProfError>(instrprof_error::bad_magic);
123   auto Result = std::make_unique<IndexedInstrProfReader>(
124       std::move(Buffer), std::move(RemappingBuffer));
125 
126   // Initialize the reader and return the result.
127   if (Error E = initializeReader(*Result))
128     return std::move(E);
129 
130   return std::move(Result);
131 }
132 
Increment()133 void InstrProfIterator::Increment() {
134   if (auto E = Reader->readNextRecord(Record)) {
135     // Handle errors in the reader.
136     InstrProfError::take(std::move(E));
137     *this = InstrProfIterator();
138   }
139 }
140 
hasFormat(const MemoryBuffer & Buffer)141 bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) {
142   // Verify that this really looks like plain ASCII text by checking a
143   // 'reasonable' number of characters (up to profile magic size).
144   size_t count = std::min(Buffer.getBufferSize(), sizeof(uint64_t));
145   StringRef buffer = Buffer.getBufferStart();
146   return count == 0 ||
147          std::all_of(buffer.begin(), buffer.begin() + count,
148                      [](char c) { return isPrint(c) || isSpace(c); });
149 }
150 
151 // Read the profile variant flag from the header: ":FE" means this is a FE
152 // generated profile. ":IR" means this is an IR level profile. Other strings
153 // with a leading ':' will be reported an error format.
readHeader()154 Error TextInstrProfReader::readHeader() {
155   Symtab.reset(new InstrProfSymtab());
156   bool IsIRInstr = false;
157   bool IsEntryFirst = false;
158   bool IsCS = false;
159 
160   while (Line->startswith(":")) {
161     StringRef Str = Line->substr(1);
162     if (Str.equals_lower("ir"))
163       IsIRInstr = true;
164     else if (Str.equals_lower("fe"))
165       IsIRInstr = false;
166     else if (Str.equals_lower("csir")) {
167       IsIRInstr = true;
168       IsCS = true;
169     } else if (Str.equals_lower("entry_first"))
170       IsEntryFirst = true;
171     else if (Str.equals_lower("not_entry_first"))
172       IsEntryFirst = false;
173     else
174       return error(instrprof_error::bad_header);
175     ++Line;
176   }
177   IsIRLevelProfile = IsIRInstr;
178   InstrEntryBBEnabled = IsEntryFirst;
179   HasCSIRLevelProfile = IsCS;
180   return success();
181 }
182 
183 Error
readValueProfileData(InstrProfRecord & Record)184 TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) {
185 
186 #define CHECK_LINE_END(Line)                                                   \
187   if (Line.is_at_end())                                                        \
188     return error(instrprof_error::truncated);
189 #define READ_NUM(Str, Dst)                                                     \
190   if ((Str).getAsInteger(10, (Dst)))                                           \
191     return error(instrprof_error::malformed);
192 #define VP_READ_ADVANCE(Val)                                                   \
193   CHECK_LINE_END(Line);                                                        \
194   uint32_t Val;                                                                \
195   READ_NUM((*Line), (Val));                                                    \
196   Line++;
197 
198   if (Line.is_at_end())
199     return success();
200 
201   uint32_t NumValueKinds;
202   if (Line->getAsInteger(10, NumValueKinds)) {
203     // No value profile data
204     return success();
205   }
206   if (NumValueKinds == 0 || NumValueKinds > IPVK_Last + 1)
207     return error(instrprof_error::malformed);
208   Line++;
209 
210   for (uint32_t VK = 0; VK < NumValueKinds; VK++) {
211     VP_READ_ADVANCE(ValueKind);
212     if (ValueKind > IPVK_Last)
213       return error(instrprof_error::malformed);
214     VP_READ_ADVANCE(NumValueSites);
215     if (!NumValueSites)
216       continue;
217 
218     Record.reserveSites(VK, NumValueSites);
219     for (uint32_t S = 0; S < NumValueSites; S++) {
220       VP_READ_ADVANCE(NumValueData);
221 
222       std::vector<InstrProfValueData> CurrentValues;
223       for (uint32_t V = 0; V < NumValueData; V++) {
224         CHECK_LINE_END(Line);
225         std::pair<StringRef, StringRef> VD = Line->rsplit(':');
226         uint64_t TakenCount, Value;
227         if (ValueKind == IPVK_IndirectCallTarget) {
228           if (InstrProfSymtab::isExternalSymbol(VD.first)) {
229             Value = 0;
230           } else {
231             if (Error E = Symtab->addFuncName(VD.first))
232               return E;
233             Value = IndexedInstrProf::ComputeHash(VD.first);
234           }
235         } else {
236           READ_NUM(VD.first, Value);
237         }
238         READ_NUM(VD.second, TakenCount);
239         CurrentValues.push_back({Value, TakenCount});
240         Line++;
241       }
242       Record.addValueData(ValueKind, S, CurrentValues.data(), NumValueData,
243                           nullptr);
244     }
245   }
246   return success();
247 
248 #undef CHECK_LINE_END
249 #undef READ_NUM
250 #undef VP_READ_ADVANCE
251 }
252 
readNextRecord(NamedInstrProfRecord & Record)253 Error TextInstrProfReader::readNextRecord(NamedInstrProfRecord &Record) {
254   // Skip empty lines and comments.
255   while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
256     ++Line;
257   // If we hit EOF while looking for a name, we're done.
258   if (Line.is_at_end()) {
259     return error(instrprof_error::eof);
260   }
261 
262   // Read the function name.
263   Record.Name = *Line++;
264   if (Error E = Symtab->addFuncName(Record.Name))
265     return error(std::move(E));
266 
267   // Read the function hash.
268   if (Line.is_at_end())
269     return error(instrprof_error::truncated);
270   if ((Line++)->getAsInteger(0, Record.Hash))
271     return error(instrprof_error::malformed);
272 
273   // Read the number of counters.
274   uint64_t NumCounters;
275   if (Line.is_at_end())
276     return error(instrprof_error::truncated);
277   if ((Line++)->getAsInteger(10, NumCounters))
278     return error(instrprof_error::malformed);
279   if (NumCounters == 0)
280     return error(instrprof_error::malformed);
281 
282   // Read each counter and fill our internal storage with the values.
283   Record.Clear();
284   Record.Counts.reserve(NumCounters);
285   for (uint64_t I = 0; I < NumCounters; ++I) {
286     if (Line.is_at_end())
287       return error(instrprof_error::truncated);
288     uint64_t Count;
289     if ((Line++)->getAsInteger(10, Count))
290       return error(instrprof_error::malformed);
291     Record.Counts.push_back(Count);
292   }
293 
294   // Check if value profile data exists and read it if so.
295   if (Error E = readValueProfileData(Record))
296     return error(std::move(E));
297 
298   return success();
299 }
300 
301 template <class IntPtrT>
hasFormat(const MemoryBuffer & DataBuffer)302 bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
303   if (DataBuffer.getBufferSize() < sizeof(uint64_t))
304     return false;
305   uint64_t Magic =
306     *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
307   return RawInstrProf::getMagic<IntPtrT>() == Magic ||
308          sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic;
309 }
310 
311 template <class IntPtrT>
readHeader()312 Error RawInstrProfReader<IntPtrT>::readHeader() {
313   if (!hasFormat(*DataBuffer))
314     return error(instrprof_error::bad_magic);
315   if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header))
316     return error(instrprof_error::bad_header);
317   auto *Header = reinterpret_cast<const RawInstrProf::Header *>(
318       DataBuffer->getBufferStart());
319   ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>();
320   return readHeader(*Header);
321 }
322 
323 template <class IntPtrT>
readNextHeader(const char * CurrentPos)324 Error RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
325   const char *End = DataBuffer->getBufferEnd();
326   // Skip zero padding between profiles.
327   while (CurrentPos != End && *CurrentPos == 0)
328     ++CurrentPos;
329   // If there's nothing left, we're done.
330   if (CurrentPos == End)
331     return make_error<InstrProfError>(instrprof_error::eof);
332   // If there isn't enough space for another header, this is probably just
333   // garbage at the end of the file.
334   if (CurrentPos + sizeof(RawInstrProf::Header) > End)
335     return make_error<InstrProfError>(instrprof_error::malformed);
336   // The writer ensures each profile is padded to start at an aligned address.
337   if (reinterpret_cast<size_t>(CurrentPos) % alignof(uint64_t))
338     return make_error<InstrProfError>(instrprof_error::malformed);
339   // The magic should have the same byte order as in the previous header.
340   uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
341   if (Magic != swap(RawInstrProf::getMagic<IntPtrT>()))
342     return make_error<InstrProfError>(instrprof_error::bad_magic);
343 
344   // There's another profile to read, so we need to process the header.
345   auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos);
346   return readHeader(*Header);
347 }
348 
349 template <class IntPtrT>
createSymtab(InstrProfSymtab & Symtab)350 Error RawInstrProfReader<IntPtrT>::createSymtab(InstrProfSymtab &Symtab) {
351   if (Error E = Symtab.create(StringRef(NamesStart, NamesSize)))
352     return error(std::move(E));
353   for (const RawInstrProf::ProfileData<IntPtrT> *I = Data; I != DataEnd; ++I) {
354     const IntPtrT FPtr = swap(I->FunctionPointer);
355     if (!FPtr)
356       continue;
357     Symtab.mapAddress(FPtr, I->NameRef);
358   }
359   return success();
360 }
361 
362 template <class IntPtrT>
readHeader(const RawInstrProf::Header & Header)363 Error RawInstrProfReader<IntPtrT>::readHeader(
364     const RawInstrProf::Header &Header) {
365   Version = swap(Header.Version);
366   if (GET_VERSION(Version) != RawInstrProf::Version)
367     return error(instrprof_error::unsupported_version);
368 
369   CountersDelta = swap(Header.CountersDelta);
370   NamesDelta = swap(Header.NamesDelta);
371   auto DataSize = swap(Header.DataSize);
372   auto PaddingBytesBeforeCounters = swap(Header.PaddingBytesBeforeCounters);
373   auto CountersSize = swap(Header.CountersSize);
374   auto PaddingBytesAfterCounters = swap(Header.PaddingBytesAfterCounters);
375   NamesSize = swap(Header.NamesSize);
376   ValueKindLast = swap(Header.ValueKindLast);
377 
378   auto DataSizeInBytes = DataSize * sizeof(RawInstrProf::ProfileData<IntPtrT>);
379   auto PaddingSize = getNumPaddingBytes(NamesSize);
380 
381   ptrdiff_t DataOffset = sizeof(RawInstrProf::Header);
382   ptrdiff_t CountersOffset =
383       DataOffset + DataSizeInBytes + PaddingBytesBeforeCounters;
384   ptrdiff_t NamesOffset = CountersOffset + (sizeof(uint64_t) * CountersSize) +
385                           PaddingBytesAfterCounters;
386   ptrdiff_t ValueDataOffset = NamesOffset + NamesSize + PaddingSize;
387 
388   auto *Start = reinterpret_cast<const char *>(&Header);
389   if (Start + ValueDataOffset > DataBuffer->getBufferEnd())
390     return error(instrprof_error::bad_header);
391 
392   Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>(
393       Start + DataOffset);
394   DataEnd = Data + DataSize;
395   CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
396   NamesStart = Start + NamesOffset;
397   ValueDataStart = reinterpret_cast<const uint8_t *>(Start + ValueDataOffset);
398 
399   std::unique_ptr<InstrProfSymtab> NewSymtab = std::make_unique<InstrProfSymtab>();
400   if (Error E = createSymtab(*NewSymtab.get()))
401     return E;
402 
403   Symtab = std::move(NewSymtab);
404   return success();
405 }
406 
407 template <class IntPtrT>
readName(NamedInstrProfRecord & Record)408 Error RawInstrProfReader<IntPtrT>::readName(NamedInstrProfRecord &Record) {
409   Record.Name = getName(Data->NameRef);
410   return success();
411 }
412 
413 template <class IntPtrT>
readFuncHash(NamedInstrProfRecord & Record)414 Error RawInstrProfReader<IntPtrT>::readFuncHash(NamedInstrProfRecord &Record) {
415   Record.Hash = swap(Data->FuncHash);
416   return success();
417 }
418 
419 template <class IntPtrT>
readRawCounts(InstrProfRecord & Record)420 Error RawInstrProfReader<IntPtrT>::readRawCounts(
421     InstrProfRecord &Record) {
422   uint32_t NumCounters = swap(Data->NumCounters);
423   IntPtrT CounterPtr = Data->CounterPtr;
424   if (NumCounters == 0)
425     return error(instrprof_error::malformed);
426 
427   auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
428   ptrdiff_t MaxNumCounters = NamesStartAsCounter - CountersStart;
429 
430   // Check bounds. Note that the counter pointer embedded in the data record
431   // may itself be corrupt.
432   if (MaxNumCounters < 0 || NumCounters > (uint32_t)MaxNumCounters)
433     return error(instrprof_error::malformed);
434   ptrdiff_t CounterOffset = getCounterOffset(CounterPtr);
435   if (CounterOffset < 0 || CounterOffset > MaxNumCounters ||
436       ((uint32_t)CounterOffset + NumCounters) > (uint32_t)MaxNumCounters)
437     return error(instrprof_error::malformed);
438 
439   auto RawCounts = makeArrayRef(getCounter(CounterOffset), NumCounters);
440 
441   if (ShouldSwapBytes) {
442     Record.Counts.clear();
443     Record.Counts.reserve(RawCounts.size());
444     for (uint64_t Count : RawCounts)
445       Record.Counts.push_back(swap(Count));
446   } else
447     Record.Counts = RawCounts;
448 
449   return success();
450 }
451 
452 template <class IntPtrT>
readValueProfilingData(InstrProfRecord & Record)453 Error RawInstrProfReader<IntPtrT>::readValueProfilingData(
454     InstrProfRecord &Record) {
455   Record.clearValueData();
456   CurValueDataSize = 0;
457   // Need to match the logic in value profile dumper code in compiler-rt:
458   uint32_t NumValueKinds = 0;
459   for (uint32_t I = 0; I < IPVK_Last + 1; I++)
460     NumValueKinds += (Data->NumValueSites[I] != 0);
461 
462   if (!NumValueKinds)
463     return success();
464 
465   Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
466       ValueProfData::getValueProfData(
467           ValueDataStart, (const unsigned char *)DataBuffer->getBufferEnd(),
468           getDataEndianness());
469 
470   if (Error E = VDataPtrOrErr.takeError())
471     return E;
472 
473   // Note that besides deserialization, this also performs the conversion for
474   // indirect call targets.  The function pointers from the raw profile are
475   // remapped into function name hashes.
476   VDataPtrOrErr.get()->deserializeTo(Record, Symtab.get());
477   CurValueDataSize = VDataPtrOrErr.get()->getSize();
478   return success();
479 }
480 
481 template <class IntPtrT>
readNextRecord(NamedInstrProfRecord & Record)482 Error RawInstrProfReader<IntPtrT>::readNextRecord(NamedInstrProfRecord &Record) {
483   if (atEnd())
484     // At this point, ValueDataStart field points to the next header.
485     if (Error E = readNextHeader(getNextHeaderPos()))
486       return error(std::move(E));
487 
488   // Read name ad set it in Record.
489   if (Error E = readName(Record))
490     return error(std::move(E));
491 
492   // Read FuncHash and set it in Record.
493   if (Error E = readFuncHash(Record))
494     return error(std::move(E));
495 
496   // Read raw counts and set Record.
497   if (Error E = readRawCounts(Record))
498     return error(std::move(E));
499 
500   // Read value data and set Record.
501   if (Error E = readValueProfilingData(Record))
502     return error(std::move(E));
503 
504   // Iterate.
505   advanceData();
506   return success();
507 }
508 
509 namespace llvm {
510 
511 template class RawInstrProfReader<uint32_t>;
512 template class RawInstrProfReader<uint64_t>;
513 
514 } // end namespace llvm
515 
516 InstrProfLookupTrait::hash_value_type
ComputeHash(StringRef K)517 InstrProfLookupTrait::ComputeHash(StringRef K) {
518   return IndexedInstrProf::ComputeHash(HashType, K);
519 }
520 
521 using data_type = InstrProfLookupTrait::data_type;
522 using offset_type = InstrProfLookupTrait::offset_type;
523 
readValueProfilingData(const unsigned char * & D,const unsigned char * const End)524 bool InstrProfLookupTrait::readValueProfilingData(
525     const unsigned char *&D, const unsigned char *const End) {
526   Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
527       ValueProfData::getValueProfData(D, End, ValueProfDataEndianness);
528 
529   if (VDataPtrOrErr.takeError())
530     return false;
531 
532   VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr);
533   D += VDataPtrOrErr.get()->TotalSize;
534 
535   return true;
536 }
537 
ReadData(StringRef K,const unsigned char * D,offset_type N)538 data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D,
539                                          offset_type N) {
540   using namespace support;
541 
542   // Check if the data is corrupt. If so, don't try to read it.
543   if (N % sizeof(uint64_t))
544     return data_type();
545 
546   DataBuffer.clear();
547   std::vector<uint64_t> CounterBuffer;
548 
549   const unsigned char *End = D + N;
550   while (D < End) {
551     // Read hash.
552     if (D + sizeof(uint64_t) >= End)
553       return data_type();
554     uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
555 
556     // Initialize number of counters for GET_VERSION(FormatVersion) == 1.
557     uint64_t CountsSize = N / sizeof(uint64_t) - 1;
558     // If format version is different then read the number of counters.
559     if (GET_VERSION(FormatVersion) != IndexedInstrProf::ProfVersion::Version1) {
560       if (D + sizeof(uint64_t) > End)
561         return data_type();
562       CountsSize = endian::readNext<uint64_t, little, unaligned>(D);
563     }
564     // Read counter values.
565     if (D + CountsSize * sizeof(uint64_t) > End)
566       return data_type();
567 
568     CounterBuffer.clear();
569     CounterBuffer.reserve(CountsSize);
570     for (uint64_t J = 0; J < CountsSize; ++J)
571       CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
572 
573     DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer));
574 
575     // Read value profiling data.
576     if (GET_VERSION(FormatVersion) > IndexedInstrProf::ProfVersion::Version2 &&
577         !readValueProfilingData(D, End)) {
578       DataBuffer.clear();
579       return data_type();
580     }
581   }
582   return DataBuffer;
583 }
584 
585 template <typename HashTableImpl>
getRecords(StringRef FuncName,ArrayRef<NamedInstrProfRecord> & Data)586 Error InstrProfReaderIndex<HashTableImpl>::getRecords(
587     StringRef FuncName, ArrayRef<NamedInstrProfRecord> &Data) {
588   auto Iter = HashTable->find(FuncName);
589   if (Iter == HashTable->end())
590     return make_error<InstrProfError>(instrprof_error::unknown_function);
591 
592   Data = (*Iter);
593   if (Data.empty())
594     return make_error<InstrProfError>(instrprof_error::malformed);
595 
596   return Error::success();
597 }
598 
599 template <typename HashTableImpl>
getRecords(ArrayRef<NamedInstrProfRecord> & Data)600 Error InstrProfReaderIndex<HashTableImpl>::getRecords(
601     ArrayRef<NamedInstrProfRecord> &Data) {
602   if (atEnd())
603     return make_error<InstrProfError>(instrprof_error::eof);
604 
605   Data = *RecordIterator;
606 
607   if (Data.empty())
608     return make_error<InstrProfError>(instrprof_error::malformed);
609 
610   return Error::success();
611 }
612 
613 template <typename HashTableImpl>
InstrProfReaderIndex(const unsigned char * Buckets,const unsigned char * const Payload,const unsigned char * const Base,IndexedInstrProf::HashT HashType,uint64_t Version)614 InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex(
615     const unsigned char *Buckets, const unsigned char *const Payload,
616     const unsigned char *const Base, IndexedInstrProf::HashT HashType,
617     uint64_t Version) {
618   FormatVersion = Version;
619   HashTable.reset(HashTableImpl::Create(
620       Buckets, Payload, Base,
621       typename HashTableImpl::InfoType(HashType, Version)));
622   RecordIterator = HashTable->data_begin();
623 }
624 
625 namespace {
626 /// A remapper that does not apply any remappings.
627 class InstrProfReaderNullRemapper : public InstrProfReaderRemapper {
628   InstrProfReaderIndexBase &Underlying;
629 
630 public:
InstrProfReaderNullRemapper(InstrProfReaderIndexBase & Underlying)631   InstrProfReaderNullRemapper(InstrProfReaderIndexBase &Underlying)
632       : Underlying(Underlying) {}
633 
getRecords(StringRef FuncName,ArrayRef<NamedInstrProfRecord> & Data)634   Error getRecords(StringRef FuncName,
635                    ArrayRef<NamedInstrProfRecord> &Data) override {
636     return Underlying.getRecords(FuncName, Data);
637   }
638 };
639 }
640 
641 /// A remapper that applies remappings based on a symbol remapping file.
642 template <typename HashTableImpl>
643 class llvm::InstrProfReaderItaniumRemapper
644     : public InstrProfReaderRemapper {
645 public:
InstrProfReaderItaniumRemapper(std::unique_ptr<MemoryBuffer> RemapBuffer,InstrProfReaderIndex<HashTableImpl> & Underlying)646   InstrProfReaderItaniumRemapper(
647       std::unique_ptr<MemoryBuffer> RemapBuffer,
648       InstrProfReaderIndex<HashTableImpl> &Underlying)
649       : RemapBuffer(std::move(RemapBuffer)), Underlying(Underlying) {
650   }
651 
652   /// Extract the original function name from a PGO function name.
extractName(StringRef Name)653   static StringRef extractName(StringRef Name) {
654     // We can have multiple :-separated pieces; there can be pieces both
655     // before and after the mangled name. Find the first part that starts
656     // with '_Z'; we'll assume that's the mangled name we want.
657     std::pair<StringRef, StringRef> Parts = {StringRef(), Name};
658     while (true) {
659       Parts = Parts.second.split(':');
660       if (Parts.first.startswith("_Z"))
661         return Parts.first;
662       if (Parts.second.empty())
663         return Name;
664     }
665   }
666 
667   /// Given a mangled name extracted from a PGO function name, and a new
668   /// form for that mangled name, reconstitute the name.
reconstituteName(StringRef OrigName,StringRef ExtractedName,StringRef Replacement,SmallVectorImpl<char> & Out)669   static void reconstituteName(StringRef OrigName, StringRef ExtractedName,
670                                StringRef Replacement,
671                                SmallVectorImpl<char> &Out) {
672     Out.reserve(OrigName.size() + Replacement.size() - ExtractedName.size());
673     Out.insert(Out.end(), OrigName.begin(), ExtractedName.begin());
674     Out.insert(Out.end(), Replacement.begin(), Replacement.end());
675     Out.insert(Out.end(), ExtractedName.end(), OrigName.end());
676   }
677 
populateRemappings()678   Error populateRemappings() override {
679     if (Error E = Remappings.read(*RemapBuffer))
680       return E;
681     for (StringRef Name : Underlying.HashTable->keys()) {
682       StringRef RealName = extractName(Name);
683       if (auto Key = Remappings.insert(RealName)) {
684         // FIXME: We could theoretically map the same equivalence class to
685         // multiple names in the profile data. If that happens, we should
686         // return NamedInstrProfRecords from all of them.
687         MappedNames.insert({Key, RealName});
688       }
689     }
690     return Error::success();
691   }
692 
getRecords(StringRef FuncName,ArrayRef<NamedInstrProfRecord> & Data)693   Error getRecords(StringRef FuncName,
694                    ArrayRef<NamedInstrProfRecord> &Data) override {
695     StringRef RealName = extractName(FuncName);
696     if (auto Key = Remappings.lookup(RealName)) {
697       StringRef Remapped = MappedNames.lookup(Key);
698       if (!Remapped.empty()) {
699         if (RealName.begin() == FuncName.begin() &&
700             RealName.end() == FuncName.end())
701           FuncName = Remapped;
702         else {
703           // Try rebuilding the name from the given remapping.
704           SmallString<256> Reconstituted;
705           reconstituteName(FuncName, RealName, Remapped, Reconstituted);
706           Error E = Underlying.getRecords(Reconstituted, Data);
707           if (!E)
708             return E;
709 
710           // If we failed because the name doesn't exist, fall back to asking
711           // about the original name.
712           if (Error Unhandled = handleErrors(
713                   std::move(E), [](std::unique_ptr<InstrProfError> Err) {
714                     return Err->get() == instrprof_error::unknown_function
715                                ? Error::success()
716                                : Error(std::move(Err));
717                   }))
718             return Unhandled;
719         }
720       }
721     }
722     return Underlying.getRecords(FuncName, Data);
723   }
724 
725 private:
726   /// The memory buffer containing the remapping configuration. Remappings
727   /// holds pointers into this buffer.
728   std::unique_ptr<MemoryBuffer> RemapBuffer;
729 
730   /// The mangling remapper.
731   SymbolRemappingReader Remappings;
732 
733   /// Mapping from mangled name keys to the name used for the key in the
734   /// profile data.
735   /// FIXME: Can we store a location within the on-disk hash table instead of
736   /// redoing lookup?
737   DenseMap<SymbolRemappingReader::Key, StringRef> MappedNames;
738 
739   /// The real profile data reader.
740   InstrProfReaderIndex<HashTableImpl> &Underlying;
741 };
742 
hasFormat(const MemoryBuffer & DataBuffer)743 bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
744   using namespace support;
745 
746   if (DataBuffer.getBufferSize() < 8)
747     return false;
748   uint64_t Magic =
749       endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
750   // Verify that it's magical.
751   return Magic == IndexedInstrProf::Magic;
752 }
753 
754 const unsigned char *
readSummary(IndexedInstrProf::ProfVersion Version,const unsigned char * Cur,bool UseCS)755 IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version,
756                                     const unsigned char *Cur, bool UseCS) {
757   using namespace IndexedInstrProf;
758   using namespace support;
759 
760   if (Version >= IndexedInstrProf::Version4) {
761     const IndexedInstrProf::Summary *SummaryInLE =
762         reinterpret_cast<const IndexedInstrProf::Summary *>(Cur);
763     uint64_t NFields =
764         endian::byte_swap<uint64_t, little>(SummaryInLE->NumSummaryFields);
765     uint64_t NEntries =
766         endian::byte_swap<uint64_t, little>(SummaryInLE->NumCutoffEntries);
767     uint32_t SummarySize =
768         IndexedInstrProf::Summary::getSize(NFields, NEntries);
769     std::unique_ptr<IndexedInstrProf::Summary> SummaryData =
770         IndexedInstrProf::allocSummary(SummarySize);
771 
772     const uint64_t *Src = reinterpret_cast<const uint64_t *>(SummaryInLE);
773     uint64_t *Dst = reinterpret_cast<uint64_t *>(SummaryData.get());
774     for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
775       Dst[I] = endian::byte_swap<uint64_t, little>(Src[I]);
776 
777     SummaryEntryVector DetailedSummary;
778     for (unsigned I = 0; I < SummaryData->NumCutoffEntries; I++) {
779       const IndexedInstrProf::Summary::Entry &Ent = SummaryData->getEntry(I);
780       DetailedSummary.emplace_back((uint32_t)Ent.Cutoff, Ent.MinBlockCount,
781                                    Ent.NumBlocks);
782     }
783     std::unique_ptr<llvm::ProfileSummary> &Summary =
784         UseCS ? this->CS_Summary : this->Summary;
785 
786     // initialize InstrProfSummary using the SummaryData from disk.
787     Summary = std::make_unique<ProfileSummary>(
788         UseCS ? ProfileSummary::PSK_CSInstr : ProfileSummary::PSK_Instr,
789         DetailedSummary, SummaryData->get(Summary::TotalBlockCount),
790         SummaryData->get(Summary::MaxBlockCount),
791         SummaryData->get(Summary::MaxInternalBlockCount),
792         SummaryData->get(Summary::MaxFunctionCount),
793         SummaryData->get(Summary::TotalNumBlocks),
794         SummaryData->get(Summary::TotalNumFunctions));
795     return Cur + SummarySize;
796   } else {
797     // The older versions do not support a profile summary. This just computes
798     // an empty summary, which will not result in accurate hot/cold detection.
799     // We would need to call addRecord for all NamedInstrProfRecords to get the
800     // correct summary. However, this version is old (prior to early 2016) and
801     // has not been supporting an accurate summary for several years.
802     InstrProfSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
803     Summary = Builder.getSummary();
804     return Cur;
805   }
806 }
807 
readHeader()808 Error IndexedInstrProfReader::readHeader() {
809   using namespace support;
810 
811   const unsigned char *Start =
812       (const unsigned char *)DataBuffer->getBufferStart();
813   const unsigned char *Cur = Start;
814   if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
815     return error(instrprof_error::truncated);
816 
817   auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur);
818   Cur += sizeof(IndexedInstrProf::Header);
819 
820   // Check the magic number.
821   uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic);
822   if (Magic != IndexedInstrProf::Magic)
823     return error(instrprof_error::bad_magic);
824 
825   // Read the version.
826   uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version);
827   if (GET_VERSION(FormatVersion) >
828       IndexedInstrProf::ProfVersion::CurrentVersion)
829     return error(instrprof_error::unsupported_version);
830 
831   Cur = readSummary((IndexedInstrProf::ProfVersion)FormatVersion, Cur,
832                     /* UseCS */ false);
833   if (FormatVersion & VARIANT_MASK_CSIR_PROF)
834     Cur = readSummary((IndexedInstrProf::ProfVersion)FormatVersion, Cur,
835                       /* UseCS */ true);
836 
837   // Read the hash type and start offset.
838   IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
839       endian::byte_swap<uint64_t, little>(Header->HashType));
840   if (HashType > IndexedInstrProf::HashT::Last)
841     return error(instrprof_error::unsupported_hash_type);
842 
843   uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
844 
845   // The rest of the file is an on disk hash table.
846   auto IndexPtr =
847       std::make_unique<InstrProfReaderIndex<OnDiskHashTableImplV3>>(
848           Start + HashOffset, Cur, Start, HashType, FormatVersion);
849 
850   // Load the remapping table now if requested.
851   if (RemappingBuffer) {
852     Remapper = std::make_unique<
853         InstrProfReaderItaniumRemapper<OnDiskHashTableImplV3>>(
854         std::move(RemappingBuffer), *IndexPtr);
855     if (Error E = Remapper->populateRemappings())
856       return E;
857   } else {
858     Remapper = std::make_unique<InstrProfReaderNullRemapper>(*IndexPtr);
859   }
860   Index = std::move(IndexPtr);
861 
862   return success();
863 }
864 
getSymtab()865 InstrProfSymtab &IndexedInstrProfReader::getSymtab() {
866   if (Symtab.get())
867     return *Symtab.get();
868 
869   std::unique_ptr<InstrProfSymtab> NewSymtab = std::make_unique<InstrProfSymtab>();
870   if (Error E = Index->populateSymtab(*NewSymtab.get())) {
871     consumeError(error(InstrProfError::take(std::move(E))));
872   }
873 
874   Symtab = std::move(NewSymtab);
875   return *Symtab.get();
876 }
877 
878 Expected<InstrProfRecord>
getInstrProfRecord(StringRef FuncName,uint64_t FuncHash)879 IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
880                                            uint64_t FuncHash) {
881   ArrayRef<NamedInstrProfRecord> Data;
882   Error Err = Remapper->getRecords(FuncName, Data);
883   if (Err)
884     return std::move(Err);
885   // Found it. Look for counters with the right hash.
886   for (unsigned I = 0, E = Data.size(); I < E; ++I) {
887     // Check for a match and fill the vector if there is one.
888     if (Data[I].Hash == FuncHash) {
889       return std::move(Data[I]);
890     }
891   }
892   return error(instrprof_error::hash_mismatch);
893 }
894 
getFunctionCounts(StringRef FuncName,uint64_t FuncHash,std::vector<uint64_t> & Counts)895 Error IndexedInstrProfReader::getFunctionCounts(StringRef FuncName,
896                                                 uint64_t FuncHash,
897                                                 std::vector<uint64_t> &Counts) {
898   Expected<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash);
899   if (Error E = Record.takeError())
900     return error(std::move(E));
901 
902   Counts = Record.get().Counts;
903   return success();
904 }
905 
readNextRecord(NamedInstrProfRecord & Record)906 Error IndexedInstrProfReader::readNextRecord(NamedInstrProfRecord &Record) {
907   ArrayRef<NamedInstrProfRecord> Data;
908 
909   Error E = Index->getRecords(Data);
910   if (E)
911     return error(std::move(E));
912 
913   Record = Data[RecordIndex++];
914   if (RecordIndex >= Data.size()) {
915     Index->advanceToNextKey();
916     RecordIndex = 0;
917   }
918   return success();
919 }
920 
accumulateCounts(CountSumOrPercent & Sum,bool IsCS)921 void InstrProfReader::accumulateCounts(CountSumOrPercent &Sum, bool IsCS) {
922   uint64_t NumFuncs = 0;
923   for (const auto &Func : *this) {
924     if (isIRLevelProfile()) {
925       bool FuncIsCS = NamedInstrProfRecord::hasCSFlagInHash(Func.Hash);
926       if (FuncIsCS != IsCS)
927         continue;
928     }
929     Func.accumulateCounts(Sum);
930     ++NumFuncs;
931   }
932   Sum.NumEntries = NumFuncs;
933 }
934