Skip to content

Commit 52b3ae1

Browse files
Add a pass to collect dropped var stats for MIR.
This patch uses the DroppedVariableStats class to add dropped variable statistics for MIR passes.
1 parent e8e3b60 commit 52b3ae1

File tree

6 files changed

+1192
-4
lines changed

6 files changed

+1192
-4
lines changed

llvm/include/llvm/CodeGen/DroppedVariableStats.h

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
///===---------------------------------------------------------------------===//
88
/// \file
99
/// Dropped Variable Statistics for Debug Information. Reports any number
10-
/// of #dbg_value that get dropped due to an optimization pass.
10+
/// of #dbg_values or DBG_VALUEs that get dropped due to an optimization pass.
1111
///
1212
///===---------------------------------------------------------------------===//
1313

@@ -160,6 +160,40 @@ class DroppedVariableStatsIR : public DroppedVariableStats {
160160
}
161161
};
162162

163+
/// A class to collect and print dropped debug information due to MIR
164+
/// optimization passes. After every MIR pass is run, it will print how many
165+
/// #DBG_VALUEs were dropped due to that pass.
166+
class DroppedVariableStatsMIR : public DroppedVariableStats {
167+
public:
168+
DroppedVariableStatsMIR() : llvm::DroppedVariableStats(false) {}
169+
170+
void runBeforePass(StringRef PassID, MachineFunction *MF) {
171+
if (PassID == "Debug Variable Analysis")
172+
return;
173+
setup();
174+
return runOnMachineFunction(MF, true);
175+
}
176+
177+
void runAfterPass(StringRef PassID, MachineFunction *MF) {
178+
if (PassID == "Debug Variable Analysis")
179+
return;
180+
runOnMachineFunction(MF, false);
181+
calculateDroppedVarStatsOnMachineFunction(MF, PassID, MF->getName().str());
182+
cleanup();
183+
}
184+
185+
private:
186+
/// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
187+
/// after a pass has run to facilitate dropped variable calculation for an
188+
/// llvm::MachineFunction.
189+
void runOnMachineFunction(const MachineFunction *MF, bool Before);
190+
/// Iterate over all Instructions in a MachineFunction and report any dropped
191+
/// debug information.
192+
void calculateDroppedVarStatsOnMachineFunction(const MachineFunction *MF,
193+
StringRef PassID,
194+
std::string FuncOrModName);
195+
};
196+
163197
} // namespace llvm
164198

165199
#endif

llvm/include/llvm/CodeGen/MachineFunctionPass.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#ifndef LLVM_CODEGEN_MACHINEFUNCTIONPASS_H
1919
#define LLVM_CODEGEN_MACHINEFUNCTIONPASS_H
2020

21+
#include "llvm/CodeGen/DroppedVariableStats.h"
2122
#include "llvm/CodeGen/MachineFunction.h"
2223
#include "llvm/Pass.h"
2324

@@ -73,6 +74,7 @@ class MachineFunctionPass : public FunctionPass {
7374
const std::string &Banner) const override;
7475

7576
bool runOnFunction(Function &F) override;
77+
DroppedVariableStatsMIR DroppedVarStatsMF;
7678
};
7779

7880
} // End llvm namespace

llvm/lib/CodeGen/DroppedVariableStats.cpp

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
///===---------------------------------------------------------------------===//
88
/// \file
99
/// Dropped Variable Statistics for Debug Information. Reports any number
10-
/// of #dbg_value that get dropped due to an optimization pass.
10+
/// of #dbg_values or DBG_VALUEs that get dropped due to an optimization pass.
1111
///
1212
///===---------------------------------------------------------------------===//
1313

