This is an archive of the discontinued LLVM Phabricator instance.

[mlir][MemRef] Simplify extract_strided_metadata(collapse_shape)
ClosedPublic

Authored by qcolombet on Sep 28 2022, 12:59 PM.

Details

Summary

The new pattern gets rid of the collapse_shape operation while materializing its effects on the sizes, and the strides of the base object.

In other words, this simplification replaces:

baseBuffer, offset, sizes, strides =
    extract_strided_metadata(collapse_shape(memref))

With

baseBuffer, offset, baseSizes, baseStrides =
    extract_strided_metadata(memref)
for reassDim in {0 .. collapseRank - 1}
  sizes#reassDim = product(baseSizes#i for i in group[reassDim])
  strides#reassDim = baseStrides[group[reassDim].back()]

Note: baseBuffer and offset are unaffected by the collapse_shape operation.

Diff Detail

Event Timeline

qcolombet created this revision.Sep 28 2022, 12:59 PM
qcolombet requested review of this revision.Sep 28 2022, 12:59 PM
qcolombet added inline comments.Sep 28 2022, 1:03 PM
mlir/lib/Dialect/MemRef/Transforms/SimplifyExtractStridedMetadata.cpp
181

Note: I can split the commit with the NFC part first. This would be one of the changes in that commit.

345

I thought I would need that more than once, but turned out I used it only once.
Let me know if you want that we get rid of it.

467–475

If you have a better name for this template type I'm taking!

In particular, this class is meant to deal with reshape ops with reassociation groups like collapse_shape and expand_shape. My problem with the name is that it could be confusing since we have memref.reshape and we don't support that here.

474

This is another change that could be part of an NFC commit first.
Essentially, we handle expand_shape and collapse_shape in the same way.

qcolombet added inline comments.Sep 28 2022, 1:08 PM
mlir/lib/Dialect/MemRef/Transforms/SimplifyExtractStridedMetadata.cpp
379

Technically for collapse shape we only return one size/stride per group. However since the overall simplification is handled through templates, we have to stick to the same return type as the expand_shape handling.

We could probably avoid that, but I expect it'll introduce more template boilderplate.

chelini added inline comments.Sep 29 2022, 6:30 AM
mlir/lib/Dialect/MemRef/Transforms/SimplifyExtractStridedMetadata.cpp
467–475
chelini added inline comments.Sep 29 2022, 7:52 AM
mlir/lib/Dialect/MemRef/Transforms/SimplifyExtractStridedMetadata.cpp
342

static ?

420

static ?

ftynse added a subscriber: ftynse.Sep 29 2022, 8:00 AM
ftynse added inline comments.
mlir/lib/Dialect/MemRef/Transforms/SimplifyExtractStridedMetadata.cpp
345

Nit: function_ref if the function isn't stored.

qcolombet updated this revision to Diff 463940.Sep 29 2022, 9:40 AM
  • Add missing static keywords
  • Rename ReshapeLikeOp into ReassociativeReshapeOp
  • Use function_ref
qcolombet marked 4 inline comments as done.Sep 29 2022, 9:41 AM
qcolombet added inline comments.
mlir/lib/Dialect/MemRef/Transforms/SimplifyExtractStridedMetadata.cpp
342

Good catch.
Done!

345

Good point!

467–475

Nice!

chelini accepted this revision.Sep 30 2022, 8:24 AM
chelini added inline comments.
mlir/lib/Dialect/MemRef/Transforms/SimplifyExtractStridedMetadata.cpp
487

re_shape -> reshape

This revision is now accepted and ready to land.Sep 30 2022, 8:24 AM

Did a cursory reading on code structure which looks fine to me, I am ok landing as is given other reviewers' accept.

This revision was automatically updated to reflect the committed changes.
qcolombet marked 3 inline comments as done.
qcolombet marked an inline comment as done.Sep 30 2022, 10:12 AM

Thanks for the review!