[llvm-branch-commits] [mlir] [mlir][ODS] Generate hooks that register the attributes and types of a dialect (PR #243481)
Markus Böck via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Nov 18 02:57:20 PST 2026
https://github.com/zero9178 updated https://github.com/llvm/llvm-project/pull/243481
>From 2700952695699c810c4934094da16e2e1003fca3 Mon Sep 17 00:00:00 2001
From: Markus Boeck <mboeck at nvidia.com>
Date: Tue, 17 Nov 2026 11:05:02 +0000
Subject: [PATCH] [mlir][ODS] Generate hooks that register the attributes and
types of a dialect
Dialects registered their generated attributes and types by expanding
`GET_ATTRDEF_LIST` (or `GET_TYPEDEF_LIST`) into the template arguments
of `addAttributes` (or `addTypes`).
This requires each and every type and attribute and their storage classes' to be defined at the callsite and is generally boilerplate that exists in most dialects.
This PR introduces a single registration hook that is generated by ODS and should be called from dialects' `initialize` method instead. The implementation of the hook now performs the registration.
The motivation and further advantage of this approach is that we can then also change the implementation of the registration method in ODS while keeping API compatibility. This is required for partitioning of attributes and types e.g. (https://github.com/llvm/llvm-project/pull/241935).
Docs have been updated to advise users to use the new registration method. However, no upstream dialect beyond the test dialect has been switched nor is the old method of registration planned to be deprecated as it is still useful in some contexts (e.g., extendible dialects or C++ defined types and attributes).
Co-authored-by: Claude <noreply at anthropic.com>
---
.../DefiningDialects/AttributesAndTypes.md | 23 +++++++------
mlir/include/mlir/IR/AttributeSupport.h | 13 ++++++++
mlir/include/mlir/IR/Dialect.h | 2 ++
mlir/include/mlir/IR/TypeSupport.h | 13 ++++++++
mlir/lib/IR/MLIRContext.cpp | 13 ++++++++
mlir/test/lib/Dialect/Test/TestAttributes.cpp | 5 +--
mlir/test/lib/Dialect/Test/TestTypes.cpp | 6 ++--
mlir/test/mlir-tblgen/attrdefs.td | 8 +++++
mlir/test/mlir-tblgen/typedefs.td | 10 ++++++
mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp | 33 +++++++++++++++++++
10 files changed, 106 insertions(+), 20 deletions(-)
diff --git a/mlir/docs/DefiningDialects/AttributesAndTypes.md b/mlir/docs/DefiningDialects/AttributesAndTypes.md
index d0dd8a0cf2aa00..44abe29de9a916 100644
--- a/mlir/docs/DefiningDialects/AttributesAndTypes.md
+++ b/mlir/docs/DefiningDialects/AttributesAndTypes.md
@@ -1234,22 +1234,21 @@ generates an `getAlias` implementation using the Attribute or Type's mnemonic.
### Registering with the Dialect
Once the attributes and types have been defined, they must then be registered
-with the parent `Dialect`. This is done via the `addAttributes` and `addTypes`
-methods. Note that when registering, the full definition of the storage classes
-must be visible.
+with the parent `Dialect`. The generated definitions define
+`register<DialectClass>Attributes` (or `register<DialectClass>Types`), which
+registers all attributes (or types) of the generated file. Since registering
+requires the full definition of the storage classes, it is a `static` function,
+which can be called in the compilation unit that includes the definitions:
```c++
-void MyDialect::initialize() {
- /// Add the defined attributes to the dialect.
- addAttributes<
-#define GET_ATTRDEF_LIST
+#define GET_ATTRDEF_CLASSES
#include "MyDialect/Attributes.cpp.inc"
- >();
- /// Add the defined types to the dialect.
- addTypes<
-#define GET_TYPEDEF_LIST
+#define GET_TYPEDEF_CLASSES
#include "MyDialect/Types.cpp.inc"
- >();
+
+void MyDialect::initialize() {
+ registerMyDialectAttributes(this);
+ registerMyDialectTypes(this);
}
```
diff --git a/mlir/include/mlir/IR/AttributeSupport.h b/mlir/include/mlir/IR/AttributeSupport.h
index 9f36ee4aae2783..45ef62198fafeb 100644
--- a/mlir/include/mlir/IR/AttributeSupport.h
+++ b/mlir/include/mlir/IR/AttributeSupport.h
@@ -53,6 +53,10 @@ class AbstractAttribute {
T::getTypeID(), T::name);
}
+ /// Register the attribute `T` with `dialect`.
+ template <typename T>
+ static void insert(Dialect &dialect);
+
/// This method is used by Dialect objects to register attributes with
/// custom TypeIDs.
/// The use of this method is in general discouraged in favor of
@@ -111,6 +115,10 @@ class AbstractAttribute {
StringRef getName() const { return name; }
private:
+ /// Registers `attrInfo` with `dialect` and returns the context of `dialect`.
+ /// This is the part of `insert<T>` that requires the definition of `Dialect`.
+ static MLIRContext *insert(Dialect &dialect, AbstractAttribute &&attrInfo);
+
AbstractAttribute(Dialect &dialect, detail::InterfaceMap &&interfaceMap,
HasTraitFn &&hasTraitFn,
WalkImmediateSubElementsFn walkImmediateSubElementsFn,
@@ -322,6 +330,11 @@ decltype(auto) unwrapForCustomParse(FailureOr<T> &failureOr) {
} // namespace detail
+template <typename T>
+void AbstractAttribute::insert(Dialect &dialect) {
+ MLIRContext *context = insert(dialect, get<T>(dialect));
+ detail::AttributeUniquer::registerAttribute<T>(context);
+}
} // namespace mlir
#endif
diff --git a/mlir/include/mlir/IR/Dialect.h b/mlir/include/mlir/IR/Dialect.h
index fb24a6895dabdc..8e0ecc9d379625 100644
--- a/mlir/include/mlir/IR/Dialect.h
+++ b/mlir/include/mlir/IR/Dialect.h
@@ -367,6 +367,8 @@ class Dialect {
/// to provide an implementation for.
DenseSet<std::pair<TypeID, TypeID>> unresolvedPromisedInterfaces;
+ friend class AbstractAttribute;
+ friend class AbstractType;
friend class DialectRegistry;
friend class MLIRContext;
};
diff --git a/mlir/include/mlir/IR/TypeSupport.h b/mlir/include/mlir/IR/TypeSupport.h
index 36ef696ba4f6fb..084eb5c81840b9 100644
--- a/mlir/include/mlir/IR/TypeSupport.h
+++ b/mlir/include/mlir/IR/TypeSupport.h
@@ -54,6 +54,10 @@ class AbstractType {
T::name);
}
+ /// Register the type `T` with `dialect`.
+ template <typename T>
+ static void insert(Dialect &dialect);
+
/// This method is used by Dialect objects to register types with
/// custom TypeIDs.
/// The use of this method is in general discouraged in favor of
@@ -110,6 +114,10 @@ class AbstractType {
StringRef getName() const { return name; }
private:
+ /// Registers `typeInfo` with `dialect` and returns the context of `dialect`.
+ /// This is the part of `insert<T>` that requires the definition of `Dialect`.
+ static MLIRContext *insert(Dialect &dialect, AbstractType &&typeInfo);
+
AbstractType(Dialect &dialect, detail::InterfaceMap &&interfaceMap,
HasTraitFn &&hasTrait,
WalkImmediateSubElementsFn walkImmediateSubElementsFn,
@@ -295,6 +303,11 @@ struct TypeUniquer {
};
} // namespace detail
+template <typename T>
+void AbstractType::insert(Dialect &dialect) {
+ MLIRContext *context = insert(dialect, get<T>(dialect));
+ detail::TypeUniquer::registerType<T>(context);
+}
} // namespace mlir
#endif
diff --git a/mlir/lib/IR/MLIRContext.cpp b/mlir/lib/IR/MLIRContext.cpp
index d8abff14be05f2..d4169aba5f6f59 100644
--- a/mlir/lib/IR/MLIRContext.cpp
+++ b/mlir/lib/IR/MLIRContext.cpp
@@ -889,6 +889,19 @@ void Dialect::addAttribute(TypeID typeID, AbstractAttribute &&attrInfo) {
newInfo->getName() + " is already registered.");
}
+MLIRContext *AbstractAttribute::insert(Dialect &dialect,
+ AbstractAttribute &&attrInfo) {
+ TypeID typeID = attrInfo.getTypeID();
+ dialect.addAttribute(typeID, std::move(attrInfo));
+ return dialect.getContext();
+}
+
+MLIRContext *AbstractType::insert(Dialect &dialect, AbstractType &&typeInfo) {
+ TypeID typeID = typeInfo.getTypeID();
+ dialect.addType(typeID, std::move(typeInfo));
+ return dialect.getContext();
+}
+
//===----------------------------------------------------------------------===//
// AbstractAttribute
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/lib/Dialect/Test/TestAttributes.cpp b/mlir/test/lib/Dialect/Test/TestAttributes.cpp
index 14d89ed5edc554..d86a32af6c39a7 100644
--- a/mlir/test/lib/Dialect/Test/TestAttributes.cpp
+++ b/mlir/test/lib/Dialect/Test/TestAttributes.cpp
@@ -604,10 +604,7 @@ mlir::AffineMap TestMemRefLayoutAttr::getAffineMap() const {
//===----------------------------------------------------------------------===//
void TestDialect::registerAttributes() {
- addAttributes<
-#define GET_ATTRDEF_LIST
-#include "TestAttrDefs.cpp.inc"
- >();
+ registerTestDialectAttributes(this);
registerDynamicAttr(getDynamicSingletonAttr(this));
registerDynamicAttr(getDynamicPairAttr(this));
registerDynamicAttr(getDynamicCustomAssemblyFormatAttr(this));
diff --git a/mlir/test/lib/Dialect/Test/TestTypes.cpp b/mlir/test/lib/Dialect/Test/TestTypes.cpp
index b290fd06e6a72b..9410d5e2aa9226 100644
--- a/mlir/test/lib/Dialect/Test/TestTypes.cpp
+++ b/mlir/test/lib/Dialect/Test/TestTypes.cpp
@@ -417,10 +417,8 @@ struct PtrElementModel
} // namespace
void TestDialect::registerTypes() {
- addTypes<TestRecursiveType,
-#define GET_TYPEDEF_LIST
-#include "TestTypeDefs.cpp.inc"
- >();
+ addTypes<TestRecursiveType>();
+ registerTestDialectTypes(this);
SimpleAType::attachInterface<PtrElementModel>(*getContext());
registerDynamicType(getSingletonDynamicType(this));
diff --git a/mlir/test/mlir-tblgen/attrdefs.td b/mlir/test/mlir-tblgen/attrdefs.td
index 046bb53858be05..69baa712b5bafc 100644
--- a/mlir/test/mlir-tblgen/attrdefs.td
+++ b/mlir/test/mlir-tblgen/attrdefs.td
@@ -204,3 +204,11 @@ def J_CustomStorageCtorAttr : AttrDef<Test_Dialect, "CustomStorageCtorAttr"> {
// DEF-LABEL: struct CustomStorageCtorAttrAttrStorage : public ::mlir::AttributeStorage
// DEF: static CustomStorageCtorAttrAttrStorage *construct
// DEF-SAME: (::mlir::AttributeStorageAllocator &allocator, KeyTy &&tblgenKey);
+
+// The registration hook registers every attribute.
+// DEF: class TestDialect;
+// DEF-NEXT: {{\[\[}}maybe_unused{{\]\]}} static void registerTestDialectAttributes(TestDialect *dialect) {
+// DEF-NEXT: ::mlir::AbstractAttribute::insert<IndexAttr>(*dialect);
+// DEF-NEXT: ::mlir::AbstractAttribute::insert<SimpleAAttr>(*dialect);
+// DEF: ::mlir::AbstractAttribute::insert<CustomStorageCtorAttrAttr>(*dialect);
+// DEF-NEXT: }
diff --git a/mlir/test/mlir-tblgen/typedefs.td b/mlir/test/mlir-tblgen/typedefs.td
index b9e3a7954e361d..30bded77d24085 100644
--- a/mlir/test/mlir-tblgen/typedefs.td
+++ b/mlir/test/mlir-tblgen/typedefs.td
@@ -153,3 +153,13 @@ def E_IntegerType : TestType<"Integer"> {
// DECL-NEXT: /// Return true if this is an unsigned integer type.
// DECL-NEXT: bool isUnsigned() const { return getSignedness() == Unsigned; }
}
+
+// The registration hook registers every type.
+// DEF: class TestDialect;
+// DEF-NEXT: {{\[\[}}maybe_unused{{\]\]}} static void registerTestDialectTypes(TestDialect *dialect) {
+// DEF-NEXT: ::mlir::AbstractType::insert<SimpleAType>(*dialect);
+// DEF-NEXT: ::mlir::AbstractType::insert<CompoundAType>(*dialect);
+// DEF-NEXT: ::mlir::AbstractType::insert<IndexType>(*dialect);
+// DEF-NEXT: ::mlir::AbstractType::insert<SingleParameterType>(*dialect);
+// DEF-NEXT: ::mlir::AbstractType::insert<IntegerType>(*dialect);
+// DEF-NEXT: }
diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index 790bc393b08f48..97f95a1c0aafdc 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -867,6 +867,12 @@ class DefGenerator {
/// Emit the code to dispatch between different defs during parsing/printing.
void emitParsePrintDispatch(ArrayRef<AttrOrTypeDef> defs);
+ /// Returns the name of the function that registers the defs of `dialect`.
+ std::string getRegistrationHookName(const Dialect &dialect) const;
+
+ /// Emit the definition of the hook that registers `defs` with their dialect.
+ void emitRegistrationHook(ArrayRef<AttrOrTypeDef> defs);
+
/// The set of def records to emit.
std::vector<const Record *> defRecords;
/// The attribute or type class to emit.
@@ -1193,6 +1199,32 @@ void DefGenerator::emitParsePrintDispatch(ArrayRef<AttrOrTypeDef> defs) {
printer.writeDeclTo(indentedOs);
}
+std::string
+DefGenerator::getRegistrationHookName(const Dialect &dialect) const {
+ return strfmt("register{0}{1}s", dialect.getCppClassName(), valueType);
+}
+
+/// Emit the hook that registers the defs with their dialect. Since registering
+/// requires the definitions of the storage classes, the hook is a static
+/// function.
+void DefGenerator::emitRegistrationHook(ArrayRef<AttrOrTypeDef> defs) {
+ Dialect dialect = defs.front().getDialect();
+ std::string dialectClass = dialect.getCppClassName();
+ DialectNamespaceEmitter ns(os, dialect);
+ // TODO: Ideally, the hook would be non-static, so that callers do not have to
+ // be in the same TU as the registration function. This is currently blocked
+ // by dialects defining their attributes and types across multiple TUs (e.g.
+ // the transform dialect and its extensions), whose hooks would cause
+ // multiple definition errors.
+ os << "class " << dialectClass << ";\n"
+ << llvm::formatv("[[maybe_unused]] static void {0}({1} *dialect) {{\n",
+ getRegistrationHookName(dialect), dialectClass);
+ for (const AttrOrTypeDef &def : defs)
+ os << llvm::formatv(" ::mlir::Abstract{0}::insert<{1}>(*dialect);\n",
+ valueType, def.getCppClassName());
+ os << "}\n";
+}
+
/// Headers declaring the C++ constructs used by every generated attribute or
/// type definition, beyond those already included by the declarations.
static constexpr StringRef attrOrTypeDefIncludes[] = {
@@ -1232,6 +1264,7 @@ bool DefGenerator::emitDefs(StringRef selectedDialect) {
<< def.getDialect().getCppNamespace() << "::" << def.getCppClassName()
<< ")\n";
}
+ emitRegistrationHook(defs);
Dialect firstDialect = defs.front().getDialect();
More information about the llvm-branch-commits
mailing list