[llvm] [NFC] Move DroppedVariableStats to its own file (PR #120711)

Shubham Sandeep Rastogi via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 20 02:16:43 PST 2024


https://github.com/rastogishubham created https://github.com/llvm/llvm-project/pull/120711

Move DroppedVariableStats code to its own file and change the class to have an extensible design so that we can use it to add dropped statistics to MIR passes and the instruction selector.

Also moved class DroppedVariableStatsIR to its own file.

Reland 2de78815604e9027efd93cac27c517bf732587d2

>From add1994bda0945c9dc51d542b45c2a497c2a1144 Mon Sep 17 00:00:00 2001
From: Shubham Sandeep Rastogi <srastogi22 at apple.com>
Date: Thu, 19 Dec 2024 20:23:12 -0800
Subject: [PATCH] [NFC] Move DroppedVariableStats to its own file

Move DroppedVariableStats code to its own file and change the class to
have an extensible design so that we can use it to add dropped
statistics to MIR passes and the instruction selector.

Also moved class DroppedVariableStatsIR to its own file.

Reland 2de78815604e9027efd93cac27c517bf732587d2
---
 .../llvm/Passes/DroppedVariableStats.h        | 167 +++++++--------
 .../llvm/Passes/DroppedVariableStatsIR.h      | 101 +++++++++
 .../llvm/Passes/StandardInstrumentations.h    |   2 +-
 llvm/lib/Passes/CMakeLists.txt                |   2 +-
 llvm/lib/Passes/DroppedVariableStats.cpp      | 194 ------------------
 llvm/lib/Passes/DroppedVariableStatsIR.cpp    |  91 ++++++++
 llvm/unittests/IR/CMakeLists.txt              |   2 +-
 7 files changed, 279 insertions(+), 280 deletions(-)
 create mode 100644 llvm/include/llvm/Passes/DroppedVariableStatsIR.h
 delete mode 100644 llvm/lib/Passes/DroppedVariableStats.cpp
 create mode 100644 llvm/lib/Passes/DroppedVariableStatsIR.cpp

diff --git a/llvm/include/llvm/Passes/DroppedVariableStats.h b/llvm/include/llvm/Passes/DroppedVariableStats.h
index 4555157c942b51..c4de849ca7554c 100644
--- a/llvm/include/llvm/Passes/DroppedVariableStats.h
+++ b/llvm/include/llvm/Passes/DroppedVariableStats.h
@@ -14,11 +14,9 @@
 #ifndef LLVM_CODEGEN_DROPPEDVARIABLESTATS_H
 #define LLVM_CODEGEN_DROPPEDVARIABLESTATS_H
 
-#include "llvm/CodeGen/MachinePassManager.h"
 #include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/DiagnosticInfo.h"
 #include "llvm/IR/Function.h"
-#include "llvm/IR/Module.h"
 #include "llvm/IR/PassInstrumentation.h"
 
 namespace llvm {
@@ -92,24 +90,75 @@ class DroppedVariableStats {
   void calculateDroppedStatsAndPrint(DebugVariables &DbgVariables,
                                      StringRef FuncName, StringRef PassID,
                                      StringRef FuncOrModName,
-                                     StringRef PassLevel, const Function *Func);
+                                     StringRef PassLevel,
+                                     const Function *Func) {
+    unsigned DroppedCount = 0;
+    DenseSet<VarID> &DebugVariablesBeforeSet =
+        DbgVariables.DebugVariablesBefore;
+    DenseSet<VarID> &DebugVariablesAfterSet = DbgVariables.DebugVariablesAfter;
+    DenseMap<VarID, DILocation *> &InlinedAtsMap = InlinedAts.back()[FuncName];
+    // Find an Instruction that shares the same scope as the dropped #dbg_value
+    // or has a scope that is the child of the scope of the #dbg_value, and has
+    // an inlinedAt equal to the inlinedAt of the #dbg_value or it's inlinedAt
+    // chain contains the inlinedAt of the #dbg_value, if such an Instruction is
+    // found, debug information is dropped.
+    for (VarID Var : DebugVariablesBeforeSet) {
+      if (DebugVariablesAfterSet.contains(Var))
+        continue;
+      visitEveryInstruction(DroppedCount, InlinedAtsMap, Var);
+      removeVarFromAllSets(Var, Func);
+    }
+    if (DroppedCount > 0) {
+      llvm::outs() << PassLevel << ", " << PassID << ", " << DroppedCount
+                   << ", " << FuncOrModName << "\n";
+      PassDroppedVariables = true;
+    } else
+      PassDroppedVariables = false;
+  }
 
   /// Check if a \p Var has been dropped or is a false positive. Also update the
   /// \p DroppedCount if a debug variable is dropped.
   bool updateDroppedCount(DILocation *DbgLoc, const DIScope *Scope,
                           const DIScope *DbgValScope,
                           DenseMap<VarID, DILocation *> &InlinedAtsMap,
-                          VarID Var, unsigned &DroppedCount);
+                          VarID Var, unsigned &DroppedCount) {
+    // If the Scope is a child of, or equal to the DbgValScope and is inlined at
+    // the Var's InlinedAt location, return true to signify that the Var has
+    // been dropped.
+    if (isScopeChildOfOrEqualTo(Scope, DbgValScope))
+      if (isInlinedAtChildOfOrEqualTo(DbgLoc->getInlinedAt(),
+                                      InlinedAtsMap[Var])) {
+        // Found another instruction in the variable's scope, so there exists a
+        // break point at which the variable could be observed. Count it as
+        // dropped.
+        DroppedCount++;
+        return true;
+      }
+    return false;
+  }
   /// Run code to populate relevant data structures over an llvm::Function or
   /// llvm::MachineFunction.
-  void run(DebugVariables &DbgVariables, StringRef FuncName, bool Before);
+  void run(DebugVariables &DbgVariables, StringRef FuncName, bool Before) {
+    auto &VarIDSet = (Before ? DbgVariables.DebugVariablesBefore
+                             : DbgVariables.DebugVariablesAfter);
+    auto &InlinedAtsMap = InlinedAts.back();
+    if (Before)
+      InlinedAtsMap.try_emplace(FuncName, DenseMap<VarID, DILocation *>());
+    VarIDSet = DenseSet<VarID>();
+    visitEveryDebugRecord(VarIDSet, InlinedAtsMap, FuncName, Before);
+  }
   /// Populate the VarIDSet and InlinedAtMap with the relevant information
   /// needed for before and after pass analysis to determine dropped variable
   /// status.
   void populateVarIDSetAndInlinedMap(
       const DILocalVariable *DbgVar, DebugLoc DbgLoc, DenseSet<VarID> &VarIDSet,
       DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
-      StringRef FuncName, bool Before);
+      StringRef FuncName, bool Before) {
+    VarID Key{DbgVar->getScope(), DbgLoc->getInlinedAtScope(), DbgVar};
+    VarIDSet.insert(Key);
+    if (Before)
+      InlinedAtsMap[FuncName].try_emplace(Key, DbgLoc.getInlinedAt());
+  }
   /// Visit every llvm::Instruction or llvm::MachineInstruction and check if the
   /// debug variable denoted by its ID \p Var may have been dropped by an
   /// optimization pass.
