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