xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
1e8d8bef9SDimitry Andric //===- llvm/CodeGen/PseudoProbePrinter.cpp - Pseudo Probe Emission -------===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric //
9e8d8bef9SDimitry Andric // This file contains support for writing pseudo probe info into asm files.
10e8d8bef9SDimitry Andric //
11e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
12e8d8bef9SDimitry Andric 
13e8d8bef9SDimitry Andric #include "PseudoProbePrinter.h"
14e8d8bef9SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
15e8d8bef9SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
16e8d8bef9SDimitry Andric #include "llvm/IR/Module.h"
17e8d8bef9SDimitry Andric #include "llvm/IR/PseudoProbe.h"
18e8d8bef9SDimitry Andric #include "llvm/MC/MCPseudoProbe.h"
19e8d8bef9SDimitry Andric #include "llvm/MC/MCStreamer.h"
20e8d8bef9SDimitry Andric 
21e8d8bef9SDimitry Andric using namespace llvm;
22e8d8bef9SDimitry Andric 
23*349cc55cSDimitry Andric PseudoProbeHandler::~PseudoProbeHandler() = default;
24*349cc55cSDimitry Andric 
25e8d8bef9SDimitry Andric void PseudoProbeHandler::emitPseudoProbe(uint64_t Guid, uint64_t Index,
26e8d8bef9SDimitry Andric                                          uint64_t Type, uint64_t Attr,
27e8d8bef9SDimitry Andric                                          const DILocation *DebugLoc) {
28e8d8bef9SDimitry Andric   // Gather all the inlined-at nodes.
29e8d8bef9SDimitry Andric   // When it's done ReversedInlineStack looks like ([66, B], [88, A])
30e8d8bef9SDimitry Andric   // which means, Function A inlines function B at calliste with a probe id 88,
31e8d8bef9SDimitry Andric   // and B inlines C at probe 66 where C is represented by Guid.
32e8d8bef9SDimitry Andric   SmallVector<InlineSite, 8> ReversedInlineStack;
33e8d8bef9SDimitry Andric   auto *InlinedAt = DebugLoc ? DebugLoc->getInlinedAt() : nullptr;
34e8d8bef9SDimitry Andric   while (InlinedAt) {
35e8d8bef9SDimitry Andric     const DISubprogram *SP = InlinedAt->getScope()->getSubprogram();
36e8d8bef9SDimitry Andric     // Use linkage name for C++ if possible.
37e8d8bef9SDimitry Andric     auto Name = SP->getLinkageName();
38e8d8bef9SDimitry Andric     if (Name.empty())
39e8d8bef9SDimitry Andric       Name = SP->getName();
40*349cc55cSDimitry Andric     // Use caching to avoid redundant md5 computation for build speed.
41*349cc55cSDimitry Andric     uint64_t &CallerGuid = NameGuidMap[Name];
42*349cc55cSDimitry Andric     if (!CallerGuid)
43*349cc55cSDimitry Andric       CallerGuid = Function::getGUID(Name);
44e8d8bef9SDimitry Andric     uint64_t CallerProbeId = PseudoProbeDwarfDiscriminator::extractProbeIndex(
45e8d8bef9SDimitry Andric         InlinedAt->getDiscriminator());
46e8d8bef9SDimitry Andric     ReversedInlineStack.emplace_back(CallerGuid, CallerProbeId);
47e8d8bef9SDimitry Andric     InlinedAt = InlinedAt->getInlinedAt();
48e8d8bef9SDimitry Andric   }
49e8d8bef9SDimitry Andric 
50e8d8bef9SDimitry Andric   SmallVector<InlineSite, 8> InlineStack(ReversedInlineStack.rbegin(),
51e8d8bef9SDimitry Andric                                          ReversedInlineStack.rend());
52e8d8bef9SDimitry Andric   Asm->OutStreamer->emitPseudoProbe(Guid, Index, Type, Attr, InlineStack);
53e8d8bef9SDimitry Andric }
54