@@ -136,87 +185,39 @@ class DroppedVariableStats {
   /// Return true if \p Scope is the same as \p DbgValScope or a child scope of
   /// \p DbgValScope, return false otherwise.
   bool isScopeChildOfOrEqualTo(const DIScope *Scope,
-                               const DIScope *DbgValScope);
+                               const DIScope *DbgValScope) {
+    while (Scope != nullptr) {
+      if (VisitedScope.find(Scope) == VisitedScope.end()) {
+        VisitedScope.insert(Scope);
+        if (Scope == DbgValScope) {
+          VisitedScope.clear();
+          return true;
+        }
+        Scope = Scope->getScope();
+      } else {
+        VisitedScope.clear();
+        return false;
+      }
+    }
+    return false;
+  }
   /// Return true if \p InlinedAt is the same as \p DbgValInlinedAt or part of
   /// the InlinedAt chain, return false otherwise.
   bool isInlinedAtChildOfOrEqualTo(const DILocation *InlinedAt,
-                                   const DILocation *DbgValInlinedAt);
-  bool PassDroppedVariables = false;
-};
-
-/// A class to collect and print dropped debug information due to LLVM IR
-/// optimization passes. After every LLVM IR pass is run, it will print how many
-/// #dbg_values were dropped due to that pass.
-class DroppedVariableStatsIR : public DroppedVariableStats {
-public:
-  DroppedVariableStatsIR(bool DroppedVarStatsEnabled)
-      : llvm::DroppedVariableStats(DroppedVarStatsEnabled) {}
-
-  void runBeforePass(Any IR) {
-    setup();
-    if (const auto *M = unwrapIR<Module>(IR))
-      return this->runOnModule(M, true);
-    if (const auto *F = unwrapIR<Function>(IR))
-      return this->runOnFunction(F, true);
-  }
-
-  void runAfterPass(StringRef P, Any IR) {
-    if (const auto *M = unwrapIR<Module>(IR))
-      runAfterPassModule(P, M);
-    else if (const auto *F = unwrapIR<Function>(IR))
-      runAfterPassFunction(P, F);
-    cleanup();
-  }
-
-  void registerCallbacks(PassInstrumentationCallbacks &PIC);
-
-private:
-  const Function *Func;
-
-  void runAfterPassFunction(StringRef PassID, const Function *F) {
-    runOnFunction(F, false);
-    calculateDroppedVarStatsOnFunction(F, PassID, F->getName().str(),
-                                       "Function");
-  }
-
-  void runAfterPassModule(StringRef PassID, const Module *M) {
-    runOnModule(M, false);
-    calculateDroppedVarStatsOnModule(M, PassID, M->getName().str(), "Module");
-  }
-  /// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
-  /// after a pass has run to facilitate dropped variable calculation for an
-  /// llvm::Function.
-  void runOnFunction(const Function *F, bool Before);
-  /// Iterate over all Instructions in a Function and report any dropped debug
-  /// information.
-  void calculateDroppedVarStatsOnFunction(const Function *F, StringRef PassID,
-                                          StringRef FuncOrModName,
-                                          StringRef PassLevel);
-  /// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
-  /// after a pass has run to facilitate dropped variable calculation for an
-  /// llvm::Module. Calls runOnFunction on every Function in the Module.
-  void runOnModule(const Module *M, bool Before);
-  /// Iterate over all Functions in a Module and report any dropped debug
-  /// information. Will call calculateDroppedVarStatsOnFunction on every
-  /// Function.
-  void calculateDroppedVarStatsOnModule(const Module *M, StringRef PassID,
-                                        StringRef FuncOrModName,
-                                        StringRef PassLevel);
-  /// Override base class method to run on an llvm::Function specifically.
-  virtual void
-  visitEveryInstruction(unsigned &DroppedCount,
-                        DenseMap<VarID, DILocation *> &InlinedAtsMap,
-                        VarID Var) override;
-  /// Override base class method to run on #dbg_values specifically.
-  virtual void visitEveryDebugRecord(
-      DenseSet<VarID> &VarIDSet,
-      DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
-      StringRef FuncName, bool Before) override;
-
-  template <typename IRUnitT> static const IRUnitT *unwrapIR(Any IR) {
-    const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
-    return IRPtr ? *IRPtr : nullptr;
+                                   const DILocation *DbgValInlinedAt) {
+    if (DbgValInlinedAt == InlinedAt)
+      return true;
+    if (!DbgValInlinedAt)
+      return false;
+    auto *IA = InlinedAt;
+    while (IA) {
+      if (IA == DbgValInlinedAt)
+        return true;
+      IA = IA->getInlinedAt();
+    }
+    return false;
   }
+  bool PassDroppedVariables = false;
 };
 
 } // namespace llvm
