Add a clear() method on the PassManager (NFC)

This allows to clear an OpPassManager and populated it again with a new
pipeline, while preserving all the other options (including instrumentations).

Differential Revision: https://reviews.llvm.org/D112393
This commit is contained in:
Mehdi Amini 2021-10-25 02:49:46 +00:00
parent 80e6aff6bb
commit a8c1d9d63e
3 changed files with 16 additions and 0 deletions

View File

@ -85,6 +85,9 @@ public:
/// operation type, it must be the same type as this pass manager.
void addPass(std::unique_ptr<Pass> 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 <typename OpT> void addNestedPass(std::unique_ptr<Pass> pass) {

View File

@ -98,6 +98,10 @@ struct OpPassManagerImpl {
/// operation type, it must be the same type as this pass manager.
void addPass(std::unique_ptr<Pass> 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 @@ void OpPassManagerImpl::addPass(std::unique_ptr<Pass> pass) {
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> &pass) {
@ -261,6 +267,8 @@ void OpPassManager::addPass(std::unique_ptr<Pass> pass) {
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(); }

View File

@ -118,6 +118,11 @@ TEST(PassManagerTest, InvalidPass) {
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<InvalidPass>()), "");
}