[llvm] Add a pass to collect dropped variable statistics (PR #102233)

Adrian Prantl via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 16 13:27:36 PDT 2024


================
@@ -578,6 +579,74 @@ class PrintCrashIRInstrumentation {
   static void SignalHandler(void *);
 };
 
+class DroppedVariableStats {
+public:
+  DroppedVariableStats(bool DroppedVarStatsEnabled) {
+    if (DroppedVarStatsEnabled)
+      llvm::outs()
+          << "Pass Level, Pass Name, Num of Dropped Variables, Func or "
+             "Module Name\n";
+  };
+  // We intend this to be unique per-compilation, thus no copies.
+  DroppedVariableStats(const DroppedVariableStats &) = delete;
+  void operator=(const DroppedVariableStats &) = delete;
+
+  void registerCallbacks(PassInstrumentationCallbacks &PIC);
+
+  void runBeforePass(StringRef PassID, Any IR);
+  void runAfterPass(StringRef PassID, Any IR, const PreservedAnalyses &PA);
+  void runAfterPassInvalidated(StringRef PassID, const PreservedAnalyses &PA);
+  bool getPassDroppedVariables() { return PassDroppedVariables; }
+
+private:
+  bool PassDroppedVariables = false;
+  /// VarID is a unique key that represents a #dbg_value
+  using VarID =
+      std::tuple<const DIScope *, const DIScope *, const DILocalVariable *>;
+  /// A stack of DenseMaps, which map the name of an llvm::Function to a
+  /// DenseSet of VarIDs before an optimization pass has run.
+  SmallVector<DenseMap<StringRef, DenseSet<VarID>>> DebugVariablesBefore;
+  /// A stack of DenseMaps, which map the name of an llvm::Function to a
+  /// DenseSet of VarIDs after an optimization pass has run.
+  SmallVector<DenseMap<StringRef, DenseSet<VarID>>> DebugVariablesAfter;
----------------
adrian-prantl wrote:

It seems unnecessary to have two separate identical stacks here, maybe have one stack with a struct containing the other sets?

https://github.com/llvm/llvm-project/pull/102233


More information about the llvm-commits mailing list