-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[MLIR] fix shape.broadcast canonicalize with all empty shape operands #118941
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-mlir Author: Chenhui Huang (YellowHCH) ChangesExample: all the operands of
One can reproduce crash when canonicalize with down-top order, cmd like this: The root cause is when all operands are empty tensor, Full diff: https://github.com/llvm/llvm-project/pull/118941.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/Shape/IR/Shape.cpp b/mlir/lib/Dialect/Shape/IR/Shape.cpp
index bebfaa8c1ea822..8779fe837c7ae7 100644
--- a/mlir/lib/Dialect/Shape/IR/Shape.cpp
+++ b/mlir/lib/Dialect/Shape/IR/Shape.cpp
@@ -699,6 +699,12 @@ struct RemoveEmptyShapeOperandsPattern : public OpRewritePattern<OpTy> {
isPotentiallyNonEmptyShape);
// Reduce op to equivalent without empty shape operands.
+ if (newOperands.empty()) {
+ rewriter.replaceOpWithNewOp<ConstShapeOp>(
+ op, op->getResultTypes().front(), rewriter.getIndexTensorAttr({}));
+ return success();
+ }
+
if (newOperands.size() < op.getNumOperands()) {
rewriter.replaceOpWithNewOp<OpTy>(op, op->getResultTypes(), newOperands,
op->getAttrs());
diff --git a/mlir/test/Dialect/Shape/canonicalize.mlir b/mlir/test/Dialect/Shape/canonicalize.mlir
index 5b98a7790debf2..d55e0d8291cfef 100644
--- a/mlir/test/Dialect/Shape/canonicalize.mlir
+++ b/mlir/test/Dialect/Shape/canonicalize.mlir
@@ -1,4 +1,5 @@
// RUN: mlir-opt -split-input-file -allow-unregistered-dialect -canonicalize="test-convergence" %s | FileCheck %s
+// RUN: mlir-opt -split-input-file -allow-unregistered-dialect -canonicalize="test-convergence top-down=1" %s | FileCheck %s
// CHECK-LABEL: func @f
func.func @f(%arg0: tensor<2x3x4xf32>) -> tensor<3xindex> {
@@ -134,6 +135,21 @@ func.func @all_but_one_empty(%arg0 : !shape.shape) -> !shape.shape {
// -----
+// All operands are known empty shapes.
+// CHECK-LABEL: @all_empty
+// CHECK-SAME: (%[[ARG_0:.*]]: tensor<f32>, %[[ARG_1:.*]]: tensor<i1>)
+func.func @all_empty(%arg0: tensor<f32>, %arg1: tensor<i1>) -> tensor<0xindex> {
+ // CHECK: %[[CST:.*]] = shape.const_shape [] : tensor<0xindex>
+ // CHECK: return %[[CST]] : tensor<0xindex>
+ %1 = shape.shape_of %arg0 : tensor<f32> -> tensor<0xindex>
+ %2 = shape.shape_of %arg1 : tensor<i1> -> tensor<0xindex>
+ %3 = shape.const_shape [] : tensor<0xindex>
+ %4 = shape.broadcast %1, %2, %3 : tensor<0xindex>, tensor<0xindex>, tensor<0xindex> -> tensor<0xindex>
+ return %4 : tensor<0xindex>
+}
+
+// -----
+
// Partial folding.
// CHECK-LABEL: @partial_folding
// CHECK-SAME: (%[[ARG:.*]]: !shape.shape)
|
@llvm/pr-subscribers-mlir-shape Author: Chenhui Huang (YellowHCH) ChangesExample: all the operands of
One can reproduce crash when canonicalize with down-top order, cmd like this: The root cause is when all operands are empty tensor, Full diff: https://github.com/llvm/llvm-project/pull/118941.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/Shape/IR/Shape.cpp b/mlir/lib/Dialect/Shape/IR/Shape.cpp
index bebfaa8c1ea822..8779fe837c7ae7 100644
--- a/mlir/lib/Dialect/Shape/IR/Shape.cpp
+++ b/mlir/lib/Dialect/Shape/IR/Shape.cpp
@@ -699,6 +699,12 @@ struct RemoveEmptyShapeOperandsPattern : public OpRewritePattern<OpTy> {
isPotentiallyNonEmptyShape);
// Reduce op to equivalent without empty shape operands.
+ if (newOperands.empty()) {
+ rewriter.replaceOpWithNewOp<ConstShapeOp>(
+ op, op->getResultTypes().front(), rewriter.getIndexTensorAttr({}));
+ return success();
+ }
+
if (newOperands.size() < op.getNumOperands()) {
rewriter.replaceOpWithNewOp<OpTy>(op, op->getResultTypes(), newOperands,
op->getAttrs());
diff --git a/mlir/test/Dialect/Shape/canonicalize.mlir b/mlir/test/Dialect/Shape/canonicalize.mlir
index 5b98a7790debf2..d55e0d8291cfef 100644
--- a/mlir/test/Dialect/Shape/canonicalize.mlir
+++ b/mlir/test/Dialect/Shape/canonicalize.mlir
@@ -1,4 +1,5 @@
// RUN: mlir-opt -split-input-file -allow-unregistered-dialect -canonicalize="test-convergence" %s | FileCheck %s
+// RUN: mlir-opt -split-input-file -allow-unregistered-dialect -canonicalize="test-convergence top-down=1" %s | FileCheck %s
// CHECK-LABEL: func @f
func.func @f(%arg0: tensor<2x3x4xf32>) -> tensor<3xindex> {
@@ -134,6 +135,21 @@ func.func @all_but_one_empty(%arg0 : !shape.shape) -> !shape.shape {
// -----
+// All operands are known empty shapes.
+// CHECK-LABEL: @all_empty
+// CHECK-SAME: (%[[ARG_0:.*]]: tensor<f32>, %[[ARG_1:.*]]: tensor<i1>)
+func.func @all_empty(%arg0: tensor<f32>, %arg1: tensor<i1>) -> tensor<0xindex> {
+ // CHECK: %[[CST:.*]] = shape.const_shape [] : tensor<0xindex>
+ // CHECK: return %[[CST]] : tensor<0xindex>
+ %1 = shape.shape_of %arg0 : tensor<f32> -> tensor<0xindex>
+ %2 = shape.shape_of %arg1 : tensor<i1> -> tensor<0xindex>
+ %3 = shape.const_shape [] : tensor<0xindex>
+ %4 = shape.broadcast %1, %2, %3 : tensor<0xindex>, tensor<0xindex>, tensor<0xindex> -> tensor<0xindex>
+ return %4 : tensor<0xindex>
+}
+
+// -----
+
// Partial folding.
// CHECK-LABEL: @partial_folding
// CHECK-SAME: (%[[ARG:.*]]: !shape.shape)
|
mlir/lib/Dialect/Shape/IR/Shape.cpp
Outdated
@@ -699,6 +699,12 @@ struct RemoveEmptyShapeOperandsPattern : public OpRewritePattern<OpTy> { | |||
isPotentiallyNonEmptyShape); | |||
|
|||
// Reduce op to equivalent without empty shape operands. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this comment is more applicable to the next branch starting from L708?
The new code seems to replace the op with the empty shape operands. The comment can be something like this?
// Replace the op with empty shape constant if all operants are reduced to be empty.
@@ -1,4 +1,5 @@ | |||
// RUN: mlir-opt -split-input-file -allow-unregistered-dialect -canonicalize="test-convergence" %s | FileCheck %s | |||
// RUN: mlir-opt -split-input-file -allow-unregistered-dialect -canonicalize="test-convergence top-down=1" %s | FileCheck %s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the original issue, we use top-down=0
to reproduce the case. Is it okay to use top-down=1
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be top-down=0
. Thanks for your reminder.
e124e18
to
0375983
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks good to me now. Thanks for updating!
@YellowHCH Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Example: all the operands of
shape.broadcast
are empty tensors.One can reproduce crash when canonicalize with down-top order, cmd like this:
mlir-opt -split-input-file -allow-unregistered-dialect -canonicalize="test-convergence top-down=0" %s
The root cause is when all operands are empty tensor,
RemoveEmptyShapeOperandsPattern
would filter out all operands.