Skip to content

[mlir][IR] Fix bug in AffineExpr simplifier lhs % rhs where lhs = lhs floordiv rhs #119245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

christopherbate
Copy link
Contributor

Fixes an issue where the SimpleAffineExprFlattener would simplify
lhs % rhs to just -(lhs floordiv rhs) instead of lhs - (lhs floordiv rhs)
if lhs happened to be equal to lhs floordiv rhs.

The reported failure case was (d0, d1) -> (((d1 - (d1 + 2)) floordiv 8) % 8)
from #114654.

Note that many paths that simplify AffineMaps (e.g. the AffineApplyOp
folder and canonicalization) would not observe this bug because of
of slightly different paths taken by the code. Slightly different grouping
of the terms could also result in avoiding the bug. The way to reproduce was
by constructing the map directly, replacing d1 with 1 and calling
mlir::simplifyAffineExpr.

Resolves #114654.

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Dec 9, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 9, 2024

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: Christopher Bate (christopherbate)

Changes

Fixes an issue where the SimpleAffineExprFlattener would simplify
lhs % rhs to just -(lhs floordiv rhs) instead of lhs - (lhs floordiv rhs)
if lhs happened to be equal to lhs floordiv rhs.

The reported failure case was (d0, d1) -> (((d1 - (d1 + 2)) floordiv 8) % 8)
from #114654.

Note that many paths that simplify AffineMaps (e.g. the AffineApplyOp
folder and canonicalization) would not observe this bug because of
of slightly different paths taken by the code. Slightly different grouping
of the terms could also result in avoiding the bug. The way to reproduce was
by constructing the map directly, replacing d1 with 1 and calling
mlir::simplifyAffineExpr.

Resolves #114654.


Full diff: https://github.com/llvm/llvm-project/pull/119245.diff

2 Files Affected:

  • (modified) mlir/lib/IR/AffineExpr.cpp (+1-1)
  • (modified) mlir/unittests/IR/AffineExprTest.cpp (+21)
diff --git a/mlir/lib/IR/AffineExpr.cpp b/mlir/lib/IR/AffineExpr.cpp
index 2291d64c50a560..59df0cd6833db3 100644
--- a/mlir/lib/IR/AffineExpr.cpp
+++ b/mlir/lib/IR/AffineExpr.cpp
@@ -1385,7 +1385,7 @@ LogicalResult SimpleAffineExprFlattener::visitModExpr(AffineBinaryOpExpr expr) {
     lhs[getLocalVarStartIndex() + numLocals - 1] = -rhsConst;
   } else {
     // Reuse the existing local id.
-    lhs[getLocalVarStartIndex() + loc] = -rhsConst;
+    lhs[getLocalVarStartIndex() + loc] -= rhsConst;
   }
   return success();
 }
diff --git a/mlir/unittests/IR/AffineExprTest.cpp b/mlir/unittests/IR/AffineExprTest.cpp
index 9e89a5b79e2e2e..7005b2004267c8 100644
--- a/mlir/unittests/IR/AffineExprTest.cpp
+++ b/mlir/unittests/IR/AffineExprTest.cpp
@@ -129,3 +129,24 @@ TEST(AffineExprTest, d0PlusD0FloorDivNeg2) {
   auto sum = d0 + d0.floorDiv(-2) * 2;
   ASSERT_EQ(toString(sum), "d0 + (d0 floordiv -2) * 2");
 }
+
+TEST(AffineExprTEst, simpleAffineExprFlattenerRegression) {
+
+  // Regression test for a bug where mod simplification was not handled
+  // properly when `lhs % rhs` was happened to have the property that `lhs
+  // floordiv rhs = lhs`.
+  MLIRContext ctx;
+  OpBuilder b(&ctx);
+
+  auto d0 = b.getAffineDimExpr(0);
+  auto d1 = b.getAffineDimExpr(1);
+
+  // Manually replace variables by constants to avoid constant folding.
+  AffineExpr expr = (d0 - (d1 + 2)).floorDiv(8) % 8;
+  expr = expr.replaceDims(
+      {b.getAffineConstantExpr(1), b.getAffineConstantExpr(1)});
+  AffineExpr result = mlir::simplifyAffineExpr(expr, 2, 0);
+
+  ASSERT_TRUE(isa<AffineConstantExpr>(result));
+  ASSERT_EQ(cast<AffineConstantExpr>(result).getValue(), 7);
+}
\ No newline at end of file

Copy link
Member

@Groverkss Groverkss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, please fix the test before landing.

@christopherbate christopherbate force-pushed the mlir-affine-simplifier-fix branch from 2d94312 to 8440285 Compare December 12, 2024 17:14
…lhs floordiv rhs`

Fixes an issue where the `SimpleAffineExprFlattener` would simplify
`lhs % rhs` to just `-(lhs floordiv rhs)` instead of `lhs - (lhs floordiv rhs)`
if `lhs` happened to be equal to `lhs floordiv rhs`.

The reported failure case was `(d0, d1) -> (((d1 - (d1 + 2)) floordiv 8) % 8)`
from llvm#114654.

Note that many paths that simplify AffineMaps (e.g. the AffineApplyOp
folder and canonicalization) would not observe this bug because of
of slightly different paths taken by the code. Slightly different grouping
of the terms could also result in avoiding the bug. The way to reproduce was
by constructing the map directly, replacing `d1` with `1` and calling
`mlir::simplifyAffineExpr`.

Resolves llvm#114654.
@christopherbate christopherbate force-pushed the mlir-affine-simplifier-fix branch from 8440285 to d3fcfc1 Compare December 18, 2024 18:14
@christopherbate christopherbate merged commit 8272b6b into llvm:main Dec 18, 2024
8 checks passed
@christopherbate christopherbate deleted the mlir-affine-simplifier-fix branch December 18, 2024 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[mlir] Inconsistent results for affine.apply
3 participants