[Mlir-commits] [mlir] 98de5df - [mlir] Add NamedAttribute ctor taking StringRef. NFC. (#123974)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Jan 22 16:02:21 PST 2025


Author: Jakub Kuderski
Date: 2025-01-22T19:02:17-05:00
New Revision: 98de5dfe6a8cbb70f21de545acec4710a77294ed

URL: https://github.com/llvm/llvm-project/commit/98de5dfe6a8cbb70f21de545acec4710a77294ed
DIFF: https://github.com/llvm/llvm-project/commit/98de5dfe6a8cbb70f21de545acec4710a77294ed.diff

LOG: [mlir] Add NamedAttribute ctor taking StringRef. NFC. (#123974)

This is a small QoL improvement so that we don't have to go through
helpers when building `NamedAttribute`s.

Added: 
    

Modified: 
    mlir/include/mlir/IR/Attributes.h
    mlir/include/mlir/IR/OperationSupport.h
    mlir/lib/IR/Attributes.cpp
    mlir/lib/IR/Builders.cpp
    mlir/lib/IR/OperationSupport.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/Attributes.h b/mlir/include/mlir/IR/Attributes.h
index d347013295d5fc..262d31b20ab084 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 5eb2d69134ea5f..d4035d14ab7465 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 cc7a2a5e586b1c..ff1cd8432fb07d 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<StringAttr>(name);
 }

diff  --git a/mlir/lib/IR/Builders.cpp b/mlir/lib/IR/Builders.cpp
index d57a7ca07ede58..16bd8201ad50a6 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 957195202d78d2..1b2cda19de1e80 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<DictionaryAttr>(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<NamedAttribute>{inStart, inEnd}, attrs);


        


More information about the Mlir-commits mailing list