LLVM  15.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 
26 using DebugFnMap =
30 using WeakInstValueMap =
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 
46 namespace llvm {
47 class 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.
58  Module &M, iterator_range<Module::iterator> Functions, StringRef Banner,
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.
74  iterator_range<Module::iterator> Functions,
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.
86  iterator_range<Module::iterator> Functions,
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 
104 class NewPMDebugifyPass : public llvm::PassInfoMixin<NewPMDebugifyPass> {
105  llvm::StringRef NameOfWrappedPass;
106  DebugInfoPerPass *DebugInfoBeforePass = nullptr;
108 public:
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.
123  unsigned NumDbgValuesMissing = 0;
124 
125  /// Number of dbg.values expected.
126  unsigned NumDbgValuesExpected = 0;
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;
170 public:
172  bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
173  DebugifyStatsMap *StatsMap = nullptr,
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 
185 namespace llvm {
186 void exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map);
187 
189  llvm::StringRef OrigDIVerifyBugsReportFilePath = "";
190  DebugInfoPerPass *DebugInfoBeforePass = nullptr;
192  DebugifyStatsMap *DIStatsMap = nullptr;
193 
194 public:
195 
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 
206  void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath) {
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  }
218  bool isOriginalDebugInfoMode() const {
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;
232 
233 public:
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  }
281  void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath) {
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  }
293  bool isOriginalDebugInfoMode() const {
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
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
DebugifyMode
DebugifyMode
Used to check whether we track synthetic or original debug info.
Definition: Debugify.h:93
llvm::DebugifyEachInstrumentation::isSyntheticDebugInfo
bool isSyntheticDebugInfo() const
Definition: Debugify.h:215
llvm::DebugifyEachInstrumentation::isOriginalDebugInfoMode
bool isOriginalDebugInfoMode() const
Definition: Debugify.h:218
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:248
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:371
StringRef.h
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
Pass.h
DebugInfoPerPass::DIVariables
DebugVarMap DIVariables
Definition: Debugify.h:43
llvm::DebugifyEachInstrumentation::setDIStatsMap
void setDIStatsMap(DebugifyStatsMap &StatMap)
Definition: Debugify.h:198
MapVector.h
NewPMCheckDebugifyPass::run
llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
Definition: Debugify.cpp:998
NewPMDebugifyPass::run
llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
Definition: Debugify.cpp:963
PIC
PassInstrumentationCallbacks PIC
Definition: PassBuilderBindings.cpp:55
llvm::PassKind
PassKind
Definition: Pass.h:63
llvm::DebugifyEachInstrumentation::registerCallbacks
void registerCallbacks(PassInstrumentationCallbacks &PIC)
Definition: Debugify.cpp:1018
llvm::MapVector< const llvm::Function *, const llvm::DISubprogram * >
llvm::applyDebugifyMetadata
bool applyDebugifyMetadata(Module &M, iterator_range< Module::iterator > Functions, StringRef Banner, std::function< bool(DIBuilder &, Function &)> ApplyToMF)
Add synthesized debug information to a module.
LegacyPassManager.h
BitcodeWriterPass.h
createCheckDebugifyFunctionPass
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:986
llvm::DebugifyEachInstrumentation::setOrigDIVerifyBugsReportFilePath
void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath)
Definition: Debugify.h:206
DebugifyMode::SyntheticDebugInfo
@ SyntheticDebugInfo
NewPMCheckDebugifyPass
Definition: Debugify.h:162
llvm::collectDebugInfoMetadata
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:288
DebugInfoPerPass::DIFunctions
DebugFnMap DIFunctions
Definition: Debugify.h:36
llvm::DebugifyCustomPassManager
DebugifyCustomPassManager wraps each pass with the debugify passes if needed.
Definition: Debugify.h:227
llvm::DebugifyCustomPassManager::getDebugInfoPerPass
DebugInfoPerPass & getDebugInfoPerPass()
Definition: Debugify.h:298
IRPrintingPasses.h
llvm::DebugifyCustomPassManager::isOriginalDebugInfoMode
bool isOriginalDebugInfoMode() const
Definition: Debugify.h:293
llvm::exportDebugifyStats
void exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map)
Definition: Debugify.cpp:923
DebugInfoPerPass::InstToDelete
WeakInstValueMap InstToDelete
Definition: Debugify.h:41
llvm::stripDebugifyMetadata
bool stripDebugifyMetadata(Module &M)
Strip out all of the metadata and debug info inserted by debugify.
Definition: Debugify.cpp:243
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
DebugifyStatistics::getMissingValueRatio
float getMissingValueRatio() const
Get the ratio of missing/expected dbg.values.
Definition: Debugify.h:135
llvm::DebugifyCustomPassManager::getOrigDIVerifyBugsReportFilePath
StringRef getOrigDIVerifyBugsReportFilePath() const
Definition: Debugify.h:284
NewPMDebugifyPass
Definition: Debugify.h:104
llvm::DebugifyEachInstrumentation
Definition: Debugify.h:188
DebugInfoPerPass
Used to track the Debug Info Metadata information.
Definition: Debugify.h:34
llvm::PT_Module
@ PT_Module
Definition: Pass.h:68
createDebugifyModulePass
llvm::ModulePass * createDebugifyModulePass(enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, llvm::StringRef NameOfWrappedPass="", DebugInfoPerPass *DebugInfoBeforePass=nullptr)
Definition: Debugify.cpp:944
llvm::DebugifyCustomPassManager::isSyntheticDebugInfo
bool isSyntheticDebugInfo() const
Definition: Debugify.h:290
llvm::DebugifyEachInstrumentation::setDebugifyMode
void setDebugifyMode(enum DebugifyMode M)
Definition: Debugify.h:213
llvm::DebugifyCustomPassManager::setDebugInfoBeforePass
void setDebugInfoBeforePass(DebugInfoPerPass &PerPassDI)
Definition: Debugify.h:278
llvm::checkDebugInfoMetadata
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:527
DebugifyStatistics::NumDbgLocsMissing
unsigned NumDbgLocsMissing
Number of instructions with empty debug locations.
Definition: Debugify.h:129
createDebugifyFunctionPass
llvm::FunctionPass * createDebugifyFunctionPass(enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, llvm::StringRef NameOfWrappedPass="", DebugInfoPerPass *DebugInfoBeforePass=nullptr)
Definition: Debugify.cpp:954
DebugifyStatistics
Track how much debugify information (in the synthetic mode only) has been lost.
Definition: Debugify.h:121
llvm::DebugifyCustomPassManager::setDIStatsMap
void setDIStatsMap(DebugifyStatsMap &StatMap)
Definition: Debugify.h:276
Mode
SI Whole Quad Mode
Definition: SIWholeQuadMode.cpp:262
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:82
NewPMCheckDebugifyPass::NewPMCheckDebugifyPass
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::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
DebugifyMode::OriginalDebugInfo
@ OriginalDebugInfo
DebugifyStatistics::NumDbgLocsExpected
unsigned NumDbgLocsExpected
Number of instructions expected to have debug locations.
Definition: Debugify.h:132
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
NewPMDebugifyPass::NewPMDebugifyPass
NewPMDebugifyPass(enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, llvm::StringRef NameOfWrappedPass="", DebugInfoPerPass *DebugInfoBeforePass=nullptr)
Definition: Debugify.h:109
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
ValueHandle.h
llvm::DebugifyEachInstrumentation::getDebugifyStatsMap
const DebugifyStatsMap & getDebugifyStatsMap() const
Definition: Debugify.h:199
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::isIRPrintingPass
bool isIRPrintingPass(Pass *P)
Return true if a pass is for IR printing.
Definition: IRPrintingPasses.cpp:135
llvm::DebugifyEachInstrumentation::getOrigDIVerifyBugsReportFilePath
StringRef getOrigDIVerifyBugsReportFilePath() const
Definition: Debugify.h:209
llvm::PT_Function
@ PT_Function
Definition: Pass.h:66
DebugifyStatistics::getEmptyLocationRatio
float getEmptyLocationRatio() const
Get the ratio of missing/expected instructions with locations.
Definition: Debugify.h:140
DebugInfoPerPass::DILocations
DebugInstMap DILocations
Definition: Debugify.h:38
llvm::legacy::PassManager
PassManager manages ModulePassManagers.
Definition: LegacyPassManager.h:52
DebugifyMode::NoDebugify
@ NoDebugify
llvm::DebugifyEachInstrumentation::getDebugInfoPerPass
DebugInfoPerPass & getDebugInfoPerPass()
Definition: Debugify.h:204
PassManager.h
llvm::legacy::PassManager::add
void add(Pass *P) override
Add a pass to the queue of passes to run.
Definition: LegacyPassManager.cpp:1665
llvm::DebugifyCustomPassManager::setOrigDIVerifyBugsReportFilePath
void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath)
Definition: Debugify.h:281
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
llvm::PassInstrumentationCallbacks
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
Definition: PassInstrumentation.h:66
createCheckDebugifyModulePass
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:974
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
DebugifyStatistics::NumDbgValuesExpected
unsigned NumDbgValuesExpected
Number of dbg.values expected.
Definition: Debugify.h:126
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
llvm::DebugifyEachInstrumentation::setDebugInfoBeforePass
void setDebugInfoBeforePass(DebugInfoPerPass &PerPassMap)
Definition: Debugify.h:201
DebugifyStatistics::NumDbgValuesMissing
unsigned NumDbgValuesMissing
Number of missing dbg.values.
Definition: Debugify.h:123
llvm::DebugifyCustomPassManager::setDebugifyMode
void setDebugifyMode(enum DebugifyMode M)
Definition: Debugify.h:288
llvm::DebugifyCustomPassManager::add
void add(Pass *P) override
Add a pass to the queue of passes to run.
Definition: Debugify.h:236
llvm::DebugifyCustomPassManager::getDebugifyStatsMap
const DebugifyStatsMap & getDebugifyStatsMap() const
Definition: Debugify.h:297
llvm::isBitcodeWriterPass
bool isBitcodeWriterPass(Pass *P)
Check whether a pass is a BitcodeWriterPass.
Definition: BitcodeWriterPass.cpp:83