[Mlir-commits] [mlir] [MLIR] fix shape.broadcast canonicalize with all empty shape operands (PR #118941)
Chenhui Huang
llvmlistbot at llvm.org
Thu Dec 12 18:32:39 PST 2024
https://github.com/YellowHCH updated https://github.com/llvm/llvm-project/pull/118941
>From 03759833d975216738d0569f0ab9fbe1bbd354f9 Mon Sep 17 00:00:00 2001
From: "huangchenhui.yellow" <huangchenhui.yellow at bytedance.com>
Date: Fri, 6 Dec 2024 16:37:02 +0800
Subject: [PATCH] [MLIR] fix shape.broadcast canonicalize all empty shape
operands
---
mlir/lib/Dialect/Shape/IR/Shape.cpp | 8 ++++++++
mlir/test/Dialect/Shape/canonicalize.mlir | 16 ++++++++++++++++
2 files changed, 24 insertions(+)
diff --git a/mlir/lib/Dialect/Shape/IR/Shape.cpp b/mlir/lib/Dialect/Shape/IR/Shape.cpp
index bebfaa8c1ea822..65efc88e9c4033 100644
--- a/mlir/lib/Dialect/Shape/IR/Shape.cpp
+++ b/mlir/lib/Dialect/Shape/IR/Shape.cpp
@@ -698,6 +698,14 @@ struct RemoveEmptyShapeOperandsPattern : public OpRewritePattern<OpTy> {
auto newOperands = llvm::filter_to_vector<8>(op->getOperands(),
isPotentiallyNonEmptyShape);
+ // Replace the op with empty shape constant if all operants are reduced to
+ // be empty.
+ if (newOperands.empty()) {
+ rewriter.replaceOpWithNewOp<ConstShapeOp>(
+ op, op->getResultTypes().front(), rewriter.getIndexTensorAttr({}));
+ return success();
+ }
+
// Reduce op to equivalent without empty shape operands.
if (newOperands.size() < op.getNumOperands()) {
rewriter.replaceOpWithNewOp<OpTy>(op, op->getResultTypes(), newOperands,
diff --git a/mlir/test/Dialect/Shape/canonicalize.mlir b/mlir/test/Dialect/Shape/canonicalize.mlir
index 5b98a7790debf2..cf439c9c1b8545 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=0" %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)
More information about the Mlir-commits
mailing list