2017-03-03 09:07:34 +08:00
|
|
|
//===- InstrProfReader.cpp - Instrumented profiling reader ----------------===//
|
2014-03-22 01:24:48 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains support for reading profiling data for clang's
|
|
|
|
// instrumentation based PGO and coverage.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/ProfileData/InstrProfReader.h"
|
2017-03-03 09:07:34 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2018-10-11 05:09:37 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2015-03-02 05:28:53 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2017-03-03 09:07:34 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/IR/ProfileSummary.h"
|
|
|
|
#include "llvm/ProfileData/InstrProf.h"
|
|
|
|
#include "llvm/ProfileData/ProfileCommon.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2018-10-11 05:09:37 +08:00
|
|
|
#include "llvm/Support/SymbolRemappingReader.h"
|
2017-03-03 09:07:34 +08:00
|
|
|
#include "llvm/Support/SwapByteOrder.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <limits>
|
|
|
|
#include <memory>
|
|
|
|
#include <system_error>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2014-03-22 01:24:48 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
static Expected<std::unique_ptr<MemoryBuffer>>
|
2016-05-29 18:31:00 +08:00
|
|
|
setupMemoryBuffer(const Twine &Path) {
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Path);
|
|
|
|
if (std::error_code EC = BufferOrErr.getError())
|
2016-05-19 11:54:45 +08:00
|
|
|
return errorCodeToError(EC);
|
2015-02-18 09:58:17 +08:00
|
|
|
return std::move(BufferOrErr.get());
|
2014-04-19 05:48:40 +08:00
|
|
|
}
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
static Error initializeReader(InstrProfReader &Reader) {
|
2014-04-19 05:48:40 +08:00
|
|
|
return Reader.readHeader();
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<std::unique_ptr<InstrProfReader>>
|
2016-05-29 18:31:00 +08:00
|
|
|
InstrProfReader::create(const Twine &Path) {
|
2014-04-19 05:48:40 +08:00
|
|
|
// Set up the buffer to read.
|
2014-11-03 08:51:45 +08:00
|
|
|
auto BufferOrError = setupMemoryBuffer(Path);
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = BufferOrError.takeError())
|
|
|
|
return std::move(E);
|
2015-02-18 09:58:17 +08:00
|
|
|
return InstrProfReader::create(std::move(BufferOrError.get()));
|
|
|
|
}
|
2015-02-17 07:31:07 +08:00
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<std::unique_ptr<InstrProfReader>>
|
2015-02-18 09:58:17 +08:00
|
|
|
InstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer) {
|
|
|
|
// Sanity check the buffer.
|
2017-12-15 06:07:03 +08:00
|
|
|
if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<unsigned>::max())
|
2016-05-19 11:54:45 +08:00
|
|
|
return make_error<InstrProfError>(instrprof_error::too_large);
|
2015-02-17 17:21:43 +08:00
|
|
|
|
2016-10-20 06:51:17 +08:00
|
|
|
if (Buffer->getBufferSize() == 0)
|
|
|
|
return make_error<InstrProfError>(instrprof_error::empty_raw_profile);
|
|
|
|
|
2015-02-18 09:58:17 +08:00
|
|
|
std::unique_ptr<InstrProfReader> Result;
|
2014-03-22 04:42:31 +08:00
|
|
|
// Create the reader.
|
2014-04-19 05:48:40 +08:00
|
|
|
if (IndexedInstrProfReader::hasFormat(*Buffer))
|
|
|
|
Result.reset(new IndexedInstrProfReader(std::move(Buffer)));
|
|
|
|
else if (RawInstrProfReader64::hasFormat(*Buffer))
|
2014-03-23 11:38:12 +08:00
|
|
|
Result.reset(new RawInstrProfReader64(std::move(Buffer)));
|
|
|
|
else if (RawInstrProfReader32::hasFormat(*Buffer))
|
|
|
|
Result.reset(new RawInstrProfReader32(std::move(Buffer)));
|
2015-11-13 11:47:58 +08:00
|
|
|
else if (TextInstrProfReader::hasFormat(*Buffer))
|
2015-11-13 02:39:26 +08:00
|
|
|
Result.reset(new TextInstrProfReader(std::move(Buffer)));
|
2015-11-13 11:47:58 +08:00
|
|
|
else
|
2016-05-19 11:54:45 +08:00
|
|
|
return make_error<InstrProfError>(instrprof_error::unrecognized_format);
|
2014-03-22 04:42:31 +08:00
|
|
|
|
2014-04-19 05:48:40 +08:00
|
|
|
// Initialize the reader and return the result.
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = initializeReader(*Result))
|
|
|
|
return std::move(E);
|
2014-11-03 08:51:45 +08:00
|
|
|
|
|
|
|
return std::move(Result);
|
2014-04-19 05:48:40 +08:00
|
|
|
}
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<std::unique_ptr<IndexedInstrProfReader>>
|
2018-10-11 05:09:37 +08:00
|
|
|
IndexedInstrProfReader::create(const Twine &Path, const Twine &RemappingPath) {
|
2014-04-19 05:48:40 +08:00
|
|
|
// Set up the buffer to read.
|
2014-11-03 08:51:45 +08:00
|
|
|
auto BufferOrError = setupMemoryBuffer(Path);
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = BufferOrError.takeError())
|
|
|
|
return std::move(E);
|
2018-10-11 05:09:37 +08:00
|
|
|
|
|
|
|
// Set up the remapping buffer if requested.
|
|
|
|
std::unique_ptr<MemoryBuffer> RemappingBuffer;
|
|
|
|
std::string RemappingPathStr = RemappingPath.str();
|
|
|
|
if (!RemappingPathStr.empty()) {
|
|
|
|
auto RemappingBufferOrError = setupMemoryBuffer(RemappingPathStr);
|
|
|
|
if (Error E = RemappingBufferOrError.takeError())
|
|
|
|
return std::move(E);
|
|
|
|
RemappingBuffer = std::move(RemappingBufferOrError.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return IndexedInstrProfReader::create(std::move(BufferOrError.get()),
|
|
|
|
std::move(RemappingBuffer));
|
2015-02-18 09:58:17 +08:00
|
|
|
}
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<std::unique_ptr<IndexedInstrProfReader>>
|
2018-10-11 05:09:37 +08:00
|
|
|
IndexedInstrProfReader::create(std::unique_ptr<MemoryBuffer> Buffer,
|
|
|
|
std::unique_ptr<MemoryBuffer> RemappingBuffer) {
|
2015-02-18 09:58:17 +08:00
|
|
|
// Sanity check the buffer.
|
2017-12-15 06:07:03 +08:00
|
|
|
if (uint64_t(Buffer->getBufferSize()) > std::numeric_limits<unsigned>::max())
|
2016-05-19 11:54:45 +08:00
|
|
|
return make_error<InstrProfError>(instrprof_error::too_large);
|
2015-02-17 05:28:58 +08:00
|
|
|
|
2014-04-19 05:48:40 +08:00
|
|
|
// Create the reader.
|
|
|
|
if (!IndexedInstrProfReader::hasFormat(*Buffer))
|
2016-05-19 11:54:45 +08:00
|
|
|
return make_error<InstrProfError>(instrprof_error::bad_magic);
|
2018-10-11 05:09:37 +08:00
|
|
|
auto Result = llvm::make_unique<IndexedInstrProfReader>(
|
|
|
|
std::move(Buffer), std::move(RemappingBuffer));
|
2014-04-19 05:48:40 +08:00
|
|
|
|
|
|
|
// Initialize the reader and return the result.
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = initializeReader(*Result))
|
|
|
|
return std::move(E);
|
2015-02-17 05:28:58 +08:00
|
|
|
|
|
|
|
return std::move(Result);
|
2014-03-22 01:24:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void InstrProfIterator::Increment() {
|
2016-05-19 11:54:45 +08:00
|
|
|
if (auto E = Reader->readNextRecord(Record)) {
|
|
|
|
// Handle errors in the reader.
|
|
|
|
InstrProfError::take(std::move(E));
|
2014-03-22 01:24:48 +08:00
|
|
|
*this = InstrProfIterator();
|
2016-05-19 11:54:45 +08:00
|
|
|
}
|
2014-03-22 01:24:48 +08:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:47:58 +08:00
|
|
|
bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) {
|
|
|
|
// Verify that this really looks like plain ASCII text by checking a
|
|
|
|
// 'reasonable' number of characters (up to profile magic size).
|
|
|
|
size_t count = std::min(Buffer.getBufferSize(), sizeof(uint64_t));
|
|
|
|
StringRef buffer = Buffer.getBufferStart();
|
2015-12-11 08:40:05 +08:00
|
|
|
return count == 0 ||
|
|
|
|
std::all_of(buffer.begin(), buffer.begin() + count,
|
2018-07-26 23:31:41 +08:00
|
|
|
[](char c) { return isPrint(c) || ::isspace(c); });
|
2015-11-13 11:47:58 +08:00
|
|
|
}
|
|
|
|
|
2016-02-11 01:18:30 +08:00
|
|
|
// Read the profile variant flag from the header: ":FE" means this is a FE
|
|
|
|
// generated profile. ":IR" means this is an IR level profile. Other strings
|
|
|
|
// with a leading ':' will be reported an error format.
|
2016-05-19 11:54:45 +08:00
|
|
|
Error TextInstrProfReader::readHeader() {
|
2015-12-20 14:22:13 +08:00
|
|
|
Symtab.reset(new InstrProfSymtab());
|
2016-02-11 01:18:30 +08:00
|
|
|
bool IsIRInstr = false;
|
|
|
|
if (!Line->startswith(":")) {
|
|
|
|
IsIRLevelProfile = false;
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
StringRef Str = (Line)->substr(1);
|
|
|
|
if (Str.equals_lower("ir"))
|
|
|
|
IsIRInstr = true;
|
|
|
|
else if (Str.equals_lower("fe"))
|
|
|
|
IsIRInstr = false;
|
|
|
|
else
|
2016-05-19 11:54:45 +08:00
|
|
|
return error(instrprof_error::bad_header);
|
2016-02-11 01:18:30 +08:00
|
|
|
|
|
|
|
++Line;
|
|
|
|
IsIRLevelProfile = IsIRInstr;
|
2015-12-20 14:22:13 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Error
|
2015-12-15 02:44:01 +08:00
|
|
|
TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) {
|
|
|
|
|
|
|
|
#define CHECK_LINE_END(Line) \
|
|
|
|
if (Line.is_at_end()) \
|
|
|
|
return error(instrprof_error::truncated);
|
|
|
|
#define READ_NUM(Str, Dst) \
|
|
|
|
if ((Str).getAsInteger(10, (Dst))) \
|
|
|
|
return error(instrprof_error::malformed);
|
|
|
|
#define VP_READ_ADVANCE(Val) \
|
|
|
|
CHECK_LINE_END(Line); \
|
|
|
|
uint32_t Val; \
|
|
|
|
READ_NUM((*Line), (Val)); \
|
|
|
|
Line++;
|
|
|
|
|
|
|
|
if (Line.is_at_end())
|
|
|
|
return success();
|
2015-12-20 14:22:13 +08:00
|
|
|
|
2015-12-15 02:44:01 +08:00
|
|
|
uint32_t NumValueKinds;
|
|
|
|
if (Line->getAsInteger(10, NumValueKinds)) {
|
|
|
|
// No value profile data
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
if (NumValueKinds == 0 || NumValueKinds > IPVK_Last + 1)
|
|
|
|
return error(instrprof_error::malformed);
|
|
|
|
Line++;
|
|
|
|
|
|
|
|
for (uint32_t VK = 0; VK < NumValueKinds; VK++) {
|
|
|
|
VP_READ_ADVANCE(ValueKind);
|
|
|
|
if (ValueKind > IPVK_Last)
|
|
|
|
return error(instrprof_error::malformed);
|
|
|
|
VP_READ_ADVANCE(NumValueSites);
|
|
|
|
if (!NumValueSites)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Record.reserveSites(VK, NumValueSites);
|
|
|
|
for (uint32_t S = 0; S < NumValueSites; S++) {
|
|
|
|
VP_READ_ADVANCE(NumValueData);
|
|
|
|
|
|
|
|
std::vector<InstrProfValueData> CurrentValues;
|
|
|
|
for (uint32_t V = 0; V < NumValueData; V++) {
|
|
|
|
CHECK_LINE_END(Line);
|
2016-05-07 07:20:58 +08:00
|
|
|
std::pair<StringRef, StringRef> VD = Line->rsplit(':');
|
2015-12-15 02:44:01 +08:00
|
|
|
uint64_t TakenCount, Value;
|
2017-02-28 05:42:39 +08:00
|
|
|
if (ValueKind == IPVK_IndirectCallTarget) {
|
2018-03-23 05:26:52 +08:00
|
|
|
if (InstrProfSymtab::isExternalSymbol(VD.first)) {
|
|
|
|
Value = 0;
|
|
|
|
} else {
|
|
|
|
if (Error E = Symtab->addFuncName(VD.first))
|
|
|
|
return E;
|
|
|
|
Value = IndexedInstrProf::ComputeHash(VD.first);
|
|
|
|
}
|
2015-12-20 14:22:13 +08:00
|
|
|
} else {
|
2015-12-15 02:44:01 +08:00
|
|
|
READ_NUM(VD.first, Value);
|
|
|
|
}
|
2015-12-20 14:22:13 +08:00
|
|
|
READ_NUM(VD.second, TakenCount);
|
2015-12-15 02:44:01 +08:00
|
|
|
CurrentValues.push_back({Value, TakenCount});
|
|
|
|
Line++;
|
|
|
|
}
|
2017-02-28 05:42:39 +08:00
|
|
|
Record.addValueData(ValueKind, S, CurrentValues.data(), NumValueData,
|
|
|
|
nullptr);
|
2015-12-15 02:44:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
|
|
|
|
#undef CHECK_LINE_END
|
|
|
|
#undef READ_NUM
|
|
|
|
#undef VP_READ_ADVANCE
|
|
|
|
}
|
|
|
|
|
2017-07-07 03:00:12 +08:00
|
|
|
Error TextInstrProfReader::readNextRecord(NamedInstrProfRecord &Record) {
|
2014-07-30 06:29:23 +08:00
|
|
|
// Skip empty lines and comments.
|
|
|
|
while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
|
2014-03-22 01:24:48 +08:00
|
|
|
++Line;
|
|
|
|
// If we hit EOF while looking for a name, we're done.
|
2015-12-20 14:22:13 +08:00
|
|
|
if (Line.is_at_end()) {
|
2014-03-22 01:24:48 +08:00
|
|
|
return error(instrprof_error::eof);
|
2015-12-20 14:22:13 +08:00
|
|
|
}
|
2014-03-22 01:24:48 +08:00
|
|
|
|
|
|
|
// Read the function name.
|
|
|
|
Record.Name = *Line++;
|
2017-06-20 09:38:56 +08:00
|
|
|
if (Error E = Symtab->addFuncName(Record.Name))
|
2018-03-23 05:26:52 +08:00
|
|
|
return error(std::move(E));
|
2014-03-22 01:24:48 +08:00
|
|
|
|
|
|
|
// Read the function hash.
|
|
|
|
if (Line.is_at_end())
|
|
|
|
return error(instrprof_error::truncated);
|
2015-03-10 02:54:49 +08:00
|
|
|
if ((Line++)->getAsInteger(0, Record.Hash))
|
2014-03-22 01:24:48 +08:00
|
|
|
return error(instrprof_error::malformed);
|
|
|
|
|
|
|
|
// Read the number of counters.
|
|
|
|
uint64_t NumCounters;
|
|
|
|
if (Line.is_at_end())
|
|
|
|
return error(instrprof_error::truncated);
|
|
|
|
if ((Line++)->getAsInteger(10, NumCounters))
|
|
|
|
return error(instrprof_error::malformed);
|
2014-04-25 10:45:33 +08:00
|
|
|
if (NumCounters == 0)
|
|
|
|
return error(instrprof_error::malformed);
|
2014-03-22 01:24:48 +08:00
|
|
|
|
|
|
|
// Read each counter and fill our internal storage with the values.
|
2017-03-04 05:56:34 +08:00
|
|
|
Record.Clear();
|
2015-06-23 07:58:05 +08:00
|
|
|
Record.Counts.reserve(NumCounters);
|
2014-03-22 01:24:48 +08:00
|
|
|
for (uint64_t I = 0; I < NumCounters; ++I) {
|
|
|
|
if (Line.is_at_end())
|
|
|
|
return error(instrprof_error::truncated);
|
|
|
|
uint64_t Count;
|
|
|
|
if ((Line++)->getAsInteger(10, Count))
|
|
|
|
return error(instrprof_error::malformed);
|
2015-06-23 07:58:05 +08:00
|
|
|
Record.Counts.push_back(Count);
|
2014-03-22 01:24:48 +08:00
|
|
|
}
|
|
|
|
|
2015-12-15 02:44:01 +08:00
|
|
|
// Check if value profile data exists and read it if so.
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = readValueProfileData(Record))
|
2018-03-23 05:26:52 +08:00
|
|
|
return error(std::move(E));
|
2015-12-15 02:44:01 +08:00
|
|
|
|
2014-03-22 01:24:48 +08:00
|
|
|
return success();
|
|
|
|
}
|
2014-03-22 02:26:05 +08:00
|
|
|
|
2014-03-23 11:38:12 +08:00
|
|
|
template <class IntPtrT>
|
|
|
|
bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
|
2014-03-24 08:47:18 +08:00
|
|
|
if (DataBuffer.getBufferSize() < sizeof(uint64_t))
|
2014-03-22 04:42:31 +08:00
|
|
|
return false;
|
2014-03-24 08:47:18 +08:00
|
|
|
uint64_t Magic =
|
|
|
|
*reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
|
2015-10-18 09:02:29 +08:00
|
|
|
return RawInstrProf::getMagic<IntPtrT>() == Magic ||
|
|
|
|
sys::getSwappedBytes(RawInstrProf::getMagic<IntPtrT>()) == Magic;
|
2014-03-22 04:42:31 +08:00
|
|
|
}
|
|
|
|
|
2014-03-23 11:38:12 +08:00
|
|
|
template <class IntPtrT>
|
2016-05-19 11:54:45 +08:00
|
|
|
Error RawInstrProfReader<IntPtrT>::readHeader() {
|
2014-03-22 04:42:31 +08:00
|
|
|
if (!hasFormat(*DataBuffer))
|
|
|
|
return error(instrprof_error::bad_magic);
|
2015-10-18 09:02:29 +08:00
|
|
|
if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header))
|
2014-03-22 04:42:28 +08:00
|
|
|
return error(instrprof_error::bad_header);
|
2015-10-18 09:02:29 +08:00
|
|
|
auto *Header = reinterpret_cast<const RawInstrProf::Header *>(
|
|
|
|
DataBuffer->getBufferStart());
|
|
|
|
ShouldSwapBytes = Header->Magic != RawInstrProf::getMagic<IntPtrT>();
|
2014-03-22 02:26:05 +08:00
|
|
|
return readHeader(*Header);
|
|
|
|
}
|
|
|
|
|
2014-05-16 08:38:00 +08:00
|
|
|
template <class IntPtrT>
|
2016-05-19 11:54:45 +08:00
|
|
|
Error RawInstrProfReader<IntPtrT>::readNextHeader(const char *CurrentPos) {
|
2014-05-16 08:38:00 +08:00
|
|
|
const char *End = DataBuffer->getBufferEnd();
|
|
|
|
// Skip zero padding between profiles.
|
|
|
|
while (CurrentPos != End && *CurrentPos == 0)
|
|
|
|
++CurrentPos;
|
|
|
|
// If there's nothing left, we're done.
|
|
|
|
if (CurrentPos == End)
|
2016-05-19 11:54:45 +08:00
|
|
|
return make_error<InstrProfError>(instrprof_error::eof);
|
2014-05-16 08:38:00 +08:00
|
|
|
// If there isn't enough space for another header, this is probably just
|
|
|
|
// garbage at the end of the file.
|
2015-10-18 09:02:29 +08:00
|
|
|
if (CurrentPos + sizeof(RawInstrProf::Header) > End)
|
2016-05-19 11:54:45 +08:00
|
|
|
return make_error<InstrProfError>(instrprof_error::malformed);
|
2014-09-13 05:22:55 +08:00
|
|
|
// The writer ensures each profile is padded to start at an aligned address.
|
2016-10-20 23:02:18 +08:00
|
|
|
if (reinterpret_cast<size_t>(CurrentPos) % alignof(uint64_t))
|
2016-05-19 11:54:45 +08:00
|
|
|
return make_error<InstrProfError>(instrprof_error::malformed);
|
2014-05-16 08:38:00 +08:00
|
|
|
// The magic should have the same byte order as in the previous header.
|
|
|
|
uint64_t Magic = *reinterpret_cast<const uint64_t *>(CurrentPos);
|
2015-10-18 09:02:29 +08:00
|
|
|
if (Magic != swap(RawInstrProf::getMagic<IntPtrT>()))
|
2016-05-19 11:54:45 +08:00
|
|
|
return make_error<InstrProfError>(instrprof_error::bad_magic);
|
2014-05-16 08:38:00 +08:00
|
|
|
|
|
|
|
// There's another profile to read, so we need to process the header.
|
2015-10-18 09:02:29 +08:00
|
|
|
auto *Header = reinterpret_cast<const RawInstrProf::Header *>(CurrentPos);
|
2014-05-16 08:38:00 +08:00
|
|
|
return readHeader(*Header);
|
|
|
|
}
|
|
|
|
|
2014-03-23 11:38:12 +08:00
|
|
|
template <class IntPtrT>
|
2016-05-19 11:54:45 +08:00
|
|
|
Error RawInstrProfReader<IntPtrT>::createSymtab(InstrProfSymtab &Symtab) {
|
|
|
|
if (Error E = Symtab.create(StringRef(NamesStart, NamesSize)))
|
|
|
|
return error(std::move(E));
|
2015-12-20 14:22:13 +08:00
|
|
|
for (const RawInstrProf::ProfileData<IntPtrT> *I = Data; I != DataEnd; ++I) {
|
|
|
|
const IntPtrT FPtr = swap(I->FunctionPointer);
|
|
|
|
if (!FPtr)
|
|
|
|
continue;
|
2016-02-09 02:13:49 +08:00
|
|
|
Symtab.mapAddress(FPtr, I->NameRef);
|
2015-12-20 14:22:13 +08:00
|
|
|
}
|
2016-04-22 05:07:25 +08:00
|
|
|
return success();
|
2015-12-20 14:22:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class IntPtrT>
|
2016-05-19 11:54:45 +08:00
|
|
|
Error RawInstrProfReader<IntPtrT>::readHeader(
|
|
|
|
const RawInstrProf::Header &Header) {
|
2016-02-11 01:18:30 +08:00
|
|
|
Version = swap(Header.Version);
|
|
|
|
if (GET_VERSION(Version) != RawInstrProf::Version)
|
2014-03-22 02:26:05 +08:00
|
|
|
return error(instrprof_error::unsupported_version);
|
|
|
|
|
|
|
|
CountersDelta = swap(Header.CountersDelta);
|
|
|
|
NamesDelta = swap(Header.NamesDelta);
|
|
|
|
auto DataSize = swap(Header.DataSize);
|
|
|
|
auto CountersSize = swap(Header.CountersSize);
|
2016-02-09 02:13:49 +08:00
|
|
|
NamesSize = swap(Header.NamesSize);
|
2015-11-19 02:14:55 +08:00
|
|
|
ValueKindLast = swap(Header.ValueKindLast);
|
|
|
|
|
|
|
|
auto DataSizeInBytes = DataSize * sizeof(RawInstrProf::ProfileData<IntPtrT>);
|
|
|
|
auto PaddingSize = getNumPaddingBytes(NamesSize);
|
2014-03-22 02:26:05 +08:00
|
|
|
|
2015-10-18 09:02:29 +08:00
|
|
|
ptrdiff_t DataOffset = sizeof(RawInstrProf::Header);
|
2015-11-19 02:14:55 +08:00
|
|
|
ptrdiff_t CountersOffset = DataOffset + DataSizeInBytes;
|
2014-03-22 02:26:05 +08:00
|
|
|
ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
|
2015-11-19 02:14:55 +08:00
|
|
|
ptrdiff_t ValueDataOffset = NamesOffset + NamesSize + PaddingSize;
|
2014-03-22 02:26:05 +08:00
|
|
|
|
2014-05-16 08:38:00 +08:00
|
|
|
auto *Start = reinterpret_cast<const char *>(&Header);
|
2016-05-06 03:41:18 +08:00
|
|
|
if (Start + ValueDataOffset > DataBuffer->getBufferEnd())
|
2014-03-22 04:42:28 +08:00
|
|
|
return error(instrprof_error::bad_header);
|
2014-03-22 02:26:05 +08:00
|
|
|
|
2015-10-18 09:02:29 +08:00
|
|
|
Data = reinterpret_cast<const RawInstrProf::ProfileData<IntPtrT> *>(
|
|
|
|
Start + DataOffset);
|
2014-03-22 02:26:05 +08:00
|
|
|
DataEnd = Data + DataSize;
|
2014-03-24 08:47:18 +08:00
|
|
|
CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
|
|
|
|
NamesStart = Start + NamesOffset;
|
2015-12-20 14:22:13 +08:00
|
|
|
ValueDataStart = reinterpret_cast<const uint8_t *>(Start + ValueDataOffset);
|
2014-03-22 02:26:05 +08:00
|
|
|
|
2015-12-20 14:22:13 +08:00
|
|
|
std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = createSymtab(*NewSymtab.get()))
|
|
|
|
return E;
|
2016-04-22 05:07:25 +08:00
|
|
|
|
2015-12-20 14:22:13 +08:00
|
|
|
Symtab = std::move(NewSymtab);
|
2014-03-22 02:26:05 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2014-03-23 11:38:12 +08:00
|
|
|
template <class IntPtrT>
|
2017-07-07 03:00:12 +08:00
|
|
|
Error RawInstrProfReader<IntPtrT>::readName(NamedInstrProfRecord &Record) {
|
2016-02-09 02:13:49 +08:00
|
|
|
Record.Name = getName(Data->NameRef);
|
2015-10-29 03:34:04 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class IntPtrT>
|
2017-07-07 03:00:12 +08:00
|
|
|
Error RawInstrProfReader<IntPtrT>::readFuncHash(NamedInstrProfRecord &Record) {
|
2015-10-29 03:34:04 +08:00
|
|
|
Record.Hash = swap(Data->FuncHash);
|
|
|
|
return success();
|
|
|
|
}
|
2014-03-22 02:26:05 +08:00
|
|
|
|
2015-10-29 03:34:04 +08:00
|
|
|
template <class IntPtrT>
|
2016-05-19 11:54:45 +08:00
|
|
|
Error RawInstrProfReader<IntPtrT>::readRawCounts(
|
2015-10-29 03:34:04 +08:00
|
|
|
InstrProfRecord &Record) {
|
2014-04-25 10:45:33 +08:00
|
|
|
uint32_t NumCounters = swap(Data->NumCounters);
|
2015-10-29 03:34:04 +08:00
|
|
|
IntPtrT CounterPtr = Data->CounterPtr;
|
2014-04-25 10:45:33 +08:00
|
|
|
if (NumCounters == 0)
|
|
|
|
return error(instrprof_error::malformed);
|
2014-03-22 02:26:05 +08:00
|
|
|
|
2015-10-29 03:34:04 +08:00
|
|
|
auto RawCounts = makeArrayRef(getCounter(CounterPtr), NumCounters);
|
2014-03-24 08:47:18 +08:00
|
|
|
auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
|
2015-10-29 03:34:04 +08:00
|
|
|
|
|
|
|
// Check bounds.
|
|
|
|
if (RawCounts.data() < CountersStart ||
|
2014-03-24 08:47:18 +08:00
|
|
|
RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
|
2014-03-22 02:26:05 +08:00
|
|
|
return error(instrprof_error::malformed);
|
|
|
|
|
|
|
|
if (ShouldSwapBytes) {
|
2015-06-23 07:58:05 +08:00
|
|
|
Record.Counts.clear();
|
|
|
|
Record.Counts.reserve(RawCounts.size());
|
2014-03-22 02:26:05 +08:00
|
|
|
for (uint64_t Count : RawCounts)
|
2015-06-23 07:58:05 +08:00
|
|
|
Record.Counts.push_back(swap(Count));
|
2014-03-22 02:26:05 +08:00
|
|
|
} else
|
|
|
|
Record.Counts = RawCounts;
|
|
|
|
|
2015-10-29 03:34:04 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class IntPtrT>
|
2016-05-19 11:54:45 +08:00
|
|
|
Error RawInstrProfReader<IntPtrT>::readValueProfilingData(
|
|
|
|
InstrProfRecord &Record) {
|
2015-11-19 02:14:55 +08:00
|
|
|
Record.clearValueData();
|
2015-12-11 14:53:53 +08:00
|
|
|
CurValueDataSize = 0;
|
|
|
|
// Need to match the logic in value profile dumper code in compiler-rt:
|
|
|
|
uint32_t NumValueKinds = 0;
|
|
|
|
for (uint32_t I = 0; I < IPVK_Last + 1; I++)
|
|
|
|
NumValueKinds += (Data->NumValueSites[I] != 0);
|
|
|
|
|
|
|
|
if (!NumValueKinds)
|
2015-11-19 02:14:55 +08:00
|
|
|
return success();
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
|
2016-05-06 03:41:18 +08:00
|
|
|
ValueProfData::getValueProfData(
|
|
|
|
ValueDataStart, (const unsigned char *)DataBuffer->getBufferEnd(),
|
|
|
|
getDataEndianness());
|
2015-11-19 02:14:55 +08:00
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = VDataPtrOrErr.takeError())
|
|
|
|
return E;
|
2015-11-19 02:14:55 +08:00
|
|
|
|
2016-03-29 02:27:44 +08:00
|
|
|
// Note that besides deserialization, this also performs the conversion for
|
|
|
|
// indirect call targets. The function pointers from the raw profile are
|
|
|
|
// remapped into function name hashes.
|
2018-03-22 06:27:31 +08:00
|
|
|
VDataPtrOrErr.get()->deserializeTo(Record, Symtab.get());
|
2015-12-11 14:53:53 +08:00
|
|
|
CurValueDataSize = VDataPtrOrErr.get()->getSize();
|
2015-11-19 02:14:55 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class IntPtrT>
|
2017-07-07 03:00:12 +08:00
|
|
|
Error RawInstrProfReader<IntPtrT>::readNextRecord(NamedInstrProfRecord &Record) {
|
2015-10-29 03:34:04 +08:00
|
|
|
if (atEnd())
|
2016-05-06 03:41:18 +08:00
|
|
|
// At this point, ValueDataStart field points to the next header.
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = readNextHeader(getNextHeaderPos()))
|
2018-04-13 23:04:36 +08:00
|
|
|
return error(std::move(E));
|
2015-10-29 03:34:04 +08:00
|
|
|
|
|
|
|
// Read name ad set it in Record.
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = readName(Record))
|
2018-04-13 23:04:36 +08:00
|
|
|
return error(std::move(E));
|
2015-10-29 03:34:04 +08:00
|
|
|
|
|
|
|
// Read FuncHash and set it in Record.
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = readFuncHash(Record))
|
2018-04-13 23:04:36 +08:00
|
|
|
return error(std::move(E));
|
2015-10-29 03:34:04 +08:00
|
|
|
|
|
|
|
// Read raw counts and set Record.
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = readRawCounts(Record))
|
2018-04-13 23:04:36 +08:00
|
|
|
return error(std::move(E));
|
2015-10-29 03:34:04 +08:00
|
|
|
|
2015-11-19 02:14:55 +08:00
|
|
|
// Read value data and set Record.
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Error E = readValueProfilingData(Record))
|
2018-04-13 23:04:36 +08:00
|
|
|
return error(std::move(E));
|
2015-11-19 02:14:55 +08:00
|
|
|
|
2014-03-22 02:26:05 +08:00
|
|
|
// Iterate.
|
2015-10-29 03:34:04 +08:00
|
|
|
advanceData();
|
2014-03-22 02:26:05 +08:00
|
|
|
return success();
|
|
|
|
}
|
2014-03-23 11:38:12 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
2017-03-03 09:07:34 +08:00
|
|
|
|
2014-03-23 11:38:12 +08:00
|
|
|
template class RawInstrProfReader<uint32_t>;
|
|
|
|
template class RawInstrProfReader<uint64_t>;
|
2017-03-03 09:07:34 +08:00
|
|
|
|
|
|
|
} // end namespace llvm
|
2014-04-19 05:48:40 +08:00
|
|
|
|
2014-04-19 06:00:22 +08:00
|
|
|
InstrProfLookupTrait::hash_value_type
|
|
|
|
InstrProfLookupTrait::ComputeHash(StringRef K) {
|
|
|
|
return IndexedInstrProf::ComputeHash(HashType, K);
|
|
|
|
}
|
|
|
|
|
2017-06-22 07:19:47 +08:00
|
|
|
using data_type = InstrProfLookupTrait::data_type;
|
|
|
|
using offset_type = InstrProfLookupTrait::offset_type;
|
2015-06-23 07:58:05 +08:00
|
|
|
|
2015-11-28 13:06:00 +08:00
|
|
|
bool InstrProfLookupTrait::readValueProfilingData(
|
2015-09-30 06:13:58 +08:00
|
|
|
const unsigned char *&D, const unsigned char *const End) {
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<std::unique_ptr<ValueProfData>> VDataPtrOrErr =
|
2015-11-18 07:00:40 +08:00
|
|
|
ValueProfData::getValueProfData(D, End, ValueProfDataEndianness);
|
2015-09-30 06:13:58 +08:00
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
if (VDataPtrOrErr.takeError())
|
2015-09-30 06:13:58 +08:00
|
|
|
return false;
|
|
|
|
|
2015-12-20 14:22:13 +08:00
|
|
|
VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr);
|
2015-11-10 08:24:45 +08:00
|
|
|
D += VDataPtrOrErr.get()->TotalSize;
|
2015-09-30 06:13:58 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-23 07:58:05 +08:00
|
|
|
data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D,
|
|
|
|
offset_type N) {
|
2017-03-03 09:07:34 +08:00
|
|
|
using namespace support;
|
|
|
|
|
2015-06-23 07:58:05 +08:00
|
|
|
// Check if the data is corrupt. If so, don't try to read it.
|
|
|
|
if (N % sizeof(uint64_t))
|
|
|
|
return data_type();
|
|
|
|
|
|
|
|
DataBuffer.clear();
|
|
|
|
std::vector<uint64_t> CounterBuffer;
|
|
|
|
|
2015-09-30 06:13:58 +08:00
|
|
|
const unsigned char *End = D + N;
|
|
|
|
while (D < End) {
|
2015-10-14 00:35:59 +08:00
|
|
|
// Read hash.
|
2015-09-30 06:13:58 +08:00
|
|
|
if (D + sizeof(uint64_t) >= End)
|
2015-06-23 07:58:05 +08:00
|
|
|
return data_type();
|
2015-09-30 06:13:58 +08:00
|
|
|
uint64_t Hash = endian::readNext<uint64_t, little, unaligned>(D);
|
2015-06-23 07:58:05 +08:00
|
|
|
|
2016-02-11 01:18:30 +08:00
|
|
|
// Initialize number of counters for GET_VERSION(FormatVersion) == 1.
|
2015-09-30 06:13:58 +08:00
|
|
|
uint64_t CountsSize = N / sizeof(uint64_t) - 1;
|
2015-10-14 00:35:59 +08:00
|
|
|
// If format version is different then read the number of counters.
|
2016-02-11 01:18:30 +08:00
|
|
|
if (GET_VERSION(FormatVersion) != IndexedInstrProf::ProfVersion::Version1) {
|
2015-09-30 06:13:58 +08:00
|
|
|
if (D + sizeof(uint64_t) > End)
|
|
|
|
return data_type();
|
|
|
|
CountsSize = endian::readNext<uint64_t, little, unaligned>(D);
|
|
|
|
}
|
2015-10-14 00:35:59 +08:00
|
|
|
// Read counter values.
|
2015-09-30 06:13:58 +08:00
|
|
|
if (D + CountsSize * sizeof(uint64_t) > End)
|
2015-06-23 07:58:05 +08:00
|
|
|
return data_type();
|
|
|
|
|
|
|
|
CounterBuffer.clear();
|
2015-09-30 06:13:58 +08:00
|
|
|
CounterBuffer.reserve(CountsSize);
|
|
|
|
for (uint64_t J = 0; J < CountsSize; ++J)
|
2015-06-23 07:58:05 +08:00
|
|
|
CounterBuffer.push_back(endian::readNext<uint64_t, little, unaligned>(D));
|
|
|
|
|
2015-11-02 13:08:23 +08:00
|
|
|
DataBuffer.emplace_back(K, Hash, std::move(CounterBuffer));
|
2015-09-30 06:13:58 +08:00
|
|
|
|
2015-10-14 00:35:59 +08:00
|
|
|
// Read value profiling data.
|
2016-02-11 01:18:30 +08:00
|
|
|
if (GET_VERSION(FormatVersion) > IndexedInstrProf::ProfVersion::Version2 &&
|
2016-01-14 10:47:01 +08:00
|
|
|
!readValueProfilingData(D, End)) {
|
2015-09-30 06:13:58 +08:00
|
|
|
DataBuffer.clear();
|
|
|
|
return data_type();
|
|
|
|
}
|
2015-06-23 07:58:05 +08:00
|
|
|
}
|
|
|
|
return DataBuffer;
|
|
|
|
}
|
|
|
|
|
2015-12-02 04:26:26 +08:00
|
|
|
template <typename HashTableImpl>
|
2016-05-19 11:54:45 +08:00
|
|
|
Error InstrProfReaderIndex<HashTableImpl>::getRecords(
|
2017-07-07 03:00:12 +08:00
|
|
|
StringRef FuncName, ArrayRef<NamedInstrProfRecord> &Data) {
|
2015-12-02 04:26:26 +08:00
|
|
|
auto Iter = HashTable->find(FuncName);
|
|
|
|
if (Iter == HashTable->end())
|
2016-05-19 11:54:45 +08:00
|
|
|
return make_error<InstrProfError>(instrprof_error::unknown_function);
|
2015-10-28 12:20:31 +08:00
|
|
|
|
|
|
|
Data = (*Iter);
|
2015-11-12 08:32:17 +08:00
|
|
|
if (Data.empty())
|
2016-05-19 11:54:45 +08:00
|
|
|
return make_error<InstrProfError>(instrprof_error::malformed);
|
2015-10-28 12:20:31 +08:00
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
return Error::success();
|
2015-10-28 12:20:31 +08:00
|
|
|
}
|
|
|
|
|
2015-12-02 04:26:26 +08:00
|
|
|
template <typename HashTableImpl>
|
2016-05-19 11:54:45 +08:00
|
|
|
Error InstrProfReaderIndex<HashTableImpl>::getRecords(
|
2017-07-07 03:00:12 +08:00
|
|
|
ArrayRef<NamedInstrProfRecord> &Data) {
|
2015-12-02 04:26:26 +08:00
|
|
|
if (atEnd())
|
2016-05-19 11:54:45 +08:00
|
|
|
return make_error<InstrProfError>(instrprof_error::eof);
|
2015-10-28 12:20:31 +08:00
|
|
|
|
|
|
|
Data = *RecordIterator;
|
|
|
|
|
2015-12-11 07:48:05 +08:00
|
|
|
if (Data.empty())
|
2016-05-19 11:54:45 +08:00
|
|
|
return make_error<InstrProfError>(instrprof_error::malformed);
|
2015-10-28 12:20:31 +08:00
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
return Error::success();
|
2015-10-28 12:20:31 +08:00
|
|
|
}
|
|
|
|
|
2015-12-02 04:26:26 +08:00
|
|
|
template <typename HashTableImpl>
|
|
|
|
InstrProfReaderIndex<HashTableImpl>::InstrProfReaderIndex(
|
|
|
|
const unsigned char *Buckets, const unsigned char *const Payload,
|
|
|
|
const unsigned char *const Base, IndexedInstrProf::HashT HashType,
|
|
|
|
uint64_t Version) {
|
2015-10-28 12:20:31 +08:00
|
|
|
FormatVersion = Version;
|
2015-12-02 04:26:26 +08:00
|
|
|
HashTable.reset(HashTableImpl::Create(
|
|
|
|
Buckets, Payload, Base,
|
|
|
|
typename HashTableImpl::InfoType(HashType, Version)));
|
|
|
|
RecordIterator = HashTable->data_begin();
|
2015-10-28 12:20:31 +08:00
|
|
|
}
|
|
|
|
|
2018-10-11 05:09:37 +08:00
|
|
|
namespace {
|
|
|
|
/// A remapper that does not apply any remappings.
|
|
|
|
class InstrProfReaderNullRemapper : public InstrProfReaderRemapper {
|
|
|
|
InstrProfReaderIndexBase &Underlying;
|
|
|
|
|
|
|
|
public:
|
|
|
|
InstrProfReaderNullRemapper(InstrProfReaderIndexBase &Underlying)
|
|
|
|
: Underlying(Underlying) {}
|
|
|
|
|
|
|
|
Error getRecords(StringRef FuncName,
|
|
|
|
ArrayRef<NamedInstrProfRecord> &Data) override {
|
|
|
|
return Underlying.getRecords(FuncName, Data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A remapper that applies remappings based on a symbol remapping file.
|
|
|
|
template <typename HashTableImpl>
|
|
|
|
class llvm::InstrProfReaderItaniumRemapper
|
|
|
|
: public InstrProfReaderRemapper {
|
|
|
|
public:
|
|
|
|
InstrProfReaderItaniumRemapper(
|
|
|
|
std::unique_ptr<MemoryBuffer> RemapBuffer,
|
|
|
|
InstrProfReaderIndex<HashTableImpl> &Underlying)
|
|
|
|
: RemapBuffer(std::move(RemapBuffer)), Underlying(Underlying) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extract the original function name from a PGO function name.
|
|
|
|
static StringRef extractName(StringRef Name) {
|
|
|
|
// We can have multiple :-separated pieces; there can be pieces both
|
|
|
|
// before and after the mangled name. Find the first part that starts
|
|
|
|
// with '_Z'; we'll assume that's the mangled name we want.
|
|
|
|
std::pair<StringRef, StringRef> Parts = {StringRef(), Name};
|
|
|
|
while (true) {
|
|
|
|
Parts = Parts.second.split(':');
|
|
|
|
if (Parts.first.startswith("_Z"))
|
|
|
|
return Parts.first;
|
|
|
|
if (Parts.second.empty())
|
|
|
|
return Name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Given a mangled name extracted from a PGO function name, and a new
|
|
|
|
/// form for that mangled name, reconstitute the name.
|
|
|
|
static void reconstituteName(StringRef OrigName, StringRef ExtractedName,
|
|
|
|
StringRef Replacement,
|
|
|
|
SmallVectorImpl<char> &Out) {
|
|
|
|
Out.reserve(OrigName.size() + Replacement.size() - ExtractedName.size());
|
|
|
|
Out.insert(Out.end(), OrigName.begin(), ExtractedName.begin());
|
|
|
|
Out.insert(Out.end(), Replacement.begin(), Replacement.end());
|
|
|
|
Out.insert(Out.end(), ExtractedName.end(), OrigName.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
Error populateRemappings() override {
|
|
|
|
if (Error E = Remappings.read(*RemapBuffer))
|
|
|
|
return E;
|
|
|
|
for (StringRef Name : Underlying.HashTable->keys()) {
|
|
|
|
StringRef RealName = extractName(Name);
|
|
|
|
if (auto Key = Remappings.insert(RealName)) {
|
|
|
|
// FIXME: We could theoretically map the same equivalence class to
|
|
|
|
// multiple names in the profile data. If that happens, we should
|
|
|
|
// return NamedInstrProfRecords from all of them.
|
|
|
|
MappedNames.insert({Key, RealName});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Error getRecords(StringRef FuncName,
|
|
|
|
ArrayRef<NamedInstrProfRecord> &Data) override {
|
|
|
|
StringRef RealName = extractName(FuncName);
|
|
|
|
if (auto Key = Remappings.lookup(RealName)) {
|
|
|
|
StringRef Remapped = MappedNames.lookup(Key);
|
|
|
|
if (!Remapped.empty()) {
|
|
|
|
if (RealName.begin() == FuncName.begin() &&
|
|
|
|
RealName.end() == FuncName.end())
|
|
|
|
FuncName = Remapped;
|
|
|
|
else {
|
|
|
|
// Try rebuilding the name from the given remapping.
|
|
|
|
SmallString<256> Reconstituted;
|
|
|
|
reconstituteName(FuncName, RealName, Remapped, Reconstituted);
|
|
|
|
Error E = Underlying.getRecords(Reconstituted, Data);
|
|
|
|
if (!E)
|
|
|
|
return E;
|
|
|
|
|
|
|
|
// If we failed because the name doesn't exist, fall back to asking
|
|
|
|
// about the original name.
|
|
|
|
if (Error Unhandled = handleErrors(
|
|
|
|
std::move(E), [](std::unique_ptr<InstrProfError> Err) {
|
|
|
|
return Err->get() == instrprof_error::unknown_function
|
|
|
|
? Error::success()
|
|
|
|
: Error(std::move(Err));
|
|
|
|
}))
|
|
|
|
return Unhandled;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Underlying.getRecords(FuncName, Data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// The memory buffer containing the remapping configuration. Remappings
|
|
|
|
/// holds pointers into this buffer.
|
|
|
|
std::unique_ptr<MemoryBuffer> RemapBuffer;
|
|
|
|
|
|
|
|
/// The mangling remapper.
|
|
|
|
SymbolRemappingReader Remappings;
|
|
|
|
|
|
|
|
/// Mapping from mangled name keys to the name used for the key in the
|
|
|
|
/// profile data.
|
|
|
|
/// FIXME: Can we store a location within the on-disk hash table instead of
|
|
|
|
/// redoing lookup?
|
|
|
|
DenseMap<SymbolRemappingReader::Key, StringRef> MappedNames;
|
|
|
|
|
|
|
|
/// The real profile data reader.
|
|
|
|
InstrProfReaderIndex<HashTableImpl> &Underlying;
|
|
|
|
};
|
|
|
|
|
2014-04-19 05:48:40 +08:00
|
|
|
bool IndexedInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
|
2017-03-03 09:07:34 +08:00
|
|
|
using namespace support;
|
|
|
|
|
2015-11-12 08:32:17 +08:00
|
|
|
if (DataBuffer.getBufferSize() < 8)
|
|
|
|
return false;
|
2014-04-19 05:48:40 +08:00
|
|
|
uint64_t Magic =
|
|
|
|
endian::read<uint64_t, little, aligned>(DataBuffer.getBufferStart());
|
2015-10-14 00:35:59 +08:00
|
|
|
// Verify that it's magical.
|
2014-04-19 05:48:40 +08:00
|
|
|
return Magic == IndexedInstrProf::Magic;
|
|
|
|
}
|
|
|
|
|
2016-02-03 12:08:18 +08:00
|
|
|
const unsigned char *
|
|
|
|
IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version,
|
|
|
|
const unsigned char *Cur) {
|
2016-05-20 05:07:12 +08:00
|
|
|
using namespace IndexedInstrProf;
|
2016-02-03 12:08:18 +08:00
|
|
|
using namespace support;
|
2017-03-03 09:07:34 +08:00
|
|
|
|
2016-02-03 12:08:18 +08:00
|
|
|
if (Version >= IndexedInstrProf::Version4) {
|
|
|
|
const IndexedInstrProf::Summary *SummaryInLE =
|
|
|
|
reinterpret_cast<const IndexedInstrProf::Summary *>(Cur);
|
|
|
|
uint64_t NFields =
|
|
|
|
endian::byte_swap<uint64_t, little>(SummaryInLE->NumSummaryFields);
|
|
|
|
uint64_t NEntries =
|
|
|
|
endian::byte_swap<uint64_t, little>(SummaryInLE->NumCutoffEntries);
|
|
|
|
uint32_t SummarySize =
|
|
|
|
IndexedInstrProf::Summary::getSize(NFields, NEntries);
|
|
|
|
std::unique_ptr<IndexedInstrProf::Summary> SummaryData =
|
|
|
|
IndexedInstrProf::allocSummary(SummarySize);
|
|
|
|
|
|
|
|
const uint64_t *Src = reinterpret_cast<const uint64_t *>(SummaryInLE);
|
|
|
|
uint64_t *Dst = reinterpret_cast<uint64_t *>(SummaryData.get());
|
|
|
|
for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
|
|
|
|
Dst[I] = endian::byte_swap<uint64_t, little>(Src[I]);
|
|
|
|
|
2017-06-22 07:19:47 +08:00
|
|
|
SummaryEntryVector DetailedSummary;
|
2016-05-20 05:07:12 +08:00
|
|
|
for (unsigned I = 0; I < SummaryData->NumCutoffEntries; I++) {
|
|
|
|
const IndexedInstrProf::Summary::Entry &Ent = SummaryData->getEntry(I);
|
|
|
|
DetailedSummary.emplace_back((uint32_t)Ent.Cutoff, Ent.MinBlockCount,
|
|
|
|
Ent.NumBlocks);
|
|
|
|
}
|
2016-02-18 02:18:47 +08:00
|
|
|
// initialize InstrProfSummary using the SummaryData from disk.
|
2016-05-20 05:53:28 +08:00
|
|
|
this->Summary = llvm::make_unique<ProfileSummary>(
|
|
|
|
ProfileSummary::PSK_Instr, DetailedSummary,
|
2016-05-20 05:07:12 +08:00
|
|
|
SummaryData->get(Summary::TotalBlockCount),
|
|
|
|
SummaryData->get(Summary::MaxBlockCount),
|
|
|
|
SummaryData->get(Summary::MaxInternalBlockCount),
|
|
|
|
SummaryData->get(Summary::MaxFunctionCount),
|
|
|
|
SummaryData->get(Summary::TotalNumBlocks),
|
2016-05-20 05:53:28 +08:00
|
|
|
SummaryData->get(Summary::TotalNumFunctions));
|
2016-02-03 12:08:18 +08:00
|
|
|
return Cur + SummarySize;
|
|
|
|
} else {
|
|
|
|
// For older version of profile data, we need to compute on the fly:
|
|
|
|
using namespace IndexedInstrProf;
|
2017-03-03 09:07:34 +08:00
|
|
|
|
2016-05-20 05:07:12 +08:00
|
|
|
InstrProfSummaryBuilder Builder(ProfileSummaryBuilder::DefaultCutoffs);
|
|
|
|
// FIXME: This only computes an empty summary. Need to call addRecord for
|
2017-07-07 03:00:12 +08:00
|
|
|
// all NamedInstrProfRecords to get the correct summary.
|
2016-05-20 17:18:37 +08:00
|
|
|
this->Summary = Builder.getSummary();
|
2016-02-03 12:08:18 +08:00
|
|
|
return Cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Error IndexedInstrProfReader::readHeader() {
|
2017-03-03 09:07:34 +08:00
|
|
|
using namespace support;
|
|
|
|
|
2014-05-02 01:16:24 +08:00
|
|
|
const unsigned char *Start =
|
|
|
|
(const unsigned char *)DataBuffer->getBufferStart();
|
2014-04-19 05:48:40 +08:00
|
|
|
const unsigned char *Cur = Start;
|
2014-05-02 01:16:24 +08:00
|
|
|
if ((const unsigned char *)DataBuffer->getBufferEnd() - Cur < 24)
|
2014-04-19 05:48:40 +08:00
|
|
|
return error(instrprof_error::truncated);
|
|
|
|
|
2015-10-18 09:02:29 +08:00
|
|
|
auto *Header = reinterpret_cast<const IndexedInstrProf::Header *>(Cur);
|
|
|
|
Cur += sizeof(IndexedInstrProf::Header);
|
|
|
|
|
2014-04-19 05:48:40 +08:00
|
|
|
// Check the magic number.
|
2015-10-18 09:02:29 +08:00
|
|
|
uint64_t Magic = endian::byte_swap<uint64_t, little>(Header->Magic);
|
2014-04-19 05:48:40 +08:00
|
|
|
if (Magic != IndexedInstrProf::Magic)
|
|
|
|
return error(instrprof_error::bad_magic);
|
|
|
|
|
|
|
|
// Read the version.
|
2015-10-28 12:20:31 +08:00
|
|
|
uint64_t FormatVersion = endian::byte_swap<uint64_t, little>(Header->Version);
|
2016-02-11 01:18:30 +08:00
|
|
|
if (GET_VERSION(FormatVersion) >
|
|
|
|
IndexedInstrProf::ProfVersion::CurrentVersion)
|
2014-04-19 05:48:40 +08:00
|
|
|
return error(instrprof_error::unsupported_version);
|
|
|
|
|
2016-02-03 12:08:18 +08:00
|
|
|
Cur = readSummary((IndexedInstrProf::ProfVersion)FormatVersion, Cur);
|
2014-04-19 05:48:40 +08:00
|
|
|
|
|
|
|
// Read the hash type and start offset.
|
|
|
|
IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
|
2015-10-18 09:02:29 +08:00
|
|
|
endian::byte_swap<uint64_t, little>(Header->HashType));
|
2014-04-19 05:48:40 +08:00
|
|
|
if (HashType > IndexedInstrProf::HashT::Last)
|
|
|
|
return error(instrprof_error::unsupported_hash_type);
|
2015-10-18 09:02:29 +08:00
|
|
|
|
|
|
|
uint64_t HashOffset = endian::byte_swap<uint64_t, little>(Header->HashOffset);
|
2014-04-19 05:48:40 +08:00
|
|
|
|
|
|
|
// The rest of the file is an on disk hash table.
|
2018-10-11 05:09:37 +08:00
|
|
|
auto IndexPtr =
|
|
|
|
llvm::make_unique<InstrProfReaderIndex<OnDiskHashTableImplV3>>(
|
|
|
|
Start + HashOffset, Cur, Start, HashType, FormatVersion);
|
|
|
|
|
|
|
|
// Load the remapping table now if requested.
|
|
|
|
if (RemappingBuffer) {
|
|
|
|
Remapper = llvm::make_unique<
|
|
|
|
InstrProfReaderItaniumRemapper<OnDiskHashTableImplV3>>(
|
|
|
|
std::move(RemappingBuffer), *IndexPtr);
|
|
|
|
if (Error E = Remapper->populateRemappings())
|
|
|
|
return E;
|
|
|
|
} else {
|
|
|
|
Remapper = llvm::make_unique<InstrProfReaderNullRemapper>(*IndexPtr);
|
|
|
|
}
|
|
|
|
Index = std::move(IndexPtr);
|
|
|
|
|
2014-04-19 05:48:40 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2015-12-20 14:22:13 +08:00
|
|
|
InstrProfSymtab &IndexedInstrProfReader::getSymtab() {
|
|
|
|
if (Symtab.get())
|
|
|
|
return *Symtab.get();
|
|
|
|
|
|
|
|
std::unique_ptr<InstrProfSymtab> NewSymtab = make_unique<InstrProfSymtab>();
|
2017-06-20 09:38:56 +08:00
|
|
|
if (Error E = Index->populateSymtab(*NewSymtab.get())) {
|
|
|
|
consumeError(error(InstrProfError::take(std::move(E))));
|
|
|
|
}
|
2015-12-20 14:22:13 +08:00
|
|
|
|
|
|
|
Symtab = std::move(NewSymtab);
|
|
|
|
return *Symtab.get();
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Expected<InstrProfRecord>
|
2015-11-06 15:54:21 +08:00
|
|
|
IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
|
|
|
|
uint64_t FuncHash) {
|
2017-07-07 03:00:12 +08:00
|
|
|
ArrayRef<NamedInstrProfRecord> Data;
|
2018-10-11 05:09:37 +08:00
|
|
|
Error Err = Remapper->getRecords(FuncName, Data);
|
2016-05-19 11:54:45 +08:00
|
|
|
if (Err)
|
|
|
|
return std::move(Err);
|
2015-10-28 12:20:31 +08:00
|
|
|
// Found it. Look for counters with the right hash.
|
2015-06-23 07:58:05 +08:00
|
|
|
for (unsigned I = 0, E = Data.size(); I < E; ++I) {
|
2014-08-02 06:50:07 +08:00
|
|
|
// Check for a match and fill the vector if there is one.
|
2015-06-23 07:58:05 +08:00
|
|
|
if (Data[I].Hash == FuncHash) {
|
2015-11-02 13:08:23 +08:00
|
|
|
return std::move(Data[I]);
|
2014-08-02 06:50:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return error(instrprof_error::hash_mismatch);
|
2014-04-19 05:48:40 +08:00
|
|
|
}
|
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Error IndexedInstrProfReader::getFunctionCounts(StringRef FuncName,
|
|
|
|
uint64_t FuncHash,
|
|
|
|
std::vector<uint64_t> &Counts) {
|
|
|
|
Expected<InstrProfRecord> Record = getInstrProfRecord(FuncName, FuncHash);
|
|
|
|
if (Error E = Record.takeError())
|
|
|
|
return error(std::move(E));
|
2015-11-02 13:08:23 +08:00
|
|
|
|
|
|
|
Counts = Record.get().Counts;
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2017-07-07 03:00:12 +08:00
|
|
|
Error IndexedInstrProfReader::readNextRecord(NamedInstrProfRecord &Record) {
|
|
|
|
ArrayRef<NamedInstrProfRecord> Data;
|
2015-10-28 12:20:31 +08:00
|
|
|
|
2016-05-19 11:54:45 +08:00
|
|
|
Error E = Index->getRecords(Data);
|
|
|
|
if (E)
|
|
|
|
return error(std::move(E));
|
2014-08-02 06:50:07 +08:00
|
|
|
|
2015-06-23 07:58:05 +08:00
|
|
|
Record = Data[RecordIndex++];
|
|
|
|
if (RecordIndex >= Data.size()) {
|
2015-12-02 04:26:26 +08:00
|
|
|
Index->advanceToNextKey();
|
2015-06-23 07:58:05 +08:00
|
|
|
RecordIndex = 0;
|
2014-08-02 06:50:07 +08:00
|
|
|
}
|
2014-04-19 05:48:40 +08:00
|
|
|
return success();
|
|
|
|
}
|