This is an archive of the discontinued LLVM Phabricator instance.

Simplify affine bound min or max with operands
ClosedPublic

Authored by bondhugula on Apr 22 2023, 7:32 PM.

Details

Summary

Add canonicalization for affine.for bounds to use operand info (when
operands are outer loop affine.for IVs) to simplify bounds: redundant
bound expressions are eliminated in specific cases that are easy to
check and well-suited for op canonicaliation. If the lowest or the
highest value the affine expression can take is already covered by other
constant bounds, the expression can be removed.

Eg:
min (d0) -> (32 * d0 + 32, 32)(%i) where 0 <= %i < 2

The first expression can't be less than 32 and can be simplified away
with a lightweight local rewrite.

This simplification being part of canonicalization only handles simple
expressions, specifically, a sum of products of operands with constants.
This is a very common and a dominant case where such simplification is
desired. These can be flattened without any local variables.

Diff Detail

Event Timeline

bondhugula created this revision.Apr 22 2023, 7:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 22 2023, 7:32 PM
bondhugula requested review of this revision.Apr 22 2023, 7:32 PM
bondhugula added inline comments.Apr 22 2023, 7:34 PM
mlir/lib/Dialect/Affine/IR/AffineOps.cpp
942

We'll later move utilities like these to an AffineIRUtils library to make things more modular and prevent this file from getting longer.

bondhugula edited the summary of this revision. (Show Details)Apr 22 2023, 11:04 PM
mehdi_amini added inline comments.Apr 23 2023, 4:00 AM
mlir/lib/Dialect/Affine/IR/AffineOps.cpp
943

operands does not seem modified in this method: ArrayRef<Value> ?

bondhugula marked an inline comment as done.

ArrayRef for input.

bondhugula added inline comments.Apr 23 2023, 5:41 AM
mlir/lib/Dialect/Affine/IR/AffineOps.cpp
943

Thanks.

springerm accepted this revision.Apr 23 2023, 7:52 PM
springerm added inline comments.
mlir/lib/Dialect/Affine/IR/AffineOps.cpp
951

I think we're moving to std::optional everywhere.

989

nit: Not really needed but I would put a * before this, otherwise it looks like a typo.

This revision is now accepted and ready to land.Apr 23 2023, 7:52 PM
dcaballe accepted this revision.Apr 24 2023, 8:40 AM

LGTM, thanks!

mlir/test/Dialect/Affine/canonicalize.mlir
1386

Split this test into smaller tests to facilitate debugging?

bondhugula marked 3 inline comments as done.

Address review comments.

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

Thanks, updated.

mlir/test/Dialect/Affine/canonicalize.mlir
1386

Sure, done.

This revision was landed with ongoing or failed builds.Apr 28 2023, 6:50 PM
This revision was automatically updated to reflect the committed changes.