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/Module.h"
24#include "llvm/IR/PassManager.h"
25#include "llvm/IR/ValueHandle.h"
26#include "llvm/Pass.h"
27
34
35/// Used to track the Debug Info Metadata information.
37 // This maps a function name to its associated DISubprogram.
39 // This maps an instruction and the info about whether it has !dbg attached.
41 // This tracks value (instruction) deletion. If an instruction gets deleted,
42 // WeakVH nulls itself.
44 // Maps variable into dbg users (#dbg values/declares for this variable).
46};
47
48namespace llvm {
49class DIBuilder;
50
51/// Add synthesized debug information to a module.
52///
53/// \param M The module to add debug information to.
54/// \param Functions A range of functions to add debug information to.
55/// \param Banner A prefix string to add to debug/error messages.
56/// \param ApplyToMF A call back that will add debug information to the
57/// MachineFunction for a Function. If nullptr, then the
58/// MachineFunction (if any) will not be modified.
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.
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, StringRef NameOfWrappedPass,
91 StringRef OrigDIVerifyBugsReportFilePath);
92} // namespace llvm
93
94/// Used to check whether we track synthetic or original debug info.
96
99 llvm::StringRef NameOfWrappedPass = "",
100 DebugInfoPerPass *DebugInfoBeforePass = nullptr);
103 llvm::StringRef NameOfWrappedPass = "",
104 DebugInfoPerPass *DebugInfoBeforePass = nullptr);
105
106class NewPMDebugifyPass : public llvm::PassInfoMixin<NewPMDebugifyPass> {
107 llvm::StringRef NameOfWrappedPass;
108 DebugInfoPerPass *DebugInfoBeforePass = nullptr;
109 enum DebugifyMode Mode = DebugifyMode::NoDebugify;
110public:
112 enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
113 llvm::StringRef NameOfWrappedPass = "",
114 DebugInfoPerPass *DebugInfoBeforePass = nullptr)
115 : NameOfWrappedPass(NameOfWrappedPass),
116 DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}
117
119};
120
121/// Track how much `debugify` information (in the `synthetic` mode only)
122/// has been lost.
124 /// Number of missing dbg.values.
126
127 /// Number of dbg.values expected.
129
130 /// Number of instructions with empty debug locations.
131 unsigned NumDbgLocsMissing = 0;
132
133 /// Number of instructions expected to have debug locations.
134 unsigned NumDbgLocsExpected = 0;
135
136 /// Get the ratio of missing/expected dbg.values.
137 float getMissingValueRatio() const {
138 return float(NumDbgValuesMissing) / float(NumDbgLocsExpected);
139 }
140
141 /// Get the ratio of missing/expected instructions with locations.
142 float getEmptyLocationRatio() const {
143 return float(NumDbgLocsMissing) / float(NumDbgLocsExpected);
144 }
145};
146
147/// Map pass names to a per-pass DebugifyStatistics instance.
149
151 bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
152 DebugifyStatsMap *StatsMap = nullptr,
154 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
155 llvm::StringRef OrigDIVerifyBugsReportFilePath = "");
156
158 bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
159 DebugifyStatsMap *StatsMap = nullptr,
161 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
162 llvm::StringRef OrigDIVerifyBugsReportFilePath = "");
163
165 : public llvm::PassInfoMixin<NewPMCheckDebugifyPass> {
166 llvm::StringRef NameOfWrappedPass;
167 llvm::StringRef OrigDIVerifyBugsReportFilePath;
168 DebugifyStatsMap *StatsMap;
169 DebugInfoPerPass *DebugInfoBeforePass;
170 enum DebugifyMode Mode;
171 bool Strip;
172public:
174 bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
175 DebugifyStatsMap *StatsMap = nullptr,
176 enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
177 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
178 llvm::StringRef OrigDIVerifyBugsReportFilePath = "")
179 : NameOfWrappedPass(NameOfWrappedPass),
180 OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),
181 StatsMap(StatsMap), DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode),
182 Strip(Strip) {}
183
185};
186
187namespace llvm {
188void exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map);
189
191 llvm::StringRef OrigDIVerifyBugsReportFilePath = "";
192 DebugInfoPerPass *DebugInfoBeforePass = nullptr;
193 enum DebugifyMode Mode = DebugifyMode::NoDebugify;
194 DebugifyStatsMap *DIStatsMap = nullptr;
195
196public:
199 // Used within DebugifyMode::SyntheticDebugInfo mode.
200 void setDIStatsMap(DebugifyStatsMap &StatMap) { DIStatsMap = &StatMap; }
201 const DebugifyStatsMap &getDebugifyStatsMap() const { return *DIStatsMap; }
202 // Used within DebugifyMode::OriginalDebugInfo mode.
204 DebugInfoBeforePass = &PerPassMap;
205 }
206 DebugInfoPerPass &getDebugInfoPerPass() { return *DebugInfoBeforePass; }
207
209 OrigDIVerifyBugsReportFilePath = BugsReportFilePath;
210 }
212 return OrigDIVerifyBugsReportFilePath;
213 }
214
215 void setDebugifyMode(enum DebugifyMode M) { Mode = M; }
216
217 bool isSyntheticDebugInfo() const {
218 return Mode == DebugifyMode::SyntheticDebugInfo;
219 }
221 return Mode == DebugifyMode::OriginalDebugInfo;
222 }
223};
224
225/// DebugifyCustomPassManager wraps each pass with the debugify passes if
226/// needed.
227/// NOTE: We support legacy custom pass manager only.
228/// TODO: Add New PM support for custom pass manager.
230 StringRef OrigDIVerifyBugsReportFilePath;
231 DebugifyStatsMap *DIStatsMap = nullptr;
232 DebugInfoPerPass *DebugInfoBeforePass = nullptr;
233 enum DebugifyMode Mode = DebugifyMode::NoDebugify;
234
235public:
237
238 void add(Pass *P) override {
239 // Wrap each pass with (-check)-debugify passes if requested, making
240 // exceptions for passes which shouldn't see -debugify instrumentation.
241 bool WrapWithDebugify = Mode != DebugifyMode::NoDebugify &&
242 !P->getAsImmutablePass() && !isIRPrintingPass(P) &&
244 if (!WrapWithDebugify) {
245 super::add(P);
246 return;
247 }
248
249 // Either apply -debugify/-check-debugify before/after each pass and collect
250 // debug info loss statistics, or collect and check original debug info in
251 // the optimizations.
252 PassKind Kind = P->getPassKind();
253 StringRef Name = P->getPassName();
254
255 // TODO: Implement Debugify for LoopPass.
256 switch (Kind) {
257 case PT_Function:
258 super::add(createDebugifyFunctionPass(Mode, Name, DebugInfoBeforePass));
259 super::add(P);
261 isSyntheticDebugInfo(), Name, DIStatsMap, Mode, DebugInfoBeforePass,
262 OrigDIVerifyBugsReportFilePath));
263 break;
264 case PT_Module:
265 super::add(createDebugifyModulePass(Mode, Name, DebugInfoBeforePass));
266 super::add(P);
268 isSyntheticDebugInfo(), Name, DIStatsMap, Mode, DebugInfoBeforePass,
269 OrigDIVerifyBugsReportFilePath));
270 break;
271 default:
272 super::add(P);
273 break;
274 }
275 }
276
277 // Used within DebugifyMode::SyntheticDebugInfo mode.
278 void setDIStatsMap(DebugifyStatsMap &StatMap) { DIStatsMap = &StatMap; }
279 // Used within DebugifyMode::OriginalDebugInfo mode.
281 DebugInfoBeforePass = &PerPassDI;
282 }
284 OrigDIVerifyBugsReportFilePath = BugsReportFilePath;
285 }
287 return OrigDIVerifyBugsReportFilePath;
288 }
289
290 void setDebugifyMode(enum DebugifyMode M) { Mode = M; }
291
292 bool isSyntheticDebugInfo() const {
293 return Mode == DebugifyMode::SyntheticDebugInfo;
294 }
296 return Mode == DebugifyMode::OriginalDebugInfo;
297 }
298
299 const DebugifyStatsMap &getDebugifyStatsMap() const { return *DIStatsMap; }
300 DebugInfoPerPass &getDebugInfoPerPass() { return *DebugInfoBeforePass; }
301};
302} // namespace llvm
303
304#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:95
llvm::ModulePass * createDebugifyModulePass(enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, llvm::StringRef NameOfWrappedPass="", DebugInfoPerPass *DebugInfoBeforePass=nullptr)
Definition: Debugify.cpp:980
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:1013
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:1025
llvm::FunctionPass * createDebugifyFunctionPass(enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, llvm::StringRef NameOfWrappedPass="", DebugInfoPerPass *DebugInfoBeforePass=nullptr)
Definition: Debugify.cpp:990
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.
Module.h This file contains the declarations for the Module class.
#define P(N)
ModuleAnalysisManager MAM
PassInstrumentationCallbacks PIC
This file defines the Pass Instrumentation classes that provide instrumentation points into the pass ...
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:1037
NewPMCheckDebugifyPass(bool Strip=false, llvm::StringRef NameOfWrappedPass="", DebugifyStatsMap *StatsMap=nullptr, enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, DebugInfoPerPass *DebugInfoBeforePass=nullptr, llvm::StringRef OrigDIVerifyBugsReportFilePath="")
Definition: Debugify.h:173
llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
Definition: Debugify.cpp:999
NewPMDebugifyPass(enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, llvm::StringRef NameOfWrappedPass="", DebugInfoPerPass *DebugInfoBeforePass=nullptr)
Definition: Debugify.h:111
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
DebugifyCustomPassManager wraps each pass with the debugify passes if needed.
Definition: Debugify.h:229
bool isOriginalDebugInfoMode() const
Definition: Debugify.h:295
void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath)
Definition: Debugify.h:283
void add(Pass *P) override
Add a pass to the queue of passes to run.
Definition: Debugify.h:238
void setDebugInfoBeforePass(DebugInfoPerPass &PerPassDI)
Definition: Debugify.h:280
const DebugifyStatsMap & getDebugifyStatsMap() const
Definition: Debugify.h:299
void setDebugifyMode(enum DebugifyMode M)
Definition: Debugify.h:290
DebugInfoPerPass & getDebugInfoPerPass()
Definition: Debugify.h:300
void setDIStatsMap(DebugifyStatsMap &StatMap)
Definition: Debugify.h:278
StringRef getOrigDIVerifyBugsReportFilePath() const
Definition: Debugify.h:286
bool isSyntheticDebugInfo() const
Definition: Debugify.h:292
StringRef getOrigDIVerifyBugsReportFilePath() const
Definition: Debugify.h:211
void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath)
Definition: Debugify.h:208
void setDebugifyMode(enum DebugifyMode M)
Definition: Debugify.h:215
const DebugifyStatsMap & getDebugifyStatsMap() const
Definition: Debugify.h:201
void registerCallbacks(PassInstrumentationCallbacks &PIC, ModuleAnalysisManager &MAM)
Definition: Debugify.cpp:1058
DebugInfoPerPass & getDebugInfoPerPass()
Definition: Debugify.h:206
bool isOriginalDebugInfoMode() const
Definition: Debugify.h:220
void setDIStatsMap(DebugifyStatsMap &StatMap)
Definition: Debugify.h:200
void setDebugInfoBeforePass(DebugInfoPerPass &PerPassMap)
Definition: Debugify.h:203
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:111
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:245
void exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map)
Definition: Debugify.cpp:959
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:295
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:541
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:36
DebugInstMap DILocations
Definition: Debugify.h:40
DebugFnMap DIFunctions
Definition: Debugify.h:38
DebugVarMap DIVariables
Definition: Debugify.h:45
WeakInstValueMap InstToDelete
Definition: Debugify.h:43
Track how much debugify information (in the synthetic mode only) has been lost.
Definition: Debugify.h:123
unsigned NumDbgValuesExpected
Number of dbg.values expected.
Definition: Debugify.h:128
unsigned NumDbgLocsExpected
Number of instructions expected to have debug locations.
Definition: Debugify.h:134
float getEmptyLocationRatio() const
Get the ratio of missing/expected instructions with locations.
Definition: Debugify.h:142
unsigned NumDbgLocsMissing
Number of instructions with empty debug locations.
Definition: Debugify.h:131
unsigned NumDbgValuesMissing
Number of missing dbg.values.
Definition: Debugify.h:125
float getMissingValueRatio() const
Get the ratio of missing/expected dbg.values.
Definition: Debugify.h:137
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69