Skip to content

Commit

Permalink
[mlir] Add fully dynamic constructor to StridedLayoutAttr bindings
Browse files Browse the repository at this point in the history
Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D135139
  • Loading branch information
shabalind committed Oct 4, 2022
1 parent ddff376 commit e3fd612
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions mlir/lib/Bindings/Python/IRAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,19 @@ class PyStridedLayoutAttribute
},
py::arg("offset"), py::arg("strides"), py::arg("context") = py::none(),
"Gets a strided layout attribute.");
c.def_static(
"get_fully_dynamic",
[](int64_t rank, DefaultingPyMlirContext ctx) {
auto dynamic = mlirShapedTypeGetDynamicStrideOrOffset();
std::vector<int64_t> strides(rank);
std::fill(strides.begin(), strides.end(), dynamic);
MlirAttribute attr = mlirStridedLayoutAttrGet(
ctx->get(), dynamic, strides.size(), strides.data());
return PyStridedLayoutAttribute(ctx->getRef(), attr);
},
py::arg("rank"), py::arg("context") = py::none(),
"Gets a strided layout attribute with dynamic offset and strides of a "
"given rank.");
c.def_property_readonly(
"offset",
[](PyStridedLayoutAttribute &self) {
Expand Down
11 changes: 11 additions & 0 deletions mlir/test/python/ir/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,14 @@ def testStridedLayoutAttr():
print(attr.strides[1])
# CHECK: 13
print(attr.strides[2])

attr = StridedLayoutAttr.get_fully_dynamic(3)
dynamic = ShapedType.get_dynamic_stride_or_offset()
# CHECK: strided<[?, ?, ?], offset: ?>
print(attr)
# CHECK: offset is dynamic: True
print(f"offset is dynamic: {attr.offset == dynamic}")
# CHECK: rank: 3
print(f"rank: {len(attr.strides)}")
# CHECK: strides are dynamic: [True, True, True]
print(f"strides are dynamic: {[s == dynamic for s in attr.strides]}")

0 comments on commit e3fd612

Please sign in to comment.