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"
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/IR/DebugInfoMetadata.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/PseudoProbe.h"
18#include "llvm/MC/MCPseudoProbe.h"
19#include "llvm/MC/MCStreamer.h"
20#include "llvm/ProfileData/SampleProf.h"
21
22#ifndef NDEBUG
23#include "llvm/IR/Module.h"
24#include "llvm/Support/WithColor.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.
33static cl::opt<bool> VerifyGuidExistence(
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
39void PseudoProbeHandler::emitPseudoProbe(uint64_t Guid, uint64_t Index,
40 uint64_t Type, uint64_t Attr,
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.
52 Name = FunctionSamples::getCanonicalCoroFnName(FnName: Name);
53 // Use caching to avoid redundant md5 computation for build speed.
54 uint64_t &CallerGuid = NameGuidMap[Name];
55 if (!CallerGuid)
56 CallerGuid = Function::getGUIDAssumingExternalLinkage(GlobalName: Name);
57#ifndef NDEBUG
58 if (VerifyGuidExistence)
59 verifyGuidExistenceInDesc(CallerGuid, Name);
60#endif
61 uint64_t CallerProbeId = PseudoProbeDwarfDiscriminator::extractProbeIndex(
62 Value: InlinedAt->getDiscriminator());
63 ReversedInlineStack.emplace_back(Args&: CallerGuid, Args&: 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.
69 if (EnableFSDiscriminator && DebugLoc &&
70 (Type == (uint64_t)PseudoProbeType::Block))
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(C&: ReversedInlineStack));
75 Asm->OutStreamer->emitPseudoProbe(Guid, Index, Type, Attr, Discriminator,
76 InlineStack, FnSym: Asm->CurrentFnSym);
77#ifndef NDEBUG
78 if (VerifyGuidExistence)
79 verifyGuidExistenceInDesc(
80 Guid, DebugLoc ? DebugLoc->getSubprogramLinkageName() : "");
81#endif
82}
83
84#ifndef NDEBUG
85void PseudoProbeHandler::verifyGuidExistenceInDesc(uint64_t Guid,
86 StringRef FuncName) {
87 NamedMDNode *Desc = Asm->MF->getFunction().getParent()->getNamedMetadata(
88 PseudoProbeDescMetadataName);
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
103