Skip to content

Commit 98de5df

Browse files
authored
[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.
1 parent 6e498bc commit 98de5df

File tree

5 files changed

+11
-7
lines changed

5 files changed

+11
-7
lines changed

mlir/include/mlir/IR/Attributes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ inline ::llvm::hash_code hash_value(Attribute arg) {
207207
class NamedAttribute {
208208
public:
209209
NamedAttribute(StringAttr name, Attribute value);
210+
NamedAttribute(StringRef name, Attribute value);
210211

211212
/// Return the name of the attribute.
212213
StringAttr getName() const;

mlir/include/mlir/IR/OperationSupport.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,9 @@ class NamedAttrList {
819819
}
820820

821821
/// Add an attribute with the specified name.
822-
void append(StringRef name, Attribute attr);
822+
void append(StringRef name, Attribute attr) {
823+
append(NamedAttribute(name, attr));
824+
}
823825

824826
/// Add an attribute with the specified name.
825827
void append(StringAttr name, Attribute attr) {

mlir/lib/IR/Attributes.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ NamedAttribute::NamedAttribute(StringAttr name, Attribute value)
4646
assert(!name.empty() && "expected valid attribute name");
4747
}
4848

49+
NamedAttribute::NamedAttribute(StringRef name, Attribute value) : value(value) {
50+
assert(value && "expected valid attribute value");
51+
assert(!name.empty() && "expected valid attribute name");
52+
this->name = StringAttr::get(value.getContext(), name);
53+
}
54+
4955
StringAttr NamedAttribute::getName() const {
5056
return llvm::cast<StringAttr>(name);
5157
}

mlir/lib/IR/Builders.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ NoneType Builder::getNoneType() { return NoneType::get(context); }
8888
//===----------------------------------------------------------------------===//
8989

9090
NamedAttribute Builder::getNamedAttr(StringRef name, Attribute val) {
91-
return NamedAttribute(getStringAttr(name), val);
91+
return NamedAttribute(name, val);
9292
}
9393

9494
UnitAttr Builder::getUnitAttr() { return UnitAttr::get(context); }

mlir/lib/IR/OperationSupport.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ DictionaryAttr NamedAttrList::getDictionary(MLIRContext *context) const {
6262
return llvm::cast<DictionaryAttr>(dictionarySorted.getPointer());
6363
}
6464

65-
/// Add an attribute with the specified name.
66-
void NamedAttrList::append(StringRef name, Attribute attr) {
67-
append(StringAttr::get(attr.getContext(), name), attr);
68-
}
69-
7065
/// Replaces the attributes with new list of attributes.
7166
void NamedAttrList::assign(const_iterator inStart, const_iterator inEnd) {
7267
DictionaryAttr::sort(ArrayRef<NamedAttribute>{inStart, inEnd}, attrs);

0 commit comments

Comments
 (0)