Skip to content

Commit ef1b735

Browse files
committed
[MLIR][python bindings] Add support for DenseElementsAttr of IndexType
Differential Revision: https://reviews.llvm.org/D149690
1 parent 218b50a commit ef1b735

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

mlir/lib/Bindings/Python/IRAttributes.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,10 @@ class PyDenseElementsAttribute
710710
// f16
711711
return bufferInfo<uint16_t>(shapedType, "e");
712712
}
713+
if (mlirTypeIsAIndex(elementType)) {
714+
// Same as IndexType::kInternalStorageBitWidth
715+
return bufferInfo<int64_t>(shapedType);
716+
}
713717
if (mlirTypeIsAInteger(elementType) &&
714718
mlirIntegerTypeGetWidth(elementType) == 32) {
715719
if (mlirIntegerTypeIsSignless(elementType) ||

mlir/test/python/ir/array_attributes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,20 @@ def testGetDenseElementsUI64():
365365
# CHECK: {{\[}}4 5 6]]
366366
print(np.array(attr))
367367

368+
369+
# CHECK-LABEL: TEST: testGetDenseElementsIndex
370+
@run
371+
def testGetDenseElementsIndex():
372+
with Context(), Location.unknown():
373+
idx_type = IndexType.get()
374+
array = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.int64)
375+
attr = DenseElementsAttr.get(array, type=idx_type)
376+
# CHECK: dense<{{\[}}[1, 2, 3], [4, 5, 6]]> : tensor<2x3xindex>
377+
print(attr)
378+
arr = np.array(attr)
379+
# CHECK: {{\[}}[1 2 3]
380+
# CHECK: {{\[}}4 5 6]]
381+
print(arr)
382+
# CHECK: True
383+
print(arr.dtype == np.int64)
384+

0 commit comments

Comments
 (0)