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

Jeremy Morse via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 28 07:14:09 PDT 2024


================
@@ -2441,19 +2448,101 @@ 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(llvm::DenseSet<VarID>());
+  DebugVariablesAfter.push_back(llvm::DenseSet<VarID>());
+  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 &VarIDs = (Before ? DebugVariablesBefore : DebugVariablesAfter).back();
+  for (const auto &I : instructions(F)) {
+    for (DbgRecord &DR : I.getDbgRecordRange()) {
+      if (auto *Dbg = dyn_cast<DbgVariableRecord>(&DR)) {
+        auto *DbgVar = Dbg->getVariable();
+        StringRef UniqueName = DbgVar->getName();
+        auto DbgLoc = DR.getDebugLoc();
+        unsigned Line = DbgVar->getLine();
+        VarID Key{cast<DILocalScope>(DbgVar->getScope()),
+                  DbgLoc->getInlinedAtScope(), UniqueName, Line};
+        VarIDs.insert(Key);
+      }
+    }
+  }
+}
+
+void DroppedVariableStats::runOnModule(const Module *M, bool Before) {
+  for (auto &F : *M)
+    runOnFunction(&F, Before);
+}
+
+void DroppedVariableStats::removeVarFromAllSets(VarID Var) {
+  // Do not remove Var from the last element, it will be popped from the stack
+  // anyway.
+  for (auto *It = DebugVariablesBefore.begin();
+       It != DebugVariablesBefore.end() - 1; It++)
----------------
jmorse wrote:

Perhaps `for (auto &Elem : llvm::drop_end(DebugVariableSBefore))`?

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


More information about the llvm-commits mailing list