Skip to content

Commit 249755c

Browse files
Reland "Add a pass to collect dropped var stats for MIR" (#117044)
Moved the MIR Test to the unittests/CodeGen folder This is patch is part of a stack of patches, and follows #117042 I moved the MIR test to the unittests/CodeGen folder I am trying to reland #115566
1 parent a0c4f85 commit 249755c

File tree

6 files changed

+1193
-3
lines changed

6 files changed

+1193
-3
lines changed

llvm/include/llvm/CodeGen/DroppedVariableStats.h

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

@@ -221,6 +221,52 @@ class DroppedVariableStatsIR : public DroppedVariableStats {
221221
}
222222
};
223223

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

226272
#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: 62 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

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

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/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ add_llvm_unittest(CodeGenTests
2828
DIEHashTest.cpp
2929
DIETest.cpp
3030
DroppedVariableStatsIRTest.cpp
31+
DroppedVariableStatsMIRTest.cpp
3132
DwarfStringPoolEntryRefTest.cpp
3233
InstrRefLDVTest.cpp
3334
LowLevelTypeTest.cpp

0 commit comments

Comments
 (0)