Skip to content

Commit 3bf91ad

Browse files
Add a pass to collect dropped var stats for MIR (#120780)
This patch uses the DroppedVariableStats class to add dropped variable statistics for MIR passes. Reland 1c082c9
1 parent 5e22597 commit 3bf91ad

File tree

7 files changed

+1245
-1
lines changed

7 files changed

+1245
-1
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
///===- DroppedVariableStatsMIR.h - Opt Diagnostics -*- C++ -*-------------===//
2+
///
3+
/// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4+
/// Exceptions. See https://llvm.org/LICENSE.txt for license information.
5+
/// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
///
7+
///===---------------------------------------------------------------------===//
8+
/// \file
9+
/// Dropped Variable Statistics for Debug Information. Reports any number
10+
/// of DBG_VALUEs that get dropped due to an optimization pass.
11+
///
12+
///===---------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_CODEGEN_DROPPEDVARIABLESTATSMIR_H
15+
#define LLVM_CODEGEN_DROPPEDVARIABLESTATSMIR_H
16+
17+
#include "llvm/CodeGen/MachineFunction.h"
18+
#include "llvm/Passes/DroppedVariableStats.h"
19+
20+
namespace llvm {
21+
22+
/// A class to collect and print dropped debug information due to MIR
23+
/// optimization passes. After every MIR pass is run, it will print how many
24+
/// #DBG_VALUEs were dropped due to that pass.
25+
class DroppedVariableStatsMIR : public DroppedVariableStats {
26+
public:
27+
DroppedVariableStatsMIR() : llvm::DroppedVariableStats(false) {}
28+
29+
void runBeforePass(StringRef PassID, MachineFunction *MF) {
30+
if (PassID == "Debug Variable Analysis")
31+
return;
32+
setup();
33+
return runOnMachineFunction(MF, true);
34+
}
35+
36+
void runAfterPass(StringRef PassID, MachineFunction *MF) {
37+
if (PassID == "Debug Variable Analysis")
38+
return;
39+
runOnMachineFunction(MF, false);
40+
calculateDroppedVarStatsOnMachineFunction(MF, PassID, MF->getName().str());
41+
cleanup();
42+
}
43+
44+
private:
45+
const MachineFunction *MFunc;
46+
/// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
47+
/// after a pass has run to facilitate dropped variable calculation for an
48+
/// llvm::MachineFunction.
49+
void runOnMachineFunction(const MachineFunction *MF, bool Before);
50+
/// Iterate over all Instructions in a MachineFunction and report any dropped
51+
/// debug information.
52+
void calculateDroppedVarStatsOnMachineFunction(const MachineFunction *MF,
53+
StringRef PassID,
54+
StringRef FuncOrModName);
55+
/// Override base class method to run on an llvm::MachineFunction
56+
/// specifically.
57+
virtual void
58+
visitEveryInstruction(unsigned &DroppedCount,
59+
DenseMap<VarID, DILocation *> &InlinedAtsMap,
60+
VarID Var) override;
61+
/// Override base class method to run on DBG_VALUEs specifically.
62+
virtual void visitEveryDebugRecord(
63+
DenseSet<VarID> &VarIDSet,
64+
DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
65+
StringRef FuncName, bool Before) override;
66+
};
67+
68+
} // namespace llvm
69+
70+
#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/DroppedVariableStatsMIR.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/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ add_llvm_component_library(LLVMCodeGen
5050
DeadMachineInstructionElim.cpp
5151
DetectDeadLanes.cpp
5252
DFAPacketizer.cpp
53+
DroppedVariableStatsMIR.cpp
5354
DwarfEHPrepare.cpp
5455
EarlyIfConversion.cpp
5556
EdgeBundles.cpp
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
///===- DroppedVariableStatsMIR.cpp ---------------------------------------===//
2+
///
3+
/// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4+
/// Exceptions. See https://llvm.org/LICENSE.txt for license information.
5+
/// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
///
7+
///===---------------------------------------------------------------------===//
8+
/// \file
9+
/// Dropped Variable Statistics for Debug Information. Reports any number
10+
/// of DBG_VALUEs that get dropped due to an optimization pass.
11+
///
12+
///===---------------------------------------------------------------------===//
13+
14+
#include "llvm/CodeGen/DroppedVariableStatsMIR.h"
15+
16+
using namespace llvm;
17+
18+
void DroppedVariableStatsMIR::runOnMachineFunction(const MachineFunction *MF,
19+
bool Before) {
20+
auto &DebugVariables = DebugVariablesStack.back()[&MF->getFunction()];
21+
auto FuncName = MF->getName();
22+
MFunc = MF;
23+
run(DebugVariables, FuncName, Before);
24+
}
25+
26+
void DroppedVariableStatsMIR::calculateDroppedVarStatsOnMachineFunction(
27+
const MachineFunction *MF, StringRef PassID, StringRef FuncOrModName) {
28+
MFunc = MF;
29+
StringRef FuncName = MF->getName();
30+
const Function *Func = &MF->getFunction();
31+
DebugVariables &DbgVariables = DebugVariablesStack.back()[Func];
32+
calculateDroppedStatsAndPrint(DbgVariables, FuncName, PassID, FuncOrModName,
33+
"MachineFunction", Func);
34+
}
35+
36+
void DroppedVariableStatsMIR::visitEveryInstruction(
37+
unsigned &DroppedCount, DenseMap<VarID, DILocation *> &InlinedAtsMap,
38+
VarID Var) {
39+
unsigned PrevDroppedCount = DroppedCount;
40+
const DIScope *DbgValScope = std::get<0>(Var);
41+
for (const auto &MBB : *MFunc) {
42+
for (const auto &MI : MBB) {
43+
if (!MI.isDebugInstr()) {
44+
auto *DbgLoc = MI.getDebugLoc().get();
45+
if (!DbgLoc)
46+
continue;
47+
48+
auto *Scope = DbgLoc->getScope();
49+
if (updateDroppedCount(DbgLoc, Scope, DbgValScope, InlinedAtsMap, Var,
50+
DroppedCount))
51+
break;
52+
}
53+
}
54+
if (PrevDroppedCount != DroppedCount) {
55+
PrevDroppedCount = DroppedCount;
56+
break;
57+
}
58+
}
59+
}
60+
61+
void DroppedVariableStatsMIR::visitEveryDebugRecord(
62+
DenseSet<VarID> &VarIDSet,
63+
DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
64+
StringRef FuncName, bool Before) {
65+
for (const auto &MBB : *MFunc) {
66+
for (const auto &MI : MBB) {
67+
if (MI.isDebugValueLike()) {
68+
auto *DbgVar = MI.getDebugVariable();
69+
if (!DbgVar)
70+
continue;
71+
auto DbgLoc = MI.getDebugLoc();
72+
populateVarIDSetAndInlinedMap(DbgVar, DbgLoc, VarIDSet, InlinedAtsMap,
73+
FuncName, Before);
74+
}
75+
}
76+
}
77+
}

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
@@ -27,6 +27,7 @@ add_llvm_unittest(CodeGenTests
2727
CCStateTest.cpp
2828
DIEHashTest.cpp
2929
DIETest.cpp
30+
DroppedVariableStatsMIRTest.cpp
3031
DwarfStringPoolEntryRefTest.cpp
3132
InstrRefLDVTest.cpp
3233
LowLevelTypeTest.cpp

0 commit comments

Comments
 (0)