Skip to content

Commit a96d157

Browse files
author
git apple-llvm automerger
committed
Merge commit 'b97aa413d166' from llvm.org/main into next
2 parents 8b522fd + b97aa41 commit a96d157

File tree

3 files changed

+18
-76
lines changed

3 files changed

+18
-76
lines changed

mlir/include/mlir/Analysis/Presburger/Simplex.h

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,21 @@ class SimplexBase {
166166
/// false otherwise.
167167
bool isEmpty() const;
168168

169+
/// Add an inequality to the tableau. If coeffs is c_0, c_1, ... c_n, where n
170+
/// is the current number of variables, then the corresponding inequality is
171+
/// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} >= 0.
172+
virtual void addInequality(ArrayRef<int64_t> coeffs) = 0;
173+
169174
/// Returns the number of variables in the tableau.
170175
unsigned getNumVariables() const;
171176

172177
/// Returns the number of constraints in the tableau.
173178
unsigned getNumConstraints() const;
174179

175-
/// Add an inequality to the tableau. If coeffs is c_0, c_1, ... c_n, where n
176-
/// is the current number of variables, then the corresponding inequality is
177-
/// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} >= 0.
178-
virtual void addInequality(ArrayRef<int64_t> coeffs) = 0;
179-
180180
/// Add an equality to the tableau. If coeffs is c_0, c_1, ... c_n, where n
181181
/// is the current number of variables, then the corresponding equality is
182182
/// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} == 0.
183-
virtual void addEquality(ArrayRef<int64_t> coeffs) = 0;
183+
void addEquality(ArrayRef<int64_t> coeffs);
184184

185185
/// Add new variables to the end of the list of variables.
186186
void appendVariable(unsigned count = 1);
@@ -249,14 +249,6 @@ class SimplexBase {
249249
/// coefficient for it.
250250
Optional<unsigned> findAnyPivotRow(unsigned col);
251251

252-
/// Return any column that this row can be pivoted with, ignoring tableau
253-
/// consistency. Equality rows are not considered.
254-
///
255-
/// Returns an empty optional if no pivot is possible, which happens only when
256-
/// the column unknown is a variable and no constraint has a non-zero
257-
/// coefficient for it.
258-
Optional<unsigned> findAnyPivotCol(unsigned row);
259-
260252
/// Swap the row with the column in the tableau's data structures but not the
261253
/// tableau itself. This is used by pivot.
262254
void swapRowWithCol(unsigned row, unsigned col);
@@ -303,7 +295,6 @@ class SimplexBase {
303295
RemoveLastVariable,
304296
UnmarkEmpty,
305297
UnmarkLastRedundant,
306-
UnmarkLastEquality,
307298
RestoreBasis
308299
};
309300

@@ -317,14 +308,13 @@ class SimplexBase {
317308
/// Undo the operation represented by the log entry.
318309
void undo(UndoLogEntry entry);
319310

320-
unsigned getNumFixedCols() const { return numFixedCols; }
311+
/// Return the number of fixed columns, as described in the constructor above,
312+
/// this is the number of columns beyond those for the variables in var.
313+
unsigned getNumFixedCols() const { return usingBigM ? 3u : 2u; }
321314

322315
/// Stores whether or not a big M column is present in the tableau.
323316
bool usingBigM;
324317

325-
/// denom + const + maybe M + equality columns
326-
unsigned numFixedCols;
327-
328318
/// The number of rows in the tableau.
329319
unsigned nRow;
330320

@@ -445,12 +435,9 @@ class LexSimplex : public SimplexBase {
445435
///
446436
/// This just adds the inequality to the tableau and does not try to create a
447437
/// consistent tableau configuration.
448-
void addInequality(ArrayRef<int64_t> coeffs) final;
449-
450-
/// Add an equality to the tableau. If coeffs is c_0, c_1, ... c_n, where n
451-
/// is the current number of variables, then the corresponding equality is
452-
/// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} == 0.
453-
void addEquality(ArrayRef<int64_t> coeffs) final;
438+
void addInequality(ArrayRef<int64_t> coeffs) final {
439+
addRow(coeffs, /*makeRestricted=*/true);
440+
}
454441

455442
/// Get a snapshot of the current state. This is used for rolling back.
456443
unsigned getSnapshot() { return SimplexBase::getSnapshotBasis(); }
@@ -546,11 +533,6 @@ class Simplex : public SimplexBase {
546533
/// state and marks the Simplex empty if this is not possible.
547534
void addInequality(ArrayRef<int64_t> coeffs) final;
548535

549-
/// Add an equality to the tableau. If coeffs is c_0, c_1, ... c_n, where n
550-
/// is the current number of variables, then the corresponding equality is
551-
/// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} == 0.
552-
void addEquality(ArrayRef<int64_t> coeffs) final;
553-
554536
/// Compute the maximum or minimum value of the given row, depending on
555537
/// direction. The specified row is never pivoted. On return, the row may
556538
/// have a negative sample value if the direction is down.

mlir/lib/Analysis/Presburger/Simplex.cpp

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ using Direction = Simplex::Direction;
1919
const int nullIndex = std::numeric_limits<int>::max();
2020

2121
SimplexBase::SimplexBase(unsigned nVar, bool mustUseBigM)
22-
: usingBigM(mustUseBigM), numFixedCols(mustUseBigM ? 3 : 2), nRow(0),
23-
nCol(numFixedCols + nVar), nRedundant(0), tableau(0, nCol), empty(false) {
24-
colUnknown.insert(colUnknown.begin(), numFixedCols, nullIndex);
22+
: usingBigM(mustUseBigM), nRow(0), nCol(getNumFixedCols() + nVar),
23+
nRedundant(0), tableau(0, nCol), empty(false) {
24+
colUnknown.insert(colUnknown.begin(), getNumFixedCols(), nullIndex);
2525
for (unsigned i = 0; i < nVar; ++i) {
2626
var.emplace_back(Orientation::Column, /*restricted=*/false,
27-
/*pos=*/numFixedCols + i);
27+
/*pos=*/getNumFixedCols() + i);
2828
colUnknown.push_back(i);
2929
}
3030
}
@@ -309,7 +309,7 @@ void LexSimplex::restoreRationalConsistency() {
309309
// minimizes the change in sample value.
310310
LogicalResult LexSimplex::moveRowUnknownToColumn(unsigned row) {
311311
Optional<unsigned> maybeColumn;
312-
for (unsigned col = getNumFixedCols(); col < nCol; ++col) {
312+
for (unsigned col = 3; col < nCol; ++col) {
313313
if (tableau(row, col) <= 0)
314314
continue;
315315
maybeColumn =
@@ -648,7 +648,7 @@ void Simplex::addInequality(ArrayRef<int64_t> coeffs) {
648648
///
649649
/// We simply add two opposing inequalities, which force the expression to
650650
/// be zero.
651-
void Simplex::addEquality(ArrayRef<int64_t> coeffs) {
651+
void SimplexBase::addEquality(ArrayRef<int64_t> coeffs) {
652652
addInequality(coeffs);
653653
SmallVector<int64_t, 8> negatedCoeffs;
654654
for (int64_t coeff : coeffs)
@@ -705,15 +705,6 @@ Optional<unsigned> SimplexBase::findAnyPivotRow(unsigned col) {
705705
return {};
706706
}
707707

708-
// This doesn't find a pivot column only if the row has zero coefficients for
709-
// every column not marked as an equality.
710-
Optional<unsigned> SimplexBase::findAnyPivotCol(unsigned row) {
711-
for (unsigned col = getNumFixedCols(); col < nCol; ++col)
712-
if (tableau(row, col) != 0)
713-
return col;
714-
return {};
715-
}
716-
717708
// It's not valid to remove the constraint by deleting the column since this
718709
// would result in an invalid basis.
719710
void Simplex::undoLastConstraint() {
@@ -789,10 +780,6 @@ void SimplexBase::undo(UndoLogEntry entry) {
789780
empty = false;
790781
} else if (entry == UndoLogEntry::UnmarkLastRedundant) {
791782
nRedundant--;
792-
} else if (entry == UndoLogEntry::UnmarkLastEquality) {
793-
numFixedCols--;
794-
assert(getNumFixedCols() >= 2 + usingBigM &&
795-
"The denominator, constant, big M and symbols are always fixed!");
796783
} else if (entry == UndoLogEntry::RestoreBasis) {
797784
assert(!savedBases.empty() && "No bases saved!");
798785

@@ -1123,26 +1110,6 @@ Optional<SmallVector<Fraction, 8>> Simplex::getRationalSample() const {
11231110
return sample;
11241111
}
11251112

1126-
void LexSimplex::addInequality(ArrayRef<int64_t> coeffs) {
1127-
addRow(coeffs, /*makeRestricted=*/true);
1128-
}
1129-
1130-
/// Try to make the equality a fixed column by finding any pivot and performing
1131-
/// it. The only time this is not possible is when the given equality's
1132-
/// direction is already in the span of the existing fixed column equalities. In
1133-
/// that case, we just leave it in row position.
1134-
void LexSimplex::addEquality(ArrayRef<int64_t> coeffs) {
1135-
const Unknown &u = con[addRow(coeffs, /*makeRestricted=*/true)];
1136-
Optional<unsigned> pivotCol = findAnyPivotCol(u.pos);
1137-
if (!pivotCol)
1138-
return;
1139-
1140-
pivot(u.pos, *pivotCol);
1141-
swapColumns(*pivotCol, getNumFixedCols());
1142-
numFixedCols++;
1143-
undoLog.push_back(UndoLogEntry::UnmarkLastEquality);
1144-
}
1145-
11461113
MaybeOptimum<SmallVector<Fraction, 8>> LexSimplex::getRationalSample() const {
11471114
if (empty)
11481115
return OptimumKind::Empty;

mlir/unittests/Analysis/Presburger/SimplexTest.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -548,10 +548,3 @@ TEST(SimplexTest, addDivisionVariable) {
548548
ASSERT_TRUE(sample.hasValue());
549549
EXPECT_EQ((*sample)[0] / 2, (*sample)[1]);
550550
}
551-
552-
TEST(LexSimplexTest, addEquality) {
553-
IntegerRelation rel(/*numDomain=*/0, /*numRange=*/1);
554-
rel.addEquality({1, 0});
555-
LexSimplex simplex(rel);
556-
EXPECT_EQ(simplex.getNumConstraints(), 1u);
557-
}

0 commit comments

Comments
 (0)