Skip to content

Commit f74e909

Browse files
authored
[mlir][memref]: Collapse strided unit dim even if strides are dynamic (#157330)
1 parent 6ad25c5 commit f74e909

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,6 +2568,11 @@ computeCollapsedLayoutMap(MemRefType srcType,
25682568
auto trailingReassocs = ArrayRef<int64_t>(reassoc).drop_front();
25692569
auto stride = SaturatedInteger::wrap(resultStrides[resultStrideIndex--]);
25702570
for (int64_t idx : llvm::reverse(trailingReassocs)) {
2571+
// Dimensions of size 1 should be skipped, because their strides are
2572+
// meaningless and could have any arbitrary value.
2573+
if (srcShape[idx - 1] == 1)
2574+
continue;
2575+
25712576
stride = stride * SaturatedInteger::wrap(srcShape[idx]);
25722577

25732578
// Both source and result stride must have the same static value. In that
@@ -2582,11 +2587,6 @@ computeCollapsedLayoutMap(MemRefType srcType,
25822587
if (strict && (stride.saturated || srcStride.saturated))
25832588
return failure();
25842589

2585-
// Dimensions of size 1 should be skipped, because their strides are
2586-
// meaningless and could have any arbitrary value.
2587-
if (srcShape[idx - 1] == 1)
2588-
continue;
2589-
25902590
if (!stride.saturated && !srcStride.saturated && stride != srcStride)
25912591
return failure();
25922592
}

mlir/test/Dialect/MemRef/ops.mlir

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ func.func @expand_collapse_shape_dynamic(%arg0: memref<?x?x?xf32>,
440440
%arg4: index,
441441
%arg5: index,
442442
%arg6: index,
443-
%arg7: memref<4x?x4xf32>) {
443+
%arg7: memref<4x?x4xf32>,
444+
%arg8: memref<1x1x18x?xsi8, strided<[?, ?, ?, 1], offset: ?>>) {
444445
// CHECK: memref.collapse_shape {{.*}} {{\[}}[0, 1], [2]]
445446
// CHECK-SAME: memref<?x?x?xf32> into memref<?x?xf32>
446447
%0 = memref.collapse_shape %arg0 [[0, 1], [2]] :
@@ -489,6 +490,10 @@ func.func @expand_collapse_shape_dynamic(%arg0: memref<?x?x?xf32>,
489490
// CHECK: memref.expand_shape {{.*}} {{\[}}[0, 1], [2], [3, 4]]
490491
%4 = memref.expand_shape %arg7 [[0, 1], [2], [3, 4]] output_shape [2, 2, %arg4, 2, 2]
491492
: memref<4x?x4xf32> into memref<2x2x?x2x2xf32>
493+
494+
// CHECK: memref.collapse_shape {{.*}} {{\[}}[0, 1], [2], [3]]
495+
// CHECK-SAME: memref<1x1x18x?xsi8, strided<[?, ?, ?, 1], offset: ?>> into memref<1x18x?xsi8, strided<[?, ?, 1], offset: ?>>
496+
%5 = memref.collapse_shape %arg8 [[0, 1], [2], [3]] : memref<1x1x18x?xsi8, strided<[?, ?, ?, 1], offset: ?>> into memref<1x18x?xsi8, strided<[?, ?, 1], offset: ?>>
492497
return
493498
}
494499

0 commit comments

Comments
 (0)