LLVM  15.0.0git
InstrProfCorrelator.cpp
Go to the documentation of this file.
1 //===-- InstrProfCorrelator.cpp -------------------------------------------===//
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 
17 #include "llvm/Object/MachO.h"
18 #include "llvm/Support/Debug.h"
19 
20 #define DEBUG_TYPE "correlator"
21 
22 using namespace llvm;
23 
24 /// Get the __llvm_prf_cnts section.
26  for (auto &Section : Obj.sections())
27  if (auto SectionName = Section.getName())
28  if (SectionName.get() == INSTR_PROF_CNTS_SECT_NAME)
29  return Section;
30  return make_error<InstrProfError>(
32  "could not find counter section (" INSTR_PROF_CNTS_SECT_NAME ")");
33 }
34 
35 const char *InstrProfCorrelator::FunctionNameAttributeName = "Function Name";
36 const char *InstrProfCorrelator::CFGHashAttributeName = "CFG Hash";
37 const char *InstrProfCorrelator::NumCountersAttributeName = "Num Counters";
38 
40 InstrProfCorrelator::Context::get(std::unique_ptr<MemoryBuffer> Buffer,
41  const object::ObjectFile &Obj) {
42  auto CountersSection = getCountersSection(Obj);
43  if (auto Err = CountersSection.takeError())
44  return std::move(Err);
45  auto C = std::make_unique<Context>();
46  C->Buffer = std::move(Buffer);
47  C->CountersSectionStart = CountersSection->getAddress();
48  C->CountersSectionEnd = C->CountersSectionStart + CountersSection->getSize();
49  C->ShouldSwapBytes = Obj.isLittleEndian() != sys::IsLittleEndianHost;
51 }
52 
54 InstrProfCorrelator::get(StringRef DebugInfoFilename) {
55  auto DsymObjectsOrErr =
57  if (auto Err = DsymObjectsOrErr.takeError())
58  return std::move(Err);
59  if (!DsymObjectsOrErr->empty()) {
60  // TODO: Enable profile correlation when there are multiple objects in a
61  // dSYM bundle.
62  if (DsymObjectsOrErr->size() > 1)
63  return make_error<InstrProfError>(
65  "using multiple objects is not yet supported");
66  DebugInfoFilename = *DsymObjectsOrErr->begin();
67  }
68  auto BufferOrErr =
69  errorOrToExpected(MemoryBuffer::getFile(DebugInfoFilename));
70  if (auto Err = BufferOrErr.takeError())
71  return std::move(Err);
72 
73  return get(std::move(*BufferOrErr));
74 }
75 
77 InstrProfCorrelator::get(std::unique_ptr<MemoryBuffer> Buffer) {
78  auto BinOrErr = object::createBinary(*Buffer);
79  if (auto Err = BinOrErr.takeError())
80  return std::move(Err);
81 
82  if (auto *Obj = dyn_cast<object::ObjectFile>(BinOrErr->get())) {
83  auto CtxOrErr = Context::get(std::move(Buffer), *Obj);
84  if (auto Err = CtxOrErr.takeError())
85  return std::move(Err);
86  auto T = Obj->makeTriple();
87  if (T.isArch64Bit())
88  return InstrProfCorrelatorImpl<uint64_t>::get(std::move(*CtxOrErr), *Obj);
89  if (T.isArch32Bit())
90  return InstrProfCorrelatorImpl<uint32_t>::get(std::move(*CtxOrErr), *Obj);
91  }
92  return make_error<InstrProfError>(
93  instrprof_error::unable_to_correlate_profile, "not an object file");
94 }
95 
97  if (auto *C = dyn_cast<InstrProfCorrelatorImpl<uint32_t>>(this)) {
98  return C->getDataSize();
99  } else if (auto *C = dyn_cast<InstrProfCorrelatorImpl<uint64_t>>(this)) {
100  return C->getDataSize();
101  }
102  return {};
103 }
104 
105 namespace llvm {
106 
107 template <>
109  std::unique_ptr<InstrProfCorrelator::Context> Ctx)
111  std::move(Ctx)) {}
112 template <>
114  std::unique_ptr<InstrProfCorrelator::Context> Ctx)
116  std::move(Ctx)) {}
117 template <>
119  return C->getKind() == InstrProfCorrelatorKind::CK_32Bit;
120 }
121 template <>
123  return C->getKind() == InstrProfCorrelatorKind::CK_64Bit;
124 }
125 
126 } // end namespace llvm
127 
128 template <class IntPtrT>
131  std::unique_ptr<InstrProfCorrelator::Context> Ctx,
132  const object::ObjectFile &Obj) {
133  if (Obj.isELF() || Obj.isMachO()) {
134  auto DICtx = DWARFContext::create(Obj);
135  return std::make_unique<DwarfInstrProfCorrelator<IntPtrT>>(std::move(DICtx),
136  std::move(Ctx));
137  }
138  return make_error<InstrProfError>(
139  instrprof_error::unable_to_correlate_profile,
140  "unsupported debug info format (only DWARF is supported)");
141 }
142 
143 template <class IntPtrT>
145  assert(Data.empty() && Names.empty() && NamesVec.empty());
146  correlateProfileDataImpl();
147  if (Data.empty() || NamesVec.empty())
148  return make_error<InstrProfError>(
149  instrprof_error::unable_to_correlate_profile,
150  "could not find any profile metadata in debug info");
151  auto Result =
152  collectPGOFuncNameStrings(NamesVec, /*doCompression=*/false, Names);
153  CounterOffsets.clear();
154  NamesVec.clear();
155  return Result;
156 }
157 
158 template <class IntPtrT>
160  uint64_t CFGHash,
161  IntPtrT CounterOffset,
162  IntPtrT FunctionPtr,
163  uint32_t NumCounters) {
164  // Check if a probe was already added for this counter offset.
165  if (!CounterOffsets.insert(CounterOffset).second)
166  return;
167  Data.push_back({
168  maybeSwap<uint64_t>(IndexedInstrProf::ComputeHash(FunctionName)),
169  maybeSwap<uint64_t>(CFGHash),
170  // In this mode, CounterPtr actually stores the section relative address
171  // of the counter.
172  maybeSwap<IntPtrT>(CounterOffset),
173  maybeSwap<IntPtrT>(FunctionPtr),
174  // TODO: Value profiling is not yet supported.
175  /*ValuesPtr=*/maybeSwap<IntPtrT>(0),
176  maybeSwap<uint32_t>(NumCounters),
177  /*NumValueSites=*/{maybeSwap<uint16_t>(0), maybeSwap<uint16_t>(0)},
178  });
179  NamesVec.push_back(FunctionName.str());
180 }
181 
182 template <class IntPtrT>
185  auto Locations = Die.getLocations(dwarf::DW_AT_location);
186  if (!Locations) {
187  consumeError(Locations.takeError());
188  return {};
189  }
190  auto &DU = *Die.getDwarfUnit();
191  auto AddressSize = DU.getAddressByteSize();
192  for (auto &Location : *Locations) {
193  DataExtractor Data(Location.Expr, DICtx->isLittleEndian(), AddressSize);
194  DWARFExpression Expr(Data, AddressSize);
195  for (auto &Op : Expr) {
196  if (Op.getCode() == dwarf::DW_OP_addr) {
197  return Op.getRawOperand(0);
198  } else if (Op.getCode() == dwarf::DW_OP_addrx) {
199  uint64_t Index = Op.getRawOperand(0);
200  if (auto SA = DU.getAddrOffsetSectionItem(Index))
201  return SA->Address;
202  }
203  }
204  }
205  return {};
206 }
207 
208 template <class IntPtrT>
210  const auto &ParentDie = Die.getParent();
211  if (!Die.isValid() || !ParentDie.isValid() || Die.isNULL())
212  return false;
213  if (Die.getTag() != dwarf::DW_TAG_variable)
214  return false;
215  if (!ParentDie.isSubprogramDIE())
216  return false;
217  if (!Die.hasChildren())
218  return false;
219  if (const char *Name = Die.getName(DINameKind::ShortName))
221  return false;
222 }
223 
224 template <class IntPtrT>
226  auto maybeAddProbe = [&](DWARFDie Die) {
227  if (!isDIEOfProbe(Die))
228  return;
229  Optional<const char *> FunctionName;
230  Optional<uint64_t> CFGHash;
231  Optional<uint64_t> CounterPtr = getLocation(Die);
232  auto FunctionPtr =
233  dwarf::toAddress(Die.getParent().find(dwarf::DW_AT_low_pc));
234  Optional<uint64_t> NumCounters;
235  for (const DWARFDie &Child : Die.children()) {
236  if (Child.getTag() != dwarf::DW_TAG_LLVM_annotation)
237  continue;
238  auto AnnotationFormName = Child.find(dwarf::DW_AT_name);
239  auto AnnotationFormValue = Child.find(dwarf::DW_AT_const_value);
240  if (!AnnotationFormName || !AnnotationFormValue)
241  continue;
242  auto AnnotationNameOrErr = AnnotationFormName->getAsCString();
243  if (auto Err = AnnotationNameOrErr.takeError()) {
244  consumeError(std::move(Err));
245  continue;
246  }
247  StringRef AnnotationName = *AnnotationNameOrErr;
248  if (AnnotationName.compare(
249  InstrProfCorrelator::FunctionNameAttributeName) == 0) {
250  if (auto EC =
251  AnnotationFormValue->getAsCString().moveInto(FunctionName))
252  consumeError(std::move(EC));
253  } else if (AnnotationName.compare(
254  InstrProfCorrelator::CFGHashAttributeName) == 0) {
255  CFGHash = AnnotationFormValue->getAsUnsignedConstant();
256  } else if (AnnotationName.compare(
257  InstrProfCorrelator::NumCountersAttributeName) == 0) {
258  NumCounters = AnnotationFormValue->getAsUnsignedConstant();
259  }
260  }
261  if (!FunctionName || !CFGHash || !CounterPtr || !NumCounters) {
262  LLVM_DEBUG(dbgs() << "Incomplete DIE for probe\n\tFunctionName: "
263  << FunctionName << "\n\tCFGHash: " << CFGHash
264  << "\n\tCounterPtr: " << CounterPtr
265  << "\n\tNumCounters: " << NumCounters);
266  LLVM_DEBUG(Die.dump(dbgs()));
267  return;
268  }
269  uint64_t CountersStart = this->Ctx->CountersSectionStart;
270  uint64_t CountersEnd = this->Ctx->CountersSectionEnd;
271  if (*CounterPtr < CountersStart || *CounterPtr >= CountersEnd) {
272  LLVM_DEBUG(
273  dbgs() << "CounterPtr out of range for probe\n\tFunction Name: "
274  << FunctionName << "\n\tExpected: [0x"
275  << Twine::utohexstr(CountersStart) << ", 0x"
276  << Twine::utohexstr(CountersEnd) << ")\n\tActual: 0x"
277  << Twine::utohexstr(*CounterPtr));
278  LLVM_DEBUG(Die.dump(dbgs()));
279  return;
280  }
281  if (!FunctionPtr) {
282  LLVM_DEBUG(dbgs() << "Could not find address of " << *FunctionName
283  << "\n");
284  LLVM_DEBUG(Die.dump(dbgs()));
285  }
286  this->addProbe(*FunctionName, *CFGHash, *CounterPtr - CountersStart,
287  FunctionPtr.getValueOr(0), *NumCounters);
288  };
289  for (auto &CU : DICtx->normal_units())
290  for (const auto &Entry : CU->dies())
291  maybeAddProbe(DWARFDie(CU.get(), &Entry));
292  for (auto &CU : DICtx->dwo_units())
293  for (const auto &Entry : CU->dies())
294  maybeAddProbe(DWARFDie(CU.get(), &Entry));
295 }
DWARFFormValue.h
llvm::StringRef::startswith
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:286
llvm::DWARFDie::isValid
bool isValid() const
Definition: DWARFDie.h:51
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::InstrProfCorrelator::NumCountersAttributeName
static const char * NumCountersAttributeName
Definition: InstrProfCorrelator.h:55
T
llvm::object::MachOObjectFile::findDsymObjectMembers
static Expected< std::vector< std::string > > findDsymObjectMembers(StringRef Path)
If the input path is a .dSYM bundle (as created by the dsymutil tool), return the paths to the object...
Definition: MachOObjectFile.cpp:4923
llvm::object::Binary::isLittleEndian
bool isLittleEndian() const
Definition: Binary.h:148
llvm::InstrProfCorrelator::getDataSize
llvm::Optional< size_t > getDataSize() const
Return the number of ProfileData elements.
Definition: InstrProfCorrelator.cpp:96
llvm::IndexedInstrProf::ComputeHash
uint64_t ComputeHash(StringRef K)
Definition: InstrProf.h:1031
DWARFContext.h
llvm::InstrProfCorrelator::CFGHashAttributeName
static const char * CFGHashAttributeName
Definition: InstrProfCorrelator.h:54
llvm::Optional< size_t >
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::DWARFDie::isNULL
bool isNULL() const
Returns true for a valid DIE that terminates a sibling chain.
Definition: DWARFDie.h:85
llvm::DWARFDie::children
iterator_range< iterator > children() const
Definition: DWARFDie.h:389
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1043
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::dwarf::toAddress
Optional< uint64_t > toAddress(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an address.
Definition: DWARFFormValue.h:290
MachO.h
llvm::InstrProfCorrelatorImpl
InstrProfCorrelatorImpl - A child of InstrProfCorrelator with a template pointer type so that the Pro...
Definition: InstrProfCorrelator.h:90
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::DwarfInstrProfCorrelator
DwarfInstrProfCorrelator - A child of InstrProfCorrelatorImpl that takes DWARF debug info as input to...
Definition: InstrProfCorrelator.h:132
llvm::MemoryBuffer::getFile
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
Definition: MemoryBuffer.cpp:239
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::InstrProfCorrelator::CK_32Bit
@ CK_32Bit
Definition: InstrProfCorrelator.h:57
llvm::InstrProfCorrelator::FunctionNameAttributeName
static const char * FunctionNameAttributeName
Definition: InstrProfCorrelator.h:53
llvm::InstrProfCorrelator::Ctx
const std::unique_ptr< InstrProfCorrelator::Context > Ctx
Definition: InstrProfCorrelator.h:72
llvm::getInstrProfCountersVarPrefix
StringRef getInstrProfCountersVarPrefix()
Return the name prefix of profile counter variables.
Definition: InstrProf.h:91
DWARFDie.h
llvm::StringRef::str
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:245
llvm::DWARFDie::dump
void dump(raw_ostream &OS, unsigned indent=0, DIDumpOptions DumpOpts=DIDumpOptions()) const
Dump the DIE and all of its attributes to the supplied stream.
Definition: DWARFDie.cpp:1114
llvm::InstrProfCorrelatorImpl::InstrProfCorrelatorImpl
InstrProfCorrelatorImpl(std::unique_ptr< InstrProfCorrelator::Context > Ctx)
llvm::InstrProfCorrelator::get
static llvm::Expected< std::unique_ptr< InstrProfCorrelator > > get(StringRef DebugInfoFilename)
Definition: InstrProfCorrelator.cpp:54
Index
uint32_t Index
Definition: ELFObjHandler.cpp:82
uint64_t
llvm::InstrProfCorrelator::InstrProfCorrelatorKind
InstrProfCorrelatorKind
Definition: InstrProfCorrelator.h:57
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
DWARFUnit.h
llvm::DWARFDie::find
Optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
Definition: DWARFDie.cpp:859
llvm::DWARFExpression
Definition: DWARFExpression.h:24
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1665
llvm::object::Binary::isELF
bool isELF() const
Definition: Binary.h:120
llvm::collectPGOFuncNameStrings
Error collectPGOFuncNameStrings(ArrayRef< std::string > NameStrs, bool doCompression, std::string &Result)
Given a vector of strings (function PGO names) NameStrs, the method generates a combined string Resul...
Definition: InstrProf.cpp:440
llvm::InstrProfCorrelatorImpl::classof
static bool classof(const InstrProfCorrelator *C)
getCountersSection
Expected< object::SectionRef > getCountersSection(const object::ObjectFile &Obj)
Get the __llvm_prf_cnts section.
Definition: InstrProfCorrelator.cpp:25
llvm::DWARFDie::getLocations
Expected< DWARFLocationExpressionsVector > getLocations(dwarf::Attribute Attr) const
Definition: DWARFDie.cpp:1018
llvm::InstrProfCorrelator::Context::get
static llvm::Expected< std::unique_ptr< Context > > get(std::unique_ptr< MemoryBuffer > Buffer, const object::ObjectFile &Obj)
Definition: InstrProfCorrelator.cpp:40
llvm::InstrProfCorrelator::Context::Buffer
std::unique_ptr< MemoryBuffer > Buffer
Definition: InstrProfCorrelator.h:65
llvm::DWARFDie::hasChildren
bool hasChildren() const
Definition: DWARFDie.h:79
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:228
llvm::DWARFDie::getDwarfUnit
DWARFUnit * getDwarfUnit() const
Definition: DWARFDie.h:54
uint32_t
llvm::DWARFDie::getName
const char * getName(DINameKind Kind) const
Return the DIE name resolving DW_AT_specification or DW_AT_abstract_origin references if necessary.
Definition: DWARFDie.cpp:1054
llvm::InstrProfCorrelatorImpl::get
static llvm::Expected< std::unique_ptr< InstrProfCorrelatorImpl< IntPtrT > > > get(std::unique_ptr< InstrProfCorrelator::Context > Ctx, const object::ObjectFile &Obj)
Definition: InstrProfCorrelator.cpp:130
DWARFLocationExpression.h
llvm::DWARFDie::getParent
DWARFDie getParent() const
Get the parent of this DIE object.
Definition: DWARFDie.cpp:1174
DIContext.h
llvm::DWARFUnit::getAddressByteSize
uint8_t getAddressByteSize() const
Definition: DWARFUnit.h:288
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
std
Definition: BitVector.h:851
get
Should compile to something r4 addze r3 instead we get
Definition: README.txt:24
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:341
llvm::SectionName
Definition: DWARFSection.h:21
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
DWARFExpression.h
llvm::object::Binary::isMachO
bool isMachO() const
Definition: Binary.h:124
llvm::errorOrToExpected
Expected< T > errorOrToExpected(ErrorOr< T > &&EO)
Convert an ErrorOr<T> to an Expected<T>.
Definition: Error.h:1179
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::DWARFDie::getTag
dwarf::Tag getTag() const
Definition: DWARFDie.h:72
llvm::sys::IsLittleEndianHost
static const bool IsLittleEndianHost
Definition: SwapByteOrder.h:101
llvm::SPIRV::Decoration::Location
@ Location
llvm::instrprof_error::unable_to_correlate_profile
@ unable_to_correlate_profile
llvm::object::ObjectFile::sections
section_iterator_range sections() const
Definition: ObjectFile.h:327
llvm::object::createBinary
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:44
llvm::InstrProfCorrelator
InstrProfCorrelator - A base class used to create raw instrumentation data to their functions.
Definition: InstrProfCorrelator.h:30
InstrProfCorrelator.h
getLocation
static MemoryLocation getLocation(Instruction *I)
Definition: SLPVectorizer.cpp:655
llvm::DWARFDie
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition: DWARFDie.h:43
llvm::StringRef::begin
iterator begin() const
Definition: StringRef.h:128
CU
Definition: AArch64AsmBackend.cpp:505
llvm::StringRef::compare
LLVM_NODISCARD int compare(StringRef RHS) const
compare - Compare two strings; the result is -1, 0, or 1 if this string is lexicographically less tha...
Definition: StringRef.h:201
Debug.h
llvm::dyn_cast
decltype(auto) LLVM_NODISCARD dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition: Casting.h:600