1//===- PseudoProbeInserter.cpp - Insert annotation for callsite profiling -===//
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 implements PseudoProbeInserter pass, which inserts pseudo probe
10// annotations for call instructions with a pseudo-probe-specific dwarf
11// discriminator. such discriminator indicates that the call instruction comes
12// with a pseudo probe, and the discriminator value holds information to
13// identify the corresponding counter.
14//===----------------------------------------------------------------------===//
15
16#include "llvm/CodeGen/MachineBasicBlock.h"
17#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/CodeGen/MachineInstr.h"
19#include "llvm/CodeGen/TargetInstrInfo.h"
20#include "llvm/IR/DebugInfoMetadata.h"
21#include "llvm/IR/Module.h"
22#include "llvm/IR/PseudoProbe.h"
23#include "llvm/InitializePasses.h"
24#include "llvm/ProfileData/SampleProf.h"
25
26#define DEBUG_TYPE "pseudo-probe-inserter"
27
28using namespace llvm;
29
30namespace {
31class PseudoProbeInserter : public MachineFunctionPass {
32public:
33 static char ID;
34
35 PseudoProbeInserter() : MachineFunctionPass(ID) {}
36
37 StringRef getPassName() const override { return "Pseudo Probe Inserter"; }
38
39 void getAnalysisUsage(AnalysisUsage &AU) const override {
40 AU.setPreservesAll();
41 MachineFunctionPass::getAnalysisUsage(AU);
42 }
43
44 bool doInitialization(Module &M) override {
45 ShouldRun = M.getNamedMetadata(Name: PseudoProbeDescMetadataName);
46 return false;
47 }
48
49 bool runOnMachineFunction(MachineFunction &MF) override {
50 if (!ShouldRun)
51 return false;
52 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
53 bool Changed = false;
54 for (MachineBasicBlock &MBB : MF) {
55 MachineInstr *FirstInstr = nullptr;
56 for (MachineInstr &MI : MBB) {
57 if (!MI.isPseudo())
58 FirstInstr = &MI;
59 if (MI.isCall()) {
60 if (DILocation *DL = MI.getDebugLoc()) {
61 auto Value = DL->getDiscriminator();
62 if (DILocation::isPseudoProbeDiscriminator(Discriminator: Value)) {
63 BuildMI(BB&: MBB, I&: MI, MIMD: DL, MCID: TII->get(Opcode: TargetOpcode::PSEUDO_PROBE))
64 .addImm(Val: getFuncGUID(M: MF.getFunction().getParent(), DL))
65 .addImm(
66 Val: PseudoProbeDwarfDiscriminator::extractProbeIndex(Value))
67 .addImm(
68 Val: PseudoProbeDwarfDiscriminator::extractProbeType(Value))
69 .addImm(Val: PseudoProbeDwarfDiscriminator::extractProbeAttributes(
70 Value));
71 Changed = true;
72 }
73 }
74 }
75 }
76
77 // Walk the block backwards, move PSEUDO_PROBE before the first real
78 // instruction to fix out-of-order probes. There is a problem with probes
79 // as the terminator of the block. During the offline counts processing,
80 // the samples collected on the first physical instruction following a
81 // probe will be counted towards the probe. This logically equals to
82 // treating the instruction next to a probe as if it is from the same
83 // block of the probe. This is accurate most of the time unless the
84 // instruction can be reached from multiple flows, which means it actually
85 // starts a new block. Samples collected on such probes may cause
86 // imprecision with the counts inference algorithm. Fortunately, if
87 // there are still other native instructions preceding the probe we can
88 // use them as a place holder to collect samples for the probe.
89 if (FirstInstr) {
90 auto MII = MBB.rbegin();
91 while (MII != MBB.rend()) {
92 // Skip all pseudo probes followed by a real instruction since they
93 // are not dangling.
94 if (!MII->isPseudo())
95 break;
96 auto Cur = MII++;
97 if (Cur->getOpcode() != TargetOpcode::PSEUDO_PROBE)
98 continue;
99 // Move the dangling probe before FirstInstr.
100 auto *ProbeInstr = &*Cur;
101 MBB.remove(I: ProbeInstr);
102 MBB.insert(I: FirstInstr, MI: ProbeInstr);
103 Changed = true;
104 }
105 } else {
106 // Probes not surrounded by any real instructions in the same block are
107 // called dangling probes. Since there's no good way to pick up a sample
108 // collection point for dangling probes at compile time, they are being
109 // removed so that the profile correlation tool will not report any
110 // samples collected for them and it's up to the counts inference tool
111 // to get them a reasonable count.
112 SmallVector<MachineInstr *, 4> ToBeRemoved;
113 for (MachineInstr &MI : MBB) {
114 if (MI.isPseudoProbe())
115 ToBeRemoved.push_back(Elt: &MI);
116 }
117
118 for (auto *MI : ToBeRemoved)
119 MI->eraseFromParent();
120
121 Changed |= !ToBeRemoved.empty();
122 }
123 }
124
125 return Changed;
126 }
127
128private:
129 uint64_t getFuncGUID(Module *M, DILocation *DL) {
130 auto Name = DL->getSubprogramLinkageName();
131 // CoroSplit Pass will change the debug info with suffixes i.e. `.resume`,
132 // `.destroy`, `.cleanup`. Strip these suffixes to make the GUID consistent
133 // with the pseudo probe
134 Name = FunctionSamples::getCanonicalCoroFnName(FnName: Name);
135 return Function::getGUIDAssumingExternalLinkage(GlobalName: Name);
136 }
137
138 bool ShouldRun = false;
139};
140} // namespace
141
142char PseudoProbeInserter::ID = 0;
143INITIALIZE_PASS_BEGIN(PseudoProbeInserter, DEBUG_TYPE,
144 "Insert pseudo probe annotations for value profiling",
145 false, false)
146INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
147INITIALIZE_PASS_END(PseudoProbeInserter, DEBUG_TYPE,
148 "Insert pseudo probe annotations for value profiling",
149 false, false)
150
151FunctionPass *llvm::createPseudoProbeInserter() {
152 return new PseudoProbeInserter();
153}
154