[Mlir-commits] [mlir] [MLIR] Convert DialectReductionPatternInterface using ODS (PR #180640)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Tue Feb 10 12:03:54 PST 2026
https://github.com/aidint updated https://github.com/llvm/llvm-project/pull/180640
>From 71e6dc1fe2579d1bd3cdf80ef5b75d9253626790 Mon Sep 17 00:00:00 2001
From: aidint <at.aidin at gmail.com>
Date: Tue, 10 Feb 2026 00:33:00 +0100
Subject: [PATCH 1/2] convert DialectReductionPatternInterface using ODS
---
mlir/include/mlir/IR/Interfaces.td | 5 ++
mlir/include/mlir/Reducer/CMakeLists.txt | 4 ++
.../mlir/Reducer/ReductionPatternInterface.h | 47 +--------------
.../mlir/Reducer/ReductionPatternInterface.td | 59 +++++++++++++++++++
mlir/include/mlir/TableGen/Interfaces.h | 3 +
mlir/lib/Reducer/CMakeLists.txt | 1 +
mlir/lib/TableGen/Interfaces.cpp | 5 ++
mlir/test/mlir-tblgen/dialect-interface.td | 23 +++++++-
.../mlir-tblgen/DialectInterfacesGen.cpp | 35 ++++++++++-
9 files changed, 133 insertions(+), 49 deletions(-)
create mode 100644 mlir/include/mlir/Reducer/ReductionPatternInterface.td
diff --git a/mlir/include/mlir/IR/Interfaces.td b/mlir/include/mlir/IR/Interfaces.td
index 149a254fa7a0d..e16de2942a043 100644
--- a/mlir/include/mlir/IR/Interfaces.td
+++ b/mlir/include/mlir/IR/Interfaces.td
@@ -85,6 +85,11 @@ class StaticInterfaceMethod<string desc, string retTy, string methodName,
: InterfaceMethod<desc, retTy, methodName, args, methodBody,
defaultImplementation>;
+// This class represents a pure virtual interface method.
+class PureVirtualInterfaceMethod<string desc, string retTy, string methodName,
+ dag args = (ins)>
+ : InterfaceMethod<desc, retTy, methodName, args>;
+
// This class represents a interface method declaration.
class InterfaceMethodDeclaration<string desc, string retTy, string methodName,
dag args = (ins)>
diff --git a/mlir/include/mlir/Reducer/CMakeLists.txt b/mlir/include/mlir/Reducer/CMakeLists.txt
index 3d09f87c6f17e..014ff6e334513 100644
--- a/mlir/include/mlir/Reducer/CMakeLists.txt
+++ b/mlir/include/mlir/Reducer/CMakeLists.txt
@@ -3,3 +3,7 @@ mlir_tablegen(Passes.h.inc -gen-pass-decls -name Reducer)
add_mlir_generic_tablegen_target(MLIRReducerIncGen)
add_mlir_doc(Passes ReducerPasses ./ -gen-pass-doc)
+
+set(LLVM_TARGET_DEFINITIONS ReductionPatternInterface.td)
+mlir_tablegen(ReductionPatternInterface.h.inc -gen-dialect-interface-decls)
+add_mlir_generic_tablegen_target(MLIRReductionPatternInterfaceGen)
diff --git a/mlir/include/mlir/Reducer/ReductionPatternInterface.h b/mlir/include/mlir/Reducer/ReductionPatternInterface.h
index a33877dc0bd77..d34c79a3ed05e 100644
--- a/mlir/include/mlir/Reducer/ReductionPatternInterface.h
+++ b/mlir/include/mlir/Reducer/ReductionPatternInterface.h
@@ -13,52 +13,9 @@
#include "mlir/Reducer/Tester.h"
namespace mlir {
-
class RewritePatternSet;
-
-/// This is used to report the reduction patterns for a Dialect. While using
-/// mlir-reduce to reduce a module, we may want to transform certain cases into
-/// simpler forms by applying certain rewrite patterns. Implement the
-/// `populateReductionPatterns` to report those patterns by adding them to the
-/// RewritePatternSet.
-///
-/// Example:
-/// MyDialectReductionPattern::populateReductionPatterns(
-/// RewritePatternSet &patterns) {
-/// patterns.add<TensorOpReduction>(patterns.getContext());
-/// }
-///
-/// For DRR, mlir-tblgen will generate a helper function
-/// `populateWithGenerated` which has the same signature therefore you can
-/// delegate to the helper function as well.
-///
-/// Example:
-/// MyDialectReductionPattern::populateReductionPatterns(
-/// RewritePatternSet &patterns) {
-/// // Include the autogen file somewhere above.
-/// populateWithGenerated(patterns);
-/// }
-class DialectReductionPatternInterface
- : public DialectInterface::Base<DialectReductionPatternInterface> {
-public:
- /// Patterns provided here are intended to transform operations from a complex
- /// form to a simpler form, without breaking the semantics of the program
- /// being reduced. For example, you may want to replace the
- /// tensor<?xindex> with a known rank and type, e.g. tensor<1xi32>, or
- /// replacing an operation with a constant.
- virtual void populateReductionPatterns(RewritePatternSet &patterns) const = 0;
-
- /// This method extends `populateReductionPatterns` by allowing reduction
- /// patterns to use a `Tester` instance. Some reduction patterns may need to
- /// run tester to determine whether certain transformations preserve the
- /// "interesting" behavior of the program. This is mostly useful when pattern
- /// should choose between multiple modifications.
- virtual void populateReductionPatternsWithTester(RewritePatternSet &patterns,
- Tester &tester) const {}
-
-protected:
- DialectReductionPatternInterface(Dialect *dialect) : Base(dialect) {}
-};
} // namespace mlir
+#include "mlir/Reducer/ReductionPatternInterface.h.inc"
+
#endif // MLIR_REDUCER_REDUCTIONPATTERNINTERFACE_H
diff --git a/mlir/include/mlir/Reducer/ReductionPatternInterface.td b/mlir/include/mlir/Reducer/ReductionPatternInterface.td
new file mode 100644
index 0000000000000..8f33897d7a75d
--- /dev/null
+++ b/mlir/include/mlir/Reducer/ReductionPatternInterface.td
@@ -0,0 +1,59 @@
+#ifndef MLIR_INTERFACES_DIALECTREDUCTIONPATTERNINTERFACE
+#define MLIR_INTERFACES_DIALECTREDUCTIONPATTERNINTERFACE
+
+include "mlir/IR/Interfaces.td"
+
+def DialectReductionPatternInterface : DialectInterface<"DialectReductionPatternInterface"> {
+ let description = [{
+ This is used to report the reduction patterns for a Dialect. While using
+ mlir-reduce to reduce a module, we may want to transform certain cases into
+ simpler forms by applying certain rewrite patterns. Implement the
+ `populateReductionPatterns` to report those patterns by adding them to the
+ RewritePatternSet.
+
+ Example:
+ MyDialectReductionPattern::populateReductionPatterns(
+ RewritePatternSet &patterns) {
+ patterns.add<TensorOpReduction>(patterns.getContext());
+ }
+
+ For DRR, mlir-tblgen will generate a helper function
+ `populateWithGenerated` which has the same signature therefore you can
+ delegate to the helper function as well.
+
+ Example:
+ MyDialectReductionPattern::populateReductionPatterns(
+ RewritePatternSet &patterns) {
+ // Include the autogen file somewhere above.
+ populateWithGenerated(patterns);
+ }
+ }];
+ let cppNamespace = "::mlir";
+
+ let methods = [
+ PureVirtualInterfaceMethod<[{
+ Patterns provided here are intended to transform operations from a complex
+ form to a simpler form, without breaking the semantics of the program
+ being reduced. For example, you may want to replace the
+ tensor<?xindex> with a known rank and type, e.g. tensor<1xi32>, or
+ replacing an operation with a constant.
+ }],
+ "void", "populateReductionPatterns",
+ (ins "::mlir::RewritePatternSet &":$patterns)
+ >,
+ InterfaceMethod<[{
+ This method extends `populateReductionPatterns` by allowing reduction
+ patterns to use a `Tester` instance. Some reduction patterns may need to
+ run tester to determine whether certain transformations preserve the
+ "interesting" behavior of the program. This is mostly useful when pattern
+ should choose between multiple modifications.
+ }],
+ "void", "populateReductionPatternsWithTester",
+ (ins "::mlir::RewritePatternSet &":$patterns, "::mlir::Tester &":$tester),
+ [{}]
+ >
+ ];
+}
+
+
+#endif
diff --git a/mlir/include/mlir/TableGen/Interfaces.h b/mlir/include/mlir/TableGen/Interfaces.h
index 09b5d6cbf6aa6..5b2bb34f8c153 100644
--- a/mlir/include/mlir/TableGen/Interfaces.h
+++ b/mlir/include/mlir/TableGen/Interfaces.h
@@ -47,6 +47,9 @@ class InterfaceMethod {
// Return if this method is static.
bool isStatic() const;
+ // Return if the method is a pure virtual one.
+ bool isPureVirtual() const;
+
// Return if the method is only a declaration.
bool isDeclaration() const;
diff --git a/mlir/lib/Reducer/CMakeLists.txt b/mlir/lib/Reducer/CMakeLists.txt
index a723263e4f41a..c3d8c610459e7 100644
--- a/mlir/lib/Reducer/CMakeLists.txt
+++ b/mlir/lib/Reducer/CMakeLists.txt
@@ -12,6 +12,7 @@ add_mlir_library(MLIRReduce
DEPENDS
MLIRReducerIncGen
+ MLIRReductionPatternInterfaceGen
)
mlir_check_all_link_libraries(MLIRReduce)
diff --git a/mlir/lib/TableGen/Interfaces.cpp b/mlir/lib/TableGen/Interfaces.cpp
index f92ef18710941..f4fa65777f585 100644
--- a/mlir/lib/TableGen/Interfaces.cpp
+++ b/mlir/lib/TableGen/Interfaces.cpp
@@ -52,6 +52,11 @@ bool InterfaceMethod::isStatic() const {
return def->isSubClassOf("StaticInterfaceMethod");
}
+// Return if the method is a pure virtual one.
+bool InterfaceMethod::isPureVirtual() const {
+ return def->isSubClassOf("PureVirtualInterfaceMethod");
+}
+
// Return if the method is only a declaration.
bool InterfaceMethod::isDeclaration() const {
return def->isSubClassOf("InterfaceMethodDeclaration");
diff --git a/mlir/test/mlir-tblgen/dialect-interface.td b/mlir/test/mlir-tblgen/dialect-interface.td
index d9035a63b2d2e..5e41fdedfa761 100644
--- a/mlir/test/mlir-tblgen/dialect-interface.td
+++ b/mlir/test/mlir-tblgen/dialect-interface.td
@@ -22,9 +22,9 @@ def NoDefaultMethod : DialectInterface<"NoDefaultMethod"> {
// DECL: class NoDefaultMethod : public {{.*}}DialectInterface::Base<NoDefaultMethod>
// DECL: public:
-// DECL: NoDefaultMethod(::mlir::Dialect *dialect) : Base(dialect) {}
// DECL: virtual bool isExampleDialect() const {}
// DECL: virtual unsigned supportSecondMethod(::mlir::Type type) const {}
+// DECL: NoDefaultMethod(::mlir::Dialect *dialect) : Base(dialect) {}
def WithDefaultMethodInterface : DialectInterface<"WithDefaultMethodInterface"> {
let description = [{
@@ -73,3 +73,24 @@ def OnlyDeclarationInterfaceWithExtraDecls : DialectInterface<"OnlyDeclarationIn
// DECL: class OnlyDeclarationInterfaceWithExtraDecls : public {{.*}}DialectInterface::Base<OnlyDeclarationInterfaceWithExtraDecls>
// DECL: virtual void exampleMethodDeclaration(::mlir::Type type) const;
// DECL: using DeclType = int;
+
+def PureVirtualInterface : DialectInterface<"PureVirtualInterface"> {
+ let description = [{
+ This is an example dialect interface with pure virtual methods.
+ }];
+
+ let cppNamespace = "::mlir::example";
+
+ let methods = [
+ PureVirtualInterfaceMethod<
+ "Check if it's an example dialect", "bool", "isExampleDialect",
+ (ins)
+ >
+ ];
+}
+
+// DECL: class PureVirtualInterface : public {{.*}}DialectInterface::Base<PureVirtualInterface>
+// DECL: public:
+// DECL: virtual bool isExampleDialect() const = 0;
+// DECL: protected:
+// DECL-NEXT: PureVirtualInterface(::mlir::Dialect *dialect) : Base(dialect) {}
diff --git a/mlir/tools/mlir-tblgen/DialectInterfacesGen.cpp b/mlir/tools/mlir-tblgen/DialectInterfacesGen.cpp
index 6ad426c78226d..e695b8c761895 100644
--- a/mlir/tools/mlir-tblgen/DialectInterfacesGen.cpp
+++ b/mlir/tools/mlir-tblgen/DialectInterfacesGen.cpp
@@ -113,6 +113,11 @@ static void emitInterfaceMethodsDef(const DialectInterface &interface,
continue;
}
+ if (method.isPureVirtual()) {
+ ios << " = 0;\n";
+ continue;
+ }
+
// if it is not a method declaration, then it's a normal interface method.
ios << " {";
@@ -126,6 +131,27 @@ static void emitInterfaceMethodsDef(const DialectInterface &interface,
}
}
+static void emitConstructor(const DialectInterface &interface,
+ raw_ostream &os) {
+
+ raw_indented_ostream ios(os);
+
+ // We consider a constructor protected if interface has at least one pure
+ // virtual method
+ auto hasProtectedConstructor =
+ llvm::any_of(interface.getMethods(), [](const InterfaceMethod &method) {
+ return method.isPureVirtual();
+ });
+
+ ios.indent(0);
+ if (hasProtectedConstructor)
+ ios << "protected:\n";
+
+ ios.indent(2);
+ ios << llvm::formatv("{0}(::mlir::Dialect *dialect) : Base(dialect) {{}\n",
+ interface.getName());
+}
+
void DialectInterfaceGenerator::emitInterfaceDecl(
const DialectInterface &interface) {
llvm::NamespaceEmitter ns(os, interface.getCppNamespace());
@@ -135,9 +161,8 @@ void DialectInterfaceGenerator::emitInterfaceDecl(
// Emit the main interface class declaration.
os << llvm::formatv(
- "class {0} : public ::mlir::DialectInterface::Base<{0}> {\n"
- "public:\n"
- " {0}(::mlir::Dialect *dialect) : Base(dialect) {{}\n",
+ "class {0} : public ::mlir::DialectInterface::Base<{0}> {{\n"
+ "public:\n",
interface.getName());
emitInterfaceMethodsDef(interface, os);
@@ -151,6 +176,10 @@ void DialectInterfaceGenerator::emitInterfaceDecl(
ios << "\n";
}
+ os << "\n";
+
+ emitConstructor(interface, os);
+
os << "};\n";
}
>From 6ddf59b36259c471f356de7cde6bf662d5a11ad0 Mon Sep 17 00:00:00 2001
From: aidint <at.aidin at gmail.com>
Date: Tue, 10 Feb 2026 21:04:03 +0100
Subject: [PATCH 2/2] rename to DialectReductionPatternInterface.td
---
mlir/include/mlir/Reducer/CMakeLists.txt | 8 ++++----
...rnInterface.td => DialectReductionPatternInterface.td} | 0
mlir/include/mlir/Reducer/ReductionPatternInterface.h | 2 +-
mlir/lib/Reducer/CMakeLists.txt | 2 +-
4 files changed, 6 insertions(+), 6 deletions(-)
rename mlir/include/mlir/Reducer/{ReductionPatternInterface.td => DialectReductionPatternInterface.td} (100%)
diff --git a/mlir/include/mlir/Reducer/CMakeLists.txt b/mlir/include/mlir/Reducer/CMakeLists.txt
index 014ff6e334513..37a19a481adc4 100644
--- a/mlir/include/mlir/Reducer/CMakeLists.txt
+++ b/mlir/include/mlir/Reducer/CMakeLists.txt
@@ -2,8 +2,8 @@ set(LLVM_TARGET_DEFINITIONS Passes.td)
mlir_tablegen(Passes.h.inc -gen-pass-decls -name Reducer)
add_mlir_generic_tablegen_target(MLIRReducerIncGen)
-add_mlir_doc(Passes ReducerPasses ./ -gen-pass-doc)
+set(LLVM_TARGET_DEFINITIONS DialectReductionPatternInterface.td)
+mlir_tablegen(DialectReductionPatternInterface.h.inc -gen-dialect-interface-decls)
+add_mlir_generic_tablegen_target(MLIRDialectReductionPatternInterfaceIncGen)
-set(LLVM_TARGET_DEFINITIONS ReductionPatternInterface.td)
-mlir_tablegen(ReductionPatternInterface.h.inc -gen-dialect-interface-decls)
-add_mlir_generic_tablegen_target(MLIRReductionPatternInterfaceGen)
+add_mlir_doc(Passes ReducerPasses ./ -gen-pass-doc)
diff --git a/mlir/include/mlir/Reducer/ReductionPatternInterface.td b/mlir/include/mlir/Reducer/DialectReductionPatternInterface.td
similarity index 100%
rename from mlir/include/mlir/Reducer/ReductionPatternInterface.td
rename to mlir/include/mlir/Reducer/DialectReductionPatternInterface.td
diff --git a/mlir/include/mlir/Reducer/ReductionPatternInterface.h b/mlir/include/mlir/Reducer/ReductionPatternInterface.h
index d34c79a3ed05e..7d7a1eeea27ac 100644
--- a/mlir/include/mlir/Reducer/ReductionPatternInterface.h
+++ b/mlir/include/mlir/Reducer/ReductionPatternInterface.h
@@ -16,6 +16,6 @@ namespace mlir {
class RewritePatternSet;
} // namespace mlir
-#include "mlir/Reducer/ReductionPatternInterface.h.inc"
+#include "mlir/Reducer/DialectReductionPatternInterface.h.inc"
#endif // MLIR_REDUCER_REDUCTIONPATTERNINTERFACE_H
diff --git a/mlir/lib/Reducer/CMakeLists.txt b/mlir/lib/Reducer/CMakeLists.txt
index c3d8c610459e7..68864e373c993 100644
--- a/mlir/lib/Reducer/CMakeLists.txt
+++ b/mlir/lib/Reducer/CMakeLists.txt
@@ -12,7 +12,7 @@ add_mlir_library(MLIRReduce
DEPENDS
MLIRReducerIncGen
- MLIRReductionPatternInterfaceGen
+ MLIRDialectReductionPatternInterfaceIncGen
)
mlir_check_all_link_libraries(MLIRReduce)
More information about the Mlir-commits
mailing list