Skip to content

Commit bad95b7

Browse files
committed
[MLIR][Presburger] fourier-motzkin: check if all LCMs are 1 using a bool instead of by multiplying them
This can easily overflow and it is possible for these unsigned overflows to result in incorrect results. For example, the two LCMs could be 641 and 6700417, which multiply to 2^32 + 1, which overflows to 1. Unsigned overflows already occur in the existing tests. Also, when switching to arbitrary-precision arithmetic, this results in a many large integer multiplications resulting in a significant slowdown. Reviewed By: Groverkss Differential Revision: https://reviews.llvm.org/D131184
1 parent f6bd0a8 commit bad95b7

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

mlir/lib/Analysis/Presburger/IntegerRelation.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ void IntegerRelation::fourierMotzkinEliminate(unsigned pos, bool darkShadow,
18021802
getNumEqualities(), getNumCols() - 1, newSpace);
18031803

18041804
// This will be used to check if the elimination was integer exact.
1805-
unsigned lcmProducts = 1;
1805+
bool allLCMsAreOne = true;
18061806

18071807
// Let x be the variable we are eliminating.
18081808
// For each lower bound, lb <= c_l*x, and each upper bound c_u*x <= ub, (note
@@ -1831,7 +1831,9 @@ void IntegerRelation::fourierMotzkinEliminate(unsigned pos, bool darkShadow,
18311831
int64_t lcm = mlir::lcm(lbCoeff, ubCoeff);
18321832
ineq.push_back(atIneq(ubPos, l) * (lcm / ubCoeff) +
18331833
atIneq(lbPos, l) * (lcm / lbCoeff));
1834-
lcmProducts *= lcm;
1834+
assert(lcm > 0 && "lcm should be positive!");
1835+
if (lcm != 1)
1836+
allLCMsAreOne = false;
18351837
}
18361838
if (darkShadow) {
18371839
// The dark shadow is a convex subset of the exact integer shadow. If
@@ -1844,9 +1846,9 @@ void IntegerRelation::fourierMotzkinEliminate(unsigned pos, bool darkShadow,
18441846
}
18451847
}
18461848

1847-
LLVM_DEBUG(llvm::dbgs() << "FM isResultIntegerExact: " << (lcmProducts == 1)
1849+
LLVM_DEBUG(llvm::dbgs() << "FM isResultIntegerExact: " << allLCMsAreOne
18481850
<< "\n");
1849-
if (lcmProducts == 1 && isResultIntegerExact)
1851+
if (allLCMsAreOne && isResultIntegerExact)
18501852
*isResultIntegerExact = true;
18511853

18521854
// Copy over the constraints not involving this variable.

0 commit comments

Comments
 (0)