LLVM 23.0.0git
PseudoProbePrinter.cpp
Go to the documentation of this file.
1//===- llvm/CodeGen/PseudoProbePrinter.cpp - Pseudo Probe Emission -------===//
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//
9// This file contains support for writing pseudo probe info into asm files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "PseudoProbePrinter.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/PseudoProbe.h"
19#include "llvm/MC/MCStreamer.h"
21
22#ifndef NDEBUG
23#include "llvm/IR/Module.h"
25#endif
26
27using namespace llvm;
28
29#ifndef NDEBUG
30// Deprecated with ThinLTO. For some modules compiled with ThinLTO, certain
31// pseudo probe descriptors may not be imported, resulting in false positive
32// warning.
34 "pseudo-probe-verify-guid-existence-in-desc",
35 cl::desc("Verify whether GUID exists in the .pseudo_probe_desc."),
36 cl::Hidden, cl::init(false));
37#endif
38
41 const DILocation *DebugLoc) {
42 // Gather all the inlined-at nodes.
43 // When it's done ReversedInlineStack looks like ([66, B], [88, A])
44 // which means, Function A inlines function B at calliste with a probe id 88,
45 // and B inlines C at probe 66 where C is represented by Guid.
46 SmallVector<InlineSite, 8> ReversedInlineStack;
47 auto *InlinedAt = DebugLoc ? DebugLoc->getInlinedAt() : nullptr;
48 while (InlinedAt) {
49 auto Name = InlinedAt->getSubprogramLinkageName();
50 // Strip Coroutine suffixes from CoroSplit Pass, since pseudo probes are
51 // generated in an earlier stage.
53 // Use caching to avoid redundant md5 computation for build speed.
54 uint64_t &CallerGuid = NameGuidMap[Name];
55 if (!CallerGuid)
57#ifndef NDEBUG
59 verifyGuidExistenceInDesc(CallerGuid, Name);
60#endif
62 InlinedAt->getDiscriminator());
63 ReversedInlineStack.emplace_back(CallerGuid, CallerProbeId);
64 InlinedAt = InlinedAt->getInlinedAt();
65 }
66 uint64_t Discriminator = 0;
67 // For now only block probes have FS discriminators. See
68 // MIRFSDiscriminator.cpp for more details.
71 Discriminator = DebugLoc->getDiscriminator();
72 assert((EnableFSDiscriminator || Discriminator == 0) &&
73 "Discriminator should not be set in non-FSAFDO mode");
74 SmallVector<InlineSite, 8> InlineStack(llvm::reverse(ReversedInlineStack));
75 Asm->OutStreamer->emitPseudoProbe(Guid, Index, Type, Attr, Discriminator,
76 InlineStack, Asm->CurrentFnSym);
77#ifndef NDEBUG
79 verifyGuidExistenceInDesc(
80 Guid, DebugLoc ? DebugLoc->getSubprogramLinkageName() : "");
81#endif
82}
83
84#ifndef NDEBUG
85void PseudoProbeHandler::verifyGuidExistenceInDesc(uint64_t Guid,
86 StringRef FuncName) {
89 assert(Desc && "pseudo probe does not exist");
90
91 // Keep DescGuidSet up to date.
92 for (size_t I = DescGuidSet.size(), E = Desc->getNumOperands(); I != E; ++I) {
93 const auto *MD = cast<MDNode>(Desc->getOperand(I));
94 auto *ID = mdconst::extract<ConstantInt>(MD->getOperand(0));
95 DescGuidSet.insert(ID->getZExtValue());
96 }
97
98 if (!DescGuidSet.contains(Guid))
99 WithColor::warning() << "Guid:" << Guid << " Name:" << FuncName
100 << " does not exist in pseudo probe desc\n";
101}
102#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition MD5.cpp:57
static cl::opt< bool > VerifyGuidExistence("pseudo-probe-verify-guid-existence-in-desc", cl::desc("Verify whether GUID exists in the .pseudo_probe_desc."), cl::Hidden, cl::init(false))
MachineFunction * MF
The current machine function.
Definition AsmPrinter.h:109
A debug info location.
Definition DebugLoc.h:123
static LLVM_ABI GUID getGUIDAssumingExternalLinkage(StringRef GlobalName)
Return a 64-bit global unique ID constructed from the name of a global symbol.
Definition Globals.cpp:77
Module * getParent()
Get the module that this global value is contained inside of...
Function & getFunction()
Return the LLVM function that this machine code represents.
NamedMDNode * getNamedMetadata(StringRef Name) const
Return the first NamedMDNode in the module with the specified name.
Definition Module.cpp:301
A tuple of MDNodes.
Definition Metadata.h:1757
void emitPseudoProbe(uint64_t Guid, uint64_t Index, uint64_t Type, uint64_t Attr, const DILocation *DebugLoc)
reference emplace_back(ArgTypes &&... Args)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition WithColor.cpp:85
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
size_type size() const
Definition DenseSet.h:87
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
static StringRef getCanonicalCoroFnName(StringRef FnName, StringRef Attr="selected")
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:667
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
LLVM_ABI cl::opt< bool > EnableFSDiscriminator
Op::Description Desc
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
constexpr const char * PseudoProbeDescMetadataName
Definition PseudoProbe.h:26
static uint32_t extractProbeIndex(uint32_t Value)
Definition PseudoProbe.h:75