Skip to content

[mlir][memref] Add better computeCollapsedLayoutMap support for unit collapse #147967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2422,7 +2422,11 @@ computeCollapsedLayoutMap(MemRefType srcType,
ArrayRef<int64_t> ref = llvm::ArrayRef(reassoc);
while (srcShape[ref.back()] == 1 && ref.size() > 1)
ref = ref.drop_back();
if (ShapedType::isStatic(srcShape[ref.back()]) || ref.size() == 1) {
auto precedingRef = ref.drop_back();
bool allUnitPreceding = llvm::all_of(
precedingRef, [&srcShape](int idx) { return srcShape[idx] == 1; });
if (ShapedType::isStatic(srcShape[ref.back()]) || ref.size() == 1 ||
allUnitPreceding) {
resultStrides.push_back(srcStrides[ref.back()]);
} else {
// Dynamically-sized dims may turn out to be dims of size 1 at runtime, so
Expand Down
42 changes: 42 additions & 0 deletions mlir/test/Dialect/MemRef/collapse-strided.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// RUN: mlir-opt %s | FileCheck %s

// CHECK-LABEL: test_collapse(
func.func @test_collapse(%arg0: memref<1x?xf32, strided<[5, 1]>>) {
%collapse_shape = memref.collapse_shape %arg0 [[0, 1]] : memref<1x?xf32, strided<[5, 1]>> into memref<?xf32, strided<[1]>>
return
}

// CHECK-LABEL: test_collapse_5d_middle_dynamic(
func.func @test_collapse_5d_middle_dynamic(%arg0: memref<1x5x1x?x1xf32, strided<[540, 108, 18, 2, 1]>>) {
%collapse_shape = memref.collapse_shape %arg0 [[0, 1, 2, 3, 4]]
: memref<1x5x1x?x1xf32, strided<[540, 108, 18, 2, 1]>> into memref<?xf32, strided<[?]>>
return
}

// CHECK-LABEL: test_collapse_5d_mostly_units(
func.func @test_collapse_5d_mostly_units(%arg0: memref<1x1x1x?x1xf32, strided<[320, 80, 16, 2, 1]>>) {
%collapse_shape = memref.collapse_shape %arg0 [[0, 1, 2, 3, 4]]
: memref<1x1x1x?x1xf32, strided<[320, 80, 16, 2, 1]>> into memref<?xf32, strided<[2]>>
return
}

// CHECK-LABEL: test_partial_collapse_6d(
func.func @test_partial_collapse_6d(%arg0: memref<1x?x1x1x5x1xf32, strided<[3360, 420, 140, 35, 7, 1]>>) {
%collapse_shape = memref.collapse_shape %arg0 [[0, 1, 2, 3], [4, 5]]
: memref<1x?x1x1x5x1xf32, strided<[3360, 420, 140, 35, 7, 1]>> into memref<?x5xf32, strided<[420, 7]>>
return
}

// CHECK-LABEL: test_collapse_5d_grouped(
func.func @test_collapse_5d_grouped(%arg0: memref<1x5x1x?x1xf32, strided<[540, 108, 18, 2, 1]>>) {
%collapse_shape = memref.collapse_shape %arg0 [[0], [1, 2, 3, 4]]
: memref<1x5x1x?x1xf32, strided<[540, 108, 18, 2, 1]>> into memref<1x?xf32, strided<[540, ?]>>
return
}

// CHECK-LABEL: test_collapse_all_units(
func.func @test_collapse_all_units(%arg0: memref<1x1x1x1x1xf32, strided<[100, 50, 25, 10, 1]>>) {
%collapse_shape = memref.collapse_shape %arg0 [[0, 1, 2, 3, 4]]
: memref<1x1x1x1x1xf32, strided<[100, 50, 25, 10, 1]>> into memref<1xf32, strided<[100]>>
return
}