diff --git a/llvm/include/llvm/Passes/DroppedVariableStatsIR.h b/llvm/include/llvm/Passes/DroppedVariableStatsIR.h
new file mode 100644
index 00000000000000..99701e8c8e1c05
--- /dev/null
+++ b/llvm/include/llvm/Passes/DroppedVariableStatsIR.h
@@ -0,0 +1,101 @@
+///===- DroppedVariableStatsIR.h - Opt Diagnostics -*- C++ -*--------------===//
+///
+/// Part of the LLVM Project, under the Apache License v2.0 with LLVM
+/// Exceptions. See https://llvm.org/LICENSE.txt for license information.
+/// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+///
+///===---------------------------------------------------------------------===//
+/// \file
+/// Dropped Variable Statistics for Debug Information. Reports any number
+/// of #dbg_value that get dropped due to an optimization pass.
+///
+///===---------------------------------------------------------------------===//
+
+#ifndef LLVM_CODEGEN_DROPPEDVARIABLESTATSIR_H
+#define LLVM_CODEGEN_DROPPEDVARIABLESTATSIR_H
+
+#include "llvm/IR/InstIterator.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Passes/DroppedVariableStats.h"
+
+namespace llvm {
+
+/// A class to collect and print dropped debug information due to LLVM IR
+/// optimization passes. After every LLVM IR pass is run, it will print how many
+/// #dbg_values were dropped due to that pass.
+class DroppedVariableStatsIR : public DroppedVariableStats {
+public:
+  DroppedVariableStatsIR(bool DroppedVarStatsEnabled)
+      : llvm::DroppedVariableStats(DroppedVarStatsEnabled) {}
+
+  void runBeforePass(Any IR) {
+    setup();
+    if (const auto *M = unwrapIR<Module>(IR))
+      return this->runOnModule(M, true);
+    if (const auto *F = unwrapIR<Function>(IR))
+      return this->runOnFunction(F, true);
+  }
+
+  void runAfterPass(StringRef P, Any IR) {
+    if (const auto *M = unwrapIR<Module>(IR))
+      runAfterPassModule(P, M);
+    else if (const auto *F = unwrapIR<Function>(IR))
+      runAfterPassFunction(P, F);
+    cleanup();
+  }
+
+  void registerCallbacks(PassInstrumentationCallbacks &PIC);
+
+private:
+  const Function *Func;
+
+  void runAfterPassFunction(StringRef PassID, const Function *F) {
+    runOnFunction(F, false);
+    calculateDroppedVarStatsOnFunction(F, PassID, F->getName().str(),
+                                       "Function");
+  }
+
+  void runAfterPassModule(StringRef PassID, const Module *M) {
+    runOnModule(M, false);
+    calculateDroppedVarStatsOnModule(M, PassID, M->getName().str(), "Module");
+  }
+  /// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
+  /// after a pass has run to facilitate dropped variable calculation for an
+  /// llvm::Function.
+  void runOnFunction(const Function *F, bool Before);
+  /// Iterate over all Instructions in a Function and report any dropped debug
+  /// information.
+  void calculateDroppedVarStatsOnFunction(const Function *F, StringRef PassID,
+                                          StringRef FuncOrModName,
+                                          StringRef PassLevel);
+  /// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
+  /// after a pass has run to facilitate dropped variable calculation for an
+  /// llvm::Module. Calls runOnFunction on every Function in the Module.
+  void runOnModule(const Module *M, bool Before);
+  /// Iterate over all Functions in a Module and report any dropped debug
+  /// information. Will call calculateDroppedVarStatsOnFunction on every
+  /// Function.
+  void calculateDroppedVarStatsOnModule(const Module *M, StringRef PassID,
+                                        StringRef FuncOrModName,
+                                        StringRef PassLevel);
+  /// Override base class method to run on an llvm::Function specifically.
+  virtual void
+  visitEveryInstruction(unsigned &DroppedCount,
+                        DenseMap<VarID, DILocation *> &InlinedAtsMap,
+                        VarID Var) override;
+
+  /// Override base class method to run on #dbg_values specifically.
+  virtual void visitEveryDebugRecord(
+      DenseSet<VarID> &VarIDSet,
+      DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
+      StringRef FuncName, bool Before) override;
+
+  template <typename IRUnitT> static const IRUnitT *unwrapIR(Any IR) {
+    const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
+    return IRPtr ? *IRPtr : nullptr;
+  }
+};
+
+} // namespace llvm
+
+#endif
diff --git a/llvm/include/llvm/Passes/StandardInstrumentations.h b/llvm/include/llvm/Passes/StandardInstrumentations.h
index 6ba466f9269f09..4e62ee9c00dafb 100644
--- a/llvm/include/llvm/Passes/StandardInstrumentations.h
+++ b/llvm/include/llvm/Passes/StandardInstrumentations.h
@@ -25,7 +25,7 @@
 #include "llvm/IR/OptBisect.h"
 #include "llvm/IR/PassTimingInfo.h"
 #include "llvm/IR/ValueHandle.h"
