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