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