| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#ifndef LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H |
| 13 |
#define LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H |
| 14 |
|
| 15 |
#include "llvm/ADT/DenseSet.h" |
| 16 |
#include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 17 |
#include "llvm/Object/Binary.h" |
| 18 |
#include "llvm/Object/ObjectFile.h" |
| 19 |
#include "llvm/ProfileData/InstrProf.h" |
| 20 |
#include "llvm/Support/Casting.h" |
| 21 |
#include "llvm/Support/Error.h" |
| 22 |
#include "llvm/Support/MemoryBuffer.h" |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
namespace llvm { |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class InstrProfCorrelator { |
| 30 |
public: |
| 31 |
static llvm::Expected<std::unique_ptr<InstrProfCorrelator>> |
| 32 |
get(StringRef DebugInfoFilename); |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
virtual Error correlateProfileData() = 0; |
| 37 |
|
| 38 |
|
| 39 |
llvm::Optional<size_t> getDataSize() const; |
| 40 |
|
| 41 |
|
| 42 |
const char *getNamesPointer() const { return Names.c_str(); } |
| 43 |
|
| 44 |
|
| 45 |
size_t getNamesSize() const { return Names.size(); } |
| 46 |
|
| 47 |
|
| 48 |
uint64_t getCountersSectionSize() const { |
| 49 |
return Ctx->CountersSectionEnd - Ctx->CountersSectionStart; |
| 50 |
} |
| 51 |
|
| 52 |
static const char *FunctionNameAttributeName; |
| 53 |
static const char *CFGHashAttributeName; |
| 54 |
static const char *NumCountersAttributeName; |
| 55 |
|
| 56 |
enum InstrProfCorrelatorKind { CK_32Bit, CK_64Bit }; |
| 57 |
InstrProfCorrelatorKind getKind() const { return Kind; } |
| 58 |
virtual ~InstrProfCorrelator() = default; |
| 59 |
|
| 60 |
protected: |
| 61 |
struct Context { |
| 62 |
static llvm::Expected<std::unique_ptr<Context>> |
| 63 |
get(std::unique_ptr<MemoryBuffer> Buffer, const object::ObjectFile &Obj); |
| 64 |
std::unique_ptr<MemoryBuffer> Buffer; |
| 65 |
|
| 66 |
uint64_t CountersSectionStart; |
| 67 |
uint64_t CountersSectionEnd; |
| 68 |
|
| 69 |
bool ShouldSwapBytes; |
| 70 |
}; |
| 71 |
const std::unique_ptr<InstrProfCorrelator::Context> Ctx; |
| 72 |
|
| 73 |
InstrProfCorrelator(InstrProfCorrelatorKind K, std::unique_ptr<Context> Ctx) |
| 74 |
: Ctx(std::move(Ctx)), Kind(K) {} |
| 75 |
|
| 76 |
std::string Names; |
| 77 |
std::vector<std::string> NamesVec; |
| 78 |
|
| 79 |
private: |
| 80 |
static llvm::Expected<std::unique_ptr<InstrProfCorrelator>> |
| 81 |
get(std::unique_ptr<MemoryBuffer> Buffer); |
| 82 |
|
| 83 |
const InstrProfCorrelatorKind Kind; |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
template <class IntPtrT> |
| 89 |
class InstrProfCorrelatorImpl : public InstrProfCorrelator { |
| 90 |
public: |
| 91 |
InstrProfCorrelatorImpl(std::unique_ptr<InstrProfCorrelator::Context> Ctx); |
| 92 |
static bool classof(const InstrProfCorrelator *C); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
const RawInstrProf::ProfileData<IntPtrT> *getDataPointer() const { |
| 97 |
return Data.empty() ? nullptr : Data.data(); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
size_t getDataSize() const { return Data.size(); } |
| 102 |
|
| 103 |
static llvm::Expected<std::unique_ptr<InstrProfCorrelatorImpl<IntPtrT>>> |
| 104 |
get(std::unique_ptr<InstrProfCorrelator::Context> Ctx, |
| 105 |
const object::ObjectFile &Obj); |
| 106 |
|
| 107 |
protected: |
| 108 |
std::vector<RawInstrProf::ProfileData<IntPtrT>> Data; |
| 109 |
|
| 110 |
Error correlateProfileData() override; |
| 111 |
virtual void correlateProfileDataImpl() = 0; |
| 112 |
|
| 113 |
void addProbe(StringRef FunctionName, uint64_t CFGHash, IntPtrT CounterOffset, |
| 114 |
IntPtrT FunctionPtr, uint32_t NumCounters); |
| 115 |
|
| 116 |
private: |
| 117 |
InstrProfCorrelatorImpl(InstrProfCorrelatorKind Kind, |
| 118 |
std::unique_ptr<InstrProfCorrelator::Context> Ctx) |
| 119 |
: InstrProfCorrelator(Kind, std::move(Ctx)){}; |
| 120 |
llvm::DenseSet<IntPtrT> CounterOffsets; |
| 121 |
|
| 122 |
|
| 123 |
template <class T> T maybeSwap(T Value) const { |
| 124 |
return Ctx->ShouldSwapBytes ? sys::getSwappedBytes(Value) : Value; |
| 125 |
} |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
template <class IntPtrT> |
| 131 |
class DwarfInstrProfCorrelator : public InstrProfCorrelatorImpl<IntPtrT> { |
| 132 |
public: |
| 133 |
DwarfInstrProfCorrelator(std::unique_ptr<DWARFContext> DICtx, |
| 134 |
std::unique_ptr<InstrProfCorrelator::Context> Ctx) |
| 135 |
: InstrProfCorrelatorImpl<IntPtrT>(std::move(Ctx)), |
| 136 |
DICtx(std::move(DICtx)) {} |
| 137 |
|
| 138 |
private: |
| 139 |
std::unique_ptr<DWARFContext> DICtx; |
| 140 |
|
| 141 |
|
| 142 |
llvm::Optional<uint64_t> getLocation(const DWARFDie &Die) const; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
static bool isDIEOfProbe(const DWARFDie &Die); |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
void correlateProfileDataImpl() override; |
| 174 |
}; |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
#endif |