[Mlir-commits] [mlir] Fix bug in `visitDivExpr` and `visitModExpr` (PR #145290)

Uday Bondhugula llvmlistbot at llvm.org
Mon Jun 23 02:03:01 PDT 2025


================
@@ -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();
+    }
----------------
bondhugula wrote:

This change is incomplete, unnecessarily special-cased, and still leaves the code in a wrong state. The fix is clear though. You should be adding modExpr (or `divExpr` below) through `addLocalVariableSemiAffine` only when they are respectively those binary mod or div expressions. You are instead checking if they are constant instead of checking whether they are binary op exprs of the right kind. For example, it's completely possible they are a dim or symbol expression or another binary op expression because affine expression construction involves folding and simplification by construction, say 2 * (d0 + d1) / 2 may fold to (d0 + d1).

https://github.com/llvm/llvm-project/pull/145290


More information about the Mlir-commits mailing list