Skip to content

[mlir][linalg] Fix EraseIdentityLinalgOp on fill-like ops #130000

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 6 commits into from
Jun 2, 2025
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
30 changes: 20 additions & 10 deletions mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1278,8 +1278,9 @@ LogicalResult GenericOp::verify() { return success(); }

namespace {

/// Remove any linalg operation (on tensors) that are just copying
/// the values from inputs to the results. Requirements are
/// Remove linalg operations that are just copying the values from inputs to
/// results. In the memref case, the operation must be copying to and from the
/// same value. Requirements are:
/// 1) All iterator types are parallel
/// 2) The body contains just a yield operation with the yielded values being
/// the arguments corresponding to the operands.
Expand All @@ -1304,18 +1305,27 @@ struct EraseIdentityLinalgOp : public OpRewritePattern<OpTy> {

// In the buffer case, we need to check exact buffer equality.
if (linalgOp.hasPureBufferSemantics()) {
if (linalgOp.getNumDpsInputs() == 1 && linalgOp.getNumDpsInits() == 1 &&
linalgOp.getDpsInputOperand(0)->get() ==
if (linalgOp.getNumDpsInputs() != 1 || linalgOp.getNumDpsInits() != 1 ||
linalgOp.getDpsInputOperand(0)->get() !=
linalgOp.getDpsInitOperand(0)->get()) {
rewriter.eraseOp(linalgOp);
return success();
return rewriter.notifyMatchFailure(
linalgOp, "expected single input and output to be the same value");
}
return failure();

auto yieldArg = dyn_cast<BlockArgument>(yieldOp.getOperand(0));
if (!yieldArg || yieldArg.getOwner() != &body) {
return rewriter.notifyMatchFailure(linalgOp,
"cannot fold fill-like op");
}

rewriter.eraseOp(linalgOp);
return success();
}

// Mixed semantics is not supported yet.
if (!linalgOp.hasPureTensorSemantics())
return failure();
if (!linalgOp.hasPureTensorSemantics()) {
return rewriter.notifyMatchFailure(
linalgOp, "mixed semantics is not supported yet");
}

// Get the argument number of the returned values. That is the operand
// number to use for replacing uses of this operation.
Expand Down
32 changes: 31 additions & 1 deletion mlir/test/Dialect/Linalg/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func.func @dynamic_fill_pack(%arg0: tensor<?x?xf32>) -> tensor<?x?x16x16xf32> {

// -----

// CHECK: func @fold_self_copy
// CHECK-LABEL: func @fold_self_copy
func.func @fold_self_copy(%0 : memref<4x16xf32>) {
// CHECK-NEXT: return
linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>,
Expand All @@ -511,6 +511,36 @@ func.func @fold_self_copy(%0 : memref<4x16xf32>) {

// -----

// CHECK-LABEL: func @no_fold_fill_like_memref
// CHECK-NEXT: linalg.generic
func.func @no_fold_fill_like_memref(%in_out : memref<4x16xf32>, %fill_val : f32) {
linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>,
affine_map<(d0, d1) -> (d0, d1)>],
iterator_types = ["parallel", "parallel"]}
ins(%in_out : memref<4x16xf32>)
outs(%in_out : memref<4x16xf32>) {
^bb0(%arg0: f32, %arg1: f32):
linalg.yield %fill_val : f32
}
return
}

// -----

// CHECK-LABEL: func @no_fold_fill_like_tensor
// CHECK-NEXT: linalg.generic
func.func @no_fold_fill_like_tensor(%in_out : tensor<4x16xf32>, %fill_val : f32) -> tensor<4x16xf32> {
%result = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>,
affine_map<(d0, d1) -> (d0, d1)>],
iterator_types = ["parallel", "parallel"]}
ins(%in_out : tensor<4x16xf32>)
outs(%in_out : tensor<4x16xf32>) {
^bb0(%arg0: f32, %arg1: f32):
linalg.yield %fill_val : f32
} -> tensor<4x16xf32>
return %result : tensor<4x16xf32>
}

// CHECK-LABEL: func @fold_static_pad_fill
// CHECK: %[[F0:.+]] = arith.constant 0.000000e+00 : f32
// CHECK: %[[INIT:.+]] = tensor.empty() : tensor<412x276xf32>
Expand Down