Skip to content

Commit a80dd15

Browse files
[CodeGen] Port mir-strip-debug to new pass manager (#190738)
Co-authored-by: Aiden Grossman <agrossman154@yahoo.com>
1 parent 66f0da7 commit a80dd15

7 files changed

Lines changed: 109 additions & 42 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This removes debug info from everything. It can be used to ensure tests can
10+
// be debugified without affecting the output MIR.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_CODEGEN_MACHINESTRIPDEBUG_H
15+
#define LLVM_CODEGEN_MACHINESTRIPDEBUG_H
16+
17+
#include "llvm/IR/Analysis.h"
18+
#include "llvm/IR/Module.h"
19+
#include "llvm/IR/PassManager.h"
20+
#include "llvm/Support/Compiler.h"
21+
22+
namespace llvm {
23+
24+
class StripDebugMachineModulePass
25+
: public PassInfoMixin<StripDebugMachineModulePass> {
26+
public:
27+
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
28+
};
29+
30+
} // namespace llvm
31+
32+
#endif // LLVM_CODEGEN_MACHINESTRIPDEBUG_H

llvm/include/llvm/CodeGen/Passes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,8 @@ LLVM_ABI ModulePass *createDebugifyMachineModulePass();
602602
/// If OnlyDebugified is true then it will only strip debug info if it was
603603
/// added by a Debugify pass. The module will be left unchanged if the debug
604604
/// info was generated by another source such as clang.
605-
LLVM_ABI ModulePass *createStripDebugMachineModulePass(bool OnlyDebugified);
605+
LLVM_ABI ModulePass *
606+
createStripDebugMachineModuleLegacyPass(bool OnlyDebugified);
606607

607608
/// Creates MIR Check Debug pass. \see MachineCheckDebugify.cpp
608609
LLVM_ABI ModulePass *createCheckDebugMachineModulePass();

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ MODULE_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
2727
MODULE_PASS("global-merge", GlobalMergePass(TM, GlobalMergeOptions()))
2828
MODULE_PASS("jmc-instrumenter", JMCInstrumenterPass())
2929
MODULE_PASS("lower-emutls", LowerEmuTLSPass())
30+
MODULE_PASS("mir-strip-debug", StripDebugMachineModulePass())
3031
MODULE_PASS("pre-isel-intrinsic-lowering", PreISelIntrinsicLoweringPass())
3132
MODULE_PASS("print<regusage>", PhysicalRegisterUsageInfoPrinterPass(errs()))
3233
MODULE_PASS("shadow-stack-gc-lowering", ShadowStackGCLoweringPass())
@@ -261,7 +262,6 @@ DUMMY_MACHINE_MODULE_PASS("static-data-annotator", StaticDataAnnotator)
261262
DUMMY_MACHINE_MODULE_PASS("pseudo-probe-inserter", PseudoProbeInserterPass)
262263
DUMMY_MACHINE_MODULE_PASS("mir-debugify", DebugifyMachineModule)
263264
DUMMY_MACHINE_MODULE_PASS("mir-check-debugify", CheckDebugMachineModulePass)
264-
DUMMY_MACHINE_MODULE_PASS("mir-strip-debug", StripDebugMachineModulePass)
265265
#undef DUMMY_MACHINE_MODULE_PASS
266266

267267
#ifndef DUMMY_MACHINE_FUNCTION_PASS

llvm/lib/CodeGen/MachineStripDebug.cpp

Lines changed: 71 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010
/// tests can be debugified without affecting the output MIR.
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "llvm/CodeGen/MachineStripDebug.h"
1314
#include "llvm/CodeGen/MachineBasicBlock.h"
1415
#include "llvm/CodeGen/MachineFunction.h"
16+
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
1517
#include "llvm/CodeGen/MachineModuleInfo.h"
1618
#include "llvm/CodeGen/Passes.h"
19+
#include "llvm/IR/Analysis.h"
20+
#include "llvm/IR/Module.h"
21+
#include "llvm/IR/PassManager.h"
1722
#include "llvm/InitializePasses.h"
1823
#include "llvm/Support/CommandLine.h"
1924
#include "llvm/Transforms/Utils/Debugify.h"
@@ -23,60 +28,69 @@
2328
using namespace llvm;
2429

2530
namespace {
31+
2632
cl::opt<bool>
2733
OnlyDebugifiedDefault("mir-strip-debugify-only",
2834
cl::desc("Should mir-strip-debug only strip debug "
2935
"info from debugified modules by default"),
3036
cl::init(true));
3137

32-
struct StripDebugMachineModule : public ModulePass {
33-
bool runOnModule(Module &M) override {
34-
if (OnlyDebugified) {
35-
NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
36-
if (!DebugifyMD) {
37-
LLVM_DEBUG(dbgs() << "Not stripping debug info"
38-
" (debugify metadata not found)?\n");
39-
return false;
40-
}
38+
bool stripDebugMachineModuleImpl(
39+
Module &M, bool OnlyDebugified,
40+
llvm::function_ref<MachineFunction *(Function &)> GetMF) {
41+
if (OnlyDebugified) {
42+
NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
43+
if (!DebugifyMD) {
44+
LLVM_DEBUG(dbgs() << "Not stripping debug info"
45+
" (debugify metadata not found)?\n");
46+
return false;
4147
}
48+
}
4249

43-
MachineModuleInfo &MMI =
44-
getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
45-
46-
bool Changed = false;
47-
for (Function &F : M.functions()) {
48-
MachineFunction *MaybeMF = MMI.getMachineFunction(F);
49-
if (!MaybeMF)
50-
continue;
51-
MachineFunction &MF = *MaybeMF;
52-
for (MachineBasicBlock &MBB : MF) {
53-
for (MachineInstr &MI : llvm::make_early_inc_range(MBB.instrs())) {
54-
if (MI.isDebugInstr()) {
55-
// FIXME: We should remove all of them. However, AArch64 emits an
56-
// invalid `DBG_VALUE $lr` with only one operand instead of
57-
// the usual three and has a test that depends on it's
58-
// preservation. Preserve it for now.
59-
if (MI.getNumOperands() > 1) {
60-
LLVM_DEBUG(dbgs() << "Removing debug instruction " << MI);
61-
MBB.erase_instr(&MI);
62-
Changed |= true;
63-
continue;
64-
}
65-
}
66-
if (MI.getDebugLoc()) {
67-
LLVM_DEBUG(dbgs() << "Removing location " << MI);
68-
MI.setDebugLoc(DebugLoc());
50+
bool Changed = false;
51+
for (Function &F : M.functions()) {
52+
MachineFunction *MaybeMF = GetMF(F);
53+
if (!MaybeMF)
54+
continue;
55+
MachineFunction &MF = *MaybeMF;
56+
for (MachineBasicBlock &MBB : MF) {
57+
for (MachineInstr &MI : llvm::make_early_inc_range(MBB.instrs())) {
58+
if (MI.isDebugInstr()) {
59+
// FIXME: We should remove all of them. However, AArch64 emits an
60+
// invalid `DBG_VALUE $lr` with only one operand instead of
61+
// the usual three and has a test that depends on it's
62+
// preservation. Preserve it for now.
63+
if (MI.getNumOperands() > 1) {
64+
LLVM_DEBUG(dbgs() << "Removing debug instruction " << MI);
65+
MBB.erase_instr(&MI);
6966
Changed |= true;
7067
continue;
7168
}
72-
LLVM_DEBUG(dbgs() << "Keeping " << MI);
7369
}
70+
if (MI.getDebugLoc()) {
71+
LLVM_DEBUG(dbgs() << "Removing location " << MI);
72+
MI.setDebugLoc(DebugLoc());
73+
Changed |= true;
74+
continue;
75+
}
76+
LLVM_DEBUG(dbgs() << "Keeping " << MI);
7477
}
7578
}
79+
}
80+
81+
Changed |= stripDebugifyMetadata(M);
7682

77-
Changed |= stripDebugifyMetadata(M);
83+
return Changed;
84+
}
7885

79-
return Changed;
86+
struct StripDebugMachineModule : public ModulePass {
87+
bool runOnModule(Module &M) override {
88+
MachineModuleInfo &MMI =
89+
getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
90+
return stripDebugMachineModuleImpl(
91+
M, OnlyDebugified, [&MMI](Function &F) -> MachineFunction * {
92+
return MMI.getMachineFunction(F);
93+
});
8094
}
8195

8296
StripDebugMachineModule() : StripDebugMachineModule(OnlyDebugifiedDefault) {}
@@ -103,6 +117,24 @@ INITIALIZE_PASS_BEGIN(StripDebugMachineModule, DEBUG_TYPE,
103117
INITIALIZE_PASS_END(StripDebugMachineModule, DEBUG_TYPE,
104118
"Machine Strip Debug Module", false, false)
105119

106-
ModulePass *llvm::createStripDebugMachineModulePass(bool OnlyDebugified) {
120+
ModulePass *llvm::createStripDebugMachineModuleLegacyPass(bool OnlyDebugified) {
107121
return new StripDebugMachineModule(OnlyDebugified);
108122
}
123+
124+
PreservedAnalyses StripDebugMachineModulePass::run(Module &M,
125+
ModuleAnalysisManager &AM) {
126+
FunctionAnalysisManager &FAM =
127+
AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
128+
const bool Changed = stripDebugMachineModuleImpl(
129+
M, OnlyDebugifiedDefault, [&FAM](Function &F) -> MachineFunction * {
130+
return &FAM.getResult<MachineFunctionAnalysis>(F).getMF();
131+
});
132+
if (!Changed)
133+
return PreservedAnalyses::all();
134+
135+
PreservedAnalyses PA;
136+
PA.preserve<MachineModuleAnalysis>();
137+
PA.preserve<FunctionAnalysisManagerModuleProxy>();
138+
PA.preserveSet<CFGAnalyses>();
139+
return PA;
140+
}

llvm/lib/CodeGen/TargetPassConfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ void TargetPassConfig::addDebugifyPass() {
810810
}
811811

812812
void TargetPassConfig::addStripDebugPass() {
813-
PM->add(createStripDebugMachineModulePass(/*OnlyDebugified=*/true));
813+
PM->add(createStripDebugMachineModuleLegacyPass(/*OnlyDebugified=*/true));
814814
}
815815

816816
void TargetPassConfig::addCheckDebugPass() {

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
#include "llvm/CodeGen/MachineRegisterInfo.h"
135135
#include "llvm/CodeGen/MachineScheduler.h"
136136
#include "llvm/CodeGen/MachineSink.h"
137+
#include "llvm/CodeGen/MachineStripDebug.h"
137138
#include "llvm/CodeGen/MachineTraceMetrics.h"
138139
#include "llvm/CodeGen/MachineUniformityAnalysis.h"
139140
#include "llvm/CodeGen/MachineVerifier.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ MODULE_PASS("memprof-remove-attributes", MemProfRemoveInfo())
119119
MODULE_PASS("memprof-module", ModuleMemProfilerPass())
120120
MODULE_PASS("mergefunc", MergeFunctionsPass())
121121
MODULE_PASS("metarenamer", MetaRenamerPass())
122+
MODULE_PASS("mir-strip-debug", StripDebugMachineModulePass())
122123
MODULE_PASS("module-inline", ModuleInlinerPass())
123124
MODULE_PASS("name-anon-globals", NameAnonGlobalPass())
124125
MODULE_PASS("no-op-module", NoOpModulePass())

0 commit comments

Comments
 (0)