Skip to content

Commit 5a99e41

Browse files
Change DroppedVariableStats to be extensible.
Change DroppedVariableStats to have an extensible design so that we can use it to add dropped statistics to MIR passes and the instruction selector.
1 parent f7045b3 commit 5a99e41

File tree

6 files changed

+173
-178
lines changed

6 files changed

+173
-178
lines changed

llvm/include/llvm/CodeGen/DroppedVariableStats.h

Lines changed: 86 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323

2424
namespace llvm {
2525

26-
/// A class to collect and print dropped debug information variable statistics.
27-
/// After every LLVM IR pass is run, it will print how many #dbg_values were
28-
/// dropped due to that pass.
26+
/// A unique key that represents a #dbg_value.
27+
using VarID =
28+
std::tuple<const DIScope *, const DIScope *, const DILocalVariable *>;
29+
30+
/// A base class to collect and print dropped debug information variable
31+
/// statistics.
2932
class DroppedVariableStats {
3033
public:
3134
DroppedVariableStats(bool DroppedVarStatsEnabled)
@@ -39,18 +42,25 @@ class DroppedVariableStats {
3942
DroppedVariableStats(const DroppedVariableStats &) = delete;
4043
void operator=(const DroppedVariableStats &) = delete;
4144

42-
void registerCallbacks(PassInstrumentationCallbacks &PIC);
43-
void runBeforePass(StringRef PassID, Any IR);
44-
void runAfterPass(StringRef PassID, Any IR, const PreservedAnalyses &PA);
45-
void runAfterPassInvalidated(StringRef PassID, const PreservedAnalyses &PA);
4645
bool getPassDroppedVariables() { return PassDroppedVariables; }
4746

48-
private:
47+
protected:
48+
void setup() {
49+
DebugVariablesStack.push_back(
50+
{DenseMap<const Function *, DebugVariables>()});
51+
InlinedAts.push_back(
52+
{DenseMap<StringRef, DenseMap<VarID, DILocation *>>()});
53+
return;
54+
}
55+
56+
void cleanup() {
57+
DebugVariablesStack.pop_back();
58+
InlinedAts.pop_back();
59+
return;
60+
}
61+
4962
bool PassDroppedVariables = false;
5063
bool DroppedVariableStatsEnabled = false;
51-
/// A unique key that represents a #dbg_value.
52-
using VarID =
53-
std::tuple<const DIScope *, const DIScope *, const DILocalVariable *>;
5464

5565
struct DebugVariables {
5666
/// DenseSet of VarIDs before an optimization pass has run.
@@ -70,36 +80,80 @@ class DroppedVariableStats {
7080
/// DenseMap of VarIDs and their inlinedAt locations before an optimization
7181
/// pass has run.
7282
SmallVector<DenseMap<StringRef, DenseMap<VarID, DILocation *>>> InlinedAts;
83+
/// Remove a dropped #dbg_value VarID from all Sets in the
84+
/// DroppedVariablesBefore stack.
85+
void removeVarFromAllSets(VarID Var, const Function *F) {
86+
// Do not remove Var from the last element, it will be popped from the
87+
// stack.
88+
for (auto &DebugVariablesMap : llvm::drop_end(DebugVariablesStack))
89+
DebugVariablesMap[F].DebugVariablesBefore.erase(Var);
90+
}
91+
/// Return true if \p Scope is the same as \p DbgValScope or a child scope of
92+
/// \p DbgValScope, return false otherwise.
93+
bool isScopeChildOfOrEqualTo(DIScope *Scope, const DIScope *DbgValScope);
94+
/// Return true if \p InlinedAt is the same as \p DbgValInlinedAt or part of
95+
/// the InlinedAt chain, return false otherwise.
96+
bool isInlinedAtChildOfOrEqualTo(const DILocation *InlinedAt,
97+
const DILocation *DbgValInlinedAt);
98+
};
7399

74-
/// Iterate over all Functions in a Module and report any dropped debug
75-
/// information. Will call calculateDroppedVarStatsOnFunction on every
76-
/// Function.
77-
void calculateDroppedVarStatsOnModule(const Module *M, StringRef PassID,
78-
std::string FuncOrModName,
79-
std::string PassLevel);
100+
/// A class to collect and print dropped debug information due to LLVM IR
101+
/// optimization passes. After every LLVM IR pass is run, it will print how many
102+
/// #dbg_values were dropped due to that pass.
103+
class DroppedVariableStatsIR : public DroppedVariableStats {
104+
public:
105+
DroppedVariableStatsIR(bool DroppedVarStatsEnabled)
106+
: llvm::DroppedVariableStats(DroppedVarStatsEnabled) {}
107+
108+
void runBeforePass(Any IR) {
109+
setup();
110+
if (const auto *M = unwrapIR<Module>(IR))
111+
return this->runOnModule(M, true);
112+
if (const auto *F = unwrapIR<Function>(IR))
113+
return this->runOnFunction(F, true);
114+
}
115+
116+
void runAfterPass(StringRef P, Any IR) {
117+
if (const auto *M = unwrapIR<Module>(IR))
118+
runAfterPassModule(P, M);
119+
else if (const auto *F = unwrapIR<Function>(IR))
120+
runAfterPassFunction(P, F);
121+
return cleanup();
122+
}
123+
124+
void registerCallbacks(PassInstrumentationCallbacks &PIC);
125+
126+
private:
127+
void runAfterPassFunction(StringRef PassID, const Function *F) {
128+
runOnFunction(F, false);
129+
calculateDroppedVarStatsOnFunction(F, PassID, F->getName().str(),
130+
"Function");
131+
}
132+
133+
void runAfterPassModule(StringRef PassID, const Module *M) {
134+
runOnModule(M, false);
135+
calculateDroppedVarStatsOnModule(M, PassID, M->getName().str(), "Module");
136+
}
137+
/// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
138+
/// after a pass has run to facilitate dropped variable calculation for an
139+
/// llvm::Function.
140+
void runOnFunction(const Function *F, bool Before);
80141
/// Iterate over all Instructions in a Function and report any dropped debug
81142
/// information.
82143
void calculateDroppedVarStatsOnFunction(const Function *F, StringRef PassID,
83144
std::string FuncOrModName,
84145
std::string PassLevel);
85146
/// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
86147
/// after a pass has run to facilitate dropped variable calculation for an
87-
/// llvm::Function.
88-
void runOnFunction(const Function *F, bool Before);
89-
/// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
90-
/// after a pass has run to facilitate dropped variable calculation for an
91148
/// llvm::Module. Calls runOnFunction on every Function in the Module.
92149
void runOnModule(const Module *M, bool Before);
93-
/// Remove a dropped #dbg_value VarID from all Sets in the
94-
/// DroppedVariablesBefore stack.
95-
void removeVarFromAllSets(VarID Var, const Function *F);
96-
/// Return true if \p Scope is the same as \p DbgValScope or a child scope of
97-
/// \p DbgValScope, return false otherwise.
98-
bool isScopeChildOfOrEqualTo(DIScope *Scope, const DIScope *DbgValScope);
99-
/// Return true if \p InlinedAt is the same as \p DbgValInlinedAt or part of
100-
/// the InlinedAt chain, return false otherwise.
101-
bool isInlinedAtChildOfOrEqualTo(const DILocation *InlinedAt,
102-
const DILocation *DbgValInlinedAt);
150+
/// Iterate over all Functions in a Module and report any dropped debug
151+
/// information. Will call calculateDroppedVarStatsOnFunction on every
152+
/// Function.
153+
void calculateDroppedVarStatsOnModule(const Module *M, StringRef PassID,
154+
std::string FuncOrModName,
155+
std::string PassLevel);
156+
103157
template <typename IRUnitT> static const IRUnitT *unwrapIR(Any IR) {
104158
const IRUnitT **IRPtr = llvm::any_cast<const IRUnitT *>(&IR);
105159
return IRPtr ? *IRPtr : nullptr;
@@ -108,4 +162,4 @@ class DroppedVariableStats {
108162

109163
} // namespace llvm
110164

111-
#endif
165+
#endif

llvm/include/llvm/Passes/StandardInstrumentations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ class StandardInstrumentations {
597597
PrintCrashIRInstrumentation PrintCrashIR;
598598
IRChangedTester ChangeTester;
599599
VerifyInstrumentation Verify;
600-
DroppedVariableStats DroppedStats;
600+
DroppedVariableStatsIR DroppedStatsIR;
601601

602602
bool VerifyEach;
603603

llvm/lib/CodeGen/DroppedVariableStats.cpp

Lines changed: 54 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,42 @@
1818

1919
using namespace llvm;
2020

21-
void DroppedVariableStats::registerCallbacks(
22-
PassInstrumentationCallbacks &PIC) {
23-
if (!DroppedVariableStatsEnabled)
24-
return;
25-
26-
PIC.registerBeforeNonSkippedPassCallback(
27-
[this](StringRef P, Any IR) { return this->runBeforePass(P, IR); });
28-
PIC.registerAfterPassCallback(
29-
[this](StringRef P, Any IR, const PreservedAnalyses &PA) {
30-
return this->runAfterPass(P, IR, PA);
31-
});
32-
PIC.registerAfterPassInvalidatedCallback(
33-
[this](StringRef P, const PreservedAnalyses &PA) {
34-
return this->runAfterPassInvalidated(P, PA);
35-
});
21+
bool DroppedVariableStats::isScopeChildOfOrEqualTo(DIScope *Scope,
22+
const DIScope *DbgValScope) {
23+
while (Scope != nullptr) {
24+
if (VisitedScope.find(Scope) == VisitedScope.end()) {
25+
VisitedScope.insert(Scope);
26+
if (Scope == DbgValScope) {
27+
VisitedScope.clear();
28+
return true;
29+
}
30+
Scope = Scope->getScope();
31+
} else {
32+
VisitedScope.clear();
33+
return false;
34+
}
35+
}
36+
return false;
3637
}
3738

38-
void DroppedVariableStats::runBeforePass(StringRef PassID, Any IR) {
39-
DebugVariablesStack.push_back({DenseMap<const Function *, DebugVariables>()});
40-
InlinedAts.push_back({DenseMap<StringRef, DenseMap<VarID, DILocation *>>()});
41-
if (auto *M = unwrapIR<Module>(IR))
42-
return this->runOnModule(M, true);
43-
if (auto *F = unwrapIR<Function>(IR))
44-
return this->runOnFunction(F, true);
45-
return;
39+
bool DroppedVariableStats::isInlinedAtChildOfOrEqualTo(
40+
const DILocation *InlinedAt, const DILocation *DbgValInlinedAt) {
41+
if (DbgValInlinedAt == InlinedAt)
42+
return true;
43+
if (!DbgValInlinedAt)
44+
return false;
45+
if (!InlinedAt)
46+
return false;
47+
auto *IA = InlinedAt;
48+
while (IA) {
49+
if (IA == DbgValInlinedAt)
50+
return true;
51+
IA = IA->getInlinedAt();
52+
}
53+
return false;
4654
}
4755

48-
void DroppedVariableStats::runOnFunction(const Function *F, bool Before) {
56+
void DroppedVariableStatsIR::runOnFunction(const Function *F, bool Before) {
4957
auto &DebugVariables = DebugVariablesStack.back()[F];
5058
auto &VarIDSet = (Before ? DebugVariables.DebugVariablesBefore
5159
: DebugVariables.DebugVariablesAfter);
@@ -68,26 +76,7 @@ void DroppedVariableStats::runOnFunction(const Function *F, bool Before) {
6876
}
6977
}
7078

71-
void DroppedVariableStats::runOnModule(const Module *M, bool Before) {
72-
for (auto &F : *M)
73-
runOnFunction(&F, Before);
74-
}
75-
76-
void DroppedVariableStats::removeVarFromAllSets(VarID Var, const Function *F) {
77-
// Do not remove Var from the last element, it will be popped from the stack.
78-
for (auto &DebugVariablesMap : llvm::drop_end(DebugVariablesStack))
79-
DebugVariablesMap[F].DebugVariablesBefore.erase(Var);
80-
}
81-
82-
void DroppedVariableStats::calculateDroppedVarStatsOnModule(
83-
const Module *M, StringRef PassID, std::string FuncOrModName,
84-
std::string PassLevel) {
85-
for (auto &F : *M) {
86-
calculateDroppedVarStatsOnFunction(&F, PassID, FuncOrModName, PassLevel);
87-
}
88-
}
89-
90-
void DroppedVariableStats::calculateDroppedVarStatsOnFunction(
79+
void DroppedVariableStatsIR::calculateDroppedVarStatsOnFunction(
9180
const Function *F, StringRef PassID, std::string FuncOrModName,
9281
std::string PassLevel) {
9382
unsigned DroppedCount = 0;
@@ -132,64 +121,30 @@ void DroppedVariableStats::calculateDroppedVarStatsOnFunction(
132121
PassDroppedVariables = false;
133122
}
134123

135-
void DroppedVariableStats::runAfterPassInvalidated(
136-
StringRef PassID, const PreservedAnalyses &PA) {
137-
DebugVariablesStack.pop_back();
138-
InlinedAts.pop_back();
124+
void DroppedVariableStatsIR::runOnModule(const Module *M, bool Before) {
125+
for (auto &F : *M)
126+
runOnFunction(&F, Before);
139127
}
140128

141-
void DroppedVariableStats::runAfterPass(StringRef PassID, Any IR,
142-
const PreservedAnalyses &PA) {
143-
std::string PassLevel;
144-
std::string FuncOrModName;
145-
if (auto *M = unwrapIR<Module>(IR)) {
146-
this->runOnModule(M, false);
147-
PassLevel = "Module";
148-
FuncOrModName = M->getName();
149-
calculateDroppedVarStatsOnModule(M, PassID, FuncOrModName, PassLevel);
150-
} else if (auto *F = unwrapIR<Function>(IR)) {
151-
this->runOnFunction(F, false);
152-
PassLevel = "Function";
153-
FuncOrModName = F->getName();
154-
calculateDroppedVarStatsOnFunction(F, PassID, FuncOrModName, PassLevel);
129+
void DroppedVariableStatsIR::calculateDroppedVarStatsOnModule(
130+
const Module *M, StringRef PassID, std::string FuncOrModName,
131+
std::string PassLevel) {
132+
for (auto &F : *M) {
133+
calculateDroppedVarStatsOnFunction(&F, PassID, FuncOrModName, PassLevel);
155134
}
156-
157-
DebugVariablesStack.pop_back();
158-
InlinedAts.pop_back();
159-
return;
160135
}
161136

162-
bool DroppedVariableStats::isScopeChildOfOrEqualTo(DIScope *Scope,
163-
const DIScope *DbgValScope) {
164-
while (Scope != nullptr) {
165-
if (VisitedScope.find(Scope) == VisitedScope.end()) {
166-
VisitedScope.insert(Scope);
167-
if (Scope == DbgValScope) {
168-
VisitedScope.clear();
169-
return true;
170-
}
171-
Scope = Scope->getScope();
172-
} else {
173-
VisitedScope.clear();
174-
return false;
175-
}
176-
}
177-
return false;
178-
}
137+
void DroppedVariableStatsIR::registerCallbacks(
138+
PassInstrumentationCallbacks &PIC) {
139+
if (!DroppedVariableStatsEnabled)
140+
return;
179141

180-
bool DroppedVariableStats::isInlinedAtChildOfOrEqualTo(
181-
const DILocation *InlinedAt, const DILocation *DbgValInlinedAt) {
182-
if (DbgValInlinedAt == InlinedAt)
183-
return true;
184-
if (!DbgValInlinedAt)
185-
return false;
186-
if (!InlinedAt)
187-
return false;
188-
auto *IA = InlinedAt;
189-
while (IA) {
190-
if (IA == DbgValInlinedAt)
191-
return true;
192-
IA = IA->getInlinedAt();
193-
}
194-
return false;
195-
}
142+
PIC.registerBeforeNonSkippedPassCallback(
143+
[this](StringRef P, Any IR) { return runBeforePass(IR); });
144+
PIC.registerAfterPassCallback(
145+
[this](StringRef P, Any IR, const PreservedAnalyses &PA) {
146+
return runAfterPass(P, IR);
147+
});
148+
PIC.registerAfterPassInvalidatedCallback(
149+
[this](StringRef P, const PreservedAnalyses &PA) { return cleanup(); });
150+
}

llvm/lib/Passes/StandardInstrumentations.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,7 +2460,7 @@ StandardInstrumentations::StandardInstrumentations(
24602460
PrintChanged == ChangePrinter::ColourDiffVerbose ||
24612461
PrintChanged == ChangePrinter::ColourDiffQuiet),
24622462
WebsiteChangeReporter(PrintChanged == ChangePrinter::DotCfgVerbose),
2463-
Verify(DebugLogging), DroppedStats(DroppedVarStats),
2463+
Verify(DebugLogging), DroppedStatsIR(DroppedVarStats),
24642464
VerifyEach(VerifyEach) {}
24652465

24662466
PrintCrashIRInstrumentation *PrintCrashIRInstrumentation::CrashReporter =
@@ -2536,7 +2536,7 @@ void StandardInstrumentations::registerCallbacks(
25362536
WebsiteChangeReporter.registerCallbacks(PIC);
25372537
ChangeTester.registerCallbacks(PIC);
25382538
PrintCrashIR.registerCallbacks(PIC);
2539-
DroppedStats.registerCallbacks(PIC);
2539+
DroppedStatsIR.registerCallbacks(PIC);
25402540
if (MAM)
25412541
PreservedCFGChecker.registerCallbacks(PIC, *MAM);
25422542

llvm/unittests/IR/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ add_llvm_unittest(IRTests
4343
ShuffleVectorInstTest.cpp
4444
StructuralHashTest.cpp
4545
TimePassesTest.cpp
46-
DroppedVariableStatsTest.cpp
46+
DroppedVariableStatsIRTest.cpp
4747
TypesTest.cpp
4848
UseTest.cpp
4949
UserTest.cpp

0 commit comments

Comments
 (0)