From e44f405bb41b8acf8ef28c4e41bc06429e87f3c3 Mon Sep 17 00:00:00 2001 From: Matthias Springer Date: Thu, 16 Feb 2023 16:05:58 +0100 Subject: [PATCH] [mlir][bufferization] Fix bug in findValueInReverseUseDefChain `alwaysIncludeLeaves` was not respected by all code paths. Differential Revision: https://reviews.llvm.org/D144187 --- .../IR/BufferizableOpInterface.cpp | 3 ++- ...shot-bufferize-empty-tensor-elimination.mlir | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp b/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp index f25a8eb0cf6c..1bba60d06c57 100644 --- a/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp +++ b/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp @@ -505,7 +505,8 @@ llvm::SetVector AnalysisState::findValueInReverseUseDefChain( 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 index 753840572f4b..aa6e6a1dbe05 100644 --- 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 @@ func.func @eleminate_multiple_ops(%t: tensor {bufferization.buffer_layout %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