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 | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 112 | std::error_code TextInstrProfReader::readHeader() { |
| 113 | Symtab.reset(new InstrProfSymtab()); |
| 114 | return success(); |
| 115 | } |
| 116 | |
Xinliang David Li | e3bf4fd3 | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 117 | std::error_code |
| 118 | TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) { |
| 119 | |
| 120 | #define CHECK_LINE_END(Line) \ |
| 121 | if (Line.is_at_end()) \ |
| 122 | return error(instrprof_error::truncated); |
| 123 | #define READ_NUM(Str, Dst) \ |
| 124 | if ((Str).getAsInteger(10, (Dst))) \ |
| 125 | return error(instrprof_error::malformed); |
| 126 | #define VP_READ_ADVANCE(Val) \ |
| 127 | CHECK_LINE_END(Line); \ |
| 128 | uint32_t Val; \ |
| 129 | READ_NUM((*Line), (Val)); \ |
| 130 | Line++; |
| 131 | |
| 132 | if (Line.is_at_end()) |
| 133 | return success(); |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 134 | |
Xinliang David Li | e3bf4fd3 | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 135 | uint32_t NumValueKinds; |
| 136 | if (Line->getAsInteger(10, NumValueKinds)) { |
| 137 | // No value profile data |
| 138 | return success(); |
| 139 | } |
| 140 | if (NumValueKinds == 0 || NumValueKinds > IPVK_Last + 1) |
| 141 | return error(instrprof_error::malformed); |
| 142 | Line++; |
| 143 | |
| 144 | for (uint32_t VK = 0; VK < NumValueKinds; VK++) { |
| 145 | VP_READ_ADVANCE(ValueKind); |
| 146 | if (ValueKind > IPVK_Last) |
| 147 | return error(instrprof_error::malformed); |
| 148 | VP_READ_ADVANCE(NumValueSites); |
| 149 | if (!NumValueSites) |
| 150 | continue; |
| 151 | |
| 152 | Record.reserveSites(VK, NumValueSites); |
| 153 | for (uint32_t S = 0; S < NumValueSites; S++) { |
| 154 | VP_READ_ADVANCE(NumValueData); |
| 155 | |
| 156 | std::vector<InstrProfValueData> CurrentValues; |
| 157 | for (uint32_t V = 0; V < NumValueData; V++) { |
| 158 | CHECK_LINE_END(Line); |
| 159 | std::pair<StringRef, StringRef> VD = Line->split(':'); |
| 160 | uint64_t TakenCount, Value; |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 161 | if (VK == IPVK_IndirectCallTarget) { |
| 162 | Symtab->addFuncName(VD.first); |
| 163 | Value = IndexedInstrProf::ComputeHash(VD.first); |
| 164 | } else { |
Xinliang David Li | e3bf4fd3 | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 165 | READ_NUM(VD.first, Value); |
| 166 | } |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 167 | READ_NUM(VD.second, TakenCount); |
Xinliang David Li | e3bf4fd3 | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 168 | CurrentValues.push_back({Value, TakenCount}); |
| 169 | Line++; |
| 170 | } |
| 171 | Record.addValueData(VK, S, CurrentValues.data(), NumValueData, nullptr); |
| 172 | } |
| 173 | } |
| 174 | return success(); |
| 175 | |
| 176 | #undef CHECK_LINE_END |
| 177 | #undef READ_NUM |
| 178 | #undef VP_READ_ADVANCE |
| 179 | } |
| 180 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 181 | std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) { |
Justin Bogner | cf36a36 | 2014-07-29 22:29:23 +0000 | [diff] [blame] | 182 | // Skip empty lines and comments. |
| 183 | while (!Line.is_at_end() && (Line->empty() || Line->startswith("#"))) |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 184 | ++Line; |
| 185 | // If we hit EOF while looking for a name, we're done. |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 186 | if (Line.is_at_end()) { |
| 187 | Symtab->finalizeSymtab(); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 188 | return error(instrprof_error::eof); |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 189 | } |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 190 | |
| 191 | // Read the function name. |
| 192 | Record.Name = *Line++; |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 193 | Symtab->addFuncName(Record.Name); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 194 | |
| 195 | // Read the function hash. |
| 196 | if (Line.is_at_end()) |
| 197 | return error(instrprof_error::truncated); |
Justin Bogner | f95ca07 | 2015-03-09 18:54:49 +0000 | [diff] [blame] | 198 | if ((Line++)->getAsInteger(0, Record.Hash)) |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 199 | return error(instrprof_error::malformed); |
| 200 | |
| 201 | // Read the number of counters. |
| 202 | uint64_t NumCounters; |
| 203 | if (Line.is_at_end()) |
| 204 | return error(instrprof_error::truncated); |
| 205 | if ((Line++)->getAsInteger(10, NumCounters)) |
| 206 | return error(instrprof_error::malformed); |
Justin Bogner | b59d7c7 | 2014-04-25 02:45:33 +0000 | [diff] [blame] | 207 | if (NumCounters == 0) |
| 208 | return error(instrprof_error::malformed); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 209 | |
| 210 | // Read each counter and fill our internal storage with the values. |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 211 | Record.Counts.clear(); |
| 212 | Record.Counts.reserve(NumCounters); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 213 | for (uint64_t I = 0; I < NumCounters; ++I) { |
| 214 | if (Line.is_at_end()) |
| 215 | return error(instrprof_error::truncated); |
| 216 | uint64_t Count; |
| 217 | if ((Line++)->getAsInteger(10, Count)) |
| 218 | return error(instrprof_error::malformed); |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 219 | Record.Counts.push_back(Count); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 220 | } |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 221 | |
Xinliang David Li | e3bf4fd3 | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 222 | // Check if value profile data exists and read it if so. |
| 223 | if (std::error_code EC = readValueProfileData(Record)) |
| 224 | return EC; |
| 225 | |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 226 | // This is needed to avoid two pass parsing because llvm-profdata |
| 227 | // does dumping while reading. |
| 228 | Symtab->finalizeSymtab(); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 229 | return success(); |
| 230 | } |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 231 | |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 232 | template <class IntPtrT> |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 233 | bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) { |
Duncan P. N. Exon Smith | d7d8347 | 2014-03-24 00:47:18 +0000 | [diff] [blame] | 234 | if (DataBuffer.getBufferSize() < sizeof(uint64_t)) |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 235 | return false; |
Duncan P. N. Exon Smith | d7d8347 | 2014-03-24 00:47:18 +0000 | [diff] [blame] | 236 | uint64_t Magic = |
| 237 | *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart()); |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 238 | return RawInstrProf::getMagic<IntPtrT>() == Magic || |
| 239 | sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic; |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | template <class IntPtrT> |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 243 | std::error_code RawInstrProfReader<IntPtrT>::readHeader() { |
Duncan P. N. Exon Smith | 09a67f4 | 2014-03-21 20:42:31 +0000 | [diff] [blame] | 244 | if (!hasFormat(*DataBuffer)) |
| 245 | return error(instrprof_error::bad_magic); |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 246 | if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header)) |
Duncan P. N. Exon Smith | 531bb48 | 2014-03-21 20:42:28 +0000 | [diff] [blame] | 247 | return error(instrprof_error::bad_header); |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 248 | auto *Header = reinterpret_cast<const RawInstrProf::Header *>( |
| 249 | DataBuffer->getBufferStart()); |
| 250 | ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>(); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 251 | return readHeader(*Header); |
| 252 | } |
| 253 | |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 254 | template <class IntPtrT> |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 255 | std::error_code |
| 256 | RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) { |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 257 | const char *End = DataBuffer->getBufferEnd(); |
| 258 | // Skip zero padding between profiles. |
| 259 | while (CurrentPos != End && *CurrentPos == 0) |
| 260 | ++CurrentPos; |
| 261 | // If there's nothing left, we're done. |
| 262 | if (CurrentPos == End) |
| 263 | return instrprof_error::eof; |
| 264 | // If there isn't enough space for another header, this is probably just |
| 265 | // garbage at the end of the file. |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 266 | if (CurrentPos + sizeof(RawInstrProf::Header) > End) |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 267 | return instrprof_error::malformed; |
Justin Bogner | 54b1128 | 2014-09-12 21:22:55 +0000 | [diff] [blame] | 268 | // The writer ensures each profile is padded to start at an aligned address. |
| 269 | if (reinterpret_cast<size_t>(CurrentPos) % alignOf<uint64_t>()) |
| 270 | return instrprof_error::malformed; |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 271 | // The magic should have the same byte order as in the previous header. |
| 272 | uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos); |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 273 | if (Magic != swap(RawInstrProf::getMagic<IntPtrT>())) |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 274 | return instrprof_error::bad_magic; |
| 275 | |
| 276 | // 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] | 277 | auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos); |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 278 | return readHeader(*Header); |
| 279 | } |
| 280 | |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 281 | template <class IntPtrT> |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 282 | void RawInstrProfReader<IntPtrT>::createSymtab(InstrProfSymtab &Symtab) { |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame^] | 283 | Symtab.create(StringRef(NamesStart, NamesSize)); |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 284 | for (const RawInstrProf::ProfileData<IntPtrT> *I = Data; I != DataEnd; ++I) { |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 285 | const IntPtrT FPtr = swap(I->FunctionPointer); |
| 286 | if (!FPtr) |
| 287 | continue; |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame^] | 288 | Symtab.mapAddress(FPtr, I->NameRef); |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 289 | } |
| 290 | Symtab.finalizeSymtab(); |
| 291 | } |
| 292 | |
| 293 | template <class IntPtrT> |
| 294 | std::error_code |
| 295 | RawInstrProfReader<IntPtrT>::readHeader(const RawInstrProf::Header &Header) { |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 296 | if (swap(Header.Version) != RawInstrProf::Version) |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 297 | return error(instrprof_error::unsupported_version); |
| 298 | |
| 299 | CountersDelta = swap(Header.CountersDelta); |
| 300 | NamesDelta = swap(Header.NamesDelta); |
| 301 | auto DataSize = swap(Header.DataSize); |
| 302 | auto CountersSize = swap(Header.CountersSize); |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame^] | 303 | NamesSize = swap(Header.NamesSize); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 304 | auto ValueDataSize = swap(Header.ValueDataSize); |
| 305 | ValueKindLast = swap(Header.ValueKindLast); |
| 306 | |
| 307 | auto DataSizeInBytes = DataSize * sizeof(RawInstrProf::ProfileData<IntPtrT>); |
| 308 | auto PaddingSize = getNumPaddingBytes(NamesSize); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 309 | |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 310 | ptrdiff_t DataOffset = sizeof(RawInstrProf::Header); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 311 | ptrdiff_t CountersOffset = DataOffset + DataSizeInBytes; |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 312 | ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 313 | ptrdiff_t ValueDataOffset = NamesOffset + NamesSize + PaddingSize; |
| 314 | size_t ProfileSize = ValueDataOffset + ValueDataSize; |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 315 | |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 316 | auto *Start = reinterpret_cast<const char *>(&Header); |
| 317 | if (Start + ProfileSize > DataBuffer->getBufferEnd()) |
Duncan P. N. Exon Smith | 531bb48 | 2014-03-21 20:42:28 +0000 | [diff] [blame] | 318 | return error(instrprof_error::bad_header); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 319 | |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 320 | Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>( |
| 321 | Start + DataOffset); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 322 | DataEnd = Data + DataSize; |
Duncan P. N. Exon Smith | d7d8347 | 2014-03-24 00:47:18 +0000 | [diff] [blame] | 323 | CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset); |
| 324 | NamesStart = Start + NamesOffset; |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 325 | ValueDataStart = reinterpret_cast<const uint8_t *>(Start + ValueDataOffset); |
Justin Bogner | a119f32 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 326 | ProfileEnd = Start + ProfileSize; |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 327 | |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 328 | std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>(); |
| 329 | createSymtab(*NewSymtab.get()); |
| 330 | Symtab = std::move(NewSymtab); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 331 | return success(); |
| 332 | } |
| 333 | |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 334 | template <class IntPtrT> |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 335 | std::error_code RawInstrProfReader<IntPtrT>::readName(InstrProfRecord &Record) { |
Xinliang David Li | a82d6c0 | 2016-02-08 18:13:49 +0000 | [diff] [blame^] | 336 | Record.Name = getName(Data->NameRef); |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 337 | return success(); |
| 338 | } |
| 339 | |
| 340 | template <class IntPtrT> |
| 341 | std::error_code RawInstrProfReader<IntPtrT>::readFuncHash( |
| 342 | InstrProfRecord &Record) { |
| 343 | Record.Hash = swap(Data->FuncHash); |
| 344 | return success(); |
| 345 | } |
| 346 | |
| 347 | template <class IntPtrT> |
| 348 | std::error_code RawInstrProfReader<IntPtrT>::readRawCounts( |
| 349 | InstrProfRecord &Record) { |
Justin Bogner | b59d7c7 | 2014-04-25 02:45:33 +0000 | [diff] [blame] | 350 | uint32_t NumCounters = swap(Data->NumCounters); |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 351 | IntPtrT CounterPtr = Data->CounterPtr; |
Justin Bogner | b59d7c7 | 2014-04-25 02:45:33 +0000 | [diff] [blame] | 352 | if (NumCounters == 0) |
| 353 | return error(instrprof_error::malformed); |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 354 | |
| 355 | auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters); |
| 356 | auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 357 | |
| 358 | // Check bounds. |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 359 | if (RawCounts.data() < CountersStart || |
Duncan P. N. Exon Smith | d7d8347 | 2014-03-24 00:47:18 +0000 | [diff] [blame] | 360 | RawCounts.data() + RawCounts.size() > NamesStartAsCounter) |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 361 | return error(instrprof_error::malformed); |
| 362 | |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 363 | if (ShouldSwapBytes) { |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 364 | Record.Counts.clear(); |
| 365 | Record.Counts.reserve(RawCounts.size()); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 366 | for (uint64_t Count : RawCounts) |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 367 | Record.Counts.push_back(swap(Count)); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 368 | } else |
| 369 | Record.Counts = RawCounts; |
| 370 | |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 371 | return success(); |
| 372 | } |
| 373 | |
| 374 | template <class IntPtrT> |
Xinliang David Li | 01cb9bd | 2015-12-04 01:02:10 +0000 | [diff] [blame] | 375 | std::error_code |
| 376 | RawInstrProfReader<IntPtrT>::readValueProfilingData(InstrProfRecord &Record) { |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 377 | |
| 378 | Record.clearValueData(); |
Xinliang David Li | d922c26 | 2015-12-11 06:53:53 +0000 | [diff] [blame] | 379 | CurValueDataSize = 0; |
| 380 | // Need to match the logic in value profile dumper code in compiler-rt: |
| 381 | uint32_t NumValueKinds = 0; |
| 382 | for (uint32_t I = 0; I < IPVK_Last + 1; I++) |
| 383 | NumValueKinds += (Data->NumValueSites[I] != 0); |
| 384 | |
| 385 | if (!NumValueKinds) |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 386 | return success(); |
| 387 | |
Xinliang David Li | 01cb9bd | 2015-12-04 01:02:10 +0000 | [diff] [blame] | 388 | ErrorOr<std::unique_ptr<ValueProfData>> VDataPtrOrErr = |
Xinliang David Li | d922c26 | 2015-12-11 06:53:53 +0000 | [diff] [blame] | 389 | ValueProfData::getValueProfData(ValueDataStart, |
Xinliang David Li | 01cb9bd | 2015-12-04 01:02:10 +0000 | [diff] [blame] | 390 | (const unsigned char *)ProfileEnd, |
| 391 | getDataEndianness()); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 392 | |
Xinliang David Li | 01cb9bd | 2015-12-04 01:02:10 +0000 | [diff] [blame] | 393 | if (VDataPtrOrErr.getError()) |
| 394 | return VDataPtrOrErr.getError(); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 395 | |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 396 | VDataPtrOrErr.get()->deserializeTo(Record, &Symtab->getAddrHashMap()); |
Xinliang David Li | d922c26 | 2015-12-11 06:53:53 +0000 | [diff] [blame] | 397 | CurValueDataSize = VDataPtrOrErr.get()->getSize(); |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 398 | return success(); |
| 399 | } |
| 400 | |
| 401 | template <class IntPtrT> |
Xinliang David Li | 01cb9bd | 2015-12-04 01:02:10 +0000 | [diff] [blame] | 402 | std::error_code |
| 403 | RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) { |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 404 | if (atEnd()) |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame] | 405 | if (std::error_code EC = readNextHeader(ProfileEnd)) |
| 406 | return EC; |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 407 | |
| 408 | // Read name ad set it in Record. |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame] | 409 | if (std::error_code EC = readName(Record)) |
| 410 | return EC; |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 411 | |
| 412 | // Read FuncHash and set it in Record. |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame] | 413 | if (std::error_code EC = readFuncHash(Record)) |
| 414 | return EC; |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 415 | |
| 416 | // Read raw counts and set Record. |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame] | 417 | if (std::error_code EC = readRawCounts(Record)) |
| 418 | return EC; |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 419 | |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 420 | // Read value data and set Record. |
Xinliang David Li | 2d4803e | 2015-12-10 23:48:05 +0000 | [diff] [blame] | 421 | if (std::error_code EC = readValueProfilingData(Record)) |
| 422 | return EC; |
Betul Buyukkurt | 6fac174 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 423 | |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 424 | // Iterate. |
Xinliang David Li | cf4a128 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 425 | advanceData(); |
Duncan P. N. Exon Smith | 24b4b65 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 426 | return success(); |
| 427 | } |
Duncan P. N. Exon Smith | 4680361 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 428 | |
| 429 | namespace llvm { |
| 430 | template class RawInstrProfReader<uint32_t>; |
| 431 | template class RawInstrProfReader<uint64_t>; |
| 432 | } |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 433 | |
Justin Bogner | b5d368e | 2014-04-18 22:00:22 +0000 | [diff] [blame] | 434 | InstrProfLookupTrait::hash_value_type |
| 435 | InstrProfLookupTrait::ComputeHash(StringRef K) { |
| 436 | return IndexedInstrProf::ComputeHash(HashType, K); |
| 437 | } |
| 438 | |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 439 | typedef InstrProfLookupTrait::data_type data_type; |
| 440 | typedef InstrProfLookupTrait::offset_type offset_type; |
| 441 | |
Xinliang David Li | be969c2 | 2015-11-28 05:06:00 +0000 | [diff] [blame] | 442 | bool InstrProfLookupTrait::readValueProfilingData( |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 443 | const unsigned char *&D, const unsigned char *const End) { |
Xinliang David Li | 9955687 | 2015-11-17 23:00:40 +0000 | [diff] [blame] | 444 | ErrorOr<std::unique_ptr<ValueProfData>> VDataPtrOrErr = |
| 445 | ValueProfData::getValueProfData(D, End, ValueProfDataEndianness); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 446 | |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 447 | if (VDataPtrOrErr.getError()) |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 448 | return false; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 449 | |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 450 | VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 451 | D += VDataPtrOrErr.get()->TotalSize; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 452 | |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 453 | return true; |
| 454 | } |
| 455 | |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 456 | data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D, |
| 457 | offset_type N) { |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 458 | // Check if the data is corrupt. If so, don't try to read it. |
| 459 | if (N % sizeof(uint64_t)) |
| 460 | return data_type(); |
| 461 | |
| 462 | DataBuffer.clear(); |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 463 | std::vector<uint64_t> CounterBuffer; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 464 | |
| 465 | using namespace support; |
| 466 | const unsigned char *End = D + N; |
| 467 | while (D < End) { |
Xinliang David Li | c758387 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 468 | // Read hash. |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 469 | if (D + sizeof(uint64_t) >= End) |
| 470 | return data_type(); |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 471 | uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D); |
| 472 | |
Xinliang David Li | c758387 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 473 | // Initialize number of counters for FormatVersion == 1. |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 474 | uint64_t CountsSize = N / sizeof(uint64_t) - 1; |
Xinliang David Li | c758387 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 475 | // If format version is different then read the number of counters. |
Xinliang David Li | a6b2c4f | 2016-01-14 02:47:01 +0000 | [diff] [blame] | 476 | if (FormatVersion != IndexedInstrProf::ProfVersion::Version1) { |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 477 | if (D + sizeof(uint64_t) > End) |
| 478 | return data_type(); |
| 479 | CountsSize = endian::readNext<uint64_t, little, unaligned>(D); |
| 480 | } |
Xinliang David Li | c758387 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 481 | // Read counter values. |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 482 | if (D + CountsSize * sizeof(uint64_t) > End) |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 483 | return data_type(); |
| 484 | |
| 485 | CounterBuffer.clear(); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 486 | CounterBuffer.reserve(CountsSize); |
| 487 | for (uint64_t J = 0; J < CountsSize; ++J) |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 488 | CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D)); |
| 489 | |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 490 | DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer)); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 491 | |
Xinliang David Li | c758387 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 492 | // Read value profiling data. |
Xinliang David Li | a6b2c4f | 2016-01-14 02:47:01 +0000 | [diff] [blame] | 493 | if (FormatVersion > IndexedInstrProf::ProfVersion::Version2 && |
| 494 | !readValueProfilingData(D, End)) { |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 495 | DataBuffer.clear(); |
| 496 | return data_type(); |
| 497 | } |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 498 | } |
| 499 | return DataBuffer; |
| 500 | } |
| 501 | |
Xinliang David Li | a28306d | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 502 | template <typename HashTableImpl> |
| 503 | std::error_code InstrProfReaderIndex<HashTableImpl>::getRecords( |
| 504 | StringRef FuncName, ArrayRef<InstrProfRecord> &Data) { |
| 505 | auto Iter = HashTable->find(FuncName); |
| 506 | if (Iter == HashTable->end()) |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame] | 507 | return instrprof_error::unknown_function; |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 508 | |
| 509 | Data = (*Iter); |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame] | 510 | if (Data.empty()) |
| 511 | return instrprof_error::malformed; |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 512 | |
| 513 | return instrprof_error::success; |
| 514 | } |
| 515 | |
Xinliang David Li | a28306d | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 516 | template <typename HashTableImpl> |
| 517 | std::error_code InstrProfReaderIndex<HashTableImpl>::getRecords( |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 518 | ArrayRef<InstrProfRecord> &Data) { |
Xinliang David Li | a28306d | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 519 | if (atEnd()) |
| 520 | return instrprof_error::eof; |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 521 | |
| 522 | Data = *RecordIterator; |
| 523 | |
Xinliang David Li | 2d4803e | 2015-12-10 23:48:05 +0000 | [diff] [blame] | 524 | if (Data.empty()) |
| 525 | return instrprof_error::malformed; |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 526 | |
| 527 | return instrprof_error::success; |
| 528 | } |
| 529 | |
Xinliang David Li | a28306d | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 530 | template <typename HashTableImpl> |
| 531 | InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex( |
| 532 | const unsigned char *Buckets, const unsigned char *const Payload, |
| 533 | const unsigned char *const Base, IndexedInstrProf::HashT HashType, |
| 534 | uint64_t Version) { |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 535 | FormatVersion = Version; |
Xinliang David Li | a28306d | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 536 | HashTable.reset(HashTableImpl::Create( |
| 537 | Buckets, Payload, Base, |
| 538 | typename HashTableImpl::InfoType(HashType, Version))); |
Xinliang David Li | a28306d | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 539 | RecordIterator = HashTable->data_begin(); |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 542 | bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) { |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame] | 543 | if (DataBuffer.getBufferSize() < 8) |
| 544 | return false; |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 545 | using namespace support; |
| 546 | uint64_t Magic = |
| 547 | endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart()); |
Xinliang David Li | c758387 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 548 | // Verify that it's magical. |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 549 | return Magic == IndexedInstrProf::Magic; |
| 550 | } |
| 551 | |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 552 | const unsigned char * |
| 553 | IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version, |
| 554 | const unsigned char *Cur) { |
| 555 | using namespace support; |
| 556 | if (Version >= IndexedInstrProf::Version4) { |
| 557 | const IndexedInstrProf::Summary *SummaryInLE = |
| 558 | reinterpret_cast<const IndexedInstrProf::Summary *>(Cur); |
| 559 | uint64_t NFields = |
| 560 | endian::byte_swap<uint64_t, little>(SummaryInLE->NumSummaryFields); |
| 561 | uint64_t NEntries = |
| 562 | endian::byte_swap<uint64_t, little>(SummaryInLE->NumCutoffEntries); |
| 563 | uint32_t SummarySize = |
| 564 | IndexedInstrProf::Summary::getSize(NFields, NEntries); |
| 565 | std::unique_ptr<IndexedInstrProf::Summary> SummaryData = |
| 566 | IndexedInstrProf::allocSummary(SummarySize); |
| 567 | |
| 568 | const uint64_t *Src = reinterpret_cast<const uint64_t *>(SummaryInLE); |
| 569 | uint64_t *Dst = reinterpret_cast<uint64_t *>(SummaryData.get()); |
| 570 | for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++) |
| 571 | Dst[I] = endian::byte_swap<uint64_t, little>(Src[I]); |
| 572 | |
| 573 | // initialize ProfileSummary using the SummaryData from disk. |
| 574 | this->Summary = llvm::make_unique<ProfileSummary>(*(SummaryData.get())); |
| 575 | return Cur + SummarySize; |
| 576 | } else { |
| 577 | // For older version of profile data, we need to compute on the fly: |
| 578 | using namespace IndexedInstrProf; |
| 579 | std::vector<uint32_t> Cutoffs(&SummaryCutoffs[0], |
| 580 | &SummaryCutoffs[NumSummaryCutoffs]); |
| 581 | this->Summary = llvm::make_unique<ProfileSummary>(Cutoffs); |
| 582 | this->Summary->computeDetailedSummary(); |
| 583 | return Cur; |
| 584 | } |
| 585 | } |
| 586 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 587 | std::error_code IndexedInstrProfReader::readHeader() { |
Aaron Ballman | a7c9ed5 | 2014-05-01 17:16:24 +0000 | [diff] [blame] | 588 | const unsigned char *Start = |
| 589 | (const unsigned char *)DataBuffer->getBufferStart(); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 590 | const unsigned char *Cur = Start; |
Aaron Ballman | a7c9ed5 | 2014-05-01 17:16:24 +0000 | [diff] [blame] | 591 | if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24) |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 592 | return error(instrprof_error::truncated); |
| 593 | |
| 594 | using namespace support; |
| 595 | |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 596 | auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur); |
| 597 | Cur += sizeof(IndexedInstrProf::Header); |
| 598 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 599 | // Check the magic number. |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 600 | uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 601 | if (Magic != IndexedInstrProf::Magic) |
| 602 | return error(instrprof_error::bad_magic); |
| 603 | |
| 604 | // Read the version. |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 605 | uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version); |
Xinliang David Li | a6b2c4f | 2016-01-14 02:47:01 +0000 | [diff] [blame] | 606 | if (FormatVersion > IndexedInstrProf::ProfVersion::CurrentVersion) |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 607 | return error(instrprof_error::unsupported_version); |
| 608 | |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 609 | Cur = readSummary((IndexedInstrProf::ProfVersion)FormatVersion, Cur); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 610 | |
| 611 | // Read the hash type and start offset. |
| 612 | IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>( |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 613 | endian::byte_swap<uint64_t, little>(Header->HashType)); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 614 | if (HashType > IndexedInstrProf::HashT::Last) |
| 615 | return error(instrprof_error::unsupported_hash_type); |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 616 | |
| 617 | uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 618 | |
| 619 | // The rest of the file is an on disk hash table. |
Xinliang David Li | a28306d | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 620 | InstrProfReaderIndexBase *IndexPtr = nullptr; |
| 621 | IndexPtr = new InstrProfReaderIndex<OnDiskHashTableImplV3>( |
| 622 | Start + HashOffset, Cur, Start, HashType, FormatVersion); |
| 623 | Index.reset(IndexPtr); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 624 | return success(); |
| 625 | } |
| 626 | |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 627 | InstrProfSymtab &IndexedInstrProfReader::getSymtab() { |
| 628 | if (Symtab.get()) |
| 629 | return *Symtab.get(); |
| 630 | |
| 631 | std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>(); |
| 632 | Index->populateSymtab(*NewSymtab.get()); |
| 633 | |
| 634 | Symtab = std::move(NewSymtab); |
| 635 | return *Symtab.get(); |
| 636 | } |
| 637 | |
Xinliang David Li | 6aa216c | 2015-11-06 07:54:21 +0000 | [diff] [blame] | 638 | ErrorOr<InstrProfRecord> |
| 639 | IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName, |
| 640 | uint64_t FuncHash) { |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 641 | ArrayRef<InstrProfRecord> Data; |
Xinliang David Li | a28306d | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 642 | std::error_code EC = Index->getRecords(FuncName, Data); |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame] | 643 | if (EC != instrprof_error::success) |
| 644 | return EC; |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 645 | // Found it. Look for counters with the right hash. |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 646 | for (unsigned I = 0, E = Data.size(); I < E; ++I) { |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 647 | // Check for a match and fill the vector if there is one. |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 648 | if (Data[I].Hash == FuncHash) { |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 649 | return std::move(Data[I]); |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | return error(instrprof_error::hash_mismatch); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Xinliang David Li | 6aa216c | 2015-11-06 07:54:21 +0000 | [diff] [blame] | 655 | std::error_code |
| 656 | IndexedInstrProfReader::getFunctionCounts(StringRef FuncName, uint64_t FuncHash, |
| 657 | std::vector<uint64_t> &Counts) { |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 658 | ErrorOr<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash); |
Xinliang David Li | 6aa216c | 2015-11-06 07:54:21 +0000 | [diff] [blame] | 659 | if (std::error_code EC = Record.getError()) |
| 660 | return EC; |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 661 | |
| 662 | Counts = Record.get().Counts; |
| 663 | return success(); |
| 664 | } |
| 665 | |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 666 | std::error_code IndexedInstrProfReader::readNextRecord( |
| 667 | InstrProfRecord &Record) { |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 668 | static unsigned RecordIndex = 0; |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 669 | |
| 670 | ArrayRef<InstrProfRecord> Data; |
| 671 | |
Xinliang David Li | a28306d | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 672 | std::error_code EC = Index->getRecords(Data); |
Xinliang David Li | 4c3ab81 | 2015-11-12 00:32:17 +0000 | [diff] [blame] | 673 | if (EC != instrprof_error::success) |
| 674 | return error(EC); |
Xinliang David Li | 140f4c4 | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 675 | |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 676 | Record = Data[RecordIndex++]; |
| 677 | if (RecordIndex >= Data.size()) { |
Xinliang David Li | a28306d | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 678 | Index->advanceToNextKey(); |
Justin Bogner | 3a7d44c | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 679 | RecordIndex = 0; |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 680 | } |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 681 | return success(); |
| 682 | } |