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