1 //===- PseudoProbeInserter.cpp - Insert annotation for callsite profiling -===//
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
7 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
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/PseudoProbe.h"
22 #include "llvm/InitializePasses.h"
23 #include "llvm/MC/MCPseudoProbe.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include <unordered_set>
27 #define DEBUG_TYPE "pseudo-probe-inserter"
32 class PseudoProbeInserter
: public MachineFunctionPass
{
36 PseudoProbeInserter() : MachineFunctionPass(ID
) {
37 initializePseudoProbeInserterPass(*PassRegistry::getPassRegistry());
40 StringRef
getPassName() const override
{ return "Pseudo Probe Inserter"; }
42 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
44 MachineFunctionPass::getAnalysisUsage(AU
);
47 bool runOnMachineFunction(MachineFunction
&MF
) override
{
48 const TargetInstrInfo
*TII
= MF
.getSubtarget().getInstrInfo();
50 for (MachineBasicBlock
&MBB
: MF
) {
51 MachineInstr
*FirstInstr
= nullptr;
52 for (MachineInstr
&MI
: MBB
) {
56 if (DILocation
*DL
= MI
.getDebugLoc()) {
57 auto Value
= DL
->getDiscriminator();
58 if (DILocation::isPseudoProbeDiscriminator(Value
)) {
59 BuildMI(MBB
, MI
, DL
, TII
->get(TargetOpcode::PSEUDO_PROBE
))
60 .addImm(getFuncGUID(MF
.getFunction().getParent(), DL
))
62 PseudoProbeDwarfDiscriminator::extractProbeIndex(Value
))
64 PseudoProbeDwarfDiscriminator::extractProbeType(Value
))
65 .addImm(PseudoProbeDwarfDiscriminator::extractProbeAttributes(
73 // Walk the block backwards, move PSEUDO_PROBE before the first real
74 // instruction to fix out-of-order probes. There is a problem with probes
75 // as the terminator of the block. During the offline counts processing,
76 // the samples collected on the first physical instruction following a
77 // probe will be counted towards the probe. This logically equals to
78 // treating the instruction next to a probe as if it is from the same
79 // block of the probe. This is accurate most of the time unless the
80 // instruction can be reached from multiple flows, which means it actually
81 // starts a new block. Samples collected on such probes may cause
82 // imprecision with the counts inference algorithm. Fortunately, if
83 // there are still other native instructions preceding the probe we can
84 // use them as a place holder to collect samples for the probe.
86 auto MII
= MBB
.rbegin();
87 while (MII
!= MBB
.rend()) {
88 // Skip all pseudo probes followed by a real instruction since they
93 if (Cur
->getOpcode() != TargetOpcode::PSEUDO_PROBE
)
95 // Move the dangling probe before FirstInstr.
96 auto *ProbeInstr
= &*Cur
;
97 MBB
.remove(ProbeInstr
);
98 MBB
.insert(FirstInstr
, ProbeInstr
);
102 // Probes not surrounded by any real instructions in the same block are
103 // called dangling probes. Since there's no good way to pick up a sample
104 // collection point for dangling probes at compile time, they are being
105 // removed so that the profile correlation tool will not report any
106 // samples collected for them and it's up to the counts inference tool
107 // to get them a reasonable count.
108 SmallVector
<MachineInstr
*, 4> ToBeRemoved
;
109 for (MachineInstr
&MI
: MBB
) {
110 if (MI
.isPseudoProbe())
111 ToBeRemoved
.push_back(&MI
);
114 for (auto *MI
: ToBeRemoved
)
115 MI
->eraseFromParent();
117 Changed
|= !ToBeRemoved
.empty();
125 uint64_t getFuncGUID(Module
*M
, DILocation
*DL
) {
126 auto *SP
= DL
->getScope()->getSubprogram();
127 auto Name
= SP
->getLinkageName();
129 Name
= SP
->getName();
130 return Function::getGUID(Name
);
135 char PseudoProbeInserter::ID
= 0;
136 INITIALIZE_PASS_BEGIN(PseudoProbeInserter
, DEBUG_TYPE
,
137 "Insert pseudo probe annotations for value profiling",
139 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig
)
140 INITIALIZE_PASS_END(PseudoProbeInserter
, DEBUG_TYPE
,
141 "Insert pseudo probe annotations for value profiling",
144 FunctionPass
*llvm::createPseudoProbeInserter() {
145 return new PseudoProbeInserter();