From a59525786d39de4af8d7ee65531c220348ec48b6 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Sun, 12 Jan 2014 12:15:39 +0000 Subject: [PATCH] [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 --- include/llvm/IR/IRPrintingPasses.h | 40 +++++++++++++++++++- include/llvm/InitializePasses.h | 4 +- lib/IR/Core.cpp | 4 +- lib/IR/IRPrintingPasses.cpp | 61 +++++++++++++++++++----------- test/Other/new-pass-manager.ll | 28 ++++++++++++++ tools/opt/Passes.cpp | 12 ++++++ 6 files changed, 122 insertions(+), 27 deletions(-) create mode 100644 test/Other/new-pass-manager.ll diff --git a/include/llvm/IR/IRPrintingPasses.h b/include/llvm/IR/IRPrintingPasses.h index 1d6cf95b0b6..3d759ef57de 100644 --- a/include/llvm/IR/IRPrintingPasses.h +++ b/include/llvm/IR/IRPrintingPasses.h @@ -19,12 +19,16 @@ #ifndef LLVM_IR_PRINTMODULEPASS_H #define LLVM_IR_PRINTMODULEPASS_H +#include "llvm/ADT/StringRef.h" #include namespace llvm { +class BasicBlockPass; +class Function; class FunctionPass; +class Module; class ModulePass; -class BasicBlockPass; +class PreservedAnalyses; class raw_ostream; /// \brief Create and return a pass that writes the module to the specified @@ -42,6 +46,40 @@ FunctionPass *createPrintFunctionPass(raw_ostream &OS, BasicBlockPass *createPrintBasicBlockPass(raw_ostream &OS, const std::string &Banner = ""); +/// \brief Pass for printing a Module as LLVM's text IR assembly. +/// +/// Note: This pass is for use with the new pass manager. Use the create...Pass +/// functions above to create passes for use with the legacy pass manager. +class PrintModulePass { + raw_ostream &OS; + std::string Banner; + +public: + PrintModulePass(); + PrintModulePass(raw_ostream &OS, const std::string &Banner = ""); + + PreservedAnalyses run(Module *M); + + static StringRef name() { return "PrintModulePass"; } +}; + +/// \brief Pass for printing a Function as LLVM's text IR assembly. +/// +/// Note: This pass is for use with the new pass manager. Use the create...Pass +/// functions above to create passes for use with the legacy pass manager. +class PrintFunctionPass { + raw_ostream &OS; + std::string Banner; + +public: + PrintFunctionPass(); + PrintFunctionPass(raw_ostream &OS, const std::string &Banner = ""); + + PreservedAnalyses run(Function *F); + + static StringRef name() { return "PrintFunctionPass"; } +}; + } // End llvm namespace #endif diff --git a/include/llvm/InitializePasses.h b/include/llvm/InitializePasses.h index e27d6b7d393..6c7ab111cb2 100644 --- a/include/llvm/InitializePasses.h +++ b/include/llvm/InitializePasses.h @@ -212,8 +212,8 @@ void initializePostDominatorTreePass(PassRegistry&); void initializePostRASchedulerPass(PassRegistry&); void initializePostMachineSchedulerPass(PassRegistry&); void initializePreVerifierPass(PassRegistry&); -void initializePrintFunctionPassPass(PassRegistry&); -void initializePrintModulePassPass(PassRegistry&); +void initializePrintFunctionPassWrapperPass(PassRegistry&); +void initializePrintModulePassWrapperPass(PassRegistry&); void initializePrintBasicBlockPassPass(PassRegistry&); void initializeProcessImplicitDefsPass(PassRegistry&); void initializePromotePassPass(PassRegistry&); diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp index c53ea4851d3..31e445ecaa1 100644 --- a/lib/IR/Core.cpp +++ b/lib/IR/Core.cpp @@ -41,8 +41,8 @@ using namespace llvm; void llvm::initializeCore(PassRegistry &Registry) { initializeDominatorTreePass(Registry); - initializePrintModulePassPass(Registry); - initializePrintFunctionPassPass(Registry); + initializePrintModulePassWrapperPass(Registry); + initializePrintFunctionPassWrapperPass(Registry); initializePrintBasicBlockPassPass(Registry); initializeVerifierPass(Registry); initializePreVerifierPass(Registry); diff --git a/lib/IR/IRPrintingPasses.cpp b/lib/IR/IRPrintingPasses.cpp index b8e2a2ba594..13336ba55b3 100644 --- a/lib/IR/IRPrintingPasses.cpp +++ b/lib/IR/IRPrintingPasses.cpp @@ -14,25 +14,43 @@ #include "llvm/IR/IRPrintingPasses.h" #include "llvm/IR/Function.h" #include "llvm/IR/Module.h" +#include "llvm/IR/PassManager.h" #include "llvm/Pass.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; +PrintModulePass::PrintModulePass() : OS(dbgs()) {} +PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner) + : OS(OS), Banner(Banner) {} + +PreservedAnalyses PrintModulePass::run(Module *M) { + OS << Banner << *M; + return PreservedAnalyses::all(); +} + +PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {} +PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner) + : OS(OS), Banner(Banner) {} + +PreservedAnalyses PrintFunctionPass::run(Function *F) { + OS << Banner << static_cast(*F); + return PreservedAnalyses::all(); +} + namespace { -class PrintModulePass : public ModulePass { - raw_ostream &Out; - std::string Banner; +class PrintModulePassWrapper : public ModulePass { + PrintModulePass P; public: static char ID; - PrintModulePass() : ModulePass(ID), Out(dbgs()) {} - PrintModulePass(raw_ostream &Out, const std::string &Banner) - : ModulePass(ID), Out(Out), Banner(Banner) {} + PrintModulePassWrapper() : ModulePass(ID) {} + PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner) + : ModulePass(ID), P(OS, Banner) {} bool runOnModule(Module &M) { - Out << Banner << M; + P.run(&M); return false; } @@ -41,19 +59,18 @@ public: } }; -class PrintFunctionPass : public FunctionPass { - raw_ostream &Out; - std::string Banner; +class PrintFunctionPassWrapper : public FunctionPass { + PrintFunctionPass P; public: static char ID; - PrintFunctionPass() : FunctionPass(ID), Out(dbgs()) {} - PrintFunctionPass(raw_ostream &Out, const std::string &Banner) - : FunctionPass(ID), Out(Out), Banner(Banner) {} + PrintFunctionPassWrapper() : FunctionPass(ID) {} + PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner) + : FunctionPass(ID), P(OS, Banner) {} // This pass just prints a banner followed by the function as it's processed. bool runOnFunction(Function &F) { - Out << Banner << static_cast(F); + P.run(&F); return false; } @@ -84,24 +101,24 @@ public: } -char PrintModulePass::ID = 0; -INITIALIZE_PASS(PrintModulePass, "print-module", "Print module to stderr", - false, false) -char PrintFunctionPass::ID = 0; -INITIALIZE_PASS(PrintFunctionPass, "print-function", "Print function to stderr", - false, false) +char PrintModulePassWrapper::ID = 0; +INITIALIZE_PASS(PrintModulePassWrapper, "print-module", + "Print module to stderr", false, false) +char PrintFunctionPassWrapper::ID = 0; +INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function", + "Print function to stderr", false, false) char PrintBasicBlockPass::ID = 0; INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false, false) ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS, const std::string &Banner) { - return new PrintModulePass(OS, Banner); + return new PrintModulePassWrapper(OS, Banner); } FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS, const std::string &Banner) { - return new PrintFunctionPass(OS, Banner); + return new PrintFunctionPassWrapper(OS, Banner); } BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream &OS, diff --git a/test/Other/new-pass-manager.ll b/test/Other/new-pass-manager.ll new file mode 100644 index 00000000000..48b97624b80 --- /dev/null +++ b/test/Other/new-pass-manager.ll @@ -0,0 +1,28 @@ +; This test is essentially doing very basic things with the opt tool and the +; new pass manager pipeline. It will be used to flesh out the feature +; completeness of the opt tool when the new pass manager is engaged. The tests +; may not be useful once it becomes the default or may get spread out into other +; files, but for now this is just going to step the new process through its +; paces. + +; RUN: opt -disable-output -debug-pass-manager -passes=print %s 2>&1 \ +; RUN: | FileCheck %s --check-prefix=CHECK-MODULE-PRINT +; CHECK-MODULE-PRINT: Starting module pass manager +; CHECK-MODULE-PRINT: Running module pass: PrintModulePass +; CHECK-MODULE-PRINT: ModuleID +; CHECK-MODULE-PRINT: define void @foo() +; CHECK-MODULE-PRINT: Finished module pass manager + +; RUN: opt -disable-output -debug-pass-manager -passes='function(print)' %s 2>&1 \ +; RUN: | FileCheck %s --check-prefix=CHECK-FUNCTION-PRINT +; CHECK-FUNCTION-PRINT: Starting module pass manager +; CHECK-FUNCTION-PRINT: Starting function pass manager +; CHECK-FUNCTION-PRINT: Running function pass: PrintFunctionPass +; CHECK-FUNCTION-PRINT-NOT: ModuleID +; CHECK-FUNCTION-PRINT: define void @foo() +; CHECK-FUNCTION-PRINT: Finished function pass manager +; CHECK-FUNCTION-PRINT: Finished module pass manager + +define void @foo() { + ret void +} diff --git a/tools/opt/Passes.cpp b/tools/opt/Passes.cpp index 29be9dee94e..e79ac422eb2 100644 --- a/tools/opt/Passes.cpp +++ b/tools/opt/Passes.cpp @@ -15,7 +15,9 @@ //===----------------------------------------------------------------------===// #include "Passes.h" +#include "llvm/IR/IRPrintingPasses.h" #include "llvm/IR/PassManager.h" +#include "llvm/Support/Debug.h" using namespace llvm; @@ -39,12 +41,14 @@ struct NoOpFunctionPass { // under different macros. static bool isModulePassName(StringRef Name) { if (Name == "no-op-module") return true; + if (Name == "print") return true; return false; } static bool isFunctionPassName(StringRef Name) { if (Name == "no-op-function") return true; + if (Name == "print") return true; return false; } @@ -54,6 +58,10 @@ static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) { MPM.addPass(NoOpModulePass()); return true; } + if (Name == "print") { + MPM.addPass(PrintModulePass(dbgs())); + return true; + } return false; } @@ -62,6 +70,10 @@ static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) { FPM.addPass(NoOpFunctionPass()); return true; } + if (Name == "print") { + FPM.addPass(PrintFunctionPass(dbgs())); + return true; + } return false; } -- 2.34.1