blob: 856f793363f7740814e4bf9311a96abc09f16fb9 [file] [log] [blame]
Eugene Zelenkoe78d1312017-03-03 01:07:34 +00001//===- InstrProfReader.cpp - Instrumented profiling reader ----------------===//
Justin Bognerf8d79192014-03-21 17:24:48 +00002//
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
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000015#include "llvm/ADT/ArrayRef.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000017#include "llvm/ADT/StringRef.h"
18#include "llvm/IR/ProfileSummary.h"
19#include "llvm/ProfileData/InstrProf.h"
20#include "llvm/ProfileData/InstrProfReader.h"
21#include "llvm/ProfileData/ProfileCommon.h"
22#include "llvm/Support/Endian.h"
23#include "llvm/Support/Error.h"
24#include "llvm/Support/ErrorOr.h"
25#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/Support/SwapByteOrder.h"
27#include <algorithm>
28#include <cctype>
29#include <cstddef>
30#include <cstdint>
31#include <limits>
32#include <memory>
33#include <system_error>
34#include <utility>
35#include <vector>
Justin Bognerf8d79192014-03-21 17:24:48 +000036
37using namespace llvm;
38
Vedant Kumar9152fd12016-05-19 03:54:45 +000039static Expected<std::unique_ptr<MemoryBuffer>>
Benjamin Kramer0da23a22016-05-29 10:31:00 +000040setupMemoryBuffer(const Twine &Path) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +000041 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
42 MemoryBuffer::getFileOrSTDIN(Path);
43 if (std::error_code EC = BufferOrErr.getError())
Vedant Kumar9152fd12016-05-19 03:54:45 +000044 return errorCodeToError(EC);
Justin Bogner2b6c5372015-02-18 01:58:17 +000045 return std::move(BufferOrErr.get());
Justin Bognerb7aa2632014-04-18 21:48:40 +000046}
47
Vedant Kumar9152fd12016-05-19 03:54:45 +000048static Error initializeReader(InstrProfReader &Reader) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000049 return Reader.readHeader();
50}
51
Vedant Kumar9152fd12016-05-19 03:54:45 +000052Expected<std::unique_ptr<InstrProfReader>>
Benjamin Kramer0da23a22016-05-29 10:31:00 +000053InstrProfReader::create(const Twine &Path) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000054 // Set up the buffer to read.
Diego Novillofcd55602014-11-03 00:51:45 +000055 auto BufferOrError = setupMemoryBuffer(Path);
Vedant Kumar9152fd12016-05-19 03:54:45 +000056 if (Error E = BufferOrError.takeError())
57 return std::move(E);
Justin Bogner2b6c5372015-02-18 01:58:17 +000058 return InstrProfReader::create(std::move(BufferOrError.get()));
59}
Justin Bognerf8d79192014-03-21 17:24:48 +000060
Vedant Kumar9152fd12016-05-19 03:54:45 +000061Expected<std::unique_ptr<InstrProfReader>>
Justin Bogner2b6c5372015-02-18 01:58:17 +000062InstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
63 // Sanity check the buffer.
64 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
Vedant Kumar9152fd12016-05-19 03:54:45 +000065 return make_error<InstrProfError>(instrprof_error::too_large);
Justin Bogner2b6c5372015-02-18 01:58:17 +000066
Rong Xu2c684cf2016-10-19 22:51:17 +000067 if (Buffer->getBufferSize() == 0)
68 return make_error<InstrProfError>(instrprof_error::empty_raw_profile);
69
Diego Novillofcd55602014-11-03 00:51:45 +000070 std::unique_ptr<InstrProfReader> Result;
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000071 // Create the reader.
Justin Bognerb7aa2632014-04-18 21:48:40 +000072 if (IndexedInstrProfReader::hasFormat(*Buffer))
73 Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
74 else if (RawInstrProfReader64::hasFormat(*Buffer))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +000075 Result.reset(new RawInstrProfReader64(std::move(Buffer)));
76 else if (RawInstrProfReader32::hasFormat(*Buffer))
77 Result.reset(new RawInstrProfReader32(std::move(Buffer)));
Nathan Slingerland4f823662015-11-13 03:47:58 +000078 else if (TextInstrProfReader::hasFormat(*Buffer))
Nathan Slingerland911ced62015-11-12 18:39:26 +000079 Result.reset(new TextInstrProfReader(std::move(Buffer)));
Nathan Slingerland4f823662015-11-13 03:47:58 +000080 else
Vedant Kumar9152fd12016-05-19 03:54:45 +000081 return make_error<InstrProfError>(instrprof_error::unrecognized_format);
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +000082
Justin Bognerb7aa2632014-04-18 21:48:40 +000083 // Initialize the reader and return the result.
Vedant Kumar9152fd12016-05-19 03:54:45 +000084 if (Error E = initializeReader(*Result))
85 return std::move(E);
Diego Novillofcd55602014-11-03 00:51:45 +000086
87 return std::move(Result);
Justin Bognerb7aa2632014-04-18 21:48:40 +000088}
89
Vedant Kumar9152fd12016-05-19 03:54:45 +000090Expected<std::unique_ptr<IndexedInstrProfReader>>
Benjamin Kramer0da23a22016-05-29 10:31:00 +000091IndexedInstrProfReader::create(const Twine &Path) {
Justin Bognerb7aa2632014-04-18 21:48:40 +000092 // Set up the buffer to read.
Diego Novillofcd55602014-11-03 00:51:45 +000093 auto BufferOrError = setupMemoryBuffer(Path);
Vedant Kumar9152fd12016-05-19 03:54:45 +000094 if (Error E = BufferOrError.takeError())
95 return std::move(E);
Justin Bogner2b6c5372015-02-18 01:58:17 +000096 return IndexedInstrProfReader::create(std::move(BufferOrError.get()));
97}
Justin Bognerb7aa2632014-04-18 21:48:40 +000098
Vedant Kumar9152fd12016-05-19 03:54:45 +000099Expected<std::unique_ptr<IndexedInstrProfReader>>
Justin Bogner2b6c5372015-02-18 01:58:17 +0000100IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
101 // Sanity check the buffer.
102 if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000103 return make_error<InstrProfError>(instrprof_error::too_large);
Justin Bognerab89ed72015-02-16 21:28:58 +0000104
Justin Bognerb7aa2632014-04-18 21:48:40 +0000105 // Create the reader.
106 if (!IndexedInstrProfReader::hasFormat(*Buffer))
Vedant Kumar9152fd12016-05-19 03:54:45 +0000107 return make_error<InstrProfError>(instrprof_error::bad_magic);
Justin Bogner2b6c5372015-02-18 01:58:17 +0000108 auto Result = llvm::make_unique<IndexedInstrProfReader>(std::move(Buffer));
Justin Bognerb7aa2632014-04-18 21:48:40 +0000109
110 // Initialize the reader and return the result.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000111 if (Error E = initializeReader(*Result))
112 return std::move(E);
Justin Bognerab89ed72015-02-16 21:28:58 +0000113
114 return std::move(Result);
Justin Bognerf8d79192014-03-21 17:24:48 +0000115}
116
117void InstrProfIterator::Increment() {
Vedant Kumar9152fd12016-05-19 03:54:45 +0000118 if (auto E = Reader->readNextRecord(Record)) {
119 // Handle errors in the reader.
120 InstrProfError::take(std::move(E));
Justin Bognerf8d79192014-03-21 17:24:48 +0000121 *this = InstrProfIterator();
Vedant Kumar9152fd12016-05-19 03:54:45 +0000122 }
Justin Bognerf8d79192014-03-21 17:24:48 +0000123}
124
Nathan Slingerland4f823662015-11-13 03:47:58 +0000125bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) {
126 // Verify that this really looks like plain ASCII text by checking a
127 // 'reasonable' number of characters (up to profile magic size).
128 size_t count = std::min(Buffer.getBufferSize(), sizeof(uint64_t));
129 StringRef buffer = Buffer.getBufferStart();
Vedant Kumar2491dd12015-12-11 00:40:05 +0000130 return count == 0 ||
131 std::all_of(buffer.begin(), buffer.begin() + count,
132 [](char c) { return ::isprint(c) || ::isspace(c); });
Nathan Slingerland4f823662015-11-13 03:47:58 +0000133}
134
Rong Xu33c76c02016-02-10 17:18:30 +0000135// Read the profile variant flag from the header: ":FE" means this is a FE
136// generated profile. ":IR" means this is an IR level profile. Other strings
137// with a leading ':' will be reported an error format.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000138Error TextInstrProfReader::readHeader() {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000139 Symtab.reset(new InstrProfSymtab());
Rong Xu33c76c02016-02-10 17:18:30 +0000140 bool IsIRInstr = false;
141 if (!Line->startswith(":")) {
142 IsIRLevelProfile = false;
143 return success();
144 }
145 StringRef Str = (Line)->substr(1);
146 if (Str.equals_lower("ir"))
147 IsIRInstr = true;
148 else if (Str.equals_lower("fe"))
149 IsIRInstr = false;
150 else
Vedant Kumar9152fd12016-05-19 03:54:45 +0000151 return error(instrprof_error::bad_header);
Rong Xu33c76c02016-02-10 17:18:30 +0000152
153 ++Line;
154 IsIRLevelProfile = IsIRInstr;
Xinliang David Lia716cc52015-12-20 06:22:13 +0000155 return success();
156}
157
Vedant Kumar9152fd12016-05-19 03:54:45 +0000158Error
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000159TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) {
160
161#define CHECK_LINE_END(Line) \
162 if (Line.is_at_end()) \
163 return error(instrprof_error::truncated);
164#define READ_NUM(Str, Dst) \
165 if ((Str).getAsInteger(10, (Dst))) \
166 return error(instrprof_error::malformed);
167#define VP_READ_ADVANCE(Val) \
168 CHECK_LINE_END(Line); \
169 uint32_t Val; \
170 READ_NUM((*Line), (Val)); \
171 Line++;
172
173 if (Line.is_at_end())
174 return success();
Xinliang David Lia716cc52015-12-20 06:22:13 +0000175
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000176 uint32_t NumValueKinds;
177 if (Line->getAsInteger(10, NumValueKinds)) {
178 // No value profile data
179 return success();
180 }
181 if (NumValueKinds == 0 || NumValueKinds > IPVK_Last + 1)
182 return error(instrprof_error::malformed);
183 Line++;
184
185 for (uint32_t VK = 0; VK < NumValueKinds; VK++) {
186 VP_READ_ADVANCE(ValueKind);
187 if (ValueKind > IPVK_Last)
188 return error(instrprof_error::malformed);
189 VP_READ_ADVANCE(NumValueSites);
190 if (!NumValueSites)
191 continue;
192
193 Record.reserveSites(VK, NumValueSites);
194 for (uint32_t S = 0; S < NumValueSites; S++) {
195 VP_READ_ADVANCE(NumValueData);
196
197 std::vector<InstrProfValueData> CurrentValues;
198 for (uint32_t V = 0; V < NumValueData; V++) {
199 CHECK_LINE_END(Line);
Rong Xu35723642016-05-06 23:20:58 +0000200 std::pair<StringRef, StringRef> VD = Line->rsplit(':');
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000201 uint64_t TakenCount, Value;
Rong Xucbb11402017-02-27 21:42:39 +0000202 if (ValueKind == IPVK_IndirectCallTarget) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000203 Symtab->addFuncName(VD.first);
204 Value = IndexedInstrProf::ComputeHash(VD.first);
205 } else {
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000206 READ_NUM(VD.first, Value);
207 }
Xinliang David Lia716cc52015-12-20 06:22:13 +0000208 READ_NUM(VD.second, TakenCount);
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000209 CurrentValues.push_back({Value, TakenCount});
210 Line++;
211 }
Rong Xucbb11402017-02-27 21:42:39 +0000212 Record.addValueData(ValueKind, S, CurrentValues.data(), NumValueData,
213 nullptr);
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000214 }
215 }
216 return success();
217
218#undef CHECK_LINE_END
219#undef READ_NUM
220#undef VP_READ_ADVANCE
221}
222
Vedant Kumar9152fd12016-05-19 03:54:45 +0000223Error TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
Justin Bognercf36a362014-07-29 22:29:23 +0000224 // Skip empty lines and comments.
225 while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
Justin Bognerf8d79192014-03-21 17:24:48 +0000226 ++Line;
227 // If we hit EOF while looking for a name, we're done.
Xinliang David Lia716cc52015-12-20 06:22:13 +0000228 if (Line.is_at_end()) {
229 Symtab->finalizeSymtab();
Justin Bognerf8d79192014-03-21 17:24:48 +0000230 return error(instrprof_error::eof);
Xinliang David Lia716cc52015-12-20 06:22:13 +0000231 }
Justin Bognerf8d79192014-03-21 17:24:48 +0000232
233 // Read the function name.
234 Record.Name = *Line++;
Xinliang David Lia716cc52015-12-20 06:22:13 +0000235 Symtab->addFuncName(Record.Name);
Justin Bognerf8d79192014-03-21 17:24:48 +0000236
237 // Read the function hash.
238 if (Line.is_at_end())
239 return error(instrprof_error::truncated);
Justin Bognerf95ca072015-03-09 18:54:49 +0000240 if ((Line++)->getAsInteger(0, Record.Hash))
Justin Bognerf8d79192014-03-21 17:24:48 +0000241 return error(instrprof_error::malformed);
242
243 // Read the number of counters.
244 uint64_t NumCounters;
245 if (Line.is_at_end())
246 return error(instrprof_error::truncated);
247 if ((Line++)->getAsInteger(10, NumCounters))
248 return error(instrprof_error::malformed);
Justin Bognerb59d7c72014-04-25 02:45:33 +0000249 if (NumCounters == 0)
250 return error(instrprof_error::malformed);
Justin Bognerf8d79192014-03-21 17:24:48 +0000251
252 // Read each counter and fill our internal storage with the values.
Rong Xu6241c2a2017-03-03 21:56:34 +0000253 Record.Clear();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000254 Record.Counts.reserve(NumCounters);
Justin Bognerf8d79192014-03-21 17:24:48 +0000255 for (uint64_t I = 0; I < NumCounters; ++I) {
256 if (Line.is_at_end())
257 return error(instrprof_error::truncated);
258 uint64_t Count;
259 if ((Line++)->getAsInteger(10, Count))
260 return error(instrprof_error::malformed);
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000261 Record.Counts.push_back(Count);
Justin Bognerf8d79192014-03-21 17:24:48 +0000262 }
Justin Bognerf8d79192014-03-21 17:24:48 +0000263
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000264 // Check if value profile data exists and read it if so.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000265 if (Error E = readValueProfileData(Record))
266 return E;
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000267
Xinliang David Lia716cc52015-12-20 06:22:13 +0000268 // This is needed to avoid two pass parsing because llvm-profdata
269 // does dumping while reading.
270 Symtab->finalizeSymtab();
Justin Bognerf8d79192014-03-21 17:24:48 +0000271 return success();
272}
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000273
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000274template <class IntPtrT>
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000275bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000276 if (DataBuffer.getBufferSize() < sizeof(uint64_t))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000277 return false;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000278 uint64_t Magic =
279 *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000280 return RawInstrProf::getMagic<IntPtrT>() == Magic ||
281 sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic;
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000282}
283
284template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000285Error RawInstrProfReader<IntPtrT>::readHeader() {
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000286 if (!hasFormat(*DataBuffer))
287 return error(instrprof_error::bad_magic);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000288 if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header))
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000289 return error(instrprof_error::bad_header);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000290 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(
291 DataBuffer->getBufferStart());
292 ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000293 return readHeader(*Header);
294}
295
Justin Bognera119f322014-05-16 00:38:00 +0000296template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000297Error RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
Justin Bognera119f322014-05-16 00:38:00 +0000298 const char *End = DataBuffer->getBufferEnd();
299 // Skip zero padding between profiles.
300 while (CurrentPos != End && *CurrentPos == 0)
301 ++CurrentPos;
302 // If there's nothing left, we're done.
303 if (CurrentPos == End)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000304 return make_error<InstrProfError>(instrprof_error::eof);
Justin Bognera119f322014-05-16 00:38:00 +0000305 // If there isn't enough space for another header, this is probably just
306 // garbage at the end of the file.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000307 if (CurrentPos + sizeof(RawInstrProf::Header) > End)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000308 return make_error<InstrProfError>(instrprof_error::malformed);
Justin Bogner54b11282014-09-12 21:22:55 +0000309 // The writer ensures each profile is padded to start at an aligned address.
Benjamin Kramerb2505002016-10-20 15:02:18 +0000310 if (reinterpret_cast<size_t>(CurrentPos) % alignof(uint64_t))
Vedant Kumar9152fd12016-05-19 03:54:45 +0000311 return make_error<InstrProfError>(instrprof_error::malformed);
Justin Bognera119f322014-05-16 00:38:00 +0000312 // The magic should have the same byte order as in the previous header.
313 uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000314 if (Magic != swap(RawInstrProf::getMagic<IntPtrT>()))
Vedant Kumar9152fd12016-05-19 03:54:45 +0000315 return make_error<InstrProfError>(instrprof_error::bad_magic);
Justin Bognera119f322014-05-16 00:38:00 +0000316
317 // There's another profile to read, so we need to process the header.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000318 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos);
Justin Bognera119f322014-05-16 00:38:00 +0000319 return readHeader(*Header);
320}
321
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000322template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000323Error RawInstrProfReader<IntPtrT>::createSymtab(InstrProfSymtab &Symtab) {
324 if (Error E = Symtab.create(StringRef(NamesStart, NamesSize)))
325 return error(std::move(E));
Xinliang David Lia716cc52015-12-20 06:22:13 +0000326 for (const RawInstrProf::ProfileData<IntPtrT> *I = Data; I != DataEnd; ++I) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000327 const IntPtrT FPtr = swap(I->FunctionPointer);
328 if (!FPtr)
329 continue;
Xinliang David Lia82d6c02016-02-08 18:13:49 +0000330 Symtab.mapAddress(FPtr, I->NameRef);
Xinliang David Lia716cc52015-12-20 06:22:13 +0000331 }
332 Symtab.finalizeSymtab();
Vedant Kumare44482f2016-04-21 21:07:25 +0000333 return success();
Xinliang David Lia716cc52015-12-20 06:22:13 +0000334}
335
336template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000337Error RawInstrProfReader<IntPtrT>::readHeader(
338 const RawInstrProf::Header &Header) {
Rong Xu33c76c02016-02-10 17:18:30 +0000339 Version = swap(Header.Version);
340 if (GET_VERSION(Version) != RawInstrProf::Version)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000341 return error(instrprof_error::unsupported_version);
342
343 CountersDelta = swap(Header.CountersDelta);
344 NamesDelta = swap(Header.NamesDelta);
345 auto DataSize = swap(Header.DataSize);
346 auto CountersSize = swap(Header.CountersSize);
Xinliang David Lia82d6c02016-02-08 18:13:49 +0000347 NamesSize = swap(Header.NamesSize);
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000348 ValueKindLast = swap(Header.ValueKindLast);
349
350 auto DataSizeInBytes = DataSize * sizeof(RawInstrProf::ProfileData<IntPtrT>);
351 auto PaddingSize = getNumPaddingBytes(NamesSize);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000352
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000353 ptrdiff_t DataOffset = sizeof(RawInstrProf::Header);
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000354 ptrdiff_t CountersOffset = DataOffset + DataSizeInBytes;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000355 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000356 ptrdiff_t ValueDataOffset = NamesOffset + NamesSize + PaddingSize;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000357
Justin Bognera119f322014-05-16 00:38:00 +0000358 auto *Start = reinterpret_cast<const char *>(&Header);
Xinliang David Li188a7c52016-05-05 19:41:18 +0000359 if (Start + ValueDataOffset > DataBuffer->getBufferEnd())
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000360 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000361
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000362 Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>(
363 Start + DataOffset);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000364 DataEnd = Data + DataSize;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000365 CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
366 NamesStart = Start + NamesOffset;
Xinliang David Lia716cc52015-12-20 06:22:13 +0000367 ValueDataStart = reinterpret_cast<const uint8_t *>(Start + ValueDataOffset);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000368
Xinliang David Lia716cc52015-12-20 06:22:13 +0000369 std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
Vedant Kumar9152fd12016-05-19 03:54:45 +0000370 if (Error E = createSymtab(*NewSymtab.get()))
371 return E;
Vedant Kumare44482f2016-04-21 21:07:25 +0000372
Xinliang David Lia716cc52015-12-20 06:22:13 +0000373 Symtab = std::move(NewSymtab);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000374 return success();
375}
376
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000377template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000378Error RawInstrProfReader<IntPtrT>::readName(InstrProfRecord &Record) {
Xinliang David Lia82d6c02016-02-08 18:13:49 +0000379 Record.Name = getName(Data->NameRef);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000380 return success();
381}
382
383template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000384Error RawInstrProfReader<IntPtrT>::readFuncHash(InstrProfRecord &Record) {
Xinliang David Licf4a1282015-10-28 19:34:04 +0000385 Record.Hash = swap(Data->FuncHash);
386 return success();
387}
388
389template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000390Error RawInstrProfReader<IntPtrT>::readRawCounts(
Xinliang David Licf4a1282015-10-28 19:34:04 +0000391 InstrProfRecord &Record) {
Justin Bognerb59d7c72014-04-25 02:45:33 +0000392 uint32_t NumCounters = swap(Data->NumCounters);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000393 IntPtrT CounterPtr = Data->CounterPtr;
Justin Bognerb59d7c72014-04-25 02:45:33 +0000394 if (NumCounters == 0)
395 return error(instrprof_error::malformed);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000396
397 auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters);
398 auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000399
400 // Check bounds.
Xinliang David Licf4a1282015-10-28 19:34:04 +0000401 if (RawCounts.data() < CountersStart ||
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000402 RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000403 return error(instrprof_error::malformed);
404
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000405 if (ShouldSwapBytes) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000406 Record.Counts.clear();
407 Record.Counts.reserve(RawCounts.size());
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000408 for (uint64_t Count : RawCounts)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000409 Record.Counts.push_back(swap(Count));
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000410 } else
411 Record.Counts = RawCounts;
412
Xinliang David Licf4a1282015-10-28 19:34:04 +0000413 return success();
414}
415
416template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000417Error RawInstrProfReader<IntPtrT>::readValueProfilingData(
418 InstrProfRecord &Record) {
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000419 Record.clearValueData();
Xinliang David Lid922c262015-12-11 06:53:53 +0000420 CurValueDataSize = 0;
421 // Need to match the logic in value profile dumper code in compiler-rt:
422 uint32_t NumValueKinds = 0;
423 for (uint32_t I = 0; I < IPVK_Last + 1; I++)
424 NumValueKinds += (Data->NumValueSites[I] != 0);
425
426 if (!NumValueKinds)
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000427 return success();
428
Vedant Kumar9152fd12016-05-19 03:54:45 +0000429 Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
Xinliang David Li188a7c52016-05-05 19:41:18 +0000430 ValueProfData::getValueProfData(
431 ValueDataStart, (const unsigned char *)DataBuffer->getBufferEnd(),
432 getDataEndianness());
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000433
Vedant Kumar9152fd12016-05-19 03:54:45 +0000434 if (Error E = VDataPtrOrErr.takeError())
435 return E;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000436
Adam Nemet2f36f052016-03-28 18:27:44 +0000437 // Note that besides deserialization, this also performs the conversion for
438 // indirect call targets. The function pointers from the raw profile are
439 // remapped into function name hashes.
Xinliang David Lia716cc52015-12-20 06:22:13 +0000440 VDataPtrOrErr.get()->deserializeTo(Record, &Symtab->getAddrHashMap());
Xinliang David Lid922c262015-12-11 06:53:53 +0000441 CurValueDataSize = VDataPtrOrErr.get()->getSize();
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000442 return success();
443}
444
445template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000446Error RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) {
Xinliang David Licf4a1282015-10-28 19:34:04 +0000447 if (atEnd())
Xinliang David Li188a7c52016-05-05 19:41:18 +0000448 // At this point, ValueDataStart field points to the next header.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000449 if (Error E = readNextHeader(getNextHeaderPos()))
450 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000451
452 // Read name ad set it in Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000453 if (Error E = readName(Record))
454 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000455
456 // Read FuncHash and set it in Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000457 if (Error E = readFuncHash(Record))
458 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000459
460 // Read raw counts and set Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000461 if (Error E = readRawCounts(Record))
462 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000463
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000464 // Read value data and set Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000465 if (Error E = readValueProfilingData(Record))
466 return E;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000467
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000468 // Iterate.
Xinliang David Licf4a1282015-10-28 19:34:04 +0000469 advanceData();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000470 return success();
471}
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000472
473namespace llvm {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000474
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000475template class RawInstrProfReader<uint32_t>;
476template class RawInstrProfReader<uint64_t>;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000477
478} // end namespace llvm
Justin Bognerb7aa2632014-04-18 21:48:40 +0000479
Justin Bognerb5d368e2014-04-18 22:00:22 +0000480InstrProfLookupTrait::hash_value_type
481InstrProfLookupTrait::ComputeHash(StringRef K) {
482 return IndexedInstrProf::ComputeHash(HashType, K);
483}
484
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000485typedef InstrProfLookupTrait::data_type data_type;
486typedef InstrProfLookupTrait::offset_type offset_type;
487
Xinliang David Libe969c22015-11-28 05:06:00 +0000488bool InstrProfLookupTrait::readValueProfilingData(
Justin Bogner9e9a0572015-09-29 22:13:58 +0000489 const unsigned char *&D, const unsigned char *const End) {
Vedant Kumar9152fd12016-05-19 03:54:45 +0000490 Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
Xinliang David Li99556872015-11-17 23:00:40 +0000491 ValueProfData::getValueProfData(D, End, ValueProfDataEndianness);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000492
Vedant Kumar9152fd12016-05-19 03:54:45 +0000493 if (VDataPtrOrErr.takeError())
Justin Bogner9e9a0572015-09-29 22:13:58 +0000494 return false;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000495
Xinliang David Lia716cc52015-12-20 06:22:13 +0000496 VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr);
Xinliang David Liee415892015-11-10 00:24:45 +0000497 D += VDataPtrOrErr.get()->TotalSize;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000498
Justin Bogner9e9a0572015-09-29 22:13:58 +0000499 return true;
500}
501
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000502data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D,
503 offset_type N) {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000504 using namespace support;
505
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000506 // Check if the data is corrupt. If so, don't try to read it.
507 if (N % sizeof(uint64_t))
508 return data_type();
509
510 DataBuffer.clear();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000511 std::vector<uint64_t> CounterBuffer;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000512
Justin Bogner9e9a0572015-09-29 22:13:58 +0000513 const unsigned char *End = D + N;
514 while (D < End) {
Xinliang David Lic7583872015-10-13 16:35:59 +0000515 // Read hash.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000516 if (D + sizeof(uint64_t) >= End)
517 return data_type();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000518 uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
519
Rong Xu33c76c02016-02-10 17:18:30 +0000520 // Initialize number of counters for GET_VERSION(FormatVersion) == 1.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000521 uint64_t CountsSize = N / sizeof(uint64_t) - 1;
Xinliang David Lic7583872015-10-13 16:35:59 +0000522 // If format version is different then read the number of counters.
Rong Xu33c76c02016-02-10 17:18:30 +0000523 if (GET_VERSION(FormatVersion) != IndexedInstrProf::ProfVersion::Version1) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000524 if (D + sizeof(uint64_t) > End)
525 return data_type();
526 CountsSize = endian::readNext<uint64_t, little, unaligned>(D);
527 }
Xinliang David Lic7583872015-10-13 16:35:59 +0000528 // Read counter values.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000529 if (D + CountsSize * sizeof(uint64_t) > End)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000530 return data_type();
531
532 CounterBuffer.clear();
Justin Bogner9e9a0572015-09-29 22:13:58 +0000533 CounterBuffer.reserve(CountsSize);
534 for (uint64_t J = 0; J < CountsSize; ++J)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000535 CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
536
Xinliang David Li2004f002015-11-02 05:08:23 +0000537 DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000538
Xinliang David Lic7583872015-10-13 16:35:59 +0000539 // Read value profiling data.
Rong Xu33c76c02016-02-10 17:18:30 +0000540 if (GET_VERSION(FormatVersion) > IndexedInstrProf::ProfVersion::Version2 &&
Xinliang David Lia6b2c4f2016-01-14 02:47:01 +0000541 !readValueProfilingData(D, End)) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000542 DataBuffer.clear();
543 return data_type();
544 }
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000545 }
546 return DataBuffer;
547}
548
Xinliang David Lia28306d2015-12-01 20:26:26 +0000549template <typename HashTableImpl>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000550Error InstrProfReaderIndex<HashTableImpl>::getRecords(
Xinliang David Lia28306d2015-12-01 20:26:26 +0000551 StringRef FuncName, ArrayRef<InstrProfRecord> &Data) {
552 auto Iter = HashTable->find(FuncName);
553 if (Iter == HashTable->end())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000554 return make_error<InstrProfError>(instrprof_error::unknown_function);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000555
556 Data = (*Iter);
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000557 if (Data.empty())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000558 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000559
Vedant Kumar9152fd12016-05-19 03:54:45 +0000560 return Error::success();
Xinliang David Li140f4c42015-10-28 04:20:31 +0000561}
562
Xinliang David Lia28306d2015-12-01 20:26:26 +0000563template <typename HashTableImpl>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000564Error InstrProfReaderIndex<HashTableImpl>::getRecords(
Xinliang David Li140f4c42015-10-28 04:20:31 +0000565 ArrayRef<InstrProfRecord> &Data) {
Xinliang David Lia28306d2015-12-01 20:26:26 +0000566 if (atEnd())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000567 return make_error<InstrProfError>(instrprof_error::eof);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000568
569 Data = *RecordIterator;
570
Xinliang David Li2d4803e2015-12-10 23:48:05 +0000571 if (Data.empty())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000572 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000573
Vedant Kumar9152fd12016-05-19 03:54:45 +0000574 return Error::success();
Xinliang David Li140f4c42015-10-28 04:20:31 +0000575}
576
Xinliang David Lia28306d2015-12-01 20:26:26 +0000577template <typename HashTableImpl>
578InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex(
579 const unsigned char *Buckets, const unsigned char *const Payload,
580 const unsigned char *const Base, IndexedInstrProf::HashT HashType,
581 uint64_t Version) {
Xinliang David Li140f4c42015-10-28 04:20:31 +0000582 FormatVersion = Version;
Xinliang David Lia28306d2015-12-01 20:26:26 +0000583 HashTable.reset(HashTableImpl::Create(
584 Buckets, Payload, Base,
585 typename HashTableImpl::InfoType(HashType, Version)));
Xinliang David Lia28306d2015-12-01 20:26:26 +0000586 RecordIterator = HashTable->data_begin();
Xinliang David Li140f4c42015-10-28 04:20:31 +0000587}
588
Justin Bognerb7aa2632014-04-18 21:48:40 +0000589bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000590 using namespace support;
591
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000592 if (DataBuffer.getBufferSize() < 8)
593 return false;
Justin Bognerb7aa2632014-04-18 21:48:40 +0000594 uint64_t Magic =
595 endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
Xinliang David Lic7583872015-10-13 16:35:59 +0000596 // Verify that it's magical.
Justin Bognerb7aa2632014-04-18 21:48:40 +0000597 return Magic == IndexedInstrProf::Magic;
598}
599
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000600const unsigned char *
601IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version,
602 const unsigned char *Cur) {
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000603 using namespace IndexedInstrProf;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000604 using namespace support;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000605
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000606 if (Version >= IndexedInstrProf::Version4) {
607 const IndexedInstrProf::Summary *SummaryInLE =
608 reinterpret_cast<const IndexedInstrProf::Summary *>(Cur);
609 uint64_t NFields =
610 endian::byte_swap<uint64_t, little>(SummaryInLE->NumSummaryFields);
611 uint64_t NEntries =
612 endian::byte_swap<uint64_t, little>(SummaryInLE->NumCutoffEntries);
613 uint32_t SummarySize =
614 IndexedInstrProf::Summary::getSize(NFields, NEntries);
615 std::unique_ptr<IndexedInstrProf::Summary> SummaryData =
616 IndexedInstrProf::allocSummary(SummarySize);
617
618 const uint64_t *Src = reinterpret_cast<const uint64_t *>(SummaryInLE);
619 uint64_t *Dst = reinterpret_cast<uint64_t *>(SummaryData.get());
620 for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
621 Dst[I] = endian::byte_swap<uint64_t, little>(Src[I]);
622
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000623 llvm::SummaryEntryVector DetailedSummary;
624 for (unsigned I = 0; I < SummaryData->NumCutoffEntries; I++) {
625 const IndexedInstrProf::Summary::Entry &Ent = SummaryData->getEntry(I);
626 DetailedSummary.emplace_back((uint32_t)Ent.Cutoff, Ent.MinBlockCount,
627 Ent.NumBlocks);
628 }
Easwaran Raman43095702016-02-17 18:18:47 +0000629 // initialize InstrProfSummary using the SummaryData from disk.
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000630 this->Summary = llvm::make_unique<ProfileSummary>(
631 ProfileSummary::PSK_Instr, DetailedSummary,
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000632 SummaryData->get(Summary::TotalBlockCount),
633 SummaryData->get(Summary::MaxBlockCount),
634 SummaryData->get(Summary::MaxInternalBlockCount),
635 SummaryData->get(Summary::MaxFunctionCount),
636 SummaryData->get(Summary::TotalNumBlocks),
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000637 SummaryData->get(Summary::TotalNumFunctions));
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000638 return Cur + SummarySize;
639 } else {
640 // For older version of profile data, we need to compute on the fly:
641 using namespace IndexedInstrProf;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000642
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000643 InstrProfSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
644 // FIXME: This only computes an empty summary. Need to call addRecord for
645 // all InstrProfRecords to get the correct summary.
Benjamin Kramer38de59e2016-05-20 09:18:37 +0000646 this->Summary = Builder.getSummary();
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000647 return Cur;
648 }
649}
650
Vedant Kumar9152fd12016-05-19 03:54:45 +0000651Error IndexedInstrProfReader::readHeader() {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000652 using namespace support;
653
Aaron Ballmana7c9ed52014-05-01 17:16:24 +0000654 const unsigned char *Start =
655 (const unsigned char *)DataBuffer->getBufferStart();
Justin Bognerb7aa2632014-04-18 21:48:40 +0000656 const unsigned char *Cur = Start;
Aaron Ballmana7c9ed52014-05-01 17:16:24 +0000657 if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
Justin Bognerb7aa2632014-04-18 21:48:40 +0000658 return error(instrprof_error::truncated);
659
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000660 auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur);
661 Cur += sizeof(IndexedInstrProf::Header);
662
Justin Bognerb7aa2632014-04-18 21:48:40 +0000663 // Check the magic number.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000664 uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000665 if (Magic != IndexedInstrProf::Magic)
666 return error(instrprof_error::bad_magic);
667
668 // Read the version.
Xinliang David Li140f4c42015-10-28 04:20:31 +0000669 uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version);
Rong Xu33c76c02016-02-10 17:18:30 +0000670 if (GET_VERSION(FormatVersion) >
671 IndexedInstrProf::ProfVersion::CurrentVersion)
Justin Bognerb7aa2632014-04-18 21:48:40 +0000672 return error(instrprof_error::unsupported_version);
673
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000674 Cur = readSummary((IndexedInstrProf::ProfVersion)FormatVersion, Cur);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000675
676 // Read the hash type and start offset.
677 IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000678 endian::byte_swap<uint64_t, little>(Header->HashType));
Justin Bognerb7aa2632014-04-18 21:48:40 +0000679 if (HashType > IndexedInstrProf::HashT::Last)
680 return error(instrprof_error::unsupported_hash_type);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000681
682 uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000683
684 // The rest of the file is an on disk hash table.
Xinliang David Lia28306d2015-12-01 20:26:26 +0000685 InstrProfReaderIndexBase *IndexPtr = nullptr;
686 IndexPtr = new InstrProfReaderIndex<OnDiskHashTableImplV3>(
687 Start + HashOffset, Cur, Start, HashType, FormatVersion);
688 Index.reset(IndexPtr);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000689 return success();
690}
691
Xinliang David Lia716cc52015-12-20 06:22:13 +0000692InstrProfSymtab &IndexedInstrProfReader::getSymtab() {
693 if (Symtab.get())
694 return *Symtab.get();
695
696 std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
697 Index->populateSymtab(*NewSymtab.get());
698
699 Symtab = std::move(NewSymtab);
700 return *Symtab.get();
701}
702
Vedant Kumar9152fd12016-05-19 03:54:45 +0000703Expected<InstrProfRecord>
Xinliang David Li6aa216c2015-11-06 07:54:21 +0000704IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
705 uint64_t FuncHash) {
Xinliang David Li140f4c42015-10-28 04:20:31 +0000706 ArrayRef<InstrProfRecord> Data;
Vedant Kumar9152fd12016-05-19 03:54:45 +0000707 Error Err = Index->getRecords(FuncName, Data);
708 if (Err)
709 return std::move(Err);
Justin Bogner821d7472014-08-01 22:50:07 +0000710 // Found it. Look for counters with the right hash.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000711 for (unsigned I = 0, E = Data.size(); I < E; ++I) {
Justin Bogner821d7472014-08-01 22:50:07 +0000712 // Check for a match and fill the vector if there is one.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000713 if (Data[I].Hash == FuncHash) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000714 return std::move(Data[I]);
Justin Bogner821d7472014-08-01 22:50:07 +0000715 }
716 }
717 return error(instrprof_error::hash_mismatch);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000718}
719
Vedant Kumar9152fd12016-05-19 03:54:45 +0000720Error IndexedInstrProfReader::getFunctionCounts(StringRef FuncName,
721 uint64_t FuncHash,
722 std::vector<uint64_t> &Counts) {
723 Expected<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash);
724 if (Error E = Record.takeError())
725 return error(std::move(E));
Xinliang David Li2004f002015-11-02 05:08:23 +0000726
727 Counts = Record.get().Counts;
728 return success();
729}
730
Vedant Kumar9152fd12016-05-19 03:54:45 +0000731Error IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000732 static unsigned RecordIndex = 0;
Xinliang David Li140f4c42015-10-28 04:20:31 +0000733
734 ArrayRef<InstrProfRecord> Data;
735
Vedant Kumar9152fd12016-05-19 03:54:45 +0000736 Error E = Index->getRecords(Data);
737 if (E)
738 return error(std::move(E));
Xinliang David Li140f4c42015-10-28 04:20:31 +0000739
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000740 Record = Data[RecordIndex++];
741 if (RecordIndex >= Data.size()) {
Xinliang David Lia28306d2015-12-01 20:26:26 +0000742 Index->advanceToNextKey();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000743 RecordIndex = 0;
Justin Bogner821d7472014-08-01 22:50:07 +0000744 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000745 return success();
746}