Skip to content

Let memref.collapse_shape implement ReifyRankedShapedTypeOpInterface. #107752

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: 4 additions & 2 deletions mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1670,7 +1670,7 @@ def MemRef_ExpandShapeOp : MemRef_ReassociativeReshapeOp<"expand_shape", [
}]>,

// Builder that infers the result layout map. The result shape must be
// specified. Otherwise, the op may be ambiguous. The output shape for
// specified. Otherwise, the op may be ambiguous. The output shape for
// the op will be inferred using the inferOutputShape() method.
OpBuilder<(ins "ArrayRef<int64_t>":$resultShape, "Value":$src,
"ArrayRef<ReassociationIndices>":$reassociation)>,
Expand Down Expand Up @@ -1699,7 +1699,9 @@ def MemRef_ExpandShapeOp : MemRef_ReassociativeReshapeOp<"expand_shape", [
}

def MemRef_CollapseShapeOp : MemRef_ReassociativeReshapeOp<"collapse_shape", [
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>]> {
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>,
DeclareOpInterfaceMethods<ReifyRankedShapedTypeOpInterface>]>
{
let summary = "operation to produce a memref with a smaller rank.";
let description = [{
The `memref.collapse_shape` op produces a new view with a smaller rank
Expand Down
36 changes: 36 additions & 0 deletions mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2497,6 +2497,42 @@ MemRefType CollapseShapeOp::computeCollapsedType(
srcType.getMemorySpace());
}

static bool isDynamicInGroup(ReassociationIndices group,
ArrayRef<int64_t> sourceShape) {
return llvm::any_of(group, [sourceShape](int64_t dim) {
return ShapedType::isDynamic(sourceShape[dim]);
});
}

// This method supports following cases only:
// - There is dynamic dimension in reassociation groups with single element.
LogicalResult CollapseShapeOp::reifyResultShapes(
OpBuilder &builder, ReifiedRankedShapedTypeDims &reifiedResultShapes) {
SmallVector<ReassociationIndices, 4> reassociationArray =
getReassociationIndices();
Value source = getSrc();
auto sourceShape = cast<MemRefType>(source.getType()).getShape();
if (!ShapedType::isDynamicShape(sourceShape))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesnt have to fail if the source shape is dynamic?

return failure();
for (auto group : enumerate(reassociationArray)) {
bool isDynInGroup = isDynamicInGroup(group.value(), sourceShape);
if (isDynInGroup && group.value().size() > 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we need this restriction either. You can always construct an expression for the result dimension in terms of source dimensions... So there seems to be little reason to bail

return failure();
}
auto resultShape = cast<ShapedType>(getResultType()).getShape();

SmallVector<Value> dynamicValues;
for (int64_t i = 0; i < resultShape.size(); ++i) {
if (ShapedType::isDynamic(resultShape[i]))
dynamicValues.push_back(builder.create<DimOp>(
source.getLoc(), source,
builder.create<arith::ConstantIndexOp>(source.getLoc(), i)));
}
reifiedResultShapes = {getMixedValues(resultShape, dynamicValues, builder)};

return success();
}

void CollapseShapeOp::build(OpBuilder &b, OperationState &result, Value src,
ArrayRef<ReassociationIndices> reassociation,
ArrayRef<NamedAttribute> attrs) {
Expand Down
65 changes: 65 additions & 0 deletions mlir/test/Dialect/MemRef/resolve-dim-ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,71 @@ func.func @dim_out_of_bounds(%m : memref<7x8xf32>) -> index {

// -----

// CHECK-LABEL: func.func @dyn_dim_of_memref_collapse_shape(
// CHECK-SAME: %[[VAL_0:.*]]: memref<?x4x8x32xsi8>) -> index {
// CHECK: %[[VAL_1:.*]] = arith.constant 0 : index
// CHECK: %[[VAL_2:.*]] = memref.dim %[[VAL_0]], %[[VAL_1]] : memref<?x4x8x32xsi8>
// CHECK: return %[[VAL_2]] : index
// CHECK: }

func.func @dyn_dim_of_memref_collapse_shape(%arg0: memref<?x4x8x32xsi8>)
-> index
{
%c0 = arith.constant 0 : index
%dim_16 = memref.dim %arg0, %c0 : memref<?x4x8x32xsi8>
%alloc_17 = memref.alloc(%dim_16) {alignment = 32 : i64} : memref<?x32x4x8xsi8>
%collapse_shape = memref.collapse_shape %alloc_17 [[0], [1], [2, 3]] : memref<?x32x4x8xsi8> into memref<?x32x32xsi8>
%dim_18 = memref.dim %collapse_shape, %c0 : memref<?x32x32xsi8>
return %dim_18: index
}

// -----

// CHECK-LABEL: func.func @resolve_when_collapse_after_collapse(
// CHECK-SAME: %[[VAL_0:.*]]: memref<?x4x8x32xsi8>) -> index {
// CHECK: %[[VAL_1:.*]] = arith.constant 0 : index
// CHECK: %[[VAL_2:.*]] = memref.dim %[[VAL_0]], %[[VAL_1]] : memref<?x4x8x32xsi8>
// CHECK: return %[[VAL_2]] : index
// CHECK: }

func.func @resolve_when_collapse_after_collapse(%arg0: memref<?x4x8x32xsi8>)
-> index
{
%c0 = arith.constant 0 : index
%dim_16 = memref.dim %arg0, %c0 : memref<?x4x8x32xsi8>
%alloc_17 = memref.alloc(%dim_16) {alignment = 32 : i64} : memref<?x32x4x8xsi8>
%collapse_shape = memref.collapse_shape %alloc_17 [[0], [1], [2, 3]] : memref<?x32x4x8xsi8> into memref<?x32x32xsi8>
%collapse_shape_1 = memref.collapse_shape %collapse_shape [[0], [1, 2]] : memref<?x32x32xsi8> into memref<?x1024xsi8>
%dim_18 = memref.dim %collapse_shape_1, %c0 : memref<?x1024xsi8>
return %dim_18: index
}

// -----

// CHECK-LABEL: func.func @unfoldable_memref_collapse_shape(
// CHECK-SAME: %[[VAL_0:.*]]: memref<1x?x8x32xsi8>) -> index {
// CHECK: %[[VAL_1:.*]] = arith.constant 1 : index
// CHECK: %[[VAL_2:.*]] = arith.constant 2 : index
// CHECK: %[[VAL_3:.*]] = memref.dim %[[VAL_0]], %[[VAL_1]] : memref<1x?x8x32xsi8>
// CHECK: %[[VAL_4:.*]] = memref.alloc(%[[VAL_3]]) {alignment = 32 : i64} : memref<1x32x?x8xsi8>
// CHECK: %[[VAL_5:.*]] = memref.collapse_shape %[[VAL_4]] {{\[\[}}0], [1], [2, 3]] : memref<1x32x?x8xsi8> into memref<1x32x?xsi8>
// CHECK: %[[VAL_6:.*]] = memref.dim %[[VAL_5]], %[[VAL_2]] : memref<1x32x?xsi8>
// CHECK: return %[[VAL_6]] : index
// CHECK: }
func.func @unfoldable_memref_collapse_shape(%arg0: memref<1x?x8x32xsi8>)
-> index
{
%c1 = arith.constant 1 : index
%c2 = arith.constant 2 : index
%dim_1 = memref.dim %arg0, %c1 : memref<1x?x8x32xsi8>
%alloc_0 = memref.alloc(%dim_1) {alignment = 32 : i64} : memref<1x32x?x8xsi8>
%collapse_shape = memref.collapse_shape %alloc_0 [[0], [1], [2, 3]] : memref<1x32x?x8xsi8> into memref<1x32x?xsi8>
%dim_3 = memref.dim %collapse_shape, %c2 : memref<1x32x?xsi8>
return %dim_3: index
}

// -----

// CHECK-LABEL: func @dim_out_of_bounds_2(
// CHECK-NEXT: arith.constant
// CHECK-NEXT: arith.constant
Expand Down
Loading