diff --git a/mlir/lib/Dialect/Tensor/Transforms/BufferizableOpInterfaceImpl.cpp b/mlir/lib/Dialect/Tensor/Transforms/BufferizableOpInterfaceImpl.cpp --- a/mlir/lib/Dialect/Tensor/Transforms/BufferizableOpInterfaceImpl.cpp +++ b/mlir/lib/Dialect/Tensor/Transforms/BufferizableOpInterfaceImpl.cpp @@ -205,7 +205,8 @@ tensor::DimOp> { bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand, const AnalysisState &state) const { - return true; + // The op reads the tensor's metadata but not its contents. + return false; } bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand, @@ -927,7 +928,8 @@ tensor::RankOp> { bool bufferizesToMemoryRead(Operation *op, OpOperand &opOperand, const AnalysisState &state) const { - return true; + // The op reads the tensor's metadata but not its contents. + return false; } bool bufferizesToMemoryWrite(Operation *op, OpOperand &opOperand, diff --git a/mlir/test/Dialect/Tensor/one-shot-bufferize.mlir b/mlir/test/Dialect/Tensor/one-shot-bufferize.mlir --- a/mlir/test/Dialect/Tensor/one-shot-bufferize.mlir +++ b/mlir/test/Dialect/Tensor/one-shot-bufferize.mlir @@ -330,3 +330,20 @@ %2 = tensor.insert_slice %b into %t[0][10][1] : tensor<10xf32> into tensor<10xf32> return %2 : tensor<10xf32> } + +// ----- + +// CHECK-LABEL: func @dim_not_reading( +// CHECK-SAME: %[[t:.*]]: memref, %f: f32, %pos: index) + -> (tensor, index) +{ + %c0 = arith.constant 0 : index + // CHECK-NOT: memref.alloc + // CHECK-NOT: memref.copy + // CHECK: memref.store %{{.*}}, %[[t]] + %0 = tensor.insert %f into %t[%pos] : tensor + // CHECK: memref.dim %[[t]] + %1 = tensor.dim %t, %c0 : tensor + return %0, %1 : tensor, index +}