Skip to content

Commit

Permalink
[mlir][memref] ReinterpretCast: allow static sizes/strides/offset whe…
Browse files Browse the repository at this point in the history
…re affine map expects dynamic

* There is no reason to forbid that case
* Also, user will get very unfriendly error like `expected result type with offset = -9223372036854775808 instead of 1`

Differential Revision: https://reviews.llvm.org/D114678
  • Loading branch information
Butygin committed Dec 21, 2021
1 parent 6f1a501 commit 28ab10f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
8 changes: 5 additions & 3 deletions mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ static LogicalResult verify(ReinterpretCastOp op) {
extractFromI64ArrayAttr(op.static_sizes())))) {
int64_t resultSize = std::get<0>(en.value());
int64_t expectedSize = std::get<1>(en.value());
if (resultSize != expectedSize)
if (!ShapedType::isDynamic(resultSize) && resultSize != expectedSize)
return op.emitError("expected result type with size = ")
<< expectedSize << " instead of " << resultSize
<< " in dim = " << en.index();
Expand All @@ -1175,7 +1175,8 @@ static LogicalResult verify(ReinterpretCastOp op) {
// Match offset in result memref type and in static_offsets attribute.
int64_t expectedOffset =
extractFromI64ArrayAttr(op.static_offsets()).front();
if (resultOffset != expectedOffset)
if (!ShapedType::isDynamicStrideOrOffset(resultOffset) &&
resultOffset != expectedOffset)
return op.emitError("expected result type with offset = ")
<< resultOffset << " instead of " << expectedOffset;

Expand All @@ -1184,7 +1185,8 @@ static LogicalResult verify(ReinterpretCastOp op) {
resultStrides, extractFromI64ArrayAttr(op.static_strides())))) {
int64_t resultStride = std::get<0>(en.value());
int64_t expectedStride = std::get<1>(en.value());
if (resultStride != expectedStride)
if (!ShapedType::isDynamicStrideOrOffset(resultStride) &&
resultStride != expectedStride)
return op.emitError("expected result type with stride = ")
<< expectedStride << " instead of " << resultStride
<< " in dim = " << en.index();
Expand Down
12 changes: 0 additions & 12 deletions mlir/test/Dialect/MemRef/invalid.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,6 @@ func @memref_reinterpret_cast_offset_mismatch(%in: memref<?xf32>) {

// -----

func @memref_reinterpret_cast_offset_mismatch(%in: memref<?xf32>) {
%c0 = arith.constant 0 : index
%c10 = arith.constant 10 : index
// expected-error @+1 {{expected result type with size = 10 instead of -1 in dim = 0}}
%out = memref.reinterpret_cast %in to
offset: [%c0], sizes: [10, %c10], strides: [%c10, 1]
: memref<?xf32> to memref<?x?xf32, offset: ?, strides: [?, 1]>
return
}

// -----

func @memref_reshape_element_type_mismatch(
%buf: memref<*xf32>, %shape: memref<1xi32>) {
// expected-error @+1 {{element types of source and destination memref types should be the same}}
Expand Down
9 changes: 9 additions & 0 deletions mlir/test/Dialect/MemRef/ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ func @memref_reinterpret_cast(%in: memref<?xf32>)
return %out : memref<10x?xf32, offset: ?, strides: [?, 1]>
}

// CHECK-LABEL: func @memref_reinterpret_cast_static_to_dynamic_sizes
func @memref_reinterpret_cast_static_to_dynamic_sizes(%in: memref<?xf32>)
-> memref<10x?xf32, offset: ?, strides: [?, 1]> {
%out = memref.reinterpret_cast %in to
offset: [1], sizes: [10, 10], strides: [1, 1]
: memref<?xf32> to memref<10x?xf32, offset: ?, strides: [?, 1]>
return %out : memref<10x?xf32, offset: ?, strides: [?, 1]>
}

// CHECK-LABEL: func @memref_reshape(
func @memref_reshape(%unranked: memref<*xf32>, %shape1: memref<1xi32>,
%shape2: memref<2xi32>, %shape3: memref<?xi32>) -> memref<*xf32> {
Expand Down

0 comments on commit 28ab10f

Please sign in to comment.