Skip to content

Commit e7ec0c9

Browse files
authored
[CodeGen] Port PrintMIR to new pass manager (#79440)
The legacy version print machine functions to a string stream, then output the module and string in `doFinalization`. This patch break `MIRPrintingPass` into two parts `PrintMIRPreparePass` and `PrintMIRPass`. `PrintMIRPreparePass` output the original IR in yaml string, `PrintMIRPass` just print the machine function, so we can avoid the `doFinalization`.
1 parent 7278cb5 commit e7ec0c9

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

llvm/include/llvm/CodeGen/MIRPrinter.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,33 @@
1414
#ifndef LLVM_CODEGEN_MIRPRINTER_H
1515
#define LLVM_CODEGEN_MIRPRINTER_H
1616

17+
#include "llvm/CodeGen/MachinePassManager.h"
18+
#include "llvm/Support/raw_ostream.h"
19+
1720
namespace llvm {
1821

1922
class MachineBasicBlock;
2023
class MachineFunction;
2124
class Module;
22-
class raw_ostream;
2325
template <typename T> class SmallVectorImpl;
2426

27+
class PrintMIRPreparePass : public MachinePassInfoMixin<PrintMIRPreparePass> {
28+
raw_ostream &OS;
29+
30+
public:
31+
PrintMIRPreparePass(raw_ostream &OS = errs()) : OS(OS) {}
32+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MFAM);
33+
};
34+
35+
class PrintMIRPass : public MachinePassInfoMixin<PrintMIRPass> {
36+
raw_ostream &OS;
37+
38+
public:
39+
PrintMIRPass(raw_ostream &OS = errs()) : OS(OS) {}
40+
PreservedAnalyses run(MachineFunction &MF,
41+
MachineFunctionAnalysisManager &MFAM);
42+
};
43+
2544
/// Print LLVM IR using the MIR serialization format to the given output stream.
2645
void printMIR(raw_ostream &OS, const Module &M);
2746

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis,
126126
#define MACHINE_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR)
127127
#endif
128128
// MACHINE_FUNCTION_PASS("free-machine-function", FreeMachineFunctionPass, ())
129-
// MACHINE_FUNCTION_PASS("mir-printer", PrintMIRPass, ())
130129
MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass, ())
130+
MACHINE_FUNCTION_PASS("print", PrintMIRPass, ())
131131
#undef MACHINE_FUNCTION_PASS
132132

133133
// After a pass is converted to new pass manager, its entry should be moved from
@@ -205,7 +205,6 @@ DUMMY_MACHINE_FUNCTION_PASS("machineinstr-printer", MachineFunctionPrinterPass,
205205
(OS, Banner))
206206
DUMMY_MACHINE_FUNCTION_PASS("machinelicm", MachineLICMPass, ())
207207
DUMMY_MACHINE_FUNCTION_PASS("machineverifier", MachineVerifierPass, (Banner))
208-
DUMMY_MACHINE_FUNCTION_PASS("mir-printer", PrintMIRPass, ())
209208
DUMMY_MACHINE_FUNCTION_PASS("mirfs-discriminators", MIRAddFSDiscriminatorsPass,
210209
(P))
211210
DUMMY_MACHINE_FUNCTION_PASS("opt-phis", OptimizePHIsPass, ())

llvm/lib/CodeGen/MIRPrintingPass.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,20 @@
1515
#include "llvm/CodeGen/MachineFunctionPass.h"
1616
#include "llvm/CodeGen/Passes.h"
1717
#include "llvm/InitializePasses.h"
18-
#include "llvm/Support/Debug.h"
19-
#include "llvm/Support/raw_ostream.h"
2018

2119
using namespace llvm;
2220

21+
PreservedAnalyses PrintMIRPreparePass::run(Module &M, ModuleAnalysisManager &) {
22+
printMIR(OS, M);
23+
return PreservedAnalyses::all();
24+
}
25+
26+
PreservedAnalyses PrintMIRPass::run(MachineFunction &MF,
27+
MachineFunctionAnalysisManager &) {
28+
printMIR(OS, MF);
29+
return PreservedAnalyses::all();
30+
}
31+
2332
namespace {
2433

2534
/// This pass prints out the LLVM IR to an output stream using the MIR

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
#include "llvm/CodeGen/InterleavedLoadCombine.h"
9090
#include "llvm/CodeGen/JMCInstrumenter.h"
9191
#include "llvm/CodeGen/LowerEmuTLS.h"
92+
#include "llvm/CodeGen/MIRPrinter.h"
9293
#include "llvm/CodeGen/SafeStack.h"
9394
#include "llvm/CodeGen/SelectOptimize.h"
9495
#include "llvm/CodeGen/ShadowStackGCLowering.h"

llvm/tools/llc/NewPMDriver.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ int llvm::compileModuleWithNewPM(
168168

169169
MachineFunctionAnalysisManager MFAM(FAM, MAM);
170170

171+
ModulePassManager MPM;
172+
MachineFunctionPassManager MFPM;
173+
171174
if (!PassPipeline.empty()) {
172175
// Construct a custom pass pipeline that starts after instruction
173176
// selection.
@@ -177,21 +180,18 @@ int llvm::compileModuleWithNewPM(
177180
return 1;
178181
}
179182

180-
MachineFunctionPassManager MFPM;
181183
ExitOnErr(PB.parsePassPipeline(MFPM, PassPipeline));
184+
MPM.addPass(PrintMIRPreparePass(*OS));
182185
MFPM.addPass(PrintMIRPass(*OS));
183186
MFPM.addPass(FreeMachineFunctionPass());
184187

185188
auto &MMI = MFAM.getResult<MachineModuleAnalysis>(*M);
186189
if (MIR->parseMachineFunctions(*M, MMI))
187190
return 1;
188191

189-
RunPasses(BOS.get(), Out.get(), M.get(), Context, Buffer, nullptr, nullptr,
190-
MFPM, MFAM);
192+
RunPasses(BOS.get(), Out.get(), M.get(), Context, Buffer, &MPM, &MAM, MFPM,
193+
MFAM);
191194
} else {
192-
ModulePassManager MPM;
193-
MachineFunctionPassManager MFPM;
194-
195195
ExitOnErr(LLVMTM.buildCodeGenPipeline(MPM, MFPM, MFAM, *OS,
196196
DwoOut ? &DwoOut->os() : nullptr,
197197
FileType, Opt, &PIC));

0 commit comments

Comments
 (0)