Skip to content

[mlir][bufferization] Canonicalize to_memref(to_tensor(x)) to a CopyO… #126692

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 1 commit into from
Feb 11, 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
4 changes: 1 addition & 3 deletions mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ FailureOr<Value> mlir::bufferization::castOrReallocMemRefValue(
const BufferizationOptions &options) {
auto srcType = llvm::cast<MemRefType>(value.getType());

// Element type, rank and memory space must match.
// Element type and rank must match.
if (srcType.getElementType() != destType.getElementType())
return failure();
if (srcType.getMemorySpace() != destType.getMemorySpace())
return failure();
if (srcType.getRank() != destType.getRank())
return failure();

Expand Down
21 changes: 12 additions & 9 deletions mlir/test/Dialect/Bufferization/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ func.func @buffer_cast_of_tensor_load(%arg0: memref<?xf32>) -> memref<?xf32> {
// -----

// If the memrefs are not the same type, don't fold them.
// If the memrefs are not cast-compatible (e.g. different address space), don't
// canonicalize them either.
// CHECK-LABEL: func @no_fold_buffer_cast_of_tensor_load(
// If the memrefs are not cast-compatible but one can be copied into the other
// (e.g. different address space), canonicalize them to add + copy.
// CHECK-LABEL: func @canonicalize_buffer_cast_of_tensor_load_different_address_space(
// CHECK-SAME: %[[MEMREF_ADDRSPACE2:.*]]: memref<?xf32, 2>)
// CHECK-SAME: -> memref<?xf32, 7> {
// CHECK: %[[TENSOR:.*]] = bufferization.to_tensor
// CHECK-SAME: %[[MEMREF_ADDRSPACE2]] : memref<?xf32, 2> to tensor<?xf32, 7 : i64>
// CHECK: %[[MEMREF_ADDRSPACE7:.*]] = bufferization.to_memref
// CHECK-SAME: %[[TENSOR]] : tensor<?xf32, 7 : i64> to memref<?xf32, 7>
// CHECK: return %[[MEMREF_ADDRSPACE7]]
func.func @no_fold_buffer_cast_of_tensor_load(%arg0: memref<?xf32, 2>)
// CHECK-NOT: bufferization.to_tensor
// CHECK-NOT: bufferization.to_memref
// CHECK: %[[C0:.*]] = arith.constant 0 : index
// CHECK: %[[DIM:.*]] = memref.dim %[[MEMREF_ADDRSPACE2]], %[[C0]] : memref<?xf32, 2>
// CHECK: %[[MEMREF_ADDRSPACE7:.*]] = memref.alloc(%[[DIM]]) : memref<?xf32, 7>
// CHECK: memref.copy %[[MEMREF_ADDRSPACE2]], %[[MEMREF_ADDRSPACE7]]
// CHECK-SAME: memref<?xf32, 2> to memref<?xf32, 7>
// CHECK: return %[[MEMREF_ADDRSPACE7]]
func.func @canonicalize_buffer_cast_of_tensor_load_different_address_space(%arg0: memref<?xf32, 2>)
-> memref<?xf32, 7> {
%0 = bufferization.to_tensor %arg0 : memref<?xf32, 2> to tensor<?xf32, 7>
%1 = bufferization.to_memref %0 : tensor<?xf32, 7> to memref<?xf32, 7>
Expand Down