LLVM 22.0.0git
PseudoProbeInserter.cpp
Go to the documentation of this file.
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
21#include "llvm/IR/Module.h"
22#include "llvm/IR/PseudoProbe.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) {
37 }
38
39 StringRef getPassName() const override { return "Pseudo Probe Inserter"; }
40
41 void getAnalysisUsage(AnalysisUsage &AU) const override {
42 AU.setPreservesAll();
44 }
45
46 bool doInitialization(Module &M) override {
47 ShouldRun = M.getNamedMetadata(PseudoProbeDescMetadataName);
48 return false;
49 }
50
51 bool runOnMachineFunction(MachineFunction &MF) override {
52 if (!ShouldRun)
53 return false;
55 bool Changed = false;
56 for (MachineBasicBlock &MBB : MF) {
57 MachineInstr *FirstInstr = nullptr;
58 for (MachineInstr &MI : MBB) {
59 if (!MI.isPseudo())
60 FirstInstr = &MI;
61 if (MI.isCall()) {
62 if (DILocation *DL = MI.getDebugLoc()) {
63 auto Value = DL->getDiscriminator();
65 BuildMI(MBB, MI, DL, TII->get(TargetOpcode::PSEUDO_PROBE))
66 .addImm(getFuncGUID(MF.getFunction().getParent(), DL))
67 .addImm(
69 .addImm(
72 Value));
73 Changed = true;
74 }
75 }
76 }
77 }
78
79 // Walk the block backwards, move PSEUDO_PROBE before the first real
80 // instruction to fix out-of-order probes. There is a problem with probes
81 // as the terminator of the block. During the offline counts processing,
82 // the samples collected on the first physical instruction following a
83 // probe will be counted towards the probe. This logically equals to
84 // treating the instruction next to a probe as if it is from the same
85 // block of the probe. This is accurate most of the time unless the
86 // instruction can be reached from multiple flows, which means it actually
87 // starts a new block. Samples collected on such probes may cause
88 // imprecision with the counts inference algorithm. Fortunately, if
89 // there are still other native instructions preceding the probe we can
90 // use them as a place holder to collect samples for the probe.
91 if (FirstInstr) {
92 auto MII = MBB.rbegin();
93 while (MII != MBB.rend()) {
94 // Skip all pseudo probes followed by a real instruction since they
95 // are not dangling.
96 if (!MII->isPseudo())
97 break;
98 auto Cur = MII++;
99 if (Cur->getOpcode() != TargetOpcode::PSEUDO_PROBE)
100 continue;
101 // Move the dangling probe before FirstInstr.
102 auto *ProbeInstr = &*Cur;
103 MBB.remove(ProbeInstr);
104 MBB.insert(FirstInstr, ProbeInstr);
105 Changed = true;
106 }
107 } else {
108 // Probes not surrounded by any real instructions in the same block are
109 // called dangling probes. Since there's no good way to pick up a sample
110 // collection point for dangling probes at compile time, they are being
111 // removed so that the profile correlation tool will not report any
112 // samples collected for them and it's up to the counts inference tool
113 // to get them a reasonable count.
115 for (MachineInstr &MI : MBB) {
116 if (MI.isPseudoProbe())
117 ToBeRemoved.push_back(&MI);
118 }
119
120 for (auto *MI : ToBeRemoved)
121 MI->eraseFromParent();
122
123 Changed |= !ToBeRemoved.empty();
124 }
125 }
126
127 return Changed;
128 }
129
130private:
131 uint64_t getFuncGUID(Module *M, DILocation *DL) {
132 auto Name = DL->getSubprogramLinkageName();
133 // CoroSplit Pass will change the debug info with suffixes i.e. `.resume`,
134 // `.destroy`, `.cleanup`. Strip these suffixes to make the GUID consistent
135 // with the pseudo probe
138 }
139
140 bool ShouldRun = false;
141};
142} // namespace
143
144char PseudoProbeInserter::ID = 0;
146 "Insert pseudo probe annotations for value profiling",
147 false, false)
149INITIALIZE_PASS_END(PseudoProbeInserter, DEBUG_TYPE,
150 "Insert pseudo probe annotations for value profiling",
152
154 return new PseudoProbeInserter();
155}
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
static bool isPseudoProbeDiscriminator(unsigned Discriminator)
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
static LLVM_ABI GUID getGUIDAssumingExternalLinkage(StringRef GlobalName)
Return a 64-bit global unique ID constructed from the name of a global symbol.
Definition Globals.cpp:77
Module * getParent()
Get the module that this global value is contained inside of...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
TargetInstrInfo - Interface to description of machine instruction set.
Target-Independent Code Generator Pass Configuration Options.
virtual const TargetInstrInfo * getInstrInfo() const
LLVM Value Representation.
Definition Value.h:75
static StringRef getCanonicalCoroFnName(StringRef FnName, StringRef Attr="selected")
Changed
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI FunctionPass * createPseudoProbeInserter()
This pass inserts pseudo probe annotation for callsite profiling.
LLVM_ABI void initializePseudoProbeInserterPass(PassRegistry &)
constexpr const char * PseudoProbeDescMetadataName
Definition PseudoProbe.h:26
static uint32_t extractProbeIndex(uint32_t Value)
Definition PseudoProbe.h:75
static uint32_t extractProbeAttributes(uint32_t Value)
Definition PseudoProbe.h:95
static uint32_t extractProbeType(uint32_t Value)
Definition PseudoProbe.h:91