LLVM 23.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 tracks value (instruction) deletion. If an instruction gets deleted,
41 // WeakVH nulls itself.
43 // Maps variable into dbg users (#dbg values/declares for this variable).
45};
46
47namespace llvm {
48class DIBuilder;
49
50/// Add synthesized debug information to a module.
51///
52/// \param M The module to add debug information to.
53/// \param Functions A range of functions to add debug information to.
54/// \param Banner A prefix string to add to debug/error messages.
55/// \param ApplyToMF A call back that will add debug information to the
56/// MachineFunction for a Function. If nullptr, then the
57/// MachineFunction (if any) will not be modified.
58LLVM_ABI bool
60 StringRef Banner,
61 std::function<bool(DIBuilder &, Function &)> ApplyToMF);
62
63/// Strip out all of the metadata and debug info inserted by debugify. If no
64/// llvm.debugify module-level named metadata is present, this is a no-op.
65/// Returns true if any change was made.
67
68/// Collect original debug information before a pass.
69///
70/// \param M The module to collect debug information from.
71/// \param Functions A range of functions to collect debug information from.
72/// \param DebugInfoBeforePass DI metadata before a pass.
73/// \param Banner A prefix string to add to debug/error messages.
74/// \param NameOfWrappedPass A name of a pass to add to debug/error messages.
75LLVM_ABI bool
77 DebugInfoPerPass &DebugInfoBeforePass,
78 StringRef Banner, StringRef NameOfWrappedPass);
79
80/// Check original debug information after a pass.
81///
82/// \param M The module to collect debug information from.
83/// \param Functions A range of functions to collect debug information from.
84/// \param DebugInfoBeforePass DI metadata before a pass.
85/// \param Banner A prefix string to add to debug/error messages.
86/// \param NameOfWrappedPass A name of a pass to add to debug/error messages.
89 DebugInfoPerPass &DebugInfoBeforePass,
90 StringRef Banner,
91 StringRef NameOfWrappedPass,
92 StringRef OrigDIVerifyBugsReportFilePath);
93} // namespace llvm
94
95/// Used to check whether we track synthetic or original debug info.
97
100
103 llvm::StringRef NameOfWrappedPass = "",
104 DebugInfoPerPass *DebugInfoBeforePass = nullptr);
107 llvm::StringRef NameOfWrappedPass = "",
108 DebugInfoPerPass *DebugInfoBeforePass = nullptr);
109
111 : public llvm::OptionalPassInfoMixin<NewPMDebugifyPass> {
112 DebugifyApplyToMFCallback ApplyToMF = nullptr;
113 llvm::StringRef NameOfWrappedPass;
114 DebugInfoPerPass *DebugInfoBeforePass = nullptr;
116public:
119 llvm::StringRef NameOfWrappedPass = "",
120 DebugInfoPerPass *DebugInfoBeforePass = nullptr)
121 : NameOfWrappedPass(NameOfWrappedPass),
122 DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}
124 : ApplyToMF(ApplyToMF), Mode(DebugifyMode::SyntheticDebugInfo) {}
125
128};
129
130/// Track how much `debugify` information (in the `synthetic` mode only)
131/// has been lost.
133 /// Number of missing dbg.values.
135
136 /// Number of dbg.values expected.
138
139 /// Number of instructions with empty debug locations.
140 unsigned NumDbgLocsMissing = 0;
141
142 /// Number of instructions expected to have debug locations.
143 unsigned NumDbgLocsExpected = 0;
144
145 /// Get the ratio of missing/expected dbg.values.
146 float getMissingValueRatio() const {
147 return float(NumDbgValuesMissing) / float(NumDbgLocsExpected);
148 }
149
150 /// Get the ratio of missing/expected instructions with locations.
151 float getEmptyLocationRatio() const {
152 return float(NumDbgLocsMissing) / float(NumDbgLocsExpected);
153 }
154};
155
156/// Map pass names to a per-pass DebugifyStatistics instance.
158
160 bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
161 DebugifyStatsMap *StatsMap = nullptr,
163 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
164 llvm::StringRef OrigDIVerifyBugsReportFilePath = "");
165
167 bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
168 DebugifyStatsMap *StatsMap = nullptr,
170 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
171 llvm::StringRef OrigDIVerifyBugsReportFilePath = "");
172
174 : public llvm::OptionalPassInfoMixin<NewPMCheckDebugifyPass> {
175 llvm::StringRef NameOfWrappedPass;
176 llvm::StringRef OrigDIVerifyBugsReportFilePath;
177 DebugifyStatsMap *StatsMap;
178 DebugInfoPerPass *DebugInfoBeforePass;
179 enum DebugifyMode Mode;
180 bool Strip;
181public:
183 bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
184 DebugifyStatsMap *StatsMap = nullptr,
186 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
187 llvm::StringRef OrigDIVerifyBugsReportFilePath = "")
188 : NameOfWrappedPass(NameOfWrappedPass),
189 OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),
190 StatsMap(StatsMap), DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode),
191 Strip(Strip) {}
192
195};
196
197namespace llvm {
199
201 llvm::StringRef OrigDIVerifyBugsReportFilePath = "";
202 DebugInfoPerPass *DebugInfoBeforePass = nullptr;
204 DebugifyStatsMap *DIStatsMap = nullptr;
205
206public:
209 // Used within DebugifyMode::SyntheticDebugInfo mode.
210 void setDIStatsMap(DebugifyStatsMap &StatMap) { DIStatsMap = &StatMap; }
211 const DebugifyStatsMap &getDebugifyStatsMap() const { return *DIStatsMap; }
212 // Used within DebugifyMode::OriginalDebugInfo mode.
214 DebugInfoBeforePass = &PerPassMap;
215 }
216 DebugInfoPerPass &getDebugInfoPerPass() { return *DebugInfoBeforePass; }
217
219 OrigDIVerifyBugsReportFilePath = BugsReportFilePath;
220 }
222 return OrigDIVerifyBugsReportFilePath;
223 }
224
225 void setDebugifyMode(enum DebugifyMode M) { Mode = M; }
226
227 bool isSyntheticDebugInfo() const {
229 }
231 return Mode == DebugifyMode::OriginalDebugInfo;
232 }
233};
234
235/// DebugifyCustomPassManager wraps each pass with the debugify passes if
236/// needed.
237/// NOTE: We support legacy custom pass manager only.
238/// TODO: Add New PM support for custom pass manager.
240 StringRef OrigDIVerifyBugsReportFilePath;
241 DebugifyStatsMap *DIStatsMap = nullptr;
242 DebugInfoPerPass *DebugInfoBeforePass = nullptr;
244
245public:
247
248 void add(Pass *P) override {
249 // Wrap each pass with (-check)-debugify passes if requested, making
250 // exceptions for passes which shouldn't see -debugify instrumentation.
251 bool WrapWithDebugify = Mode != DebugifyMode::NoDebugify &&
252 !P->getAsImmutablePass() && !isIRPrintingPass(P) &&
254 if (!WrapWithDebugify) {
255 super::add(P);
256 return;
257 }
258
259 // Either apply -debugify/-check-debugify before/after each pass and collect
260 // debug info loss statistics, or collect and check original debug info in
261 // the optimizations.
262 PassKind Kind = P->getPassKind();
263 StringRef Name = P->getPassName();
264
265 // TODO: Implement Debugify for LoopPass.
266 switch (Kind) {
267 case PT_Function:
268 super::add(createDebugifyFunctionPass(Mode, Name, DebugInfoBeforePass));
269 super::add(P);
271 isSyntheticDebugInfo(), Name, DIStatsMap, Mode, DebugInfoBeforePass,
272 OrigDIVerifyBugsReportFilePath));
273 break;
274 case PT_Module:
275 super::add(createDebugifyModulePass(Mode, Name, DebugInfoBeforePass));
276 super::add(P);
278 isSyntheticDebugInfo(), Name, DIStatsMap, Mode, DebugInfoBeforePass,
279 OrigDIVerifyBugsReportFilePath));
280 break;
281 default:
282 super::add(P);
283 break;
284 }
285 }
286
287 // Used within DebugifyMode::SyntheticDebugInfo mode.
288 void setDIStatsMap(DebugifyStatsMap &StatMap) { DIStatsMap = &StatMap; }
289 // Used within DebugifyMode::OriginalDebugInfo mode.
291 DebugInfoBeforePass = &PerPassDI;
292 }
294 OrigDIVerifyBugsReportFilePath = BugsReportFilePath;
295 }
297 return OrigDIVerifyBugsReportFilePath;
298 }
299
300 void setDebugifyMode(enum DebugifyMode M) { Mode = M; }
301
302 bool isSyntheticDebugInfo() const {
304 }
306 return Mode == DebugifyMode::OriginalDebugInfo;
307 }
308
309 const DebugifyStatsMap &getDebugifyStatsMap() const { return *DIStatsMap; }
310 DebugInfoPerPass &getDebugInfoPerPass() { return *DebugInfoBeforePass; }
311};
312} // namespace llvm
313
314#endif // LLVM_TRANSFORMS_UTILS_DEBUGIFY_H
This file provides a bitcode writing pass.
#define LLVM_ABI
Definition Compiler.h:215
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:96
@ SyntheticDebugInfo
Definition Debugify.h:96
@ OriginalDebugInfo
Definition Debugify.h:96
llvm::MapVector< const llvm::Function *, const llvm::DISubprogram * > DebugFnMap
Definition Debugify.h:29
llvm::function_ref< bool( llvm::DIBuilder &, llvm::Function &, llvm::ModuleAnalysisManager &)> DebugifyApplyToMFCallback
Definition Debugify.h:98
llvm::MapVector< llvm::StringRef, DebugifyStatistics > DebugifyStatsMap
Map pass names to a per-pass DebugifyStatistics instance.
Definition Debugify.h:157
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:182
LLVM_ABI llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
NewPMDebugifyPass(DebugifyApplyToMFCallback ApplyToMF)
Definition Debugify.h:123
NewPMDebugifyPass(enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, llvm::StringRef NameOfWrappedPass="", DebugInfoPerPass *DebugInfoBeforePass=nullptr)
Definition Debugify.h:117
DebugifyCustomPassManager wraps each pass with the debugify passes if needed.
Definition Debugify.h:239
bool isOriginalDebugInfoMode() const
Definition Debugify.h:305
legacy::PassManager super
Definition Debugify.h:246
void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath)
Definition Debugify.h:293
void add(Pass *P) override
Add a pass to the queue of passes to run.
Definition Debugify.h:248
void setDebugInfoBeforePass(DebugInfoPerPass &PerPassDI)
Definition Debugify.h:290
const DebugifyStatsMap & getDebugifyStatsMap() const
Definition Debugify.h:309
void setDebugifyMode(enum DebugifyMode M)
Definition Debugify.h:300
DebugInfoPerPass & getDebugInfoPerPass()
Definition Debugify.h:310
void setDIStatsMap(DebugifyStatsMap &StatMap)
Definition Debugify.h:288
StringRef getOrigDIVerifyBugsReportFilePath() const
Definition Debugify.h:296
StringRef getOrigDIVerifyBugsReportFilePath() const
Definition Debugify.h:221
void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath)
Definition Debugify.h:218
void setDebugifyMode(enum DebugifyMode M)
Definition Debugify.h:225
const DebugifyStatsMap & getDebugifyStatsMap() const
Definition Debugify.h:211
LLVM_ABI void registerCallbacks(PassInstrumentationCallbacks &PIC, ModuleAnalysisManager &MAM)
DebugInfoPerPass & getDebugInfoPerPass()
Definition Debugify.h:216
void setDIStatsMap(DebugifyStatsMap &StatMap)
Definition Debugify.h:210
void setDebugInfoBeforePass(DebugInfoPerPass &PerPassMap)
Definition Debugify.h:213
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:38
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
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
An efficient, type-erasing, non-owning reference to a callable.
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:353
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:413
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:620
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
DebugFnMap DIFunctions
Definition Debugify.h:39
DebugVarMap DIVariables
Definition Debugify.h:44
WeakInstValueMap InstToDelete
Definition Debugify.h:42
Track how much debugify information (in the synthetic mode only) has been lost.
Definition Debugify.h:132
unsigned NumDbgValuesExpected
Number of dbg.values expected.
Definition Debugify.h:137
unsigned NumDbgLocsExpected
Number of instructions expected to have debug locations.
Definition Debugify.h:143
float getEmptyLocationRatio() const
Get the ratio of missing/expected instructions with locations.
Definition Debugify.h:151
unsigned NumDbgLocsMissing
Number of instructions with empty debug locations.
Definition Debugify.h:140
unsigned NumDbgValuesMissing
Number of missing dbg.values.
Definition Debugify.h:134
float getMissingValueRatio() const
Get the ratio of missing/expected dbg.values.
Definition Debugify.h:146
A CRTP mix-in for passes that can be skipped.