Skip to content

Commit 3cd31de

Browse files
Add a pass to collect dropped var stats for MIR.
This patch extends the DroppedVariableStats class to add dropped variable statistics for MIR passes.
1 parent 582f796 commit 3cd31de

File tree

6 files changed

+1193
-4
lines changed

6 files changed

+1193
-4
lines changed

llvm/include/llvm/CodeGen/DroppedVariableStats.h

Lines changed: 48 additions & 2 deletions
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

@@ -211,6 +211,52 @@ class DroppedVariableStatsIR : public DroppedVariableStats {
211211
}
212212
};
213213

214+
/// A class to collect and print dropped debug information due to MIR
215+
/// optimization passes. After every MIR pass is run, it will print how many
216+
/// #DBG_VALUEs were dropped due to that pass.
217+
class DroppedVariableStatsMIR : public DroppedVariableStats {
218+
public:
219+
DroppedVariableStatsMIR() : llvm::DroppedVariableStats(false) {}
220+
221+
void runBeforePass(StringRef PassID, MachineFunction *MF) {
222+
if (PassID == "Debug Variable Analysis")
223+
return;
224+
setup();
225+
return runOnMachineFunction(MF, true);
226+
}
227+
228+
void runAfterPass(StringRef PassID, MachineFunction *MF) {
229+
if (PassID == "Debug Variable Analysis")
230+
return;
231+
runOnMachineFunction(MF, false);
232+
calculateDroppedVarStatsOnMachineFunction(MF, PassID, MF->getName().str());
233+
cleanup();
234+
}
235+
236+
private:
237+
const MachineFunction *MFunc;
238+
/// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
239+
/// after a pass has run to facilitate dropped variable calculation for an
240+
/// llvm::MachineFunction.
241+
void runOnMachineFunction(const MachineFunction *MF, bool Before);
242+
/// Iterate over all Instructions in a MachineFunction and report any dropped
243+
/// debug information.
244+
void calculateDroppedVarStatsOnMachineFunction(const MachineFunction *MF,
245+
StringRef PassID,
246+
StringRef FuncOrModName);
247+
/// Override base class method to run on an llvm::MachineFunction
248+
/// specifically.
249+
virtual void visitEveryDebugVariable(
250+
unsigned &DroppedCount, DenseSet<VarID> &DebugVariablesBeforeSet,
251+
DenseSet<VarID> &DebugVariablesAfterSet,
252+
DenseMap<VarID, DILocation *> &InlinedAtsMap, VarID Var) override;
253+
/// Override base class method to run on DBG_VALUEs specifically.
254+
virtual void visitEveryDebugIntrinsic(
255+
DenseSet<VarID> &VarIDSet,
256+
DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
257+
StringRef FuncName, bool Before) override;
258+
};
259+
214260
} // namespace llvm
215261

216-
#endif
262+
#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

@@ -67,6 +68,7 @@ class MachineFunctionPass : public FunctionPass {
6768
MachineFunctionProperties RequiredProperties;
6869
MachineFunctionProperties SetProperties;
6970
MachineFunctionProperties ClearedProperties;
71+
DroppedVariableStatsMIR DroppedVarStatsMF;
7072

7173
/// createPrinterPass - Get a machine function printer pass.
7274
Pass *createPrinterPass(raw_ostream &O,

llvm/lib/CodeGen/DroppedVariableStats.cpp

Lines changed: 61 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

@@ -196,3 +196,63 @@ void DroppedVariableStatsIR::visitEveryDebugIntrinsic(
196196
}
197197
}
198198
}
199+
200+
void DroppedVariableStatsMIR::runOnMachineFunction(const MachineFunction *MF,
201+
bool Before) {
202+
auto &DebugVariables = DebugVariablesStack.back()[&MF->getFunction()];
203+
auto FuncName = MF->getName();
204+
MFunc = MF;
205+
run(DebugVariables, FuncName, Before);
206+
}
207+
208+
void DroppedVariableStatsMIR::calculateDroppedVarStatsOnMachineFunction(
209+
const MachineFunction *MF, StringRef PassID, StringRef FuncOrModName) {
210+
MFunc = MF;
211+
StringRef FuncName = MF->getName();
212+
const Function *Func = &MF->getFunction();
213+
DebugVariables &DbgVariables = DebugVariablesStack.back()[Func];
214+
calculateDroppedStatsAndPrint(DbgVariables, FuncName, PassID, FuncOrModName,
215+
"MachineFunction", Func);
216+
}
217+
218+
void DroppedVariableStatsMIR::visitEveryDebugVariable(
219+
unsigned &DroppedCount, DenseSet<VarID> &DebugVariablesBeforeSet,
220+
DenseSet<VarID> &DebugVariablesAfterSet,
221+
DenseMap<VarID, DILocation *> &InlinedAtsMap, VarID Var) {
222+
unsigned PrevDroppedCount = DroppedCount;
223+
const DIScope *DbgValScope = std::get<0>(Var);
224+
for (const auto &MBB : *MFunc) {
225+
for (const auto &MI : MBB) {
226+
auto *DbgLoc = MI.getDebugLoc().get();
227+
if (!DbgLoc)
228+
continue;
229+
230+
auto *Scope = DbgLoc->getScope();
231+
if (wasDropped(DbgLoc, Scope, DbgValScope, InlinedAtsMap, Var,
232+
DroppedCount))
233+
break;
234+
}
235+
if (PrevDroppedCount != DroppedCount) {
236+
PrevDroppedCount = DroppedCount;
237+
break;
238+
}
239+
}
240+
}
241+
242+
void DroppedVariableStatsMIR::visitEveryDebugIntrinsic(
243+
DenseSet<VarID> &VarIDSet,
244+
DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
245+
StringRef FuncName, bool Before) {
246+
for (const auto &MBB : *MFunc) {
247+
for (const auto &MI : MBB) {
248+
if (MI.isDebugValueLike()) {
249+
auto *DbgVar = MI.getDebugVariable();
250+
if (!DbgVar)
251+
continue;
252+
auto DbgLoc = MI.getDebugLoc();
253+
populateVarIDSetAndInlinedMap(DbgVar, DbgLoc, VarIDSet, InlinedAtsMap,
254+
FuncName, Before);
255+
}
256+
}
257+
}
258+
}

llvm/lib/CodeGen/MachineFunctionPass.cpp

Lines changed: 14 additions & 1 deletion
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);
@@ -91,7 +96,15 @@ bool MachineFunctionPass::runOnFunction(Function &F) {
9196

9297
MFProps.reset(ClearedProperties);
9398

94-
bool RV = runOnMachineFunction(MF);
99+
bool RV;
100+
if (DroppedVarStatsMIR) {
101+
auto PassName = getPassName();
102+
DroppedVarStatsMF.runBeforePass(PassName, &MF);
103+
RV = runOnMachineFunction(MF);
104+
DroppedVarStatsMF.runAfterPass(PassName, &MF);
105+
} else {
106+
RV = runOnMachineFunction(MF);
107+
}
95108

96109
if (ShouldEmitSizeRemarks) {
97110
// 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)