Skip to content

Commit

Permalink
Merge commit 'eb28bbcb0230' into matthias.bump_282d50147628
Browse files Browse the repository at this point in the history
  • Loading branch information
mgehre-amd committed Mar 11, 2024
2 parents b2d8371 + eb28bbc commit 245b7f0
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions mlir/include/mlir-c/BuiltinAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ MLIR_CAPI_EXPORTED int64_t
mlirDenseElementsAttrGetInt64Value(MlirAttribute attr, intptr_t pos);
MLIR_CAPI_EXPORTED uint64_t
mlirDenseElementsAttrGetUInt64Value(MlirAttribute attr, intptr_t pos);
MLIR_CAPI_EXPORTED size_t mlirDenseElementsAttrGetIndexValue(MlirAttribute attr,
intptr_t pos);
MLIR_CAPI_EXPORTED float mlirDenseElementsAttrGetFloatValue(MlirAttribute attr,
intptr_t pos);
MLIR_CAPI_EXPORTED double
Expand Down
10 changes: 8 additions & 2 deletions mlir/lib/Bindings/Python/IRAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,13 +973,19 @@ class PyDenseIntElementsAttribute

MlirType type = mlirAttributeGetType(*this);
type = mlirShapedTypeGetElementType(type);
assert(mlirTypeIsAInteger(type) &&
"expected integer element type in dense int elements attribute");
// Index type can also appear as a DenseIntElementsAttr and therefore can be
// casted to integer.
assert(mlirTypeIsAInteger(type) ||
mlirTypeIsAIndex(type) && "expected integer/index element type in "
"dense int elements attribute");
// Dispatch element extraction to an appropriate C function based on the
// elemental type of the attribute. py::int_ is implicitly constructible
// from any C++ integral type and handles bitwidth correctly.
// TODO: consider caching the type properties in the constructor to avoid
// querying them on each element access.
if (mlirTypeIsAIndex(type)) {
return mlirDenseElementsAttrGetIndexValue(*this, pos);
}
unsigned width = mlirIntegerTypeGetWidth(type);
bool isUnsigned = mlirIntegerTypeIsUnsigned(type);
if (isUnsigned) {
Expand Down
3 changes: 3 additions & 0 deletions mlir/lib/CAPI/IR/BuiltinAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,9 @@ int64_t mlirDenseElementsAttrGetInt64Value(MlirAttribute attr, intptr_t pos) {
uint64_t mlirDenseElementsAttrGetUInt64Value(MlirAttribute attr, intptr_t pos) {
return llvm::cast<DenseElementsAttr>(unwrap(attr)).getValues<uint64_t>()[pos];
}
size_t mlirDenseElementsAttrGetIndexValue(MlirAttribute attr, intptr_t pos) {
return llvm::cast<DenseElementsAttr>(unwrap(attr)).getValues<size_t>()[pos];
}
float mlirDenseElementsAttrGetFloatValue(MlirAttribute attr, intptr_t pos) {
return llvm::cast<DenseElementsAttr>(unwrap(attr)).getValues<float>()[pos];
}
Expand Down
4 changes: 4 additions & 0 deletions mlir/test/python/dialects/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,7 @@ def testDenseElementsAttr():
# CHECK{LITERAL}: dense<[[0, 1], [2, 3]]> : tensor<2x2xi32>
print(DenseElementsAttr.get(values, type=VectorType.get((2, 2), i32)))
# CHECK{LITERAL}: dense<[[0, 1], [2, 3]]> : vector<2x2xi32>
idx_values = np.arange(4, dtype=np.int64)
idx_type = IndexType.get()
print(DenseElementsAttr.get(idx_values, type=VectorType.get([4], idx_type)))
# CHECK{LITERAL}: dense<[0, 1, 2, 3]> : vector<4xindex>
4 changes: 4 additions & 0 deletions mlir/test/python/ir/array_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@ def testGetDenseElementsIndex():
print(arr)
# CHECK: True
print(arr.dtype == np.int64)
array = np.array([1, 2, 3], dtype=np.int64)
attr = DenseIntElementsAttr.get(array, type=VectorType.get([3], idx_type))
# CHECK: [1, 2, 3]
print(list(DenseIntElementsAttr(attr)))


# CHECK-LABEL: TEST: testGetDenseResourceElementsAttr
Expand Down
4 changes: 4 additions & 0 deletions mlir/test/python/ir/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ def testDenseIntAttr():
# CHECK: i1
print(ShapedType(a.type).element_type)

shape = Attribute.parse("dense<[0, 1, 2, 3]> : vector<4xindex>")
# CHECK: attr: dense<[0, 1, 2, 3]>
print("attr:", shape)


@run
def testDenseArrayGetItem():
Expand Down

0 comments on commit 245b7f0

Please sign in to comment.