blob: cdf50c2df0c8541f4f1b120061cb8cc76a2242ae [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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/ProfileData/InstrProfReader.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000016#include "llvm/ADT/ArrayRef.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000017#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000018#include "llvm/ADT/StringRef.h"
19#include "llvm/IR/ProfileSummary.h"
20#include "llvm/ProfileData/InstrProf.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000021#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) {
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000203 if (Error E = Symtab->addFuncName(VD.first))
204 return E;
Xinliang David Lia716cc52015-12-20 06:22:13 +0000205 Value = IndexedInstrProf::ComputeHash(VD.first);
206 } else {
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000207 READ_NUM(VD.first, Value);
208 }
Xinliang David Lia716cc52015-12-20 06:22:13 +0000209 READ_NUM(VD.second, TakenCount);
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000210 CurrentValues.push_back({Value, TakenCount});
211 Line++;
212 }
Rong Xucbb11402017-02-27 21:42:39 +0000213 Record.addValueData(ValueKind, S, CurrentValues.data(), NumValueData,
214 nullptr);
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000215 }
216 }
217 return success();
218
219#undef CHECK_LINE_END
220#undef READ_NUM
221#undef VP_READ_ADVANCE
222}
223
David Blaikiecf9d52c2017-07-06 19:00:12 +0000224Error TextInstrProfReader::readNextRecord(NamedInstrProfRecord &Record) {
Justin Bognercf36a362014-07-29 22:29:23 +0000225 // Skip empty lines and comments.
226 while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
Justin Bognerf8d79192014-03-21 17:24:48 +0000227 ++Line;
228 // If we hit EOF while looking for a name, we're done.
Xinliang David Lia716cc52015-12-20 06:22:13 +0000229 if (Line.is_at_end()) {
230 Symtab->finalizeSymtab();
Justin Bognerf8d79192014-03-21 17:24:48 +0000231 return error(instrprof_error::eof);
Xinliang David Lia716cc52015-12-20 06:22:13 +0000232 }
Justin Bognerf8d79192014-03-21 17:24:48 +0000233
234 // Read the function name.
235 Record.Name = *Line++;
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000236 if (Error E = Symtab->addFuncName(Record.Name))
237 return E;
Justin Bognerf8d79192014-03-21 17:24:48 +0000238
239 // Read the function hash.
240 if (Line.is_at_end())
241 return error(instrprof_error::truncated);
Justin Bognerf95ca072015-03-09 18:54:49 +0000242 if ((Line++)->getAsInteger(0, Record.Hash))
Justin Bognerf8d79192014-03-21 17:24:48 +0000243 return error(instrprof_error::malformed);
244
245 // Read the number of counters.
246 uint64_t NumCounters;
247 if (Line.is_at_end())
248 return error(instrprof_error::truncated);
249 if ((Line++)->getAsInteger(10, NumCounters))
250 return error(instrprof_error::malformed);
Justin Bognerb59d7c72014-04-25 02:45:33 +0000251 if (NumCounters == 0)
252 return error(instrprof_error::malformed);
Justin Bognerf8d79192014-03-21 17:24:48 +0000253
254 // Read each counter and fill our internal storage with the values.
Rong Xu6241c2a2017-03-03 21:56:34 +0000255 Record.Clear();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000256 Record.Counts.reserve(NumCounters);
Justin Bognerf8d79192014-03-21 17:24:48 +0000257 for (uint64_t I = 0; I < NumCounters; ++I) {
258 if (Line.is_at_end())
259 return error(instrprof_error::truncated);
260 uint64_t Count;
261 if ((Line++)->getAsInteger(10, Count))
262 return error(instrprof_error::malformed);
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000263 Record.Counts.push_back(Count);
Justin Bognerf8d79192014-03-21 17:24:48 +0000264 }
Justin Bognerf8d79192014-03-21 17:24:48 +0000265
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000266 // Check if value profile data exists and read it if so.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000267 if (Error E = readValueProfileData(Record))
268 return E;
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000269
Xinliang David Lia716cc52015-12-20 06:22:13 +0000270 // This is needed to avoid two pass parsing because llvm-profdata
271 // does dumping while reading.
272 Symtab->finalizeSymtab();
Justin Bognerf8d79192014-03-21 17:24:48 +0000273 return success();
274}
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000275
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000276template <class IntPtrT>
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000277bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000278 if (DataBuffer.getBufferSize() < sizeof(uint64_t))
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000279 return false;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000280 uint64_t Magic =
281 *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000282 return RawInstrProf::getMagic<IntPtrT>() == Magic ||
283 sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic;
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000284}
285
286template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000287Error RawInstrProfReader<IntPtrT>::readHeader() {
Duncan P. N. Exon Smith09a67f42014-03-21 20:42:31 +0000288 if (!hasFormat(*DataBuffer))
289 return error(instrprof_error::bad_magic);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000290 if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header))
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000291 return error(instrprof_error::bad_header);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000292 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(
293 DataBuffer->getBufferStart());
294 ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000295 return readHeader(*Header);
296}
297
Justin Bognera119f322014-05-16 00:38:00 +0000298template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000299Error RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
Justin Bognera119f322014-05-16 00:38:00 +0000300 const char *End = DataBuffer->getBufferEnd();
301 // Skip zero padding between profiles.
302 while (CurrentPos != End && *CurrentPos == 0)
303 ++CurrentPos;
304 // If there's nothing left, we're done.
305 if (CurrentPos == End)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000306 return make_error<InstrProfError>(instrprof_error::eof);
Justin Bognera119f322014-05-16 00:38:00 +0000307 // If there isn't enough space for another header, this is probably just
308 // garbage at the end of the file.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000309 if (CurrentPos + sizeof(RawInstrProf::Header) > End)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000310 return make_error<InstrProfError>(instrprof_error::malformed);
Justin Bogner54b11282014-09-12 21:22:55 +0000311 // The writer ensures each profile is padded to start at an aligned address.
Benjamin Kramerb2505002016-10-20 15:02:18 +0000312 if (reinterpret_cast<size_t>(CurrentPos) % alignof(uint64_t))
Vedant Kumar9152fd12016-05-19 03:54:45 +0000313 return make_error<InstrProfError>(instrprof_error::malformed);
Justin Bognera119f322014-05-16 00:38:00 +0000314 // The magic should have the same byte order as in the previous header.
315 uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000316 if (Magic != swap(RawInstrProf::getMagic<IntPtrT>()))
Vedant Kumar9152fd12016-05-19 03:54:45 +0000317 return make_error<InstrProfError>(instrprof_error::bad_magic);
Justin Bognera119f322014-05-16 00:38:00 +0000318
319 // There's another profile to read, so we need to process the header.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000320 auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos);
Justin Bognera119f322014-05-16 00:38:00 +0000321 return readHeader(*Header);
322}
323
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000324template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000325Error RawInstrProfReader<IntPtrT>::createSymtab(InstrProfSymtab &Symtab) {
326 if (Error E = Symtab.create(StringRef(NamesStart, NamesSize)))
327 return error(std::move(E));
Xinliang David Lia716cc52015-12-20 06:22:13 +0000328 for (const RawInstrProf::ProfileData<IntPtrT> *I = Data; I != DataEnd; ++I) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000329 const IntPtrT FPtr = swap(I->FunctionPointer);
330 if (!FPtr)
331 continue;
Xinliang David Lia82d6c02016-02-08 18:13:49 +0000332 Symtab.mapAddress(FPtr, I->NameRef);
Xinliang David Lia716cc52015-12-20 06:22:13 +0000333 }
334 Symtab.finalizeSymtab();
Vedant Kumare44482f2016-04-21 21:07:25 +0000335 return success();
Xinliang David Lia716cc52015-12-20 06:22:13 +0000336}
337
338template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000339Error RawInstrProfReader<IntPtrT>::readHeader(
340 const RawInstrProf::Header &Header) {
Rong Xu33c76c02016-02-10 17:18:30 +0000341 Version = swap(Header.Version);
342 if (GET_VERSION(Version) != RawInstrProf::Version)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000343 return error(instrprof_error::unsupported_version);
344
345 CountersDelta = swap(Header.CountersDelta);
346 NamesDelta = swap(Header.NamesDelta);
347 auto DataSize = swap(Header.DataSize);
348 auto CountersSize = swap(Header.CountersSize);
Xinliang David Lia82d6c02016-02-08 18:13:49 +0000349 NamesSize = swap(Header.NamesSize);
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000350 ValueKindLast = swap(Header.ValueKindLast);
351
352 auto DataSizeInBytes = DataSize * sizeof(RawInstrProf::ProfileData<IntPtrT>);
353 auto PaddingSize = getNumPaddingBytes(NamesSize);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000354
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000355 ptrdiff_t DataOffset = sizeof(RawInstrProf::Header);
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000356 ptrdiff_t CountersOffset = DataOffset + DataSizeInBytes;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000357 ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000358 ptrdiff_t ValueDataOffset = NamesOffset + NamesSize + PaddingSize;
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000359
Justin Bognera119f322014-05-16 00:38:00 +0000360 auto *Start = reinterpret_cast<const char *>(&Header);
Xinliang David Li188a7c52016-05-05 19:41:18 +0000361 if (Start + ValueDataOffset > DataBuffer->getBufferEnd())
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +0000362 return error(instrprof_error::bad_header);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000363
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000364 Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>(
365 Start + DataOffset);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000366 DataEnd = Data + DataSize;
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000367 CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
368 NamesStart = Start + NamesOffset;
Xinliang David Lia716cc52015-12-20 06:22:13 +0000369 ValueDataStart = reinterpret_cast<const uint8_t *>(Start + ValueDataOffset);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000370
Xinliang David Lia716cc52015-12-20 06:22:13 +0000371 std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
Vedant Kumar9152fd12016-05-19 03:54:45 +0000372 if (Error E = createSymtab(*NewSymtab.get()))
373 return E;
Vedant Kumare44482f2016-04-21 21:07:25 +0000374
Xinliang David Lia716cc52015-12-20 06:22:13 +0000375 Symtab = std::move(NewSymtab);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000376 return success();
377}
378
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000379template <class IntPtrT>
David Blaikiecf9d52c2017-07-06 19:00:12 +0000380Error RawInstrProfReader<IntPtrT>::readName(NamedInstrProfRecord &Record) {
Xinliang David Lia82d6c02016-02-08 18:13:49 +0000381 Record.Name = getName(Data->NameRef);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000382 return success();
383}
384
385template <class IntPtrT>
David Blaikiecf9d52c2017-07-06 19:00:12 +0000386Error RawInstrProfReader<IntPtrT>::readFuncHash(NamedInstrProfRecord &Record) {
Xinliang David Licf4a1282015-10-28 19:34:04 +0000387 Record.Hash = swap(Data->FuncHash);
388 return success();
389}
390
391template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000392Error RawInstrProfReader<IntPtrT>::readRawCounts(
Xinliang David Licf4a1282015-10-28 19:34:04 +0000393 InstrProfRecord &Record) {
Justin Bognerb59d7c72014-04-25 02:45:33 +0000394 uint32_t NumCounters = swap(Data->NumCounters);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000395 IntPtrT CounterPtr = Data->CounterPtr;
Justin Bognerb59d7c72014-04-25 02:45:33 +0000396 if (NumCounters == 0)
397 return error(instrprof_error::malformed);
Xinliang David Licf4a1282015-10-28 19:34:04 +0000398
399 auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters);
400 auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000401
402 // Check bounds.
Xinliang David Licf4a1282015-10-28 19:34:04 +0000403 if (RawCounts.data() < CountersStart ||
Duncan P. N. Exon Smithd7d83472014-03-24 00:47:18 +0000404 RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000405 return error(instrprof_error::malformed);
406
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000407 if (ShouldSwapBytes) {
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000408 Record.Counts.clear();
409 Record.Counts.reserve(RawCounts.size());
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000410 for (uint64_t Count : RawCounts)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000411 Record.Counts.push_back(swap(Count));
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000412 } else
413 Record.Counts = RawCounts;
414
Xinliang David Licf4a1282015-10-28 19:34:04 +0000415 return success();
416}
417
418template <class IntPtrT>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000419Error RawInstrProfReader<IntPtrT>::readValueProfilingData(
420 InstrProfRecord &Record) {
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000421 Record.clearValueData();
Xinliang David Lid922c262015-12-11 06:53:53 +0000422 CurValueDataSize = 0;
423 // Need to match the logic in value profile dumper code in compiler-rt:
424 uint32_t NumValueKinds = 0;
425 for (uint32_t I = 0; I < IPVK_Last + 1; I++)
426 NumValueKinds += (Data->NumValueSites[I] != 0);
427
428 if (!NumValueKinds)
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000429 return success();
430
Vedant Kumar9152fd12016-05-19 03:54:45 +0000431 Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
Xinliang David Li188a7c52016-05-05 19:41:18 +0000432 ValueProfData::getValueProfData(
433 ValueDataStart, (const unsigned char *)DataBuffer->getBufferEnd(),
434 getDataEndianness());
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000435
Vedant Kumar9152fd12016-05-19 03:54:45 +0000436 if (Error E = VDataPtrOrErr.takeError())
437 return E;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000438
Adam Nemet2f36f052016-03-28 18:27:44 +0000439 // Note that besides deserialization, this also performs the conversion for
440 // indirect call targets. The function pointers from the raw profile are
441 // remapped into function name hashes.
Xinliang David Lia716cc52015-12-20 06:22:13 +0000442 VDataPtrOrErr.get()->deserializeTo(Record, &Symtab->getAddrHashMap());
Xinliang David Lid922c262015-12-11 06:53:53 +0000443 CurValueDataSize = VDataPtrOrErr.get()->getSize();
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000444 return success();
445}
446
447template <class IntPtrT>
David Blaikiecf9d52c2017-07-06 19:00:12 +0000448Error RawInstrProfReader<IntPtrT>::readNextRecord(NamedInstrProfRecord &Record) {
Xinliang David Licf4a1282015-10-28 19:34:04 +0000449 if (atEnd())
Xinliang David Li188a7c52016-05-05 19:41:18 +0000450 // At this point, ValueDataStart field points to the next header.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000451 if (Error E = readNextHeader(getNextHeaderPos()))
452 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000453
454 // Read name ad set it in Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000455 if (Error E = readName(Record))
456 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000457
458 // Read FuncHash and set it in Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000459 if (Error E = readFuncHash(Record))
460 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000461
462 // Read raw counts and set Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000463 if (Error E = readRawCounts(Record))
464 return E;
Xinliang David Licf4a1282015-10-28 19:34:04 +0000465
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000466 // Read value data and set Record.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000467 if (Error E = readValueProfilingData(Record))
468 return E;
Betul Buyukkurt6fac1742015-11-18 18:14:55 +0000469
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000470 // Iterate.
Xinliang David Licf4a1282015-10-28 19:34:04 +0000471 advanceData();
Duncan P. N. Exon Smith24b4b652014-03-21 18:26:05 +0000472 return success();
473}
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000474
475namespace llvm {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000476
Duncan P. N. Exon Smith46803612014-03-23 03:38:12 +0000477template class RawInstrProfReader<uint32_t>;
478template class RawInstrProfReader<uint64_t>;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000479
480} // end namespace llvm
Justin Bognerb7aa2632014-04-18 21:48:40 +0000481
Justin Bognerb5d368e2014-04-18 22:00:22 +0000482InstrProfLookupTrait::hash_value_type
483InstrProfLookupTrait::ComputeHash(StringRef K) {
484 return IndexedInstrProf::ComputeHash(HashType, K);
485}
486
Eugene Zelenko72208a82017-06-21 23:19:47 +0000487using data_type = InstrProfLookupTrait::data_type;
488using offset_type = InstrProfLookupTrait::offset_type;
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000489
Xinliang David Libe969c22015-11-28 05:06:00 +0000490bool InstrProfLookupTrait::readValueProfilingData(
Justin Bogner9e9a0572015-09-29 22:13:58 +0000491 const unsigned char *&D, const unsigned char *const End) {
Vedant Kumar9152fd12016-05-19 03:54:45 +0000492 Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
Xinliang David Li99556872015-11-17 23:00:40 +0000493 ValueProfData::getValueProfData(D, End, ValueProfDataEndianness);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000494
Vedant Kumar9152fd12016-05-19 03:54:45 +0000495 if (VDataPtrOrErr.takeError())
Justin Bogner9e9a0572015-09-29 22:13:58 +0000496 return false;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000497
Xinliang David Lia716cc52015-12-20 06:22:13 +0000498 VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr);
Xinliang David Liee415892015-11-10 00:24:45 +0000499 D += VDataPtrOrErr.get()->TotalSize;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000500
Justin Bogner9e9a0572015-09-29 22:13:58 +0000501 return true;
502}
503
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000504data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D,
505 offset_type N) {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000506 using namespace support;
507
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000508 // Check if the data is corrupt. If so, don't try to read it.
509 if (N % sizeof(uint64_t))
510 return data_type();
511
512 DataBuffer.clear();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000513 std::vector<uint64_t> CounterBuffer;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000514
Justin Bogner9e9a0572015-09-29 22:13:58 +0000515 const unsigned char *End = D + N;
516 while (D < End) {
Xinliang David Lic7583872015-10-13 16:35:59 +0000517 // Read hash.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000518 if (D + sizeof(uint64_t) >= End)
519 return data_type();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000520 uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
521
Rong Xu33c76c02016-02-10 17:18:30 +0000522 // Initialize number of counters for GET_VERSION(FormatVersion) == 1.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000523 uint64_t CountsSize = N / sizeof(uint64_t) - 1;
Xinliang David Lic7583872015-10-13 16:35:59 +0000524 // If format version is different then read the number of counters.
Rong Xu33c76c02016-02-10 17:18:30 +0000525 if (GET_VERSION(FormatVersion) != IndexedInstrProf::ProfVersion::Version1) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000526 if (D + sizeof(uint64_t) > End)
527 return data_type();
528 CountsSize = endian::readNext<uint64_t, little, unaligned>(D);
529 }
Xinliang David Lic7583872015-10-13 16:35:59 +0000530 // Read counter values.
Justin Bogner9e9a0572015-09-29 22:13:58 +0000531 if (D + CountsSize * sizeof(uint64_t) > End)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000532 return data_type();
533
534 CounterBuffer.clear();
Justin Bogner9e9a0572015-09-29 22:13:58 +0000535 CounterBuffer.reserve(CountsSize);
536 for (uint64_t J = 0; J < CountsSize; ++J)
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000537 CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
538
Xinliang David Li2004f002015-11-02 05:08:23 +0000539 DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer));
Justin Bogner9e9a0572015-09-29 22:13:58 +0000540
Xinliang David Lic7583872015-10-13 16:35:59 +0000541 // Read value profiling data.
Rong Xu33c76c02016-02-10 17:18:30 +0000542 if (GET_VERSION(FormatVersion) > IndexedInstrProf::ProfVersion::Version2 &&
Xinliang David Lia6b2c4f2016-01-14 02:47:01 +0000543 !readValueProfilingData(D, End)) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000544 DataBuffer.clear();
545 return data_type();
546 }
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000547 }
548 return DataBuffer;
549}
550
Xinliang David Lia28306d2015-12-01 20:26:26 +0000551template <typename HashTableImpl>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000552Error InstrProfReaderIndex<HashTableImpl>::getRecords(
David Blaikiecf9d52c2017-07-06 19:00:12 +0000553 StringRef FuncName, ArrayRef<NamedInstrProfRecord> &Data) {
Xinliang David Lia28306d2015-12-01 20:26:26 +0000554 auto Iter = HashTable->find(FuncName);
555 if (Iter == HashTable->end())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000556 return make_error<InstrProfError>(instrprof_error::unknown_function);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000557
558 Data = (*Iter);
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000559 if (Data.empty())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000560 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000561
Vedant Kumar9152fd12016-05-19 03:54:45 +0000562 return Error::success();
Xinliang David Li140f4c42015-10-28 04:20:31 +0000563}
564
Xinliang David Lia28306d2015-12-01 20:26:26 +0000565template <typename HashTableImpl>
Vedant Kumar9152fd12016-05-19 03:54:45 +0000566Error InstrProfReaderIndex<HashTableImpl>::getRecords(
David Blaikiecf9d52c2017-07-06 19:00:12 +0000567 ArrayRef<NamedInstrProfRecord> &Data) {
Xinliang David Lia28306d2015-12-01 20:26:26 +0000568 if (atEnd())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000569 return make_error<InstrProfError>(instrprof_error::eof);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000570
571 Data = *RecordIterator;
572
Xinliang David Li2d4803e2015-12-10 23:48:05 +0000573 if (Data.empty())
Vedant Kumar9152fd12016-05-19 03:54:45 +0000574 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li140f4c42015-10-28 04:20:31 +0000575
Vedant Kumar9152fd12016-05-19 03:54:45 +0000576 return Error::success();
Xinliang David Li140f4c42015-10-28 04:20:31 +0000577}
578
Xinliang David Lia28306d2015-12-01 20:26:26 +0000579template <typename HashTableImpl>
580InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex(
581 const unsigned char *Buckets, const unsigned char *const Payload,
582 const unsigned char *const Base, IndexedInstrProf::HashT HashType,
583 uint64_t Version) {
Xinliang David Li140f4c42015-10-28 04:20:31 +0000584 FormatVersion = Version;
Xinliang David Lia28306d2015-12-01 20:26:26 +0000585 HashTable.reset(HashTableImpl::Create(
586 Buckets, Payload, Base,
587 typename HashTableImpl::InfoType(HashType, Version)));
Xinliang David Lia28306d2015-12-01 20:26:26 +0000588 RecordIterator = HashTable->data_begin();
Xinliang David Li140f4c42015-10-28 04:20:31 +0000589}
590
Justin Bognerb7aa2632014-04-18 21:48:40 +0000591bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000592 using namespace support;
593
Xinliang David Li4c3ab812015-11-12 00:32:17 +0000594 if (DataBuffer.getBufferSize() < 8)
595 return false;
Justin Bognerb7aa2632014-04-18 21:48:40 +0000596 uint64_t Magic =
597 endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
Xinliang David Lic7583872015-10-13 16:35:59 +0000598 // Verify that it's magical.
Justin Bognerb7aa2632014-04-18 21:48:40 +0000599 return Magic == IndexedInstrProf::Magic;
600}
601
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000602const unsigned char *
603IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version,
604 const unsigned char *Cur) {
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000605 using namespace IndexedInstrProf;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000606 using namespace support;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000607
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000608 if (Version >= IndexedInstrProf::Version4) {
609 const IndexedInstrProf::Summary *SummaryInLE =
610 reinterpret_cast<const IndexedInstrProf::Summary *>(Cur);
611 uint64_t NFields =
612 endian::byte_swap<uint64_t, little>(SummaryInLE->NumSummaryFields);
613 uint64_t NEntries =
614 endian::byte_swap<uint64_t, little>(SummaryInLE->NumCutoffEntries);
615 uint32_t SummarySize =
616 IndexedInstrProf::Summary::getSize(NFields, NEntries);
617 std::unique_ptr<IndexedInstrProf::Summary> SummaryData =
618 IndexedInstrProf::allocSummary(SummarySize);
619
620 const uint64_t *Src = reinterpret_cast<const uint64_t *>(SummaryInLE);
621 uint64_t *Dst = reinterpret_cast<uint64_t *>(SummaryData.get());
622 for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
623 Dst[I] = endian::byte_swap<uint64_t, little>(Src[I]);
624
Eugene Zelenko72208a82017-06-21 23:19:47 +0000625 SummaryEntryVector DetailedSummary;
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000626 for (unsigned I = 0; I < SummaryData->NumCutoffEntries; I++) {
627 const IndexedInstrProf::Summary::Entry &Ent = SummaryData->getEntry(I);
628 DetailedSummary.emplace_back((uint32_t)Ent.Cutoff, Ent.MinBlockCount,
629 Ent.NumBlocks);
630 }
Easwaran Raman43095702016-02-17 18:18:47 +0000631 // initialize InstrProfSummary using the SummaryData from disk.
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000632 this->Summary = llvm::make_unique<ProfileSummary>(
633 ProfileSummary::PSK_Instr, DetailedSummary,
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000634 SummaryData->get(Summary::TotalBlockCount),
635 SummaryData->get(Summary::MaxBlockCount),
636 SummaryData->get(Summary::MaxInternalBlockCount),
637 SummaryData->get(Summary::MaxFunctionCount),
638 SummaryData->get(Summary::TotalNumBlocks),
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000639 SummaryData->get(Summary::TotalNumFunctions));
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000640 return Cur + SummarySize;
641 } else {
642 // For older version of profile data, we need to compute on the fly:
643 using namespace IndexedInstrProf;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000644
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000645 InstrProfSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
646 // FIXME: This only computes an empty summary. Need to call addRecord for
David Blaikiecf9d52c2017-07-06 19:00:12 +0000647 // all NamedInstrProfRecords to get the correct summary.
Benjamin Kramer38de59e2016-05-20 09:18:37 +0000648 this->Summary = Builder.getSummary();
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000649 return Cur;
650 }
651}
652
Vedant Kumar9152fd12016-05-19 03:54:45 +0000653Error IndexedInstrProfReader::readHeader() {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000654 using namespace support;
655
Aaron Ballmana7c9ed52014-05-01 17:16:24 +0000656 const unsigned char *Start =
657 (const unsigned char *)DataBuffer->getBufferStart();
Justin Bognerb7aa2632014-04-18 21:48:40 +0000658 const unsigned char *Cur = Start;
Aaron Ballmana7c9ed52014-05-01 17:16:24 +0000659 if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
Justin Bognerb7aa2632014-04-18 21:48:40 +0000660 return error(instrprof_error::truncated);
661
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000662 auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur);
663 Cur += sizeof(IndexedInstrProf::Header);
664
Justin Bognerb7aa2632014-04-18 21:48:40 +0000665 // Check the magic number.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000666 uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000667 if (Magic != IndexedInstrProf::Magic)
668 return error(instrprof_error::bad_magic);
669
670 // Read the version.
Xinliang David Li140f4c42015-10-28 04:20:31 +0000671 uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version);
Rong Xu33c76c02016-02-10 17:18:30 +0000672 if (GET_VERSION(FormatVersion) >
673 IndexedInstrProf::ProfVersion::CurrentVersion)
Justin Bognerb7aa2632014-04-18 21:48:40 +0000674 return error(instrprof_error::unsupported_version);
675
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000676 Cur = readSummary((IndexedInstrProf::ProfVersion)FormatVersion, Cur);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000677
678 // Read the hash type and start offset.
679 IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000680 endian::byte_swap<uint64_t, little>(Header->HashType));
Justin Bognerb7aa2632014-04-18 21:48:40 +0000681 if (HashType > IndexedInstrProf::HashT::Last)
682 return error(instrprof_error::unsupported_hash_type);
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000683
684 uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000685
686 // The rest of the file is an on disk hash table.
Xinliang David Lia28306d2015-12-01 20:26:26 +0000687 InstrProfReaderIndexBase *IndexPtr = nullptr;
688 IndexPtr = new InstrProfReaderIndex<OnDiskHashTableImplV3>(
689 Start + HashOffset, Cur, Start, HashType, FormatVersion);
690 Index.reset(IndexPtr);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000691 return success();
692}
693
Xinliang David Lia716cc52015-12-20 06:22:13 +0000694InstrProfSymtab &IndexedInstrProfReader::getSymtab() {
695 if (Symtab.get())
696 return *Symtab.get();
697
698 std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000699 if (Error E = Index->populateSymtab(*NewSymtab.get())) {
700 consumeError(error(InstrProfError::take(std::move(E))));
701 }
Xinliang David Lia716cc52015-12-20 06:22:13 +0000702
703 Symtab = std::move(NewSymtab);
704 return *Symtab.get();
705}
706
Vedant Kumar9152fd12016-05-19 03:54:45 +0000707Expected<InstrProfRecord>
Xinliang David Li6aa216c2015-11-06 07:54:21 +0000708IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
709 uint64_t FuncHash) {
David Blaikiecf9d52c2017-07-06 19:00:12 +0000710 ArrayRef<NamedInstrProfRecord> Data;
Vedant Kumar9152fd12016-05-19 03:54:45 +0000711 Error Err = Index->getRecords(FuncName, Data);
712 if (Err)
713 return std::move(Err);
Justin Bogner821d7472014-08-01 22:50:07 +0000714 // Found it. Look for counters with the right hash.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000715 for (unsigned I = 0, E = Data.size(); I < E; ++I) {
Justin Bogner821d7472014-08-01 22:50:07 +0000716 // Check for a match and fill the vector if there is one.
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000717 if (Data[I].Hash == FuncHash) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000718 return std::move(Data[I]);
Justin Bogner821d7472014-08-01 22:50:07 +0000719 }
720 }
721 return error(instrprof_error::hash_mismatch);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000722}
723
Vedant Kumar9152fd12016-05-19 03:54:45 +0000724Error IndexedInstrProfReader::getFunctionCounts(StringRef FuncName,
725 uint64_t FuncHash,
726 std::vector<uint64_t> &Counts) {
727 Expected<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash);
728 if (Error E = Record.takeError())
729 return error(std::move(E));
Xinliang David Li2004f002015-11-02 05:08:23 +0000730
731 Counts = Record.get().Counts;
732 return success();
733}
734
David Blaikiecf9d52c2017-07-06 19:00:12 +0000735Error IndexedInstrProfReader::readNextRecord(NamedInstrProfRecord &Record) {
David Blaikiecf9d52c2017-07-06 19:00:12 +0000736 ArrayRef<NamedInstrProfRecord> Data;
Xinliang David Li140f4c42015-10-28 04:20:31 +0000737
Vedant Kumar9152fd12016-05-19 03:54:45 +0000738 Error E = Index->getRecords(Data);
739 if (E)
740 return error(std::move(E));
Xinliang David Li140f4c42015-10-28 04:20:31 +0000741
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000742 Record = Data[RecordIndex++];
743 if (RecordIndex >= Data.size()) {
Xinliang David Lia28306d2015-12-01 20:26:26 +0000744 Index->advanceToNextKey();
Justin Bogner3a7d44c2015-06-22 23:58:05 +0000745 RecordIndex = 0;
Justin Bogner821d7472014-08-01 22:50:07 +0000746 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000747 return success();
748}