This is an archive of the discontinued LLVM Phabricator instance.

[mlir][Linalg] Cleanup the drop unit dims pass in Linalg.
ClosedPublic

Authored by mravishankar on Jul 17 2023, 2:51 PM.

Details

Summary

TL;DR the following API functions have been merged

void populateFoldUnitExtentDimsViaReshapesPatterns(RewritePatternSet &patterns);
void populateFoldUnitExtentDimsViaSlicesPatterns(RewritePatternSet &patterns);

into

void populateFoldUnitExtentDimsPatterns(RewritePatternSet &patterns,
                                        ControlDropUnitDims &options);

To use the previous functionality use

ControlDropUnitDims options;
// By default options.rankReductionStrategy is
// ControlDropUnitDims::RankReductionStrategy::ReassociativeReshape.
populateFoldUnitExtentDimsPatterns(patterns, options);

and

ControlDropUnitDims options;
options.rankReductionStrategy = ControlDropUnitDims::RankReductionStrategy::ExtractInsertSlice
populateFoldUnitExtentDimsPatterns(patterns, options);

This pass is quite old and needed to be updated based on the current
approach to transformations in Linalg.

  • Instead of two patterns, one to just remove loop dimensions that are unit extent (and using 0 in the indexing maps), and another to drop the unit-extents in the operand shapes, combine into a single transformation. This avoid creating an intermediate step with indexing maps having 0's in the domains exp ressions.
  • Expose the core transformation as a utility function and add a pattern that calls this transformation.

This is a mostly NFC change, apart from the API change and dropping
the patterns/test that only dropped the loops that are unit extents.

Diff Detail

Event Timeline

mravishankar created this revision.Jul 17 2023, 2:51 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 17 2023, 2:51 PM
mravishankar requested review of this revision.Jul 17 2023, 2:51 PM
mravishankar edited the summary of this revision. (Show Details)Jul 17 2023, 3:00 PM
mravishankar added reviewers: springerm, qedawkins.
qedawkins requested changes to this revision.Jul 17 2023, 8:47 PM

Mostly looks good to me. Mainly a comment around the implementation of the control function.

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

nit: it looks like reassociation is not used for ExtractInsertSlice

281

ditto on the above; comment says the reverse of the code.

410

I do not see this filter used anywhere, and this is the only place I see the result of the control filter used. Can you add a test for a non-trivial control function to make sure it is able to affect the pattern?

412

nit: unused variable

635

Is there a reason not to allow an optional control function here (and below for Slices)?

This revision now requires changes to proceed.Jul 17 2023, 8:47 PM
mravishankar marked 3 inline comments as done.

Address comments.

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

Thanks for catching that!

410

Good catch. I missed this actually (but thats why its a draft). I added a test as well for the "control" case.

qedawkins accepted this revision.Jul 18 2023, 3:18 PM
qedawkins added inline comments.
mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp
410

Thanks! I did not realize it was a draft though, I thought the request for review could only happen after marking it as ready. Apologies if it was a premature review then.

This revision is now accepted and ready to land.Jul 18 2023, 3:18 PM
mravishankar added inline comments.Jul 18 2023, 4:12 PM
mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp
410

I might have pushed the ready for review button by mistake. No worries at all. I had the draft up for initial comments anyway.

mravishankar edited the summary of this revision. (Show Details)Jul 18 2023, 10:11 PM
mravishankar edited the summary of this revision. (Show Details)
mravishankar edited the summary of this revision. (Show Details)
mravishankar edited the summary of this revision. (Show Details)
mravishankar edited the summary of this revision. (Show Details)Jul 18 2023, 10:13 PM
This revision was landed with ongoing or failed builds.Jul 19 2023, 10:47 AM
This revision was automatically updated to reflect the committed changes.

It looks like this patch introduced a few warnings:

/Users/rik/git/llvm-project/mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp:365:14: warning: comparison of integers of different signs: 'int64_t' (aka 'long long') and 'size_t' (aka 'unsigned long') [-Wsign-compare]
  while (dim < operandShape.size() && isUnitDim(dim))
         ~~~ ^ ~~~~~~~~~~~~~~~~~~~
/Users/rik/git/llvm-project/mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp:367:14: warning: comparison of integers of different signs: 'int64_t' (aka 'long long') and 'size_t' (aka 'unsigned long') [-Wsign-compare]
  while (dim < operandShape.size()) {
         ~~~ ^ ~~~~~~~~~~~~~~~~~~~
/Users/rik/git/llvm-project/mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp:375:16: warning: comparison of integers of different signs: 'int64_t' (aka 'long long') and 'size_t' (aka 'unsigned long') [-Wsign-compare]
    while (dim < operandShape.size() && isUnitDim(dim)) {
           ~~~ ^ ~~~~~~~~~~~~~~~~~~~
/Users/rik/git/llvm-project/mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp:412:13: warning: unused variable 'iteratorTypes' [-Wunused-variable]
  ArrayAttr iteratorTypes = genericOp.getIteratorTypes();
            ^
4 warnings generated.
[2374/2535] Building CXX object tools/mlir/test/lib/D...MLIRLinalgTestPasses.dir/TestLinalgDropUnitDims.cpp.o
/Users/rik/git/llvm-project/mlir/test/lib/Dialect/Linalg/TestLinalgDropUnitDims.cpp:61:7: warning: ignoring return value of function declared with 'nodiscard' attribute [-Wunused-result]
      dropOutermostUnitDims(rewriter, genericOp);
      ^~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~
1 warning generated.

https://reviews.llvm.org/D155738 fixes it. Checking locally first before landing.

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