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"
1681ad6265SDimitry Andric #include "llvm/IR/Function.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
emitPseudoProbe(uint64_t Guid,uint64_t Index,uint64_t Type,uint64_t Attr,const DILocation * DebugLoc)23e8d8bef9SDimitry Andric void PseudoProbeHandler::emitPseudoProbe(uint64_t Guid, uint64_t Index,
24e8d8bef9SDimitry Andric uint64_t Type, uint64_t Attr,
25e8d8bef9SDimitry Andric const DILocation *DebugLoc) {
26e8d8bef9SDimitry Andric // Gather all the inlined-at nodes.
27e8d8bef9SDimitry Andric // When it's done ReversedInlineStack looks like ([66, B], [88, A])
28e8d8bef9SDimitry Andric // which means, Function A inlines function B at calliste with a probe id 88,
29e8d8bef9SDimitry Andric // and B inlines C at probe 66 where C is represented by Guid.
30e8d8bef9SDimitry Andric SmallVector<InlineSite, 8> ReversedInlineStack;
31e8d8bef9SDimitry Andric auto *InlinedAt = DebugLoc ? DebugLoc->getInlinedAt() : nullptr;
32e8d8bef9SDimitry Andric while (InlinedAt) {
33*06c3fb27SDimitry Andric auto Name = InlinedAt->getSubprogramLinkageName();
34349cc55cSDimitry Andric // Use caching to avoid redundant md5 computation for build speed.
35349cc55cSDimitry Andric uint64_t &CallerGuid = NameGuidMap[Name];
36349cc55cSDimitry Andric if (!CallerGuid)
37349cc55cSDimitry Andric CallerGuid = Function::getGUID(Name);
38e8d8bef9SDimitry Andric uint64_t CallerProbeId = PseudoProbeDwarfDiscriminator::extractProbeIndex(
39e8d8bef9SDimitry Andric InlinedAt->getDiscriminator());
40e8d8bef9SDimitry Andric ReversedInlineStack.emplace_back(CallerGuid, CallerProbeId);
41e8d8bef9SDimitry Andric InlinedAt = InlinedAt->getInlinedAt();
42e8d8bef9SDimitry Andric }
43*06c3fb27SDimitry Andric uint64_t Discriminator = 0;
44*06c3fb27SDimitry Andric // For now only block probes have FS discriminators. See
45*06c3fb27SDimitry Andric // MIRFSDiscriminator.cpp for more details.
46*06c3fb27SDimitry Andric if (EnableFSDiscriminator && DebugLoc &&
47*06c3fb27SDimitry Andric (Type == (uint64_t)PseudoProbeType::Block))
48*06c3fb27SDimitry Andric Discriminator = DebugLoc->getDiscriminator();
49*06c3fb27SDimitry Andric assert((EnableFSDiscriminator || Discriminator == 0) &&
50*06c3fb27SDimitry Andric "Discriminator should not be set in non-FSAFDO mode");
510eae32dcSDimitry Andric SmallVector<InlineSite, 8> InlineStack(llvm::reverse(ReversedInlineStack));
52*06c3fb27SDimitry Andric Asm->OutStreamer->emitPseudoProbe(Guid, Index, Type, Attr, Discriminator,
53*06c3fb27SDimitry Andric InlineStack, Asm->CurrentFnSym);
54e8d8bef9SDimitry Andric }
55