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 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
105 llvm::StringRef NameOfWrappedPass = "",
106 DebugInfoPerPass *DebugInfoBeforePass = nullptr);
109 llvm::StringRef NameOfWrappedPass = "",
110 DebugInfoPerPass *DebugInfoBeforePass = nullptr);
111
113 : public llvm::OptionalPassInfoMixin<NewPMDebugifyPass> {
114 DebugifyApplyToMFCallback ApplyToMF = nullptr;
115 llvm::StringRef NameOfWrappedPass;
116 DebugInfoPerPass *DebugInfoBeforePass = nullptr;
118public:
121 llvm::StringRef NameOfWrappedPass = "",
122 DebugInfoPerPass *DebugInfoBeforePass = nullptr)
123 : NameOfWrappedPass(NameOfWrappedPass),
124 DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}
126 : ApplyToMF(ApplyToMF), Mode(DebugifyMode::SyntheticDebugInfo) {}
127
130};
131
132/// Track how much `debugify` information (in the `synthetic` mode only)
133/// has been lost.
135 /// Number of missing dbg.values.
137
138 /// Number of dbg.values expected.
140
141 /// Number of instructions with empty debug locations.
142 unsigned NumDbgLocsMissing = 0;
143
144 /// Number of instructions expected to have debug locations.
145 unsigned NumDbgLocsExpected = 0;
146
147 /// Get the ratio of missing/expected dbg.values.
148 float getMissingValueRatio() const {
149 return float(NumDbgValuesMissing) / float(NumDbgLocsExpected);
150 }
151
152 /// Get the ratio of missing/expected instructions with locations.
153 float getEmptyLocationRatio() const {
154 return float(NumDbgLocsMissing) / float(NumDbgLocsExpected);
155 }
156};
157
158/// Map pass names to a per-pass DebugifyStatistics instance.
160
162 bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
163 DebugifyStatsMap *StatsMap = nullptr,
165 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
166 llvm::StringRef OrigDIVerifyBugsReportFilePath = "");
167
169 bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
170 DebugifyStatsMap *StatsMap = nullptr,
172 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
173 llvm::StringRef OrigDIVerifyBugsReportFilePath = "");
174
176 : public llvm::OptionalPassInfoMixin<NewPMCheckDebugifyPass> {
177 llvm::StringRef NameOfWrappedPass;
178 llvm::StringRef OrigDIVerifyBugsReportFilePath;
179 DebugifyStatsMap *StatsMap;
180 DebugInfoPerPass *DebugInfoBeforePass;
181 enum DebugifyMode Mode;
182 bool Strip;
183public:
185 bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
186 DebugifyStatsMap *StatsMap = nullptr,
188 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
189 llvm::StringRef OrigDIVerifyBugsReportFilePath = "")
190 : NameOfWrappedPass(NameOfWrappedPass),
191 OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),
192 StatsMap(StatsMap), DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode),
193 Strip(Strip) {}
194
197};
198
199namespace llvm {
201
203 llvm::StringRef OrigDIVerifyBugsReportFilePath = "";
204 DebugInfoPerPass *DebugInfoBeforePass = nullptr;
206 DebugifyStatsMap *DIStatsMap = nullptr;
207
208public:
211 // Used within DebugifyMode::SyntheticDebugInfo mode.
212 void setDIStatsMap(DebugifyStatsMap &StatMap) { DIStatsMap = &StatMap; }
213 const DebugifyStatsMap &getDebugifyStatsMap() const { return *DIStatsMap; }
214 // Used within DebugifyMode::OriginalDebugInfo mode.
216 DebugInfoBeforePass = &PerPassMap;
217 }
218 DebugInfoPerPass &getDebugInfoPerPass() { return *DebugInfoBeforePass; }
219
221 OrigDIVerifyBugsReportFilePath = BugsReportFilePath;
222 }
224 return OrigDIVerifyBugsReportFilePath;
225 }
226
227 void setDebugifyMode(enum DebugifyMode M) { Mode = M; }
228
229 bool isSyntheticDebugInfo() const {
231 }
233 return Mode == DebugifyMode::OriginalDebugInfo;
234 }
235};
236
237/// DebugifyCustomPassManager wraps each pass with the debugify passes if
238/// needed.
239/// NOTE: We support legacy custom pass manager only.
240/// TODO: Add New PM support for custom pass manager.
242 StringRef OrigDIVerifyBugsReportFilePath;
243 DebugifyStatsMap *DIStatsMap = nullptr;
244 DebugInfoPerPass *DebugInfoBeforePass = nullptr;
246
247public:
249
250 void add(Pass *P) override {
251 // Wrap each pass with (-check)-debugify passes if requested, making
252 // exceptions for passes which shouldn't see -debugify instrumentation.
253 bool WrapWithDebugify = Mode != DebugifyMode::NoDebugify &&
254 !P->getAsImmutablePass() && !isIRPrintingPass(P) &&
256 if (!WrapWithDebugify) {
257 super::add(P);
258 return;
259 }
260
261 // Either apply -debugify/-check-debugify before/after each pass and collect
262 // debug info loss statistics, or collect and check original debug info in
263 // the optimizations.
264 PassKind Kind = P->getPassKind();
265 StringRef Name = P->getPassName();
266
267 // TODO: Implement Debugify for LoopPass.
268 switch (Kind) {
269 case PT_Function:
270 super::add(createDebugifyFunctionPass(Mode, Name, DebugInfoBeforePass));
271 super::add(P);
273 isSyntheticDebugInfo(), Name, DIStatsMap, Mode, DebugInfoBeforePass,
274 OrigDIVerifyBugsReportFilePath));
275 break;
276 case PT_Module:
277 super::add(createDebugifyModulePass(Mode, Name, DebugInfoBeforePass));
278 super::add(P);
280 isSyntheticDebugInfo(), Name, DIStatsMap, Mode, DebugInfoBeforePass,
281 OrigDIVerifyBugsReportFilePath));
282 break;
283 default:
284 super::add(P);
285 break;
286 }
287 }
288
289 // Used within DebugifyMode::SyntheticDebugInfo mode.
290 void setDIStatsMap(DebugifyStatsMap &StatMap) { DIStatsMap = &StatMap; }
291 // Used within DebugifyMode::OriginalDebugInfo mode.
293 DebugInfoBeforePass = &PerPassDI;
294 }
296 OrigDIVerifyBugsReportFilePath = BugsReportFilePath;
297 }
299 return OrigDIVerifyBugsReportFilePath;
300 }
301
302 void setDebugifyMode(enum DebugifyMode M) { Mode = M; }
303
304 bool isSyntheticDebugInfo() const {
306 }
308 return Mode == DebugifyMode::OriginalDebugInfo;
309 }
310
311 const DebugifyStatsMap &getDebugifyStatsMap() const { return *DIStatsMap; }
312 DebugInfoPerPass &getDebugInfoPerPass() { return *DebugInfoBeforePass; }
313};
314} // namespace llvm
315
316#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::function_ref< bool( llvm::DIBuilder &, llvm::Function &, llvm::ModuleAnalysisManager &)> DebugifyApplyToMFCallback
Definition Debugify.h:100
llvm::MapVector< llvm::StringRef, DebugifyStatistics > DebugifyStatsMap
Map pass names to a per-pass DebugifyStatistics instance.
Definition Debugify.h:159
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:184
LLVM_ABI llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
NewPMDebugifyPass(DebugifyApplyToMFCallback ApplyToMF)
Definition Debugify.h:125
NewPMDebugifyPass(enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, llvm::StringRef NameOfWrappedPass="", DebugInfoPerPass *DebugInfoBeforePass=nullptr)
Definition Debugify.h:119
DebugifyCustomPassManager wraps each pass with the debugify passes if needed.
Definition Debugify.h:241
bool isOriginalDebugInfoMode() const
Definition Debugify.h:307
legacy::PassManager super
Definition Debugify.h:248
void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath)
Definition Debugify.h:295
void add(Pass *P) override
Add a pass to the queue of passes to run.
Definition Debugify.h:250
void setDebugInfoBeforePass(DebugInfoPerPass &PerPassDI)
Definition Debugify.h:292
const DebugifyStatsMap & getDebugifyStatsMap() const
Definition Debugify.h:311
void setDebugifyMode(enum DebugifyMode M)
Definition Debugify.h:302
DebugInfoPerPass & getDebugInfoPerPass()
Definition Debugify.h:312
void setDIStatsMap(DebugifyStatsMap &StatMap)
Definition Debugify.h:290
StringRef getOrigDIVerifyBugsReportFilePath() const
Definition Debugify.h:298
StringRef getOrigDIVerifyBugsReportFilePath() const
Definition Debugify.h:223
void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath)
Definition Debugify.h:220
void setDebugifyMode(enum DebugifyMode M)
Definition Debugify.h:227
const DebugifyStatsMap & getDebugifyStatsMap() const
Definition Debugify.h:213
LLVM_ABI void registerCallbacks(PassInstrumentationCallbacks &PIC, ModuleAnalysisManager &MAM)
DebugInfoPerPass & getDebugInfoPerPass()
Definition Debugify.h:218
void setDIStatsMap(DebugifyStatsMap &StatMap)
Definition Debugify.h:212
void setDebugInfoBeforePass(DebugInfoPerPass &PerPassMap)
Definition Debugify.h:215
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:306
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:366
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:611
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:134
unsigned NumDbgValuesExpected
Number of dbg.values expected.
Definition Debugify.h:139
unsigned NumDbgLocsExpected
Number of instructions expected to have debug locations.
Definition Debugify.h:145
float getEmptyLocationRatio() const
Get the ratio of missing/expected instructions with locations.
Definition Debugify.h:153
unsigned NumDbgLocsMissing
Number of instructions with empty debug locations.
Definition Debugify.h:142
unsigned NumDbgValuesMissing
Number of missing dbg.values.
Definition Debugify.h:136
float getMissingValueRatio() const
Get the ratio of missing/expected dbg.values.
Definition Debugify.h:148
A CRTP mix-in for passes that can be skipped.