This is an archive of the discontinued LLVM Phabricator instance.

[mlir][ExpandStridedMetadata] Handle collapse_shape of dim of size 1 gracefully
ClosedPublic

Authored by qcolombet on Dec 5 2022, 7:36 AM.

Details

Summary

Collapsing dimensions of size 1 with random strides (a.k.a. non-contiguous w.r.t. collapsed dimensions) is a grey area that we'd like to clean-up. (See https://reviews.llvm.org/D136483#3909856)

That said, the implementation in memref-to-llvm currently skips dimensions of size 1 when computing the stride of a group.

While longer term we may want to clean that up, for now matches this behavior, at least in the static case.

For the dynamic case, for this patch we stick to min(group strides). However, if we want to handle the dynamic cases correctly while allowing non-truly-contiguous dynamic size of 1, we would need to if-then-else every dynamic size. In other words min(stride_i, for all i in group and dim_i != 1).

I didn't implement that in this patch at the moment since memref-to-llvm is technically broken in the general case for this. (It currently would only produce something sensible for row major tensors.)

Diff Detail

Event Timeline

qcolombet created this revision.Dec 5 2022, 7:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 5 2022, 7:36 AM
qcolombet requested review of this revision.Dec 5 2022, 7:36 AM
qcolombet added inline comments.Dec 5 2022, 7:48 AM
mlir/lib/Dialect/MemRef/Transforms/ExpandStridedMetadata.cpp
461

If we need to support the dynamic case with "non-truly-contiguous" dimensions of size 1, we could do a quick and dirty fix here where we could generate:

%stride_i = size_i != 1? origStride_i: int_max
%resulting_stride_i = min(stride_j for j in reassociation group)

The codegen won't be pretty, but it would be correct in the general case. I just don't know if it's worth it right now.
For the static case, it definitely worth it, because we see it happening in real workloads.

@springerm should be able to tell us if we need to support the dynamic case short term with the asserts he is adding for the dynamic cases.

Alternatively, @bkramer do you know if we need to support the dynamic case right now?

qcolombet updated this revision to Diff 480408.Dec 6 2022, 2:47 AM
  • When dealing with 1x1x...x1 groups, even though the stride is meaningless, make sure we make the type system happy by feeding it the stride it expects
jreiffers added inline comments.Dec 6 2022, 6:18 AM
mlir/lib/Dialect/MemRef/Transforms/ExpandStridedMetadata.cpp
455–461

This pattern is repeated multiple times in this file - maybe introduce a function returning a SmallVector<int64_t> and handling the assertion to make the call sites easier to read?

467–468

Does this assertion do anything useful here?

473

If you return here, you can drop the condition below.

481

Or maybe just do this now?

Thanks @jreiffers for the review!

mlir/lib/Dialect/MemRef/Transforms/ExpandStridedMetadata.cpp
455–461

Good idea!

467–468

That's debatable.
Essentially I wanted to make clear that this code is not intended to be used in other cases and futur proof a potential refactoring.

Basically, comments are easy to miss, asserts have more teeth :).

473

Let me play a bit with the code layout.

What I liked with the current solution was the assert below, but I can turn this into an unreachable if we early return here.

481

I feel that this should be its own PR.

In the meantime, I could drop this specialization and temporarily generate min(a), that'll probably be clearer.

What do you think?

Did you forget to upload your new version?

qcolombet updated this revision to Diff 480864.Dec 7 2022, 5:17 AM
  • Simplify the control flow with early return
  • Don't specialize min(a) => a, this should be done as an improvement of the affine.min folding.
qcolombet marked 2 inline comments as done.Dec 7 2022, 5:19 AM

Did you forget to upload your new version?

At the time no :).
I was just replying to your feedbacks.

Here is a new version.

thanks for providing this stopgap fix until we have time to properly think this one through!

jreiffers accepted this revision.Dec 7 2022, 10:34 PM

Thanks!

This revision is now accepted and ready to land.Dec 7 2022, 10:34 PM
qcolombet added inline comments.Dec 8 2022, 6:43 AM
mlir/lib/Dialect/MemRef/Transforms/ExpandStridedMetadata.cpp
481

FYI, I filed https://github.com/llvm/llvm-project/issues/59399 for the missing folding pattern min(a) -> a in affine.

This is not a problem for codegen in practice since the lowering will properly propagate the one value involved in this expression, but that would be a nice clean-up nonetheless.