[mlir][bufferize] Treat certain aliasing-only uses like memory reads
authorMatthias Springer <springerm@google.com>
Fri, 14 Oct 2022 01:39:50 +0000 (10:39 +0900)
committerMatthias Springer <springerm@google.com>
Fri, 14 Oct 2022 01:40:45 +0000 (10:40 +0900)
commit1b99f3a224afcab4a08f3b5b32ece63aa22cae0c
tree3c01748735ed7c946282ccc70a1548dc5883603a
parente750c41ec1accc25f12e46fd64e52cd1495cfc58
[mlir][bufferize] Treat certain aliasing-only uses like memory reads

This fixes an issue in One-Shot Bufferize that could lead to missing buffer copies in the future. This bug can currently not be triggered because of the order in which ops are analyzed (always bottom-to-top). However, if we consider different traversal orders for the analysis in the future, this bug can cause subtle issues that are difficult to debug.

Example:
```
%0 = ...
%1 = tensor.insert ... into %0
%2 = tensor.extract_slice %0
tensor.extract %2[...]
```

In case of a top-to-bottom analysis of the above IR, the `tensor.insert` is analyzed before the `tensor.extract_slice`. In that case, the `tensor.insert` will bufferize in-place because %2 is not yet known to become an alias of %0 (and therefore causing a conflict).

With this change, the `tensor.insert` will bufferize out-of-place, regardless of the traversal order.

Differential Revision: https://reviews.llvm.org/D135049
mlir/include/mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h
mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td
mlir/lib/Dialect/Bufferization/Transforms/Bufferize.cpp
mlir/lib/Dialect/Bufferization/Transforms/OneShotAnalysis.cpp
mlir/test/Dialect/Bufferization/Transforms/one-shot-bufferize.mlir