diff --git a/mlir/lib/IR/AffineExpr.cpp b/mlir/lib/IR/AffineExpr.cpp index 75cc01ee9a14..7978b35e7147 100644 --- a/mlir/lib/IR/AffineExpr.cpp +++ b/mlir/lib/IR/AffineExpr.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include #include #include #include @@ -257,7 +258,7 @@ int64_t AffineExpr::getLargestKnownDivisor() const { if (rhs && rhs.getValue() != 0) { int64_t lhsDiv = binExpr.getLHS().getLargestKnownDivisor(); if (lhsDiv % rhs.getValue() == 0) - return lhsDiv / rhs.getValue(); + return std::abs(lhsDiv / rhs.getValue()); } return 1; } diff --git a/mlir/unittests/IR/AffineExprTest.cpp b/mlir/unittests/IR/AffineExprTest.cpp index 75c893334943..dc78bbac85f3 100644 --- a/mlir/unittests/IR/AffineExprTest.cpp +++ b/mlir/unittests/IR/AffineExprTest.cpp @@ -106,3 +106,9 @@ TEST(AffineExprTest, modSimplificationRegression) { auto sum = d0 + d0.floorDiv(3).floorDiv(-3); ASSERT_EQ(sum.getKind(), AffineExprKind::Add); } + +TEST(AffineExprTest, divisorOfNegativeFloorDiv) { + MLIRContext ctx; + OpBuilder b(&ctx); + ASSERT_EQ(b.getAffineDimExpr(0).floorDiv(-1).getLargestKnownDivisor(), 1); +}