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