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

Felipe de Azevedo Piovezan via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 8 09:33:10 PDT 2024


================
@@ -2441,6 +2447,84 @@ void DotCfgChangeReporter::registerCallbacks(
   }
 }
 
+void DroppedVariableStats::registerCallbacks(
+    PassInstrumentationCallbacks &PIC) {
+  if (!DroppedVarStats)
+    return;
+
+  PIC.registerBeforeNonSkippedPassCallback(
+      [this](StringRef P, Any IR) { return this->runBeforePass(P, IR); });
+  PIC.registerAfterPassCallback(
+      [this](StringRef P, Any IR, const PreservedAnalyses &PA) {
+        return this->runAfterPass(P, IR, PA);
+      });
+}
+
+void DroppedVariableStats::runBeforePass(StringRef PassID, Any IR) {
+  DebugVariablesBefore.push_back(
+      std::make_pair(llvm::DenseSet<VarID>(), PassID.str()));
+  DebugVariablesAfter.push_back(
+      std::make_pair(llvm::DenseSet<VarID>(), PassID.str()));
+  if (auto *M = unwrapIR<Module>(IR))
+    return this->runOnModule(M, true);
+  if (auto *F = unwrapIR<Function>(IR))
+    return this->runOnFunction(F, true);
+  return;
+}
+
+void DroppedVariableStats::runOnFunction(const Function *F, bool Before) {
+  auto &DebugVariables = Before ? DebugVariablesBefore : DebugVariablesAfter;
+  for (auto &BB : *F) {
+    for (const Instruction &I : BB) {
+      for (DbgRecord &DR : I.getDbgRecordRange()) {
+        if (auto *Dbg = dyn_cast<DbgVariableRecord>(&DR)) {
+          llvm::StringRef UniqueName = Dbg->getVariable()->getName();
----------------
felipepiovezan wrote:

we don't generally use `llvm::` inside the llvm subdirectory

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


More information about the llvm-commits mailing list