LLVM  14.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 
28 using WeakInstValueMap =
30 
31 /// Used to track the Debug Info Metadata information.
33  // This maps a function name to its associated DISubprogram.
35  // This maps an instruction and the info about whether it has !dbg attached.
37  // This tracks value (instruction) deletion. If an instruction gets deleted,
38  // WeakVH nulls itself.
40  // Maps variable into dbg users (#dbg values/declares for this variable).
42 };
43 
44 /// Map pass names to a per-pass DebugInfoPerPass instance.
46 
47 namespace llvm {
48 class 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.
59  Module &M, iterator_range<Module::iterator> Functions, StringRef Banner,
60  std::function<bool(DIBuilder &, Function &)> ApplyToMF);
61 
62 /// Strip out all of the metadata and debug info inserted by debugify. If no
63 /// llvm.debugify module-level named metadata is present, this is a no-op.
64 /// Returns true if any change was made.
66 
67 /// Collect original debug information before a pass.
68 ///
69 /// \param M The module to collect debug information from.
70 /// \param Functions A range of functions to collect debug information from.
71 /// \param DIPreservationMap A map to collect the DI metadata.
72 /// \param Banner A prefix string to add to debug/error messages.
73 /// \param NameOfWrappedPass A name of a pass to add to debug/error messages.
75  iterator_range<Module::iterator> Functions,
76  DebugInfoPerPassMap &DIPreservationMap,
77  StringRef Banner, StringRef NameOfWrappedPass);
78 
79 /// Check original debug information after a pass.
80 ///
81 /// \param M The module to collect debug information from.
82 /// \param Functions A range of functions to collect debug information from.
83 /// \param DIPreservationMap A map used to check collected the DI metadata.
84 /// \param Banner A prefix string to add to debug/error messages.
85 /// \param NameOfWrappedPass A name of a pass to add to debug/error messages.
87  iterator_range<Module::iterator> Functions,
88  DebugInfoPerPassMap &DIPreservationMap,
89  StringRef Banner, StringRef NameOfWrappedPass,
90  StringRef OrigDIVerifyBugsReportFilePath);
91 } // namespace llvm
92 
93 /// Used to check whether we track synthetic or original debug info.
95 
98  llvm::StringRef NameOfWrappedPass = "",
99  DebugInfoPerPassMap *DIPreservationMap = nullptr);
102  llvm::StringRef NameOfWrappedPass = "",
103  DebugInfoPerPassMap *DIPreservationMap = nullptr);
104 
105 struct NewPMDebugifyPass : public llvm::PassInfoMixin<NewPMDebugifyPass> {
107 };
108 
109 /// Track how much `debugify` information (in the `synthetic` mode only)
110 /// has been lost.
112  /// Number of missing dbg.values.
113  unsigned NumDbgValuesMissing = 0;
114 
115  /// Number of dbg.values expected.
116  unsigned NumDbgValuesExpected = 0;
117 
118  /// Number of instructions with empty debug locations.
119  unsigned NumDbgLocsMissing = 0;
120 
121  /// Number of instructions expected to have debug locations.
122  unsigned NumDbgLocsExpected = 0;
123 
124  /// Get the ratio of missing/expected dbg.values.
125  float getMissingValueRatio() const {
126  return float(NumDbgValuesMissing) / float(NumDbgLocsExpected);
127  }
128 
129  /// Get the ratio of missing/expected instructions with locations.
130  float getEmptyLocationRatio() const {
131  return float(NumDbgLocsMissing) / float(NumDbgLocsExpected);
132  }
133 };
134 
135 /// Map pass names to a per-pass DebugifyStatistics instance.
137 
139  bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
140  DebugifyStatsMap *StatsMap = nullptr,
142  DebugInfoPerPassMap *DIPreservationMap = nullptr,
143  llvm::StringRef OrigDIVerifyBugsReportFilePath = "");
144 
146  bool Strip = false, llvm::StringRef NameOfWrappedPass = "",
147  DebugifyStatsMap *StatsMap = nullptr,
149  DebugInfoPerPassMap *DIPreservationMap = nullptr,
150  llvm::StringRef OrigDIVerifyBugsReportFilePath = "");
151 
153  : public llvm::PassInfoMixin<NewPMCheckDebugifyPass> {
155 };
156 
157 namespace llvm {
158 void exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map);
159 
162 
164 };
165 
166 /// DebugifyCustomPassManager wraps each pass with the debugify passes if
167 /// needed.
168 /// NOTE: We support legacy custom pass manager only.
169 /// TODO: Add New PM support for custom pass manager.
171  StringRef OrigDIVerifyBugsReportFilePath;
172  DebugifyStatsMap *DIStatsMap = nullptr;
173  DebugInfoPerPassMap *DIPreservationMap = nullptr;
175 
176 public:
178 
179  void add(Pass *P) override {
180  // Wrap each pass with (-check)-debugify passes if requested, making
181  // exceptions for passes which shouldn't see -debugify instrumentation.
182  bool WrapWithDebugify = Mode != DebugifyMode::NoDebugify &&
183  !P->getAsImmutablePass() && !isIRPrintingPass(P) &&
185  if (!WrapWithDebugify) {
186  super::add(P);
187  return;
188  }
189 
190  // Either apply -debugify/-check-debugify before/after each pass and collect
191  // debug info loss statistics, or collect and check original debug info in
192  // the optimizations.
193  PassKind Kind = P->getPassKind();
194  StringRef Name = P->getPassName();
195 
196  // TODO: Implement Debugify for LoopPass.
197  switch (Kind) {
198  case PT_Function:
199  super::add(createDebugifyFunctionPass(Mode, Name, DIPreservationMap));
200  super::add(P);
202  isSyntheticDebugInfo(), Name, DIStatsMap, Mode, DIPreservationMap,
203  OrigDIVerifyBugsReportFilePath));
204  break;
205  case PT_Module:
206  super::add(createDebugifyModulePass(Mode, Name, DIPreservationMap));
207  super::add(P);
209  isSyntheticDebugInfo(), Name, DIStatsMap, Mode, DIPreservationMap,
210  OrigDIVerifyBugsReportFilePath));
211  break;
212  default:
213  super::add(P);
214  break;
215  }
216  }
217 
218  // Used within DebugifyMode::SyntheticDebugInfo mode.
219  void setDIStatsMap(DebugifyStatsMap &StatMap) { DIStatsMap = &StatMap; }
220  // Used within DebugifyMode::OriginalDebugInfo mode.
222  DIPreservationMap = &PerPassMap;
223  }
224  void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath) {
225  OrigDIVerifyBugsReportFilePath = BugsReportFilePath;
226  }
228  return OrigDIVerifyBugsReportFilePath;
229  }
230 
231  void setDebugifyMode(enum DebugifyMode M) { Mode = M; }
232 
233  bool isSyntheticDebugInfo() const {
234  return Mode == DebugifyMode::SyntheticDebugInfo;
235  }
236  bool isOriginalDebugInfoMode() const {
237  return Mode == DebugifyMode::OriginalDebugInfo;
238  }
239 
240  const DebugifyStatsMap &getDebugifyStatsMap() const { return *DIStatsMap; }
241  DebugInfoPerPassMap &getDebugInfoPerPassMap() { return *DIPreservationMap; }
242 };
243 } // namespace llvm
244 
245 #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:155
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
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:94
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:374
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
DebugInfoPerPass::DIVariables
DebugVarMap DIVariables
Definition: Debugify.h:41
MapVector.h
createCheckDebugifyFunctionPass
llvm::FunctionPass * createCheckDebugifyFunctionPass(bool Strip=false, llvm::StringRef NameOfWrappedPass="", DebugifyStatsMap *StatsMap=nullptr, enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, DebugInfoPerPassMap *DIPreservationMap=nullptr, llvm::StringRef OrigDIVerifyBugsReportFilePath="")
llvm::collectDebugInfoMetadata
bool collectDebugInfoMetadata(Module &M, iterator_range< Module::iterator > Functions, DebugInfoPerPassMap &DIPreservationMap, StringRef Banner, StringRef NameOfWrappedPass)
Collect original debug information before a pass.
Definition: Debugify.cpp:284
PIC
PassInstrumentationCallbacks PIC
Definition: PassBuilderBindings.cpp:55
llvm::PassKind
PassKind
Definition: Pass.h:63
llvm::MapVector< llvm::StringRef, const llvm::DISubprogram * >
NewPMDebugifyPass::run
llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
Definition: Debugify.cpp:943
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
DebugifyMode::SyntheticDebugInfo
@ SyntheticDebugInfo
llvm::checkDebugInfoMetadata
bool checkDebugInfoMetadata(Module &M, iterator_range< Module::iterator > Functions, DebugInfoPerPassMap &DIPreservationMap, StringRef Banner, StringRef NameOfWrappedPass, StringRef OrigDIVerifyBugsReportFilePath)
Check original debug information after a pass.
Definition: Debugify.cpp:516
DebugInfoPerPass::DIFunctions
DebugFnMap DIFunctions
Definition: Debugify.h:34
llvm::DebugifyCustomPassManager
DebugifyCustomPassManager wraps each pass with the debugify passes if needed.
Definition: Debugify.h:170
IRPrintingPasses.h
llvm::DebugifyCustomPassManager::isOriginalDebugInfoMode
bool isOriginalDebugInfoMode() const
Definition: Debugify.h:236
llvm::exportDebugifyStats
void exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map)
Definition: Debugify.cpp:903
DebugInfoPerPass::InstToDelete
WeakInstValueMap InstToDelete
Definition: Debugify.h:39
llvm::stripDebugifyMetadata
bool stripDebugifyMetadata(Module &M)
Strip out all of the metadata and debug info inserted by debugify.
Definition: Debugify.cpp:239
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
DebugifyStatistics::getMissingValueRatio
float getMissingValueRatio() const
Get the ratio of missing/expected dbg.values.
Definition: Debugify.h:125
llvm::DebugifyCustomPassManager::getOrigDIVerifyBugsReportFilePath
StringRef getOrigDIVerifyBugsReportFilePath() const
Definition: Debugify.h:227
llvm::DebugifyCustomPassManager::getDebugInfoPerPassMap
DebugInfoPerPassMap & getDebugInfoPerPassMap()
Definition: Debugify.h:241
createDebugifyModulePass
llvm::ModulePass * createDebugifyModulePass(enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, llvm::StringRef NameOfWrappedPass="", DebugInfoPerPassMap *DIPreservationMap=nullptr)
Definition: Debugify.cpp:924
DebugInfoPerPass
Used to track the Debug Info Metadata information.
Definition: Debugify.h:32
llvm::PT_Module
@ PT_Module
Definition: Pass.h:68
llvm::DebugifyEachInstrumentation::StatsMap
DebugifyStatsMap StatsMap
Definition: Debugify.h:161
llvm::DebugifyCustomPassManager::isSyntheticDebugInfo
bool isSyntheticDebugInfo() const
Definition: Debugify.h:233
createCheckDebugifyModulePass
llvm::ModulePass * createCheckDebugifyModulePass(bool Strip=false, llvm::StringRef NameOfWrappedPass="", DebugifyStatsMap *StatsMap=nullptr, enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, DebugInfoPerPassMap *DIPreservationMap=nullptr, llvm::StringRef OrigDIVerifyBugsReportFilePath="")
DebugifyStatistics::NumDbgLocsMissing
unsigned NumDbgLocsMissing
Number of instructions with empty debug locations.
Definition: Debugify.h:119
DebugifyStatistics
Track how much debugify information (in the synthetic mode only) has been lost.
Definition: Debugify.h:111
llvm::DebugifyCustomPassManager::setDIStatsMap
void setDIStatsMap(DebugifyStatsMap &StatMap)
Definition: Debugify.h:219
Mode
SI Whole Quad Mode
Definition: SIWholeQuadMode.cpp:262
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
DebugifyMode::OriginalDebugInfo
@ OriginalDebugInfo
DebugifyStatistics::NumDbgLocsExpected
unsigned NumDbgLocsExpected
Number of instructions expected to have debug locations.
Definition: Debugify.h:122
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
NewPMCheckDebugifyPass
Definition: Debugify.h:152
Module
Machine Check Debug Module
Definition: MachineCheckDebugify.cpp:122
ValueHandle.h
createDebugifyFunctionPass
llvm::FunctionPass * createDebugifyFunctionPass(enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, llvm::StringRef NameOfWrappedPass="", DebugInfoPerPassMap *DIPreservationMap=nullptr)
Definition: Debugify.cpp:934
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
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:130
DebugInfoPerPass::DILocations
DebugInstMap DILocations
Definition: Debugify.h:36
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:973
llvm::DebugifyCustomPassManager::setDIPreservationMap
void setDIPreservationMap(DebugInfoPerPassMap &PerPassMap)
Definition: Debugify.h:221
llvm::legacy::PassManager::add
void add(Pass *P) override
Add a pass to the queue of passes to run.
Definition: LegacyPassManager.cpp:1674
llvm::DebugifyCustomPassManager::setOrigDIVerifyBugsReportFilePath
void setOrigDIVerifyBugsReportFilePath(StringRef BugsReportFilePath)
Definition: Debugify.h:224
NewPMDebugifyPass
Definition: Debugify.h:105
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
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
DebugifyStatistics::NumDbgValuesExpected
unsigned NumDbgValuesExpected
Number of dbg.values expected.
Definition: Debugify.h:116
llvm::DebugifyEachInstrumentation
Definition: Debugify.h:160
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
DebugifyStatistics::NumDbgValuesMissing
unsigned NumDbgValuesMissing
Number of missing dbg.values.
Definition: Debugify.h:113
llvm::DebugifyEachInstrumentation::registerCallbacks
void registerCallbacks(PassInstrumentationCallbacks &PIC)
Definition: Debugify.cpp:987
llvm::DebugifyCustomPassManager::setDebugifyMode
void setDebugifyMode(enum DebugifyMode M)
Definition: Debugify.h:231
llvm::DebugifyCustomPassManager::add
void add(Pass *P) override
Add a pass to the queue of passes to run.
Definition: Debugify.h:179
llvm::DebugifyCustomPassManager::getDebugifyStatsMap
const DebugifyStatsMap & getDebugifyStatsMap() const
Definition: Debugify.h:240
llvm::isBitcodeWriterPass
bool isBitcodeWriterPass(Pass *P)
Check whether a pass is a BitcodeWriterPass.
Definition: BitcodeWriterPass.cpp:84