[Mlir-commits] [mlir] 936fc55 - [MLIR] Add custom option parser type to PassOption

River Riddle llvmlistbot at llvm.org
Tue Mar 10 11:01:57 PDT 2020


Author: Eugene Zhulenev
Date: 2020-03-10T11:01:46-07:00
New Revision: 936fc55bd46df7dbd3f3e84e6946373b351328d8

URL: https://github.com/llvm/llvm-project/commit/936fc55bd46df7dbd3f3e84e6946373b351328d8
DIFF: https://github.com/llvm/llvm-project/commit/936fc55bd46df7dbd3f3e84e6946373b351328d8.diff

LOG: [MLIR] Add custom option parser type to PassOption

Summary: Pass custom DataType parser to pass options, similar to llvm::cl::opt class.

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

Added: 
    

Modified: 
    mlir/include/mlir/Pass/Pass.h
    mlir/include/mlir/Pass/PassOptions.h

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Pass/Pass.h b/mlir/include/mlir/Pass/Pass.h
index 279aeac2429a..80c4ddfeae62 100644
--- a/mlir/include/mlir/Pass/Pass.h
+++ b/mlir/include/mlir/Pass/Pass.h
@@ -66,23 +66,26 @@ class Pass {
   //===--------------------------------------------------------------------===//
 
   /// This class represents a specific pass option, with a provided data type.
-  template <typename DataType>
-  struct Option : public detail::PassOptions::Option<DataType> {
+  template <typename DataType,
+            typename OptionParser = detail::PassOptions::OptionParser<DataType>>
+  struct Option : public detail::PassOptions::Option<DataType, OptionParser> {
     template <typename... Args>
     Option(Pass &parent, StringRef arg, Args &&... args)
-        : detail::PassOptions::Option<DataType>(parent.passOptions, arg,
-                                                std::forward<Args>(args)...) {}
-    using detail::PassOptions::Option<DataType>::operator=;
+        : detail::PassOptions::Option<DataType, OptionParser>(
+              parent.passOptions, arg, std::forward<Args>(args)...) {}
+    using detail::PassOptions::Option<DataType, OptionParser>::operator=;
   };
   /// This class represents a specific pass option that contains a list of
   /// values of the provided data type.
-  template <typename DataType>
-  struct ListOption : public detail::PassOptions::ListOption<DataType> {
+  template <typename DataType,
+            typename OptionParser = detail::PassOptions::OptionParser<DataType>>
+  struct ListOption
+      : public detail::PassOptions::ListOption<DataType, OptionParser> {
     template <typename... Args>
     ListOption(Pass &parent, StringRef arg, Args &&... args)
-        : detail::PassOptions::ListOption<DataType>(
+        : detail::PassOptions::ListOption<DataType, OptionParser>(
               parent.passOptions, arg, std::forward<Args>(args)...) {}
-    using detail::PassOptions::ListOption<DataType>::operator=;
+    using detail::PassOptions::ListOption<DataType, OptionParser>::operator=;
   };
 
   /// Attempt to initialize the options of this pass from the given string.

diff  --git a/mlir/include/mlir/Pass/PassOptions.h b/mlir/include/mlir/Pass/PassOptions.h
index 7b35609b8c32..d360bad673fa 100644
--- a/mlir/include/mlir/Pass/PassOptions.h
+++ b/mlir/include/mlir/Pass/PassOptions.h
@@ -69,18 +69,6 @@ class PassOptions : protected llvm::cl::SubCommand {
     }
   };
 
-  /// The specific parser to use depending on llvm::cl parser used. This is only
-  /// necessary because we need to provide additional methods for certain data
-  /// type parsers.
-  /// TODO(riverriddle) We should upstream the methods in GenericOptionParser to
-  /// avoid the need to do this.
-  template <typename DataType>
-  using OptionParser =
-      std::conditional_t<std::is_base_of<llvm::cl::generic_parser_base,
-                                         llvm::cl::parser<DataType>>::value,
-                         GenericOptionParser<DataType>,
-                         llvm::cl::parser<DataType>>;
-
   /// Utility methods for printing option values.
   template <typename DataT>
   static void printValue(raw_ostream &os, GenericOptionParser<DataT> &parser,
@@ -100,23 +88,34 @@ class PassOptions : protected llvm::cl::SubCommand {
   }
 
 public:
-  /// This class represents a specific pass option, with a provided data type.
+  /// The specific parser to use depending on llvm::cl parser used. This is only
+  /// necessary because we need to provide additional methods for certain data
+  /// type parsers.
+  /// TODO(riverriddle) We should upstream the methods in GenericOptionParser to
+  /// avoid the need to do this.
   template <typename DataType>
-  class Option : public llvm::cl::opt<DataType, /*ExternalStorage=*/false,
-                                      OptionParser<DataType>>,
-                 public OptionBase {
+  using OptionParser =
+      std::conditional_t<std::is_base_of<llvm::cl::generic_parser_base,
+                                         llvm::cl::parser<DataType>>::value,
+                         GenericOptionParser<DataType>,
+                         llvm::cl::parser<DataType>>;
+
+  /// This class represents a specific pass option, with a provided data type.
+  template <typename DataType, typename OptionParser = OptionParser<DataType>>
+  class Option
+      : public llvm::cl::opt<DataType, /*ExternalStorage=*/false, OptionParser>,
+        public OptionBase {
   public:
     template <typename... Args>
     Option(PassOptions &parent, StringRef arg, Args &&... args)
-        : llvm::cl::opt<DataType, /*ExternalStorage=*/false,
-                        OptionParser<DataType>>(arg, llvm::cl::sub(parent),
-                                                std::forward<Args>(args)...) {
+        : llvm::cl::opt<DataType, /*ExternalStorage=*/false, OptionParser>(
+              arg, llvm::cl::sub(parent), std::forward<Args>(args)...) {
       assert(!this->isPositional() && !this->isSink() &&
              "sink and positional options are not supported");
       parent.options.push_back(this);
     }
     using llvm::cl::opt<DataType, /*ExternalStorage=*/false,
-                        OptionParser<DataType>>::operator=;
+                        OptionParser>::operator=;
     ~Option() override = default;
 
   private:
@@ -131,22 +130,22 @@ class PassOptions : protected llvm::cl::SubCommand {
 
     /// Copy the value from the given option into this one.
     void copyValueFrom(const OptionBase &other) final {
-      this->setValue(static_cast<const Option<DataType> &>(other).getValue());
+      this->setValue(static_cast<const Option<DataType, OptionParser> &>(other)
+                         .getValue());
     }
   };
 
   /// This class represents a specific pass option that contains a list of
   /// values of the provided data type.
-  template <typename DataType>
-  class ListOption : public llvm::cl::list<DataType, /*StorageClass=*/bool,
-                                           OptionParser<DataType>>,
-                     public OptionBase {
+  template <typename DataType, typename OptionParser = OptionParser<DataType>>
+  class ListOption
+      : public llvm::cl::list<DataType, /*StorageClass=*/bool, OptionParser>,
+        public OptionBase {
   public:
     template <typename... Args>
     ListOption(PassOptions &parent, StringRef arg, Args &&... args)
-        : llvm::cl::list<DataType, /*StorageClass=*/bool,
-                         OptionParser<DataType>>(arg, llvm::cl::sub(parent),
-                                                 std::forward<Args>(args)...) {
+        : llvm::cl::list<DataType, /*StorageClass=*/bool, OptionParser>(
+              arg, llvm::cl::sub(parent), std::forward<Args>(args)...) {
       assert(!this->isPositional() && !this->isSink() &&
              "sink and positional options are not supported");
       parent.options.push_back(this);
@@ -154,7 +153,7 @@ class PassOptions : protected llvm::cl::SubCommand {
     ~ListOption() override = default;
 
     /// Allow assigning from an ArrayRef.
-    ListOption<DataType> &operator=(ArrayRef<DataType> values) {
+    ListOption<DataType, OptionParser> &operator=(ArrayRef<DataType> values) {
       (*this)->assign(values.begin(), values.end());
       return *this;
     }
@@ -177,7 +176,8 @@ class PassOptions : protected llvm::cl::SubCommand {
     /// Copy the value from the given option into this one.
     void copyValueFrom(const OptionBase &other) final {
       (*this) = ArrayRef<DataType>(
-          (ListOption<DataType> &)(const_cast<OptionBase &>(other)));
+          (ListOption<DataType, OptionParser> &)(const_cast<OptionBase &>(
+              other)));
     }
   };
 


        


More information about the Mlir-commits mailing list