//===- PassRegistry.h - Pass Registration Utilities -------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file contains utilities for registering information about compiler // passes. // //===----------------------------------------------------------------------===// #ifndef MLIR_PASS_PASSREGISTRY_H_ #define MLIR_PASS_PASSREGISTRY_H_ #include "mlir/Pass/PassOptions.h" #include "mlir/Support/TypeID.h" #include <functional> #include <utility> #include <optional> namespace mlir { class OpPassManager; class ParserConfig; class Pass; class PassManager; namespace detail { class PassOptions; } // namespace detail /// A registry function that adds passes to the given pass manager. This should /// also parse options and return success() if parsing succeeded. /// `errorHandler` is a functor used to emit errors during parsing. /// parameter corresponds to the raw location within the pipeline string. This /// should always return failure. PassRegistryFunction; PassAllocatorFunction; //===----------------------------------------------------------------------===// // PassRegistry //===----------------------------------------------------------------------===// /// Prints the passes that were previously registered and stored in passRegistry void printRegisteredPasses(); /// Structure to group information about a passes and pass pipelines (argument /// to invoke via mlir-opt, description, pass pipeline builder). class PassRegistryEntry { … }; /// A structure to represent the information of a registered pass pipeline. class PassPipelineInfo : public PassRegistryEntry { … }; /// A structure to represent the information for a derived pass class. class PassInfo : public PassRegistryEntry { … }; //===----------------------------------------------------------------------===// // PassRegistration //===----------------------------------------------------------------------===// /// Register a specific dialect pipeline registry function with the system, /// typically used through the PassPipelineRegistration template. void registerPassPipeline( StringRef arg, StringRef description, const PassRegistryFunction &function, std::function<void(function_ref<void(const detail::PassOptions &)>)> optHandler); /// Register a specific dialect pass allocator function with the system, /// typically used through the PassRegistration template. void registerPass(const PassAllocatorFunction &function); /// PassRegistration provides a global initializer that registers a Pass /// allocation routine for a concrete pass instance. The argument is /// optional and provides a callback to construct a pass that does not have /// a default constructor. /// /// Usage: /// /// /// At namespace scope. /// static PassRegistration<MyPass> reg; /// template <typename ConcretePass> struct PassRegistration { … }; /// PassPipelineRegistration provides a global initializer that registers a Pass /// pipeline builder routine. /// /// Usage: /// /// // At namespace scope. /// void pipelineBuilder(OpPassManager &pm) { /// pm.addPass(new MyPass()); /// pm.addPass(new MyOtherPass()); /// } /// /// static PassPipelineRegistration Unused("unused", "Unused pass", /// pipelineBuilder); template <typename Options = EmptyPipelineOptions> struct PassPipelineRegistration { … }; /// Convenience specialization of PassPipelineRegistration for EmptyPassOptions /// that does not pass an empty options struct to the pass builder function. template <> struct PassPipelineRegistration<EmptyPipelineOptions> { … }; /// Parse the textual representation of a pass pipeline, adding the result to /// 'pm' on success. Returns failure if the given pipeline was invalid. /// 'errorStream' is the output stream used to emit errors found during parsing. LogicalResult parsePassPipeline(StringRef pipeline, OpPassManager &pm, raw_ostream &errorStream = llvm::errs()); /// Parse the given textual representation of a pass pipeline, and return the /// parsed pipeline on success. The given pipeline string should be wrapped with /// the desired type of operation to root the created operation, i.e. /// `builtin.module(cse)` over `cse`. Returns failure if the given pipeline was /// invalid. 'errorStream' is the output stream used to emit errors found during /// parsing. FailureOr<OpPassManager> parsePassPipeline(StringRef pipeline, raw_ostream &errorStream = llvm::errs()); //===----------------------------------------------------------------------===// // PassPipelineCLParser //===----------------------------------------------------------------------===// namespace detail { struct PassPipelineCLParserImpl; } // namespace detail /// This class implements a command-line parser for MLIR passes. It registers a /// cl option with a given argument and description. This parser will register /// options for each of the passes and pipelines that have been registered with /// the pass registry; Meaning that `-cse` will refer to the CSE pass in MLIR. /// It also registers an argument, `pass-pipeline`, that supports parsing a /// textual description of a pipeline. This option is mutually exclusive with /// the individual pass options. class PassPipelineCLParser { … }; /// This class implements a command-line parser specifically for MLIR pass /// names. It registers a cl option with a given argument and description that /// accepts a comma delimited list of pass names. class PassNameCLParser { … }; //===----------------------------------------------------------------------===// // Pass Reproducer //===----------------------------------------------------------------------===// struct PassReproducerOptions { … }; } // namespace mlir #endif // MLIR_PASS_PASSREGISTRY_H_