Skip to content

Commit 9bd62a6

Browse files
River707memfrob
authored andcommitted
[mlir][NFC] Use fold expressions instead of variadic class templates for adding operations/etc. to dialects.
Summary: This is much simpler, and also greatly reduces the generated template recursion stack. Differential Revision: https://reviews.llvm.org/D76025
1 parent 35c121f commit 9bd62a6

File tree

1 file changed

+7
-45
lines changed

1 file changed

+7
-45
lines changed

mlir/include/mlir/IR/Dialect.h

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -181,57 +181,22 @@ class Dialect {
181181
/// This method is used by derived classes to add their operations to the set.
182182
///
183183
template <typename... Args> void addOperations() {
184-
VariadicOperationAdder<Args...>::addToSet(*this);
184+
(void)std::initializer_list<int>{
185+
0, (addOperation(AbstractOperation::get<Args>(*this)), 0)...};
185186
}
186187

187-
// It would be nice to define this as variadic functions instead of a nested
188-
// variadic type, but we can't do that: function template partial
189-
// specialization is not allowed, and we can't define an overload set because
190-
// we don't have any arguments of the types we are pushing around.
191-
template <typename First, typename... Rest> class VariadicOperationAdder {
192-
public:
193-
static void addToSet(Dialect &dialect) {
194-
dialect.addOperation(AbstractOperation::get<First>(dialect));
195-
VariadicOperationAdder<Rest...>::addToSet(dialect);
196-
}
197-
};
198-
199-
template <typename First> class VariadicOperationAdder<First> {
200-
public:
201-
static void addToSet(Dialect &dialect) {
202-
dialect.addOperation(AbstractOperation::get<First>(dialect));
203-
}
204-
};
205-
206188
void addOperation(AbstractOperation opInfo);
207189

208190
/// This method is used by derived classes to add their types to the set.
209191
template <typename... Args> void addTypes() {
210-
VariadicSymbolAdder<Args...>::addToSet(*this);
192+
(void)std::initializer_list<int>{0, (addSymbol(Args::getClassID()), 0)...};
211193
}
212194

213195
/// This method is used by derived classes to add their attributes to the set.
214196
template <typename... Args> void addAttributes() {
215-
VariadicSymbolAdder<Args...>::addToSet(*this);
197+
(void)std::initializer_list<int>{0, (addSymbol(Args::getClassID()), 0)...};
216198
}
217199

218-
// It would be nice to define this as variadic functions instead of a nested
219-
// variadic type, but we can't do that: function template partial
220-
// specialization is not allowed, and we can't define an overload set
221-
// because we don't have any arguments of the types we are pushing around.
222-
template <typename First, typename... Rest> struct VariadicSymbolAdder {
223-
static void addToSet(Dialect &dialect) {
224-
VariadicSymbolAdder<First>::addToSet(dialect);
225-
VariadicSymbolAdder<Rest...>::addToSet(dialect);
226-
}
227-
};
228-
229-
template <typename First> struct VariadicSymbolAdder<First> {
230-
static void addToSet(Dialect &dialect) {
231-
dialect.addSymbol(First::getClassID());
232-
}
233-
};
234-
235200
/// Enable support for unregistered operations.
236201
void allowUnknownOperations(bool allow = true) { unknownOpsAllowed = allow; }
237202

@@ -242,12 +207,9 @@ class Dialect {
242207
void addInterface(std::unique_ptr<DialectInterface> interface);
243208

244209
/// Register a set of dialect interfaces with this dialect instance.
245-
template <typename T, typename T2, typename... Tys> void addInterfaces() {
246-
addInterfaces<T>();
247-
addInterfaces<T2, Tys...>();
248-
}
249-
template <typename T> void addInterfaces() {
250-
addInterface(std::make_unique<T>(this));
210+
template <typename... Args> void addInterfaces() {
211+
(void)std::initializer_list<int>{
212+
0, (addInterface(std::make_unique<Args>(this)), 0)...};
251213
}
252214

253215
private:

0 commit comments

Comments
 (0)