This is an archive of the discontinued LLVM Phabricator instance.

[mlir][Linalg] Reimplement hoisting on tensors as a subset-based transformation
ClosedPublic

Authored by nicolasvasilache on Feb 23 2023, 10:01 AM.

Details

Summary

This revision significantly rewrites hoisting on tensors.
Previously, vector.transfer_read/write and tensor.extract/insert_slice would
be clumped together when looking for candidate pairs.
This would significantly increase the complexity of the logic and would not apply
independently to tensor.extract/insert_slice.

The new implementation decouples the cases and starts to cast the problem
as a generic matching subset extract/insert, which will be future proof when
other such operation pairs are introduced.

Lastly, the implementation makes the distinction clear between vector.transfer_read/write for
which we allow bypasses of the disjoint subsets from tensor.extract/insert_slice for which we
do not yet allow it.

This can be extended in the future and unified once we have subset disjunction implemented more generally.

The algorithm can be rewritten to be less of a fixed point with interspersed canonicalizations.
As a consequence, the test explicitly adds a canonicalization to clean up the IR and verify we end up in the same state.

That extra canonicalization exhibited that one of the uses in one of the tests was dead, so we fix the appropriate test.

Diff Detail

Event Timeline

nicolasvasilache requested review of this revision.Feb 23 2023, 10:01 AM
ThomasRaoux accepted this revision.Feb 23 2023, 2:39 PM
ThomasRaoux added a subscriber: ThomasRaoux.
ThomasRaoux added inline comments.
mlir/include/mlir/Dialect/Linalg/Transforms/Hoisting.h
35–37

The other implementation won't work on buffers right? I don't think we can retire that until we have an alternative.

This revision is now accepted and ready to land.Feb 23 2023, 2:39 PM
nicolasvasilache edited the summary of this revision. (Show Details)

Separate loop canonicalization out, fix bugs and tests.

Split out transform control and better document limitation of the buffer version.

springerm added inline comments.Feb 24 2023, 1:47 AM
mlir/include/mlir/Dialect/Linalg/Transforms/Hoisting.h
40–140

"redundant" sounds like they would fold away.

43

Will this break if the loop yields two iter_args in a different order?

46

It's a bit unclear what a WAW conflict on tensors is.

51

An example would be nice here.

52

nit: returned

mlir/lib/Dialect/Linalg/Transforms/SubsetHoisting.cpp
38

I think you can use ViewLikeInterface::isSameAs.

54

Note: Does not capture cases where the operand is an affine.apply etc. that's defined inside the loop but does not use the IV.

76

Should this be called findHoistableMatchingExtractSlice?

springerm added inline comments.Feb 24 2023, 1:54 AM
mlir/lib/Dialect/Linalg/Transforms/SubsetHoisting.cpp
76

It's not clear to me what this function is doing. In particular, isDefinedOutsideOfLoop is used in the function but loops are not mentioned in the description. An example could be useful.

86

Naming: The comment says

- The read is to the same subset that `tensor.insert_slice` writes.

So I expected something like SmallVector<Operation *> users(destTensor.getUsers());

nicolasvasilache marked 9 inline comments as done.

Address comments

mlir/include/mlir/Dialect/Linalg/Transforms/Hoisting.h
35–37

ack

It's still broken in the parallel case though.

40–140

They are redundant within the loop: out of N iterations of the loop, N-1 read/write are redundant.
It can be viewed as a form of folding: to 1 read/write outside of the loop,

43

The algorithm only considers bbArg and matching yield, if the input IR ping-pongs, it will not apply.

46

a WAW appears by virtue of reordering ops and using iter_args.
Dropping WAW though because it is indeed biased towards "memory".

mlir/lib/Dialect/Linalg/Transforms/SubsetHoisting.cpp
38

nice ,thanks !

54

It does not: this is the job of the classical loop hoisting that I am not planning on duplicating here.

nicolasvasilache marked 2 inline comments as done.

Addres.

mlir/lib/Dialect/Linalg/Transforms/SubsetHoisting.cpp
76

clarified

86

clarified

nicolasvasilache edited the summary of this revision. (Show Details)

Fix mistakenly disabled bypassing behavior.

This revision was landed with ongoing or failed builds.Feb 27 2023, 8:26 AM
This revision was automatically updated to reflect the committed changes.