Skip to content

Conversation

@kuhar
Copy link
Member

@kuhar kuhar commented Jan 22, 2025

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

This is a small QoL improvement so that we don't have to go through
helpers when building `NamedAttribute`s.
@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Jan 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 22, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: Jakub Kuderski (kuhar)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/123974.diff

5 Files Affected:

  • (modified) mlir/include/mlir/IR/Attributes.h (+1)
  • (modified) mlir/include/mlir/IR/OperationSupport.h (+3-1)
  • (modified) mlir/lib/IR/Attributes.cpp (+6)
  • (modified) mlir/lib/IR/Builders.cpp (+1-1)
  • (modified) mlir/lib/IR/OperationSupport.cpp (-5)
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);

@kuhar kuhar merged commit 98de5df into llvm:main Jan 23, 2025
11 checks passed
kuhar added a commit to kuhar/iree that referenced this pull request Jan 25, 2025
With llvm/llvm-project#123974, we no longer have
to construct a StringAttr for the name and can simplify a bunch of code.

Also avoid some needless memory allocations and needless small vectors
in related code.
kuhar added a commit to iree-org/iree that referenced this pull request Jan 25, 2025
With llvm/llvm-project#123974, we no longer have
to construct a StringAttr for the name and can simplify a bunch of code.

Also avoid some needless memory allocations and needless small vectors
in related code.
ita9naiwa pushed a commit to ita9naiwa/iree that referenced this pull request Feb 4, 2025
With llvm/llvm-project#123974, we no longer have
to construct a StringAttr for the name and can simplify a bunch of code.

Also avoid some needless memory allocations and needless small vectors
in related code.

Signed-off-by: Hyunsung Lee <[email protected]>
AWoloszyn pushed a commit to iree-org/iree that referenced this pull request Dec 1, 2025
With llvm/llvm-project#123974, we no longer have
to construct a StringAttr for the name and can simplify a bunch of code.

Also avoid some needless memory allocations and needless small vectors
in related code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mlir:core MLIR Core Infrastructure mlir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants