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 struct NewPMDebugifyPass : public llvm::PassInfoMixin<NewPMDebugifyPass> {
106 };
107 
108 /// Track how much `debugify` information (in the `synthetic` mode only)
109 /// has been lost.
111  /// Number of missing dbg.values.
112  unsigned NumDbgValuesMissing = 0;
113 
114  /// Number of dbg.values expected.
115  unsigned NumDbgValuesExpected = 0;
116 
117  /// Number of instructions with empty debug locations.
118  unsigned NumDbgLocsMissing = 0;
119 
120  /// Number of instructions expected to have debug locations.
121  unsigned NumDbgLocsExpected = 0;
122 
123  /// Get the ratio of missing/expected dbg.values.
124  float getMissingValueRatio() const {
125  return float(NumDbgValuesMissing) / float(NumDbgLocsExpected);
126  }
127 
128  /// Get the ratio of missing/expected instructions with locations.
129  float getEmptyLocationRatio() const {
130  return float(NumDbgLocsMissing) / float(NumDbgLocsExpected);
131  }
132 };
133 
134 /// Map pass names to a per-pass DebugifyStatistics instance.
136 
138  bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
139  DebugifyStatsMap *StatsMap = nullptr,
141  DebugInfoPerPass *DebugInfoBeforePass = nullptr,
142  llvm::StringRef OrigDIVerifyBugsReportFilePath = "");
143 
145  bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
146  DebugifyStatsMap *StatsMap = nullptr,
148  DebugInfoPerPass *DebugInfoBeforePass = nullptr,
149  llvm::StringRef OrigDIVerifyBugsReportFilePath = "");
150 
152  : public llvm::PassInfoMixin<NewPMCheckDebugifyPass> {
154 };
155 
156 namespace llvm {
157 void exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map);
158 
161 
163 };
164 
165 /// DebugifyCustomPassManager wraps each pass with the debugify passes if
166 /// needed.
167 /// NOTE: We support legacy custom pass manager only.
168 /// TODO: Add New PM support for custom pass manager.
170  StringRef OrigDIVerifyBugsReportFilePath;
171  DebugifyStatsMap *DIStatsMap = nullptr;
172  DebugInfoPerPass *DebugInfoBeforePass = nullptr;
174 
175 public:
177 
178  void add(Pass *P) override {
179  // Wrap each pass with (-check)-debugify passes if requested, making
180  // exceptions for passes which shouldn't see -debugify instrumentation.
181  bool WrapWithDebugify = Mode != DebugifyMode::NoDebugify &&
182  !P->getAsImmutablePass() && !isIRPrintingPass(P) &&
184  if (!WrapWithDebugify) {
185  super::add(P);
186  return;
187  }
188 
189  // Either apply -debugify/-check-debugify before/after each pass and collect
190  // debug info loss statistics, or collect and check original debug info in
191  // the optimizations.
192  PassKind Kind = P->getPassKind();
193  StringRef Name = P->getPassName();
194 
195  // TODO: Implement Debugify for LoopPass.
196  switch (Kind) {
197  case PT_Function:
198  super::add(createDebugifyFunctionPass(Mode, Name, DebugInfoBeforePass));
199  super::add(P);
201  isSyntheticDebugInfo(), Name, DIStatsMap, Mode, DebugInfoBeforePass,
202  OrigDIVerifyBugsReportFilePath));
203  break;
204  case PT_Module:
205  super::add(createDebugifyModulePass(Mode, Name, DebugInfoBeforePass));
206  super::add(P);
208  isSyntheticDebugInfo(), Name, DIStatsMap, Mode, DebugInfoBeforePass,
209  OrigDIVerifyBugsReportFilePath));
210  break;
211  default:
212  super::add(P);
213  break;
214  }
215  }
216 
217  // Used within DebugifyMode::SyntheticDebugInfo mode.
218  void setDIStatsMap(DebugifyStatsMap &StatMap) { DIStatsMap = &StatMap; }
219  // Used within DebugifyMode::OriginalDebugInfo mode.
221  DebugInfoBeforePass = &PerPassDI;
222  }
223  void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath) {
224  OrigDIVerifyBugsReportFilePath = BugsReportFilePath;
225  }
227  return OrigDIVerifyBugsReportFilePath;
228  }
229 
230  void setDebugifyMode(enum DebugifyMode M) { Mode = M; }
231 
232  bool isSyntheticDebugInfo() const {
233  return Mode == DebugifyMode::SyntheticDebugInfo;
234  }
235  bool isOriginalDebugInfoMode() const {
236  return Mode == DebugifyMode::OriginalDebugInfo;
237  }
238 
239  const DebugifyStatsMap &getDebugifyStatsMap() const { return *DIStatsMap; }
240  DebugInfoPerPass &getDebugInfoPerPass() { return *DebugInfoBeforePass; }
241 };
242 } // namespace llvm
243 
244 #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::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
MapVector.h
PIC
PassInstrumentationCallbacks PIC
Definition: PassBuilderBindings.cpp:55
llvm::PassKind
PassKind
Definition: Pass.h:63
llvm::MapVector< const llvm::Function *, const llvm::DISubprogram * >
NewPMDebugifyPass::run
llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
Definition: Debugify.cpp:963
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="")
DebugifyMode::SyntheticDebugInfo
@ SyntheticDebugInfo
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:169
llvm::DebugifyCustomPassManager::getDebugInfoPerPass
DebugInfoPerPass & getDebugInfoPerPass()
Definition: Debugify.h:240
IRPrintingPasses.h
llvm::DebugifyCustomPassManager::isOriginalDebugInfoMode
bool isOriginalDebugInfoMode() const
Definition: Debugify.h:235
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:124
llvm::DebugifyCustomPassManager::getOrigDIVerifyBugsReportFilePath
StringRef getOrigDIVerifyBugsReportFilePath() const
Definition: Debugify.h:226
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::DebugifyEachInstrumentation::StatsMap
DebugifyStatsMap StatsMap
Definition: Debugify.h:160
llvm::DebugifyCustomPassManager::isSyntheticDebugInfo
bool isSyntheticDebugInfo() const
Definition: Debugify.h:232
llvm::DebugifyCustomPassManager::setDebugInfoBeforePass
void setDebugInfoBeforePass(DebugInfoPerPass &PerPassDI)
Definition: Debugify.h:220
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:118
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:110
llvm::DebugifyCustomPassManager::setDIStatsMap
void setDIStatsMap(DebugifyStatsMap &StatMap)
Definition: Debugify.h:218
Mode
SI Whole Quad Mode
Definition: SIWholeQuadMode.cpp:262
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:82
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:121
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
NewPMCheckDebugifyPass
Definition: Debugify.h:151
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
ValueHandle.h
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::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:129
DebugInfoPerPass::DILocations
DebugInstMap DILocations
Definition: Debugify.h:38
llvm::legacy::PassManager
PassManager manages ModulePassManagers.
Definition: LegacyPassManager.h:52
DebugifyMode::NoDebugify
@ NoDebugify
PassManager.h
NewPMCheckDebugifyPass::run
llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
Definition: Debugify.cpp:993
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:223
NewPMDebugifyPass
Definition: Debugify.h:104
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="")
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:115
llvm::DebugifyEachInstrumentation
Definition: Debugify.h:159
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
DebugifyStatistics::NumDbgValuesMissing
unsigned NumDbgValuesMissing
Number of missing dbg.values.
Definition: Debugify.h:112
llvm::DebugifyEachInstrumentation::registerCallbacks
void registerCallbacks(PassInstrumentationCallbacks &PIC)
Definition: Debugify.cpp:1007
llvm::DebugifyCustomPassManager::setDebugifyMode
void setDebugifyMode(enum DebugifyMode M)
Definition: Debugify.h:230
llvm::DebugifyCustomPassManager::add
void add(Pass *P) override
Add a pass to the queue of passes to run.
Definition: Debugify.h:178
llvm::DebugifyCustomPassManager::getDebugifyStatsMap
const DebugifyStatsMap & getDebugifyStatsMap() const
Definition: Debugify.h:239
llvm::isBitcodeWriterPass
bool isBitcodeWriterPass(Pass *P)
Check whether a pass is a BitcodeWriterPass.
Definition: BitcodeWriterPass.cpp:83