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 doInitialization(Module
&M
) override
{
48 ShouldRun
= M
.getNamedMetadata(PseudoProbeDescMetadataName
);
52 bool runOnMachineFunction(MachineFunction
&MF
) override
{
55 const TargetInstrInfo
*TII
= MF
.getSubtarget().getInstrInfo();
57 for (MachineBasicBlock
&MBB
: MF
) {
58 MachineInstr
*FirstInstr
= nullptr;
59 for (MachineInstr
&MI
: MBB
) {
63 if (DILocation
*DL
= MI
.getDebugLoc()) {
64 auto Value
= DL
->getDiscriminator();
65 if (DILocation::isPseudoProbeDiscriminator(Value
)) {
66 BuildMI(MBB
, MI
, DL
, TII
->get(TargetOpcode::PSEUDO_PROBE
))
67 .addImm(getFuncGUID(MF
.getFunction().getParent(), DL
))
69 PseudoProbeDwarfDiscriminator::extractProbeIndex(Value
))
71 PseudoProbeDwarfDiscriminator::extractProbeType(Value
))
72 .addImm(PseudoProbeDwarfDiscriminator::extractProbeAttributes(
80 // Walk the block backwards, move PSEUDO_PROBE before the first real
81 // instruction to fix out-of-order probes. There is a problem with probes
82 // as the terminator of the block. During the offline counts processing,
83 // the samples collected on the first physical instruction following a
84 // probe will be counted towards the probe. This logically equals to
85 // treating the instruction next to a probe as if it is from the same
86 // block of the probe. This is accurate most of the time unless the
87 // instruction can be reached from multiple flows, which means it actually
88 // starts a new block. Samples collected on such probes may cause
89 // imprecision with the counts inference algorithm. Fortunately, if
90 // there are still other native instructions preceding the probe we can
91 // use them as a place holder to collect samples for the probe.
93 auto MII
= MBB
.rbegin();
94 while (MII
!= MBB
.rend()) {
95 // Skip all pseudo probes followed by a real instruction since they
100 if (Cur
->getOpcode() != TargetOpcode::PSEUDO_PROBE
)
102 // Move the dangling probe before FirstInstr.
103 auto *ProbeInstr
= &*Cur
;
104 MBB
.remove(ProbeInstr
);
105 MBB
.insert(FirstInstr
, ProbeInstr
);
109 // Probes not surrounded by any real instructions in the same block are
110 // called dangling probes. Since there's no good way to pick up a sample
111 // collection point for dangling probes at compile time, they are being
112 // removed so that the profile correlation tool will not report any
113 // samples collected for them and it's up to the counts inference tool
114 // to get them a reasonable count.
115 SmallVector
<MachineInstr
*, 4> ToBeRemoved
;
116 for (MachineInstr
&MI
: MBB
) {
117 if (MI
.isPseudoProbe())
118 ToBeRemoved
.push_back(&MI
);
121 for (auto *MI
: ToBeRemoved
)
122 MI
->eraseFromParent();
124 Changed
|= !ToBeRemoved
.empty();
132 uint64_t getFuncGUID(Module
*M
, DILocation
*DL
) {
133 auto *SP
= DL
->getScope()->getSubprogram();
134 auto Name
= SP
->getLinkageName();
136 Name
= SP
->getName();
137 return Function::getGUID(Name
);
140 bool ShouldRun
= false;
144 char PseudoProbeInserter::ID
= 0;
145 INITIALIZE_PASS_BEGIN(PseudoProbeInserter
, DEBUG_TYPE
,
146 "Insert pseudo probe annotations for value profiling",
148 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig
)
149 INITIALIZE_PASS_END(PseudoProbeInserter
, DEBUG_TYPE
,
150 "Insert pseudo probe annotations for value profiling",
153 FunctionPass
*llvm::createPseudoProbeInserter() {
154 return new PseudoProbeInserter();