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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mlir/lib/IR/AffineExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
18 changes: 18 additions & 0 deletions mlir/unittests/IR/AffineExprTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,21 @@ 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);

// Manually replace variables by constants to avoid constant folding.
AffineExpr expr = (d0 - (d0 + 2)).floorDiv(8) % 8;
AffineExpr result = mlir::simplifyAffineExpr(expr, 1, 0);

ASSERT_TRUE(isa<AffineConstantExpr>(result));
ASSERT_EQ(cast<AffineConstantExpr>(result).getValue(), 7);
}
Loading