Clarify the invariant of the MLIR pass pipeline around `Pass::initialize()`
authorMehdi Amini <joker.eph@gmail.com>
Sat, 5 Aug 2023 21:53:03 +0000 (14:53 -0700)
committerTobias Hieta <tobias@hieta.se>
Thu, 10 Aug 2023 07:06:20 +0000 (09:06 +0200)
This method should not load new dialect or affect the context itself.

Differential Revision: https://reviews.llvm.org/D157198

mlir/include/mlir/Pass/Pass.h
mlir/lib/Pass/Pass.cpp

index 714667c239a8aac3b14a9118ab047c835cee2254..028ab4bc2cda9223155976ab60db21a8fdd4c2e1 100644 (file)
@@ -180,8 +180,10 @@ protected:
   /// should not rely on any state accessible during the execution of a pass.
   /// For example, `getContext`/`getOperation`/`getAnalysis`/etc. should not be
   /// invoked within this hook.
-  /// Returns a LogicalResult to indicate failure, in which case the pass
-  /// pipeline won't execute.
+  /// This method is invoked after all dependent dialects for the pipeline are
+  /// loaded, and is not allowed to load any further dialects (override the
+  /// `geDependentDialects()` for this purpose instead). Returns a LogicalResult
+  /// to indicate failure, in which case the pass pipeline won't execute.
   virtual LogicalResult initialize(MLIRContext *context) { return success(); }
 
   /// Indicate if the current pass can be scheduled on the given operation type.
index c7738a2248a103136a648e36e50defac43073391..fe4597f3df3d2544434289e9ffa26e89f6c05104 100644 (file)
@@ -820,6 +820,9 @@ LogicalResult PassManager::run(Operation *op) {
   if (failed(getImpl().finalizePassList(context)))
     return failure();
 
+  // Notify the context that we start running a pipeline for book keeping.
+  context->enterMultiThreadedExecution();
+
   // Initialize all of the passes within the pass manager with a new generation.
   llvm::hash_code newInitKey = context->getRegistryHash();
   if (newInitKey != initializationKey) {
@@ -831,9 +834,6 @@ LogicalResult PassManager::run(Operation *op) {
   // Construct a top level analysis manager for the pipeline.
   ModuleAnalysisManager am(op, instrumentor.get());
 
-  // Notify the context that we start running a pipeline for book keeping.
-  context->enterMultiThreadedExecution();
-
   // If reproducer generation is enabled, run the pass manager with crash
   // handling enabled.
   LogicalResult result =