This is an archive of the discontinued LLVM Phabricator instance.

[mlir][Linalg] Reimplement and extend getStridesAndOffset
ClosedPublic

Authored by nicolasvasilache on Jan 2 2020, 12:25 PM.

Details

Summary

This diff reimplements getStridesAndOffset in a significantly simpler way by operating on the AffineExpr and calling into simplifyAffineExpr instead of rolling its own saturating arithmetic.

As a consequence it becomes quite simple to extend the behavior of getStridesAndOffset to encompass more cases by manipulating the AffineExpr more directly.
The divisions are still filtered out and continue to yield fully dynamic strides.
Simplifying the divisions is left for a later time if compelling use cases arise.

Relevant tests are added.

Diff Detail

Event Timeline

Add calls to simplifyAffineExpr.

Unit tests: pass. 61127 tests passed, 0 failed and 728 were skipped.

clang-tidy: fail. Please fix clang-tidy findings.

clang-format: pass.

Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml

Unit tests: pass. 61175 tests passed, 0 failed and 729 were skipped.

clang-tidy: fail. Please fix clang-tidy findings.

clang-format: pass.

Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml

Do you have an estimate on the impact of doing this? If I recall correctly, simplifyAffineExpr is kind of expensive/heavy.

@rriddle I am not concerned because the heavy cases involve the introduction of local variables that come from divisions/modulo and FourierMotzkin.
Concretely I do not see a measurable effect when running the tests.

There are 2 additional orthogonal aspects:

  1. I proposed in the past that we have a very strict type layering of AffineExpr to prevent people from accidentally pulling in expensive computations silently. This was rejected at the time but is probably due for a refresh now.
  2. Since we're uniquing properly, at some point it will make sense to cache the a map<AffineExpr, simplied AffineExpr>, I had done some measurements more than a year ago and a good 75% of invocations were redundant.

These are low-pri for me to the overall trajectory but if they become problematic they should of course be addressed.

ftynse requested changes to this revision.Jan 3 2020, 12:28 AM
ftynse added inline comments.
mlir/lib/IR/StandardTypes.cpp
472

If this dim hasn't been seen yet, can strides[dim] be something other than zero? If not, just assign 1 to it.

476

This looks like it could accept a hitherto non-canonical form of offset with multiple symbols -- N+M+K+42 -- and treat it as dynamic offset. Is this intentional? If so, please document. Otherwise, there may be a check on seenOffset missing somewhere.

542

(maybe later) I'd consider extracting a utility function from makeCanonicalStridedLayoutExpr that gives you strides as affine expressions, and reusing it here for immediate return instead of doing rounds of simplification on the expression you already know constructed from strides.

545

There's already a check in line 530

560

There's a check that it has exactly one element in line 530

564

There's a check above already

This revision now requires changes to proceed.Jan 3 2020, 12:28 AM
nicolasvasilache marked 7 inline comments as done.

Simplifying further by dropping seen and failed.
Addressing review comments.

nicolasvasilache marked 3 inline comments as done.Jan 3 2020, 7:55 AM
nicolasvasilache added inline comments.
mlir/lib/IR/StandardTypes.cpp
472

Now that I accumulate AffineExpr symbolically, the assertion on seen is incorrect, thanks!
In fact I can remove seen everywhere and just check against 0 in the end.

476

Yes it is intentional: previously as I was doing my own saturation arithmetic I needed to special case.
Now I just compute the symbolic expr and simplify in the end.

542

This was already almost the case.
I pushed simplification a bit further and got to what you suggested.

560

It can be empty.

nicolasvasilache marked an inline comment as done.Jan 3 2020, 7:56 AM

Unit tests: pass. 61175 tests passed, 0 failed and 729 were skipped.

clang-tidy: fail. Please fix clang-tidy findings.

clang-format: pass.

Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml

ftynse added inline comments.Jan 3 2020, 8:44 AM
mlir/lib/IR/StandardTypes.cpp
476

Then I wouildn't call this diff "NFC", and rather add tests and user-visible documentation for these new cases. IMO, it deserves mentioning that (N+M+K + N*i0 + M*i1 - K*i1) gets stored as three values that are _not_ N,M,K.

481

This never fails, maybe just keep void as return type?

562

Nit: I remember writing code that drops identity maps from the layout, if it's still there, we shouldn't see them here. I'm fine with this code in any case.

rriddle added inline comments.Jan 3 2020, 9:48 AM
mlir/lib/IR/StandardTypes.cpp
578

failed

nicolasvasilache marked 6 inline comments as done.

Address review comments, extend the behavior and add relevant tests.

mlir/lib/IR/StandardTypes.cpp
476

Ok then .. benhavior extend and tests added.

562

ah cool, I'll update to assertions everywhere.

Delete spurious commented out code and include.

Unit tests: pass. 61236 tests passed, 0 failed and 729 were skipped.

clang-tidy: fail. Please fix clang-tidy findings.

clang-format: pass.

Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml

Unit tests: pass. 61236 tests passed, 0 failed and 729 were skipped.

clang-tidy: fail. Please fix clang-tidy findings.

clang-format: pass.

Build artifacts: diff.json, clang-tidy.txt, clang-format.patch, CMakeCache.txt, console-log.txt, test-results.xml

nicolasvasilache retitled this revision from [mlir][Linalg] NFC - Reimplement getStridesAndOffset to [mlir][Linalg] Reimplement and extend getStridesAndOffset.Jan 3 2020, 3:13 PM
nicolasvasilache edited the summary of this revision. (Show Details)
ftynse accepted this revision.Jan 6 2020, 1:08 AM

Thanks!

This revision is now accepted and ready to land.Jan 6 2020, 1:08 AM
This revision was automatically updated to reflect the committed changes.