@@ -148,3 +148,75 @@ void DroppedVariableStatsIR::registerCallbacks(
148148
PIC.registerAfterPassInvalidatedCallback(
149149
[this](StringRef P, const PreservedAnalyses &PA) { return cleanup(); });
150150
}
151+
152+
void DroppedVariableStatsMIR::runOnMachineFunction(const MachineFunction *MF,
153+
bool Before) {
154+
auto &DebugVariables = DebugVariablesStack.back()[&MF->getFunction()];
155+
auto &VarIDSet = (Before ? DebugVariables.DebugVariablesBefore
156+
: DebugVariables.DebugVariablesAfter);
157+
auto &InlinedAtsMap = InlinedAts.back();
158+
auto FuncName = MF->getName();
159+
if (Before)
160+
InlinedAtsMap.try_emplace(FuncName, DenseMap<VarID, DILocation *>());
161+
VarIDSet = DenseSet<VarID>();
162+
for (const auto &MBB : *MF) {
163+
for (const auto &MI : MBB) {
164+
if (MI.isDebugValueLike()) {
165+
auto *DbgVar = MI.getDebugVariable();
166+
if (!DbgVar)
167+
continue;
168+
auto DbgLoc = MI.getDebugLoc();
169+
VarID Key{DbgVar->getScope(), DbgLoc->getInlinedAtScope(), DbgVar};
170+
VarIDSet.insert(Key);
171+
if (Before)
172+
InlinedAtsMap[FuncName].try_emplace(Key, DbgLoc.getInlinedAt());
173+
}
174+
}
175+
}
176+
}
177+
178+
void DroppedVariableStatsMIR::calculateDroppedVarStatsOnMachineFunction(
179+
const MachineFunction *MF, StringRef PassID, std::string FuncOrModName) {
180+
unsigned DroppedCount = 0;
181+
StringRef FuncName = MF->getName();
182+
DebugVariables &DbgVariables = DebugVariablesStack.back()[&MF->getFunction()];
183+
DenseSet<VarID> &DebugVariablesBeforeSet = DbgVariables.DebugVariablesBefore;
184+
DenseSet<VarID> &DebugVariablesAfterSet = DbgVariables.DebugVariablesAfter;
185+
DenseMap<VarID, DILocation *> &InlinedAtsMap = InlinedAts.back()[FuncName];
186+
// Find an Instruction that shares the same scope as the dropped DBG_VALUE or
187+
// has a scope that is the child of the scope of the DBG_VALUE, and has an
188+
// inlinedAt equal to the inlinedAt of the DBG_VALUE or it's inlinedAt chain
189+
// contains the inlinedAt of the DBG_VALUE, if such an Instruction is found,
190+
// debug information is dropped.
191+
for (VarID Var : DebugVariablesBeforeSet) {
192+
if (DebugVariablesAfterSet.contains(Var))
193+
continue;
194+
const DIScope *DbgValScope = std::get<0>(Var);
195+
for (const auto &MBB : *MF) {
196+
for (const auto &MI : MBB) {
197+
auto *DbgLoc = MI.getDebugLoc().get();
198+
if (!DbgLoc)
199+
continue;
200+
201+
auto *Scope = DbgLoc->getScope();
202+
if (isScopeChildOfOrEqualTo(Scope, DbgValScope)) {
203+
if (isInlinedAtChildOfOrEqualTo(DbgLoc->getInlinedAt(),
204+
InlinedAtsMap[Var])) {
205+
// Found another instruction in the variable's scope, so there
206+
// exists a break point at which the variable could be observed.
207+
// Count it as dropped.
208+
DroppedCount++;
209+
break;
210+
}
211+
}
212+
}
213+
}
214+
removeVarFromAllSets(Var, &MF->getFunction());
215+
}
216+
if (DroppedCount > 0) {
217+
llvm::outs() << "MachineFunction" << ", " << PassID << ", " << DroppedCount
218+
<< ", " << FuncOrModName << "\n";
219+
PassDroppedVariables = true;
220+
} else
221+
PassDroppedVariables = false;
222+
}

llvm/lib/CodeGen/MachineFunctionPass.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
using namespace llvm;
3333
using namespace ore;
3434

35+
static cl::opt<bool> DroppedVarStatsMIR(
36+
"dropped-variable-stats-mir", cl::Hidden,
37+
cl::desc("Dump dropped debug variables stats for MIR passes"),
38+
cl::init(false));
39+
3540
Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
3641
const std::string &Banner) const {
3742
return createMachineFunctionPrinterPass(O, Banner);
@@ -90,8 +95,15 @@ bool MachineFunctionPass::runOnFunction(Function &F) {
9095
}
9196

9297
MFProps.reset(ClearedProperties);
93-
94-
bool RV = runOnMachineFunction(MF);
98+
bool RV;
99+
if (DroppedVarStatsMIR) {
100+
auto PassName = getPassName();
101+
DroppedVarStatsMF.runBeforePass(PassName, &MF);
102+
RV = runOnMachineFunction(MF);
103+
DroppedVarStatsMF.runAfterPass(PassName, &MF);
104+
} else {
105+
RV = runOnMachineFunction(MF);
106+
}
95107

96108
if (ShouldEmitSizeRemarks) {
97109
// We wanted size remarks. Check if there was a change to the number of

llvm/unittests/MIR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(LLVM_LINK_COMPONENTS
1616
add_llvm_unittest(MIRTests
1717
MachineMetadata.cpp
1818
MachineStableHashTest.cpp
19+
DroppedVariableStatsMIRTest.cpp
1920
)
2021

2122
target_link_libraries(MIRTests PRIVATE LLVMTestingSupport)

0 commit comments

Comments
 (0)