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
12 changes: 10 additions & 2 deletions mlir/lib/Dialect/Bufferization/IR/BufferizationOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,10 +805,18 @@ struct ToBufferOfCast : public OpRewritePattern<ToBufferOp> {
tensorCastOperand.getOperand().getType());
if (!srcTensorType)
return failure();
auto currentOutputMemRefType =
dyn_cast<MemRefType>(toBuffer.getResult().getType());
if (!currentOutputMemRefType)
Copy link
Member

Choose a reason for hiding this comment

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

I don't know what to make out of this, but it looks like some Tensorflow tests started failing due to this early return. If I change it to this code and remove the return failure(), the test passes again:

Type memrefType;
if (currentOutputMemRefType) {
  memrefType = MemRefType::get(srcTensorType.getShape(),
                               srcTensorType.getElementType(),
                               currentOutputMemRefType.getLayout(),
                               currentOutputMemRefType.getMemorySpace());
} else {
  memrefType = MemRefType::get(srcTensorType.getShape(),
                               srcTensorType.getElementType());
}

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if that's what's happening here, but it looks like this PR changes the pattern so that it no longer applies to IR such as:

%0 = tensor.cast %src : ranked -> unranked
%1 = bufferization.to_buffer %0

Should we be looking for BaseMemRefType here instead of MemRefType?

Copy link
Member

Choose a reason for hiding this comment

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

@matthias-springer Thanks for the hint, I tried it out and it works. We can use BaseMemRefType::cloneWith to generate the new MemRefType that will preserve layout and memory space if the output type is MemRefType. I will prepare a PR.

return failure();

auto memrefType = MemRefType::get(srcTensorType.getShape(),
srcTensorType.getElementType());
srcTensorType.getElementType(),
currentOutputMemRefType.getLayout(),
currentOutputMemRefType.getMemorySpace());
Value memref = ToBufferOp::create(rewriter, toBuffer.getLoc(), memrefType,
tensorCastOperand.getOperand());
tensorCastOperand.getOperand(),
toBuffer.getReadOnly());
rewriter.replaceOpWithNewOp<memref::CastOp>(toBuffer, toBuffer.getType(),
memref);
return success();
Expand Down
20 changes: 18 additions & 2 deletions mlir/test/Dialect/Bufferization/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,32 @@ func.func @clone_and_preceding_dealloc(%arg0: memref<?xf32>) -> memref<32xf32> {
func.func @tensor_cast_to_buffer(%arg0 : tensor<4x6x16x32xi8>) ->
memref<?x?x16x32xi8> {
%0 = tensor.cast %arg0 : tensor<4x6x16x32xi8> to tensor<?x?x16x32xi8>
%1 = bufferization.to_buffer %0 : tensor<?x?x16x32xi8> to memref<?x?x16x32xi8>
%1 = bufferization.to_buffer %0 read_only : tensor<?x?x16x32xi8> to memref<?x?x16x32xi8>
return %1 : memref<?x?x16x32xi8>
}
// CHECK: %[[M:.+]] = bufferization.to_buffer %[[ARG0]] : tensor<4x6x16x32xi8>
// CHECK: %[[M:.+]] = bufferization.to_buffer %[[ARG0]] read_only : tensor<4x6x16x32xi8>
// CHECK: %[[M1:.+]] = memref.cast %[[M]]
// CHECK-SAME: memref<4x6x16x32xi8> to memref<?x?x16x32xi8>
// CHECK: return %[[M1]] : memref<?x?x16x32xi8>

// -----

// CHECK-LABEL: func @tensor_cast_to_buffer
// CHECK-SAME: %[[ARG0:.+]]: tensor<4x6x16x32xi8>
func.func @tensor_cast_to_buffer_layout_and_memspace(%arg0 : tensor<4x6x16x32xi8>) ->
memref<?x?x16x32xi8, strided<[?, ?, ?, 1], offset: ?>, 1> {
%0 = tensor.cast %arg0 : tensor<4x6x16x32xi8> to tensor<?x?x16x32xi8>
%1 = bufferization.to_buffer %0 : tensor<?x?x16x32xi8> to memref<?x?x16x32xi8, strided<[?, ?, ?, 1], offset: ?>, 1>
return %1 : memref<?x?x16x32xi8, strided<[?, ?, ?, 1], offset: ?>, 1>
}
// CHECK: %[[M:.+]] = bufferization.to_buffer %[[ARG0]] : tensor<4x6x16x32xi8>
// CHECK: %[[M1:.+]] = memref.cast %[[M]]
// CHECK-SAME: memref<4x6x16x32xi8, strided<[?, ?, ?, 1], offset: ?>, 1>
// CHECK-SAME: to memref<?x?x16x32xi8, strided<[?, ?, ?, 1], offset: ?>, 1>
// CHECK: return %[[M1]] : memref<?x?x16x32xi8, strided<[?, ?, ?, 1], offset: ?>, 1>

// -----

// Folding of memref.load(to_buffer(%v, %idxs)) -> tensor.extract(%v, %idx)
// CHECK-LABEL: func @load_from_buffer_cast(
func.func @load_from_buffer_cast(%arg0: index, %arg1: index,
Expand Down