|
| 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 class to collect and print dropped debug information variable statistics. |
| 27 | +/// After every LLVM IR pass is run, it will print how many #dbg_values were |
| 28 | +/// dropped due to that pass. |
| 29 | +class DroppedVariableStats { |
| 30 | +public: |
| 31 | + DroppedVariableStats(bool DroppedVarStatsEnabled) |
| 32 | + : DroppedVariableStatsEnabled(DroppedVarStatsEnabled) { |
| 33 | + if (DroppedVarStatsEnabled) |
| 34 | + llvm::outs() |
| 35 | + << "Pass Level, Pass Name, Num of Dropped Variables, Func or " |
| 36 | + "Module Name\n"; |
| 37 | + }; |
| 38 | + // We intend this to be unique per-compilation, thus no copies. |
| 39 | + DroppedVariableStats(const DroppedVariableStats &) = delete; |
| 40 | + void operator=(const DroppedVariableStats &) = delete; |
| 41 | + |
| 42 | + void registerCallbacks(PassInstrumentationCallbacks &PIC); |
| 43 | + void runBeforePass(StringRef PassID, Any IR); |
| 44 | + void runAfterPass(StringRef PassID, Any IR, const PreservedAnalyses &PA); |
| 45 | + void runAfterPassInvalidated(StringRef PassID, const PreservedAnalyses &PA); |
| 46 | + bool getPassDroppedVariables() { return PassDroppedVariables; } |
| 47 | + |
| 48 | +private: |
| 49 | + bool PassDroppedVariables = false; |
| 50 | + bool DroppedVariableStatsEnabled = false; |
| 51 | + /// A unique key that represents a #dbg_value. |
| 52 | + using VarID = |
| 53 | + std::tuple<const DIScope *, const DIScope *, const DILocalVariable *>; |
| 54 | + |
| 55 | + struct DebugVariables { |
| 56 | + /// DenseSet of VarIDs before an optimization pass has run. |
| 57 | + DenseSet<VarID> DebugVariablesBefore; |
| 58 | + /// DenseSet of VarIDs after an optimization pass has run. |
| 59 | + DenseSet<VarID> DebugVariablesAfter; |
| 60 | + }; |
| 61 | + |
| 62 | + /// A stack of a DenseMap, that maps DebugVariables for every pass to an |
| 63 | + /// llvm::Function. A stack is used because an optimization pass can call |
| 64 | + /// other passes. |
| 65 | + SmallVector<DenseMap<const Function *, DebugVariables>> DebugVariablesStack; |
| 66 | + |
| 67 | + /// A DenseSet tracking whether a scope was visited before. |
| 68 | + DenseSet<const DIScope *> VisitedScope; |
| 69 | + /// A stack of DenseMaps, which map the name of an llvm::Function to a |
| 70 | + /// DenseMap of VarIDs and their inlinedAt locations before an optimization |
| 71 | + /// pass has run. |
| 72 | + SmallVector<DenseMap<StringRef, DenseMap<VarID, DILocation *>>> InlinedAts; |
| 73 | + |
| 74 | + /// Iterate over all Functions in a Module and report any dropped debug |
| 75 | + /// information. Will call calculateDroppedVarStatsOnFunction on every |
| 76 | + /// Function. |
| 77 | + void calculateDroppedVarStatsOnModule(const Module *M, StringRef PassID, |
| 78 | + std::string FuncOrModName, |
| 79 | + std::string PassLevel); |
| 80 | + /// Iterate over all Instructions in a Function and report any dropped debug |
| 81 | + /// information. |
| 82 | + void calculateDroppedVarStatsOnFunction(const Function *F, StringRef PassID, |
| 83 | + std::string FuncOrModName, |
| 84 | + std::string PassLevel); |
| 85 | + /// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or |
| 86 | + /// after a pass has run to facilitate dropped variable calculation for an |
| 87 | + /// llvm::Function. |
| 88 | + void runOnFunction(const Function *F, bool Before); |
| 89 | + /// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or |
| 90 | + /// after a pass has run to facilitate dropped variable calculation for an |
| 91 | + /// llvm::Module. Calls runOnFunction on every Function in the Module. |
| 92 | + void runOnModule(const Module *M, bool Before); |
| 93 | + /// Remove a dropped #dbg_value VarID from all Sets in the |
| 94 | + /// DroppedVariablesBefore stack. |
| 95 | + void removeVarFromAllSets(VarID Var, const Function *F); |
| 96 | + /// Return true if \p Scope is the same as \p DbgValScope or a child scope of |
| 97 | + /// \p DbgValScope, return false otherwise. |
| 98 | + bool isScopeChildOfOrEqualTo(DIScope *Scope, const DIScope *DbgValScope); |
| 99 | + /// Return true if \p InlinedAt is the same as \p DbgValInlinedAt or part of |
| 100 | + /// the InlinedAt chain, return false otherwise. |
| 101 | + bool isInlinedAtChildOfOrEqualTo(const DILocation *InlinedAt, |
| 102 | + const DILocation *DbgValInlinedAt); |
| 103 | + template <typename IRUnitT> static const IRUnitT *unwrapIR(Any IR) { |
| 104 | + const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR); |
| 105 | + return IRPtr ? *IRPtr : nullptr; |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +} // namespace llvm |
| 110 | + |
| 111 | +#endif |
0 commit comments