This is an archive of the discontinued LLVM Phabricator instance.

Support non identity layout map for reshape ops in MemRefToLLVM lowering
ClosedPublic

Authored by cathyzhyi on Apr 19 2022, 6:35 AM.

Details

Summary

This change borrows the ideas from computeExpanded/CollapsedLayoutMap
and computes the dynamic strides at runtime for the memref descriptors.

Diff Detail

Event Timeline

cathyzhyi created this revision.Apr 19 2022, 6:35 AM
Herald added a project: Restricted Project. · View Herald Transcript
cathyzhyi requested review of this revision.Apr 19 2022, 6:35 AM
cathyzhyi retitled this revision from Support non identity layout map for reshape ops in MemRefToLLVM lowering to WIP: Support non identity layout map for reshape ops in MemRefToLLVM lowering.Apr 19 2022, 6:37 AM
cathyzhyi updated this revision to Diff 423719.Apr 19 2022, 1:20 PM
  • Fix cases where all collapsed dims in an association are size 1.
  • Fix tests.
cathyzhyi edited the summary of this revision. (Show Details)Apr 19 2022, 2:05 PM
cathyzhyi retitled this revision from WIP: Support non identity layout map for reshape ops in MemRefToLLVM lowering to Support non identity layout map for reshape ops in MemRefToLLVM lowering.Apr 19 2022, 2:07 PM
cathyzhyi updated this revision to Diff 423738.Apr 19 2022, 2:27 PM

Fix testcases format

springerm requested changes to this revision.Apr 20 2022, 6:16 AM
springerm added inline comments.
mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
1352

calculated

mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
1825–1836 ↗(On Diff #423738)

Can you factor this part of the CL out into a separate one? Along with the bufferization test case.

1826–1827 ↗(On Diff #423738)

Good catch, there is definitely a problem here in the old implementation. The old implementation assumes that strides are sorted according to size. This is not always the case, however. E.g., you can use memref.transpose (which only touches the memref descriptor, not the data) to produce a new view on the data that is iterated over in a transposed fashion.

The check for 1 only solves this issue partial. Instead, we should be using the minimum stride in the current reassociation group. (The old code always used the last one.)

1828 ↗(On Diff #423738)

This is problematic because we would be failing silently at runtime with a wrong result. We cannot just return failure() unfortunately. We should do the same computation as described above, choosing the minimum stride in the reassociation group. We don't need a loop for this, just a sequence of min operations.

This revision now requires changes to proceed.Apr 20 2022, 6:16 AM
cathyzhyi added inline comments.Apr 20 2022, 10:15 AM
mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
1826–1827 ↗(On Diff #423738)

The checking for 1 here is need for non transposed cases when the strides are in order. When the innermost dim is of size 1 the result stride needs to take the smallest stride on the src dim that's not size 1. This is the case for the test case collapse_shape_of_slice3 I added. I realize the input of collpase_shape for this case would not be contiguous but it can still be represented by the strided form and is needed for our testcase. I can't think of a good way to detect this at runtime unless inserting runtime assertion to assume dynamic dims are not size 1. Do you have any suggestions on that?

springerm added inline comments.Apr 20 2022, 11:54 AM
mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
1826–1827 ↗(On Diff #423738)

We had a discussion and concluded that this handling of dims of size 1 is correct. More work is needed to handle cases where strides are not sorted. This can be done in separate change.

cathyzhyi added inline comments.Apr 20 2022, 6:11 PM
mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
1825–1836 ↗(On Diff #423738)

Moved this part to a new patch https://reviews.llvm.org/D124137.

cathyzhyi updated this revision to Diff 424069.Apr 20 2022, 6:20 PM

Factored out the part fixing size 1 cases into https://reviews.llvm.org/D124137.
The remaining memref to llvm lowering part needs to be updated to
take into consideration where the dynamic size is 1 as well.

cathyzhyi updated this revision to Diff 424684.EditedApr 22 2022, 7:36 PM

Calculate with the new logic at runtime. I got an error about dominance here https://gist.github.com/949dafb96801866cf067b5baba4ac614. It's complaining the last mem ref descriptor doesn't dominate the use tensor.collapse_shape but it seems to me it does dominate tensor.collapse_shape.

cathyzhyi retitled this revision from Support non identity layout map for reshape ops in MemRefToLLVM lowering to WIP: Support non identity layout map for reshape ops in MemRefToLLVM lowering.Apr 23 2022, 8:07 AM
cathyzhyi updated this revision to Diff 424809.Apr 24 2022, 7:38 PM

Fixed the previous error about dominance.

This looks good. Can you add another test case for collapse and expand, so that we have one for the static case (taking the stride directly from the src type) and one for the dynamic case (also checking that the blocks+branches are generated correctly).

mlir/include/mlir/Conversion/LLVMCommon/MemRefBuilder.h
82

Returns

mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
1369

calculated

1382

Add a TODO that we should take the minimum stride in the reassociation group instead of just the first where the dimension is not 1.

1517–1523

Add a comment that there could be mixed static/dynamic strides. For simplicity, we recompute all strides if there is at least one dynamic stride.

mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir
852

Why did this test case change? This commit is just adding new functionality for a previously not handled case, so existing test cases should not change.

springerm added inline comments.Apr 25 2022, 4:57 AM
mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
1412

should this be called "index type" maybe?

1419

I think there could be another small optimization here:

if (srcShape[srcIndex] == 1 && srcIndex != ref.front())
  continue;
cathyzhyi updated this revision to Diff 425029.Apr 25 2022, 2:22 PM
cathyzhyi marked 8 inline comments as done.

Address comments.

Thanks for the review!

mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir
852

yea, I should have kept the implementation for the identity layout case instead of just using the new logic. Changed back the test cases and added the identity layout logic.

cathyzhyi updated this revision to Diff 425030.Apr 25 2022, 2:28 PM

Move the comment about dynamic stride to a more propriate place

cathyzhyi retitled this revision from WIP: Support non identity layout map for reshape ops in MemRefToLLVM lowering to Support non identity layout map for reshape ops in MemRefToLLVM lowering.Apr 25 2022, 2:28 PM
springerm accepted this revision.Apr 26 2022, 1:16 AM

thanks!

This revision is now accepted and ready to land.Apr 26 2022, 1:16 AM