Skip to content
Merged
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
8 changes: 8 additions & 0 deletions mlir/lib/Dialect/Shape/IR/Shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions mlir/test/Dialect/Shape/canonicalize.mlir
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down Expand Up @@ -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)
Expand Down
Loading