llvm.org GIT mirror llvm / a595257
[PM] Add module and function printing passes for the new pass manager. This implements the legacy passes in terms of the new ones. It adds basic testing using explicit runs of the passes. Next up will be wiring the basic output mechanism of opt up when the new pass manager is engaged unless bitcode writing is requested. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199049 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 6 years ago
6 changed file(s) with 122 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
1818 #ifndef LLVM_IR_PRINTMODULEPASS_H
1919 #define LLVM_IR_PRINTMODULEPASS_H
2020
21 #include "llvm/ADT/StringRef.h"
2122 #include
2223
2324 namespace llvm {
25 class BasicBlockPass;
26 class Function;
2427 class FunctionPass;
28 class Module;
2529 class ModulePass;
26 class BasicBlockPass;
30 class PreservedAnalyses;
2731 class raw_ostream;
2832
2933 /// \brief Create and return a pass that writes the module to the specified
4145 BasicBlockPass *createPrintBasicBlockPass(raw_ostream &OS,
4246 const std::string &Banner = "");
4347
48 /// \brief Pass for printing a Module as LLVM's text IR assembly.
49 ///
50 /// Note: This pass is for use with the new pass manager. Use the create...Pass
51 /// functions above to create passes for use with the legacy pass manager.
52 class PrintModulePass {
53 raw_ostream &OS;
54 std::string Banner;
55
56 public:
57 PrintModulePass();
58 PrintModulePass(raw_ostream &OS, const std::string &Banner = "");
59
60 PreservedAnalyses run(Module *M);
61
62 static StringRef name() { return "PrintModulePass"; }
63 };
64
65 /// \brief Pass for printing a Function as LLVM's text IR assembly.
66 ///
67 /// Note: This pass is for use with the new pass manager. Use the create...Pass
68 /// functions above to create passes for use with the legacy pass manager.
69 class PrintFunctionPass {
70 raw_ostream &OS;
71 std::string Banner;
72
73 public:
74 PrintFunctionPass();
75 PrintFunctionPass(raw_ostream &OS, const std::string &Banner = "");
76
77 PreservedAnalyses run(Function *F);
78
79 static StringRef name() { return "PrintFunctionPass"; }
80 };
81
4482 } // End llvm namespace
4583
4684 #endif
211211 void initializePostRASchedulerPass(PassRegistry&);
212212 void initializePostMachineSchedulerPass(PassRegistry&);
213213 void initializePreVerifierPass(PassRegistry&);
214 void initializePrintFunctionPassPass(PassRegistry&);
215 void initializePrintModulePassPass(PassRegistry&);
214 void initializePrintFunctionPassWrapperPass(PassRegistry&);
215 void initializePrintModulePassWrapperPass(PassRegistry&);
216216 void initializePrintBasicBlockPassPass(PassRegistry&);
217217 void initializeProcessImplicitDefsPass(PassRegistry&);
218218 void initializePromotePassPass(PassRegistry&);
4040
4141 void llvm::initializeCore(PassRegistry &Registry) {
4242 initializeDominatorTreePass(Registry);
43 initializePrintModulePassPass(Registry);
44 initializePrintFunctionPassPass(Registry);
43 initializePrintModulePassWrapperPass(Registry);
44 initializePrintFunctionPassWrapperPass(Registry);
4545 initializePrintBasicBlockPassPass(Registry);
4646 initializeVerifierPass(Registry);
4747 initializePreVerifierPass(Registry);
1313 #include "llvm/IR/IRPrintingPasses.h"
1414 #include "llvm/IR/Function.h"
1515 #include "llvm/IR/Module.h"
16 #include "llvm/IR/PassManager.h"
1617 #include "llvm/Pass.h"
1718 #include "llvm/Support/Debug.h"
1819 #include "llvm/Support/raw_ostream.h"
1920 using namespace llvm;
2021
22 PrintModulePass::PrintModulePass() : OS(dbgs()) {}
23 PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner)
24 : OS(OS), Banner(Banner) {}
25
26 PreservedAnalyses PrintModulePass::run(Module *M) {
27 OS << Banner << *M;
28 return PreservedAnalyses::all();
29 }
30
31 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
32 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
33 : OS(OS), Banner(Banner) {}
34
35 PreservedAnalyses PrintFunctionPass::run(Function *F) {
36 OS << Banner << static_cast(*F);
37 return PreservedAnalyses::all();
38 }
39
2140 namespace {
2241
23 class PrintModulePass : public ModulePass {
24 raw_ostream &Out;
25 std::string Banner;
42 class PrintModulePassWrapper : public ModulePass {
43 PrintModulePass P;
2644
2745 public:
2846 static char ID;
29 PrintModulePass() : ModulePass(ID), Out(dbgs()) {}
30 PrintModulePass(raw_ostream &Out, const std::string &Banner)
31 : ModulePass(ID), Out(Out), Banner(Banner) {}
47 PrintModulePassWrapper() : ModulePass(ID) {}
48 PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner)
49 : ModulePass(ID), P(OS, Banner) {}
3250
3351 bool runOnModule(Module &M) {
34 Out << Banner << M;
52 P.run(&M);
3553 return false;
3654 }
3755
4058 }
4159 };
4260
43 class PrintFunctionPass : public FunctionPass {
44 raw_ostream &Out;
45 std::string Banner;
61 class PrintFunctionPassWrapper : public FunctionPass {
62 PrintFunctionPass P;
4663
4764 public:
4865 static char ID;
49 PrintFunctionPass() : FunctionPass(ID), Out(dbgs()) {}
50 PrintFunctionPass(raw_ostream &Out, const std::string &Banner)
51 : FunctionPass(ID), Out(Out), Banner(Banner) {}
66 PrintFunctionPassWrapper() : FunctionPass(ID) {}
67 PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
68 : FunctionPass(ID), P(OS, Banner) {}
5269
5370 // This pass just prints a banner followed by the function as it's processed.
5471 bool runOnFunction(Function &F) {
55 Out << Banner << static_cast(F);
72 P.run(&F);
5673 return false;
5774 }
5875
83100
84101 }
85102
86 char PrintModulePass::ID = 0;
87 INITIALIZE_PASS(PrintModulePass, "print-module", "Print module to stderr",
88 false, false)
89 char PrintFunctionPass::ID = 0;
90 INITIALIZE_PASS(PrintFunctionPass, "print-function", "Print function to stderr",
91 false, false)
103 char PrintModulePassWrapper::ID = 0;
104 INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
105 "Print module to stderr", false, false)
106 char PrintFunctionPassWrapper::ID = 0;
107 INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
108 "Print function to stderr", false, false)
92109 char PrintBasicBlockPass::ID = 0;
93110 INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false,
94111 false)
95112
96113 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,
97114 const std::string &Banner) {
98 return new PrintModulePass(OS, Banner);
115 return new PrintModulePassWrapper(OS, Banner);
99116 }
100117
101118 FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
102119 const std::string &Banner) {
103 return new PrintFunctionPass(OS, Banner);
120 return new PrintFunctionPassWrapper(OS, Banner);
104121 }
105122
106123 BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream &OS,
0 ; This test is essentially doing very basic things with the opt tool and the
1 ; new pass manager pipeline. It will be used to flesh out the feature
2 ; completeness of the opt tool when the new pass manager is engaged. The tests
3 ; may not be useful once it becomes the default or may get spread out into other
4 ; files, but for now this is just going to step the new process through its
5 ; paces.
6
7 ; RUN: opt -disable-output -debug-pass-manager -passes=print %s 2>&1 \
8 ; RUN: | FileCheck %s --check-prefix=CHECK-MODULE-PRINT
9 ; CHECK-MODULE-PRINT: Starting module pass manager
10 ; CHECK-MODULE-PRINT: Running module pass: PrintModulePass
11 ; CHECK-MODULE-PRINT: ModuleID
12 ; CHECK-MODULE-PRINT: define void @foo()
13 ; CHECK-MODULE-PRINT: Finished module pass manager
14
15 ; RUN: opt -disable-output -debug-pass-manager -passes='function(print)' %s 2>&1 \
16 ; RUN: | FileCheck %s --check-prefix=CHECK-FUNCTION-PRINT
17 ; CHECK-FUNCTION-PRINT: Starting module pass manager
18 ; CHECK-FUNCTION-PRINT: Starting function pass manager
19 ; CHECK-FUNCTION-PRINT: Running function pass: PrintFunctionPass
20 ; CHECK-FUNCTION-PRINT-NOT: ModuleID
21 ; CHECK-FUNCTION-PRINT: define void @foo()
22 ; CHECK-FUNCTION-PRINT: Finished function pass manager
23 ; CHECK-FUNCTION-PRINT: Finished module pass manager
24
25 define void @foo() {
26 ret void
27 }
1414 //===----------------------------------------------------------------------===//
1515
1616 #include "Passes.h"
17 #include "llvm/IR/IRPrintingPasses.h"
1718 #include "llvm/IR/PassManager.h"
19 #include "llvm/Support/Debug.h"
1820
1921 using namespace llvm;
2022
3840 // under different macros.
3941 static bool isModulePassName(StringRef Name) {
4042 if (Name == "no-op-module") return true;
43 if (Name == "print") return true;
4144
4245 return false;
4346 }
4447
4548 static bool isFunctionPassName(StringRef Name) {
4649 if (Name == "no-op-function") return true;
50 if (Name == "print") return true;
4751
4852 return false;
4953 }
5357 MPM.addPass(NoOpModulePass());
5458 return true;
5559 }
60 if (Name == "print") {
61 MPM.addPass(PrintModulePass(dbgs()));
62 return true;
63 }
5664 return false;
5765 }
5866
5967 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
6068 if (Name == "no-op-function") {
6169 FPM.addPass(NoOpFunctionPass());
70 return true;
71 }
72 if (Name == "print") {
73 FPM.addPass(PrintFunctionPass(dbgs()));
6274 return true;
6375 }
6476 return false;