[MLIR][Affine] Simplify nested modulo operations when able
authorKrzysztof Drewniak <Krzysztof.Drewniak@amd.com>
Thu, 16 Sep 2021 21:25:20 +0000 (21:25 +0000)
committerKrzysztof Drewniak <Krzysztof.Drewniak@amd.com>
Fri, 17 Sep 2021 19:06:00 +0000 (19:06 +0000)
commit121aab84d16f659cea539becff2cc2fef82ec152
tree53732c6916855179e1b8efbd58dc7ecf31ccdb32
parent08f0cb77197dc2842baa00f22f0264fa49d1475a
[MLIR][Affine] Simplify nested modulo operations when able

It is the case that, for all positive a and b such that b divides a
(e mod (a * b)) mod b = e mod b. For example, ((d0 mod 35) mod 5) can
be simplified to (d0 mod 5), but ((d0 mod 35) mod 4) cannot be simplified
further (x = 36 is a counterexample).

This change enables more complex simplifications. For example,
((d0 * 72 + d1) mod 144) mod 9 can now simplify to (d0 * 72 + d1) mod 9
and thus to d1 mod 9. Expressions with chained modulus operators are
reasonably common in tensor applications, and this change _should_
improve code generation for such expressions.

Reviewed By: nicolasvasilache

Differential Revision: https://reviews.llvm.org/D109930
mlir/lib/IR/AffineExpr.cpp
mlir/test/IR/affine-map.mlir
mlir/test/Transforms/loop-fusion-2.mlir
mlir/test/Transforms/loop-fusion.mlir