diff --git a/mlir/include/mlir/Pass/PassManager.h b/mlir/include/mlir/Pass/PassManager.h --- a/mlir/include/mlir/Pass/PassManager.h +++ b/mlir/include/mlir/Pass/PassManager.h @@ -85,6 +85,9 @@ /// operation type, it must be the same type as this pass manager. void addPass(std::unique_ptr pass); + /// Clear the pipeline, but not the other options set on this OpPassManager. + void clear(); + /// Add the given pass to a nested pass manager for the given operation kind /// `OpT`. template void addNestedPass(std::unique_ptr pass) { diff --git a/mlir/lib/Pass/Pass.cpp b/mlir/lib/Pass/Pass.cpp --- a/mlir/lib/Pass/Pass.cpp +++ b/mlir/lib/Pass/Pass.cpp @@ -98,6 +98,10 @@ /// operation type, it must be the same type as this pass manager. void addPass(std::unique_ptr pass); + /// Clear the list of passes in this pass manager, other options are + /// preserved. + void clear(); + /// Coalesce adjacent AdaptorPasses into one large adaptor. This runs /// recursively through the pipeline graph. void coalesceAdjacentAdaptorPasses(); @@ -167,6 +171,8 @@ passes.emplace_back(std::move(pass)); } +void OpPassManagerImpl::clear() { passes.clear(); } + void OpPassManagerImpl::coalesceAdjacentAdaptorPasses() { // Bail out early if there are no adaptor passes. if (llvm::none_of(passes, [](std::unique_ptr &pass) { @@ -261,6 +267,8 @@ impl->addPass(std::move(pass)); } +void OpPassManager::clear() { impl->clear(); } + /// Returns the number of passes held by this manager. size_t OpPassManager::size() const { return impl->passes.size(); } diff --git a/mlir/unittests/Pass/PassManagerTest.cpp b/mlir/unittests/Pass/PassManagerTest.cpp --- a/mlir/unittests/Pass/PassManagerTest.cpp +++ b/mlir/unittests/Pass/PassManagerTest.cpp @@ -118,6 +118,11 @@ diagnostic->str(), "'invalid_op' op trying to schedule a pass on an unregistered operation"); + // Check that clearing the pass manager effectively removed the pass. + pm.clear(); + result = pm.run(module.get()); + EXPECT_TRUE(succeeded(result)); + // Check that adding the pass at the top-level triggers a fatal error. ASSERT_DEATH(pm.addPass(std::make_unique()), ""); }