LLVM 24.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
23#include "llvm/IR/Analysis.h"
25#include "llvm/IR/Module.h"
26#include "llvm/IR/PseudoProbe.h"
29
30#define DEBUG_TYPE "pseudo-probe-inserter"
31
32using namespace llvm;
33
34namespace {
35
36// A real instruction is a non-meta, non-pseudo instruction. Calls (including
37// call pseudos like tail-call returns) are also treated as real because they
38// expand to a branch and any preceding call probe must be preserved.
39static bool isCallOrRealInstruction(const MachineInstr &MI) {
40 return MI.isCall() || (!MI.isPseudo() && !MI.isMetaInstruction());
41}
42
43static uint64_t getFuncGUID(Module *M, DILocation *DL) {
44 auto Name = DL->getSubprogramLinkageName();
45 // CoroSplit Pass will change the debug info with suffixes i.e. `.resume`,
46 // `.destroy`, `.cleanup`. Strip these suffixes to make the GUID consistent
47 // with the pseudo probe
50}
51
52static bool insertPseudoProbesInMF(MachineFunction &MF) {
55 return false;
57 bool Changed = false;
58 for (MachineBasicBlock &MBB : MF) {
59 MachineInstr *FirstInstr = nullptr;
60 for (MachineInstr &MI : MBB) {
61 // Pseudo instructions like TCRETURNdi results in a branch instruction
62 // and the call probe for that tail call should be preserved.
63 if (isCallOrRealInstruction(MI))
64 FirstInstr = &MI;
65 if (MI.isCall()) {
66 if (DILocation *DL = MI.getDebugLoc()) {
67 auto Value = DL->getDiscriminator();
69 BuildMI(MBB, MI, DL, TII->get(TargetOpcode::PSEUDO_PROBE))
70 .addImm(getFuncGUID(MF.getFunction().getParent(), DL))
74 Value));
75 Changed = true;
76 }
77 }
78 }
79 }
80
81 // Walk the block backwards, move PSEUDO_PROBE before the first real
82 // instruction to fix out-of-order probes. There is a problem with probes
83 // as the terminator of the block. During the offline counts processing,
84 // the samples collected on the first physical instruction following a
85 // probe will be counted towards the probe. This logically equals to
86 // treating the instruction next to a probe as if it is from the same
87 // block of the probe. This is accurate most of the time unless the
88 // instruction can be reached from multiple flows, which means it actually
89 // starts a new block. Samples collected on such probes may cause
90 // imprecision with the counts inference algorithm. Fortunately, if
91 // there are still other native instructions preceding the probe we can
92 // use them as a place holder to collect samples for the probe.
93 if (FirstInstr) {
94 auto MII = MBB.rbegin();
95 while (MII != MBB.rend()) {
96 // Skip all pseudo probes followed by a real instruction since they
97 // are not dangling. Treat call pseudos (e.g. tail-call returns)
98 // as real instructions to keep this consistent with the forward
99 // scan above.
100 if (isCallOrRealInstruction(*MII))
101 break;
102 auto Cur = MII++;
103 if (Cur->getOpcode() != TargetOpcode::PSEUDO_PROBE)
104 continue;
105 // Move the dangling probe before FirstInstr.
106 auto *ProbeInstr = &*Cur;
107 MBB.remove(ProbeInstr);
108 MBB.insert(FirstInstr, ProbeInstr);
109 Changed = true;
110 }
111 } else {
112 // Probes not surrounded by any real instructions in the same block are
113 // called dangling probes. Since there's no good way to pick up a sample
114 // collection point for dangling probes at compile time, they are being
115 // removed so that the profile correlation tool will not report any
116 // samples collected for them and it's up to the counts inference tool
117 // to get them a reasonable count.
119 for (MachineInstr &MI : MBB) {
120 if (MI.isPseudoProbe())
121 ToBeRemoved.push_back(&MI);
122 }
123
124 for (auto *MI : ToBeRemoved)
125 MI->eraseFromParent();
126
127 Changed |= !ToBeRemoved.empty();
128 }
129 }
130
131 return Changed;
132}
133
134class PseudoProbeInserterLegacy : public MachineFunctionPass {
135public:
136 static char ID;
137
138 PseudoProbeInserterLegacy() : MachineFunctionPass(ID) {}
139
140 StringRef getPassName() const override { return "Pseudo Probe Inserter"; }
141
142 void getAnalysisUsage(AnalysisUsage &AU) const override {
143 AU.setPreservesAll();
145 }
146
147 bool runOnMachineFunction(MachineFunction &MF) override {
148 return insertPseudoProbesInMF(MF);
149 }
150};
151} // namespace
152
160
161char PseudoProbeInserterLegacy::ID = 0;
162INITIALIZE_PASS_BEGIN(PseudoProbeInserterLegacy, DEBUG_TYPE,
163 "Insert pseudo probe annotations for value profiling",
164 false, false)
165INITIALIZE_PASS_END(PseudoProbeInserterLegacy, DEBUG_TYPE,
166 "Insert pseudo probe annotations for value profiling",
168
170 return new PseudoProbeInserterLegacy();
171}
uint64_t uint64_t
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_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.
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
static bool isPseudoProbeDiscriminator(unsigned Discriminator)
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
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:80
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:68
NamedMDNode * getNamedMetadata(StringRef Name) const
Return the first NamedMDNode in the module with the specified name.
Definition Module.cpp:301
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetInstrInfo * getInstrInfo() const
LLVM Value Representation.
Definition Value.h:75
static StringRef getCanonicalCoroFnName(StringRef FnName, StringRef Attr="selected")
Changed
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI FunctionPass * createPseudoProbeInserterLegacyPass()
This pass inserts pseudo probe annotation for callsite profiling.
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
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