This is an archive of the discontinued LLVM Phabricator instance.

[MLIR] Simplify affine maps + operands exploiting IV info
ClosedPublic

Authored by bondhugula on Oct 3 2022, 9:37 AM.

Details

Summary

Simplify affine expressions and maps while exploiting simple range and
step info of any IVs that are operands. This simplification is local,
O(1) and practically useful in several scenarios. Accesses with
floordiv's and mod's where the LHS is non-negative and bounded or is a
known multiple of a constant can often be simplified. This is
implemented as a canonicalization for all affine ops in a generic way:
all affine.load/store, vector_load/store, affine.apply, affine.min/max,
etc. ops.

Eg: For tiled loop nests accessing buffers this way:

affine.for %i = 0 to 1024 step 32 {
  affine.for %ii = 0 to 32 {
    affine.load [(%i + %ii) floordiv 32, (%i + %ii) mod 32]
  }
}

// Note that %i is a multiple of 32 and %ii < 32, hence:
(%i + %ii) floordiv 32 here is the same as %i floordiv 32, and
(%i + %ii) mod 32 here is the same as %ii mod 32.

The simplification leads to simpler index/subscript arithmetic for
multi-dimensional arrays and also in turn enables detection of spatial
locality (for vectorization for eg.), temporal locality or loop
invariance for hoisting or scalar replacement.

Diff Detail

Event Timeline

bondhugula created this revision.Oct 3 2022, 9:37 AM
Herald added a project: Restricted Project. · View Herald Transcript
bondhugula requested review of this revision.Oct 3 2022, 9:37 AM
bondhugula edited the summary of this revision. (Show Details)Oct 3 2022, 9:39 AM
ftynse added inline comments.Oct 4 2022, 1:29 AM
mlir/lib/Dialect/Affine/IR/AffineOps.cpp
593

Nit: a word is missing here.

625

This doesn't seem to handle the non-negative part of condition.

647

Nit: the "lhsBin" name sounds confusing here, it isn't really a left-hand side of something, just "e" casted to AffineBinaryOpExpr.

654

Variable assignment inside a function argument inside a condition looks very difficult to parse for a human reader. Please hoist it into temporary variable that can be copied to div inside the branch, with branches following LLVM-style early return. The code is likely to end up shorter.

701–706

Nit: please add braces here and below in similar conditions. We slightly updated the rules for brace omissions and prefer using braces when either the condition or the body of a conditional doesn't fit into one line.

723

Is the attribute wrong here? The function seems to be used below. Otherwise, we shouldn't commit unused code.

726

Nit: drop the explicit number of stack elements from SmallVector unless there is a strong reason to pick one.

728

Nit: please expand auto unless the type is obvious from the RHS (e.g., ends with a cast), excessively long (e.g., iterators) or impossible to spell (lambdas).

bondhugula updated this revision to Diff 464939.Oct 4 2022, 3:42 AM
bondhugula marked 7 inline comments as done.

Address review comments.

Thanks for the review, addressed.

mlir/lib/Dialect/Affine/IR/AffineOps.cpp
625

Good, catch; thanks.

647

You are right - that was an artefact of some refactoring. Fixed.

654

Sure, done.

723

This is due to a weird artefact. The function is used, but due to the way the SimplifyAffineOp template is expanded, clang-tidy thinks that this function is unused and emits a warning. We've had to similarly attach this attribute to remainsLegalAfterInline as well.

bondhugula updated this revision to Diff 464940.Oct 4 2022, 3:44 AM

Add some more braces.

ftynse accepted this revision.Oct 4 2022, 4:04 AM
ftynse added inline comments.
mlir/lib/Dialect/Affine/IR/AffineOps.cpp
723

I see. Could you add a comment explaining that so future refactorings don't remove the attribute accidentally?

This revision is now accepted and ready to land.Oct 4 2022, 4:04 AM
bondhugula updated this revision to Diff 464979.Oct 4 2022, 5:38 AM
bondhugula marked 2 inline comments as done.

Add comment on unused attribute marker.

This revision was landed with ongoing or failed builds.Oct 4 2022, 5:51 AM
This revision was automatically updated to reflect the committed changes.