github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/lib/Pass/PassDetail.h (about)

     1  //===- PassDetail.h - MLIR Pass details -------------------------*- C++ -*-===//
     2  //
     3  // Copyright 2019 The MLIR Authors.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //   http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  // =============================================================================
    17  #ifndef MLIR_PASS_PASSDETAIL_H_
    18  #define MLIR_PASS_PASSDETAIL_H_
    19  
    20  #include "mlir/Pass/Pass.h"
    21  
    22  namespace mlir {
    23  namespace detail {
    24  
    25  //===----------------------------------------------------------------------===//
    26  // Verifier Passes
    27  //===----------------------------------------------------------------------===//
    28  
    29  /// Pass to verify a function and signal failure if necessary.
    30  class FunctionVerifierPass : public FunctionPass<FunctionVerifierPass> {
    31    void runOnFunction() override;
    32  };
    33  
    34  /// Pass to verify a module and signal failure if necessary.
    35  class ModuleVerifierPass : public ModulePass<ModuleVerifierPass> {
    36    void runOnModule() override;
    37  };
    38  
    39  //===----------------------------------------------------------------------===//
    40  // PassExecutor
    41  //===----------------------------------------------------------------------===//
    42  
    43  /// The abstract base pass executor class.
    44  class PassExecutor {
    45  public:
    46    enum Kind { FunctionExecutor, ModuleExecutor };
    47    explicit PassExecutor(Kind kind) : kind(kind) {}
    48  
    49    /// Get the kind of this executor.
    50    Kind getKind() const { return kind; }
    51  
    52  private:
    53    /// The kind of executor this object is.
    54    Kind kind;
    55  };
    56  
    57  /// A pass executor that contains a list of passes over a function.
    58  class FunctionPassExecutor : public PassExecutor {
    59  public:
    60    FunctionPassExecutor() : PassExecutor(Kind::FunctionExecutor) {}
    61    FunctionPassExecutor(FunctionPassExecutor &&) = default;
    62    FunctionPassExecutor(const FunctionPassExecutor &rhs);
    63  
    64    /// Run the executor on the given function.
    65    LogicalResult run(FuncOp function, AnalysisManager am);
    66  
    67    /// Add a pass to the current executor. This takes ownership over the provided
    68    /// pass pointer.
    69    void addPass(std::unique_ptr<Pass> pass) {
    70      passes.push_back(std::move(pass));
    71    }
    72  
    73    /// Returns the number of passes held by this executor.
    74    size_t size() const { return passes.size(); }
    75  
    76    static bool classof(const PassExecutor *pe) {
    77      return pe->getKind() == Kind::FunctionExecutor;
    78    }
    79  
    80  private:
    81    std::vector<std::unique_ptr<Pass>> passes;
    82  };
    83  
    84  /// A pass executor that contains a list of passes over a module unit.
    85  class ModulePassExecutor : public PassExecutor {
    86  public:
    87    ModulePassExecutor() : PassExecutor(Kind::ModuleExecutor) {}
    88    ModulePassExecutor(ModulePassExecutor &&) = default;
    89  
    90    // Don't allow copying.
    91    ModulePassExecutor(const ModulePassExecutor &) = delete;
    92    ModulePassExecutor &operator=(const ModulePassExecutor &) = delete;
    93  
    94    /// Run the executor on the given module.
    95    LogicalResult run(ModuleOp module, AnalysisManager am);
    96  
    97    /// Add a pass to the current executor. This takes ownership over the provided
    98    /// pass pointer.
    99    void addPass(std::unique_ptr<ModulePassBase> pass) {
   100      passes.push_back(std::move(pass));
   101    }
   102  
   103    static bool classof(const PassExecutor *pe) {
   104      return pe->getKind() == Kind::ModuleExecutor;
   105    }
   106  
   107  private:
   108    /// Set of passes to run on the given module.
   109    std::vector<std::unique_ptr<ModulePassBase>> passes;
   110  };
   111  
   112  //===----------------------------------------------------------------------===//
   113  // ModuleToFunctionPassAdaptor
   114  //===----------------------------------------------------------------------===//
   115  
   116  /// An adaptor module pass used to run function passes over all of the
   117  /// non-external functions of a module synchronously on a single thread.
   118  class ModuleToFunctionPassAdaptor
   119      : public ModulePass<ModuleToFunctionPassAdaptor> {
   120  public:
   121    /// Run the held function pipeline over all non-external functions within the
   122    /// module.
   123    void runOnModule() override;
   124  
   125    /// Returns the function pass executor for this adaptor.
   126    FunctionPassExecutor &getFunctionExecutor() { return fpe; }
   127  
   128  private:
   129    FunctionPassExecutor fpe;
   130  };
   131  
   132  /// An adaptor module pass used to run function passes over all of the
   133  /// non-external functions of a module asynchronously across multiple threads.
   134  class ModuleToFunctionPassAdaptorParallel
   135      : public ModulePass<ModuleToFunctionPassAdaptorParallel> {
   136  public:
   137    /// Run the held function pipeline over all non-external functions within the
   138    /// module.
   139    void runOnModule() override;
   140  
   141    /// Returns the function pass executor for this adaptor.
   142    FunctionPassExecutor &getFunctionExecutor() { return fpe; }
   143  
   144  private:
   145    // The main function pass executor for this adaptor.
   146    FunctionPassExecutor fpe;
   147  
   148    // A set of executors, cloned from the main executor, that run asynchronously
   149    // on different threads.
   150    std::vector<FunctionPassExecutor> asyncExecutors;
   151  };
   152  
   153  /// Utility function to return if a pass refers to an
   154  /// ModuleToFunctionPassAdaptor instance.
   155  inline bool isModuleToFunctionAdaptorPass(Pass *pass) {
   156    return isa<ModuleToFunctionPassAdaptorParallel>(pass) ||
   157           isa<ModuleToFunctionPassAdaptor>(pass);
   158  }
   159  
   160  /// Utility function to return if a pass refers to an adaptor pass. Adaptor
   161  /// passes are those that internally execute a pipeline, such as the
   162  /// ModuleToFunctionPassAdaptor.
   163  inline bool isAdaptorPass(Pass *pass) {
   164    return isModuleToFunctionAdaptorPass(pass);
   165  }
   166  
   167  /// Utility function to return if a pass refers to a verifier pass.
   168  inline bool isVerifierPass(Pass *pass) {
   169    return isa<FunctionVerifierPass>(pass) || isa<ModuleVerifierPass>(pass);
   170  }
   171  
   172  } // end namespace detail
   173  } // end namespace mlir
   174  #endif // MLIR_PASS_PASSDETAIL_H_