24
24
#include " llvm/CodeGen/MachineVerifier.h"
25
25
#include " llvm/IR/Constants.h"
26
26
#include " llvm/IR/Function.h"
27
+ #include " llvm/IR/InstIterator.h"
28
+ #include " llvm/IR/IntrinsicInst.h"
27
29
#include " llvm/IR/Module.h"
28
30
#include " llvm/IR/PassInstrumentation.h"
29
31
#include " llvm/IR/PassManager.h"
@@ -138,6 +140,11 @@ static cl::opt<std::string> IRDumpDirectory(
138
140
" files in this directory rather than written to stderr" ),
139
141
cl::Hidden, cl::value_desc(" filename" ));
140
142
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
+
141
148
template <typename IRUnitT> static const IRUnitT *unwrapIR (Any IR) {
142
149
const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
143
150
return IRPtr ? *IRPtr : nullptr ;
@@ -2445,16 +2452,16 @@ void DotCfgChangeReporter::registerCallbacks(
2445
2452
StandardInstrumentations::StandardInstrumentations (
2446
2453
LLVMContext &Context, bool DebugLogging, bool VerifyEach,
2447
2454
PrintPassOptions PrintPassOpts)
2448
- : PrintPass(DebugLogging, PrintPassOpts),
2449
- OptNone (DebugLogging),
2455
+ : PrintPass(DebugLogging, PrintPassOpts), OptNone(DebugLogging),
2450
2456
OptPassGate (Context),
2451
2457
PrintChangedIR(PrintChanged == ChangePrinter::Verbose),
2452
2458
PrintChangedDiff(PrintChanged == ChangePrinter::DiffVerbose ||
2453
2459
PrintChanged == ChangePrinter::ColourDiffVerbose,
2454
2460
PrintChanged == ChangePrinter::ColourDiffVerbose ||
2455
2461
PrintChanged == ChangePrinter::ColourDiffQuiet),
2456
2462
WebsiteChangeReporter(PrintChanged == ChangePrinter::DotCfgVerbose),
2457
- Verify(DebugLogging), VerifyEach(VerifyEach) {}
2463
+ Verify(DebugLogging), DroppedStats(DroppedVarStats),
2464
+ VerifyEach(VerifyEach) {}
2458
2465
2459
2466
PrintCrashIRInstrumentation *PrintCrashIRInstrumentation::CrashReporter =
2460
2467
nullptr ;
@@ -2514,6 +2521,180 @@ void PrintCrashIRInstrumentation::registerCallbacks(
2514
2521
});
2515
2522
}
2516
2523
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
+
2517
2698
void StandardInstrumentations::registerCallbacks (
2518
2699
PassInstrumentationCallbacks &PIC, ModuleAnalysisManager *MAM) {
2519
2700
PrintIR.registerCallbacks (PIC);
@@ -2529,6 +2710,7 @@ void StandardInstrumentations::registerCallbacks(
2529
2710
WebsiteChangeReporter.registerCallbacks (PIC);
2530
2711
ChangeTester.registerCallbacks (PIC);
2531
2712
PrintCrashIR.registerCallbacks (PIC);
2713
+ DroppedStats.registerCallbacks (PIC);
2532
2714
if (MAM)
2533
2715
PreservedCFGChecker.registerCallbacks (PIC, *MAM);
2534
2716
0 commit comments