Skip to content

Commit 988bc8d

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. Reland 44de16f
1 parent 5e59fa5 commit 988bc8d

File tree

7 files changed

+1253
-1
lines changed

7 files changed

+1253
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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/IR/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+
31+
void runAfterPass(StringRef PassID, MachineFunction *MF);
32+
33+
private:
34+
const MachineFunction *MFunc;
35+
/// Populate DebugVariablesBefore, DebugVariablesAfter, InlinedAts before or
36+
/// after a pass has run to facilitate dropped variable calculation for an
37+
/// llvm::MachineFunction.
38+
void runOnMachineFunction(const MachineFunction *MF, bool Before);
39+
/// Iterate over all Instructions in a MachineFunction and report any dropped
40+
/// debug information.
41+
void calculateDroppedVarStatsOnMachineFunction(const MachineFunction *MF,
42+
StringRef PassID,
43+
StringRef FuncOrModName);
44+
/// Override base class method to run on an llvm::MachineFunction
45+
/// specifically.
46+
virtual void
47+
visitEveryInstruction(unsigned &DroppedCount,
48+
DenseMap<VarID, DILocation *> &InlinedAtsMap,
49+
VarID Var) override;
50+
/// Override base class method to run on DBG_VALUEs specifically.
51+
virtual void visitEveryDebugRecord(
52+
DenseSet<VarID> &VarIDSet,
53+
DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
54+
StringRef FuncName, bool Before) override;
55+
};
56+
57+
} // namespace llvm
58+
59+
#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: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
#include "llvm/IR/DebugInfoMetadata.h"
16+
17+
using namespace llvm;
18+
19+
void DroppedVariableStatsMIR::runBeforePass(StringRef PassID,
20+
MachineFunction *MF) {
21+
if (PassID == "Debug Variable Analysis")
22+
return;
23+
setup();
24+
return runOnMachineFunction(MF, true);
25+
}
26+
27+
void DroppedVariableStatsMIR::runAfterPass(StringRef PassID,
28+
MachineFunction *MF) {
29+
if (PassID == "Debug Variable Analysis")
30+
return;
31+
runOnMachineFunction(MF, false);
32+
calculateDroppedVarStatsOnMachineFunction(MF, PassID, MF->getName().str());
33+
cleanup();
34+
}
35+
36+
void DroppedVariableStatsMIR::runOnMachineFunction(const MachineFunction *MF,
37+
bool Before) {
38+
auto &DebugVariables = DebugVariablesStack.back()[&MF->getFunction()];
39+
auto FuncName = MF->getName();
40+
MFunc = MF;
41+
run(DebugVariables, FuncName, Before);
42+
}
43+
44+
void DroppedVariableStatsMIR::calculateDroppedVarStatsOnMachineFunction(
45+
const MachineFunction *MF, StringRef PassID, StringRef FuncOrModName) {
46+
MFunc = MF;
47+
StringRef FuncName = MF->getName();
48+
const Function *Func = &MF->getFunction();
49+
DebugVariables &DbgVariables = DebugVariablesStack.back()[Func];
50+
calculateDroppedStatsAndPrint(DbgVariables, FuncName, PassID, FuncOrModName,
51+
"MachineFunction", Func);
52+
}
53+
54+
void DroppedVariableStatsMIR::visitEveryInstruction(
55+
unsigned &DroppedCount, DenseMap<VarID, DILocation *> &InlinedAtsMap,
56+
VarID Var) {
57+
unsigned PrevDroppedCount = DroppedCount;
58+
const DIScope *DbgValScope = std::get<0>(Var);
59+
for (const auto &MBB : *MFunc) {
60+
for (const auto &MI : MBB) {
61+
if (!MI.isDebugInstr()) {
62+
auto *DbgLoc = MI.getDebugLoc().get();
63+
if (!DbgLoc)
64+
continue;
65+
66+
auto *Scope = DbgLoc->getScope();
67+
if (updateDroppedCount(DbgLoc, Scope, DbgValScope, InlinedAtsMap, Var,
68+
DroppedCount))
69+
break;
70+
}
71+
}
72+
if (PrevDroppedCount != DroppedCount) {
73+
PrevDroppedCount = DroppedCount;
74+
break;
75+
}
76+
}
77+
}
78+
79+
void DroppedVariableStatsMIR::visitEveryDebugRecord(
80+
DenseSet<VarID> &VarIDSet,
81+
DenseMap<StringRef, DenseMap<VarID, DILocation *>> &InlinedAtsMap,
82+
StringRef FuncName, bool Before) {
83+
for (const auto &MBB : *MFunc) {
84+
for (const auto &MI : MBB) {
85+
if (MI.isDebugValueLike()) {
86+
auto *DbgVar = MI.getDebugVariable();
87+
if (!DbgVar)
88+
continue;
89+
auto DbgLoc = MI.getDebugLoc();
90+
populateVarIDSetAndInlinedMap(DbgVar, DbgLoc, VarIDSet, InlinedAtsMap,
91+
FuncName, Before);
92+
}
93+
}
94+
}
95+
}

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)