Skip to content

Commit c16f331

Browse files
author
git apple-llvm automerger
committed
Merge commit '0d5caa894017' from llvm.org/master into apple/master
2 parents c84cba1 + 0d5caa8 commit c16f331

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

mlir/docs/LangRef.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,9 +1334,10 @@ dense-elements-attribute ::= `dense` `<` attribute-value `>` `:`
13341334
```
13351335

13361336
A dense elements attribute is an elements attribute where the storage for the
1337-
constant vector or tensor value has been packed to the element bitwidth. The
1338-
element type of the vector or tensor constant must be of integer, index, or
1339-
floating point type.
1337+
constant vector or tensor value has been densely packed. The attribute supports
1338+
storing integer or floating point elements, with integer/index/floating element
1339+
types. It also support storing string elements with a custom dialect string
1340+
element type.
13401341

13411342
##### Opaque Elements Attribute
13421343

mlir/include/mlir/IR/Attributes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,8 @@ class DenseElementsAttr : public ElementsAttr {
703703

704704
/// Constructs a dense elements attribute from an array of element values.
705705
/// Each element attribute value is expected to be an element of 'type'.
706-
/// 'type' must be a vector or tensor with static shape.
706+
/// 'type' must be a vector or tensor with static shape. If the element of
707+
/// `type` is non-integer/index/float it is assumed to be a string type.
707708
static DenseElementsAttr get(ShapedType type, ArrayRef<Attribute> values);
708709

709710
/// Constructs a dense integer elements attribute from an array of integer

mlir/lib/IR/Attributes.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,8 @@ Attribute DenseElementsAttr::AttributeElementIterator::operator*() const {
615615
FloatElementIterator floatIt(floatEltTy.getFloatSemantics(), intIt);
616616
return FloatAttr::get(eltTy, *floatIt);
617617
}
618+
if (owner.isa<DenseStringElementsAttr>())
619+
return StringAttr::get(owner.getRawStringData()[index], eltTy);
618620
llvm_unreachable("unexpected element type");
619621
}
620622

@@ -655,11 +657,23 @@ DenseElementsAttr::FloatElementIterator::FloatElementIterator(
655657

656658
DenseElementsAttr DenseElementsAttr::get(ShapedType type,
657659
ArrayRef<Attribute> values) {
658-
assert(type.getElementType().isIntOrIndexOrFloat() &&
659-
"expected int or index or float element type");
660660
assert(hasSameElementsOrSplat(type, values));
661661

662+
// If the element type is not based on int/float/index, assume it is a string
663+
// type.
662664
auto eltType = type.getElementType();
665+
if (!type.getElementType().isIntOrIndexOrFloat()) {
666+
SmallVector<StringRef, 8> stringValues;
667+
stringValues.reserve(values.size());
668+
for (Attribute attr : values) {
669+
assert(attr.isa<StringAttr>() &&
670+
"expected string value for non integer/index/float element");
671+
stringValues.push_back(attr.cast<StringAttr>().getValue());
672+
}
673+
return get(type, stringValues);
674+
}
675+
676+
// Otherwise, get the raw storage width to use for the allocation.
663677
size_t bitWidth = getDenseElementBitWidth(eltType);
664678
size_t storageBitWidth = getDenseElementStorageWidth(bitWidth);
665679

mlir/unittests/IR/AttributeTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,12 @@ TEST(DenseSplatTest, StringSplat) {
154154
testSplat(stringType, value);
155155
}
156156

157+
TEST(DenseSplatTest, StringAttrSplat) {
158+
MLIRContext context;
159+
Type stringType =
160+
OpaqueType::get(Identifier::get("test", &context), "string", &context);
161+
Attribute stringAttr = StringAttr::get("test-string", stringType);
162+
testSplat(stringType, stringAttr);
163+
}
164+
157165
} // end namespace

0 commit comments

Comments
 (0)