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