This is an archive of the discontinued LLVM Phabricator instance.

[mlir][linalg] Add option to generate rank-reducing slices in DropUnitDims
ClosedPublic

Authored by springerm on Dec 13 2022, 8:52 AM.

Details

Summary

This change extends the ReplaceUnitExtents pattern so that users can choose between of two strategies for generating rank reductions:

  • CollapseShapeOp / ExpandShapeOp (was already implemented but code was cleaned up; default strategy)
  • rank-reducing ExtractSliceOp / InsertSliceOp

Also add helper functions to the memref dialect that we already have on the tensor dialect: getMixedSizes, createCanonicalRankReducingSubViewOp, rankReduceIfNeeded.

Diff Detail

Event Timeline

springerm created this revision.Dec 13 2022, 8:52 AM
springerm requested review of this revision.Dec 13 2022, 8:52 AM
qcolombet accepted this revision.Dec 14 2022, 4:45 AM

LGTM, nits inlined.

mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp
440

Nit: you can origOutput.getType().cast<RankedTensorType>(); and drop the assert.
Also you could just move the auto with cast from below, here.

mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
118

Should we make sure (e.g., save, move at the right place, restore, in this function) that the insertion point of the builder is properly set in a position that value dominates?

mlir/test/Dialect/Linalg/drop-unit-extent-dims.mlir
30

Why do we have an additional collapse_shape here?
(Additional as in: it wasn't tested before this patch.)

And related comment, I would suggest that we check that these collapses properly feed the related instructions.

79

We don't have the CHECK-SLICES for this one, is it expected?

This revision is now accepted and ready to land.Dec 14 2022, 4:45 AM
springerm marked an inline comment as done.Dec 14 2022, 5:00 AM
springerm added inline comments.
mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
118

Keeping this as is for now because it is a copy of tensor::getMixedSizes, which is also written like that (to keep the functions in sync). But in general, we may want to do that in helper functions like this one. We need a few additional helpers for setting the insertion point (value can be an OpResult or a BbArg), so let's do that in a separate change.

mlir/test/Dialect/Linalg/drop-unit-extent-dims.mlir
30

The collapse_shape was already generated before. But the writer of this test forgot to add a CHECK. We can omit the src operand here because based on the reassociation indices only %shape is a valid argument and %arg0 or %arg1 would not pass the CollapseShapeOp verifier.

springerm marked an inline comment as done.Dec 14 2022, 5:12 AM
springerm added inline comments.
mlir/test/Dialect/Linalg/drop-unit-extent-dims.mlir
79

I just added it for two test cases: One with tensors, one with memrefs. There are many other test cases in this file for which I did not add it because in both cases (reshapes or slices strategy), the same code is executed except for when the CollapseShapeOp/ExpandShapeOp is generated. So we don't get any more test coverage.

qcolombet added inline comments.Dec 14 2022, 6:27 AM
mlir/test/Dialect/Linalg/drop-unit-extent-dims.mlir
79

Make sense.
Thanks!