Skip to content

Commit 47602a6

Browse files
committed
[mlir][memref] Fix computeCollapsedLayoutMap for contiguous dynamic dim
1 parent 71037ee commit 47602a6

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "mlir/Interfaces/ViewLikeInterface.h"
2424
#include "llvm/ADT/STLExtras.h"
2525
#include "llvm/ADT/SmallBitVector.h"
26+
#include <algorithm>
2627

2728
using namespace mlir;
2829
using namespace mlir::memref;
@@ -2401,11 +2402,19 @@ computeCollapsedLayoutMap(MemRefType srcType,
24012402
if (!ShapedType::isDynamic(srcShape[ref.back()]) || ref.size() == 1) {
24022403
resultStrides.push_back(srcStrides[ref.back()]);
24032404
} else {
2404-
// Dynamically-sized dims may turn out to be dims of size 1 at runtime, so
2405-
// the corresponding stride may have to be skipped. (See above comment.)
2406-
// Therefore, the result stride cannot be statically determined and must
2407-
// be dynamic.
2408-
resultStrides.push_back(ShapedType::kDynamic);
2405+
bool contiguousSrcDim = srcStrides[ref.back()] == 1;
2406+
bool dynamicSizeIsPreserved =
2407+
std::all_of(ref.begin(), ref.end() - 1,
2408+
[srcShape](int64_t dim) { return srcShape[dim] == 1; });
2409+
if (contiguousSrcDim && dynamicSizeIsPreserved)
2410+
resultStrides.push_back(1);
2411+
else {
2412+
// Dynamically-sized dims may turn out to be dims of size 1 at runtime,
2413+
// so the corresponding stride may have to be skipped. (See above
2414+
// comment.) Therefore, the result stride cannot be statically
2415+
// determined and must be dynamic.
2416+
resultStrides.push_back(ShapedType::kDynamic);
2417+
}
24092418
}
24102419
}
24112420

mlir/test/Dialect/MemRef/invalid.mlir

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,14 @@ func.func @collapse_shape_wrong_collapsed_type(%arg0: memref<?x?x?xf32>) {
502502

503503
// -----
504504

505+
func.func @collapse_shape_infer_stride_of_dynamic_dim(%arg0: memref<1x1x?x1xsi32, strided<[36960, 4620, 1, 330]>, 1>, %dim : index) -> (memref<?xsi32, strided<[?]>, 1>) {
506+
// expected-error @+1 {{expected collapsed type to be 'memref<?xsi32, strided<[1]>, 1>' but found 'memref<?xsi32, strided<[?]>, 1>'}}
507+
%collapse_shape = memref.collapse_shape %arg0 [[0, 1, 2, 3]] : memref<1x1x?x1xsi32, strided<[36960, 4620, 1, 330]>, 1> into memref<?xsi32, strided<[?]>, 1>
508+
return %collapse_shape : memref<?xsi32, strided<[?]>, 1>
509+
}
510+
511+
// -----
512+
505513
func.func @expand_shape_illegal_static_memref
506514
(%arg0: memref<2x3x20xf32>) -> memref<2x3x2x4x5xf32> {
507515
// expected-error @+1 {{collapsed dim size (20) must equal reassociation group size (40)}}

0 commit comments

Comments
 (0)