Skip to content

[mlir][linalg] Fix a DCE crash with memref<0x..> and the op has uses #73908

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2101,7 +2101,7 @@ struct EraseDeadLinalgOp : public OpInterfaceRewritePattern<LinalgOp> {
auto mt = llvm::dyn_cast<MemRefType>(opOperand.get().getType());
if (!mt)
continue;
if (llvm::is_contained(op.getShape(&opOperand), 0)) {
if (llvm::is_contained(op.getShape(&opOperand), 0) && op->use_empty()) {
rewriter.eraseOp(op);
return success();
}
Expand Down
27 changes: 26 additions & 1 deletion mlir/test/Dialect/Linalg/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func.func @memref_cast(%a: index, %b: index) -> memref<?x?xf32> {
}

func.func @dce_zero_memref(%arg0 : memref<0xf32>, %arg1: tensor<0xf32>) -> tensor<0xf32> {
// memref<0x32> is expected to be dce'ed
// memref<0xf32> is expected to be dce'ed
memref.copy %arg0, %arg0 : memref<0xf32> to memref<0xf32>

// tensor<0xf32> cannot be dce'ed
Expand All @@ -47,6 +47,31 @@ func.func @dce_zero_memref(%arg0 : memref<0xf32>, %arg1: tensor<0xf32>) -> tenso

// -----

#accesses = [
affine_map<(i) -> (i)>,
affine_map<(i) -> (i)>
]

#trait = {
indexing_maps = #accesses,
iterator_types = ["parallel"]
}

func.func @no_dce_zero_memref(%arg0 : memref<0xf32>, %arg1: tensor<0xf32>) -> tensor<0xf32> {
// memref<0xf32> cannot be dce'ed
%2 = linalg.generic #trait ins(%arg0: memref<0xf32>) outs(%arg1 : tensor<0xf32>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I thought we had tightened the op semantics to not have mixed memref/tensor anymore.
I would have expected a verification error here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review!
It seems to me that the linalg.GenericOp verification does not perform any meaningful action.

https://github.com/llvm/llvm-project/blob/main/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp#L1081

Do you think we should add a verify that prohibits the mix of memref/tensor at linalg.GenericOp's verification?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it would be a good idea to verify whether the mix of memref/tensor is present in the operand of inputs in the verifyStructuredOpInterface of LinalgOp.

However, I'm unsure whether it is correct to restrict the mix of memref/tensor in the input for the entire LinalgOp. Is it correct?

For example, in the linalg_effects test, there is a case where tensor/memref mixing exists in the inputs of linalg.matmul, and it is unclear whether such mixing is allowed in linalg.matmul.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolasvasilache Sorry for forgetting a mention... What do you think about it?

^bb(%0: f32, %1: f32) :
linalg.yield %1 : f32
} -> tensor<0xf32>

return %2: tensor<0xf32>
}

// CHECK-LABEL: @no_dce_zero_memref
// CHECK-NEXT: linalg.generic

// -----

func.func @dce_self_linalg_copy(%arg0 : memref<?xf32>) {
linalg.copy ins(%arg0: memref<?xf32>) outs(%arg0: memref<?xf32>)
return
Expand Down