xref: /llvm-project-15.0.7/mlir/lib/Pass/Pass.cpp (revision ff9e596b)
1 //===- Pass.cpp - Pass infrastructure implementation ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements common pass infrastructure.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Pass/Pass.h"
14 #include "PassDetail.h"
15 #include "mlir/IR/Diagnostics.h"
16 #include "mlir/IR/Dialect.h"
17 #include "mlir/IR/Threading.h"
18 #include "mlir/IR/Verifier.h"
19 #include "mlir/Support/FileUtilities.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/ScopeExit.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/CrashRecoveryContext.h"
25 #include "llvm/Support/Mutex.h"
26 #include "llvm/Support/Signals.h"
27 #include "llvm/Support/Threading.h"
28 #include "llvm/Support/ToolOutputFile.h"
29 
30 using namespace mlir;
31 using namespace mlir::detail;
32 
33 //===----------------------------------------------------------------------===//
34 // Pass
35 //===----------------------------------------------------------------------===//
36 
37 /// Out of line virtual method to ensure vtables and metadata are emitted to a
38 /// single .o file.
39 void Pass::anchor() {}
40 
41 /// Attempt to initialize the options of this pass from the given string.
42 LogicalResult Pass::initializeOptions(StringRef options) {
43   return passOptions.parseFromString(options);
44 }
45 
46 /// Copy the option values from 'other', which is another instance of this
47 /// pass.
48 void Pass::copyOptionValuesFrom(const Pass *other) {
49   passOptions.copyOptionValuesFrom(other->passOptions);
50 }
51 
52 /// Prints out the pass in the textual representation of pipelines. If this is
53 /// an adaptor pass, print with the op_name(sub_pass,...) format.
54 void Pass::printAsTextualPipeline(raw_ostream &os) {
55   // Special case for adaptors to use the 'op_name(sub_passes)' format.
56   if (auto *adaptor = dyn_cast<OpToOpPassAdaptor>(this)) {
57     llvm::interleaveComma(adaptor->getPassManagers(), os,
58                           [&](OpPassManager &pm) {
59                             os << pm.getOpName() << "(";
60                             pm.printAsTextualPipeline(os);
61                             os << ")";
62                           });
63     return;
64   }
65   // Otherwise, print the pass argument followed by its options. If the pass
66   // doesn't have an argument, print the name of the pass to give some indicator
67   // of what pass was run.
68   StringRef argument = getArgument();
69   if (!argument.empty())
70     os << argument;
71   else
72     os << "unknown<" << getName() << ">";
73   passOptions.print(os);
74 }
75 
76 //===----------------------------------------------------------------------===//
77 // OpPassManagerImpl
78 //===----------------------------------------------------------------------===//
79 
80 namespace mlir {
81 namespace detail {
82 struct OpPassManagerImpl {
83   OpPassManagerImpl(OperationName opName, OpPassManager::Nesting nesting)
84       : name(opName.getStringRef()), opName(opName),
85         initializationGeneration(0), nesting(nesting) {}
86   OpPassManagerImpl(StringRef name, OpPassManager::Nesting nesting)
87       : name(name), initializationGeneration(0), nesting(nesting) {}
88 
89   /// Merge the passes of this pass manager into the one provided.
90   void mergeInto(OpPassManagerImpl &rhs);
91 
92   /// Nest a new operation pass manager for the given operation kind under this
93   /// pass manager.
94   OpPassManager &nest(StringAttr nestedName);
95   OpPassManager &nest(StringRef nestedName);
96 
97   /// Add the given pass to this pass manager. If this pass has a concrete
98   /// operation type, it must be the same type as this pass manager.
99   void addPass(std::unique_ptr<Pass> pass);
100 
101   /// Clear the list of passes in this pass manager, other options are
102   /// preserved.
103   void clear();
104 
105   /// Finalize the pass list in preparation for execution. This includes
106   /// coalescing adjacent pass managers when possible, verifying scheduled
107   /// passes, etc.
108   LogicalResult finalizePassList(MLIRContext *ctx);
109 
110   /// Return the operation name of this pass manager.
111   OperationName getOpName(MLIRContext &context) {
112     if (!opName)
113       opName = OperationName(name, &context);
114     return *opName;
115   }
116 
117   /// The name of the operation that passes of this pass manager operate on.
118   std::string name;
119 
120   /// The cached OperationName (internalized in the context) for the name of the
121   /// operation that passes of this pass manager operate on.
122   Optional<OperationName> opName;
123 
124   /// The set of passes to run as part of this pass manager.
125   std::vector<std::unique_ptr<Pass>> passes;
126 
127   /// The current initialization generation of this pass manager. This is used
128   /// to indicate when a pass manager should be reinitialized.
129   unsigned initializationGeneration;
130 
131   /// Control the implicit nesting of passes that mismatch the name set for this
132   /// OpPassManager.
133   OpPassManager::Nesting nesting;
134 };
135 } // namespace detail
136 } // namespace mlir
137 
138 void OpPassManagerImpl::mergeInto(OpPassManagerImpl &rhs) {
139   assert(name == rhs.name && "merging unrelated pass managers");
140   for (auto &pass : passes)
141     rhs.passes.push_back(std::move(pass));
142   passes.clear();
143 }
144 
145 OpPassManager &OpPassManagerImpl::nest(StringAttr nestedName) {
146   OpPassManager nested(nestedName, nesting);
147   auto *adaptor = new OpToOpPassAdaptor(std::move(nested));
148   addPass(std::unique_ptr<Pass>(adaptor));
149   return adaptor->getPassManagers().front();
150 }
151 
152 OpPassManager &OpPassManagerImpl::nest(StringRef nestedName) {
153   OpPassManager nested(nestedName, nesting);
154   auto *adaptor = new OpToOpPassAdaptor(std::move(nested));
155   addPass(std::unique_ptr<Pass>(adaptor));
156   return adaptor->getPassManagers().front();
157 }
158 
159 void OpPassManagerImpl::addPass(std::unique_ptr<Pass> pass) {
160   // If this pass runs on a different operation than this pass manager, then
161   // implicitly nest a pass manager for this operation if enabled.
162   auto passOpName = pass->getOpName();
163   if (passOpName && passOpName->str() != name) {
164     if (nesting == OpPassManager::Nesting::Implicit)
165       return nest(*passOpName).addPass(std::move(pass));
166     llvm::report_fatal_error(llvm::Twine("Can't add pass '") + pass->getName() +
167                              "' restricted to '" + *passOpName +
168                              "' on a PassManager intended to run on '" + name +
169                              "', did you intend to nest?");
170   }
171 
172   passes.emplace_back(std::move(pass));
173 }
174 
175 void OpPassManagerImpl::clear() { passes.clear(); }
176 
177 LogicalResult OpPassManagerImpl::finalizePassList(MLIRContext *ctx) {
178   // Walk the pass list and merge adjacent adaptors.
179   OpToOpPassAdaptor *lastAdaptor = nullptr;
180   for (auto &pass : passes) {
181     // Check to see if this pass is an adaptor.
182     if (auto *currentAdaptor = dyn_cast<OpToOpPassAdaptor>(pass.get())) {
183       // If it is the first adaptor in a possible chain, remember it and
184       // continue.
185       if (!lastAdaptor) {
186         lastAdaptor = currentAdaptor;
187         continue;
188       }
189 
190       // Otherwise, merge into the existing adaptor and delete the current one.
191       currentAdaptor->mergeInto(*lastAdaptor);
192       pass.reset();
193     } else if (lastAdaptor) {
194       // If this pass is not an adaptor, then finalize and forget any existing
195       // adaptor.
196       for (auto &pm : lastAdaptor->getPassManagers())
197         if (failed(pm.getImpl().finalizePassList(ctx)))
198           return failure();
199       lastAdaptor = nullptr;
200     }
201   }
202 
203   // If there was an adaptor at the end of the manager, finalize it as well.
204   if (lastAdaptor) {
205     for (auto &pm : lastAdaptor->getPassManagers())
206       if (failed(pm.getImpl().finalizePassList(ctx)))
207         return failure();
208   }
209 
210   // Now that the adaptors have been merged, erase any empty slots corresponding
211   // to the merged adaptors that were nulled-out in the loop above.
212   Optional<RegisteredOperationName> opName =
213       getOpName(*ctx).getRegisteredInfo();
214   llvm::erase_if(passes, std::logical_not<std::unique_ptr<Pass>>());
215 
216   // Verify that all of the passes are valid for the operation.
217   for (std::unique_ptr<Pass> &pass : passes) {
218     if (opName && !pass->canScheduleOn(*opName)) {
219       return emitError(UnknownLoc::get(ctx))
220              << "unable to schedule pass '" << pass->getName()
221              << "' on a PassManager intended to run on '" << name << "'!";
222     }
223   }
224   return success();
225 }
226 
227 //===----------------------------------------------------------------------===//
228 // OpPassManager
229 //===----------------------------------------------------------------------===//
230 
231 OpPassManager::OpPassManager(StringAttr name, Nesting nesting)
232     : impl(new OpPassManagerImpl(name, nesting)) {}
233 OpPassManager::OpPassManager(StringRef name, Nesting nesting)
234     : impl(new OpPassManagerImpl(name, nesting)) {}
235 OpPassManager::OpPassManager(OpPassManager &&rhs) : impl(std::move(rhs.impl)) {}
236 OpPassManager::OpPassManager(const OpPassManager &rhs) { *this = rhs; }
237 OpPassManager &OpPassManager::operator=(const OpPassManager &rhs) {
238   impl = std::make_unique<OpPassManagerImpl>(rhs.impl->name, rhs.impl->nesting);
239   impl->initializationGeneration = rhs.impl->initializationGeneration;
240   for (auto &pass : rhs.impl->passes) {
241     auto newPass = pass->clone();
242     newPass->threadingSibling = pass.get();
243     impl->passes.push_back(std::move(newPass));
244   }
245   return *this;
246 }
247 
248 OpPassManager::~OpPassManager() = default;
249 
250 OpPassManager::pass_iterator OpPassManager::begin() {
251   return MutableArrayRef<std::unique_ptr<Pass>>{impl->passes}.begin();
252 }
253 OpPassManager::pass_iterator OpPassManager::end() {
254   return MutableArrayRef<std::unique_ptr<Pass>>{impl->passes}.end();
255 }
256 
257 OpPassManager::const_pass_iterator OpPassManager::begin() const {
258   return ArrayRef<std::unique_ptr<Pass>>{impl->passes}.begin();
259 }
260 OpPassManager::const_pass_iterator OpPassManager::end() const {
261   return ArrayRef<std::unique_ptr<Pass>>{impl->passes}.end();
262 }
263 
264 /// Nest a new operation pass manager for the given operation kind under this
265 /// pass manager.
266 OpPassManager &OpPassManager::nest(StringAttr nestedName) {
267   return impl->nest(nestedName);
268 }
269 OpPassManager &OpPassManager::nest(StringRef nestedName) {
270   return impl->nest(nestedName);
271 }
272 
273 /// Add the given pass to this pass manager. If this pass has a concrete
274 /// operation type, it must be the same type as this pass manager.
275 void OpPassManager::addPass(std::unique_ptr<Pass> pass) {
276   impl->addPass(std::move(pass));
277 }
278 
279 void OpPassManager::clear() { impl->clear(); }
280 
281 /// Returns the number of passes held by this manager.
282 size_t OpPassManager::size() const { return impl->passes.size(); }
283 
284 /// Returns the internal implementation instance.
285 OpPassManagerImpl &OpPassManager::getImpl() { return *impl; }
286 
287 /// Return the operation name that this pass manager operates on.
288 StringRef OpPassManager::getOpName() const { return impl->name; }
289 
290 /// Return the operation name that this pass manager operates on.
291 OperationName OpPassManager::getOpName(MLIRContext &context) const {
292   return impl->getOpName(context);
293 }
294 
295 /// Prints out the given passes as the textual representation of a pipeline.
296 static void printAsTextualPipeline(ArrayRef<std::unique_ptr<Pass>> passes,
297                                    raw_ostream &os) {
298   llvm::interleaveComma(passes, os, [&](const std::unique_ptr<Pass> &pass) {
299     pass->printAsTextualPipeline(os);
300   });
301 }
302 
303 /// Prints out the passes of the pass manager as the textual representation
304 /// of pipelines.
305 void OpPassManager::printAsTextualPipeline(raw_ostream &os) {
306   ::printAsTextualPipeline(impl->passes, os);
307 }
308 
309 void OpPassManager::dump() {
310   llvm::errs() << "Pass Manager with " << impl->passes.size() << " passes: ";
311   ::printAsTextualPipeline(impl->passes, llvm::errs());
312   llvm::errs() << "\n";
313 }
314 
315 static void registerDialectsForPipeline(const OpPassManager &pm,
316                                         DialectRegistry &dialects) {
317   for (const Pass &pass : pm.getPasses())
318     pass.getDependentDialects(dialects);
319 }
320 
321 void OpPassManager::getDependentDialects(DialectRegistry &dialects) const {
322   registerDialectsForPipeline(*this, dialects);
323 }
324 
325 void OpPassManager::setNesting(Nesting nesting) { impl->nesting = nesting; }
326 
327 OpPassManager::Nesting OpPassManager::getNesting() { return impl->nesting; }
328 
329 LogicalResult OpPassManager::initialize(MLIRContext *context,
330                                         unsigned newInitGeneration) {
331   if (impl->initializationGeneration == newInitGeneration)
332     return success();
333   impl->initializationGeneration = newInitGeneration;
334   for (Pass &pass : getPasses()) {
335     // If this pass isn't an adaptor, directly initialize it.
336     auto *adaptor = dyn_cast<OpToOpPassAdaptor>(&pass);
337     if (!adaptor) {
338       if (failed(pass.initialize(context)))
339         return failure();
340       continue;
341     }
342 
343     // Otherwise, initialize each of the adaptors pass managers.
344     for (OpPassManager &adaptorPM : adaptor->getPassManagers())
345       if (failed(adaptorPM.initialize(context, newInitGeneration)))
346         return failure();
347   }
348   return success();
349 }
350 
351 //===----------------------------------------------------------------------===//
352 // OpToOpPassAdaptor
353 //===----------------------------------------------------------------------===//
354 
355 LogicalResult OpToOpPassAdaptor::run(Pass *pass, Operation *op,
356                                      AnalysisManager am, bool verifyPasses,
357                                      unsigned parentInitGeneration) {
358   if (!op->isRegistered())
359     return op->emitOpError()
360            << "trying to schedule a pass on an unregistered operation";
361   if (!op->hasTrait<OpTrait::IsIsolatedFromAbove>())
362     return op->emitOpError() << "trying to schedule a pass on an operation not "
363                                 "marked as 'IsolatedFromAbove'";
364 
365   // Initialize the pass state with a callback for the pass to dynamically
366   // execute a pipeline on the currently visited operation.
367   PassInstrumentor *pi = am.getPassInstrumentor();
368   PassInstrumentation::PipelineParentInfo parentInfo = {llvm::get_threadid(),
369                                                         pass};
370   auto dynamicPipelineCallback = [&](OpPassManager &pipeline,
371                                      Operation *root) -> LogicalResult {
372     if (!op->isAncestor(root))
373       return root->emitOpError()
374              << "Trying to schedule a dynamic pipeline on an "
375                 "operation that isn't "
376                 "nested under the current operation the pass is processing";
377     assert(pipeline.getOpName() == root->getName().getStringRef());
378 
379     // Before running, finalize the passes held by the pipeline.
380     if (failed(pipeline.getImpl().finalizePassList(root->getContext())))
381       return failure();
382 
383     // Initialize the user provided pipeline and execute the pipeline.
384     if (failed(pipeline.initialize(root->getContext(), parentInitGeneration)))
385       return failure();
386     AnalysisManager nestedAm = root == op ? am : am.nest(root);
387     return OpToOpPassAdaptor::runPipeline(pipeline.getPasses(), root, nestedAm,
388                                           verifyPasses, parentInitGeneration,
389                                           pi, &parentInfo);
390   };
391   pass->passState.emplace(op, am, dynamicPipelineCallback);
392 
393   // Instrument before the pass has run.
394   if (pi)
395     pi->runBeforePass(pass, op);
396 
397   // Invoke the virtual runOnOperation method.
398   if (auto *adaptor = dyn_cast<OpToOpPassAdaptor>(pass))
399     adaptor->runOnOperation(verifyPasses);
400   else
401     pass->runOnOperation();
402   bool passFailed = pass->passState->irAndPassFailed.getInt();
403 
404   // Invalidate any non preserved analyses.
405   am.invalidate(pass->passState->preservedAnalyses);
406 
407   // When verifyPasses is specified, we run the verifier (unless the pass
408   // failed).
409   if (!passFailed && verifyPasses) {
410     bool runVerifierNow = true;
411 
412     // If the pass is an adaptor pass, we don't run the verifier recursively
413     // because the nested operations should have already been verified after
414     // nested passes had run.
415     bool runVerifierRecursively = !isa<OpToOpPassAdaptor>(pass);
416 
417     // Reduce compile time by avoiding running the verifier if the pass didn't
418     // change the IR since the last time the verifier was run:
419     //
420     //  1) If the pass said that it preserved all analyses then it can't have
421     //     permuted the IR.
422     //
423     // We run these checks in EXPENSIVE_CHECKS mode out of caution.
424 #ifndef EXPENSIVE_CHECKS
425     runVerifierNow = !pass->passState->preservedAnalyses.isAll();
426 #endif
427     if (runVerifierNow)
428       passFailed = failed(verify(op, runVerifierRecursively));
429   }
430 
431   // Instrument after the pass has run.
432   if (pi) {
433     if (passFailed)
434       pi->runAfterPassFailed(pass, op);
435     else
436       pi->runAfterPass(pass, op);
437   }
438 
439   // Return if the pass signaled a failure.
440   return failure(passFailed);
441 }
442 
443 /// Run the given operation and analysis manager on a provided op pass manager.
444 LogicalResult OpToOpPassAdaptor::runPipeline(
445     iterator_range<OpPassManager::pass_iterator> passes, Operation *op,
446     AnalysisManager am, bool verifyPasses, unsigned parentInitGeneration,
447     PassInstrumentor *instrumentor,
448     const PassInstrumentation::PipelineParentInfo *parentInfo) {
449   assert((!instrumentor || parentInfo) &&
450          "expected parent info if instrumentor is provided");
451   auto scopeExit = llvm::make_scope_exit([&] {
452     // Clear out any computed operation analyses. These analyses won't be used
453     // any more in this pipeline, and this helps reduce the current working set
454     // of memory. If preserving these analyses becomes important in the future
455     // we can re-evaluate this.
456     am.clear();
457   });
458 
459   // Run the pipeline over the provided operation.
460   if (instrumentor)
461     instrumentor->runBeforePipeline(op->getName().getIdentifier(), *parentInfo);
462   for (Pass &pass : passes)
463     if (failed(run(&pass, op, am, verifyPasses, parentInitGeneration)))
464       return failure();
465   if (instrumentor)
466     instrumentor->runAfterPipeline(op->getName().getIdentifier(), *parentInfo);
467   return success();
468 }
469 
470 /// Find an operation pass manager that can operate on an operation of the given
471 /// type, or nullptr if one does not exist.
472 static OpPassManager *findPassManagerFor(MutableArrayRef<OpPassManager> mgrs,
473                                          StringRef name) {
474   auto *it = llvm::find_if(
475       mgrs, [&](OpPassManager &mgr) { return mgr.getOpName() == name; });
476   return it == mgrs.end() ? nullptr : &*it;
477 }
478 
479 /// Find an operation pass manager that can operate on an operation of the given
480 /// type, or nullptr if one does not exist.
481 static OpPassManager *findPassManagerFor(MutableArrayRef<OpPassManager> mgrs,
482                                          OperationName name,
483                                          MLIRContext &context) {
484   auto *it = llvm::find_if(
485       mgrs, [&](OpPassManager &mgr) { return mgr.getOpName(context) == name; });
486   return it == mgrs.end() ? nullptr : &*it;
487 }
488 
489 OpToOpPassAdaptor::OpToOpPassAdaptor(OpPassManager &&mgr) {
490   mgrs.emplace_back(std::move(mgr));
491 }
492 
493 void OpToOpPassAdaptor::getDependentDialects(DialectRegistry &dialects) const {
494   for (auto &pm : mgrs)
495     pm.getDependentDialects(dialects);
496 }
497 
498 /// Merge the current pass adaptor into given 'rhs'.
499 void OpToOpPassAdaptor::mergeInto(OpToOpPassAdaptor &rhs) {
500   for (auto &pm : mgrs) {
501     // If an existing pass manager exists, then merge the given pass manager
502     // into it.
503     if (auto *existingPM = findPassManagerFor(rhs.mgrs, pm.getOpName())) {
504       pm.getImpl().mergeInto(existingPM->getImpl());
505     } else {
506       // Otherwise, add the given pass manager to the list.
507       rhs.mgrs.emplace_back(std::move(pm));
508     }
509   }
510   mgrs.clear();
511 
512   // After coalescing, sort the pass managers within rhs by name.
513   llvm::array_pod_sort(rhs.mgrs.begin(), rhs.mgrs.end(),
514                        [](const OpPassManager *lhs, const OpPassManager *rhs) {
515                          return lhs->getOpName().compare(rhs->getOpName());
516                        });
517 }
518 
519 /// Returns the adaptor pass name.
520 std::string OpToOpPassAdaptor::getAdaptorName() {
521   std::string name = "Pipeline Collection : [";
522   llvm::raw_string_ostream os(name);
523   llvm::interleaveComma(getPassManagers(), os, [&](OpPassManager &pm) {
524     os << '\'' << pm.getOpName() << '\'';
525   });
526   os << ']';
527   return os.str();
528 }
529 
530 void OpToOpPassAdaptor::runOnOperation() {
531   llvm_unreachable(
532       "Unexpected call to Pass::runOnOperation() on OpToOpPassAdaptor");
533 }
534 
535 /// Run the held pipeline over all nested operations.
536 void OpToOpPassAdaptor::runOnOperation(bool verifyPasses) {
537   if (getContext().isMultithreadingEnabled())
538     runOnOperationAsyncImpl(verifyPasses);
539   else
540     runOnOperationImpl(verifyPasses);
541 }
542 
543 /// Run this pass adaptor synchronously.
544 void OpToOpPassAdaptor::runOnOperationImpl(bool verifyPasses) {
545   auto am = getAnalysisManager();
546   PassInstrumentation::PipelineParentInfo parentInfo = {llvm::get_threadid(),
547                                                         this};
548   auto *instrumentor = am.getPassInstrumentor();
549   for (auto &region : getOperation()->getRegions()) {
550     for (auto &block : region) {
551       for (auto &op : block) {
552         auto *mgr = findPassManagerFor(mgrs, op.getName(), *op.getContext());
553         if (!mgr)
554           continue;
555 
556         // Run the held pipeline over the current operation.
557         unsigned initGeneration = mgr->impl->initializationGeneration;
558         if (failed(runPipeline(mgr->getPasses(), &op, am.nest(&op),
559                                verifyPasses, initGeneration, instrumentor,
560                                &parentInfo)))
561           return signalPassFailure();
562       }
563     }
564   }
565 }
566 
567 /// Utility functor that checks if the two ranges of pass managers have a size
568 /// mismatch.
569 static bool hasSizeMismatch(ArrayRef<OpPassManager> lhs,
570                             ArrayRef<OpPassManager> rhs) {
571   return lhs.size() != rhs.size() ||
572          llvm::any_of(llvm::seq<size_t>(0, lhs.size()),
573                       [&](size_t i) { return lhs[i].size() != rhs[i].size(); });
574 }
575 
576 /// Run this pass adaptor synchronously.
577 void OpToOpPassAdaptor::runOnOperationAsyncImpl(bool verifyPasses) {
578   AnalysisManager am = getAnalysisManager();
579   MLIRContext *context = &getContext();
580 
581   // Create the async executors if they haven't been created, or if the main
582   // pipeline has changed.
583   if (asyncExecutors.empty() || hasSizeMismatch(asyncExecutors.front(), mgrs))
584     asyncExecutors.assign(context->getThreadPool().getThreadCount(), mgrs);
585 
586   // Run a prepass over the operation to collect the nested operations to
587   // execute over. This ensures that an analysis manager exists for each
588   // operation, as well as providing a queue of operations to execute over.
589   std::vector<std::pair<Operation *, AnalysisManager>> opAMPairs;
590   for (auto &region : getOperation()->getRegions()) {
591     for (auto &block : region) {
592       for (auto &op : block) {
593         // Add this operation iff the name matches any of the pass managers.
594         if (findPassManagerFor(mgrs, op.getName(), *context))
595           opAMPairs.emplace_back(&op, am.nest(&op));
596       }
597     }
598   }
599 
600   // Get the current thread for this adaptor.
601   PassInstrumentation::PipelineParentInfo parentInfo = {llvm::get_threadid(),
602                                                         this};
603   auto *instrumentor = am.getPassInstrumentor();
604 
605   // An atomic failure variable for the async executors.
606   std::vector<std::atomic<bool>> activePMs(asyncExecutors.size());
607   std::fill(activePMs.begin(), activePMs.end(), false);
608   auto processFn = [&](auto &opPMPair) {
609     // Find a pass manager for this operation.
610     auto it = llvm::find_if(activePMs, [](std::atomic<bool> &isActive) {
611       bool expectedInactive = false;
612       return isActive.compare_exchange_strong(expectedInactive, true);
613     });
614     unsigned pmIndex = it - activePMs.begin();
615 
616     // Get the pass manager for this operation and execute it.
617     auto *pm = findPassManagerFor(asyncExecutors[pmIndex],
618                                   opPMPair.first->getName(), *context);
619     assert(pm && "expected valid pass manager for operation");
620 
621     unsigned initGeneration = pm->impl->initializationGeneration;
622     LogicalResult pipelineResult =
623         runPipeline(pm->getPasses(), opPMPair.first, opPMPair.second,
624                     verifyPasses, initGeneration, instrumentor, &parentInfo);
625 
626     // Reset the active bit for this pass manager.
627     activePMs[pmIndex].store(false);
628     return pipelineResult;
629   };
630 
631   // Signal a failure if any of the executors failed.
632   if (failed(failableParallelForEach(context, opAMPairs, processFn)))
633     signalPassFailure();
634 }
635 
636 //===----------------------------------------------------------------------===//
637 // PassManager
638 //===----------------------------------------------------------------------===//
639 
640 PassManager::PassManager(MLIRContext *ctx, Nesting nesting,
641                          StringRef operationName)
642     : OpPassManager(StringAttr::get(ctx, operationName), nesting), context(ctx),
643       initializationKey(DenseMapInfo<llvm::hash_code>::getTombstoneKey()),
644       passTiming(false), verifyPasses(true) {}
645 
646 PassManager::~PassManager() = default;
647 
648 void PassManager::enableVerifier(bool enabled) { verifyPasses = enabled; }
649 
650 /// Run the passes within this manager on the provided operation.
651 LogicalResult PassManager::run(Operation *op) {
652   MLIRContext *context = getContext();
653   assert(op->getName() == getOpName(*context) &&
654          "operation has a different name than the PassManager or is from a "
655          "different context");
656 
657   // Register all dialects for the current pipeline.
658   DialectRegistry dependentDialects;
659   getDependentDialects(dependentDialects);
660   context->appendDialectRegistry(dependentDialects);
661   for (StringRef name : dependentDialects.getDialectNames())
662     context->getOrLoadDialect(name);
663 
664   // Before running, make sure to finalize the pipeline pass list.
665   if (failed(getImpl().finalizePassList(context)))
666     return failure();
667 
668   // Initialize all of the passes within the pass manager with a new generation.
669   llvm::hash_code newInitKey = context->getRegistryHash();
670   if (newInitKey != initializationKey) {
671     if (failed(initialize(context, impl->initializationGeneration + 1)))
672       return failure();
673     initializationKey = newInitKey;
674   }
675 
676   // Construct a top level analysis manager for the pipeline.
677   ModuleAnalysisManager am(op, instrumentor.get());
678 
679   // Notify the context that we start running a pipeline for book keeping.
680   context->enterMultiThreadedExecution();
681 
682   // If reproducer generation is enabled, run the pass manager with crash
683   // handling enabled.
684   LogicalResult result =
685       crashReproGenerator ? runWithCrashRecovery(op, am) : runPasses(op, am);
686 
687   // Notify the context that the run is done.
688   context->exitMultiThreadedExecution();
689 
690   // Dump all of the pass statistics if necessary.
691   if (passStatisticsMode)
692     dumpStatistics();
693   return result;
694 }
695 
696 /// Add the provided instrumentation to the pass manager.
697 void PassManager::addInstrumentation(std::unique_ptr<PassInstrumentation> pi) {
698   if (!instrumentor)
699     instrumentor = std::make_unique<PassInstrumentor>();
700 
701   instrumentor->addInstrumentation(std::move(pi));
702 }
703 
704 LogicalResult PassManager::runPasses(Operation *op, AnalysisManager am) {
705   return OpToOpPassAdaptor::runPipeline(getPasses(), op, am, verifyPasses,
706                                         impl->initializationGeneration);
707 }
708 
709 //===----------------------------------------------------------------------===//
710 // AnalysisManager
711 //===----------------------------------------------------------------------===//
712 
713 /// Get an analysis manager for the given operation, which must be a proper
714 /// descendant of the current operation represented by this analysis manager.
715 AnalysisManager AnalysisManager::nest(Operation *op) {
716   Operation *currentOp = impl->getOperation();
717   assert(currentOp->isProperAncestor(op) &&
718          "expected valid descendant operation");
719 
720   // Check for the base case where the provided operation is immediately nested.
721   if (currentOp == op->getParentOp())
722     return nestImmediate(op);
723 
724   // Otherwise, we need to collect all ancestors up to the current operation.
725   SmallVector<Operation *, 4> opAncestors;
726   do {
727     opAncestors.push_back(op);
728     op = op->getParentOp();
729   } while (op != currentOp);
730 
731   AnalysisManager result = *this;
732   for (Operation *op : llvm::reverse(opAncestors))
733     result = result.nestImmediate(op);
734   return result;
735 }
736 
737 /// Get an analysis manager for the given immediately nested child operation.
738 AnalysisManager AnalysisManager::nestImmediate(Operation *op) {
739   assert(impl->getOperation() == op->getParentOp() &&
740          "expected immediate child operation");
741 
742   auto it = impl->childAnalyses.find(op);
743   if (it == impl->childAnalyses.end())
744     it = impl->childAnalyses
745              .try_emplace(op, std::make_unique<NestedAnalysisMap>(op, impl))
746              .first;
747   return {it->second.get()};
748 }
749 
750 /// Invalidate any non preserved analyses.
751 void detail::NestedAnalysisMap::invalidate(
752     const detail::PreservedAnalyses &pa) {
753   // If all analyses were preserved, then there is nothing to do here.
754   if (pa.isAll())
755     return;
756 
757   // Invalidate the analyses for the current operation directly.
758   analyses.invalidate(pa);
759 
760   // If no analyses were preserved, then just simply clear out the child
761   // analysis results.
762   if (pa.isNone()) {
763     childAnalyses.clear();
764     return;
765   }
766 
767   // Otherwise, invalidate each child analysis map.
768   SmallVector<NestedAnalysisMap *, 8> mapsToInvalidate(1, this);
769   while (!mapsToInvalidate.empty()) {
770     auto *map = mapsToInvalidate.pop_back_val();
771     for (auto &analysisPair : map->childAnalyses) {
772       analysisPair.second->invalidate(pa);
773       if (!analysisPair.second->childAnalyses.empty())
774         mapsToInvalidate.push_back(analysisPair.second.get());
775     }
776   }
777 }
778 
779 //===----------------------------------------------------------------------===//
780 // PassInstrumentation
781 //===----------------------------------------------------------------------===//
782 
783 PassInstrumentation::~PassInstrumentation() = default;
784 
785 //===----------------------------------------------------------------------===//
786 // PassInstrumentor
787 //===----------------------------------------------------------------------===//
788 
789 namespace mlir {
790 namespace detail {
791 struct PassInstrumentorImpl {
792   /// Mutex to keep instrumentation access thread-safe.
793   llvm::sys::SmartMutex<true> mutex;
794 
795   /// Set of registered instrumentations.
796   std::vector<std::unique_ptr<PassInstrumentation>> instrumentations;
797 };
798 } // namespace detail
799 } // namespace mlir
800 
801 PassInstrumentor::PassInstrumentor() : impl(new PassInstrumentorImpl()) {}
802 PassInstrumentor::~PassInstrumentor() = default;
803 
804 /// See PassInstrumentation::runBeforePipeline for details.
805 void PassInstrumentor::runBeforePipeline(
806     StringAttr name,
807     const PassInstrumentation::PipelineParentInfo &parentInfo) {
808   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
809   for (auto &instr : impl->instrumentations)
810     instr->runBeforePipeline(name, parentInfo);
811 }
812 
813 /// See PassInstrumentation::runAfterPipeline for details.
814 void PassInstrumentor::runAfterPipeline(
815     StringAttr name,
816     const PassInstrumentation::PipelineParentInfo &parentInfo) {
817   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
818   for (auto &instr : llvm::reverse(impl->instrumentations))
819     instr->runAfterPipeline(name, parentInfo);
820 }
821 
822 /// See PassInstrumentation::runBeforePass for details.
823 void PassInstrumentor::runBeforePass(Pass *pass, Operation *op) {
824   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
825   for (auto &instr : impl->instrumentations)
826     instr->runBeforePass(pass, op);
827 }
828 
829 /// See PassInstrumentation::runAfterPass for details.
830 void PassInstrumentor::runAfterPass(Pass *pass, Operation *op) {
831   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
832   for (auto &instr : llvm::reverse(impl->instrumentations))
833     instr->runAfterPass(pass, op);
834 }
835 
836 /// See PassInstrumentation::runAfterPassFailed for details.
837 void PassInstrumentor::runAfterPassFailed(Pass *pass, Operation *op) {
838   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
839   for (auto &instr : llvm::reverse(impl->instrumentations))
840     instr->runAfterPassFailed(pass, op);
841 }
842 
843 /// See PassInstrumentation::runBeforeAnalysis for details.
844 void PassInstrumentor::runBeforeAnalysis(StringRef name, TypeID id,
845                                          Operation *op) {
846   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
847   for (auto &instr : impl->instrumentations)
848     instr->runBeforeAnalysis(name, id, op);
849 }
850 
851 /// See PassInstrumentation::runAfterAnalysis for details.
852 void PassInstrumentor::runAfterAnalysis(StringRef name, TypeID id,
853                                         Operation *op) {
854   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
855   for (auto &instr : llvm::reverse(impl->instrumentations))
856     instr->runAfterAnalysis(name, id, op);
857 }
858 
859 /// Add the given instrumentation to the collection.
860 void PassInstrumentor::addInstrumentation(
861     std::unique_ptr<PassInstrumentation> pi) {
862   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
863   impl->instrumentations.emplace_back(std::move(pi));
864 }
865