Skip to content

[MLIR] Fix canonicalization pattern for 'shape.shape_of' #134234

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

Merged
merged 15 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
18 changes: 15 additions & 3 deletions mlir/lib/Dialect/Shape/IR/Shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1734,10 +1734,22 @@ struct ShapeOfFromReshape : public OpRewritePattern<shape::ShapeOfOp> {
// Operand 'shape' of 'tensor.reshape' may now be used as the result of
// 'shape.shape_of'. While its type is guaranteed to be compatible in well-
// formed IR, it may not be identical (dynamically vs statically shaped),
// in which case it needs to be cast first.
// in which case it needs to be cast first using 'tensor.cast'.
// Additionally, it may not have identical element type (i32 vs index)
// while it has identical shaped type (dynamic vs static), in which case it needs
// to be cast first using 'arith.index_cast'.
// Note: 'shape.shape_of' op result must be shape or extent tensor.
Value shape = tensorReshapeOp.getShape();
if (op.getType() != shape.getType())
shape = rewriter.create<tensor::CastOp>(op.getLoc(), op.getType(), shape);

auto opTensorTy = llvm::cast<RankedTensorType>(op.getType());
auto shapeTensorTy = llvm::cast<RankedTensorType>(shape.getType());

if (op.getType() != shape.getType()) {
if (opTensorTy.getElementType() == shapeTensorTy.getElementType())
shape = rewriter.create<tensor::CastOp>(op.getLoc(), op.getType(), shape);
else if (!isExtentTensorType(shape.getType()))
shape = rewriter.create<arith::IndexCastOp>(op.getLoc(), op.getType(), shape);
}

rewriter.replaceOp(op, shape);
return success();
Expand Down
33 changes: 31 additions & 2 deletions mlir/test/Dialect/Shape/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1389,10 +1389,25 @@ func.func @shape_of_from_reshape(%arg0: tensor<*xf32>, %arg1: tensor<?xindex>) -

// -----

// CHECK-LABEL: func @shape_of_from_reshape_compatible_types
// Check statically shaped types, with element types i32 to index.
// CHECK-LABEL: func @shape_of_from_reshape_compatible_types1
// CHECK-SAME: %[[INPUT:.*]]: tensor<?x1xf32>
// CHECK-SAME: %[[SHAPE:.*]]: tensor<3xi32>
func.func @shape_of_from_reshape_compatible_types1(%arg0: tensor<?x1xf32>, %arg1: tensor<3xi32>) -> tensor<3xindex> {
// CHECK: %[[CAST_SHAPE:.*]] = arith.index_cast %[[SHAPE]] : tensor<3xi32> to tensor<3xindex>
// CHECK: return %[[CAST_SHAPE]] : tensor<3xindex>
%0 = tensor.reshape %arg0(%arg1) : (tensor<?x1xf32>, tensor<3xi32>) -> tensor<?x1x1xf32>
%1 = shape.shape_of %0 : tensor<?x1x1xf32> -> tensor<3xindex>
return %1 : tensor<3xindex>
}

// -----

// Check similar element types, with statically shaped to dynamically shaped.
// CHECK-LABEL: func @shape_of_from_reshape_compatible_types2
// CHECK-SAME: %[[INPUT:.*]]: tensor<*xf32>
// CHECK-SAME: %[[SHAPE:.*]]: tensor<5xindex>
func.func @shape_of_from_reshape_compatible_types(%arg0: tensor<*xf32>, %arg1: tensor<5xindex>) -> tensor<?xindex> {
func.func @shape_of_from_reshape_compatible_types2(%arg0: tensor<*xf32>, %arg1: tensor<5xindex>) -> tensor<?xindex> {
// CHECK: %[[CAST_SHAPE:.*]] = tensor.cast %[[SHAPE]] : tensor<5xindex> to tensor<?xindex>
// CHECK: return %[[CAST_SHAPE]] : tensor<?xindex>
%0 = tensor.reshape %arg0(%arg1) : (tensor<*xf32>, tensor<5xindex>) -> tensor<*xf32>
Expand All @@ -1402,6 +1417,20 @@ func.func @shape_of_from_reshape_compatible_types(%arg0: tensor<*xf32>, %arg1: t

// -----

// Check similar element types, with dynamically shaped to statically shaped.
// CHECK-LABEL: func @shape_of_from_reshape_compatible_types3
// CHECK-SAME: %[[INPUT:.*]]: tensor<*xf32>
// CHECK-SAME: %[[SHAPE:.*]]: tensor<?xindex>
func.func @shape_of_from_reshape_compatible_types3(%arg0: tensor<*xf32>, %arg1: tensor<?xindex>) -> tensor<5xindex> {
// CHECK: %[[CAST_SHAPE:.*]] = tensor.cast %[[SHAPE]] : tensor<?xindex> to tensor<5xindex>
// CHECK: return %[[CAST_SHAPE]] : tensor<5xindex>
%0 = tensor.reshape %arg0(%arg1) : (tensor<*xf32>, tensor<?xindex>) -> tensor<*xf32>
%1 = shape.shape_of %0 : tensor<*xf32> -> tensor<5xindex>
return %1 : tensor<5xindex>
}

// -----

// CHECK-LABEL: func @shape_of_from_reshape_nofold
// CHECK-SAME: %[[INPUT:.*]]: tensor<*xf32>
// CHECK-SAME: %[[SHAPE:.*]]: tensor<?xindex>
Expand Down
Loading