This is an archive of the discontinued LLVM Phabricator instance.

[mlir][bufferize] Fix tensor copy insertion for dynamic tensors
ClosedPublic

Authored by springerm on Dec 21 2022, 3:40 AM.

Details

Summary

TensorCopyInsertion inserts bufferization.alloc_tensor ops in case of RaW conflicts. If such a tensor is dynamically shaped, tensor.dim ops are inserted. There is an optimization for ops such as tensor.extract_slice: A copy of the result is created instead of the operand. Afterwards, all uses of the result are updated. E.g.:

%0 = tensor.extract_slice ... : tensor<?xf32> to tensor<?xf32>
%1 = tensor.dim %0, %c0 : tensor<?xf32>
%2 = bufferization.alloc_tensor(%dim) : tensor<?xf32>

All uses of %0, except for tensor.dim and bufferization.alloc_tensor (if any), should be replaced. Before this change, the use in tensor.dim was also replaced, resulting in IR that had a dominance error.

Note: There is no test case for this fix because the bug cannot be triggered with tensor.extract_slice, which implements an interface to reify result shapes. This bug appeared in an external project with a tensor.extract_slice-like op that does not implement that interface, in which case tensor.dim ops must be created. We do not have such an op in MLIR to trigger this bug.

Diff Detail

Event Timeline

springerm created this revision.Dec 21 2022, 3:40 AM
springerm requested review of this revision.Dec 21 2022, 3:40 AM
pifon2a accepted this revision.Dec 21 2022, 3:41 AM
This revision is now accepted and ready to land.Dec 21 2022, 3:41 AM
This revision was landed with ongoing or failed builds.Dec 21 2022, 3:47 AM
This revision was automatically updated to reflect the committed changes.