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