xref: /llvm-project-15.0.7/mlir/lib/Pass/Pass.cpp (revision cfa1376a)
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/Module.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, bool filterVerifier) {
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, filterVerifier);
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 // Verifier Passes
79 //===----------------------------------------------------------------------===//
80 
81 void VerifierPass::runOnOperation() {
82   if (failed(verify(getOperation())))
83     signalPassFailure();
84   markAllAnalysesPreserved();
85 }
86 
87 //===----------------------------------------------------------------------===//
88 // OpPassManagerImpl
89 //===----------------------------------------------------------------------===//
90 
91 namespace mlir {
92 namespace detail {
93 struct OpPassManagerImpl {
94   OpPassManagerImpl(Identifier identifier, bool verifyPasses)
95       : name(identifier), identifier(identifier), verifyPasses(verifyPasses) {}
96   OpPassManagerImpl(StringRef name, bool verifyPasses)
97       : name(name), verifyPasses(verifyPasses) {}
98 
99   /// Merge the passes of this pass manager into the one provided.
100   void mergeInto(OpPassManagerImpl &rhs);
101 
102   /// Nest a new operation pass manager for the given operation kind under this
103   /// pass manager.
104   OpPassManager &nest(Identifier nestedName);
105   OpPassManager &nest(StringRef nestedName);
106 
107   /// Add the given pass to this pass manager. If this pass has a concrete
108   /// operation type, it must be the same type as this pass manager.
109   void addPass(std::unique_ptr<Pass> pass);
110 
111   /// Coalesce adjacent AdaptorPasses into one large adaptor. This runs
112   /// recursively through the pipeline graph.
113   void coalesceAdjacentAdaptorPasses();
114 
115   /// Split all of AdaptorPasses such that each adaptor only contains one leaf
116   /// pass.
117   void splitAdaptorPasses();
118 
119   Identifier getOpName(MLIRContext &context) {
120     if (!identifier)
121       identifier = Identifier::get(name, &context);
122     return *identifier;
123   }
124 
125   /// The name of the operation that passes of this pass manager operate on.
126   StringRef name;
127 
128   /// The cached identifier (internalized in the context) for the name of the
129   /// operation that passes of this pass manager operate on.
130   Optional<Identifier> identifier;
131 
132   /// Flag that specifies if the IR should be verified after each pass has run.
133   bool verifyPasses : 1;
134 
135   /// The set of passes to run as part of this pass manager.
136   std::vector<std::unique_ptr<Pass>> passes;
137 };
138 } // end namespace detail
139 } // end 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(Identifier nestedName) {
149   OpPassManager nested(nestedName, verifyPasses);
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, verifyPasses);
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.
165   auto passOpName = pass->getOpName();
166   if (passOpName && passOpName != name)
167     return nest(*passOpName).addPass(std::move(pass));
168 
169   passes.emplace_back(std::move(pass));
170   if (verifyPasses)
171     passes.emplace_back(std::make_unique<VerifierPass>());
172 }
173 
174 void OpPassManagerImpl::coalesceAdjacentAdaptorPasses() {
175   // Bail out early if there are no adaptor passes.
176   if (llvm::none_of(passes, [](std::unique_ptr<Pass> &pass) {
177         return isa<OpToOpPassAdaptor>(pass.get());
178       }))
179     return;
180 
181   // Walk the pass list and merge adjacent adaptors.
182   OpToOpPassAdaptor *lastAdaptor = nullptr;
183   for (auto it = passes.begin(), e = passes.end(); it != e; ++it) {
184     // Check to see if this pass is an adaptor.
185     if (auto *currentAdaptor = dyn_cast<OpToOpPassAdaptor>(it->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       it->reset();
196 
197       // If the verifier is enabled, then next pass is a verifier run so
198       // drop it. Verifier passes are inserted after every pass, so this one
199       // would be a duplicate.
200       if (verifyPasses) {
201         assert(std::next(it) != e && isa<VerifierPass>(*std::next(it)));
202         (++it)->reset();
203       }
204     } else if (lastAdaptor && !isa<VerifierPass>(*it)) {
205       // If this pass is not an adaptor and not a verifier pass, then coalesce
206       // and forget any existing adaptor.
207       for (auto &pm : lastAdaptor->getPassManagers())
208         pm.getImpl().coalesceAdjacentAdaptorPasses();
209       lastAdaptor = nullptr;
210     }
211   }
212 
213   // If there was an adaptor at the end of the manager, coalesce it as well.
214   if (lastAdaptor) {
215     for (auto &pm : lastAdaptor->getPassManagers())
216       pm.getImpl().coalesceAdjacentAdaptorPasses();
217   }
218 
219   // Now that the adaptors have been merged, erase the empty slot corresponding
220   // to the merged adaptors that were nulled-out in the loop above.
221   llvm::erase_if(passes, std::logical_not<std::unique_ptr<Pass>>());
222 }
223 
224 void OpPassManagerImpl::splitAdaptorPasses() {
225   std::vector<std::unique_ptr<Pass>> oldPasses;
226   std::swap(passes, oldPasses);
227 
228   for (std::unique_ptr<Pass> &pass : oldPasses) {
229     // Ignore verifier passes, they are added back in the "addPass()" calls.
230     if (isa<VerifierPass>(pass.get()))
231       continue;
232 
233     // If this pass isn't an adaptor, move it directly to the new pass list.
234     auto *currentAdaptor = dyn_cast<OpToOpPassAdaptor>(pass.get());
235     if (!currentAdaptor) {
236       addPass(std::move(pass));
237       continue;
238     }
239 
240     // Otherwise, split the adaptors of each manager within the adaptor.
241     for (OpPassManager &adaptorPM : currentAdaptor->getPassManagers()) {
242       adaptorPM.getImpl().splitAdaptorPasses();
243 
244       // Add all non-verifier passes to this pass manager.
245       for (std::unique_ptr<Pass> &nestedPass : adaptorPM.getImpl().passes) {
246         if (!isa<VerifierPass>(nestedPass.get()))
247           nest(adaptorPM.getOpName()).addPass(std::move(nestedPass));
248       }
249     }
250   }
251 }
252 
253 //===----------------------------------------------------------------------===//
254 // OpPassManager
255 //===----------------------------------------------------------------------===//
256 
257 OpPassManager::OpPassManager(Identifier name, bool verifyPasses)
258     : impl(new OpPassManagerImpl(name, verifyPasses)) {}
259 OpPassManager::OpPassManager(StringRef name, bool verifyPasses)
260     : impl(new OpPassManagerImpl(name, verifyPasses)) {}
261 OpPassManager::OpPassManager(OpPassManager &&rhs) : impl(std::move(rhs.impl)) {}
262 OpPassManager::OpPassManager(const OpPassManager &rhs) { *this = rhs; }
263 OpPassManager &OpPassManager::operator=(const OpPassManager &rhs) {
264   impl.reset(new OpPassManagerImpl(rhs.impl->name, rhs.impl->verifyPasses));
265   for (auto &pass : rhs.impl->passes)
266     impl->passes.emplace_back(pass->clone());
267   return *this;
268 }
269 
270 OpPassManager::~OpPassManager() {}
271 
272 OpPassManager::pass_iterator OpPassManager::begin() {
273   return MutableArrayRef<std::unique_ptr<Pass>>{impl->passes}.begin();
274 }
275 OpPassManager::pass_iterator OpPassManager::end() {
276   return MutableArrayRef<std::unique_ptr<Pass>>{impl->passes}.end();
277 }
278 
279 OpPassManager::const_pass_iterator OpPassManager::begin() const {
280   return ArrayRef<std::unique_ptr<Pass>>{impl->passes}.begin();
281 }
282 OpPassManager::const_pass_iterator OpPassManager::end() const {
283   return ArrayRef<std::unique_ptr<Pass>>{impl->passes}.end();
284 }
285 
286 /// Nest a new operation pass manager for the given operation kind under this
287 /// pass manager.
288 OpPassManager &OpPassManager::nest(Identifier nestedName) {
289   return impl->nest(nestedName);
290 }
291 OpPassManager &OpPassManager::nest(StringRef nestedName) {
292   return impl->nest(nestedName);
293 }
294 
295 /// Add the given pass to this pass manager. If this pass has a concrete
296 /// operation type, it must be the same type as this pass manager.
297 void OpPassManager::addPass(std::unique_ptr<Pass> pass) {
298   impl->addPass(std::move(pass));
299 }
300 
301 /// Returns the number of passes held by this manager.
302 size_t OpPassManager::size() const { return impl->passes.size(); }
303 
304 /// Returns the internal implementation instance.
305 OpPassManagerImpl &OpPassManager::getImpl() { return *impl; }
306 
307 /// Return the operation name that this pass manager operates on.
308 StringRef OpPassManager::getOpName() const { return impl->name; }
309 
310 /// Return the operation name that this pass manager operates on.
311 Identifier OpPassManager::getOpName(MLIRContext &context) const {
312   return impl->getOpName(context);
313 }
314 
315 /// Prints out the given passes as the textual representation of a pipeline.
316 static void printAsTextualPipeline(ArrayRef<std::unique_ptr<Pass>> passes,
317                                    raw_ostream &os,
318                                    bool filterVerifier = true) {
319   // Filter out passes that are not part of the public pipeline.
320   auto filteredPasses =
321       llvm::make_filter_range(passes, [&](const std::unique_ptr<Pass> &pass) {
322         return !filterVerifier || !isa<VerifierPass>(pass);
323       });
324   llvm::interleaveComma(filteredPasses, os,
325                         [&](const std::unique_ptr<Pass> &pass) {
326                           pass->printAsTextualPipeline(os, filterVerifier);
327                         });
328 }
329 
330 /// Prints out the passes of the pass manager as the textual representation
331 /// of pipelines.
332 void OpPassManager::printAsTextualPipeline(raw_ostream &os,
333                                            bool filterVerifier) {
334   ::printAsTextualPipeline(impl->passes, os, filterVerifier);
335 }
336 
337 void OpPassManager::dump() {
338   llvm::errs() << "Pass Manager with " << impl->passes.size() << " passes: ";
339   ::printAsTextualPipeline(impl->passes, llvm::errs(),
340                            /*filterVerifier=*/false);
341   llvm::errs() << "\n";
342 }
343 
344 static void registerDialectsForPipeline(const OpPassManager &pm,
345                                         DialectRegistry &dialects) {
346   for (const Pass &pass : pm.getPasses())
347     pass.getDependentDialects(dialects);
348 }
349 
350 void OpPassManager::getDependentDialects(DialectRegistry &dialects) const {
351   registerDialectsForPipeline(*this, dialects);
352 }
353 
354 //===----------------------------------------------------------------------===//
355 // OpToOpPassAdaptor
356 //===----------------------------------------------------------------------===//
357 
358 LogicalResult OpToOpPassAdaptor::run(Pass *pass, Operation *op,
359                                      AnalysisManager am) {
360   if (!op->getName().getAbstractOperation())
361     return op->emitOpError()
362            << "trying to schedule a pass on an unregistered operation";
363   if (!op->getName().getAbstractOperation()->hasProperty(
364           OperationProperty::IsolatedFromAbove))
365     return op->emitOpError() << "trying to schedule a pass on an operation not "
366                                 "marked as 'IsolatedFromAbove'";
367 
368   // Initialize the pass state with a callback for the pass to dynamically
369   // execute a pipeline on the currently visited operation.
370   auto dynamic_pipeline_callback =
371       [op, &am](OpPassManager &pipeline, Operation *root) {
372         if (!op->isAncestor(root)) {
373           root->emitOpError()
374               << "Trying to schedule a dynamic pipeline on an "
375                  "operation that isn't "
376                  "nested under the current operation the pass is processing";
377           return failure();
378         }
379         AnalysisManager nestedAm = am.nest(root);
380         return OpToOpPassAdaptor::runPipeline(pipeline.getPasses(), root,
381                                               nestedAm);
382       };
383   pass->passState.emplace(op, am, dynamic_pipeline_callback);
384   // Instrument before the pass has run.
385   PassInstrumentor *pi = am.getPassInstrumentor();
386   if (pi)
387     pi->runBeforePass(pass, op);
388 
389   // Invoke the virtual runOnOperation method.
390   pass->runOnOperation();
391 
392   // Invalidate any non preserved analyses.
393   am.invalidate(pass->passState->preservedAnalyses);
394 
395   // Instrument after the pass has run.
396   bool passFailed = pass->passState->irAndPassFailed.getInt();
397   if (pi) {
398     if (passFailed)
399       pi->runAfterPassFailed(pass, op);
400     else
401       pi->runAfterPass(pass, op);
402   }
403 
404   // Return if the pass signaled a failure.
405   return failure(passFailed);
406 }
407 
408 /// Run the given operation and analysis manager on a provided op pass manager.
409 LogicalResult OpToOpPassAdaptor::runPipeline(
410     iterator_range<OpPassManager::pass_iterator> passes, Operation *op,
411     AnalysisManager am) {
412   auto scope_exit = llvm::make_scope_exit([&] {
413     // Clear out any computed operation analyses. These analyses won't be used
414     // any more in this pipeline, and this helps reduce the current working set
415     // of memory. If preserving these analyses becomes important in the future
416     // we can re-evaluate this.
417     am.clear();
418   });
419 
420   // Run the pipeline over the provided operation.
421   for (Pass &pass : passes)
422     if (failed(run(&pass, op, am)))
423       return failure();
424 
425   return success();
426 }
427 
428 /// Find an operation pass manager that can operate on an operation of the given
429 /// type, or nullptr if one does not exist.
430 static OpPassManager *findPassManagerFor(MutableArrayRef<OpPassManager> mgrs,
431                                          StringRef name) {
432   auto it = llvm::find_if(
433       mgrs, [&](OpPassManager &mgr) { return mgr.getOpName() == name; });
434   return it == mgrs.end() ? nullptr : &*it;
435 }
436 
437 /// Find an operation pass manager that can operate on an operation of the given
438 /// type, or nullptr if one does not exist.
439 static OpPassManager *findPassManagerFor(MutableArrayRef<OpPassManager> mgrs,
440                                          Identifier name,
441                                          MLIRContext &context) {
442   auto it = llvm::find_if(
443       mgrs, [&](OpPassManager &mgr) { return mgr.getOpName(context) == name; });
444   return it == mgrs.end() ? nullptr : &*it;
445 }
446 
447 OpToOpPassAdaptor::OpToOpPassAdaptor(OpPassManager &&mgr) {
448   mgrs.emplace_back(std::move(mgr));
449 }
450 
451 void OpToOpPassAdaptor::getDependentDialects(DialectRegistry &dialects) const {
452   for (auto &pm : mgrs)
453     pm.getDependentDialects(dialects);
454 }
455 
456 /// Merge the current pass adaptor into given 'rhs'.
457 void OpToOpPassAdaptor::mergeInto(OpToOpPassAdaptor &rhs) {
458   for (auto &pm : mgrs) {
459     // If an existing pass manager exists, then merge the given pass manager
460     // into it.
461     if (auto *existingPM = findPassManagerFor(rhs.mgrs, pm.getOpName())) {
462       pm.getImpl().mergeInto(existingPM->getImpl());
463     } else {
464       // Otherwise, add the given pass manager to the list.
465       rhs.mgrs.emplace_back(std::move(pm));
466     }
467   }
468   mgrs.clear();
469 
470   // After coalescing, sort the pass managers within rhs by name.
471   llvm::array_pod_sort(rhs.mgrs.begin(), rhs.mgrs.end(),
472                        [](const OpPassManager *lhs, const OpPassManager *rhs) {
473                          return lhs->getOpName().compare(rhs->getOpName());
474                        });
475 }
476 
477 /// Returns the adaptor pass name.
478 std::string OpToOpPassAdaptor::getAdaptorName() {
479   std::string name = "Pipeline Collection : [";
480   llvm::raw_string_ostream os(name);
481   llvm::interleaveComma(getPassManagers(), os, [&](OpPassManager &pm) {
482     os << '\'' << pm.getOpName() << '\'';
483   });
484   os << ']';
485   return os.str();
486 }
487 
488 /// Run the held pipeline over all nested operations.
489 void OpToOpPassAdaptor::runOnOperation() {
490   if (getContext().isMultithreadingEnabled())
491     runOnOperationAsyncImpl();
492   else
493     runOnOperationImpl();
494 }
495 
496 /// Run this pass adaptor synchronously.
497 void OpToOpPassAdaptor::runOnOperationImpl() {
498   auto am = getAnalysisManager();
499   PassInstrumentation::PipelineParentInfo parentInfo = {llvm::get_threadid(),
500                                                         this};
501   auto *instrumentor = am.getPassInstrumentor();
502   for (auto &region : getOperation()->getRegions()) {
503     for (auto &block : region) {
504       for (auto &op : block) {
505         auto *mgr = findPassManagerFor(mgrs, op.getName().getIdentifier(),
506                                        *op.getContext());
507         if (!mgr)
508           continue;
509         Identifier opName = mgr->getOpName(*getOperation()->getContext());
510 
511         // Run the held pipeline over the current operation.
512         if (instrumentor)
513           instrumentor->runBeforePipeline(opName, parentInfo);
514         auto result = runPipeline(mgr->getPasses(), &op, am.nest(&op));
515         if (instrumentor)
516           instrumentor->runAfterPipeline(opName, parentInfo);
517 
518         if (failed(result))
519           return signalPassFailure();
520       }
521     }
522   }
523 }
524 
525 /// Utility functor that checks if the two ranges of pass managers have a size
526 /// mismatch.
527 static bool hasSizeMismatch(ArrayRef<OpPassManager> lhs,
528                             ArrayRef<OpPassManager> rhs) {
529   return lhs.size() != rhs.size() ||
530          llvm::any_of(llvm::seq<size_t>(0, lhs.size()),
531                       [&](size_t i) { return lhs[i].size() != rhs[i].size(); });
532 }
533 
534 /// Run this pass adaptor synchronously.
535 void OpToOpPassAdaptor::runOnOperationAsyncImpl() {
536   AnalysisManager am = getAnalysisManager();
537 
538   // Create the async executors if they haven't been created, or if the main
539   // pipeline has changed.
540   if (asyncExecutors.empty() || hasSizeMismatch(asyncExecutors.front(), mgrs))
541     asyncExecutors.assign(llvm::hardware_concurrency().compute_thread_count(),
542                           mgrs);
543 
544   // Run a prepass over the module to collect the operations to execute over.
545   // This ensures that an analysis manager exists for each operation, as well as
546   // providing a queue of operations to execute over.
547   std::vector<std::pair<Operation *, AnalysisManager>> opAMPairs;
548   for (auto &region : getOperation()->getRegions()) {
549     for (auto &block : region) {
550       for (auto &op : block) {
551         // Add this operation iff the name matches any of the pass managers.
552         if (findPassManagerFor(mgrs, op.getName().getIdentifier(),
553                                getContext()))
554           opAMPairs.emplace_back(&op, am.nest(&op));
555       }
556     }
557   }
558 
559   // A parallel diagnostic handler that provides deterministic diagnostic
560   // ordering.
561   ParallelDiagnosticHandler diagHandler(&getContext());
562 
563   // An index for the current operation/analysis manager pair.
564   std::atomic<unsigned> opIt(0);
565 
566   // Get the current thread for this adaptor.
567   PassInstrumentation::PipelineParentInfo parentInfo = {llvm::get_threadid(),
568                                                         this};
569   auto *instrumentor = am.getPassInstrumentor();
570 
571   // An atomic failure variable for the async executors.
572   std::atomic<bool> passFailed(false);
573   llvm::parallelForEach(
574       asyncExecutors.begin(),
575       std::next(asyncExecutors.begin(),
576                 std::min(asyncExecutors.size(), opAMPairs.size())),
577       [&](MutableArrayRef<OpPassManager> pms) {
578         for (auto e = opAMPairs.size(); !passFailed && opIt < e;) {
579           // Get the next available operation index.
580           unsigned nextID = opIt++;
581           if (nextID >= e)
582             break;
583 
584           // Set the order id for this thread in the diagnostic handler.
585           diagHandler.setOrderIDForThread(nextID);
586 
587           // Get the pass manager for this operation and execute it.
588           auto &it = opAMPairs[nextID];
589           auto *pm = findPassManagerFor(
590               pms, it.first->getName().getIdentifier(), getContext());
591           assert(pm && "expected valid pass manager for operation");
592 
593           Identifier opName = pm->getOpName(*getOperation()->getContext());
594           if (instrumentor)
595             instrumentor->runBeforePipeline(opName, parentInfo);
596           auto pipelineResult =
597               runPipeline(pm->getPasses(), it.first, it.second);
598           if (instrumentor)
599             instrumentor->runAfterPipeline(opName, parentInfo);
600 
601           // Drop this thread from being tracked by the diagnostic handler.
602           // After this task has finished, the thread may be used outside of
603           // this pass manager context meaning that we don't want to track
604           // diagnostics from it anymore.
605           diagHandler.eraseOrderIDForThread();
606 
607           // Handle a failed pipeline result.
608           if (failed(pipelineResult)) {
609             passFailed = true;
610             break;
611           }
612         }
613       });
614 
615   // Signal a failure if any of the executors failed.
616   if (passFailed)
617     signalPassFailure();
618 }
619 
620 //===----------------------------------------------------------------------===//
621 // PassCrashReproducer
622 //===----------------------------------------------------------------------===//
623 
624 namespace {
625 /// This class contains all of the context for generating a recovery reproducer.
626 /// Each recovery context is registered globally to allow for generating
627 /// reproducers when a signal is raised, such as a segfault.
628 struct RecoveryReproducerContext {
629   RecoveryReproducerContext(MutableArrayRef<std::unique_ptr<Pass>> passes,
630                             ModuleOp module, StringRef filename,
631                             bool disableThreads, bool verifyPasses);
632   ~RecoveryReproducerContext();
633 
634   /// Generate a reproducer with the current context.
635   LogicalResult generate(std::string &error);
636 
637 private:
638   /// This function is invoked in the event of a crash.
639   static void crashHandler(void *);
640 
641   /// Register a signal handler to run in the event of a crash.
642   static void registerSignalHandler();
643 
644   /// The textual description of the currently executing pipeline.
645   std::string pipeline;
646 
647   /// The MLIR module representing the IR before the crash.
648   OwningModuleRef module;
649 
650   /// The filename to use when generating the reproducer.
651   StringRef filename;
652 
653   /// Various pass manager and context flags.
654   bool disableThreads;
655   bool verifyPasses;
656 
657   /// The current set of active reproducer contexts. This is used in the event
658   /// of a crash. This is not thread_local as the pass manager may produce any
659   /// number of child threads. This uses a set to allow for multiple MLIR pass
660   /// managers to be running at the same time.
661   static llvm::ManagedStatic<llvm::sys::SmartMutex<true>> reproducerMutex;
662   static llvm::ManagedStatic<
663       llvm::SmallSetVector<RecoveryReproducerContext *, 1>>
664       reproducerSet;
665 };
666 } // end anonymous namespace
667 
668 llvm::ManagedStatic<llvm::sys::SmartMutex<true>>
669     RecoveryReproducerContext::reproducerMutex;
670 llvm::ManagedStatic<llvm::SmallSetVector<RecoveryReproducerContext *, 1>>
671     RecoveryReproducerContext::reproducerSet;
672 
673 RecoveryReproducerContext::RecoveryReproducerContext(
674     MutableArrayRef<std::unique_ptr<Pass>> passes, ModuleOp module,
675     StringRef filename, bool disableThreads, bool verifyPasses)
676     : module(module.clone()), filename(filename),
677       disableThreads(disableThreads), verifyPasses(verifyPasses) {
678   // Grab the textual pipeline being executed..
679   {
680     llvm::raw_string_ostream pipelineOS(pipeline);
681     ::printAsTextualPipeline(passes, pipelineOS);
682   }
683 
684   // Make sure that the handler is registered, and update the current context.
685   llvm::sys::SmartScopedLock<true> producerLock(*reproducerMutex);
686   registerSignalHandler();
687   reproducerSet->insert(this);
688 }
689 
690 RecoveryReproducerContext::~RecoveryReproducerContext() {
691   llvm::sys::SmartScopedLock<true> producerLock(*reproducerMutex);
692   reproducerSet->remove(this);
693 }
694 
695 LogicalResult RecoveryReproducerContext::generate(std::string &error) {
696   std::unique_ptr<llvm::ToolOutputFile> outputFile =
697       mlir::openOutputFile(filename, &error);
698   if (!outputFile)
699     return failure();
700   auto &outputOS = outputFile->os();
701 
702   // Output the current pass manager configuration.
703   outputOS << "// configuration: -pass-pipeline='" << pipeline << "'";
704   if (disableThreads)
705     outputOS << " -mlir-disable-threading";
706 
707   // TODO: Should this also be configured with a pass manager flag?
708   outputOS << "\n// note: verifyPasses=" << (verifyPasses ? "true" : "false")
709            << "\n";
710 
711   // Output the .mlir module.
712   module->print(outputOS);
713   outputFile->keep();
714   return success();
715 }
716 
717 void RecoveryReproducerContext::crashHandler(void *) {
718   // Walk the current stack of contexts and generate a reproducer for each one.
719   // We can't know for certain which one was the cause, so we need to generate
720   // a reproducer for all of them.
721   std::string ignored;
722   for (RecoveryReproducerContext *context : *reproducerSet)
723     context->generate(ignored);
724 }
725 
726 void RecoveryReproducerContext::registerSignalHandler() {
727   // Ensure that the handler is only registered once.
728   static bool registered =
729       (llvm::sys::AddSignalHandler(crashHandler, nullptr), false);
730   (void)registered;
731 }
732 
733 /// Run the pass manager with crash recover enabled.
734 LogicalResult PassManager::runWithCrashRecovery(ModuleOp module,
735                                                 AnalysisManager am) {
736   // If this isn't a local producer, run all of the passes in recovery mode.
737   if (!localReproducer)
738     return runWithCrashRecovery(impl->passes, module, am);
739 
740   // Split the passes within adaptors to ensure that each pass can be run in
741   // isolation.
742   impl->splitAdaptorPasses();
743 
744   // If this is a local producer, run each of the passes individually. If the
745   // verifier is enabled, each pass will have a verifier after. This is included
746   // in the recovery run.
747   unsigned stride = impl->verifyPasses ? 2 : 1;
748   MutableArrayRef<std::unique_ptr<Pass>> passes = impl->passes;
749   for (unsigned i = 0, e = passes.size(); i != e; i += stride) {
750     if (failed(runWithCrashRecovery(passes.slice(i, stride), module, am)))
751       return failure();
752   }
753   return success();
754 }
755 
756 /// Run the given passes with crash recover enabled.
757 LogicalResult
758 PassManager::runWithCrashRecovery(MutableArrayRef<std::unique_ptr<Pass>> passes,
759                                   ModuleOp module, AnalysisManager am) {
760   RecoveryReproducerContext context(passes, module, *crashReproducerFileName,
761                                     !getContext()->isMultithreadingEnabled(),
762                                     impl->verifyPasses);
763 
764   // Safely invoke the passes within a recovery context.
765   llvm::CrashRecoveryContext::Enable();
766   LogicalResult passManagerResult = failure();
767   llvm::CrashRecoveryContext recoveryContext;
768   recoveryContext.RunSafelyOnThread([&] {
769     for (std::unique_ptr<Pass> &pass : passes)
770       if (failed(OpToOpPassAdaptor::run(pass.get(), module, am)))
771         return;
772     passManagerResult = success();
773   });
774   llvm::CrashRecoveryContext::Disable();
775   if (succeeded(passManagerResult))
776     return success();
777 
778   std::string error;
779   if (failed(context.generate(error)))
780     return module.emitError("<MLIR-PassManager-Crash-Reproducer>: ") << error;
781   return module.emitError()
782          << "A failure has been detected while processing the MLIR module, a "
783             "reproducer has been generated in '"
784          << *crashReproducerFileName << "'";
785 }
786 
787 //===----------------------------------------------------------------------===//
788 // PassManager
789 //===----------------------------------------------------------------------===//
790 
791 PassManager::PassManager(MLIRContext *ctx, bool verifyPasses)
792     : OpPassManager(Identifier::get(ModuleOp::getOperationName(), ctx),
793                     verifyPasses),
794       context(ctx), passTiming(false), localReproducer(false) {}
795 
796 PassManager::~PassManager() {}
797 
798 /// Run the passes within this manager on the provided module.
799 LogicalResult PassManager::run(ModuleOp module) {
800   // Before running, make sure to coalesce any adjacent pass adaptors in the
801   // pipeline.
802   getImpl().coalesceAdjacentAdaptorPasses();
803 
804   // Register all dialects for the current pipeline.
805   DialectRegistry dependentDialects;
806   getDependentDialects(dependentDialects);
807   dependentDialects.loadAll(module.getContext());
808 
809   // Construct an analysis manager for the pipeline.
810   ModuleAnalysisManager am(module, instrumentor.get());
811 
812   // Notify the context that we start running a pipeline for book keeping.
813   module.getContext()->enterMultiThreadedExecution();
814 
815   // If reproducer generation is enabled, run the pass manager with crash
816   // handling enabled.
817   LogicalResult result =
818       crashReproducerFileName
819           ? runWithCrashRecovery(module, am)
820           : OpToOpPassAdaptor::runPipeline(getPasses(), module, am);
821 
822   // Notify the context that the run is done.
823   module.getContext()->exitMultiThreadedExecution();
824 
825   // Dump all of the pass statistics if necessary.
826   if (passStatisticsMode)
827     dumpStatistics();
828   return result;
829 }
830 
831 /// Enable support for the pass manager to generate a reproducer on the event
832 /// of a crash or a pass failure. `outputFile` is a .mlir filename used to write
833 /// the generated reproducer. If `genLocalReproducer` is true, the pass manager
834 /// will attempt to generate a local reproducer that contains the smallest
835 /// pipeline.
836 void PassManager::enableCrashReproducerGeneration(StringRef outputFile,
837                                                   bool genLocalReproducer) {
838   crashReproducerFileName = std::string(outputFile);
839   localReproducer = genLocalReproducer;
840 }
841 
842 /// Add the provided instrumentation to the pass manager.
843 void PassManager::addInstrumentation(std::unique_ptr<PassInstrumentation> pi) {
844   if (!instrumentor)
845     instrumentor = std::make_unique<PassInstrumentor>();
846 
847   instrumentor->addInstrumentation(std::move(pi));
848 }
849 
850 //===----------------------------------------------------------------------===//
851 // AnalysisManager
852 //===----------------------------------------------------------------------===//
853 
854 /// Returns a pass instrumentation object for the current operation.
855 PassInstrumentor *AnalysisManager::getPassInstrumentor() const {
856   ParentPointerT curParent = parent;
857   while (auto *parentAM = curParent.dyn_cast<const AnalysisManager *>())
858     curParent = parentAM->parent;
859   return curParent.get<const ModuleAnalysisManager *>()->getPassInstrumentor();
860 }
861 
862 /// Get an analysis manager for the given child operation.
863 AnalysisManager AnalysisManager::nest(Operation *op) {
864   auto it = impl->childAnalyses.find(op);
865   if (it == impl->childAnalyses.end())
866     it = impl->childAnalyses
867              .try_emplace(op, std::make_unique<NestedAnalysisMap>(op))
868              .first;
869   return {this, it->second.get()};
870 }
871 
872 /// Invalidate any non preserved analyses.
873 void detail::NestedAnalysisMap::invalidate(
874     const detail::PreservedAnalyses &pa) {
875   // If all analyses were preserved, then there is nothing to do here.
876   if (pa.isAll())
877     return;
878 
879   // Invalidate the analyses for the current operation directly.
880   analyses.invalidate(pa);
881 
882   // If no analyses were preserved, then just simply clear out the child
883   // analysis results.
884   if (pa.isNone()) {
885     childAnalyses.clear();
886     return;
887   }
888 
889   // Otherwise, invalidate each child analysis map.
890   SmallVector<NestedAnalysisMap *, 8> mapsToInvalidate(1, this);
891   while (!mapsToInvalidate.empty()) {
892     auto *map = mapsToInvalidate.pop_back_val();
893     for (auto &analysisPair : map->childAnalyses) {
894       analysisPair.second->invalidate(pa);
895       if (!analysisPair.second->childAnalyses.empty())
896         mapsToInvalidate.push_back(analysisPair.second.get());
897     }
898   }
899 }
900 
901 //===----------------------------------------------------------------------===//
902 // PassInstrumentation
903 //===----------------------------------------------------------------------===//
904 
905 PassInstrumentation::~PassInstrumentation() {}
906 
907 //===----------------------------------------------------------------------===//
908 // PassInstrumentor
909 //===----------------------------------------------------------------------===//
910 
911 namespace mlir {
912 namespace detail {
913 struct PassInstrumentorImpl {
914   /// Mutex to keep instrumentation access thread-safe.
915   llvm::sys::SmartMutex<true> mutex;
916 
917   /// Set of registered instrumentations.
918   std::vector<std::unique_ptr<PassInstrumentation>> instrumentations;
919 };
920 } // end namespace detail
921 } // end namespace mlir
922 
923 PassInstrumentor::PassInstrumentor() : impl(new PassInstrumentorImpl()) {}
924 PassInstrumentor::~PassInstrumentor() {}
925 
926 /// See PassInstrumentation::runBeforePipeline for details.
927 void PassInstrumentor::runBeforePipeline(
928     Identifier name,
929     const PassInstrumentation::PipelineParentInfo &parentInfo) {
930   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
931   for (auto &instr : impl->instrumentations)
932     instr->runBeforePipeline(name, parentInfo);
933 }
934 
935 /// See PassInstrumentation::runAfterPipeline for details.
936 void PassInstrumentor::runAfterPipeline(
937     Identifier name,
938     const PassInstrumentation::PipelineParentInfo &parentInfo) {
939   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
940   for (auto &instr : llvm::reverse(impl->instrumentations))
941     instr->runAfterPipeline(name, parentInfo);
942 }
943 
944 /// See PassInstrumentation::runBeforePass for details.
945 void PassInstrumentor::runBeforePass(Pass *pass, Operation *op) {
946   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
947   for (auto &instr : impl->instrumentations)
948     instr->runBeforePass(pass, op);
949 }
950 
951 /// See PassInstrumentation::runAfterPass for details.
952 void PassInstrumentor::runAfterPass(Pass *pass, Operation *op) {
953   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
954   for (auto &instr : llvm::reverse(impl->instrumentations))
955     instr->runAfterPass(pass, op);
956 }
957 
958 /// See PassInstrumentation::runAfterPassFailed for details.
959 void PassInstrumentor::runAfterPassFailed(Pass *pass, Operation *op) {
960   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
961   for (auto &instr : llvm::reverse(impl->instrumentations))
962     instr->runAfterPassFailed(pass, op);
963 }
964 
965 /// See PassInstrumentation::runBeforeAnalysis for details.
966 void PassInstrumentor::runBeforeAnalysis(StringRef name, TypeID id,
967                                          Operation *op) {
968   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
969   for (auto &instr : impl->instrumentations)
970     instr->runBeforeAnalysis(name, id, op);
971 }
972 
973 /// See PassInstrumentation::runAfterAnalysis for details.
974 void PassInstrumentor::runAfterAnalysis(StringRef name, TypeID id,
975                                         Operation *op) {
976   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
977   for (auto &instr : llvm::reverse(impl->instrumentations))
978     instr->runAfterAnalysis(name, id, op);
979 }
980 
981 /// Add the given instrumentation to the collection.
982 void PassInstrumentor::addInstrumentation(
983     std::unique_ptr<PassInstrumentation> pi) {
984   llvm::sys::SmartScopedLock<true> instrumentationLock(impl->mutex);
985   impl->instrumentations.emplace_back(std::move(pi));
986 }
987