From 0007cffc19afc13de7cfe7faa74e4c42c10bb9e4 Mon Sep 17 00:00:00 2001 From: Jakub Kuderski Date: Wed, 22 Jan 2025 12:48:18 -0500 Subject: [PATCH] [mlir] Add NamedAttribute ctor taking StringRef This is a small QoL improvement so that we don't have to go through helpers when building `NamedAttribute`s. --- mlir/include/mlir/IR/Attributes.h | 1 + mlir/include/mlir/IR/OperationSupport.h | 4 +++- mlir/lib/IR/Attributes.cpp | 6 ++++++ mlir/lib/IR/Builders.cpp | 2 +- mlir/lib/IR/OperationSupport.cpp | 5 ----- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/mlir/include/mlir/IR/Attributes.h b/mlir/include/mlir/IR/Attributes.h index d347013295d5f..262d31b20ab08 100644 --- a/mlir/include/mlir/IR/Attributes.h +++ b/mlir/include/mlir/IR/Attributes.h @@ -207,6 +207,7 @@ inline ::llvm::hash_code hash_value(Attribute arg) { class NamedAttribute { public: NamedAttribute(StringAttr name, Attribute value); + NamedAttribute(StringRef name, Attribute value); /// Return the name of the attribute. StringAttr getName() const; diff --git a/mlir/include/mlir/IR/OperationSupport.h b/mlir/include/mlir/IR/OperationSupport.h index 5eb2d69134ea5..d4035d14ab746 100644 --- a/mlir/include/mlir/IR/OperationSupport.h +++ b/mlir/include/mlir/IR/OperationSupport.h @@ -819,7 +819,9 @@ class NamedAttrList { } /// Add an attribute with the specified name. - void append(StringRef name, Attribute attr); + void append(StringRef name, Attribute attr) { + append(NamedAttribute(name, attr)); + } /// Add an attribute with the specified name. void append(StringAttr name, Attribute attr) { diff --git a/mlir/lib/IR/Attributes.cpp b/mlir/lib/IR/Attributes.cpp index cc7a2a5e586b1..ff1cd8432fb07 100644 --- a/mlir/lib/IR/Attributes.cpp +++ b/mlir/lib/IR/Attributes.cpp @@ -46,6 +46,12 @@ NamedAttribute::NamedAttribute(StringAttr name, Attribute value) assert(!name.empty() && "expected valid attribute name"); } +NamedAttribute::NamedAttribute(StringRef name, Attribute value) : value(value) { + assert(value && "expected valid attribute value"); + assert(!name.empty() && "expected valid attribute name"); + this->name = StringAttr::get(value.getContext(), name); +} + StringAttr NamedAttribute::getName() const { return llvm::cast(name); } diff --git a/mlir/lib/IR/Builders.cpp b/mlir/lib/IR/Builders.cpp index d57a7ca07ede5..16bd8201ad50a 100644 --- a/mlir/lib/IR/Builders.cpp +++ b/mlir/lib/IR/Builders.cpp @@ -88,7 +88,7 @@ NoneType Builder::getNoneType() { return NoneType::get(context); } //===----------------------------------------------------------------------===// NamedAttribute Builder::getNamedAttr(StringRef name, Attribute val) { - return NamedAttribute(getStringAttr(name), val); + return NamedAttribute(name, val); } UnitAttr Builder::getUnitAttr() { return UnitAttr::get(context); } diff --git a/mlir/lib/IR/OperationSupport.cpp b/mlir/lib/IR/OperationSupport.cpp index 957195202d78d..1b2cda19de1e8 100644 --- a/mlir/lib/IR/OperationSupport.cpp +++ b/mlir/lib/IR/OperationSupport.cpp @@ -62,11 +62,6 @@ DictionaryAttr NamedAttrList::getDictionary(MLIRContext *context) const { return llvm::cast(dictionarySorted.getPointer()); } -/// Add an attribute with the specified name. -void NamedAttrList::append(StringRef name, Attribute attr) { - append(StringAttr::get(attr.getContext(), name), attr); -} - /// Replaces the attributes with new list of attributes. void NamedAttrList::assign(const_iterator inStart, const_iterator inEnd) { DictionaryAttr::sort(ArrayRef{inStart, inEnd}, attrs);