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