[Mlir-commits] [mlir] [mlir][ODS] Share generated attribute property conversion (NFC) (PR #226057)
Mehdi Amini
llvmlistbot at llvm.org
Thu Sep 24 01:00:37 PDT 2026
https://github.com/joker-eph created https://github.com/llvm/llvm-project/pull/226057
Move repeated attribute-backed property assignment and dictionary entry construction into common helpers. This reduces MLIR build time by saving optimizer work in large generated dialect translation units while preserving diagnostics and conversion behavior.
Assisted-by: Codex
>From e9eb47e2d9350b0914e1337bae28460b60421cb4 Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Wed, 23 Sep 2026 08:28:47 -0700
Subject: [PATCH] [mlir][ODS] Share generated attribute property conversion
Move repeated attribute-backed property assignment and dictionary
entry construction into common helpers. This reduces optimizer work in
large generated dialect translation units while preserving diagnostics
and conversion behavior.
Assisted-by: Codex
(cherry picked from commit 57035fa4f3a4e86a3017167da18ccaf6ed7cf054)
---
mlir/include/mlir/IR/OperationSupport.h | 25 ++++++++++++++++++++
mlir/lib/IR/OperationSupport.cpp | 7 ++++++
mlir/test/mlir-tblgen/op-decl-and-defs.td | 4 ++++
mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp | 26 ++++++---------------
4 files changed, 43 insertions(+), 19 deletions(-)
diff --git a/mlir/include/mlir/IR/OperationSupport.h b/mlir/include/mlir/IR/OperationSupport.h
index 54beafdeab27e..0d94433ef42fc 100644
--- a/mlir/include/mlir/IR/OperationSupport.h
+++ b/mlir/include/mlir/IR/OperationSupport.h
@@ -27,6 +27,8 @@
#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/STLFunctionalExtras.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/PointerLikeTypeTraits.h"
#include "llvm/Support/TrailingObjects.h"
@@ -62,6 +64,29 @@ class ValueRange;
template <typename ValueRangeT>
class ValueTypeRange;
+namespace detail {
+/// Append a present attribute-backed property to a dictionary's attributes.
+void appendAttributeProperty(llvm::SmallVectorImpl<NamedAttribute> &attrs,
+ StringRef name, Attribute attr);
+
+/// Assign a generated attribute-backed property after checking its type.
+/// Keep the conversion out of each operation's generated property setter.
+template <typename AttrT>
+LLVM_ATTRIBUTE_NOINLINE LogicalResult
+setAttributeProperty(AttrT &storage, Attribute attr, StringRef name,
+ llvm::function_ref<InFlightDiagnostic()> emitError) {
+ if (!attr)
+ return success();
+ if (auto converted = llvm::dyn_cast<AttrT>(attr)) {
+ storage = converted;
+ return success();
+ }
+ emitError() << "Invalid attribute `" << name
+ << "` in property conversion: " << attr;
+ return failure();
+}
+} // namespace detail
+
//===----------------------------------------------------------------------===//
// PropertyRef
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/IR/OperationSupport.cpp b/mlir/lib/IR/OperationSupport.cpp
index 4ecb73398aa77..83f9ae3be139f 100644
--- a/mlir/lib/IR/OperationSupport.cpp
+++ b/mlir/lib/IR/OperationSupport.cpp
@@ -21,6 +21,13 @@
using namespace mlir;
+void mlir::detail::appendAttributeProperty(
+ llvm::SmallVectorImpl<NamedAttribute> &attrs, StringRef name,
+ Attribute attr) {
+ if (attr)
+ attrs.emplace_back(name, attr);
+}
+
//===----------------------------------------------------------------------===//
// NamedAttrList
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/mlir-tblgen/op-decl-and-defs.td b/mlir/test/mlir-tblgen/op-decl-and-defs.td
index 32c4bb88d188b..088c15df4c509 100644
--- a/mlir/test/mlir-tblgen/op-decl-and-defs.td
+++ b/mlir/test/mlir-tblgen/op-decl-and-defs.td
@@ -158,6 +158,10 @@ def NS_AOp : NS_Op<"a_op", [IsolatedFromAbove, IsolatedFromAbove]> {
// CHECK: };
// DEFS-LABEL: NS::AOp definitions
+// DEFS: ::llvm::LogicalResult AOp::setPropertiesFromAttr
+// DEFS: ::mlir::detail::setAttributeProperty(
+// DEFS: ::mlir::Attribute AOp::getPropertiesAsAttr
+// DEFS: ::mlir::detail::appendAttributeProperty(attrs, "attr1", prop.attr1);
// DEFS: void AOp::build(::mlir::OpBuilder &, ::mlir::OperationState &odsState, ::mlir::TypeRange resultTypes, ::mlir::ValueRange operands, ::llvm::ArrayRef<::mlir::NamedAttribute> attributes, unsigned numRegions) {
// DEFS: buildPropertiesAndDiscardableAttributes(odsState, attributes);
// DEFS: void AOp::build(::mlir::OpBuilder &, ::mlir::OperationState &odsState, ::mlir::TypeRange resultTypes, ::mlir::ValueRange operands, const Properties &properties, ::llvm::ArrayRef<::mlir::NamedAttribute> discardableAttributes, unsigned numRegions) {
diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index aa9ff159903bc..a6887ff57a303 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -1399,17 +1399,10 @@ void OpEmitter::genPropertiesSupport() {
setPropMethod << formatv(R"decl(
{{
- auto &propStorage = prop.{0};
{1}
- if (attr) {{
- auto convertedAttr = ::llvm::dyn_cast<std::remove_reference_t<decltype(propStorage)>>(attr);
- if (convertedAttr) {{
- propStorage = convertedAttr;
- } else {{
- emitError() << "Invalid attribute `{0}` in property conversion: " << attr;
- return ::mlir::failure();
- }
- }
+ if (::mlir::failed(::mlir::detail::setAttributeProperty(
+ prop.{0}, attr, "{0}", emitError)))
+ return ::mlir::failure();
}
)decl",
name, getAttr);
@@ -1446,15 +1439,10 @@ void OpEmitter::genPropertiesSupport() {
const auto *namedAttr =
llvm::dyn_cast_if_present<const AttributeMetadata *>(attrOrProp);
StringRef name = namedAttr->attrName;
- getPropMethod << formatv(R"decl(
- {{
- const auto &propStorage = prop.{0};
- if (propStorage)
- attrs.push_back(odsBuilder.getNamedAttr("{0}",
- propStorage));
- }
-)decl",
- name);
+ getPropMethod << formatv(
+ " ::mlir::detail::appendAttributeProperty(attrs, \"{0}\", "
+ "prop.{0});\n",
+ name);
}
getPropMethod << R"decl(
if (!attrs.empty())
More information about the Mlir-commits
mailing list