LLVM 19.0.0git
Debugify.h
Go to the documentation of this file.
1//===- Debugify.h - Check debug info preservation in optimizations --------===//
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/// \file Interface to the `debugify` synthetic/original debug info testing
10/// utility.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_UTILS_DEBUGIFY_H
15#define LLVM_TRANSFORMS_UTILS_DEBUGIFY_H
16
17#include "llvm/ADT/MapVector.h"
18#include "llvm/ADT/StringRef.h"
22#include "llvm/IR/PassManager.h"
23#include "llvm/IR/ValueHandle.h"
24#include "llvm/Pass.h"
25
32
33/// Used to track the Debug Info Metadata information.
35 // This maps a function name to its associated DISubprogram.
37 // This maps an instruction and the info about whether it has !dbg attached.
39 // This tracks value (instruction) deletion. If an instruction gets deleted,
40 // WeakVH nulls itself.
42 // Maps variable into dbg users (#dbg values/declares for this variable).
44};
45
46namespace llvm {
47class DIBuilder;
48
49/// Add synthesized debug information to a module.
50///
51/// \param M The module to add debug information to.
52/// \param Functions A range of functions to add debug information to.
53/// \param Banner A prefix string to add to debug/error messages.
54/// \param ApplyToMF A call back that will add debug information to the
55/// MachineFunction for a Function. If nullptr, then the
56/// MachineFunction (if any) will not be modified.
59 std::function<bool(DIBuilder &, Function &)> ApplyToMF);
60
61/// Strip out all of the metadata and debug info inserted by debugify. If no
62/// llvm.debugify module-level named metadata is present, this is a no-op.
63/// Returns true if any change was made.
65
66/// Collect original debug information before a pass.
67///
68/// \param M The module to collect debug information from.
69/// \param Functions A range of functions to collect debug information from.
70/// \param DebugInfoBeforePass DI metadata before a pass.
71/// \param Banner A prefix string to add to debug/error messages.
72/// \param NameOfWrappedPass A name of a pass to add to debug/error messages.
75 DebugInfoPerPass &DebugInfoBeforePass,
76 StringRef Banner, StringRef NameOfWrappedPass);
77
78/// Check original debug information after a pass.
79///
80/// \param M The module to collect debug information from.
81/// \param Functions A range of functions to collect debug information from.
82/// \param DebugInfoBeforePass DI metadata before a pass.
83/// \param Banner A prefix string to add to debug/error messages.
84/// \param NameOfWrappedPass A name of a pass to add to debug/error messages.
87 DebugInfoPerPass &DebugInfoBeforePass,
88 StringRef Banner, StringRef NameOfWrappedPass,
89 StringRef OrigDIVerifyBugsReportFilePath);
90} // namespace llvm
91
92/// Used to check whether we track synthetic or original debug info.
94
97 llvm::StringRef NameOfWrappedPass = "",
98 DebugInfoPerPass *DebugInfoBeforePass = nullptr);
101 llvm::StringRef NameOfWrappedPass = "",
102 DebugInfoPerPass *DebugInfoBeforePass = nullptr);
103
104class NewPMDebugifyPass : public llvm::PassInfoMixin<NewPMDebugifyPass> {
105 llvm::StringRef NameOfWrappedPass;
106 DebugInfoPerPass *DebugInfoBeforePass = nullptr;
107 enum DebugifyMode Mode = DebugifyMode::NoDebugify;
108public:
110 enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
111 llvm::StringRef NameOfWrappedPass = "",
112 DebugInfoPerPass *DebugInfoBeforePass = nullptr)
113 : NameOfWrappedPass(NameOfWrappedPass),
114 DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}
115
117};
118
119/// Track how much `debugify` information (in the `synthetic` mode only)
120/// has been lost.
122 /// Number of missing dbg.values.
124
125 /// Number of dbg.values expected.
127
128 /// Number of instructions with empty debug locations.
129 unsigned NumDbgLocsMissing = 0;
130
131 /// Number of instructions expected to have debug locations.
132 unsigned NumDbgLocsExpected = 0;
133
134 /// Get the ratio of missing/expected dbg.values.
135 float getMissingValueRatio() const {
136 return float(NumDbgValuesMissing) / float(NumDbgLocsExpected);
137 }
138
139 /// Get the ratio of missing/expected instructions with locations.
140 float getEmptyLocationRatio() const {
141 return float(NumDbgLocsMissing) / float(NumDbgLocsExpected);
142 }
143};
144
145/// Map pass names to a per-pass DebugifyStatistics instance.
147
149 bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
150 DebugifyStatsMap *StatsMap = nullptr,
152 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
153 llvm::StringRef OrigDIVerifyBugsReportFilePath = "");
154
156 bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
157 DebugifyStatsMap *StatsMap = nullptr,
159 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
160 llvm::StringRef OrigDIVerifyBugsReportFilePath = "");
161
163 : public llvm::PassInfoMixin<NewPMCheckDebugifyPass> {
164 llvm::StringRef NameOfWrappedPass;
165 llvm::StringRef OrigDIVerifyBugsReportFilePath;
166 DebugifyStatsMap *StatsMap;
167 DebugInfoPerPass *DebugInfoBeforePass;
168 enum DebugifyMode Mode;
169 bool Strip;
170public:
172 bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
173 DebugifyStatsMap *StatsMap = nullptr,
174 enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
175 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
176 llvm::StringRef OrigDIVerifyBugsReportFilePath = "")
177 : NameOfWrappedPass(NameOfWrappedPass),
178 OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),
179 StatsMap(StatsMap), DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode),
180 Strip(Strip) {}
181
183};
184
185namespace llvm {
186void exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map);
187
189 llvm::StringRef OrigDIVerifyBugsReportFilePath = "";
190 DebugInfoPerPass *DebugInfoBeforePass = nullptr;
191 enum DebugifyMode Mode = DebugifyMode::NoDebugify;
192 DebugifyStatsMap *DIStatsMap = nullptr;
193
194public:
197 // Used within DebugifyMode::SyntheticDebugInfo mode.
198 void setDIStatsMap(DebugifyStatsMap &StatMap) { DIStatsMap = &StatMap; }
199 const DebugifyStatsMap &getDebugifyStatsMap() const { return *DIStatsMap; }
200 // Used within DebugifyMode::OriginalDebugInfo mode.
202 DebugInfoBeforePass = &PerPassMap;
203 }
204 DebugInfoPerPass &getDebugInfoPerPass() { return *DebugInfoBeforePass; }
205
207 OrigDIVerifyBugsReportFilePath = BugsReportFilePath;
208 }
210 return OrigDIVerifyBugsReportFilePath;
211 }
212
213 void setDebugifyMode(enum DebugifyMode M) { Mode = M; }
214
215 bool isSyntheticDebugInfo() const {
216 return Mode == DebugifyMode::SyntheticDebugInfo;
217 }
219 return Mode == DebugifyMode::OriginalDebugInfo;
220 }
221};
222
223/// DebugifyCustomPassManager wraps each pass with the debugify passes if
224/// needed.
225/// NOTE: We support legacy custom pass manager only.
226/// TODO: Add New PM support for custom pass manager.
228 StringRef OrigDIVerifyBugsReportFilePath;
229 DebugifyStatsMap *DIStatsMap = nullptr;
230 DebugInfoPerPass *DebugInfoBeforePass = nullptr;
231 enum DebugifyMode Mode = DebugifyMode::NoDebugify;
232
233public:
235
236 void add(Pass *P) override {
237 // Wrap each pass with (-check)-debugify passes if requested, making
238 // exceptions for passes which shouldn't see -debugify instrumentation.
239 bool WrapWithDebugify = Mode != DebugifyMode::NoDebugify &&
240 !P->getAsImmutablePass() && !isIRPrintingPass(P) &&
242 if (!WrapWithDebugify) {
243 super::add(P);
244 return;
245 }
246
247 // Either apply -debugify/-check-debugify before/after each pass and collect
248 // debug info loss statistics, or collect and check original debug info in
249 // the optimizations.
250 PassKind Kind = P->getPassKind();
251 StringRef Name = P->getPassName();
252
253 // TODO: Implement Debugify for LoopPass.
254 switch (Kind) {
255 case PT_Function:
256 super::add(createDebugifyFunctionPass(Mode, Name, DebugInfoBeforePass));
257 super::add(P);
259 isSyntheticDebugInfo(), Name, DIStatsMap, Mode, DebugInfoBeforePass,
260 OrigDIVerifyBugsReportFilePath));
261 break;
262 case PT_Module:
263 super::add(createDebugifyModulePass(Mode, Name, DebugInfoBeforePass));
264 super::add(P);
266 isSyntheticDebugInfo(), Name, DIStatsMap, Mode, DebugInfoBeforePass,
267 OrigDIVerifyBugsReportFilePath));
268 break;
269 default:
270 super::add(P);
271 break;
272 }
273 }
274
275 // Used within DebugifyMode::SyntheticDebugInfo mode.
276 void setDIStatsMap(DebugifyStatsMap &StatMap) { DIStatsMap = &StatMap; }
277 // Used within DebugifyMode::OriginalDebugInfo mode.
279 DebugInfoBeforePass = &PerPassDI;
280 }
282 OrigDIVerifyBugsReportFilePath = BugsReportFilePath;
283 }
285 return OrigDIVerifyBugsReportFilePath;
286 }
287
288 void setDebugifyMode(enum DebugifyMode M) { Mode = M; }
289
290 bool isSyntheticDebugInfo() const {
291 return Mode == DebugifyMode::SyntheticDebugInfo;
292 }
294 return Mode == DebugifyMode::OriginalDebugInfo;
295 }
296
297 const DebugifyStatsMap &getDebugifyStatsMap() const { return *DIStatsMap; }
298 DebugInfoPerPass &getDebugInfoPerPass() { return *DebugInfoBeforePass; }
299};
300} // namespace llvm
301
302#endif // LLVM_TRANSFORMS_UTILS_DEBUGIFY_H
This file provides a bitcode writing pass.
DebugifyMode
Used to check whether we track synthetic or original debug info.
Definition: Debugify.h:93
llvm::ModulePass * createDebugifyModulePass(enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, llvm::StringRef NameOfWrappedPass="", DebugInfoPerPass *DebugInfoBeforePass=nullptr)
Definition: Debugify.cpp:996
llvm::ModulePass * createCheckDebugifyModulePass(bool Strip=false, llvm::StringRef NameOfWrappedPass="", DebugifyStatsMap *StatsMap=nullptr, enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, DebugInfoPerPass *DebugInfoBeforePass=nullptr, llvm::StringRef OrigDIVerifyBugsReportFilePath="")
Definition: Debugify.cpp:1029
llvm::FunctionPass * createCheckDebugifyFunctionPass(bool Strip=false, llvm::StringRef NameOfWrappedPass="", DebugifyStatsMap *StatsMap=nullptr, enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, DebugInfoPerPass *DebugInfoBeforePass=nullptr, llvm::StringRef OrigDIVerifyBugsReportFilePath="")
Definition: Debugify.cpp:1041
llvm::FunctionPass * createDebugifyFunctionPass(enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, llvm::StringRef NameOfWrappedPass="", DebugInfoPerPass *DebugInfoBeforePass=nullptr)
Definition: Debugify.cpp:1006
std::string Name
This file contains an interface for creating legacy passes to print out IR in various granularities.
This file implements a map that provides insertion order iteration.
#define P(N)
ModuleAnalysisManager MAM
PassInstrumentationCallbacks PIC
This header defines various interfaces for pass management in LLVM.
static cl::opt< RegAllocEvictionAdvisorAnalysis::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Development, "development", "for training")))
llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
Definition: Debugify.cpp:1053
NewPMCheckDebugifyPass(bool Strip=false, llvm::StringRef NameOfWrappedPass="", DebugifyStatsMap *StatsMap=nullptr, enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, DebugInfoPerPass *DebugInfoBeforePass=nullptr, llvm::StringRef OrigDIVerifyBugsReportFilePath="")
Definition: Debugify.h:171
llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
Definition: Debugify.cpp:1015
NewPMDebugifyPass(enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, llvm::StringRef NameOfWrappedPass="", DebugInfoPerPass *DebugInfoBeforePass=nullptr)
Definition: Debugify.h:109
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:348
DebugifyCustomPassManager wraps each pass with the debugify passes if needed.
Definition: Debugify.h:227
bool isOriginalDebugInfoMode() const
Definition: Debugify.h:293
void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath)
Definition: Debugify.h:281
void add(Pass *P) override
Add a pass to the queue of passes to run.
Definition: Debugify.h:236
void setDebugInfoBeforePass(DebugInfoPerPass &PerPassDI)
Definition: Debugify.h:278
const DebugifyStatsMap & getDebugifyStatsMap() const
Definition: Debugify.h:297
void setDebugifyMode(enum DebugifyMode M)
Definition: Debugify.h:288
DebugInfoPerPass & getDebugInfoPerPass()
Definition: Debugify.h:298
void setDIStatsMap(DebugifyStatsMap &StatMap)
Definition: Debugify.h:276
StringRef getOrigDIVerifyBugsReportFilePath() const
Definition: Debugify.h:284
bool isSyntheticDebugInfo() const
Definition: Debugify.h:290
StringRef getOrigDIVerifyBugsReportFilePath() const
Definition: Debugify.h:209
void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath)
Definition: Debugify.h:206
void setDebugifyMode(enum DebugifyMode M)
Definition: Debugify.h:213
const DebugifyStatsMap & getDebugifyStatsMap() const
Definition: Debugify.h:199
void registerCallbacks(PassInstrumentationCallbacks &PIC, ModuleAnalysisManager &MAM)
Definition: Debugify.cpp:1081
DebugInfoPerPass & getDebugInfoPerPass()
Definition: Debugify.h:204
bool isOriginalDebugInfoMode() const
Definition: Debugify.h:218
void setDIStatsMap(DebugifyStatsMap &StatMap)
Definition: Debugify.h:198
void setDebugInfoBeforePass(DebugInfoPerPass &PerPassMap)
Definition: Debugify.h:201
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
A range adaptor for a pair of iterators.
PassManager manages ModulePassManagers.
void add(Pass *P) override
Add a pass to the queue of passes to run.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
PassKind
Definition: Pass.h:66
@ PT_Module
Definition: Pass.h:71
@ PT_Function
Definition: Pass.h:69
bool applyDebugifyMetadata(Module &M, iterator_range< Module::iterator > Functions, StringRef Banner, std::function< bool(DIBuilder &, Function &)> ApplyToMF)
Add synthesized debug information to a module.
bool stripDebugifyMetadata(Module &M)
Strip out all of the metadata and debug info inserted by debugify.
Definition: Debugify.cpp:252
void exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map)
Definition: Debugify.cpp:975
bool collectDebugInfoMetadata(Module &M, iterator_range< Module::iterator > Functions, DebugInfoPerPass &DebugInfoBeforePass, StringRef Banner, StringRef NameOfWrappedPass)
Collect original debug information before a pass.
Definition: Debugify.cpp:302
bool checkDebugInfoMetadata(Module &M, iterator_range< Module::iterator > Functions, DebugInfoPerPass &DebugInfoBeforePass, StringRef Banner, StringRef NameOfWrappedPass, StringRef OrigDIVerifyBugsReportFilePath)
Check original debug information after a pass.
Definition: Debugify.cpp:552
bool isIRPrintingPass(Pass *P)
Return true if a pass is for IR printing.
bool isBitcodeWriterPass(Pass *P)
Check whether a pass is a BitcodeWriterPass.
Used to track the Debug Info Metadata information.
Definition: Debugify.h:34
DebugInstMap DILocations
Definition: Debugify.h:38
DebugFnMap DIFunctions
Definition: Debugify.h:36
DebugVarMap DIVariables
Definition: Debugify.h:43
WeakInstValueMap InstToDelete
Definition: Debugify.h:41
Track how much debugify information (in the synthetic mode only) has been lost.
Definition: Debugify.h:121
unsigned NumDbgValuesExpected
Number of dbg.values expected.
Definition: Debugify.h:126
unsigned NumDbgLocsExpected
Number of instructions expected to have debug locations.
Definition: Debugify.h:132
float getEmptyLocationRatio() const
Get the ratio of missing/expected instructions with locations.
Definition: Debugify.h:140
unsigned NumDbgLocsMissing
Number of instructions with empty debug locations.
Definition: Debugify.h:129
unsigned NumDbgValuesMissing
Number of missing dbg.values.
Definition: Debugify.h:123
float getMissingValueRatio() const
Get the ratio of missing/expected dbg.values.
Definition: Debugify.h:135
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:91