xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1*e8d8bef9SDimitry Andric //===- llvm/CodeGen/PseudoProbePrinter.cpp - Pseudo Probe Emission -------===//
2*e8d8bef9SDimitry Andric //
3*e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e8d8bef9SDimitry Andric //
7*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8*e8d8bef9SDimitry Andric //
9*e8d8bef9SDimitry Andric // This file contains support for writing pseudo probe info into asm files.
10*e8d8bef9SDimitry Andric //
11*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
12*e8d8bef9SDimitry Andric 
13*e8d8bef9SDimitry Andric #include "PseudoProbePrinter.h"
14*e8d8bef9SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
15*e8d8bef9SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
16*e8d8bef9SDimitry Andric #include "llvm/IR/Module.h"
17*e8d8bef9SDimitry Andric #include "llvm/IR/PseudoProbe.h"
18*e8d8bef9SDimitry Andric #include "llvm/MC/MCPseudoProbe.h"
19*e8d8bef9SDimitry Andric #include "llvm/MC/MCStreamer.h"
20*e8d8bef9SDimitry Andric 
21*e8d8bef9SDimitry Andric using namespace llvm;
22*e8d8bef9SDimitry Andric 
23*e8d8bef9SDimitry Andric #define DEBUG_TYPE "pseudoprobe"
24*e8d8bef9SDimitry Andric 
25*e8d8bef9SDimitry Andric PseudoProbeHandler::~PseudoProbeHandler() = default;
26*e8d8bef9SDimitry Andric 
27*e8d8bef9SDimitry Andric PseudoProbeHandler::PseudoProbeHandler(AsmPrinter *A, Module *M) : Asm(A) {
28*e8d8bef9SDimitry Andric   NamedMDNode *FuncInfo = M->getNamedMetadata(PseudoProbeDescMetadataName);
29*e8d8bef9SDimitry Andric   assert(FuncInfo && "Pseudo probe descriptors are missing");
30*e8d8bef9SDimitry Andric   for (const auto *Operand : FuncInfo->operands()) {
31*e8d8bef9SDimitry Andric     const auto *MD = cast<MDNode>(Operand);
32*e8d8bef9SDimitry Andric     auto GUID =
33*e8d8bef9SDimitry Andric         mdconst::dyn_extract<ConstantInt>(MD->getOperand(0))->getZExtValue();
34*e8d8bef9SDimitry Andric     auto Name = cast<MDString>(MD->getOperand(2))->getString();
35*e8d8bef9SDimitry Andric     // We may see pairs with same name but different GUIDs here in LTO mode, due
36*e8d8bef9SDimitry Andric     // to static same-named functions inlined from other modules into this
37*e8d8bef9SDimitry Andric     // module. Function profiles with the same name will be merged no matter
38*e8d8bef9SDimitry Andric     // whether they are collected on the same function. Therefore we just pick
39*e8d8bef9SDimitry Andric     // up the last <Name, GUID> pair here to represent the same-named function
40*e8d8bef9SDimitry Andric     // collection and all probes from the collection will be merged into a
41*e8d8bef9SDimitry Andric     // single profile eventually.
42*e8d8bef9SDimitry Andric     Names[Name] = GUID;
43*e8d8bef9SDimitry Andric   }
44*e8d8bef9SDimitry Andric 
45*e8d8bef9SDimitry Andric   LLVM_DEBUG(dump());
46*e8d8bef9SDimitry Andric }
47*e8d8bef9SDimitry Andric 
48*e8d8bef9SDimitry Andric void PseudoProbeHandler::emitPseudoProbe(uint64_t Guid, uint64_t Index,
49*e8d8bef9SDimitry Andric                                          uint64_t Type, uint64_t Attr,
50*e8d8bef9SDimitry Andric                                          const DILocation *DebugLoc) {
51*e8d8bef9SDimitry Andric   // Gather all the inlined-at nodes.
52*e8d8bef9SDimitry Andric   // When it's done ReversedInlineStack looks like ([66, B], [88, A])
53*e8d8bef9SDimitry Andric   // which means, Function A inlines function B at calliste with a probe id 88,
54*e8d8bef9SDimitry Andric   // and B inlines C at probe 66 where C is represented by Guid.
55*e8d8bef9SDimitry Andric   SmallVector<InlineSite, 8> ReversedInlineStack;
56*e8d8bef9SDimitry Andric   auto *InlinedAt = DebugLoc ? DebugLoc->getInlinedAt() : nullptr;
57*e8d8bef9SDimitry Andric   while (InlinedAt) {
58*e8d8bef9SDimitry Andric     const DISubprogram *SP = InlinedAt->getScope()->getSubprogram();
59*e8d8bef9SDimitry Andric     // Use linkage name for C++ if possible.
60*e8d8bef9SDimitry Andric     auto Name = SP->getLinkageName();
61*e8d8bef9SDimitry Andric     if (Name.empty())
62*e8d8bef9SDimitry Andric       Name = SP->getName();
63*e8d8bef9SDimitry Andric     assert(Names.count(Name) && "Pseudo probe descriptor missing for function");
64*e8d8bef9SDimitry Andric     uint64_t CallerGuid = Names[Name];
65*e8d8bef9SDimitry Andric     uint64_t CallerProbeId = PseudoProbeDwarfDiscriminator::extractProbeIndex(
66*e8d8bef9SDimitry Andric         InlinedAt->getDiscriminator());
67*e8d8bef9SDimitry Andric     ReversedInlineStack.emplace_back(CallerGuid, CallerProbeId);
68*e8d8bef9SDimitry Andric     InlinedAt = InlinedAt->getInlinedAt();
69*e8d8bef9SDimitry Andric   }
70*e8d8bef9SDimitry Andric 
71*e8d8bef9SDimitry Andric   SmallVector<InlineSite, 8> InlineStack(ReversedInlineStack.rbegin(),
72*e8d8bef9SDimitry Andric                                          ReversedInlineStack.rend());
73*e8d8bef9SDimitry Andric   Asm->OutStreamer->emitPseudoProbe(Guid, Index, Type, Attr, InlineStack);
74*e8d8bef9SDimitry Andric }
75*e8d8bef9SDimitry Andric 
76*e8d8bef9SDimitry Andric #ifndef NDEBUG
77*e8d8bef9SDimitry Andric void PseudoProbeHandler::dump() const {
78*e8d8bef9SDimitry Andric   dbgs() << "\n=============================\n";
79*e8d8bef9SDimitry Andric   dbgs() << "\nFunction Name to GUID map:\n";
80*e8d8bef9SDimitry Andric   dbgs() << "\n=============================\n";
81*e8d8bef9SDimitry Andric   for (const auto &Item : Names)
82*e8d8bef9SDimitry Andric     dbgs() << "Func: " << Item.first << "   GUID: " << Item.second << "\n";
83*e8d8bef9SDimitry Andric }
84*e8d8bef9SDimitry Andric #endif
85