Skip to content

Commit c20a295

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 4600cae commit c20a295

File tree

6 files changed

+1202
-4
lines changed

6 files changed

+1202
-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

@@ -214,6 +214,52 @@ class DroppedVariableStatsIR : public DroppedVariableStats {
214214
}
215215
};
216216

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

219-
#endif
265+
#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: 70 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

@@ -194,3 +194,72 @@ void DroppedVariableStatsIR::visitEveryDebugIntrinsic(
194194
}
195195
}
196196
}
197+
198+
void DroppedVariableStatsMIR::runOnMachineFunction(const MachineFunction *MF,
199+
bool Before) {
200+
auto &DebugVariables = DebugVariablesStack.back()[&MF->getFunction()];
201+
auto FuncName = MF->getName();
202+
MFunc = MF;
203+
run(DebugVariables, FuncName, Before);
204+
}
205+
206+
void DroppedVariableStatsMIR::calculateDroppedVarStatsOnMachineFunction(
207+
const MachineFunction *MF, StringRef PassID, StringRef FuncOrModName) {
208+
MFunc = MF;
209+
StringRef FuncName = MF->getName();
210+
DebugVariables &DbgVariables = DebugVariablesStack.back()[&MF->getFunction()];
211+
calculateDroppedStatsAndPrint(DbgVariables, FuncName, PassID, FuncOrModName,
212+
"MachineFunction");
213+
}
214+
215+
void DroppedVariableStatsMIR::visitEveryDebugVariable(
216+
unsigned &DroppedCount, DenseSet<VarID> &DebugVariablesBeforeSet,
217+
DenseSet<VarID> &DebugVariablesAfterSet,
218+
DenseMap<VarID, DILocation *> &InlinedAtsMap) {
219+
unsigned PrevDroppedCount = DroppedCount;
220+
for (VarID Var : DebugVariablesBeforeSet) {
221+
if (DebugVariablesAfterSet.contains(Var))
222+
continue;
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 (checkDroppedStatus(DbgLoc, Scope, DbgValScope, InlinedAtsMap,
232+
Var)) {
233+
// Found another instruction in the variable's scope, so there exists
234+
// a break point at which the variable could be observed. Count it as
235+
// dropped.
236+
DroppedCount++;
237+
break;
238+
}
239+
}
240+
if (PrevDroppedCount != DroppedCount) {
241+
PrevDroppedCount = DroppedCount;
242+
break;
243+
}
244+
}
245+
removeVarFromAllSets(Var, &MFunc->getFunction());
246+
}
247+
}
248+
249+
void DroppedVariableStatsMIR::visitEveryDebugIntrinsic(
250+
DenseSet<VarID> &VarIDSet,
251+
DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
252+
StringRef FuncName, bool Before) {
253+
for (const auto &MBB : *MFunc) {
254+
for (const auto &MI : MBB) {
255+
if (MI.isDebugValueLike()) {
256+
auto *DbgVar = MI.getDebugVariable();
257+
if (!DbgVar)
258+
continue;
259+
auto DbgLoc = MI.getDebugLoc();
260+
populateVarIDSetAndInlinedMap(DbgVar, DbgLoc, VarIDSet, InlinedAtsMap,
261+
FuncName, Before);
262+
}
263+
}
264+
}
265+
}

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)