[Mlir-commits] [mlir] Fix bug in `visitDivExpr` and `visitModExpr` (PR #145290)
Arnab Dutta
llvmlistbot at llvm.org
Mon Jun 23 01:41:46 PDT 2025
https://github.com/arnab-polymage updated https://github.com/llvm/llvm-project/pull/145290
>From 7c184d5d307f0bc194e62ae10e60dd8af0ef3841 Mon Sep 17 00:00:00 2001
From: Arnab Dutta <arnab at polymagelabs.com>
Date: Mon, 23 Jun 2025 14:11:17 +0530
Subject: [PATCH] Fix bug in `visitDivExpr` and `visitModExpr`
Whenever the result of a div or mod affine expression is a
constant expression, place the value in the constant index of the
flattened expression instead of adding it as a local expression.
---
mlir/lib/IR/AffineExpr.cpp | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/mlir/lib/IR/AffineExpr.cpp b/mlir/lib/IR/AffineExpr.cpp
index cc81f9d19aca7..455563f62c3aa 100644
--- a/mlir/lib/IR/AffineExpr.cpp
+++ b/mlir/lib/IR/AffineExpr.cpp
@@ -1178,9 +1178,9 @@ static AffineExpr getSemiAffineExprFromFlatForm(ArrayRef<int64_t> flatExprs,
continue;
AffineExpr expr = it.value();
auto binaryExpr = dyn_cast<AffineBinaryOpExpr>(expr);
- if (!binaryExpr)
- continue;
-
+ assert(binaryExpr &&
+ "local expression cannot be a dimension, symbol or a constant -- it "
+ "should be a binary op expression");
AffineExpr lhs = binaryExpr.getLHS();
AffineExpr rhs = binaryExpr.getRHS();
if (!((isa<AffineDimExpr>(lhs) || isa<AffineSymbolExpr>(lhs)) &&
@@ -1348,6 +1348,11 @@ LogicalResult SimpleAffineExprFlattener::visitModExpr(AffineBinaryOpExpr expr) {
AffineExpr divisorExpr = getAffineExprFromFlatForm(rhs, numDims, numSymbols,
localExprs, context);
AffineExpr modExpr = dividendExpr % divisorExpr;
+ if (auto constModExpr = dyn_cast<AffineConstantExpr>(modExpr)) {
+ std::fill(lhs.begin(), lhs.end(), 0);
+ lhs[getConstantIndex()] = constModExpr.getValue();
+ return success();
+ }
return addLocalVariableSemiAffine(modLhs, rhs, modExpr, lhs, lhs.size());
}
@@ -1482,6 +1487,11 @@ LogicalResult SimpleAffineExprFlattener::visitDivExpr(AffineBinaryOpExpr expr,
AffineExpr b = getAffineExprFromFlatForm(rhs, numDims, numSymbols,
localExprs, context);
AffineExpr divExpr = isCeil ? a.ceilDiv(b) : a.floorDiv(b);
+ if (auto constDivExpr = dyn_cast<AffineConstantExpr>(divExpr)) {
+ std::fill(lhs.begin(), lhs.end(), 0);
+ lhs[getConstantIndex()] = constDivExpr.getValue();
+ return success();
+ }
return addLocalVariableSemiAffine(divLhs, rhs, divExpr, lhs, lhs.size());
}
More information about the Mlir-commits
mailing list