[llvm] Move DroppedVariableStats to CodeGen lib (PR #120650)

Shubham Sandeep Rastogi via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 19 15:39:40 PST 2024


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

To get Dropped variable statistics for MIR, we need to move the base class DroppedVariableStats code to the CodeGen library because we cannot have CodeGen link against Passes.

Also moved the code for the virtual functions to the header because clang/lib/CodeGen doesn't link against llvm/lib/CodeGen however it does link against Passes which contains the `class StandardInstrumentations` code but not the definition for the virtual functions leading to the error about not finding vtable for `class DroppedVariableStatsIR`

>From 5dfa8afa8624b3c715b5c286eec5df4ef56853d6 Mon Sep 17 00:00:00 2001
From: Shubham Sandeep Rastogi <srastogi22 at apple.com>
Date: Thu, 19 Dec 2024 15:33:20 -0800
Subject: [PATCH] Move DroppedVariableStats to CodeGen lib

To get Dropped variable statistics for MIR, we need to move the base
class DroppedVariableStats code to the CodeGen library because we
cannot have CodeGen link against Passes.
---
 .../DroppedVariableStats.h                    | 26 +++++++++++++--
 .../llvm/Passes/StandardInstrumentations.h    |  2 +-
 llvm/lib/CodeGen/CMakeLists.txt               |  1 +
 .../DroppedVariableStats.cpp                  | 32 +------------------
 llvm/lib/Passes/CMakeLists.txt                |  1 -
 llvm/unittests/CodeGen/CMakeLists.txt         |  1 +
 .../DroppedVariableStatsIRTest.cpp            |  0
 llvm/unittests/IR/CMakeLists.txt              |  1 -
 8 files changed, 28 insertions(+), 36 deletions(-)
 rename llvm/include/llvm/{Passes => CodeGen}/DroppedVariableStats.h (91%)
 rename llvm/lib/{Passes => CodeGen}/DroppedVariableStats.cpp (84%)
 rename llvm/unittests/{IR => CodeGen}/DroppedVariableStatsIRTest.cpp (100%)

diff --git a/llvm/include/llvm/Passes/DroppedVariableStats.h b/llvm/include/llvm/CodeGen/DroppedVariableStats.h
similarity index 91%
rename from llvm/include/llvm/Passes/DroppedVariableStats.h
rename to llvm/include/llvm/CodeGen/DroppedVariableStats.h
index 4555157c942b51..8986da53284e87 100644
--- a/llvm/include/llvm/Passes/DroppedVariableStats.h
+++ b/llvm/include/llvm/CodeGen/DroppedVariableStats.h
@@ -18,6 +18,7 @@
 #include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/DiagnosticInfo.h"
 #include "llvm/IR/Function.h"
+#include "llvm/IR/InstIterator.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/PassInstrumentation.h"
 
@@ -206,12 +207,33 @@ class DroppedVariableStatsIR : public DroppedVariableStats {
   virtual void
   visitEveryInstruction(unsigned &DroppedCount,
                         DenseMap<VarID, DILocation *> &InlinedAtsMap,
-                        VarID Var) override;
+                        VarID Var) override {
+    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;
+    }
+  }
   /// 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;
