[PATCH] D76025: [mlir][NFC] Use fold expressions instead of variadic class templates for adding operations/etc. to dialects.

River Riddle via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 11 14:07:36 PDT 2020


rriddle created this revision.
rriddle added a reviewer: matthiaskramm.
Herald added subscribers: llvm-commits, Joonsoo, liufengdb, lucyrfox, mgester, arpith-jacob, nicolasvasilache, antiagainst, shauheen, burmako, jpienaar, mehdi_amini.
Herald added a project: LLVM.

This is much simpler, and also greatly reduces the generated template recursion stack.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76025

Files:
  mlir/include/mlir/IR/Dialect.h


Index: mlir/include/mlir/IR/Dialect.h
===================================================================
--- mlir/include/mlir/IR/Dialect.h
+++ mlir/include/mlir/IR/Dialect.h
@@ -181,57 +181,22 @@
   /// This method is used by derived classes to add their operations to the set.
   ///
   template <typename... Args> void addOperations() {
-    VariadicOperationAdder<Args...>::addToSet(*this);
+    (void)std::initializer_list<int>{
+        0, (addOperation(AbstractOperation::get<Args>(*this)), 0)...};
   }
 
-  // It would be nice to define this as variadic functions instead of a nested
-  // variadic type, but we can't do that: function template partial
-  // specialization is not allowed, and we can't define an overload set because
-  // we don't have any arguments of the types we are pushing around.
-  template <typename First, typename... Rest> class VariadicOperationAdder {
-  public:
-    static void addToSet(Dialect &dialect) {
-      dialect.addOperation(AbstractOperation::get<First>(dialect));
-      VariadicOperationAdder<Rest...>::addToSet(dialect);
-    }
-  };
-
-  template <typename First> class VariadicOperationAdder<First> {
-  public:
-    static void addToSet(Dialect &dialect) {
-      dialect.addOperation(AbstractOperation::get<First>(dialect));
-    }
-  };
-
   void addOperation(AbstractOperation opInfo);
 
   /// This method is used by derived classes to add their types to the set.
   template <typename... Args> void addTypes() {
-    VariadicSymbolAdder<Args...>::addToSet(*this);
+    (void)std::initializer_list<int>{0, (addSymbol(Args::getClassID()), 0)...};
   }
 
   /// This method is used by derived classes to add their attributes to the set.
   template <typename... Args> void addAttributes() {
-    VariadicSymbolAdder<Args...>::addToSet(*this);
+    (void)std::initializer_list<int>{0, (addSymbol(Args::getClassID()), 0)...};
   }
 
-  // It would be nice to define this as variadic functions instead of a nested
-  // variadic type, but we can't do that: function template partial
-  // specialization is not allowed, and we can't define an overload set
-  // because we don't have any arguments of the types we are pushing around.
-  template <typename First, typename... Rest> struct VariadicSymbolAdder {
-    static void addToSet(Dialect &dialect) {
-      VariadicSymbolAdder<First>::addToSet(dialect);
-      VariadicSymbolAdder<Rest...>::addToSet(dialect);
-    }
-  };
-
-  template <typename First> struct VariadicSymbolAdder<First> {
-    static void addToSet(Dialect &dialect) {
-      dialect.addSymbol(First::getClassID());
-    }
-  };
-
   /// Enable support for unregistered operations.
   void allowUnknownOperations(bool allow = true) { unknownOpsAllowed = allow; }
 
@@ -242,12 +207,9 @@
   void addInterface(std::unique_ptr<DialectInterface> interface);
 
   /// Register a set of dialect interfaces with this dialect instance.
-  template <typename T, typename T2, typename... Tys> void addInterfaces() {
-    addInterfaces<T>();
-    addInterfaces<T2, Tys...>();
-  }
-  template <typename T> void addInterfaces() {
-    addInterface(std::make_unique<T>(this));
+  template <typename... Args> void addInterfaces() {
+    (void)std::initializer_list<int>{
+        0, (addInterface(std::make_unique<Args>(this)), 0)...};
   }
 
 private:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76025.249746.patch
Type: text/x-patch
Size: 3319 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200311/e2053969/attachment.bin>


More information about the llvm-commits mailing list