[Mlir-commits] [mlir] [MLIR] Add trivial simplifications for affine mod, div, ceil (PR #182234)
Uday Bondhugula
llvmlistbot at llvm.org
Thu Feb 19 04:16:18 PST 2026
================
@@ -912,13 +912,17 @@ AffineExpr AffineExpr::operator-(AffineExpr other) const {
}
static AffineExpr simplifyFloorDiv(AffineExpr lhs, AffineExpr rhs) {
- auto lhsConst = dyn_cast<AffineConstantExpr>(lhs);
auto rhsConst = dyn_cast<AffineConstantExpr>(rhs);
+ // For the defined cases, simplify x floordiv x is 1.
+ if (lhs == rhs && (!rhsConst || rhsConst.getValue() >= 1))
----------------
bondhugula wrote:
MLIR affine floordiv, ceildiv, and mod expression are defined only for positive RHS/divisors. It's undefined behavior otherwise. Here's the spec: https://mlir.llvm.org/docs/Dialects/Affine/#affine-expressions
There's rationale for it. So whenever the RHS < 1 (known to be so), we leave the expression as is.
At runtime, if it turns out to be zero or negative, it again falls into the territory of undefined behavior and it wouldn't be wrong to have done this simplification at compile-time.
https://github.com/llvm/llvm-project/pull/182234
More information about the Mlir-commits
mailing list