Skip to content

Commit

Permalink
Fix simplification of x + x//c*-c to x mod c. (#98909)
Browse files Browse the repository at this point in the history
There was no check that rhs is actually a multiplication.
  • Loading branch information
jreiffers committed Jul 15, 2024
1 parent becceca commit dd7d81e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mlir/lib/IR/AffineExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,10 @@ static AffineExpr simplifyAdd(AffineExpr lhs, AffineExpr rhs) {
}

// Process lrhs, which is 'expr floordiv c'.
// expr + (expr // c * -c) = expr % c
AffineBinaryOpExpr lrBinOpExpr = dyn_cast<AffineBinaryOpExpr>(lrhs);
if (!lrBinOpExpr || lrBinOpExpr.getKind() != AffineExprKind::FloorDiv)
if (!lrBinOpExpr || rhs.getKind() != AffineExprKind::Mul ||
lrBinOpExpr.getKind() != AffineExprKind::FloorDiv)
return nullptr;

llrhs = lrBinOpExpr.getLHS();
Expand Down
8 changes: 8 additions & 0 deletions mlir/unittests/IR/AffineExprTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,11 @@ TEST(AffineExprTest, divisionSimplification) {
ASSERT_EQ((d0 * 6).ceilDiv(4).getKind(), AffineExprKind::CeilDiv);
ASSERT_EQ((d0 * 6).ceilDiv(-2), d0 * -3);
}

TEST(AffineExprTest, modSimplificationRegression) {
MLIRContext ctx;
OpBuilder b(&ctx);
auto d0 = b.getAffineDimExpr(0);
auto sum = d0 + d0.floorDiv(3).floorDiv(-3);
ASSERT_EQ(sum.getKind(), AffineExprKind::Add);
}

0 comments on commit dd7d81e

Please sign in to comment.