diff --git a/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp b/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp --- a/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp +++ b/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp @@ -505,7 +505,8 @@ if (followEquivalentOnly && a.relation != BufferRelation::Equivalent) { // Stop iterating if `followEquivalentOnly` is set but the alias is not // equivalent. - result.insert(value); + if (alwaysIncludeLeaves) + result.insert(value); } else { workingSet.insert(a.opOperand->get()); } diff --git a/mlir/test/Dialect/Bufferization/Transforms/one-shot-bufferize-empty-tensor-elimination.mlir b/mlir/test/Dialect/Bufferization/Transforms/one-shot-bufferize-empty-tensor-elimination.mlir --- a/mlir/test/Dialect/Bufferization/Transforms/one-shot-bufferize-empty-tensor-elimination.mlir +++ b/mlir/test/Dialect/Bufferization/Transforms/one-shot-bufferize-empty-tensor-elimination.mlir @@ -204,3 +204,20 @@ %r1 = tensor.insert_slice %if into %t[42][%sz][1]: tensor into tensor return %r1: tensor } + +// ----- + +// This is a regression test. Make sure that the tensor.extract_slice is not +// eliminated. + +// CHECK-LABEL: func.func @regression_do_not_eliminate_non_empty( +// CHECK: memref.subview +// CHECK: memref.subview +// CHECK: memref.copy +func.func @regression_do_not_eliminate_non_empty( + %t: tensor<10xf32>, %t2: tensor<10xf32>) -> tensor<10xf32> { + %1 = tensor.extract_slice %t[0] [5] [1] : tensor<10xf32> to tensor<5xf32> + %2 = tensor.insert_slice %1 into %t2[1] [5] [1] + : tensor<5xf32> into tensor<10xf32> + return %2 : tensor<10xf32> +} \ No newline at end of file