Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 1 | //===- InstrProfReader.cpp - Instrumented profiling reader ----------------===// |
Justin Bogner | c0f3b72 | 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 | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/ProfileData/InstrProfReader.h" |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ArrayRef.h" |
Richard Smith | 3b1e430 | 2018-10-10 21:09:37 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Benjamin Kramer | d59c5f9 | 2015-03-01 21:28:53 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringRef.h" |
| 20 | #include "llvm/IR/ProfileSummary.h" |
| 21 | #include "llvm/ProfileData/InstrProf.h" |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 22 | #include "llvm/ProfileData/ProfileCommon.h" |
| 23 | #include "llvm/Support/Endian.h" |
| 24 | #include "llvm/Support/Error.h" |
| 25 | #include "llvm/Support/ErrorOr.h" |
| 26 | #include "llvm/Support/MemoryBuffer.h" |
Richard Smith | 3b1e430 | 2018-10-10 21:09:37 +0000 | [diff] [blame] | 27 | #include "llvm/Support/SymbolRemappingReader.h" |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 28 | #include "llvm/Support/SwapByteOrder.h" |
| 29 | #include <algorithm> |
| 30 | #include <cctype> |
| 31 | #include <cstddef> |
| 32 | #include <cstdint> |
| 33 | #include <limits> |
| 34 | #include <memory> |
| 35 | #include <system_error> |
| 36 | #include <utility> |
| 37 | #include <vector> |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace llvm; |
| 40 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 41 | static Expected<std::unique_ptr<MemoryBuffer>> |
Benjamin Kramer | 2ff22b7 | 2016-05-29 10:31:00 +0000 | [diff] [blame] | 42 | setupMemoryBuffer(const Twine &Path) { |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 43 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 44 | MemoryBuffer::getFileOrSTDIN(Path); |
| 45 | if (std::error_code EC = BufferOrErr.getError()) |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 46 | return errorCodeToError(EC); |
Justin Bogner | 09e5af7 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 47 | return std::move(BufferOrErr.get()); |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 50 | static Error initializeReader(InstrProfReader &Reader) { |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 51 | return Reader.readHeader(); |
| 52 | } |
| 53 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 54 | Expected<std::unique_ptr<InstrProfReader>> |
Benjamin Kramer | 2ff22b7 | 2016-05-29 10:31:00 +0000 | [diff] [blame] | 55 | InstrProfReader::create(const Twine &Path) { |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 56 | // Set up the buffer to read. |
Diego Novillo | 2b6bd7a | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 57 | auto BufferOrError = setupMemoryBuffer(Path); |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 58 | if (Error E = BufferOrError.takeError()) |
| 59 | return std::move(E); |
Justin Bogner | 09e5af7 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 60 | return InstrProfReader::create(std::move(BufferOrError.get())); |
| 61 | } |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 62 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 63 | Expected<std::unique_ptr<InstrProfReader>> |
Justin Bogner | 09e5af7 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 64 | InstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) { |
| 65 | // Sanity check the buffer. |
Zachary Turner | ece9b23 | 2017-12-14 22:07:03 +0000 | [diff] [blame] | 66 | if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<unsigned>::max()) |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 67 | return make_error<InstrProfError>(instrprof_error::too_large); |
Justin Bogner | 09e5af7 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 68 | |
Rong Xu | c887230 | 2016-10-19 22:51:17 +0000 | [diff] [blame] | 69 | if (Buffer->getBufferSize() == 0) |
| 70 | return make_error<InstrProfError>(instrprof_error::empty_raw_profile); |
| 71 | |
Diego Novillo | 2b6bd7a | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 72 | std::unique_ptr<InstrProfReader> Result; |
Duncan P. N. Exon Smith | bb56c48 | 2014-03-21 20:42:31 +0000 | [diff] [blame] | 73 | // Create the reader. |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 74 | if (IndexedInstrProfReader::hasFormat(*Buffer)) |
| 75 | Result.reset(new IndexedInstrProfReader(std::move(Buffer))); |
| 76 | else if (RawInstrProfReader64::hasFormat(*Buffer)) |
Duncan P. N. Exon Smith | 0695b20 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 77 | Result.reset(new RawInstrProfReader64(std::move(Buffer))); |
| 78 | else if (RawInstrProfReader32::hasFormat(*Buffer)) |
| 79 | Result.reset(new RawInstrProfReader32(std::move(Buffer))); |
Nathan Slingerland | 572e633 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 80 | else if (TextInstrProfReader::hasFormat(*Buffer)) |
Nathan Slingerland | 69c9ea3 | 2015-11-12 18:39:26 +0000 | [diff] [blame] | 81 | Result.reset(new TextInstrProfReader(std::move(Buffer))); |
Nathan Slingerland | 572e633 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 82 | else |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 83 | return make_error<InstrProfError>(instrprof_error::unrecognized_format); |
Duncan P. N. Exon Smith | bb56c48 | 2014-03-21 20:42:31 +0000 | [diff] [blame] | 84 | |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 85 | // Initialize the reader and return the result. |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 86 | if (Error E = initializeReader(*Result)) |
| 87 | return std::move(E); |
Diego Novillo | 2b6bd7a | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 88 | |
| 89 | return std::move(Result); |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 92 | Expected<std::unique_ptr<IndexedInstrProfReader>> |
Richard Smith | 3b1e430 | 2018-10-10 21:09:37 +0000 | [diff] [blame] | 93 | IndexedInstrProfReader::create(const Twine &Path, const Twine &RemappingPath) { |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 94 | // Set up the buffer to read. |
Diego Novillo | 2b6bd7a | 2014-11-03 00:51:45 +0000 | [diff] [blame] | 95 | auto BufferOrError = setupMemoryBuffer(Path); |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 96 | if (Error E = BufferOrError.takeError()) |
| 97 | return std::move(E); |
Richard Smith | 3b1e430 | 2018-10-10 21:09:37 +0000 | [diff] [blame] | 98 | |
| 99 | // Set up the remapping buffer if requested. |
| 100 | std::unique_ptr<MemoryBuffer> RemappingBuffer; |
| 101 | std::string RemappingPathStr = RemappingPath.str(); |
| 102 | if (!RemappingPathStr.empty()) { |
| 103 | auto RemappingBufferOrError = setupMemoryBuffer(RemappingPathStr); |
| 104 | if (Error E = RemappingBufferOrError.takeError()) |
| 105 | return std::move(E); |
| 106 | RemappingBuffer = std::move(RemappingBufferOrError.get()); |
| 107 | } |
| 108 | |
| 109 | return IndexedInstrProfReader::create(std::move(BufferOrError.get()), |
| 110 | std::move(RemappingBuffer)); |
Justin Bogner | 09e5af7 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 111 | } |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 112 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 113 | Expected<std::unique_ptr<IndexedInstrProfReader>> |
Richard Smith | 3b1e430 | 2018-10-10 21:09:37 +0000 | [diff] [blame] | 114 | IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer, |
| 115 | std::unique_ptr<MemoryBuffer> RemappingBuffer) { |
Justin Bogner | 09e5af7 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 116 | // Sanity check the buffer. |
Zachary Turner | ece9b23 | 2017-12-14 22:07:03 +0000 | [diff] [blame] | 117 | if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<unsigned>::max()) |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 118 | return make_error<InstrProfError>(instrprof_error::too_large); |
Justin Bogner | e8e3eb8 | 2015-02-16 21:28:58 +0000 | [diff] [blame] | 119 | |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 120 | // Create the reader. |
| 121 | if (!IndexedInstrProfReader::hasFormat(*Buffer)) |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 122 | return make_error<InstrProfError>(instrprof_error::bad_magic); |
Richard Smith | 3b1e430 | 2018-10-10 21:09:37 +0000 | [diff] [blame] | 123 | auto Result = llvm::make_unique<IndexedInstrProfReader>( |
| 124 | std::move(Buffer), std::move(RemappingBuffer)); |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 125 | |
| 126 | // Initialize the reader and return the result. |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 127 | if (Error E = initializeReader(*Result)) |
| 128 | return std::move(E); |
Justin Bogner | e8e3eb8 | 2015-02-16 21:28:58 +0000 | [diff] [blame] | 129 | |
| 130 | return std::move(Result); |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void InstrProfIterator::Increment() { |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 134 | if (auto E = Reader->readNextRecord(Record)) { |
| 135 | // Handle errors in the reader. |
| 136 | InstrProfError::take(std::move(E)); |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 137 | *this = InstrProfIterator(); |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 138 | } |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Nathan Slingerland | 572e633 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 141 | bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) { |
| 142 | // Verify that this really looks like plain ASCII text by checking a |
| 143 | // 'reasonable' number of characters (up to profile magic size). |
| 144 | size_t count = std::min(Buffer.getBufferSize(), sizeof(uint64_t)); |
| 145 | StringRef buffer = Buffer.getBufferStart(); |
Vedant Kumar | 88b8ce2 | 2015-12-11 00:40:05 +0000 | [diff] [blame] | 146 | return count == 0 || |
| 147 | std::all_of(buffer.begin(), buffer.begin() + count, |
Michael Kruse | 6069e66 | 2018-07-26 15:31:41 +0000 | [diff] [blame] | 148 | [](char c) { return isPrint(c) || ::isspace(c); }); |
Nathan Slingerland | 572e633 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Rong Xu | c7d7fb0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 151 | // Read the profile variant flag from the header: ":FE" means this is a FE |
| 152 | // generated profile. ":IR" means this is an IR level profile. Other strings |
| 153 | // with a leading ':' will be reported an error format. |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 154 | Error TextInstrProfReader::readHeader() { |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 155 | Symtab.reset(new InstrProfSymtab()); |
Rong Xu | c7d7fb0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 156 | bool IsIRInstr = false; |
| 157 | if (!Line->startswith(":")) { |
| 158 | IsIRLevelProfile = false; |
| 159 | return success(); |
| 160 | } |
| 161 | StringRef Str = (Line)->substr(1); |
| 162 | if (Str.equals_lower("ir")) |
| 163 | IsIRInstr = true; |
| 164 | else if (Str.equals_lower("fe")) |
| 165 | IsIRInstr = false; |
| 166 | else |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 167 | return error(instrprof_error::bad_header); |
Rong Xu | c7d7fb0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 168 | |
| 169 | ++Line; |
| 170 | IsIRLevelProfile = IsIRInstr; |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 171 | return success(); |
| 172 | } |
| 173 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 174 | Error |
Xinliang David Li | ea0570f | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 175 | TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) { |
| 176 | |
| 177 | #define CHECK_LINE_END(Line) \ |
| 178 | if (Line.is_at_end()) \ |
| 179 | return error(instrprof_error::truncated); |
| 180 | #define READ_NUM(Str, Dst) \ |
| 181 | if ((Str).getAsInteger(10, (Dst))) \ |
| 182 | return error(instrprof_error::malformed); |
| 183 | #define VP_READ_ADVANCE(Val) \ |
| 184 | CHECK_LINE_END(Line); \ |
| 185 | uint32_t Val; \ |
| 186 | READ_NUM((*Line), (Val)); \ |
| 187 | Line++; |
| 188 | |
| 189 | if (Line.is_at_end()) |
| 190 | return success(); |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 191 | |
Xinliang David Li | ea0570f | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 192 | uint32_t NumValueKinds; |
| 193 | if (Line->getAsInteger(10, NumValueKinds)) { |
| 194 | // No value profile data |
| 195 | return success(); |
| 196 | } |
| 197 | if (NumValueKinds == 0 || NumValueKinds > IPVK_Last + 1) |
| 198 | return error(instrprof_error::malformed); |
| 199 | Line++; |
| 200 | |
| 201 | for (uint32_t VK = 0; VK < NumValueKinds; VK++) { |
| 202 | VP_READ_ADVANCE(ValueKind); |
| 203 | if (ValueKind > IPVK_Last) |
| 204 | return error(instrprof_error::malformed); |
| 205 | VP_READ_ADVANCE(NumValueSites); |
| 206 | if (!NumValueSites) |
| 207 | continue; |
| 208 | |
| 209 | Record.reserveSites(VK, NumValueSites); |
| 210 | for (uint32_t S = 0; S < NumValueSites; S++) { |
| 211 | VP_READ_ADVANCE(NumValueData); |
| 212 | |
| 213 | std::vector<InstrProfValueData> CurrentValues; |
| 214 | for (uint32_t V = 0; V < NumValueData; V++) { |
| 215 | CHECK_LINE_END(Line); |
Rong Xu | 3a963d1 | 2016-05-06 23:20:58 +0000 | [diff] [blame] | 216 | std::pair<StringRef, StringRef> VD = Line->rsplit(':'); |
Xinliang David Li | ea0570f | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 217 | uint64_t TakenCount, Value; |
Rong Xu | 676fd31 | 2017-02-27 21:42:39 +0000 | [diff] [blame] | 218 | if (ValueKind == IPVK_IndirectCallTarget) { |
Mircea Trofin | 21d9c7a | 2018-03-22 21:26:52 +0000 | [diff] [blame] | 219 | if (InstrProfSymtab::isExternalSymbol(VD.first)) { |
| 220 | Value = 0; |
| 221 | } else { |
| 222 | if (Error E = Symtab->addFuncName(VD.first)) |
| 223 | return E; |
| 224 | Value = IndexedInstrProf::ComputeHash(VD.first); |
| 225 | } |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 226 | } else { |
Xinliang David Li | ea0570f | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 227 | READ_NUM(VD.first, Value); |
| 228 | } |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 229 | READ_NUM(VD.second, TakenCount); |
Xinliang David Li | ea0570f | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 230 | CurrentValues.push_back({Value, TakenCount}); |
| 231 | Line++; |
| 232 | } |
Rong Xu | 676fd31 | 2017-02-27 21:42:39 +0000 | [diff] [blame] | 233 | Record.addValueData(ValueKind, S, CurrentValues.data(), NumValueData, |
| 234 | nullptr); |
Xinliang David Li | ea0570f | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | return success(); |
| 238 | |
| 239 | #undef CHECK_LINE_END |
| 240 | #undef READ_NUM |
| 241 | #undef VP_READ_ADVANCE |
| 242 | } |
| 243 | |
David Blaikie | 450ef2a | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 244 | Error TextInstrProfReader::readNextRecord(NamedInstrProfRecord &Record) { |
Justin Bogner | 12855f6 | 2014-07-29 22:29:23 +0000 | [diff] [blame] | 245 | // Skip empty lines and comments. |
| 246 | while (!Line.is_at_end() && (Line->empty() || Line->startswith("#"))) |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 247 | ++Line; |
| 248 | // If we hit EOF while looking for a name, we're done. |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 249 | if (Line.is_at_end()) { |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 250 | return error(instrprof_error::eof); |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 251 | } |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 252 | |
| 253 | // Read the function name. |
| 254 | Record.Name = *Line++; |
Vedant Kumar | b945463 | 2017-06-20 01:38:56 +0000 | [diff] [blame] | 255 | if (Error E = Symtab->addFuncName(Record.Name)) |
Mircea Trofin | 21d9c7a | 2018-03-22 21:26:52 +0000 | [diff] [blame] | 256 | return error(std::move(E)); |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 257 | |
| 258 | // Read the function hash. |
| 259 | if (Line.is_at_end()) |
| 260 | return error(instrprof_error::truncated); |
Justin Bogner | cbbfb05 | 2015-03-09 18:54:49 +0000 | [diff] [blame] | 261 | if ((Line++)->getAsInteger(0, Record.Hash)) |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 262 | return error(instrprof_error::malformed); |
| 263 | |
| 264 | // Read the number of counters. |
| 265 | uint64_t NumCounters; |
| 266 | if (Line.is_at_end()) |
| 267 | return error(instrprof_error::truncated); |
| 268 | if ((Line++)->getAsInteger(10, NumCounters)) |
| 269 | return error(instrprof_error::malformed); |
Justin Bogner | 38ac7e9 | 2014-04-25 02:45:33 +0000 | [diff] [blame] | 270 | if (NumCounters == 0) |
| 271 | return error(instrprof_error::malformed); |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 272 | |
| 273 | // Read each counter and fill our internal storage with the values. |
Rong Xu | 5497dd5 | 2017-03-03 21:56:34 +0000 | [diff] [blame] | 274 | Record.Clear(); |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 275 | Record.Counts.reserve(NumCounters); |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 276 | for (uint64_t I = 0; I < NumCounters; ++I) { |
| 277 | if (Line.is_at_end()) |
| 278 | return error(instrprof_error::truncated); |
| 279 | uint64_t Count; |
| 280 | if ((Line++)->getAsInteger(10, Count)) |
| 281 | return error(instrprof_error::malformed); |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 282 | Record.Counts.push_back(Count); |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 283 | } |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 284 | |
Xinliang David Li | ea0570f | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 285 | // Check if value profile data exists and read it if so. |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 286 | if (Error E = readValueProfileData(Record)) |
Mircea Trofin | 21d9c7a | 2018-03-22 21:26:52 +0000 | [diff] [blame] | 287 | return error(std::move(E)); |
Xinliang David Li | ea0570f | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 288 | |
Justin Bogner | c0f3b72 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 289 | return success(); |
| 290 | } |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 291 | |
Duncan P. N. Exon Smith | 0695b20 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 292 | template <class IntPtrT> |
Duncan P. N. Exon Smith | 0695b20 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 293 | bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) { |
Duncan P. N. Exon Smith | b83e788 | 2014-03-24 00:47:18 +0000 | [diff] [blame] | 294 | if (DataBuffer.getBufferSize() < sizeof(uint64_t)) |
Duncan P. N. Exon Smith | 0695b20 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 295 | return false; |
Duncan P. N. Exon Smith | b83e788 | 2014-03-24 00:47:18 +0000 | [diff] [blame] | 296 | uint64_t Magic = |
| 297 | *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart()); |
Xinliang David Li | 09717fa | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 298 | return RawInstrProf::getMagic<IntPtrT>() == Magic || |
| 299 | sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic; |
Duncan P. N. Exon Smith | 0695b20 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | template <class IntPtrT> |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 303 | Error RawInstrProfReader<IntPtrT>::readHeader() { |
Duncan P. N. Exon Smith | bb56c48 | 2014-03-21 20:42:31 +0000 | [diff] [blame] | 304 | if (!hasFormat(*DataBuffer)) |
| 305 | return error(instrprof_error::bad_magic); |
Xinliang David Li | 09717fa | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 306 | if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header)) |
Duncan P. N. Exon Smith | ebaeaa9 | 2014-03-21 20:42:28 +0000 | [diff] [blame] | 307 | return error(instrprof_error::bad_header); |
Xinliang David Li | 09717fa | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 308 | auto *Header = reinterpret_cast<const RawInstrProf::Header *>( |
| 309 | DataBuffer->getBufferStart()); |
| 310 | ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>(); |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 311 | return readHeader(*Header); |
| 312 | } |
| 313 | |
Justin Bogner | 6d81fc7 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 314 | template <class IntPtrT> |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 315 | Error RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) { |
Justin Bogner | 6d81fc7 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 316 | const char *End = DataBuffer->getBufferEnd(); |
| 317 | // Skip zero padding between profiles. |
| 318 | while (CurrentPos != End && *CurrentPos == 0) |
| 319 | ++CurrentPos; |
| 320 | // If there's nothing left, we're done. |
| 321 | if (CurrentPos == End) |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 322 | return make_error<InstrProfError>(instrprof_error::eof); |
Justin Bogner | 6d81fc7 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 323 | // If there isn't enough space for another header, this is probably just |
| 324 | // garbage at the end of the file. |
Xinliang David Li | 09717fa | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 325 | if (CurrentPos + sizeof(RawInstrProf::Header) > End) |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 326 | return make_error<InstrProfError>(instrprof_error::malformed); |
Justin Bogner | 824444e | 2014-09-12 21:22:55 +0000 | [diff] [blame] | 327 | // The writer ensures each profile is padded to start at an aligned address. |
Benjamin Kramer | cb58e1e | 2016-10-20 15:02:18 +0000 | [diff] [blame] | 328 | if (reinterpret_cast<size_t>(CurrentPos) % alignof(uint64_t)) |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 329 | return make_error<InstrProfError>(instrprof_error::malformed); |
Justin Bogner | 6d81fc7 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 330 | // The magic should have the same byte order as in the previous header. |
| 331 | uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos); |
Xinliang David Li | 09717fa | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 332 | if (Magic != swap(RawInstrProf::getMagic<IntPtrT>())) |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 333 | return make_error<InstrProfError>(instrprof_error::bad_magic); |
Justin Bogner | 6d81fc7 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 334 | |
| 335 | // There's another profile to read, so we need to process the header. |
Xinliang David Li | 09717fa | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 336 | auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos); |
Justin Bogner | 6d81fc7 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 337 | return readHeader(*Header); |
| 338 | } |
| 339 | |
Duncan P. N. Exon Smith | 0695b20 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 340 | template <class IntPtrT> |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 341 | Error RawInstrProfReader<IntPtrT>::createSymtab(InstrProfSymtab &Symtab) { |
| 342 | if (Error E = Symtab.create(StringRef(NamesStart, NamesSize))) |
| 343 | return error(std::move(E)); |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 344 | for (const RawInstrProf::ProfileData<IntPtrT> *I = Data; I != DataEnd; ++I) { |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 345 | const IntPtrT FPtr = swap(I->FunctionPointer); |
| 346 | if (!FPtr) |
| 347 | continue; |
Xinliang David Li | 04638a6 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 348 | Symtab.mapAddress(FPtr, I->NameRef); |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 349 | } |
Vedant Kumar | 089f755 | 2016-04-21 21:07:25 +0000 | [diff] [blame] | 350 | return success(); |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | template <class IntPtrT> |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 354 | Error RawInstrProfReader<IntPtrT>::readHeader( |
| 355 | const RawInstrProf::Header &Header) { |
Rong Xu | c7d7fb0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 356 | Version = swap(Header.Version); |
| 357 | if (GET_VERSION(Version) != RawInstrProf::Version) |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 358 | return error(instrprof_error::unsupported_version); |
| 359 | |
| 360 | CountersDelta = swap(Header.CountersDelta); |
| 361 | NamesDelta = swap(Header.NamesDelta); |
| 362 | auto DataSize = swap(Header.DataSize); |
| 363 | auto CountersSize = swap(Header.CountersSize); |
Xinliang David Li | 04638a6 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 364 | NamesSize = swap(Header.NamesSize); |
Betul Buyukkurt | a5be9e3 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 365 | ValueKindLast = swap(Header.ValueKindLast); |
| 366 | |
| 367 | auto DataSizeInBytes = DataSize * sizeof(RawInstrProf::ProfileData<IntPtrT>); |
| 368 | auto PaddingSize = getNumPaddingBytes(NamesSize); |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 369 | |
Xinliang David Li | 09717fa | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 370 | ptrdiff_t DataOffset = sizeof(RawInstrProf::Header); |
Betul Buyukkurt | a5be9e3 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 371 | ptrdiff_t CountersOffset = DataOffset + DataSizeInBytes; |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 372 | ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize; |
Betul Buyukkurt | a5be9e3 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 373 | ptrdiff_t ValueDataOffset = NamesOffset + NamesSize + PaddingSize; |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 374 | |
Justin Bogner | 6d81fc7 | 2014-05-16 00:38:00 +0000 | [diff] [blame] | 375 | auto *Start = reinterpret_cast<const char *>(&Header); |
Xinliang David Li | 8fa402e | 2016-05-05 19:41:18 +0000 | [diff] [blame] | 376 | if (Start + ValueDataOffset > DataBuffer->getBufferEnd()) |
Duncan P. N. Exon Smith | ebaeaa9 | 2014-03-21 20:42:28 +0000 | [diff] [blame] | 377 | return error(instrprof_error::bad_header); |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 378 | |
Xinliang David Li | 09717fa | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 379 | Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>( |
| 380 | Start + DataOffset); |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 381 | DataEnd = Data + DataSize; |
Duncan P. N. Exon Smith | b83e788 | 2014-03-24 00:47:18 +0000 | [diff] [blame] | 382 | CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset); |
| 383 | NamesStart = Start + NamesOffset; |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 384 | ValueDataStart = reinterpret_cast<const uint8_t *>(Start + ValueDataOffset); |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 385 | |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 386 | std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>(); |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 387 | if (Error E = createSymtab(*NewSymtab.get())) |
| 388 | return E; |
Vedant Kumar | 089f755 | 2016-04-21 21:07:25 +0000 | [diff] [blame] | 389 | |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 390 | Symtab = std::move(NewSymtab); |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 391 | return success(); |
| 392 | } |
| 393 | |
Duncan P. N. Exon Smith | 0695b20 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 394 | template <class IntPtrT> |
David Blaikie | 450ef2a | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 395 | Error RawInstrProfReader<IntPtrT>::readName(NamedInstrProfRecord &Record) { |
Xinliang David Li | 04638a6 | 2016-02-08 18:13:49 +0000 | [diff] [blame] | 396 | Record.Name = getName(Data->NameRef); |
Xinliang David Li | 1e66849 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 397 | return success(); |
| 398 | } |
| 399 | |
| 400 | template <class IntPtrT> |
David Blaikie | 450ef2a | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 401 | Error RawInstrProfReader<IntPtrT>::readFuncHash(NamedInstrProfRecord &Record) { |
Xinliang David Li | 1e66849 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 402 | Record.Hash = swap(Data->FuncHash); |
| 403 | return success(); |
| 404 | } |
| 405 | |
| 406 | template <class IntPtrT> |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 407 | Error RawInstrProfReader<IntPtrT>::readRawCounts( |
Xinliang David Li | 1e66849 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 408 | InstrProfRecord &Record) { |
Justin Bogner | 38ac7e9 | 2014-04-25 02:45:33 +0000 | [diff] [blame] | 409 | uint32_t NumCounters = swap(Data->NumCounters); |
Xinliang David Li | 1e66849 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 410 | IntPtrT CounterPtr = Data->CounterPtr; |
Justin Bogner | 38ac7e9 | 2014-04-25 02:45:33 +0000 | [diff] [blame] | 411 | if (NumCounters == 0) |
| 412 | return error(instrprof_error::malformed); |
Xinliang David Li | 1e66849 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 413 | |
| 414 | auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters); |
| 415 | auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart); |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 416 | |
| 417 | // Check bounds. |
Xinliang David Li | 1e66849 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 418 | if (RawCounts.data() < CountersStart || |
Duncan P. N. Exon Smith | b83e788 | 2014-03-24 00:47:18 +0000 | [diff] [blame] | 419 | RawCounts.data() + RawCounts.size() > NamesStartAsCounter) |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 420 | return error(instrprof_error::malformed); |
| 421 | |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 422 | if (ShouldSwapBytes) { |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 423 | Record.Counts.clear(); |
| 424 | Record.Counts.reserve(RawCounts.size()); |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 425 | for (uint64_t Count : RawCounts) |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 426 | Record.Counts.push_back(swap(Count)); |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 427 | } else |
| 428 | Record.Counts = RawCounts; |
| 429 | |
Xinliang David Li | 1e66849 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 430 | return success(); |
| 431 | } |
| 432 | |
| 433 | template <class IntPtrT> |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 434 | Error RawInstrProfReader<IntPtrT>::readValueProfilingData( |
| 435 | InstrProfRecord &Record) { |
Betul Buyukkurt | a5be9e3 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 436 | Record.clearValueData(); |
Xinliang David Li | 6f139ce | 2015-12-11 06:53:53 +0000 | [diff] [blame] | 437 | CurValueDataSize = 0; |
| 438 | // Need to match the logic in value profile dumper code in compiler-rt: |
| 439 | uint32_t NumValueKinds = 0; |
| 440 | for (uint32_t I = 0; I < IPVK_Last + 1; I++) |
| 441 | NumValueKinds += (Data->NumValueSites[I] != 0); |
| 442 | |
| 443 | if (!NumValueKinds) |
Betul Buyukkurt | a5be9e3 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 444 | return success(); |
| 445 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 446 | Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr = |
Xinliang David Li | 8fa402e | 2016-05-05 19:41:18 +0000 | [diff] [blame] | 447 | ValueProfData::getValueProfData( |
| 448 | ValueDataStart, (const unsigned char *)DataBuffer->getBufferEnd(), |
| 449 | getDataEndianness()); |
Betul Buyukkurt | a5be9e3 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 450 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 451 | if (Error E = VDataPtrOrErr.takeError()) |
| 452 | return E; |
Betul Buyukkurt | a5be9e3 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 453 | |
Adam Nemet | 4ba91c0 | 2016-03-28 18:27:44 +0000 | [diff] [blame] | 454 | // Note that besides deserialization, this also performs the conversion for |
| 455 | // indirect call targets. The function pointers from the raw profile are |
| 456 | // remapped into function name hashes. |
Mircea Trofin | 06b0783 | 2018-03-21 22:27:31 +0000 | [diff] [blame] | 457 | VDataPtrOrErr.get()->deserializeTo(Record, Symtab.get()); |
Xinliang David Li | 6f139ce | 2015-12-11 06:53:53 +0000 | [diff] [blame] | 458 | CurValueDataSize = VDataPtrOrErr.get()->getSize(); |
Betul Buyukkurt | a5be9e3 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 459 | return success(); |
| 460 | } |
| 461 | |
| 462 | template <class IntPtrT> |
David Blaikie | 450ef2a | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 463 | Error RawInstrProfReader<IntPtrT>::readNextRecord(NamedInstrProfRecord &Record) { |
Xinliang David Li | 1e66849 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 464 | if (atEnd()) |
Xinliang David Li | 8fa402e | 2016-05-05 19:41:18 +0000 | [diff] [blame] | 465 | // At this point, ValueDataStart field points to the next header. |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 466 | if (Error E = readNextHeader(getNextHeaderPos())) |
Mircea Trofin | 6852646 | 2018-04-13 15:04:36 +0000 | [diff] [blame] | 467 | return error(std::move(E)); |
Xinliang David Li | 1e66849 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 468 | |
| 469 | // Read name ad set it in Record. |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 470 | if (Error E = readName(Record)) |
Mircea Trofin | 6852646 | 2018-04-13 15:04:36 +0000 | [diff] [blame] | 471 | return error(std::move(E)); |
Xinliang David Li | 1e66849 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 472 | |
| 473 | // Read FuncHash and set it in Record. |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 474 | if (Error E = readFuncHash(Record)) |
Mircea Trofin | 6852646 | 2018-04-13 15:04:36 +0000 | [diff] [blame] | 475 | return error(std::move(E)); |
Xinliang David Li | 1e66849 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 476 | |
| 477 | // Read raw counts and set Record. |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 478 | if (Error E = readRawCounts(Record)) |
Mircea Trofin | 6852646 | 2018-04-13 15:04:36 +0000 | [diff] [blame] | 479 | return error(std::move(E)); |
Xinliang David Li | 1e66849 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 480 | |
Betul Buyukkurt | a5be9e3 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 481 | // Read value data and set Record. |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 482 | if (Error E = readValueProfilingData(Record)) |
Mircea Trofin | 6852646 | 2018-04-13 15:04:36 +0000 | [diff] [blame] | 483 | return error(std::move(E)); |
Betul Buyukkurt | a5be9e3 | 2015-11-18 18:14:55 +0000 | [diff] [blame] | 484 | |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 485 | // Iterate. |
Xinliang David Li | 1e66849 | 2015-10-28 19:34:04 +0000 | [diff] [blame] | 486 | advanceData(); |
Duncan P. N. Exon Smith | ddfcb21 | 2014-03-21 18:26:05 +0000 | [diff] [blame] | 487 | return success(); |
| 488 | } |
Duncan P. N. Exon Smith | 0695b20 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 489 | |
| 490 | namespace llvm { |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 491 | |
Duncan P. N. Exon Smith | 0695b20 | 2014-03-23 03:38:12 +0000 | [diff] [blame] | 492 | template class RawInstrProfReader<uint32_t>; |
| 493 | template class RawInstrProfReader<uint64_t>; |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 494 | |
| 495 | } // end namespace llvm |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 496 | |
Justin Bogner | 1c60993 | 2014-04-18 22:00:22 +0000 | [diff] [blame] | 497 | InstrProfLookupTrait::hash_value_type |
| 498 | InstrProfLookupTrait::ComputeHash(StringRef K) { |
| 499 | return IndexedInstrProf::ComputeHash(HashType, K); |
| 500 | } |
| 501 | |
Eugene Zelenko | dfaebc4 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 502 | using data_type = InstrProfLookupTrait::data_type; |
| 503 | using offset_type = InstrProfLookupTrait::offset_type; |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 504 | |
Xinliang David Li | 52a156f | 2015-11-28 05:06:00 +0000 | [diff] [blame] | 505 | bool InstrProfLookupTrait::readValueProfilingData( |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 506 | const unsigned char *&D, const unsigned char *const End) { |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 507 | Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr = |
Xinliang David Li | e34401d | 2015-11-17 23:00:40 +0000 | [diff] [blame] | 508 | ValueProfData::getValueProfData(D, End, ValueProfDataEndianness); |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 509 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 510 | if (VDataPtrOrErr.takeError()) |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 511 | return false; |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 512 | |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 513 | VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr); |
Xinliang David Li | bcd8e0a | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 514 | D += VDataPtrOrErr.get()->TotalSize; |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 515 | |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 516 | return true; |
| 517 | } |
| 518 | |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 519 | data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D, |
| 520 | offset_type N) { |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 521 | using namespace support; |
| 522 | |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 523 | // Check if the data is corrupt. If so, don't try to read it. |
| 524 | if (N % sizeof(uint64_t)) |
| 525 | return data_type(); |
| 526 | |
| 527 | DataBuffer.clear(); |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 528 | std::vector<uint64_t> CounterBuffer; |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 529 | |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 530 | const unsigned char *End = D + N; |
| 531 | while (D < End) { |
Xinliang David Li | f747d12 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 532 | // Read hash. |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 533 | if (D + sizeof(uint64_t) >= End) |
| 534 | return data_type(); |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 535 | uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D); |
| 536 | |
Rong Xu | c7d7fb0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 537 | // Initialize number of counters for GET_VERSION(FormatVersion) == 1. |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 538 | uint64_t CountsSize = N / sizeof(uint64_t) - 1; |
Xinliang David Li | f747d12 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 539 | // If format version is different then read the number of counters. |
Rong Xu | c7d7fb0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 540 | if (GET_VERSION(FormatVersion) != IndexedInstrProf::ProfVersion::Version1) { |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 541 | if (D + sizeof(uint64_t) > End) |
| 542 | return data_type(); |
| 543 | CountsSize = endian::readNext<uint64_t, little, unaligned>(D); |
| 544 | } |
Xinliang David Li | f747d12 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 545 | // Read counter values. |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 546 | if (D + CountsSize * sizeof(uint64_t) > End) |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 547 | return data_type(); |
| 548 | |
| 549 | CounterBuffer.clear(); |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 550 | CounterBuffer.reserve(CountsSize); |
| 551 | for (uint64_t J = 0; J < CountsSize; ++J) |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 552 | CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D)); |
| 553 | |
Xinliang David Li | 61c7e68 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 554 | DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer)); |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 555 | |
Xinliang David Li | f747d12 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 556 | // Read value profiling data. |
Rong Xu | c7d7fb0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 557 | if (GET_VERSION(FormatVersion) > IndexedInstrProf::ProfVersion::Version2 && |
Xinliang David Li | 5fd06e2 | 2016-01-14 02:47:01 +0000 | [diff] [blame] | 558 | !readValueProfilingData(D, End)) { |
Justin Bogner | c96f87a | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 559 | DataBuffer.clear(); |
| 560 | return data_type(); |
| 561 | } |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 562 | } |
| 563 | return DataBuffer; |
| 564 | } |
| 565 | |
Xinliang David Li | d89494b | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 566 | template <typename HashTableImpl> |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 567 | Error InstrProfReaderIndex<HashTableImpl>::getRecords( |
David Blaikie | 450ef2a | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 568 | StringRef FuncName, ArrayRef<NamedInstrProfRecord> &Data) { |
Xinliang David Li | d89494b | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 569 | auto Iter = HashTable->find(FuncName); |
| 570 | if (Iter == HashTable->end()) |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 571 | return make_error<InstrProfError>(instrprof_error::unknown_function); |
Xinliang David Li | 50b691d | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 572 | |
| 573 | Data = (*Iter); |
Xinliang David Li | a8ea67e | 2015-11-12 00:32:17 +0000 | [diff] [blame] | 574 | if (Data.empty()) |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 575 | return make_error<InstrProfError>(instrprof_error::malformed); |
Xinliang David Li | 50b691d | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 576 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 577 | return Error::success(); |
Xinliang David Li | 50b691d | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Xinliang David Li | d89494b | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 580 | template <typename HashTableImpl> |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 581 | Error InstrProfReaderIndex<HashTableImpl>::getRecords( |
David Blaikie | 450ef2a | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 582 | ArrayRef<NamedInstrProfRecord> &Data) { |
Xinliang David Li | d89494b | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 583 | if (atEnd()) |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 584 | return make_error<InstrProfError>(instrprof_error::eof); |
Xinliang David Li | 50b691d | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 585 | |
| 586 | Data = *RecordIterator; |
| 587 | |
Xinliang David Li | 849df79 | 2015-12-10 23:48:05 +0000 | [diff] [blame] | 588 | if (Data.empty()) |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 589 | return make_error<InstrProfError>(instrprof_error::malformed); |
Xinliang David Li | 50b691d | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 590 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 591 | return Error::success(); |
Xinliang David Li | 50b691d | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Xinliang David Li | d89494b | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 594 | template <typename HashTableImpl> |
| 595 | InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex( |
| 596 | const unsigned char *Buckets, const unsigned char *const Payload, |
| 597 | const unsigned char *const Base, IndexedInstrProf::HashT HashType, |
| 598 | uint64_t Version) { |
Xinliang David Li | 50b691d | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 599 | FormatVersion = Version; |
Xinliang David Li | d89494b | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 600 | HashTable.reset(HashTableImpl::Create( |
| 601 | Buckets, Payload, Base, |
| 602 | typename HashTableImpl::InfoType(HashType, Version))); |
Xinliang David Li | d89494b | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 603 | RecordIterator = HashTable->data_begin(); |
Xinliang David Li | 50b691d | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Richard Smith | 3b1e430 | 2018-10-10 21:09:37 +0000 | [diff] [blame] | 606 | namespace { |
| 607 | /// A remapper that does not apply any remappings. |
| 608 | class InstrProfReaderNullRemapper : public InstrProfReaderRemapper { |
| 609 | InstrProfReaderIndexBase &Underlying; |
| 610 | |
| 611 | public: |
| 612 | InstrProfReaderNullRemapper(InstrProfReaderIndexBase &Underlying) |
| 613 | : Underlying(Underlying) {} |
| 614 | |
| 615 | Error getRecords(StringRef FuncName, |
| 616 | ArrayRef<NamedInstrProfRecord> &Data) override { |
| 617 | return Underlying.getRecords(FuncName, Data); |
| 618 | } |
| 619 | }; |
| 620 | } |
| 621 | |
| 622 | /// A remapper that applies remappings based on a symbol remapping file. |
| 623 | template <typename HashTableImpl> |
| 624 | class llvm::InstrProfReaderItaniumRemapper |
| 625 | : public InstrProfReaderRemapper { |
| 626 | public: |
| 627 | InstrProfReaderItaniumRemapper( |
| 628 | std::unique_ptr<MemoryBuffer> RemapBuffer, |
| 629 | InstrProfReaderIndex<HashTableImpl> &Underlying) |
| 630 | : RemapBuffer(std::move(RemapBuffer)), Underlying(Underlying) { |
| 631 | } |
| 632 | |
| 633 | /// Extract the original function name from a PGO function name. |
| 634 | static StringRef extractName(StringRef Name) { |
| 635 | // We can have multiple :-separated pieces; there can be pieces both |
| 636 | // before and after the mangled name. Find the first part that starts |
| 637 | // with '_Z'; we'll assume that's the mangled name we want. |
| 638 | std::pair<StringRef, StringRef> Parts = {StringRef(), Name}; |
| 639 | while (true) { |
| 640 | Parts = Parts.second.split(':'); |
| 641 | if (Parts.first.startswith("_Z")) |
| 642 | return Parts.first; |
| 643 | if (Parts.second.empty()) |
| 644 | return Name; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | /// Given a mangled name extracted from a PGO function name, and a new |
| 649 | /// form for that mangled name, reconstitute the name. |
| 650 | static void reconstituteName(StringRef OrigName, StringRef ExtractedName, |
| 651 | StringRef Replacement, |
| 652 | SmallVectorImpl<char> &Out) { |
| 653 | Out.reserve(OrigName.size() + Replacement.size() - ExtractedName.size()); |
| 654 | Out.insert(Out.end(), OrigName.begin(), ExtractedName.begin()); |
| 655 | Out.insert(Out.end(), Replacement.begin(), Replacement.end()); |
| 656 | Out.insert(Out.end(), ExtractedName.end(), OrigName.end()); |
| 657 | } |
| 658 | |
| 659 | Error populateRemappings() override { |
| 660 | if (Error E = Remappings.read(*RemapBuffer)) |
| 661 | return E; |
| 662 | for (StringRef Name : Underlying.HashTable->keys()) { |
| 663 | StringRef RealName = extractName(Name); |
| 664 | if (auto Key = Remappings.insert(RealName)) { |
| 665 | // FIXME: We could theoretically map the same equivalence class to |
| 666 | // multiple names in the profile data. If that happens, we should |
| 667 | // return NamedInstrProfRecords from all of them. |
| 668 | MappedNames.insert({Key, RealName}); |
| 669 | } |
| 670 | } |
| 671 | return Error::success(); |
| 672 | } |
| 673 | |
| 674 | Error getRecords(StringRef FuncName, |
| 675 | ArrayRef<NamedInstrProfRecord> &Data) override { |
| 676 | StringRef RealName = extractName(FuncName); |
| 677 | if (auto Key = Remappings.lookup(RealName)) { |
| 678 | StringRef Remapped = MappedNames.lookup(Key); |
| 679 | if (!Remapped.empty()) { |
| 680 | if (RealName.begin() == FuncName.begin() && |
| 681 | RealName.end() == FuncName.end()) |
| 682 | FuncName = Remapped; |
| 683 | else { |
| 684 | // Try rebuilding the name from the given remapping. |
| 685 | SmallString<256> Reconstituted; |
| 686 | reconstituteName(FuncName, RealName, Remapped, Reconstituted); |
| 687 | Error E = Underlying.getRecords(Reconstituted, Data); |
| 688 | if (!E) |
| 689 | return E; |
| 690 | |
| 691 | // If we failed because the name doesn't exist, fall back to asking |
| 692 | // about the original name. |
| 693 | if (Error Unhandled = handleErrors( |
| 694 | std::move(E), [](std::unique_ptr<InstrProfError> Err) { |
| 695 | return Err->get() == instrprof_error::unknown_function |
| 696 | ? Error::success() |
| 697 | : Error(std::move(Err)); |
| 698 | })) |
| 699 | return Unhandled; |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | return Underlying.getRecords(FuncName, Data); |
| 704 | } |
| 705 | |
| 706 | private: |
| 707 | /// The memory buffer containing the remapping configuration. Remappings |
| 708 | /// holds pointers into this buffer. |
| 709 | std::unique_ptr<MemoryBuffer> RemapBuffer; |
| 710 | |
| 711 | /// The mangling remapper. |
| 712 | SymbolRemappingReader Remappings; |
| 713 | |
| 714 | /// Mapping from mangled name keys to the name used for the key in the |
| 715 | /// profile data. |
| 716 | /// FIXME: Can we store a location within the on-disk hash table instead of |
| 717 | /// redoing lookup? |
| 718 | DenseMap<SymbolRemappingReader::Key, StringRef> MappedNames; |
| 719 | |
| 720 | /// The real profile data reader. |
| 721 | InstrProfReaderIndex<HashTableImpl> &Underlying; |
| 722 | }; |
| 723 | |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 724 | bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) { |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 725 | using namespace support; |
| 726 | |
Xinliang David Li | a8ea67e | 2015-11-12 00:32:17 +0000 | [diff] [blame] | 727 | if (DataBuffer.getBufferSize() < 8) |
| 728 | return false; |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 729 | uint64_t Magic = |
| 730 | endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart()); |
Xinliang David Li | f747d12 | 2015-10-13 16:35:59 +0000 | [diff] [blame] | 731 | // Verify that it's magical. |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 732 | return Magic == IndexedInstrProf::Magic; |
| 733 | } |
| 734 | |
Xinliang David Li | 8fd9cfb | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 735 | const unsigned char * |
| 736 | IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version, |
| 737 | const unsigned char *Cur) { |
Easwaran Raman | 17e7f119 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 738 | using namespace IndexedInstrProf; |
Xinliang David Li | 8fd9cfb | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 739 | using namespace support; |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 740 | |
Xinliang David Li | 8fd9cfb | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 741 | if (Version >= IndexedInstrProf::Version4) { |
| 742 | const IndexedInstrProf::Summary *SummaryInLE = |
| 743 | reinterpret_cast<const IndexedInstrProf::Summary *>(Cur); |
| 744 | uint64_t NFields = |
| 745 | endian::byte_swap<uint64_t, little>(SummaryInLE->NumSummaryFields); |
| 746 | uint64_t NEntries = |
| 747 | endian::byte_swap<uint64_t, little>(SummaryInLE->NumCutoffEntries); |
| 748 | uint32_t SummarySize = |
| 749 | IndexedInstrProf::Summary::getSize(NFields, NEntries); |
| 750 | std::unique_ptr<IndexedInstrProf::Summary> SummaryData = |
| 751 | IndexedInstrProf::allocSummary(SummarySize); |
| 752 | |
| 753 | const uint64_t *Src = reinterpret_cast<const uint64_t *>(SummaryInLE); |
| 754 | uint64_t *Dst = reinterpret_cast<uint64_t *>(SummaryData.get()); |
| 755 | for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++) |
| 756 | Dst[I] = endian::byte_swap<uint64_t, little>(Src[I]); |
| 757 | |
Eugene Zelenko | dfaebc4 | 2017-06-21 23:19:47 +0000 | [diff] [blame] | 758 | SummaryEntryVector DetailedSummary; |
Easwaran Raman | 17e7f119 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 759 | for (unsigned I = 0; I < SummaryData->NumCutoffEntries; I++) { |
| 760 | const IndexedInstrProf::Summary::Entry &Ent = SummaryData->getEntry(I); |
| 761 | DetailedSummary.emplace_back((uint32_t)Ent.Cutoff, Ent.MinBlockCount, |
| 762 | Ent.NumBlocks); |
| 763 | } |
Easwaran Raman | 233e7d3 | 2016-02-17 18:18:47 +0000 | [diff] [blame] | 764 | // initialize InstrProfSummary using the SummaryData from disk. |
Easwaran Raman | 30c760d | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 765 | this->Summary = llvm::make_unique<ProfileSummary>( |
| 766 | ProfileSummary::PSK_Instr, DetailedSummary, |
Easwaran Raman | 17e7f119 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 767 | SummaryData->get(Summary::TotalBlockCount), |
| 768 | SummaryData->get(Summary::MaxBlockCount), |
| 769 | SummaryData->get(Summary::MaxInternalBlockCount), |
| 770 | SummaryData->get(Summary::MaxFunctionCount), |
| 771 | SummaryData->get(Summary::TotalNumBlocks), |
Easwaran Raman | 30c760d | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 772 | SummaryData->get(Summary::TotalNumFunctions)); |
Xinliang David Li | 8fd9cfb | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 773 | return Cur + SummarySize; |
| 774 | } else { |
| 775 | // For older version of profile data, we need to compute on the fly: |
| 776 | using namespace IndexedInstrProf; |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 777 | |
Easwaran Raman | 17e7f119 | 2016-05-19 21:07:12 +0000 | [diff] [blame] | 778 | InstrProfSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs); |
| 779 | // FIXME: This only computes an empty summary. Need to call addRecord for |
David Blaikie | 450ef2a | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 780 | // all NamedInstrProfRecords to get the correct summary. |
Benjamin Kramer | e3bf664 | 2016-05-20 09:18:37 +0000 | [diff] [blame] | 781 | this->Summary = Builder.getSummary(); |
Xinliang David Li | 8fd9cfb | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 782 | return Cur; |
| 783 | } |
| 784 | } |
| 785 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 786 | Error IndexedInstrProfReader::readHeader() { |
Eugene Zelenko | f4f67a0 | 2017-03-03 01:07:34 +0000 | [diff] [blame] | 787 | using namespace support; |
| 788 | |
Aaron Ballman | 32207fa | 2014-05-01 17:16:24 +0000 | [diff] [blame] | 789 | const unsigned char *Start = |
| 790 | (const unsigned char *)DataBuffer->getBufferStart(); |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 791 | const unsigned char *Cur = Start; |
Aaron Ballman | 32207fa | 2014-05-01 17:16:24 +0000 | [diff] [blame] | 792 | if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24) |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 793 | return error(instrprof_error::truncated); |
| 794 | |
Xinliang David Li | 09717fa | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 795 | auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur); |
| 796 | Cur += sizeof(IndexedInstrProf::Header); |
| 797 | |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 798 | // Check the magic number. |
Xinliang David Li | 09717fa | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 799 | uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic); |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 800 | if (Magic != IndexedInstrProf::Magic) |
| 801 | return error(instrprof_error::bad_magic); |
| 802 | |
| 803 | // Read the version. |
Xinliang David Li | 50b691d | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 804 | uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version); |
Rong Xu | c7d7fb0 | 2016-02-10 17:18:30 +0000 | [diff] [blame] | 805 | if (GET_VERSION(FormatVersion) > |
| 806 | IndexedInstrProf::ProfVersion::CurrentVersion) |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 807 | return error(instrprof_error::unsupported_version); |
| 808 | |
Xinliang David Li | 8fd9cfb | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 809 | Cur = readSummary((IndexedInstrProf::ProfVersion)FormatVersion, Cur); |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 810 | |
| 811 | // Read the hash type and start offset. |
| 812 | IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>( |
Xinliang David Li | 09717fa | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 813 | endian::byte_swap<uint64_t, little>(Header->HashType)); |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 814 | if (HashType > IndexedInstrProf::HashT::Last) |
| 815 | return error(instrprof_error::unsupported_hash_type); |
Xinliang David Li | 09717fa | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 816 | |
| 817 | uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset); |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 818 | |
| 819 | // The rest of the file is an on disk hash table. |
Richard Smith | 3b1e430 | 2018-10-10 21:09:37 +0000 | [diff] [blame] | 820 | auto IndexPtr = |
| 821 | llvm::make_unique<InstrProfReaderIndex<OnDiskHashTableImplV3>>( |
| 822 | Start + HashOffset, Cur, Start, HashType, FormatVersion); |
| 823 | |
| 824 | // Load the remapping table now if requested. |
| 825 | if (RemappingBuffer) { |
| 826 | Remapper = llvm::make_unique< |
| 827 | InstrProfReaderItaniumRemapper<OnDiskHashTableImplV3>>( |
| 828 | std::move(RemappingBuffer), *IndexPtr); |
| 829 | if (Error E = Remapper->populateRemappings()) |
| 830 | return E; |
| 831 | } else { |
| 832 | Remapper = llvm::make_unique<InstrProfReaderNullRemapper>(*IndexPtr); |
| 833 | } |
| 834 | Index = std::move(IndexPtr); |
| 835 | |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 836 | return success(); |
| 837 | } |
| 838 | |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 839 | InstrProfSymtab &IndexedInstrProfReader::getSymtab() { |
| 840 | if (Symtab.get()) |
| 841 | return *Symtab.get(); |
| 842 | |
| 843 | std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>(); |
Vedant Kumar | b945463 | 2017-06-20 01:38:56 +0000 | [diff] [blame] | 844 | if (Error E = Index->populateSymtab(*NewSymtab.get())) { |
| 845 | consumeError(error(InstrProfError::take(std::move(E)))); |
| 846 | } |
Xinliang David Li | cfbaf11 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 847 | |
| 848 | Symtab = std::move(NewSymtab); |
| 849 | return *Symtab.get(); |
| 850 | } |
| 851 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 852 | Expected<InstrProfRecord> |
Xinliang David Li | 4d774a3 | 2015-11-06 07:54:21 +0000 | [diff] [blame] | 853 | IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName, |
| 854 | uint64_t FuncHash) { |
David Blaikie | 450ef2a | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 855 | ArrayRef<NamedInstrProfRecord> Data; |
Richard Smith | 3b1e430 | 2018-10-10 21:09:37 +0000 | [diff] [blame] | 856 | Error Err = Remapper->getRecords(FuncName, Data); |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 857 | if (Err) |
| 858 | return std::move(Err); |
Justin Bogner | 4a6fa13 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 859 | // Found it. Look for counters with the right hash. |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 860 | for (unsigned I = 0, E = Data.size(); I < E; ++I) { |
Justin Bogner | 4a6fa13 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 861 | // Check for a match and fill the vector if there is one. |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 862 | if (Data[I].Hash == FuncHash) { |
Xinliang David Li | 61c7e68 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 863 | return std::move(Data[I]); |
Justin Bogner | 4a6fa13 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | return error(instrprof_error::hash_mismatch); |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 869 | Error IndexedInstrProfReader::getFunctionCounts(StringRef FuncName, |
| 870 | uint64_t FuncHash, |
| 871 | std::vector<uint64_t> &Counts) { |
| 872 | Expected<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash); |
| 873 | if (Error E = Record.takeError()) |
| 874 | return error(std::move(E)); |
Xinliang David Li | 61c7e68 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 875 | |
| 876 | Counts = Record.get().Counts; |
| 877 | return success(); |
| 878 | } |
| 879 | |
David Blaikie | 450ef2a | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 880 | Error IndexedInstrProfReader::readNextRecord(NamedInstrProfRecord &Record) { |
David Blaikie | 450ef2a | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 881 | ArrayRef<NamedInstrProfRecord> Data; |
Xinliang David Li | 50b691d | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 882 | |
Vedant Kumar | c77570e | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 883 | Error E = Index->getRecords(Data); |
| 884 | if (E) |
| 885 | return error(std::move(E)); |
Xinliang David Li | 50b691d | 2015-10-28 04:20:31 +0000 | [diff] [blame] | 886 | |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 887 | Record = Data[RecordIndex++]; |
| 888 | if (RecordIndex >= Data.size()) { |
Xinliang David Li | d89494b | 2015-12-01 20:26:26 +0000 | [diff] [blame] | 889 | Index->advanceToNextKey(); |
Justin Bogner | b06f3b4 | 2015-06-22 23:58:05 +0000 | [diff] [blame] | 890 | RecordIndex = 0; |
Justin Bogner | 4a6fa13 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 891 | } |
Justin Bogner | e153fb3 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 892 | return success(); |
| 893 | } |