Skip to content

Commit 94f4cf9

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 94f4cf9

File tree

6 files changed

+1253
-1
lines changed

6 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/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: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
2121
#include "llvm/Analysis/ScalarEvolution.h"
2222
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
23+
#include "llvm/CodeGen/DroppedVariableStatsMIR.h"
2324
#include "llvm/CodeGen/MachineFunction.h"
2425
#include "llvm/CodeGen/MachineModuleInfo.h"
2526
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
@@ -32,6 +33,11 @@
3233
using namespace llvm;
3334
using namespace ore;
3435

36+
static cl::opt<bool> DroppedVarStatsMIR(
37+
"dropped-variable-stats-mir", cl::Hidden,
38+
cl::desc("Dump dropped debug variables stats for MIR passes"),
39+
cl::init(false));
40+
3541
Pass *MachineFunctionPass::createPrinterPass(raw_ostream &O,
3642
const std::string &Banner) const {
3743
return createMachineFunctionPrinterPass(O, Banner);
@@ -91,7 +97,16 @@ bool MachineFunctionPass::runOnFunction(Function &F) {
9197

9298
MFProps.reset(ClearedProperties);
9399

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

96111
if (ShouldEmitSizeRemarks) {
97112
// 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)