This is an archive of the discontinued LLVM Phabricator instance.

[mlir][affine] Add affine.min / affine.max canonicalization.
ClosedPublic

Authored by gysit on Mar 16 2022, 9:25 AM.

Details

Summary

The revision introduces a affine.min and affine.max canonicalization pattern that orders the result expressions. It flattens the result expressions to arrays of dimension and symbol coefficients plus one constant coefficient and rearranges them in lexicographic order.

Without the pattern, CSE will not eliminate two affine.min / affine.max operation if the results are ordered differently. For example, the operations

%1 = affine.min affine_map<(d0) -> (8, -d0 + 27)>(%arg4)
%2 = affine.min affine_map<(d0) -> (-d0 + 27, 8)>(%arg4)

doe not CSE. After applying the pattern, the two operations are equivalent

%1 = affine.min affine_map<(d0) -> (8, -d0 + 27)>(%arg4)
%2 = affine.min affine_map<(d0) -> (8, -d0 + 27)>(%arg4)

which enables CSE.

Diff Detail

Event Timeline

gysit created this revision.Mar 16 2022, 9:25 AM
gysit requested review of this revision.Mar 16 2022, 9:25 AM
bondhugula added inline comments.Mar 18 2022, 5:16 AM
mlir/lib/Dialect/Affine/IR/AffineOps.cpp
2722

Nit: Missing operands; op is ill-formed.

2726

Likewise.

2735–2770

You can pull this logic out into a separate method like:

static LogicalResult canonicalizeMapExprAndTermOrder(AffineMap &map);

If it returns success, map is updated to the reordered form.

nicolasvasilache accepted this revision.Mar 18 2022, 9:47 AM

LGTM once all comments addressed, thanks!

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

The affine map is multi-"result" but the min/max operations themselves are single result.
Could we maybe rename as CanonicalizeAffineMinMaxOpExpressions or something a little less ambiguous.

mlir/test/Dialect/Affine/canonicalize.mlir
1095

Can we change the order in this test to be a different order? e.g. (s0 + s1, 32, s1 + 16)

This revision is now accepted and ready to land.Mar 18 2022, 9:47 AM
gysit updated this revision to Diff 416848.Mar 21 2022, 1:35 AM
gysit marked 5 inline comments as done.

Address comments.