Skip to content

Commit 528a662

Browse files
authored
Fix sign of largest known divisor of div. (#100081)
There's a missing abs, so it returns a negative value if the divisor is negative. Later this is then cast to uint.
1 parent 430b254 commit 528a662

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

mlir/lib/IR/AffineExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include <cmath>
910
#include <cstdint>
1011
#include <limits>
1112
#include <utility>
@@ -257,7 +258,7 @@ int64_t AffineExpr::getLargestKnownDivisor() const {
257258
if (rhs && rhs.getValue() != 0) {
258259
int64_t lhsDiv = binExpr.getLHS().getLargestKnownDivisor();
259260
if (lhsDiv % rhs.getValue() == 0)
260-
return lhsDiv / rhs.getValue();
261+
return std::abs(lhsDiv / rhs.getValue());
261262
}
262263
return 1;
263264
}

mlir/unittests/IR/AffineExprTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,9 @@ TEST(AffineExprTest, modSimplificationRegression) {
106106
auto sum = d0 + d0.floorDiv(3).floorDiv(-3);
107107
ASSERT_EQ(sum.getKind(), AffineExprKind::Add);
108108
}
109+
110+
TEST(AffineExprTest, divisorOfNegativeFloorDiv) {
111+
MLIRContext ctx;
112+
OpBuilder b(&ctx);
113+
ASSERT_EQ(b.getAffineDimExpr(0).floorDiv(-1).getLargestKnownDivisor(), 1);
114+
}

0 commit comments

Comments
 (0)