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