blob: 394893d3701c2b9a1b7d8c4de51b5ac65d8a34d2 [file] [log] [blame]
Justin Bognerf8d79192014-03-21 17:24:48 +00001//=-- 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"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000017#include <cassert>
18
19using namespace llvm;
20
Diego Novillofcd55602014-11-03 00:51:45 +000021static ErrorOr<std::unique_ptr<MemoryBuffer>>
22setupMemoryBuffer(std::string Path) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +000023 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
24 MemoryBuffer::getFileOrSTDIN(Path);
25 if (std::error_code EC = BufferOrErr.getError())
Justin Bognerf8d79192014-03-21 17:24:48 +000026 return EC;
Justin Bogner2b6c5372015-02-18 01:58:17 +000027 return std::move(BufferOrErr.get());
Justin Bognerb7aa2632014-04-18 21:48:40 +000028}
29
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000030static std::error_code initializeReader(InstrProfReader &Reader) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000031 return Reader.readHeader();
32}
33
Diego Novillofcd55602014-11-03 00:51:45 +000034ErrorOr<std::unique_ptr<InstrProfReader>>
35InstrProfReader::create(std::string Path) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000036 // Set up the buffer to read.
Diego Novillofcd55602014-11-03 00:51:45 +000037 auto BufferOrError = setupMemoryBuffer(Path);
38 if (std::error_code EC = BufferOrError.getError())
Justin Bognerb7aa2632014-04-18 21:48:40 +000039 return EC;
Justin Bogner2b6c5372015-02-18 01:58:17 +000040 return InstrProfReader::create(std::move(BufferOrError.get()));
41}
Justin Bognerf8d79192014-03-21 17:24:48 +000042
Justin Bogner2b6c5372015-02-18 01:58:17 +000043ErrorOr<std::unique_ptr<InstrProfReader>>
44InstrProfReader::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
Diego Novillofcd55602014-11-03 00:51:45 +000049 std::unique_ptr<InstrProfReader> Result;
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000050 // Create the reader.
Justin Bognerb7aa2632014-04-18 21:48:40 +000051 if (IndexedInstrProfReader::hasFormat(*Buffer))
52 Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
53 else if (RawInstrProfReader64::hasFormat(*Buffer))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +000054 Result.reset(new RawInstrProfReader64(std::move(Buffer)));
55 else if (RawInstrProfReader32::hasFormat(*Buffer))
56 Result.reset(new RawInstrProfReader32(std::move(Buffer)));
Nathan Slingerland4f823662015-11-13 03:47:58 +000057 else if (TextInstrProfReader::hasFormat(*Buffer))
Nathan Slingerland911ced62015-11-12 18:39:26 +000058 Result.reset(new TextInstrProfReader(std::move(Buffer)));
Nathan Slingerland4f823662015-11-13 03:47:58 +000059 else
60 return instrprof_error::unrecognized_format;
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000061
Justin Bognerb7aa2632014-04-18 21:48:40 +000062 // Initialize the reader and return the result.
Diego Novillofcd55602014-11-03 00:51:45 +000063 if (std::error_code EC = initializeReader(*Result))
64 return EC;
65
66 return std::move(Result);
Justin Bognerb7aa2632014-04-18 21:48:40 +000067}
68
Justin Bognerab89ed72015-02-16 21:28:58 +000069ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
70IndexedInstrProfReader::create(std::string Path) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000071 // Set up the buffer to read.
Diego Novillofcd55602014-11-03 00:51:45 +000072 auto BufferOrError = setupMemoryBuffer(Path);
73 if (std::error_code EC = BufferOrError.getError())
Justin Bognerb7aa2632014-04-18 21:48:40 +000074 return EC;
Justin Bogner2b6c5372015-02-18 01:58:17 +000075 return IndexedInstrProfReader::create(std::move(BufferOrError.get()));
76}
Justin Bognerb7aa2632014-04-18 21:48:40 +000077
Justin Bogner2b6c5372015-02-18 01:58:17 +000078
79ErrorOr<std::unique_ptr<IndexedInstrProfReader>>
80IndexedInstrProfReader::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;
Justin Bognerab89ed72015-02-16 21:28:58 +000084
Justin Bognerb7aa2632014-04-18 21:48:40 +000085 // Create the reader.
86 if (!IndexedInstrProfReader::hasFormat(*Buffer))
87 return instrprof_error::bad_magic;
Justin Bogner2b6c5372015-02-18 01:58:17 +000088 auto Result = llvm::make_unique<IndexedInstrProfReader>(std::move(Buffer));
Justin Bognerb7aa2632014-04-18 21:48:40 +000089
90 // Initialize the reader and return the result.
Justin Bognerab89ed72015-02-16 21:28:58 +000091 if (std::error_code EC = initializeReader(*Result))
92 return EC;
93
94 return std::move(Result);
Justin Bognerf8d79192014-03-21 17:24:48 +000095}
96
97void InstrProfIterator::Increment() {
98 if (Reader->readNextRecord(Record))
99 *this = InstrProfIterator();
100}
101
Nathan Slingerland4f823662015-11-13 03:47:58 +0000102bool 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();
Vedant Kumar2491dd12015-12-11 00:40:05 +0000107 return count == 0 ||
108 std::all_of(buffer.begin(), buffer.begin() + count,
109 [](char c) { return ::isprint(c) || ::isspace(c); });
Nathan Slingerland4f823662015-11-13 03:47:58 +0000110}
111
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000112std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
Justin Bognercf36a362014-07-29 22:29:23 +0000113 // Skip empty lines and comments.
114 while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
Justin Bognerf8d79192014-03-21 17:24:48 +0000115 ++Line;
116 // If we hit EOF while looking for a name, we're done.
117 if (Line.is_at_end())
118 return error(instrprof_error::eof);
119
120 // Read the function name.
121 Record.Name = *Line++;
122
123 // Read the function hash.
124 if (Line.is_at_end())
125 return error(instrprof_error::truncated);
Justin Bognerf95ca072015-03-09 18:54:49 +0000126 if ((Line++)->getAsInteger(0, Record.Hash))
Justin Bognerf8d79192014-03-21 17:24:48 +0000127 return error(instrprof_error::malformed);
128
129 // Read the number of counters.
130 uint64_t NumCounters;
131 if (Line.is_at_end())
132 return error(instrprof_error::truncated);
133 if ((Line++)->getAsInteger(10, NumCounters))
134 return error(instrprof_error::malformed);
Justin Bognerb59d7c72014-04-25 02:45:33 +0000135 if (NumCounters == 0)
136 return error(instrprof_error::malformed);
Justin Bognerf8d79192014-03-21 17:24:48 +0000137
138 // Read each counter and fill our internal storage with the values.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000139 Record.Counts.clear();
140 Record.Counts.reserve(NumCounters);
Justin Bognerf8d79192014-03-21 17:24:48 +0000141 for (uint64_t I = 0; I < NumCounters; ++I) {
142 if (Line.is_at_end())
143 return error(instrprof_error::truncated);
144 uint64_t Count;
145 if ((Line++)->getAsInteger(10, Count))
146 return error(instrprof_error::malformed);
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000147 Record.Counts.push_back(Count);
Justin Bognerf8d79192014-03-21 17:24:48 +0000148 }
Justin Bognerf8d79192014-03-21 17:24:48 +0000149
150 return success();
151}
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000152
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000153template <class IntPtrT>
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000154bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000155 if (DataBuffer.getBufferSize() < sizeof(uint64_t))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000156 return false;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000157 uint64_t Magic =
158 *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000159 return RawInstrProf::getMagic<IntPtrT>() == Magic ||
160 sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic;
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000161}
162
163template <class IntPtrT>
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000164std::error_code RawInstrProfReader<IntPtrT>::readHeader() {
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000165 if (!hasFormat(*DataBuffer))
166 return error(instrprof_error::bad_magic);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000167 if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header))
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000168 return error(instrprof_error::bad_header);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000169 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(
170 DataBuffer->getBufferStart());
171 ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000172 return readHeader(*Header);
173}
174
Justin Bognera119f322014-05-16 00:38:00 +0000175template <class IntPtrT>
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000176std::error_code
177RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
Justin Bognera119f322014-05-16 00:38:00 +0000178 const char *End = DataBuffer->getBufferEnd();
179 // Skip zero padding between profiles.
180 while (CurrentPos != End && *CurrentPos == 0)
181 ++CurrentPos;
182 // If there's nothing left, we're done.
183 if (CurrentPos == End)
184 return instrprof_error::eof;
185 // If there isn't enough space for another header, this is probably just
186 // garbage at the end of the file.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000187 if (CurrentPos + sizeof(RawInstrProf::Header) > End)
Justin Bognera119f322014-05-16 00:38:00 +0000188 return instrprof_error::malformed;
Justin Bogner54b11282014-09-12 21:22:55 +0000189 // The writer ensures each profile is padded to start at an aligned address.
190 if (reinterpret_cast<size_t>(CurrentPos) % alignOf<uint64_t>())
191 return instrprof_error::malformed;
Justin Bognera119f322014-05-16 00:38:00 +0000192 // The magic should have the same byte order as in the previous header.
193 uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000194 if (Magic != swap(RawInstrProf::getMagic<IntPtrT>()))
Justin Bognera119f322014-05-16 00:38:00 +0000195 return instrprof_error::bad_magic;
196
197 // There's another profile to read, so we need to process the header.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000198 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos);
Justin Bognera119f322014-05-16 00:38:00 +0000199 return readHeader(*Header);
200}
201
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000202template <class IntPtrT>
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000203std::error_code RawInstrProfReader<IntPtrT>::readHeader(
204 const RawInstrProf::Header &Header) {
205 if (swap(Header.Version) != RawInstrProf::Version)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000206 return error(instrprof_error::unsupported_version);
207
208 CountersDelta = swap(Header.CountersDelta);
209 NamesDelta = swap(Header.NamesDelta);
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000210 ValueDataDelta = swap(Header.ValueDataDelta);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000211 auto DataSize = swap(Header.DataSize);
212 auto CountersSize = swap(Header.CountersSize);
213 auto NamesSize = swap(Header.NamesSize);
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000214 auto ValueDataSize = swap(Header.ValueDataSize);
215 ValueKindLast = swap(Header.ValueKindLast);
216
217 auto DataSizeInBytes = DataSize * sizeof(RawInstrProf::ProfileData<IntPtrT>);
218 auto PaddingSize = getNumPaddingBytes(NamesSize);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000219
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000220 ptrdiff_t DataOffset = sizeof(RawInstrProf::Header);
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000221 ptrdiff_t CountersOffset = DataOffset + DataSizeInBytes;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000222 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000223 ptrdiff_t ValueDataOffset = NamesOffset + NamesSize + PaddingSize;
224 size_t ProfileSize = ValueDataOffset + ValueDataSize;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000225
Justin Bognera119f322014-05-16 00:38:00 +0000226 auto *Start = reinterpret_cast<const char *>(&Header);
227 if (Start + ProfileSize > DataBuffer->getBufferEnd())
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000228 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000229
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000230 Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>(
231 Start + DataOffset);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000232 DataEnd = Data + DataSize;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000233 CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
234 NamesStart = Start + NamesOffset;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000235 ValueDataStart = reinterpret_cast<const uint8_t*>(Start + ValueDataOffset);
Justin Bognera119f322014-05-16 00:38:00 +0000236 ProfileEnd = Start + ProfileSize;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000237
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000238 FunctionPtrToNameMap.clear();
239 for (const RawInstrProf::ProfileData<IntPtrT> *I = Data; I != DataEnd; ++I) {
240 const IntPtrT FPtr = swap(I->FunctionPointer);
241 if (!FPtr)
242 continue;
243 StringRef FunctionName(getName(I->NamePtr), swap(I->NameSize));
244 const char* NameEntryPtr = StringTable.insertString(FunctionName);
245 FunctionPtrToNameMap.push_back(std::pair<const IntPtrT, const char*>
246 (FPtr, NameEntryPtr));
247 }
248 std::sort(FunctionPtrToNameMap.begin(), FunctionPtrToNameMap.end(), less_first());
249 FunctionPtrToNameMap.erase(std::unique(FunctionPtrToNameMap.begin(),
250 FunctionPtrToNameMap.end()),
251 FunctionPtrToNameMap.end());
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000252 return success();
253}
254
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000255template <class IntPtrT>
Xinliang David Licf4a1282015-10-28 19:34:04 +0000256std::error_code RawInstrProfReader<IntPtrT>::readName(InstrProfRecord &Record) {
257 Record.Name = StringRef(getName(Data->NamePtr), swap(Data->NameSize));
258 if (Record.Name.data() < NamesStart ||
Xinliang David Licfb14562015-11-18 22:42:27 +0000259 Record.Name.data() + Record.Name.size() >
260 reinterpret_cast<const char *>(ValueDataStart))
Xinliang David Licf4a1282015-10-28 19:34:04 +0000261 return error(instrprof_error::malformed);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000262 return success();
263}
264
265template <class IntPtrT>
266std::error_code RawInstrProfReader<IntPtrT>::readFuncHash(
267 InstrProfRecord &Record) {
268 Record.Hash = swap(Data->FuncHash);
269 return success();
270}
271
272template <class IntPtrT>
273std::error_code RawInstrProfReader<IntPtrT>::readRawCounts(
274 InstrProfRecord &Record) {
Justin Bognerb59d7c72014-04-25 02:45:33 +0000275 uint32_t NumCounters = swap(Data->NumCounters);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000276 IntPtrT CounterPtr = Data->CounterPtr;
Justin Bognerb59d7c72014-04-25 02:45:33 +0000277 if (NumCounters == 0)
278 return error(instrprof_error::malformed);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000279
280 auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters);
281 auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000282
283 // Check bounds.
Xinliang David Licf4a1282015-10-28 19:34:04 +0000284 if (RawCounts.data() < CountersStart ||
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000285 RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000286 return error(instrprof_error::malformed);
287
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000288 if (ShouldSwapBytes) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000289 Record.Counts.clear();
290 Record.Counts.reserve(RawCounts.size());
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000291 for (uint64_t Count : RawCounts)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000292 Record.Counts.push_back(swap(Count));
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000293 } else
294 Record.Counts = RawCounts;
295
Xinliang David Licf4a1282015-10-28 19:34:04 +0000296 return success();
297}
298
299template <class IntPtrT>
Xinliang David Li01cb9bd2015-12-04 01:02:10 +0000300std::error_code
301RawInstrProfReader<IntPtrT>::readValueProfilingData(InstrProfRecord &Record) {
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000302
303 Record.clearValueData();
304 if (!Data->Values || (ValueDataDelta == 0))
305 return success();
306
Xinliang David Li01cb9bd2015-12-04 01:02:10 +0000307 ErrorOr<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
308 ValueProfData::getValueProfData(getValueDataCounts(Data->Values),
309 (const unsigned char *)ProfileEnd,
310 getDataEndianness());
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000311
Xinliang David Li01cb9bd2015-12-04 01:02:10 +0000312 if (VDataPtrOrErr.getError())
313 return VDataPtrOrErr.getError();
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000314
Xinliang David Li01cb9bd2015-12-04 01:02:10 +0000315 VDataPtrOrErr.get()->deserializeTo(Record, &FunctionPtrToNameMap);
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000316 return success();
317}
318
319template <class IntPtrT>
Xinliang David Li01cb9bd2015-12-04 01:02:10 +0000320std::error_code
321RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) {
Xinliang David Licf4a1282015-10-28 19:34:04 +0000322 if (atEnd())
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000323 if (std::error_code EC = readNextHeader(ProfileEnd))
324 return EC;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000325
326 // Read name ad set it in Record.
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000327 if (std::error_code EC = readName(Record))
328 return EC;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000329
330 // Read FuncHash and set it in Record.
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000331 if (std::error_code EC = readFuncHash(Record))
332 return EC;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000333
334 // Read raw counts and set Record.
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000335 if (std::error_code EC = readRawCounts(Record))
336 return EC;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000337
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000338 // Read value data and set Record.
Xinliang David Li2d4803e2015-12-10 23:48:05 +0000339 if (std::error_code EC = readValueProfilingData(Record))
340 return EC;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000341
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000342 // Iterate.
Xinliang David Licf4a1282015-10-28 19:34:04 +0000343 advanceData();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000344 return success();
345}
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000346
347namespace llvm {
348template class RawInstrProfReader<uint32_t>;
349template class RawInstrProfReader<uint64_t>;
350}
Justin Bognerb7aa2632014-04-18 21:48:40 +0000351
Justin Bognerb5d368e2014-04-18 22:00:22 +0000352InstrProfLookupTrait::hash_value_type
353InstrProfLookupTrait::ComputeHash(StringRef K) {
354 return IndexedInstrProf::ComputeHash(HashType, K);
355}
356
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000357typedef InstrProfLookupTrait::data_type data_type;
358typedef InstrProfLookupTrait::offset_type offset_type;
359
Xinliang David Libe969c22015-11-28 05:06:00 +0000360bool InstrProfLookupTrait::readValueProfilingData(
Justin Bogner9e9a0572015-09-29 22:13:58 +0000361 const unsigned char *&D, const unsigned char *const End) {
Xinliang David Li99556872015-11-17 23:00:40 +0000362 ErrorOr<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
363 ValueProfData::getValueProfData(D, End, ValueProfDataEndianness);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000364
Xinliang David Liee415892015-11-10 00:24:45 +0000365 if (VDataPtrOrErr.getError())
Justin Bogner9e9a0572015-09-29 22:13:58 +0000366 return false;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000367
Xinliang David Liee415892015-11-10 00:24:45 +0000368 VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), &HashKeys);
369 D += VDataPtrOrErr.get()->TotalSize;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000370
Justin Bogner9e9a0572015-09-29 22:13:58 +0000371 return true;
372}
373
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000374data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D,
375 offset_type N) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000376 // Check if the data is corrupt. If so, don't try to read it.
377 if (N % sizeof(uint64_t))
378 return data_type();
379
380 DataBuffer.clear();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000381 std::vector<uint64_t> CounterBuffer;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000382
383 using namespace support;
384 const unsigned char *End = D + N;
385 while (D < End) {
Xinliang David Lic7583872015-10-13 16:35:59 +0000386 // Read hash.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000387 if (D + sizeof(uint64_t) >= End)
388 return data_type();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000389 uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
390
Xinliang David Lic7583872015-10-13 16:35:59 +0000391 // Initialize number of counters for FormatVersion == 1.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000392 uint64_t CountsSize = N / sizeof(uint64_t) - 1;
Xinliang David Lic7583872015-10-13 16:35:59 +0000393 // If format version is different then read the number of counters.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000394 if (FormatVersion != 1) {
395 if (D + sizeof(uint64_t) > End)
396 return data_type();
397 CountsSize = endian::readNext<uint64_t, little, unaligned>(D);
398 }
Xinliang David Lic7583872015-10-13 16:35:59 +0000399 // Read counter values.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000400 if (D + CountsSize * sizeof(uint64_t) > End)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000401 return data_type();
402
403 CounterBuffer.clear();
Justin Bogner9e9a0572015-09-29 22:13:58 +0000404 CounterBuffer.reserve(CountsSize);
405 for (uint64_t J = 0; J < CountsSize; ++J)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000406 CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
407
Xinliang David Li2004f002015-11-02 05:08:23 +0000408 DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000409
Xinliang David Lic7583872015-10-13 16:35:59 +0000410 // Read value profiling data.
Xinliang David Libe969c22015-11-28 05:06:00 +0000411 if (FormatVersion > 2 && !readValueProfilingData(D, End)) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000412 DataBuffer.clear();
413 return data_type();
414 }
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000415 }
416 return DataBuffer;
417}
418
Xinliang David Lia28306d2015-12-01 20:26:26 +0000419template <typename HashTableImpl>
420std::error_code InstrProfReaderIndex<HashTableImpl>::getRecords(
421 StringRef FuncName, ArrayRef<InstrProfRecord> &Data) {
422 auto Iter = HashTable->find(FuncName);
423 if (Iter == HashTable->end())
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000424 return instrprof_error::unknown_function;
Xinliang David Li140f4c42015-10-28 04:20:31 +0000425
426 Data = (*Iter);
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000427 if (Data.empty())
428 return instrprof_error::malformed;
Xinliang David Li140f4c42015-10-28 04:20:31 +0000429
430 return instrprof_error::success;
431}
432
Xinliang David Lia28306d2015-12-01 20:26:26 +0000433template <typename HashTableImpl>
434std::error_code InstrProfReaderIndex<HashTableImpl>::getRecords(
Xinliang David Li140f4c42015-10-28 04:20:31 +0000435 ArrayRef<InstrProfRecord> &Data) {
Xinliang David Lia28306d2015-12-01 20:26:26 +0000436 if (atEnd())
437 return instrprof_error::eof;
Xinliang David Li140f4c42015-10-28 04:20:31 +0000438
439 Data = *RecordIterator;
440
Xinliang David Li2d4803e2015-12-10 23:48:05 +0000441 if (Data.empty())
442 return instrprof_error::malformed;
Xinliang David Li140f4c42015-10-28 04:20:31 +0000443
444 return instrprof_error::success;
445}
446
Xinliang David Lia28306d2015-12-01 20:26:26 +0000447template <typename HashTableImpl>
448InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex(
449 const unsigned char *Buckets, const unsigned char *const Payload,
450 const unsigned char *const Base, IndexedInstrProf::HashT HashType,
451 uint64_t Version) {
Xinliang David Li140f4c42015-10-28 04:20:31 +0000452 FormatVersion = Version;
Xinliang David Lia28306d2015-12-01 20:26:26 +0000453 HashTable.reset(HashTableImpl::Create(
454 Buckets, Payload, Base,
455 typename HashTableImpl::InfoType(HashType, Version)));
Xinliang David Li140f4c42015-10-28 04:20:31 +0000456 // Form the map of hash values to const char* keys in profiling data.
457 std::vector<std::pair<uint64_t, const char *>> HashKeys;
Xinliang David Lia28306d2015-12-01 20:26:26 +0000458 for (auto Key : HashTable->keys()) {
Xinliang David Li140f4c42015-10-28 04:20:31 +0000459 const char *KeyTableRef = StringTable.insertString(Key);
460 HashKeys.push_back(std::make_pair(ComputeHash(HashType, Key), KeyTableRef));
461 }
462 std::sort(HashKeys.begin(), HashKeys.end(), less_first());
463 HashKeys.erase(std::unique(HashKeys.begin(), HashKeys.end()), HashKeys.end());
464 // Set the hash key map for the InstrLookupTrait
Xinliang David Lia28306d2015-12-01 20:26:26 +0000465 HashTable->getInfoObj().setHashKeys(std::move(HashKeys));
466 RecordIterator = HashTable->data_begin();
Xinliang David Li140f4c42015-10-28 04:20:31 +0000467}
468
Justin Bognerb7aa2632014-04-18 21:48:40 +0000469bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000470 if (DataBuffer.getBufferSize() < 8)
471 return false;
Justin Bognerb7aa2632014-04-18 21:48:40 +0000472 using namespace support;
473 uint64_t Magic =
474 endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
Xinliang David Lic7583872015-10-13 16:35:59 +0000475 // Verify that it's magical.
Justin Bognerb7aa2632014-04-18 21:48:40 +0000476 return Magic == IndexedInstrProf::Magic;
477}
478
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000479std::error_code IndexedInstrProfReader::readHeader() {
Aaron Ballmana7c9ed52014-05-01 17:16:24 +0000480 const unsigned char *Start =
481 (const unsigned char *)DataBuffer->getBufferStart();
Justin Bognerb7aa2632014-04-18 21:48:40 +0000482 const unsigned char *Cur = Start;
Aaron Ballmana7c9ed52014-05-01 17:16:24 +0000483 if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
Justin Bognerb7aa2632014-04-18 21:48:40 +0000484 return error(instrprof_error::truncated);
485
486 using namespace support;
487
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000488 auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur);
489 Cur += sizeof(IndexedInstrProf::Header);
490
Justin Bognerb7aa2632014-04-18 21:48:40 +0000491 // Check the magic number.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000492 uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000493 if (Magic != IndexedInstrProf::Magic)
494 return error(instrprof_error::bad_magic);
495
496 // Read the version.
Xinliang David Li140f4c42015-10-28 04:20:31 +0000497 uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version);
Justin Bogner821d7472014-08-01 22:50:07 +0000498 if (FormatVersion > IndexedInstrProf::Version)
Justin Bognerb7aa2632014-04-18 21:48:40 +0000499 return error(instrprof_error::unsupported_version);
500
501 // Read the maximal function count.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000502 MaxFunctionCount =
503 endian::byte_swap<uint64_t, little>(Header->MaxFunctionCount);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000504
505 // Read the hash type and start offset.
506 IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000507 endian::byte_swap<uint64_t, little>(Header->HashType));
Justin Bognerb7aa2632014-04-18 21:48:40 +0000508 if (HashType > IndexedInstrProf::HashT::Last)
509 return error(instrprof_error::unsupported_hash_type);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000510
511 uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000512
513 // The rest of the file is an on disk hash table.
Xinliang David Lia28306d2015-12-01 20:26:26 +0000514 InstrProfReaderIndexBase *IndexPtr = nullptr;
515 IndexPtr = new InstrProfReaderIndex<OnDiskHashTableImplV3>(
516 Start + HashOffset, Cur, Start, HashType, FormatVersion);
517 Index.reset(IndexPtr);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000518 return success();
519}
520
Xinliang David Li6aa216c2015-11-06 07:54:21 +0000521ErrorOr<InstrProfRecord>
522IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
523 uint64_t FuncHash) {
Xinliang David Li140f4c42015-10-28 04:20:31 +0000524 ArrayRef<InstrProfRecord> Data;
Xinliang David Lia28306d2015-12-01 20:26:26 +0000525 std::error_code EC = Index->getRecords(FuncName, Data);
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000526 if (EC != instrprof_error::success)
527 return EC;
Justin Bogner821d7472014-08-01 22:50:07 +0000528 // Found it. Look for counters with the right hash.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000529 for (unsigned I = 0, E = Data.size(); I < E; ++I) {
Justin Bogner821d7472014-08-01 22:50:07 +0000530 // Check for a match and fill the vector if there is one.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000531 if (Data[I].Hash == FuncHash) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000532 return std::move(Data[I]);
Justin Bogner821d7472014-08-01 22:50:07 +0000533 }
534 }
535 return error(instrprof_error::hash_mismatch);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000536}
537
Xinliang David Li6aa216c2015-11-06 07:54:21 +0000538std::error_code
539IndexedInstrProfReader::getFunctionCounts(StringRef FuncName, uint64_t FuncHash,
540 std::vector<uint64_t> &Counts) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000541 ErrorOr<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash);
Xinliang David Li6aa216c2015-11-06 07:54:21 +0000542 if (std::error_code EC = Record.getError())
543 return EC;
Xinliang David Li2004f002015-11-02 05:08:23 +0000544
545 Counts = Record.get().Counts;
546 return success();
547}
548
Xinliang David Li140f4c42015-10-28 04:20:31 +0000549std::error_code IndexedInstrProfReader::readNextRecord(
550 InstrProfRecord &Record) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000551 static unsigned RecordIndex = 0;
Xinliang David Li140f4c42015-10-28 04:20:31 +0000552
553 ArrayRef<InstrProfRecord> Data;
554
Xinliang David Lia28306d2015-12-01 20:26:26 +0000555 std::error_code EC = Index->getRecords(Data);
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000556 if (EC != instrprof_error::success)
557 return error(EC);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000558
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000559 Record = Data[RecordIndex++];
560 if (RecordIndex >= Data.size()) {
Xinliang David Lia28306d2015-12-01 20:26:26 +0000561 Index->advanceToNextKey();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000562 RecordIndex = 0;
Justin Bogner821d7472014-08-01 22:50:07 +0000563 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000564 return success();
565}