[llvm] Add a pass to collect dropped variable statistics (PR #102233)
Adrian Prantl via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 18 14:46:38 PDT 2024
================
@@ -2514,6 +2521,181 @@ void PrintCrashIRInstrumentation::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);
+ });
+ PIC.registerAfterPassInvalidatedCallback(
+ [this](StringRef P, const PreservedAnalyses &PA) {
+ return this->runAfterPassInvalidated(P, PA);
+ });
+}
+
+void DroppedVariableStats::runBeforePass(StringRef PassID, Any IR) {
+ DebugVariablesStack.push_back(
+ {DenseMap<const Function *, DenseSet<VarID>>(),
+ DenseMap<const Function *, DenseSet<VarID>>()});
+ InlinedAts.push_back(DenseMap<StringRef, DenseMap<VarID, DILocation *>>());
+ 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 = DebugVariablesStack.back();
+ auto &VarIDMap = (Before ? DebugVariables.DebugVariablesBefore
+ : DebugVariables.DebugVariablesAfter);
+ auto &InlinedAtsMap = InlinedAts.back();
+ auto FuncName = F->getName();
+ if (Before)
+ InlinedAtsMap.try_emplace(FuncName, DenseMap<VarID, DILocation *>());
+ VarIDMap.try_emplace(F, DenseSet<VarID>());
+ auto &VarIDs = VarIDMap[F];
+ for (const auto &I : instructions(F)) {
+ for (DbgRecord &DR : I.getDbgRecordRange()) {
+ if (auto *Dbg = dyn_cast<DbgVariableRecord>(&DR)) {
+ auto *DbgVar = Dbg->getVariable();
+ auto DbgLoc = DR.getDebugLoc();
+ VarID Key{DbgVar->getScope(), DbgLoc->getInlinedAtScope(), DbgVar};
+ VarIDs.insert(Key);
+ if (Before)
+ InlinedAtsMap[FuncName].try_emplace(Key, DbgLoc.getInlinedAt());
+ }
+ }
+ }
+}
+
+void DroppedVariableStats::runOnModule(const Module *M, bool Before) {
+ for (auto &F : *M)
+ runOnFunction(&F, Before);
+}
+
+void DroppedVariableStats::removeVarFromAllSets(VarID Var, const Function *F) {
+ // Do not remove Var from the last element, it will be popped from the stack.
+ for (auto &DebugVariables : llvm::drop_end(DebugVariablesStack))
+ DebugVariables.DebugVariablesBefore[F].erase(Var);
+}
+
+void DroppedVariableStats::calculateDroppedVarStatsOnModule(
+ const Module *M, StringRef PassID, std::string FuncOrModName,
+ std::string PassLevel) {
+ for (auto &F : *M) {
+ calculateDroppedVarStatsOnFunction(&F, PassID, FuncOrModName, PassLevel);
+ }
+}
+
+void DroppedVariableStats::calculateDroppedVarStatsOnFunction(
+ const Function *F, StringRef PassID, std::string FuncOrModName,
+ std::string PassLevel) {
+ unsigned DroppedCount = 0;
+ auto FuncName = F->getName();
+ auto &DebugVariables = DebugVariablesStack.back();
+ auto &DebugVariablesBeforeMap = DebugVariables.DebugVariablesBefore[F];
+ auto &DebugVariablesAfterMap = DebugVariables.DebugVariablesAfter[F];
+ auto &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 (auto Var : DebugVariablesBeforeMap) {
+ if (!DebugVariablesAfterMap.contains(Var)) {
+ const auto *DbgValScope = std::get<0>(Var);
----------------
adrian-prantl wrote:
If you can fit it within the 80 column limit, I would replace the autos here with the type names
https://github.com/llvm/llvm-project/pull/102233
More information about the llvm-commits
mailing list