Skip to content

Commit 9dd69c5

Browse files
Gather dropped debug info variable statistics.
This patch adds the class DroppedVariableStats to StandardInstrumentations which gathers information on whether debug information was dropped by an optimization pass in llvm. This runs on every Function-level and Module-level IR pass.
1 parent d617371 commit 9dd69c5

File tree

5 files changed

+867
-3
lines changed

5 files changed

+867
-3
lines changed

llvm/include/llvm/Passes/StandardInstrumentations.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/ADT/StringSet.h"
2222
#include "llvm/CodeGen/MachineBasicBlock.h"
2323
#include "llvm/IR/BasicBlock.h"
24+
#include "llvm/IR/DebugInfoMetadata.h"
2425
#include "llvm/IR/OptBisect.h"
2526
#include "llvm/IR/PassTimingInfo.h"
2627
#include "llvm/IR/ValueHandle.h"
@@ -578,6 +579,74 @@ class PrintCrashIRInstrumentation {
578579
static void SignalHandler(void *);
579580
};
580581

582+
class DroppedVariableStats {
583+
public:
584+
DroppedVariableStats(bool DroppedVarStatsEnabled) {
585+
if (DroppedVarStatsEnabled)
586+
llvm::outs()
587+
<< "Pass Level, Pass Name, Num of Dropped Variables, Func or "
588+
"Module Name\n";
589+
};
590+
// We intend this to be unique per-compilation, thus no copies.
591+
DroppedVariableStats(const DroppedVariableStats &) = delete;
592+
void operator=(const DroppedVariableStats &) = delete;
593+
594+
void registerCallbacks(PassInstrumentationCallbacks &PIC);
595+
596+
void runBeforePass(StringRef PassID, Any IR);
597+
void runAfterPass(StringRef PassID, Any IR, const PreservedAnalyses &PA);
598+
void runAfterPassInvalidated(StringRef PassID, const PreservedAnalyses &PA);
599+
bool getPassDroppedVariables() { return PassDroppedVariables; }
600+
601+
private:
602+
bool PassDroppedVariables = false;
603+
/// VarID is a unique key that represents a #dbg_value
604+
using VarID =
605+
std::tuple<const DIScope *, const DIScope *, const DILocalVariable *>;
606+
/// A stack of DenseMaps, which map the name of an llvm::Function to a
607+
/// DenseSet of VarIDs before an optimization pass has run.
608+
SmallVector<DenseMap<StringRef, DenseSet<VarID>>> DebugVariablesBefore;
609+
/// A stack of DenseMaps, which map the name of an llvm::Function to a
610+
/// DenseSet of VarIDs after an optimization pass has run.
611+
SmallVector<DenseMap<StringRef, DenseSet<VarID>>> DebugVariablesAfter;
612+
/// A DenseSet tracking whether a scope was visited before.
613+
DenseSet<const DIScope *> VisitedScope;
614+
/// A stack of DenseMaps, which map the name of an llvm::Function to a
615+
/// DenseMap of VarIDs and their inlinedAt locations before an optimization
616+
/// pass has run.
617+
SmallVector<DenseMap<StringRef, DenseMap<VarID, DILocation *>>> InlinedAts;
618+
619+
/// Iterate over all Functions in a Module and report any dropped debug
620+
/// information. Will call calculateDropppedVarStatsOnFunction on every
621+
/// Function.
622+
void calculateDropppedVarStatsOnModule(const Module *M, StringRef PassID,
623+
std::string FuncOrModName,
624+
std::string PassLevel);
625+
/// Iterate over all Instructions in a Function and report any dropped debug
626+
/// information.
627+
void calculateDropppedVarStatsOnFunction(const Function *F, StringRef PassID,
628+
std::string FuncOrModName,
629+
std::string PassLevel);
630+
/// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
631+
/// after a pass has run to facilitate dropped variable calculation for an
632+
/// llvm::Function.
633+
void runOnFunction(const Function *F, bool Before);
634+
/// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
635+
/// after a pass has run to facilitate dropped variable calculation for an
636+
/// llvm::Module. Calls runOnFunction on every Function in the Module.
637+
void runOnModule(const Module *M, bool Before);
638+
/// Remove a dropped #dbg_value VarID from all Sets in the
639+
/// DroppedVariablesBefore stack.
640+
void removeVarFromAllSets(VarID Var, StringRef FuncName);
641+
/// Return true if \p Scope is the same as \p DbgValScope or a child scope of
642+
/// \p DbgValScope, return false otherwise.
643+
bool isScopeChildOfOrEqualTo(DIScope *Scope, const DIScope *DbgValScope);
644+
/// Return true if \p InlinedAt is the same as \p DbgValInlinedAt or part of
645+
/// the InlinedAt chain, return false otherwise.
646+
bool isInlinedAtChildOfOrEqualTo(const DILocation *InlinedAt,
647+
const DILocation *DbgValInlinedAt);
648+
};
649+
581650
/// This class provides an interface to register all the standard pass
582651
/// instrumentations and manages their state (if any).
583652
class StandardInstrumentations {
@@ -595,6 +664,7 @@ class StandardInstrumentations {
595664
PrintCrashIRInstrumentation PrintCrashIR;
596665
IRChangedTester ChangeTester;
597666
VerifyInstrumentation Verify;
667+
DroppedVariableStats DroppedStats;
598668

599669
bool VerifyEach;
600670

llvm/lib/Passes/StandardInstrumentations.cpp

Lines changed: 185 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "llvm/CodeGen/MachineVerifier.h"
2525
#include "llvm/IR/Constants.h"
2626
#include "llvm/IR/Function.h"
27+
#include "llvm/IR/InstIterator.h"
28+
#include "llvm/IR/IntrinsicInst.h"
2729
#include "llvm/IR/Module.h"
2830
#include "llvm/IR/PassInstrumentation.h"
2931
#include "llvm/IR/PassManager.h"
@@ -138,6 +140,11 @@ static cl::opt<std::string> IRDumpDirectory(
138140
"files in this directory rather than written to stderr"),
139141
cl::Hidden, cl::value_desc("filename"));
140142

143+
static cl::opt<bool>
144+
DroppedVarStats("dropped-variable-stats", cl::Hidden,
145+
cl::desc("Dump dropped debug variables stats"),
146+
cl::init(false));
147+
141148
template <typename IRUnitT> static const IRUnitT *unwrapIR(Any IR) {
142149
const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
143150
return IRPtr ? *IRPtr : nullptr;
@@ -2445,16 +2452,16 @@ void DotCfgChangeReporter::registerCallbacks(
24452452
StandardInstrumentations::StandardInstrumentations(
24462453
LLVMContext &Context, bool DebugLogging, bool VerifyEach,
24472454
PrintPassOptions PrintPassOpts)
2448-
: PrintPass(DebugLogging, PrintPassOpts),
2449-
OptNone(DebugLogging),
2455+
: PrintPass(DebugLogging, PrintPassOpts), OptNone(DebugLogging),
24502456
OptPassGate(Context),
24512457
PrintChangedIR(PrintChanged == ChangePrinter::Verbose),
24522458
PrintChangedDiff(PrintChanged == ChangePrinter::DiffVerbose ||
24532459
PrintChanged == ChangePrinter::ColourDiffVerbose,
24542460
PrintChanged == ChangePrinter::ColourDiffVerbose ||
24552461
PrintChanged == ChangePrinter::ColourDiffQuiet),
24562462
WebsiteChangeReporter(PrintChanged == ChangePrinter::DotCfgVerbose),
2457-
Verify(DebugLogging), VerifyEach(VerifyEach) {}
2463+
Verify(DebugLogging), DroppedStats(DroppedVarStats),
2464+
VerifyEach(VerifyEach) {}
24582465

24592466
PrintCrashIRInstrumentation *PrintCrashIRInstrumentation::CrashReporter =
24602467
nullptr;
@@ -2514,6 +2521,180 @@ void PrintCrashIRInstrumentation::registerCallbacks(
25142521
});
25152522
}
25162523

2524+
void DroppedVariableStats::registerCallbacks(
2525+
PassInstrumentationCallbacks &PIC) {
2526+
if (!DroppedVarStats)
2527+
return;
2528+
2529+
PIC.registerBeforeNonSkippedPassCallback(
2530+
[this](StringRef P, Any IR) { return this->runBeforePass(P, IR); });
2531+
PIC.registerAfterPassCallback(
2532+
[this](StringRef P, Any IR, const PreservedAnalyses &PA) {
2533+
return this->runAfterPass(P, IR, PA);
2534+
});
2535+
PIC.registerAfterPassInvalidatedCallback(
2536+
[this](StringRef P, const PreservedAnalyses &PA) {
2537+
return this->runAfterPassInvalidated(P, PA);
2538+
});
2539+
}
2540+
2541+
void DroppedVariableStats::runBeforePass(StringRef PassID, Any IR) {
2542+
DebugVariablesBefore.push_back(DenseMap<StringRef, DenseSet<VarID>>());
2543+
DebugVariablesAfter.push_back(DenseMap<StringRef, DenseSet<VarID>>());
2544+
InlinedAts.push_back(DenseMap<StringRef, DenseMap<VarID, DILocation *>>());
2545+
if (auto *M = unwrapIR<Module>(IR))
2546+
return this->runOnModule(M, true);
2547+
if (auto *F = unwrapIR<Function>(IR))
2548+
return this->runOnFunction(F, true);
2549+
return;
2550+
}
2551+
2552+
void DroppedVariableStats::runOnFunction(const Function *F, bool Before) {
2553+
auto &VarIDMap = (Before ? DebugVariablesBefore : DebugVariablesAfter).back();
2554+
auto &InlinedAtsMap = InlinedAts.back();
2555+
auto FuncName = F->getName();
2556+
if (Before)
2557+
InlinedAtsMap.try_emplace(FuncName, DenseMap<VarID, DILocation *>());
2558+
VarIDMap.try_emplace(FuncName, DenseSet<VarID>());
2559+
auto &VarIDs = VarIDMap[FuncName];
2560+
for (const auto &I : instructions(F)) {
2561+
for (DbgRecord &DR : I.getDbgRecordRange()) {
2562+
if (auto *Dbg = dyn_cast<DbgVariableRecord>(&DR)) {
2563+
auto *DbgVar = Dbg->getVariable();
2564+
auto DbgLoc = DR.getDebugLoc();
2565+
VarID Key{DbgVar->getScope(), DbgLoc->getInlinedAtScope(), DbgVar};
2566+
VarIDs.insert(Key);
2567+
if (Before)
2568+
InlinedAtsMap[FuncName].try_emplace(Key, DbgLoc.getInlinedAt());
2569+
}
2570+
}
2571+
}
2572+
}
2573+
2574+
void DroppedVariableStats::runOnModule(const Module *M, bool Before) {
2575+
for (auto &F : *M)
2576+
runOnFunction(&F, Before);
2577+
}
2578+
2579+
void DroppedVariableStats::removeVarFromAllSets(VarID Var, StringRef FuncName) {
2580+
// Do not remove Var from the last element, it will be popped from the stack
2581+
// anyway.
2582+
for (auto &BeforeMap : llvm::drop_end(DebugVariablesBefore))
2583+
BeforeMap[FuncName].erase(Var);
2584+
}
2585+
2586+
void DroppedVariableStats::calculateDropppedVarStatsOnModule(
2587+
const Module *M, StringRef PassID, std::string FuncOrModName,
2588+
std::string PassLevel) {
2589+
for (auto &F : *M) {
2590+
calculateDropppedVarStatsOnFunction(&F, PassID, FuncOrModName, PassLevel);
2591+
}
2592+
}
2593+
2594+
void DroppedVariableStats::calculateDropppedVarStatsOnFunction(
2595+
const Function *F, StringRef PassID, std::string FuncOrModName,
2596+
std::string PassLevel) {
2597+
unsigned DroppedCount = 0;
2598+
auto FuncName = F->getName();
2599+
auto &DebugVariablesBeforeMap = DebugVariablesBefore.back()[FuncName];
2600+
auto &DebugVariablesAfterMap = DebugVariablesAfter.back()[FuncName];
2601+
auto &InlinedAtsMap = InlinedAts.back()[FuncName];
2602+
// Find an Instruction that shares the same scope as the dropped #dbg_value or
2603+
// has a scope that is the child of the scope of the #dbg_value, and has an
2604+
// inlinedAt equal to the inlinedAt of the #dbg_value or it's inlinedAt chain
2605+
// contains the inlinedAt of the #dbg_value, if such an Instruction is found,
2606+
// debug information is dropped.
2607+
for (auto Var : DebugVariablesBeforeMap) {
2608+
if (!DebugVariablesAfterMap.contains(Var)) {
2609+
const auto *DbgValScope = std::get<0>(Var);
2610+
for (const auto &I : instructions(F)) {
2611+
auto *DbgLoc = I.getDebugLoc().get();
2612+
if (DbgLoc) {
2613+
auto *Scope = DbgLoc->getScope();
2614+
if (isScopeChildOfOrEqualTo(Scope, DbgValScope)) {
2615+
if (isInlinedAtChildOfOrEqualTo(DbgLoc->getInlinedAt(),
2616+
InlinedAtsMap[Var])) {
2617+
DroppedCount++;
2618+
break;
2619+
}
2620+
}
2621+
}
2622+
}
2623+
removeVarFromAllSets(Var, FuncName);
2624+
}
2625+
}
2626+
if (DroppedCount > 0) {
2627+
llvm::outs() << PassLevel << ", " << PassID << ", " << DroppedCount << ", "
2628+
<< FuncOrModName << "\n";
2629+
PassDroppedVariables = true;
2630+
} else
2631+
PassDroppedVariables = false;
2632+
}
2633+
2634+
void DroppedVariableStats::runAfterPassInvalidated(
2635+
StringRef PassID, const PreservedAnalyses &PA) {
2636+
DebugVariablesBefore.pop_back();
2637+
DebugVariablesAfter.pop_back();
2638+
InlinedAts.pop_back();
2639+
}
2640+
2641+
void DroppedVariableStats::runAfterPass(StringRef PassID, Any IR,
2642+
const PreservedAnalyses &PA) {
2643+
std::string PassLevel;
2644+
std::string FuncOrModName;
2645+
if (auto *M = unwrapIR<Module>(IR)) {
2646+
this->runOnModule(M, false);
2647+
PassLevel = "Module";
2648+
FuncOrModName = M->getName();
2649+
calculateDropppedVarStatsOnModule(M, PassID, FuncOrModName, PassLevel);
2650+
} else if (auto *F = unwrapIR<Function>(IR)) {
2651+
this->runOnFunction(F, false);
2652+
PassLevel = "Function";
2653+
FuncOrModName = F->getName();
2654+
calculateDropppedVarStatsOnFunction(F, PassID, FuncOrModName, PassLevel);
2655+
}
2656+
2657+
DebugVariablesBefore.pop_back();
2658+
DebugVariablesAfter.pop_back();
2659+
InlinedAts.pop_back();
2660+
return;
2661+
}
2662+
2663+
bool DroppedVariableStats::isScopeChildOfOrEqualTo(DIScope *Scope,
2664+
const DIScope *DbgValScope) {
2665+
while (Scope != nullptr) {
2666+
if (VisitedScope.find(Scope) == VisitedScope.end()) {
2667+
VisitedScope.insert(Scope);
2668+
if (Scope == DbgValScope) {
2669+
VisitedScope.clear();
2670+
return true;
2671+
}
2672+
Scope = Scope->getScope();
2673+
} else {
2674+
VisitedScope.clear();
2675+
return false;
2676+
}
2677+
}
2678+
return false;
2679+
}
2680+
2681+
bool DroppedVariableStats::isInlinedAtChildOfOrEqualTo(
2682+
const DILocation *InlinedAt, const DILocation *DbgValInlinedAt) {
2683+
if (DbgValInlinedAt == InlinedAt)
2684+
return true;
2685+
if (!DbgValInlinedAt)
2686+
return false;
2687+
if (!InlinedAt)
2688+
return false;
2689+
auto *IA = InlinedAt;
2690+
while (IA) {
2691+
if (IA == DbgValInlinedAt)
2692+
return true;
2693+
IA = IA->getInlinedAt();
2694+
}
2695+
return false;
2696+
}
2697+
25172698
void StandardInstrumentations::registerCallbacks(
25182699
PassInstrumentationCallbacks &PIC, ModuleAnalysisManager *MAM) {
25192700
PrintIR.registerCallbacks(PIC);
@@ -2529,6 +2710,7 @@ void StandardInstrumentations::registerCallbacks(
25292710
WebsiteChangeReporter.registerCallbacks(PIC);
25302711
ChangeTester.registerCallbacks(PIC);
25312712
PrintCrashIR.registerCallbacks(PIC);
2713+
DroppedStats.registerCallbacks(PIC);
25322714
if (MAM)
25332715
PreservedCFGChecker.registerCallbacks(PIC, *MAM);
25342716

llvm/test/Other/dropped-var-stats.ll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: opt -dropped-variable-stats %s -passes='verify' -S | FileCheck %s --check-prefix=NOT-DROPPED
2+
; NOT-DROPPED: Pass Level, Pass Name, Num of Dropped Variables, Func or Module Name
3+
; NOT-DROPPED-NOT: Function, ADCEPass, 1, _Z3bari
4+
5+
; ModuleID = '/tmp/dropped.cpp'
6+
define noundef range(i32 -2147483646, -2147483648) i32 @_Z3bari(i32 noundef %y) local_unnamed_addr #1 !dbg !19 {
7+
#dbg_value(i32 %y, !15, !DIExpression(), !23)
8+
%add = add nsw i32 %y, 2,!dbg !25
9+
ret i32 %add,!dbg !26
10+
}
11+
!llvm.module.flags = !{ !3, !7}
12+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 20.0.0git ([email protected]:llvm/llvm-project.git 7fc8398aaad65c4c29f1511c374d07308e667af5)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
13+
!1 = !DIFile(filename: "/tmp/dropped.cpp", directory: "/Users/shubham/Development/llvm-project")
14+
!3 = !{i32 2, !"Debug Info Version", i32 3}
15+
!7 = !{i32 7, !"frame-pointer", i32 1}
16+
!9 = distinct !DISubprogram( unit: !0, retainedNodes: !14)
17+
!13 = !DIBasicType()
18+
!14 = !{}
19+
!15 = !DILocalVariable( scope: !9, type: !13)
20+
!19 = distinct !DISubprogram( unit: !0, retainedNodes: !20)
21+
!20 = !{}
22+
!23 = !DILocation( scope: !9, inlinedAt: !24)
23+
!24 = distinct !DILocation( scope: !19)
24+
!25 = !DILocation( scope: !19)
25+
!26 = !DILocation( scope: !19)

llvm/unittests/IR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ add_llvm_unittest(IRTests
4343
ShuffleVectorInstTest.cpp
4444
StructuralHashTest.cpp
4545
TimePassesTest.cpp
46+
DroppedVariableStatsTest.cpp
4647
TypesTest.cpp
4748
UseTest.cpp
4849
UserTest.cpp

0 commit comments

Comments
 (0)