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