[Mlir-commits] [mlir] bc83777 - [mlir][ODS] Share generated attribute property conversion (NFC) (#226057)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Thu Sep 24 01:24:51 PDT 2026
Author: Mehdi Amini
Date: 2026-09-24T08:24:44Z
New Revision: bc83777c2e2c3d027cd12e7715a138d12921c98b
URL: https://github.com/llvm/llvm-project/commit/bc83777c2e2c3d027cd12e7715a138d12921c98b
DIFF: https://github.com/llvm/llvm-project/commit/bc83777c2e2c3d027cd12e7715a138d12921c98b.diff
LOG: [mlir][ODS] Share generated attribute property conversion (NFC) (#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
Added:
Modified:
mlir/include/mlir/IR/OperationSupport.h
mlir/lib/IR/OperationSupport.cpp
mlir/test/mlir-tblgen/op-decl-and-defs.td
mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
Removed:
################################################################################
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 f9cb5695b982e..71fd83d034d3f 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 2fddead48132c..475336506993a 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