[llvm] r199049 - [PM] Add module and function printing passes for the new pass manager.

Chandler Carruth chandlerc at gmail.com
Sun Jan 12 04:15:40 PST 2014


Author: chandlerc
Date: Sun Jan 12 06:15:39 2014
New Revision: 199049

URL: http://llvm.org/viewvc/llvm-project?rev=199049&view=rev
Log:
[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.

Added:
    llvm/trunk/test/Other/new-pass-manager.ll
Modified:
    llvm/trunk/include/llvm/IR/IRPrintingPasses.h
    llvm/trunk/include/llvm/InitializePasses.h
    llvm/trunk/lib/IR/Core.cpp
    llvm/trunk/lib/IR/IRPrintingPasses.cpp
    llvm/trunk/tools/opt/Passes.cpp

Modified: llvm/trunk/include/llvm/IR/IRPrintingPasses.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/IRPrintingPasses.h?rev=199049&r1=199048&r2=199049&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/IRPrintingPasses.h (original)
+++ llvm/trunk/include/llvm/IR/IRPrintingPasses.h Sun Jan 12 06:15:39 2014
@@ -19,12 +19,16 @@
 #ifndef LLVM_IR_PRINTMODULEPASS_H
 #define LLVM_IR_PRINTMODULEPASS_H
 
+#include "llvm/ADT/StringRef.h"
 #include <string>
 
 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(ra
 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

Modified: llvm/trunk/include/llvm/InitializePasses.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=199049&r1=199048&r2=199049&view=diff
==============================================================================
--- llvm/trunk/include/llvm/InitializePasses.h (original)
+++ llvm/trunk/include/llvm/InitializePasses.h Sun Jan 12 06:15:39 2014
@@ -212,8 +212,8 @@ void initializePostDominatorTreePass(Pas
 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&);

Modified: llvm/trunk/lib/IR/Core.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Core.cpp?rev=199049&r1=199048&r2=199049&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Core.cpp (original)
+++ llvm/trunk/lib/IR/Core.cpp Sun Jan 12 06:15:39 2014
@@ -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);

Modified: llvm/trunk/lib/IR/IRPrintingPasses.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/IRPrintingPasses.cpp?rev=199049&r1=199048&r2=199049&view=diff
==============================================================================
--- llvm/trunk/lib/IR/IRPrintingPasses.cpp (original)
+++ llvm/trunk/lib/IR/IRPrintingPasses.cpp Sun Jan 12 06:15:39 2014
@@ -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<Value &>(*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<Value &>(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,

Added: llvm/trunk/test/Other/new-pass-manager.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Other/new-pass-manager.ll?rev=199049&view=auto
==============================================================================
--- llvm/trunk/test/Other/new-pass-manager.ll (added)
+++ llvm/trunk/test/Other/new-pass-manager.ll Sun Jan 12 06:15:39 2014
@@ -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
+}

Modified: llvm/trunk/tools/opt/Passes.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/opt/Passes.cpp?rev=199049&r1=199048&r2=199049&view=diff
==============================================================================
--- llvm/trunk/tools/opt/Passes.cpp (original)
+++ llvm/trunk/tools/opt/Passes.cpp Sun Jan 12 06:15:39 2014
@@ -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(ModulePa
     MPM.addPass(NoOpModulePass());
     return true;
   }
+  if (Name == "print") {
+    MPM.addPass(PrintModulePass(dbgs()));
+    return true;
+  }
   return false;
 }
 
@@ -62,6 +70,10 @@ static bool parseFunctionPassName(Functi
     FPM.addPass(NoOpFunctionPass());
     return true;
   }
+  if (Name == "print") {
+    FPM.addPass(PrintFunctionPass(dbgs()));
+    return true;
+  }
   return false;
 }
 





More information about the llvm-commits mailing list