-#include "llvm/Passes/DroppedVariableStats.h"
+#include "llvm/Passes/DroppedVariableStatsIR.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/TimeProfiler.h"
 #include "llvm/Transforms/IPO/SampleProfileProbe.h"
diff --git a/llvm/lib/Passes/CMakeLists.txt b/llvm/lib/Passes/CMakeLists.txt
index 9e16a446c9b399..23799ac4f98f7a 100644
--- a/llvm/lib/Passes/CMakeLists.txt
+++ b/llvm/lib/Passes/CMakeLists.txt
@@ -1,6 +1,6 @@
 add_llvm_component_library(LLVMPasses
   CodeGenPassBuilder.cpp
-  DroppedVariableStats.cpp
+  DroppedVariableStatsIR.cpp
   OptimizationLevel.cpp
   PassBuilder.cpp
   PassBuilderBindings.cpp
diff --git a/llvm/lib/Passes/DroppedVariableStats.cpp b/llvm/lib/Passes/DroppedVariableStats.cpp
deleted file mode 100644
index 5dc6b75fb8ace9..00000000000000
--- a/llvm/lib/Passes/DroppedVariableStats.cpp
+++ /dev/null
@@ -1,194 +0,0 @@
-///===- DroppedVariableStats.cpp ------------------------------------------===//
-///
-/// Part of the LLVM Project, under the Apache License v2.0 with LLVM
-/// Exceptions. See https://llvm.org/LICENSE.txt for license information.
-/// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-///
-///===---------------------------------------------------------------------===//
-/// \file
-/// Dropped Variable Statistics for Debug Information. Reports any number
-/// of #dbg_value that get dropped due to an optimization pass.
-///
-///===---------------------------------------------------------------------===//
-
-#include "llvm/Passes/DroppedVariableStats.h"
-#include "llvm/IR/DebugInfoMetadata.h"
-#include "llvm/IR/InstIterator.h"
-#include "llvm/IR/Module.h"
-
-using namespace llvm;
-
-bool DroppedVariableStats::isScopeChildOfOrEqualTo(const DIScope *Scope,
-                                                   const DIScope *DbgValScope) {
-  while (Scope != nullptr) {
-    if (VisitedScope.find(Scope) == VisitedScope.end()) {
-      VisitedScope.insert(Scope);
-      if (Scope == DbgValScope) {
-        VisitedScope.clear();
-        return true;
-      }
-      Scope = Scope->getScope();
-    } else {
-      VisitedScope.clear();
-      return false;
-    }
-  }
-  return false;
-}
-
-bool DroppedVariableStats::isInlinedAtChildOfOrEqualTo(
-    const DILocation *InlinedAt, const DILocation *DbgValInlinedAt) {
-  if (DbgValInlinedAt == InlinedAt)
-    return true;
-  if (!DbgValInlinedAt)
-    return false;
-  auto *IA = InlinedAt;
-  while (IA) {
-    if (IA == DbgValInlinedAt)
-      return true;
-    IA = IA->getInlinedAt();
-  }
-  return false;
-}
-
-void DroppedVariableStats::calculateDroppedStatsAndPrint(
-    DebugVariables &DbgVariables, StringRef FuncName, StringRef PassID,
-    StringRef FuncOrModName, StringRef PassLevel, const Function *Func) {
-  unsigned DroppedCount = 0;
-  DenseSet<VarID> &DebugVariablesBeforeSet = DbgVariables.DebugVariablesBefore;
-  DenseSet<VarID> &DebugVariablesAfterSet = DbgVariables.DebugVariablesAfter;
-  DenseMap<VarID, DILocation *> &InlinedAtsMap = InlinedAts.back()[FuncName];
-  // Find an Instruction that shares the same scope as the dropped #dbg_value or
-  // has a scope that is the child of the scope of the #dbg_value, and has an
-  // inlinedAt equal to the inlinedAt of the #dbg_value or it's inlinedAt chain
-  // contains the inlinedAt of the #dbg_value, if such an Instruction is found,
-  // debug information is dropped.
-  for (VarID Var : DebugVariablesBeforeSet) {
-    if (DebugVariablesAfterSet.contains(Var))
-      continue;
-    visitEveryInstruction(DroppedCount, InlinedAtsMap, Var);
-    removeVarFromAllSets(Var, Func);
-  }
-  if (DroppedCount > 0) {
-    llvm::outs() << PassLevel << ", " << PassID << ", " << DroppedCount << ", "
-                 << FuncOrModName << "\n";
-    PassDroppedVariables = true;
-  } else
-    PassDroppedVariables = false;
-}
-
-bool DroppedVariableStats::updateDroppedCount(
-    DILocation *DbgLoc, const DIScope *Scope, const DIScope *DbgValScope,
-    DenseMap<VarID, DILocation *> &InlinedAtsMap, VarID Var,
-    unsigned &DroppedCount) {
-
-  // If the Scope is a child of, or equal to the DbgValScope and is inlined at
-  // the Var's InlinedAt location, return true to signify that the Var has been
-  // dropped.
-  if (isScopeChildOfOrEqualTo(Scope, DbgValScope))
-    if (isInlinedAtChildOfOrEqualTo(DbgLoc->getInlinedAt(),
-                                    InlinedAtsMap[Var])) {
-      // Found another instruction in the variable's scope, so there exists a
-      // break point at which the variable could be observed. Count it as
-      // dropped.
-      DroppedCount++;
-      return true;
-    }
-  return false;
-}
-
-void DroppedVariableStats::run(DebugVariables &DbgVariables, StringRef FuncName,
-                               bool Before) {
-  auto &VarIDSet = (Before ? DbgVariables.DebugVariablesBefore
-                           : DbgVariables.DebugVariablesAfter);
-  auto &InlinedAtsMap = InlinedAts.back();
-  if (Before)
-    InlinedAtsMap.try_emplace(FuncName, DenseMap<VarID, DILocation *>());
-  VarIDSet = DenseSet<VarID>();
-  visitEveryDebugRecord(VarIDSet, InlinedAtsMap, FuncName, Before);
-}
-
-void DroppedVariableStats::populateVarIDSetAndInlinedMap(
-    const DILocalVariable *DbgVar, DebugLoc DbgLoc, DenseSet<VarID> &VarIDSet,
-    DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
-    StringRef FuncName, bool Before) {
-  VarID Key{DbgVar->getScope(), DbgLoc->getInlinedAtScope(), DbgVar};
-  VarIDSet.insert(Key);
-  if (Before)
-    InlinedAtsMap[FuncName].try_emplace(Key, DbgLoc.getInlinedAt());
-}
-
-void DroppedVariableStatsIR::runOnFunction(const Function *F, bool Before) {
-  auto &DebugVariables = DebugVariablesStack.back()[F];
-  auto FuncName = F->getName();
-  Func = F;
-  run(DebugVariables, FuncName, Before);
-}
-
-void DroppedVariableStatsIR::calculateDroppedVarStatsOnFunction(
-    const Function *F, StringRef PassID, StringRef FuncOrModName,
-    StringRef PassLevel) {
-  Func = F;
-  StringRef FuncName = F->getName();
-  DebugVariables &DbgVariables = DebugVariablesStack.back()[F];
-  calculateDroppedStatsAndPrint(DbgVariables, FuncName, PassID, FuncOrModName,
-                                PassLevel, Func);
-}
-
-void DroppedVariableStatsIR::runOnModule(const Module *M, bool Before) {
-  for (auto &F : *M)
-    runOnFunction(&F, Before);
-}
-
-void DroppedVariableStatsIR::calculateDroppedVarStatsOnModule(
-    const Module *M, StringRef PassID, StringRef FuncOrModName,
-    StringRef PassLevel) {
-  for (auto &F : *M) {
-    calculateDroppedVarStatsOnFunction(&F, PassID, FuncOrModName, PassLevel);
-  }
-}
-
-void DroppedVariableStatsIR::registerCallbacks(
-    PassInstrumentationCallbacks &PIC) {
-  if (!DroppedVariableStatsEnabled)
-    return;
-
-  PIC.registerBeforeNonSkippedPassCallback(
-      [this](StringRef P, Any IR) { return runBeforePass(IR); });
-  PIC.registerAfterPassCallback(
-      [this](StringRef P, Any IR, const PreservedAnalyses &PA) {
-        return runAfterPass(P, IR);
-      });
-  PIC.registerAfterPassInvalidatedCallback(
-      [this](StringRef P, const PreservedAnalyses &PA) { return cleanup(); });
-}
-
-void DroppedVariableStatsIR::visitEveryInstruction(
-    unsigned &DroppedCount, DenseMap<VarID, DILocation *> &InlinedAtsMap,
-    VarID Var) {
-  const DIScope *DbgValScope = std::get<0>(Var);
-  for (const auto &I : instructions(Func)) {
-    auto *DbgLoc = I.getDebugLoc().get();
-    if (!DbgLoc)
-      continue;
-    if (updateDroppedCount(DbgLoc, DbgLoc->getScope(), DbgValScope,
-                           InlinedAtsMap, Var, DroppedCount))
-      break;
-  }
-}
-
-void DroppedVariableStatsIR::visitEveryDebugRecord(
-    DenseSet<VarID> &VarIDSet,
-    DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
-    StringRef FuncName, bool Before) {
-  for (const auto &I : instructions(Func)) {
-    for (DbgRecord &DR : I.getDbgRecordRange()) {
-      if (auto *Dbg = dyn_cast<DbgVariableRecord>(&DR)) {
-        auto *DbgVar = Dbg->getVariable();
-        auto DbgLoc = DR.getDebugLoc();
-        populateVarIDSetAndInlinedMap(DbgVar, DbgLoc, VarIDSet, InlinedAtsMap,
-                                      FuncName, Before);
-      }
-    }
-  }
-}
diff --git a/llvm/lib/Passes/DroppedVariableStatsIR.cpp b/llvm/lib/Passes/DroppedVariableStatsIR.cpp
new file mode 100644
index 00000000000000..496a47e71182e9
--- /dev/null
+++ b/llvm/lib/Passes/DroppedVariableStatsIR.cpp
@@ -0,0 +1,91 @@
+///===- DroppedVariableStatsIR.cpp ----------------------------------------===//
+///
+/// Part of the LLVM Project, under the Apache License v2.0 with LLVM
+/// Exceptions. See https://llvm.org/LICENSE.txt for license information.
+/// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+///
+///===---------------------------------------------------------------------===//
+/// \file
+/// Dropped Variable Statistics for Debug Information. Reports any number
+/// of #dbg_value that get dropped due to an optimization pass.
+///
+///===---------------------------------------------------------------------===//
+
+#include "llvm/Passes/DroppedVariableStatsIR.h"
+
+using namespace llvm;
+
+void DroppedVariableStatsIR::runOnFunction(const Function *F, bool Before) {
+  auto &DebugVariables = DebugVariablesStack.back()[F];
+  auto FuncName = F->getName();
+  Func = F;
+  run(DebugVariables, FuncName, Before);
+}
+
+void DroppedVariableStatsIR::calculateDroppedVarStatsOnFunction(
+    const Function *F, StringRef PassID, StringRef FuncOrModName,
+    StringRef PassLevel) {
+  Func = F;
+  StringRef FuncName = F->getName();
+  DebugVariables &DbgVariables = DebugVariablesStack.back()[F];
+  calculateDroppedStatsAndPrint(DbgVariables, FuncName, PassID, FuncOrModName,
+                                PassLevel, Func);
+}
+
+void DroppedVariableStatsIR::runOnModule(const Module *M, bool Before) {
+  for (auto &F : *M)
+    runOnFunction(&F, Before);
+}
+
+void DroppedVariableStatsIR::calculateDroppedVarStatsOnModule(
+    const Module *M, StringRef PassID, StringRef FuncOrModName,
+    StringRef PassLevel) {
+  for (auto &F : *M) {
+    calculateDroppedVarStatsOnFunction(&F, PassID, FuncOrModName, PassLevel);
+  }
+}
+
+void DroppedVariableStatsIR::registerCallbacks(
+    PassInstrumentationCallbacks &PIC) {
+  if (!DroppedVariableStatsEnabled)
+    return;
+
+  PIC.registerBeforeNonSkippedPassCallback(
+      [this](StringRef P, Any IR) { return runBeforePass(IR); });
+  PIC.registerAfterPassCallback(
+      [this](StringRef P, Any IR, const PreservedAnalyses &PA) {
+        return runAfterPass(P, IR);
+      });
+  PIC.registerAfterPassInvalidatedCallback(
+      [this](StringRef P, const PreservedAnalyses &PA) { return cleanup(); });
+}
+
+void DroppedVariableStatsIR::visitEveryInstruction(
+    unsigned &DroppedCount, DenseMap<VarID, DILocation *> &InlinedAtsMap,
+    VarID Var) {
+  const DIScope *DbgValScope = std::get<0>(Var);
+  for (const auto &I : instructions(Func)) {
+    auto *DbgLoc = I.getDebugLoc().get();
+    if (!DbgLoc)
+      continue;
+    if (updateDroppedCount(DbgLoc, DbgLoc->getScope(), DbgValScope,
+                           InlinedAtsMap, Var, DroppedCount))
+      break;
+  }
+}
+
+void DroppedVariableStatsIR::visitEveryDebugRecord(
+    DenseSet<VarID> &VarIDSet,
+    DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
+    StringRef FuncName, bool Before) {
+  for (const auto &I : instructions(Func)) {
+    for (DbgRecord &DR : I.getDbgRecordRange()) {
+      if (auto *Dbg = dyn_cast<DbgVariableRecord>(&DR)) {
+        auto *DbgVar = Dbg->getVariable();
+        auto DbgLoc = DR.getDebugLoc();
+        populateVarIDSetAndInlinedMap(DbgVar, DbgLoc, VarIDSet, InlinedAtsMap,
+                                      FuncName, Before);
+      }
+    }
+  }
+}
diff --git a/llvm/unittests/IR/CMakeLists.txt b/llvm/unittests/IR/CMakeLists.txt
index 01f02bf5d70ac1..bea6b1b46f5732 100644
--- a/llvm/unittests/IR/CMakeLists.txt
+++ b/llvm/unittests/IR/CMakeLists.txt
@@ -26,6 +26,7 @@ add_llvm_unittest(IRTests
   DemandedBitsTest.cpp
   DominatorTreeTest.cpp
   DominatorTreeBatchUpdatesTest.cpp
+  DroppedVariableStatsIRTest.cpp
   FunctionTest.cpp
   PassBuilderCallbacksTest.cpp
   IRBuilderTest.cpp
@@ -43,7 +44,6 @@ add_llvm_unittest(IRTests
   ShuffleVectorInstTest.cpp
   StructuralHashTest.cpp
   TimePassesTest.cpp
-  DroppedVariableStatsIRTest.cpp
   TypesTest.cpp
   UseTest.cpp
   UserTest.cpp



More information about the llvm-commits mailing list