Skip to content

AffineExpr: Fix result of d0 + (d0 // -c) * c. #107530

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 1 commit into from
Sep 6, 2024
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
5 changes: 4 additions & 1 deletion mlir/lib/IR/AffineExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,11 @@ static AffineExpr simplifyAdd(AffineExpr lhs, AffineExpr rhs) {

llrhs = lrBinOpExpr.getLHS();
rlrhs = lrBinOpExpr.getRHS();
auto rlrhsConstOpExpr = dyn_cast<AffineConstantExpr>(rlrhs);
// We don't support modulo with a negative RHS.
bool isPositiveRhs = rlrhsConstOpExpr && rlrhsConstOpExpr.getValue() > 0;

if (lhs == llrhs && rlrhs == -rrhs) {
if (isPositiveRhs && lhs == llrhs && rlrhs == -rrhs) {
return lhs % rlrhs;
}
return nullptr;
Expand Down
17 changes: 17 additions & 0 deletions mlir/unittests/IR/AffineExprTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@

using namespace mlir;

static std::string toString(AffineExpr expr) {
std::string s;
llvm::raw_string_ostream ss(s);
ss << expr;
return s;
}

// Test creating AffineExprs using the overloaded binary operators.
TEST(AffineExprTest, constructFromBinaryOperators) {
MLIRContext ctx;
Expand Down Expand Up @@ -112,3 +119,13 @@ TEST(AffineExprTest, divisorOfNegativeFloorDiv) {
OpBuilder b(&ctx);
ASSERT_EQ(b.getAffineDimExpr(0).floorDiv(-1).getLargestKnownDivisor(), 1);
}

TEST(AffineExprTest, d0PlusD0FloorDivNeg2) {
// Regression test for a bug where this was rewritten to d0 mod -2. We do not
// support a negative RHS for mod in LowerAffinePass.
MLIRContext ctx;
OpBuilder b(&ctx);
auto d0 = b.getAffineDimExpr(0);
auto sum = d0 + d0.floorDiv(-2) * 2;
ASSERT_EQ(toString(sum), "d0 + (d0 floordiv -2) * 2");
}
Loading