|
| 1 | +///===- DroppedVariableStats.h - Opt Diagnostics -*- C++ -*----------------===// |
| 2 | +/// |
| 3 | +/// Part of the LLVM Project, under the Apache License v2.0 with LLVM |
| 4 | +/// Exceptions. See https://llvm.org/LICENSE.txt for license information. |
| 5 | +/// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +/// |
| 7 | +///===---------------------------------------------------------------------===// |
| 8 | +/// \file |
| 9 | +/// Dropped Variable Statistics for Debug Information. Reports any number |
| 10 | +/// of #dbg_value that get dropped due to an optimization pass. |
| 11 | +/// |
| 12 | +///===---------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#ifndef LLVM_CODEGEN_DROPPEDVARIABLESTATS_H |
| 15 | +#define LLVM_CODEGEN_DROPPEDVARIABLESTATS_H |
| 16 | + |
| 17 | +#include "llvm/CodeGen/MachinePassManager.h" |
| 18 | +#include "llvm/IR/DebugInfoMetadata.h" |
| 19 | +#include "llvm/IR/DiagnosticInfo.h" |
| 20 | +#include "llvm/IR/Function.h" |
| 21 | +#include "llvm/IR/Module.h" |
| 22 | +#include "llvm/IR/PassInstrumentation.h" |
| 23 | + |
| 24 | +namespace llvm { |
| 25 | + |
| 26 | +/// A unique key that represents a #dbg_value. |
| 27 | +using VarID = |
| 28 | + std::tuple<const DIScope *, const DIScope *, const DILocalVariable *>; |
| 29 | + |
| 30 | +/// A base class to collect and print dropped debug information variable |
| 31 | +/// statistics. |
| 32 | +class DroppedVariableStats { |
| 33 | +public: |
| 34 | + DroppedVariableStats(bool DroppedVarStatsEnabled) |
| 35 | + : DroppedVariableStatsEnabled(DroppedVarStatsEnabled) { |
| 36 | + if (DroppedVarStatsEnabled) |
| 37 | + llvm::outs() |
| 38 | + << "Pass Level, Pass Name, Num of Dropped Variables, Func or " |
| 39 | + "Module Name\n"; |
| 40 | + }; |
| 41 | + // We intend this to be unique per-compilation, thus no copies. |
| 42 | + DroppedVariableStats(const DroppedVariableStats &) = delete; |
| 43 | + void operator=(const DroppedVariableStats &) = delete; |
| 44 | + |
| 45 | + void setup() { |
| 46 | + DebugVariablesStack.push_back( |
| 47 | + {DenseMap<const Function *, DebugVariables>()}); |
| 48 | + InlinedAts.push_back( |
| 49 | + {DenseMap<StringRef, DenseMap<VarID, DILocation *>>()}); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + void cleanup() { |
| 54 | + DebugVariablesStack.pop_back(); |
| 55 | + InlinedAts.pop_back(); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + bool getPassDroppedVariables() { return PassDroppedVariables; } |
| 60 | + |
| 61 | +protected: |
| 62 | + bool PassDroppedVariables = false; |
| 63 | + bool DroppedVariableStatsEnabled = false; |
| 64 | + |
| 65 | + struct DebugVariables { |
| 66 | + /// DenseSet of VarIDs before an optimization pass has run. |
| 67 | + DenseSet<VarID> DebugVariablesBefore; |
| 68 | + /// DenseSet of VarIDs after an optimization pass has run. |
| 69 | + DenseSet<VarID> DebugVariablesAfter; |
| 70 | + }; |
| 71 | + |
| 72 | + /// A stack of a DenseMap, that maps DebugVariables for every pass to an |
| 73 | + /// llvm::Function. A stack is used because an optimization pass can call |
| 74 | + /// other passes. |
| 75 | + SmallVector<DenseMap<const Function *, DebugVariables>> DebugVariablesStack; |
| 76 | + |
| 77 | + /// A DenseSet tracking whether a scope was visited before. |
| 78 | + DenseSet<const DIScope *> VisitedScope; |
| 79 | + /// A stack of DenseMaps, which map the name of an llvm::Function to a |
| 80 | + /// DenseMap of VarIDs and their inlinedAt locations before an optimization |
| 81 | + /// pass has run. |
| 82 | + SmallVector<DenseMap<StringRef, DenseMap<VarID, DILocation *>>> InlinedAts; |
| 83 | + /// Remove a dropped #dbg_value VarID from all Sets in the |
| 84 | + /// DroppedVariablesBefore stack. |
| 85 | + void removeVarFromAllSets(VarID Var, const Function *F) { |
| 86 | + // Do not remove Var from the last element, it will be popped from the |
| 87 | + // stack. |
| 88 | + for (auto &DebugVariablesMap : llvm::drop_end(DebugVariablesStack)) |
| 89 | + DebugVariablesMap[F].DebugVariablesBefore.erase(Var); |
| 90 | + } |
| 91 | + /// Return true if \p Scope is the same as \p DbgValScope or a child scope of |
| 92 | + /// \p DbgValScope, return false otherwise. |
| 93 | + bool isScopeChildOfOrEqualTo(DIScope *Scope, const DIScope *DbgValScope); |
| 94 | + /// Return true if \p InlinedAt is the same as \p DbgValInlinedAt or part of |
| 95 | + /// the InlinedAt chain, return false otherwise. |
| 96 | + bool isInlinedAtChildOfOrEqualTo(const DILocation *InlinedAt, |
| 97 | + const DILocation *DbgValInlinedAt); |
| 98 | +}; |
| 99 | + |
| 100 | +/// A class to collect and print dropped debug information due to LLVM IR |
| 101 | +/// optimization passes. After every LLVM IR pass is run, it will print how many |
| 102 | +/// #dbg_values were dropped due to that pass. |
| 103 | +class DroppedVariableStatsIR : public DroppedVariableStats { |
| 104 | +public: |
| 105 | + DroppedVariableStatsIR(bool DroppedVarStatsEnabled) |
| 106 | + : llvm::DroppedVariableStats(DroppedVarStatsEnabled) {} |
| 107 | + |
| 108 | + void runBeforePass(Any IR) { |
| 109 | + setup(); |
| 110 | + if (const auto *M = unwrapIR<Module>(IR)) |
| 111 | + return this->runOnModule(M, true); |
| 112 | + if (const auto *F = unwrapIR<Function>(IR)) |
| 113 | + return this->runOnFunction(F, true); |
| 114 | + } |
| 115 | + |
| 116 | + void runAfterPass(StringRef P, Any IR) { |
| 117 | + if (const auto *M = unwrapIR<Module>(IR)) |
| 118 | + runAfterPassModule(P, M); |
| 119 | + else if (const auto *F = unwrapIR<Function>(IR)) |
| 120 | + runAfterPassFunction(P, F); |
| 121 | + return cleanup(); |
| 122 | + } |
| 123 | + |
| 124 | + void registerCallbacks(PassInstrumentationCallbacks &PIC); |
| 125 | + |
| 126 | +private: |
| 127 | + void runAfterPassFunction(StringRef PassID, const Function *F) { |
| 128 | + runOnFunction(F, false); |
| 129 | + calculateDroppedVarStatsOnFunction(F, PassID, F->getName().str(), |
| 130 | + "Function"); |
| 131 | + } |
| 132 | + |
| 133 | + void runAfterPassModule(StringRef PassID, const Module *M) { |
| 134 | + runOnModule(M, false); |
| 135 | + calculateDroppedVarStatsOnModule(M, PassID, M->getName().str(), "Module"); |
| 136 | + } |
| 137 | + /// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or |
| 138 | + /// after a pass has run to facilitate dropped variable calculation for an |
| 139 | + /// llvm::Function. |
| 140 | + void runOnFunction(const Function *F, bool Before); |
| 141 | + /// Iterate over all Instructions in a Function and report any dropped debug |
| 142 | + /// information. |
| 143 | + void calculateDroppedVarStatsOnFunction(const Function *F, StringRef PassID, |
| 144 | + std::string FuncOrModName, |
| 145 | + std::string PassLevel); |
| 146 | + /// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or |
| 147 | + /// after a pass has run to facilitate dropped variable calculation for an |
| 148 | + /// llvm::Module. Calls runOnFunction on every Function in the Module. |
| 149 | + void runOnModule(const Module *M, bool Before); |
| 150 | + /// Iterate over all Functions in a Module and report any dropped debug |
| 151 | + /// information. Will call calculateDroppedVarStatsOnFunction on every |
| 152 | + /// Function. |
| 153 | + void calculateDroppedVarStatsOnModule(const Module *M, StringRef PassID, |
| 154 | + std::string FuncOrModName, |
| 155 | + std::string PassLevel); |
| 156 | + |
| 157 | + template <typename IRUnitT> static const IRUnitT *unwrapIR(Any IR) { |
| 158 | + const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR); |
| 159 | + return IRPtr ? *IRPtr : nullptr; |
| 160 | + } |
| 161 | +}; |
| 162 | + |
| 163 | +} // namespace llvm |
| 164 | + |
| 165 | +#endif |
0 commit comments