//===- PassOptions.h - Pass Option 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 options with compiler passes and // pipelines. // //===----------------------------------------------------------------------===// #ifndef MLIR_PASS_PASSOPTIONS_H_ #define MLIR_PASS_PASSOPTIONS_H_ #include "mlir/Support/LLVM.h" #include "llvm/ADT/FunctionExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Compiler.h" #include <memory> namespace mlir { class OpPassManager; namespace detail { namespace pass_options { /// Parse a string containing a list of comma-delimited elements, invoking the /// given parser for each sub-element and passing them to the provided /// element-append functor. LogicalResult parseCommaSeparatedList(llvm::cl::Option &opt, StringRef argName, StringRef optionStr, function_ref<LogicalResult(StringRef)> elementParseFn); template <typename ElementParser, typename ElementAppendFn> LogicalResult parseCommaSeparatedList(llvm::cl::Option &opt, StringRef argName, StringRef optionStr, ElementParser &elementParser, ElementAppendFn &&appendFn) { … } /// Trait used to detect if a type has a operator<< method. has_stream_operator_trait; has_stream_operator; /// Utility methods for printing option values. template <typename ParserT> static void printOptionValue(raw_ostream &os, const bool &value) { … } template <typename ParserT> static void printOptionValue(raw_ostream &os, const std::string &str) { … } template <typename ParserT, typename DataT> static std::enable_if_t<has_stream_operator<DataT>::value> printOptionValue(raw_ostream &os, const DataT &value) { … } template <typename ParserT, typename DataT> static std::enable_if_t<!has_stream_operator<DataT>::value> printOptionValue(raw_ostream &os, const DataT &value) { … } } // namespace pass_options /// Base container class and manager for all pass options. class PassOptions : protected llvm::cl::SubCommand { … }; } // namespace detail //===----------------------------------------------------------------------===// // PassPipelineOptions //===----------------------------------------------------------------------===// /// Subclasses of PassPipelineOptions provide a set of options that can be used /// to initialize a pass pipeline. See PassPipelineRegistration for usage /// details. /// /// Usage: /// /// struct MyPipelineOptions : PassPipelineOptions<MyPassOptions> { /// ListOption<int> someListFlag{*this, "flag-name", llvm::cl::desc("...")}; /// }; template <typename T> class PassPipelineOptions : public detail::PassOptions { … }; /// A default empty option struct to be used for passes that do not need to take /// any options. struct EmptyPipelineOptions : public PassPipelineOptions<EmptyPipelineOptions> { … }; } // namespace mlir //===----------------------------------------------------------------------===// // MLIR Options //===----------------------------------------------------------------------===// namespace llvm { namespace cl { //===----------------------------------------------------------------------===// // std::vector+SmallVector namespace detail { template <typename VectorT, typename ElementT> class VectorParserBase : public basic_parser_impl { … }; } // namespace detail parser<std::vector<T>>; parser<SmallVector<T, N>>; //===----------------------------------------------------------------------===// // OpPassManager: OptionValue template <> struct OptionValue<mlir::OpPassManager> final : GenericOptionValue { … }; //===----------------------------------------------------------------------===// // OpPassManager: Parser extern template class basic_parser<mlir::OpPassManager>; template <> class parser<mlir::OpPassManager> : public basic_parser<mlir::OpPassManager> { … }; } // namespace cl } // namespace llvm #endif // MLIR_PASS_PASSOPTIONS_H_