|
| 1 | +//===- InstrProfCorrelator.h ------------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// This file defines InstrProfCorrelator used to generate PGO profiles from |
| 9 | +// raw profile data and debug info. |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +#ifndef LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H |
| 13 | +#define LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H |
| 14 | + |
| 15 | +#include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 16 | +#include "llvm/Object/Binary.h" |
| 17 | +#include "llvm/Object/ObjectFile.h" |
| 18 | +#include "llvm/ProfileData/InstrProf.h" |
| 19 | +#include "llvm/Support/Casting.h" |
| 20 | +#include "llvm/Support/Error.h" |
| 21 | +#include "llvm/Support/MemoryBuffer.h" |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +namespace llvm { |
| 25 | + |
| 26 | +/// InstrProfCorrelator - A base class used to create raw instrumentation data |
| 27 | +/// to their functions. |
| 28 | +class InstrProfCorrelator { |
| 29 | +public: |
| 30 | + static llvm::Expected<std::unique_ptr<InstrProfCorrelator>> |
| 31 | + get(StringRef DebugInfoFilename); |
| 32 | + |
| 33 | + /// Construct a ProfileData vector used to correlate raw instrumentation data |
| 34 | + /// to their functions. |
| 35 | + virtual Error correlateProfileData() = 0; |
| 36 | + |
| 37 | + static const char *FunctionNameAttributeName; |
| 38 | + static const char *CFGHashAttributeName; |
| 39 | + static const char *NumCountersAttributeName; |
| 40 | + |
| 41 | + enum InstrProfCorrelatorKind { CK_32Bit, CK_64Bit }; |
| 42 | + InstrProfCorrelatorKind getKind() const { return Kind; } |
| 43 | + virtual ~InstrProfCorrelator() {} |
| 44 | + |
| 45 | +protected: |
| 46 | + struct Context { |
| 47 | + static llvm::Expected<std::unique_ptr<Context>> |
| 48 | + get(std::unique_ptr<MemoryBuffer> Buffer, const object::ObjectFile &Obj); |
| 49 | + std::unique_ptr<MemoryBuffer> Buffer; |
| 50 | + /// The address range of the __llvm_prf_cnts section. |
| 51 | + uint64_t CountersSectionStart; |
| 52 | + uint64_t CountersSectionEnd; |
| 53 | + /// True if target and host have different endian orders. |
| 54 | + bool ShouldSwapBytes; |
| 55 | + }; |
| 56 | + const std::unique_ptr<InstrProfCorrelator::Context> Ctx; |
| 57 | + |
| 58 | + InstrProfCorrelator(InstrProfCorrelatorKind K, std::unique_ptr<Context> Ctx) |
| 59 | + : Ctx(std::move(Ctx)), Kind(K) {} |
| 60 | + |
| 61 | +private: |
| 62 | + static llvm::Expected<std::unique_ptr<InstrProfCorrelator>> |
| 63 | + get(std::unique_ptr<MemoryBuffer> Buffer); |
| 64 | + |
| 65 | + const InstrProfCorrelatorKind Kind; |
| 66 | +}; |
| 67 | + |
| 68 | +/// InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template |
| 69 | +/// pointer type so that the ProfileData vector can be materialized. |
| 70 | +template <class IntPtrT> |
| 71 | +class InstrProfCorrelatorImpl : public InstrProfCorrelator { |
| 72 | +public: |
| 73 | + InstrProfCorrelatorImpl(std::unique_ptr<InstrProfCorrelator::Context> Ctx); |
| 74 | + static bool classof(const InstrProfCorrelator *C); |
| 75 | + |
| 76 | + /// Return a pointer to the underlying ProfileData vector that this class |
| 77 | + /// constructs. |
| 78 | + const RawInstrProf::ProfileData<IntPtrT> *getDataPointer() const { |
| 79 | + return Data.empty() ? nullptr : Data.data(); |
| 80 | + } |
| 81 | + |
| 82 | + /// Return the number of ProfileData elements. |
| 83 | + size_t getDataSize() const { return Data.size(); } |
| 84 | + |
| 85 | + /// Return a pointer to the compressed names string that this class |
| 86 | + /// constructs. |
| 87 | + const char *getCompressedNamesPointer() const { |
| 88 | + return CompressedNames.c_str(); |
| 89 | + } |
| 90 | + |
| 91 | + /// Return the number of bytes in the compressed names string. |
| 92 | + size_t getCompressedNamesSize() const { return CompressedNames.size(); } |
| 93 | + |
| 94 | + static llvm::Expected<std::unique_ptr<InstrProfCorrelatorImpl<IntPtrT>>> |
| 95 | + get(std::unique_ptr<InstrProfCorrelator::Context> Ctx, |
| 96 | + const object::ObjectFile &Obj); |
| 97 | + |
| 98 | +protected: |
| 99 | + std::vector<RawInstrProf::ProfileData<IntPtrT>> Data; |
| 100 | + std::string CompressedNames; |
| 101 | + |
| 102 | + Error correlateProfileData() override; |
| 103 | + virtual void correlateProfileDataImpl() = 0; |
| 104 | + |
| 105 | + void addProbe(StringRef FunctionName, uint64_t CFGHash, IntPtrT CounterOffset, |
| 106 | + IntPtrT FunctionPtr, uint32_t NumCounters); |
| 107 | + |
| 108 | +private: |
| 109 | + InstrProfCorrelatorImpl(InstrProfCorrelatorKind Kind, |
| 110 | + std::unique_ptr<InstrProfCorrelator::Context> Ctx) |
| 111 | + : InstrProfCorrelator(Kind, std::move(Ctx)){}; |
| 112 | + std::vector<std::string> Names; |
| 113 | + |
| 114 | + // Byte-swap the value if necessary. |
| 115 | + template <class T> T maybeSwap(T Value) const { |
| 116 | + return Ctx->ShouldSwapBytes ? sys::getSwappedBytes(Value) : Value; |
| 117 | + } |
| 118 | +}; |
| 119 | + |
| 120 | +/// DwarfInstrProfCorrelator - A child of InstrProfCorrelatorImpl that takes |
| 121 | +/// DWARF debug info as input to correlate profiles. |
| 122 | +template <class IntPtrT> |
| 123 | +class DwarfInstrProfCorrelator : public InstrProfCorrelatorImpl<IntPtrT> { |
| 124 | +public: |
| 125 | + DwarfInstrProfCorrelator(std::unique_ptr<DWARFContext> DICtx, |
| 126 | + std::unique_ptr<InstrProfCorrelator::Context> Ctx) |
| 127 | + : InstrProfCorrelatorImpl<IntPtrT>(std::move(Ctx)), |
| 128 | + DICtx(std::move(DICtx)) {} |
| 129 | + |
| 130 | +private: |
| 131 | + std::unique_ptr<DWARFContext> DICtx; |
| 132 | + |
| 133 | + /// Return the address of the object that the provided DIE symbolizes. |
| 134 | + llvm::Optional<uint64_t> getLocation(const DWARFDie &Die) const; |
| 135 | + |
| 136 | + /// Returns true if the provided DIE symbolizes an instrumentation probe |
| 137 | + /// symbol. |
| 138 | + static bool isDIEOfProbe(const DWARFDie &Die); |
| 139 | + |
| 140 | + /// Iterate over DWARF DIEs to find those that symbolize instrumentation |
| 141 | + /// probes and construct the ProfileData vector and CompressedNames string. |
| 142 | + /// |
| 143 | + /// Here is some example DWARF for an instrumentation probe we are looking |
| 144 | + /// for: |
| 145 | + /// \code |
| 146 | + /// DW_TAG_subprogram |
| 147 | + /// DW_AT_low_pc (0x0000000000000000) |
| 148 | + /// DW_AT_high_pc (0x0000000000000014) |
| 149 | + /// DW_AT_name ("foo") |
| 150 | + /// DW_TAG_variable |
| 151 | + /// DW_AT_name ("__profc_foo") |
| 152 | + /// DW_AT_location (DW_OP_addr 0x0) |
| 153 | + /// DW_TAG_LLVM_annotation |
| 154 | + /// DW_AT_name ("Function Name") |
| 155 | + /// DW_AT_const_value ("foo") |
| 156 | + /// DW_TAG_LLVM_annotation |
| 157 | + /// DW_AT_name ("CFG Hash") |
| 158 | + /// DW_AT_const_value (12345678) |
| 159 | + /// DW_TAG_LLVM_annotation |
| 160 | + /// DW_AT_name ("Num Counters") |
| 161 | + /// DW_AT_const_value (2) |
| 162 | + /// NULL |
| 163 | + /// NULL |
| 164 | + /// \endcode |
| 165 | + void correlateProfileDataImpl() override; |
| 166 | +}; |
| 167 | + |
| 168 | +} // end namespace llvm |
| 169 | + |
| 170 | +#endif // LLVM_PROFILEDATA_INSTRPROFCORRELATOR_H |
0 commit comments