LLVM  12.0.0git
Debugify.h
Go to the documentation of this file.
1 //===- Debugify.h - Attach synthetic debug info to everything -------------===//
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 debug info testing utility.
10 ///
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_TRANSFORM_UTILS_DEBUGIFY_H
14 #define LLVM_TRANSFORM_UTILS_DEBUGIFY_H
15 
16 #include "llvm/ADT/MapVector.h"
17 #include "llvm/ADT/StringRef.h"
21 #include "llvm/IR/PassManager.h"
22 
23 namespace llvm {
24 class DIBuilder;
25 
26 /// Add synthesized debug information to a module.
27 ///
28 /// \param M The module to add debug information to.
29 /// \param Functions A range of functions to add debug information to.
30 /// \param Banner A prefix string to add to debug/error messages.
31 /// \param ApplyToMF A call back that will add debug information to the
32 /// MachineFunction for a Function. If nullptr, then the
33 /// MachineFunction (if any) will not be modified.
35  Module &M, iterator_range<Module::iterator> Functions, StringRef Banner,
36  std::function<bool(DIBuilder &, Function &)> ApplyToMF);
37 
38 /// Strip out all of the metadata and debug info inserted by debugify. If no
39 /// llvm.debugify module-level named metadata is present, this is a no-op.
40 /// Returns true if any change was made.
42 
45 
46 struct NewPMDebugifyPass : public llvm::PassInfoMixin<NewPMDebugifyPass> {
48 };
49 
50 /// Track how much `debugify` information has been lost.
52  /// Number of missing dbg.values.
53  unsigned NumDbgValuesMissing = 0;
54 
55  /// Number of dbg.values expected.
56  unsigned NumDbgValuesExpected = 0;
57 
58  /// Number of instructions with empty debug locations.
59  unsigned NumDbgLocsMissing = 0;
60 
61  /// Number of instructions expected to have debug locations.
62  unsigned NumDbgLocsExpected = 0;
63 
64  /// Get the ratio of missing/expected dbg.values.
65  float getMissingValueRatio() const {
66  return float(NumDbgValuesMissing) / float(NumDbgLocsExpected);
67  }
68 
69  /// Get the ratio of missing/expected instructions with locations.
70  float getEmptyLocationRatio() const {
71  return float(NumDbgLocsMissing) / float(NumDbgLocsExpected);
72  }
73 };
74 
75 /// Map pass names to a per-pass DebugifyStatistics instance.
77 
78 void exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map);
79 
81 createCheckDebugifyModulePass(bool Strip = false,
82  llvm::StringRef NameOfWrappedPass = "",
83  DebugifyStatsMap *StatsMap = nullptr);
84 
86 createCheckDebugifyFunctionPass(bool Strip = false,
87  llvm::StringRef NameOfWrappedPass = "",
88  DebugifyStatsMap *StatsMap = nullptr);
89 
91  : public llvm::PassInfoMixin<NewPMCheckDebugifyPass> {
93 };
94 
97 
99 };
100 
101 /// DebugifyCustomPassManager wraps each pass with the debugify passes if
102 /// needed.
103 /// NOTE: We support legacy custom pass manager only.
104 /// TODO: Add New PM support for custom pass manager.
106  DebugifyStatsMap DIStatsMap;
107  bool EnableDebugifyEach = false;
108 
109 public:
111 
112  void add(Pass *P) override {
113  // Wrap each pass with (-check)-debugify passes if requested, making
114  // exceptions for passes which shouldn't see -debugify instrumentation.
115  bool WrapWithDebugify = EnableDebugifyEach && !P->getAsImmutablePass() &&
117  if (!WrapWithDebugify) {
118  super::add(P);
119  return;
120  }
121 
122  // Apply -debugify/-check-debugify before/after each pass and collect
123  // debug info loss statistics.
124  PassKind Kind = P->getPassKind();
125  StringRef Name = P->getPassName();
126 
127  // TODO: Implement Debugify for LoopPass.
128  switch (Kind) {
129  case PT_Function:
131  super::add(P);
132  super::add(createCheckDebugifyFunctionPass(true, Name, &DIStatsMap));
133  break;
134  case PT_Module:
136  super::add(P);
137  super::add(createCheckDebugifyModulePass(true, Name, &DIStatsMap));
138  break;
139  default:
140  super::add(P);
141  break;
142  }
143  }
144 
145  void enableDebugifyEach() { EnableDebugifyEach = true; }
146 
147  const DebugifyStatsMap &getDebugifyStatsMap() const { return DIStatsMap; }
148 };
149 } // namespace llvm
150 
151 #endif // LLVM_TRANSFORM_UTILS_DEBUGIFY_H
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
This file provides a bitcode writing pass.
unsigned NumDbgValuesMissing
Number of missing dbg.values.
Definition: Debugify.h:53
DebugifyCustomPassManager wraps each pass with the debugify passes if needed.
Definition: Debugify.h:105
bool stripDebugifyMetadata(Module &M)
Strip out all of the metadata and debug info inserted by debugify.
Definition: Debugify.cpp:214
const DebugifyStatsMap & getDebugifyStatsMap() const
Definition: Debugify.h:147
bool isBitcodeWriterPass(Pass *P)
Check whether a pass is a BitcodeWriterPass.
bool isIRPrintingPass(Pass *P)
Return true if a pass is for IR printing.
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:374
Track how much debugify information has been lost.
Definition: Debugify.h:51
llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
Definition: Debugify.cpp:527
PassKind
Definition: Pass.h:63
PassManager manages ModulePassManagers.
#define P(N)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
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::ModulePass * createCheckDebugifyModulePass(bool Strip=false, llvm::StringRef NameOfWrappedPass="", DebugifyStatsMap *StatsMap=nullptr)
Definition: Debugify.cpp:515
void registerCallbacks(PassInstrumentationCallbacks &PIC)
Definition: Debugify.cpp:541
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::ModulePass * createDebugifyModulePass()
Definition: Debugify.cpp:501
void add(Pass *P) override
Add a pass to the queue of passes to run.
Definition: Debugify.h:112
Machine Check Debug Module
void exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map)
Definition: Debugify.cpp:480
float getEmptyLocationRatio() const
Get the ratio of missing/expected instructions with locations.
Definition: Debugify.h:70
float getMissingValueRatio() const
Get the ratio of missing/expected dbg.values.
Definition: Debugify.h:65
unsigned NumDbgLocsExpected
Number of instructions expected to have debug locations.
Definition: Debugify.h:62
void add(Pass *P) override
Add a pass to the queue of passes to run.
llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
Definition: Debugify.cpp:509
unsigned NumDbgValuesExpected
Number of dbg.values expected.
Definition: Debugify.h:56
llvm::FunctionPass * createDebugifyFunctionPass()
Definition: Debugify.cpp:505
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
This file defines passes to print out IR in various granularities.
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
unsigned NumDbgLocsMissing
Number of instructions with empty debug locations.
Definition: Debugify.h:59
llvm::FunctionPass * createCheckDebugifyFunctionPass(bool Strip=false, llvm::StringRef NameOfWrappedPass="", DebugifyStatsMap *StatsMap=nullptr)
Definition: Debugify.cpp:522