+      StringRef FuncName, bool Before) override {
+    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);
+        }
+      }
+    }
+  }
 
   template <typename IRUnitT> static const IRUnitT *unwrapIR(Any IR) {
     const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
diff --git a/llvm/include/llvm/Passes/StandardInstrumentations.h b/llvm/include/llvm/Passes/StandardInstrumentations.h
index 6ba466f9269f09..12a34c099eaffe 100644
--- a/llvm/include/llvm/Passes/StandardInstrumentations.h
+++ b/llvm/include/llvm/Passes/StandardInstrumentations.h
@@ -19,13 +19,13 @@
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSet.h"
+#include "llvm/CodeGen/DroppedVariableStats.h"
 #include "llvm/CodeGen/MachineBasicBlock.h"
 #include "llvm/IR/BasicBlock.h"
 #include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/OptBisect.h"
 #include "llvm/IR/PassTimingInfo.h"
 #include "llvm/IR/ValueHandle.h"
-#include "llvm/Passes/DroppedVariableStats.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/TimeProfiler.h"
 #include "llvm/Transforms/IPO/SampleProfileProbe.h"
diff --git a/llvm/lib/CodeGen/CMakeLists.txt b/llvm/lib/CodeGen/CMakeLists.txt
index 145fd2fac8b564..11d8ebf869344b 100644
--- a/llvm/lib/CodeGen/CMakeLists.txt
+++ b/llvm/lib/CodeGen/CMakeLists.txt
@@ -50,6 +50,7 @@ add_llvm_component_library(LLVMCodeGen
   DeadMachineInstructionElim.cpp
   DetectDeadLanes.cpp
   DFAPacketizer.cpp
+  DroppedVariableStats.cpp
   DwarfEHPrepare.cpp
   EarlyIfConversion.cpp
   EdgeBundles.cpp
diff --git a/llvm/lib/Passes/DroppedVariableStats.cpp b/llvm/lib/CodeGen/DroppedVariableStats.cpp
similarity index 84%
rename from llvm/lib/Passes/DroppedVariableStats.cpp
rename to llvm/lib/CodeGen/DroppedVariableStats.cpp
index 5dc6b75fb8ace9..ef6abad802e4df 100644
--- a/llvm/lib/Passes/DroppedVariableStats.cpp
+++ b/llvm/lib/CodeGen/DroppedVariableStats.cpp
@@ -11,7 +11,7 @@
 ///
 ///===---------------------------------------------------------------------===//
 
-#include "llvm/Passes/DroppedVariableStats.h"
+#include "llvm/CodeGen/DroppedVariableStats.h"
 #include "llvm/IR/DebugInfoMetadata.h"
 #include "llvm/IR/InstIterator.h"
 #include "llvm/IR/Module.h"
@@ -162,33 +162,3 @@ void DroppedVariableStatsIR::registerCallbacks(
   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/CMakeLists.txt b/llvm/lib/Passes/CMakeLists.txt
index 9e16a446c9b399..6425f4934b2103 100644
--- a/llvm/lib/Passes/CMakeLists.txt
+++ b/llvm/lib/Passes/CMakeLists.txt
@@ -1,6 +1,5 @@
 add_llvm_component_library(LLVMPasses
   CodeGenPassBuilder.cpp
-  DroppedVariableStats.cpp
   OptimizationLevel.cpp
   PassBuilder.cpp
   PassBuilderBindings.cpp
diff --git a/llvm/unittests/CodeGen/CMakeLists.txt b/llvm/unittests/CodeGen/CMakeLists.txt
index 963cdcc0275e16..807fd1a9b7b568 100644
--- a/llvm/unittests/CodeGen/CMakeLists.txt
+++ b/llvm/unittests/CodeGen/CMakeLists.txt
@@ -27,6 +27,7 @@ add_llvm_unittest(CodeGenTests
   CCStateTest.cpp
   DIEHashTest.cpp
   DIETest.cpp
+  DroppedVariableStatsIRTest.cpp
   DwarfStringPoolEntryRefTest.cpp
   InstrRefLDVTest.cpp
   LowLevelTypeTest.cpp
diff --git a/llvm/unittests/IR/DroppedVariableStatsIRTest.cpp b/llvm/unittests/CodeGen/DroppedVariableStatsIRTest.cpp
similarity index 100%
rename from llvm/unittests/IR/DroppedVariableStatsIRTest.cpp
rename to llvm/unittests/CodeGen/DroppedVariableStatsIRTest.cpp
diff --git a/llvm/unittests/IR/CMakeLists.txt b/llvm/unittests/IR/CMakeLists.txt
index 01f02bf5d70ac1..441ef271b20e12 100644
--- a/llvm/unittests/IR/CMakeLists.txt
+++ b/llvm/unittests/IR/CMakeLists.txt
@@ -43,7 +43,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