[PM] Wire up the Verifier for the new pass manager and connect it to the
authorChandler Carruth <chandlerc@gmail.com>
Mon, 20 Jan 2014 11:34:08 +0000 (11:34 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Mon, 20 Jan 2014 11:34:08 +0000 (11:34 +0000)
various opt verifier commandline options.

Mostly mechanical wiring of the verifier to the new pass manager.
Exercises one of the more unusual aspects of it -- a pass can be either
a module or function pass interchangably. If this is ever problematic,
we can make things more constrained, but for things like the verifier
where there is an "obvious" applicability at both levels, it seems
convenient.

This is the next-to-last piece of basic functionality left to make the
opt commandline driving of the new pass manager minimally functional for
testing and further development. There is still a lot to be done there
(notably the factoring into .def files to kill the current boilerplate
code) but it is relatively uninteresting. The only interesting bit left
for minimal functionality is supporting the registration of analyses.
I'm planning on doing that on top of the .def file switch mostly because
the boilerplate for the analyses would be significantly worse.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199646 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/Verifier.h
include/llvm/InitializePasses.h
lib/IR/Core.cpp
lib/IR/Verifier.cpp
test/Other/new-pass-manager.ll
tools/opt/NewPMDriver.cpp
tools/opt/NewPMDriver.h
tools/opt/Passes.cpp
tools/opt/Passes.h
tools/opt/opt.cpp

index 0f146e6936d85fefd90c0ee42c12843560a4edcb..9a2f402ac8f8a06090b933e3516f21fe7d4b0486 100644 (file)
 #ifndef LLVM_IR_VERIFIER_H
 #define LLVM_IR_VERIFIER_H
 
+#include "llvm/ADT/StringRef.h"
 #include <string>
 
 namespace llvm {
 
+class Function;
 class FunctionPass;
 class Module;
-class Function;
+class PreservedAnalyses;
 class raw_ostream;
 
 /// \brief Check a function for errors, useful for use when debugging a
@@ -52,8 +54,22 @@ bool verifyModule(const Module &M, raw_ostream *OS = 0);
 /// functionality. When the pass detects a verification error it is always
 /// printed to stderr, and by default they are fatal. You can override that by
 /// passing \c false to \p FatalErrors.
+///
+/// Note that this creates a pass suitable for the legacy pass manager. It has nothing to do with \c VerifierPass.
 FunctionPass *createVerifierPass(bool FatalErrors = true);
 
+class VerifierPass {
+  bool FatalErrors;
+
+public:
+  explicit VerifierPass(bool FatalErrors = true) : FatalErrors(FatalErrors) {}
+
+  PreservedAnalyses run(Module *M);
+  PreservedAnalyses run(Function *F);
+
+  static StringRef name() { return "VerifierPass"; }
+};
+
 } // End llvm namespace
 
 #endif
index e08e0c4baf202c64ee40fc1ad97d63a8008034f4..36efee570b4efaa5ca3f019c2f73f99d485df768 100644 (file)
@@ -256,7 +256,7 @@ void initializeTypeBasedAliasAnalysisPass(PassRegistry&);
 void initializeUnifyFunctionExitNodesPass(PassRegistry&);
 void initializeUnreachableBlockElimPass(PassRegistry&);
 void initializeUnreachableMachineBlockElimPass(PassRegistry&);
-void initializeVerifierPassPass(PassRegistry&);
+void initializeVerifierLegacyPassPass(PassRegistry&);
 void initializeVirtRegMapPass(PassRegistry&);
 void initializeVirtRegRewriterPass(PassRegistry&);
 void initializeInstSimplifierPass(PassRegistry&);
index cd7cb9ed147226694cf7fbc931741f6286dd6a37..3a6a0b0b4471d230d2d6bfeceea85e92beeadf33 100644 (file)
@@ -44,7 +44,7 @@ void llvm::initializeCore(PassRegistry &Registry) {
   initializePrintModulePassWrapperPass(Registry);
   initializePrintFunctionPassWrapperPass(Registry);
   initializePrintBasicBlockPassPass(Registry);
-  initializeVerifierPassPass(Registry);
+  initializeVerifierLegacyPassPass(Registry);
 }
 
 void LLVMInitializeCore(LLVMPassRegistryRef R) {
index feea485019593c344e736739a4b4f2254bd143e7..f81260960e8ebb006b688d73ba3e54319361374e 100644 (file)
@@ -62,9 +62,9 @@
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Module.h"
+#include "llvm/IR/PassManager.h"
 #include "llvm/InstVisitor.h"
 #include "llvm/Pass.h"
-#include "llvm/PassManager.h"
 #include "llvm/Support/CFG.h"
 #include "llvm/Support/CallSite.h"
 #include "llvm/Support/CommandLine.h"
@@ -2387,18 +2387,18 @@ bool llvm::verifyModule(const Module &M, raw_ostream *OS) {
 }
 
 namespace {
-struct VerifierPass : public FunctionPass {
+struct VerifierLegacyPass : public FunctionPass {
   static char ID;
 
   Verifier V;
   bool FatalErrors;
 
-  VerifierPass() : FunctionPass(ID), FatalErrors(true) {
-    initializeVerifierPassPass(*PassRegistry::getPassRegistry());
+  VerifierLegacyPass() : FunctionPass(ID), FatalErrors(true) {
+    initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
   }
-  explicit VerifierPass(bool FatalErrors)
+  explicit VerifierLegacyPass(bool FatalErrors)
       : FunctionPass(ID), V(dbgs()), FatalErrors(FatalErrors) {
-    initializeVerifierPassPass(*PassRegistry::getPassRegistry());
+    initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
   }
 
   bool runOnFunction(Function &F) {
@@ -2421,10 +2421,23 @@ struct VerifierPass : public FunctionPass {
 };
 }
 
-char VerifierPass::ID = 0;
-INITIALIZE_PASS(VerifierPass, "verify", "Module Verifier", false, false)
+char VerifierLegacyPass::ID = 0;
+INITIALIZE_PASS(VerifierLegacyPass, "verify", "Module Verifier", false, false)
 
 FunctionPass *llvm::createVerifierPass(bool FatalErrors) {
-  return new VerifierPass(FatalErrors);
+  return new VerifierLegacyPass(FatalErrors);
 }
 
+PreservedAnalyses VerifierPass::run(Module *M) {
+  if (verifyModule(*M, &dbgs()) && FatalErrors)
+    report_fatal_error("Broken module found, compilation aborted!");
+
+  return PreservedAnalyses::all();
+}
+
+PreservedAnalyses VerifierPass::run(Function *F) {
+  if (verifyFunction(*F, &dbgs()) && FatalErrors)
+    report_fatal_error("Broken function found, compilation aborted!");
+
+  return PreservedAnalyses::all();
+}
index 376bb2625ef6a305fe63ff2eecf3cef36d46cf1c..cec01b54ff0cd860ff1c9c7dd9eda8bd406c5f5c 100644 (file)
@@ -8,19 +8,23 @@
 ; 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: VerifierPass
 ; CHECK-MODULE-PRINT: Running module pass: PrintModulePass
 ; CHECK-MODULE-PRINT: ModuleID
 ; CHECK-MODULE-PRINT: define void @foo()
+; CHECK-MODULE-PRINT: Running module pass: VerifierPass
 ; 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: Running module pass: VerifierPass
 ; 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: Running module pass: VerifierPass
 ; CHECK-FUNCTION-PRINT: Finished module pass manager
 
 ; RUN: opt -S -o - -passes='no-op-module,no-op-module' %s \
 ; RUN:     | llvm-dis \
 ; RUN:     | FileCheck %s --check-prefix=CHECK-NOOP
 
+; RUN: opt -disable-output -debug-pass-manager -verify-each -passes='no-op-module,function(no-op-function)' %s 2>&1 \
+; RUN:     | FileCheck %s --check-prefix=CHECK-VERIFY-EACH
+; CHECK-VERIFY-EACH: Starting module pass manager
+; CHECK-VERIFY-EACH: Running module pass: VerifierPass
+; CHECK-VERIFY-EACH: Running module pass: NoOpModulePass
+; CHECK-VERIFY-EACH: Running module pass: VerifierPass
+; CHECK-VERIFY-EACH: Starting function pass manager
+; CHECK-VERIFY-EACH: Running function pass: NoOpFunctionPass
+; CHECK-VERIFY-EACH: Running function pass: VerifierPass
+; CHECK-VERIFY-EACH: Finished function pass manager
+; CHECK-VERIFY-EACH: Running module pass: VerifierPass
+; CHECK-VERIFY-EACH: Finished module pass manager
+
+; RUN: opt -disable-output -debug-pass-manager -disable-verify -passes='no-op-module,function(no-op-function)' %s 2>&1 \
+; RUN:     | FileCheck %s --check-prefix=CHECK-NO-VERIFY
+; CHECK-NO-VERIFY: Starting module pass manager
+; CHECK-NO-VERIFY-NOT: VerifierPass
+; CHECK-NO-VERIFY: Running module pass: NoOpModulePass
+; CHECK-NO-VERIFY-NOT: VerifierPass
+; CHECK-NO-VERIFY: Starting function pass manager
+; CHECK-NO-VERIFY: Running function pass: NoOpFunctionPass
+; CHECK-NO-VERIFY-NOT: VerifierPass
+; CHECK-NO-VERIFY: Finished function pass manager
+; CHECK-NO-VERIFY-NOT: VerifierPass
+; CHECK-NO-VERIFY: Finished module pass manager
+
 define void @foo() {
   ret void
 }
index 2d210387f84de27e40373d42b176466eab24d8bb..f21a68fc8aa42411a419522486872765a5b59126 100644 (file)
@@ -21,6 +21,7 @@
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/PassManager.h"
+#include "llvm/IR/Verifier.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/ToolOutputFile.h"
@@ -30,13 +31,20 @@ using namespace opt_tool;
 
 bool llvm::runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
                            tool_output_file *Out, StringRef PassPipeline,
-                           OutputKind OK) {
+                           OutputKind OK, VerifierKind VK) {
   ModulePassManager MPM;
-  if (!parsePassPipeline(MPM, PassPipeline)) {
+
+  if (VK > VK_NoVerifier)
+    MPM.addPass(VerifierPass());
+
+  if (!parsePassPipeline(MPM, PassPipeline, VK == VK_VerifyEachPass)) {
     errs() << Arg0 << ": unable to parse pass pipeline description.\n";
     return false;
   }
 
+  if (VK > VK_NoVerifier)
+    MPM.addPass(VerifierPass());
+
   // Add any relevant output pass at the end of the pipeline.
   switch (OK) {
   case OK_NoOutput:
index 2ae1ad5d8383cf06d1e22e4d4066b7dc5a039263..3661d3e6778a4fb168463ad21e47db605d099487 100644 (file)
@@ -34,6 +34,11 @@ enum OutputKind {
   OK_OutputAssembly,
   OK_OutputBitcode
 };
+enum VerifierKind {
+  VK_NoVerifier,
+  VK_VerifyInAndOut,
+  VK_VerifyEachPass
+};
 }
 
 /// \brief Driver function to run the new pass manager over a module.
@@ -44,7 +49,7 @@ enum OutputKind {
 /// when the transition finishes.
 bool runPassPipeline(StringRef Arg0, LLVMContext &Context, Module &M,
                      tool_output_file *Out, StringRef PassPipeline,
-                     opt_tool::OutputKind OK);
+                     opt_tool::OutputKind OK, opt_tool::VerifierKind VK);
 }
 
 #endif
index e79ac422eb24b241ab659479605cc5f84d26ec58..36fe6ad12627b2ac9fc69db79aa129eb3e2b35fd 100644 (file)
@@ -17,6 +17,7 @@
 #include "Passes.h"
 #include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/PassManager.h"
+#include "llvm/IR/Verifier.h"
 #include "llvm/Support/Debug.h"
 
 using namespace llvm;
@@ -78,7 +79,8 @@ static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
 }
 
 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
-                                      StringRef &PipelineText) {
+                                      StringRef &PipelineText,
+                                      bool VerifyEachPass) {
   for (;;) {
     // Parse nested pass managers by recursing.
     if (PipelineText.startswith("function(")) {
@@ -86,7 +88,7 @@ static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
 
       // Parse the inner pipeline inte the nested manager.
       PipelineText = PipelineText.substr(strlen("function("));
-      if (!parseFunctionPassPipeline(NestedFPM, PipelineText) ||
+      if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
           PipelineText.empty())
         return false;
       assert(PipelineText[0] == ')');
@@ -99,6 +101,8 @@ static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
       size_t End = PipelineText.find_first_of(",)");
       if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
         return false;
+      if (VerifyEachPass)
+        FPM.addPass(VerifierPass());
 
       PipelineText = PipelineText.substr(End);
     }
@@ -112,7 +116,8 @@ static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
 }
 
 static bool parseModulePassPipeline(ModulePassManager &MPM,
-                                    StringRef &PipelineText) {
+                                    StringRef &PipelineText,
+                                    bool VerifyEachPass) {
   for (;;) {
     // Parse nested pass managers by recursing.
     if (PipelineText.startswith("module(")) {
@@ -120,7 +125,7 @@ static bool parseModulePassPipeline(ModulePassManager &MPM,
 
       // Parse the inner pipeline into the nested manager.
       PipelineText = PipelineText.substr(strlen("module("));
-      if (!parseModulePassPipeline(NestedMPM, PipelineText) ||
+      if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass) ||
           PipelineText.empty())
         return false;
       assert(PipelineText[0] == ')');
@@ -133,7 +138,7 @@ static bool parseModulePassPipeline(ModulePassManager &MPM,
 
       // Parse the inner pipeline inte the nested manager.
       PipelineText = PipelineText.substr(strlen("function("));
-      if (!parseFunctionPassPipeline(NestedFPM, PipelineText) ||
+      if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass) ||
           PipelineText.empty())
         return false;
       assert(PipelineText[0] == ')');
@@ -146,6 +151,8 @@ static bool parseModulePassPipeline(ModulePassManager &MPM,
       size_t End = PipelineText.find_first_of(",)");
       if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
         return false;
+      if (VerifyEachPass)
+        MPM.addPass(VerifierPass());
 
       PipelineText = PipelineText.substr(End);
     }
@@ -161,13 +168,16 @@ static bool parseModulePassPipeline(ModulePassManager &MPM,
 // Primary pass pipeline description parsing routine.
 // FIXME: Should this routine accept a TargetMachine or require the caller to
 // pre-populate the analysis managers with target-specific stuff?
-bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText) {
+bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
+                             bool VerifyEachPass) {
   // Look at the first entry to figure out which layer to start parsing at.
   if (PipelineText.startswith("module("))
-    return parseModulePassPipeline(MPM, PipelineText) && PipelineText.empty();
+    return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
+           PipelineText.empty();
   if (PipelineText.startswith("function(")) {
     FunctionPassManager FPM;
-    if (!parseFunctionPassPipeline(FPM, PipelineText) || !PipelineText.empty())
+    if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
+        !PipelineText.empty())
       return false;
     MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
     return true;
@@ -177,11 +187,13 @@ bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText) {
   StringRef FirstName =
       PipelineText.substr(0, PipelineText.find_first_of(",)"));
   if (isModulePassName(FirstName))
-    return parseModulePassPipeline(MPM, PipelineText) && PipelineText.empty();
+    return parseModulePassPipeline(MPM, PipelineText, VerifyEachPass) &&
+           PipelineText.empty();
 
   if (isFunctionPassName(FirstName)) {
     FunctionPassManager FPM;
-    if (!parseFunctionPassPipeline(FPM, PipelineText) || !PipelineText.empty())
+    if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass) ||
+        !PipelineText.empty())
       return false;
     MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
     return true;
index 6016b74c80bcf5b6c1ef247e6b53010ca22f9451..3bd675269245d4e54b8ce54e894c86b66c8655c5 100644 (file)
@@ -49,7 +49,8 @@ class ModulePassManager;
 /// the sequence of passes aren't all the exact same kind of pass, it will be
 /// an error. You cannot mix different levels implicitly, you must explicitly
 /// form a pass manager in which to nest passes.
-bool parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText);
+bool parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText,
+                       bool VerifyEachPass = true);
 
 }
 
index ef8c504d0fee3731d8f783aa614c62d7988aa560..782cac213f034f1ad7510b1ea4f197ec9a3c97c6 100644 (file)
@@ -676,11 +676,17 @@ int main(int argc, char **argv) {
     if (!NoOutput)
       OK = OutputAssembly ? OK_OutputAssembly : OK_OutputBitcode;
 
+    VerifierKind VK = VK_VerifyInAndOut;
+    if (NoVerify)
+      VK = VK_NoVerifier;
+    else if (VerifyEach)
+      VK = VK_VerifyEachPass;
+
     // The user has asked to use the new pass manager and provided a pipeline
     // string. Hand off the rest of the functionality to the new code for that
     // layer.
     return runPassPipeline(argv[0], Context, *M.get(), Out.get(), PassPipeline,
-                           OK)
+                           OK, VK)
                ? 0
                : 1;
   }