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