1 //===- Simplex.cpp - MLIR Simplex Class -----------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Analysis/Presburger/Simplex.h"
10 #include "mlir/Analysis/Presburger/Matrix.h"
11 #include "mlir/Support/MathExtras.h"
12 
13 namespace mlir {
14 using Direction = Simplex::Direction;
15 
16 const int nullIndex = std::numeric_limits<int>::max();
17 
18 /// Construct a Simplex object with `nVar` variables.
19 Simplex::Simplex(unsigned nVar)
20     : nRow(0), nCol(2), nRedundant(0), tableau(0, 2 + nVar), empty(false) {
21   colUnknown.push_back(nullIndex);
22   colUnknown.push_back(nullIndex);
23   for (unsigned i = 0; i < nVar; ++i) {
24     var.emplace_back(Orientation::Column, /*restricted=*/false, /*pos=*/nCol);
25     colUnknown.push_back(i);
26     nCol++;
27   }
28 }
29 
30 Simplex::Simplex(const FlatAffineConstraints &constraints)
31     : Simplex(constraints.getNumIds()) {
32   for (unsigned i = 0, numIneqs = constraints.getNumInequalities();
33        i < numIneqs; ++i)
34     addInequality(constraints.getInequality(i));
35   for (unsigned i = 0, numEqs = constraints.getNumEqualities(); i < numEqs; ++i)
36     addEquality(constraints.getEquality(i));
37 }
38 
39 const Simplex::Unknown &Simplex::unknownFromIndex(int index) const {
40   assert(index != nullIndex && "nullIndex passed to unknownFromIndex");
41   return index >= 0 ? var[index] : con[~index];
42 }
43 
44 const Simplex::Unknown &Simplex::unknownFromColumn(unsigned col) const {
45   assert(col < nCol && "Invalid column");
46   return unknownFromIndex(colUnknown[col]);
47 }
48 
49 const Simplex::Unknown &Simplex::unknownFromRow(unsigned row) const {
50   assert(row < nRow && "Invalid row");
51   return unknownFromIndex(rowUnknown[row]);
52 }
53 
54 Simplex::Unknown &Simplex::unknownFromIndex(int index) {
55   assert(index != nullIndex && "nullIndex passed to unknownFromIndex");
56   return index >= 0 ? var[index] : con[~index];
57 }
58 
59 Simplex::Unknown &Simplex::unknownFromColumn(unsigned col) {
60   assert(col < nCol && "Invalid column");
61   return unknownFromIndex(colUnknown[col]);
62 }
63 
64 Simplex::Unknown &Simplex::unknownFromRow(unsigned row) {
65   assert(row < nRow && "Invalid row");
66   return unknownFromIndex(rowUnknown[row]);
67 }
68 
69 /// Add a new row to the tableau corresponding to the given constant term and
70 /// list of coefficients. The coefficients are specified as a vector of
71 /// (variable index, coefficient) pairs.
72 unsigned Simplex::addRow(ArrayRef<int64_t> coeffs) {
73   assert(coeffs.size() == 1 + var.size() &&
74          "Incorrect number of coefficients!");
75 
76   ++nRow;
77   // If the tableau is not big enough to accomodate the extra row, we extend it.
78   if (nRow >= tableau.getNumRows())
79     tableau.resizeVertically(nRow);
80   rowUnknown.push_back(~con.size());
81   con.emplace_back(Orientation::Row, false, nRow - 1);
82 
83   tableau(nRow - 1, 0) = 1;
84   tableau(nRow - 1, 1) = coeffs.back();
85   for (unsigned col = 2; col < nCol; ++col)
86     tableau(nRow - 1, col) = 0;
87 
88   // Process each given variable coefficient.
89   for (unsigned i = 0; i < var.size(); ++i) {
90     unsigned pos = var[i].pos;
91     if (coeffs[i] == 0)
92       continue;
93 
94     if (var[i].orientation == Orientation::Column) {
95       // If a variable is in column position at column col, then we just add the
96       // coefficient for that variable (scaled by the common row denominator) to
97       // the corresponding entry in the new row.
98       tableau(nRow - 1, pos) += coeffs[i] * tableau(nRow - 1, 0);
99       continue;
100     }
101 
102     // If the variable is in row position, we need to add that row to the new
103     // row, scaled by the coefficient for the variable, accounting for the two
104     // rows potentially having different denominators. The new denominator is
105     // the lcm of the two.
106     int64_t lcm = mlir::lcm(tableau(nRow - 1, 0), tableau(pos, 0));
107     int64_t nRowCoeff = lcm / tableau(nRow - 1, 0);
108     int64_t idxRowCoeff = coeffs[i] * (lcm / tableau(pos, 0));
109     tableau(nRow - 1, 0) = lcm;
110     for (unsigned col = 1; col < nCol; ++col)
111       tableau(nRow - 1, col) =
112           nRowCoeff * tableau(nRow - 1, col) + idxRowCoeff * tableau(pos, col);
113   }
114 
115   normalizeRow(nRow - 1);
116   // Push to undo log along with the index of the new constraint.
117   undoLog.push_back(UndoLogEntry::RemoveLastConstraint);
118   return con.size() - 1;
119 }
120 
121 /// Normalize the row by removing factors that are common between the
122 /// denominator and all the numerator coefficients.
123 void Simplex::normalizeRow(unsigned row) {
124   int64_t gcd = 0;
125   for (unsigned col = 0; col < nCol; ++col) {
126     if (gcd == 1)
127       break;
128     gcd = llvm::greatestCommonDivisor(gcd, std::abs(tableau(row, col)));
129   }
130   for (unsigned col = 0; col < nCol; ++col)
131     tableau(row, col) /= gcd;
132 }
133 
134 namespace {
135 bool signMatchesDirection(int64_t elem, Direction direction) {
136   assert(elem != 0 && "elem should not be 0");
137   return direction == Direction::Up ? elem > 0 : elem < 0;
138 }
139 
140 Direction flippedDirection(Direction direction) {
141   return direction == Direction::Up ? Direction::Down : Simplex::Direction::Up;
142 }
143 } // anonymous namespace
144 
145 /// Find a pivot to change the sample value of the row in the specified
146 /// direction. The returned pivot row will involve `row` if and only if the
147 /// unknown is unbounded in the specified direction.
148 ///
149 /// To increase (resp. decrease) the value of a row, we need to find a live
150 /// column with a non-zero coefficient. If the coefficient is positive, we need
151 /// to increase (decrease) the value of the column, and if the coefficient is
152 /// negative, we need to decrease (increase) the value of the column. Also,
153 /// we cannot decrease the sample value of restricted columns.
154 ///
155 /// If multiple columns are valid, we break ties by considering a lexicographic
156 /// ordering where we prefer unknowns with lower index.
157 Optional<Simplex::Pivot> Simplex::findPivot(int row,
158                                             Direction direction) const {
159   Optional<unsigned> col;
160   for (unsigned j = 2; j < nCol; ++j) {
161     int64_t elem = tableau(row, j);
162     if (elem == 0)
163       continue;
164 
165     if (unknownFromColumn(j).restricted &&
166         !signMatchesDirection(elem, direction))
167       continue;
168     if (!col || colUnknown[j] < colUnknown[*col])
169       col = j;
170   }
171 
172   if (!col)
173     return {};
174 
175   Direction newDirection =
176       tableau(row, *col) < 0 ? flippedDirection(direction) : direction;
177   Optional<unsigned> maybePivotRow = findPivotRow(row, newDirection, *col);
178   return Pivot{maybePivotRow.getValueOr(row), *col};
179 }
180 
181 /// Swap the associated unknowns for the row and the column.
182 ///
183 /// First we swap the index associated with the row and column. Then we update
184 /// the unknowns to reflect their new position and orientation.
185 void Simplex::swapRowWithCol(unsigned row, unsigned col) {
186   std::swap(rowUnknown[row], colUnknown[col]);
187   Unknown &uCol = unknownFromColumn(col);
188   Unknown &uRow = unknownFromRow(row);
189   uCol.orientation = Orientation::Column;
190   uRow.orientation = Orientation::Row;
191   uCol.pos = col;
192   uRow.pos = row;
193 }
194 
195 void Simplex::pivot(Pivot pair) { pivot(pair.row, pair.column); }
196 
197 /// Pivot pivotRow and pivotCol.
198 ///
199 /// Let R be the pivot row unknown and let C be the pivot col unknown.
200 /// Since initially R = a*C + sum b_i * X_i
201 /// (where the sum is over the other column's unknowns, x_i)
202 /// C = (R - (sum b_i * X_i))/a
203 ///
204 /// Let u be some other row unknown.
205 /// u = c*C + sum d_i * X_i
206 /// So u = c*(R - sum b_i * X_i)/a + sum d_i * X_i
207 ///
208 /// This results in the following transform:
209 ///            pivot col    other col                   pivot col    other col
210 /// pivot row     a             b       ->   pivot row     1/a         -b/a
211 /// other row     c             d            other row     c/a        d - bc/a
212 ///
213 /// Taking into account the common denominators p and q:
214 ///
215 ///            pivot col    other col                    pivot col   other col
216 /// pivot row     a/p          b/p     ->   pivot row      p/a         -b/a
217 /// other row     c/q          d/q          other row     cp/aq    (da - bc)/aq
218 ///
219 /// The pivot row transform is accomplished be swapping a with the pivot row's
220 /// common denominator and negating the pivot row except for the pivot column
221 /// element.
222 void Simplex::pivot(unsigned pivotRow, unsigned pivotCol) {
223   assert(pivotCol >= 2 && "Refusing to pivot invalid column");
224 
225   swapRowWithCol(pivotRow, pivotCol);
226   std::swap(tableau(pivotRow, 0), tableau(pivotRow, pivotCol));
227   // We need to negate the whole pivot row except for the pivot column.
228   if (tableau(pivotRow, 0) < 0) {
229     // If the denominator is negative, we negate the row by simply negating the
230     // denominator.
231     tableau(pivotRow, 0) = -tableau(pivotRow, 0);
232     tableau(pivotRow, pivotCol) = -tableau(pivotRow, pivotCol);
233   } else {
234     for (unsigned col = 1; col < nCol; ++col) {
235       if (col == pivotCol)
236         continue;
237       tableau(pivotRow, col) = -tableau(pivotRow, col);
238     }
239   }
240   normalizeRow(pivotRow);
241 
242   for (unsigned row = nRedundant; row < nRow; ++row) {
243     if (row == pivotRow)
244       continue;
245     if (tableau(row, pivotCol) == 0) // Nothing to do.
246       continue;
247     tableau(row, 0) *= tableau(pivotRow, 0);
248     for (unsigned j = 1; j < nCol; ++j) {
249       if (j == pivotCol)
250         continue;
251       // Add rather than subtract because the pivot row has been negated.
252       tableau(row, j) = tableau(row, j) * tableau(pivotRow, 0) +
253                         tableau(row, pivotCol) * tableau(pivotRow, j);
254     }
255     tableau(row, pivotCol) *= tableau(pivotRow, pivotCol);
256     normalizeRow(row);
257   }
258 }
259 
260 /// Perform pivots until the unknown has a non-negative sample value or until
261 /// no more upward pivots can be performed. Return the sign of the final sample
262 /// value.
263 LogicalResult Simplex::restoreRow(Unknown &u) {
264   assert(u.orientation == Orientation::Row &&
265          "unknown should be in row position");
266 
267   while (tableau(u.pos, 1) < 0) {
268     Optional<Pivot> maybePivot = findPivot(u.pos, Direction::Up);
269     if (!maybePivot)
270       break;
271 
272     pivot(*maybePivot);
273     if (u.orientation == Orientation::Column)
274       return LogicalResult::Success; // the unknown is unbounded above.
275   }
276   return success(tableau(u.pos, 1) >= 0);
277 }
278 
279 /// Find a row that can be used to pivot the column in the specified direction.
280 /// This returns an empty optional if and only if the column is unbounded in the
281 /// specified direction (ignoring skipRow, if skipRow is set).
282 ///
283 /// If skipRow is set, this row is not considered, and (if it is restricted) its
284 /// restriction may be violated by the returned pivot. Usually, skipRow is set
285 /// because we don't want to move it to column position unless it is unbounded,
286 /// and we are either trying to increase the value of skipRow or explicitly
287 /// trying to make skipRow negative, so we are not concerned about this.
288 ///
289 /// If the direction is up (resp. down) and a restricted row has a negative
290 /// (positive) coefficient for the column, then this row imposes a bound on how
291 /// much the sample value of the column can change. Such a row with constant
292 /// term c and coefficient f for the column imposes a bound of c/|f| on the
293 /// change in sample value (in the specified direction). (note that c is
294 /// non-negative here since the row is restricted and the tableau is consistent)
295 ///
296 /// We iterate through the rows and pick the row which imposes the most
297 /// stringent bound, since pivoting with a row changes the row's sample value to
298 /// 0 and hence saturates the bound it imposes. We break ties between rows that
299 /// impose the same bound by considering a lexicographic ordering where we
300 /// prefer unknowns with lower index value.
301 Optional<unsigned> Simplex::findPivotRow(Optional<unsigned> skipRow,
302                                          Direction direction,
303                                          unsigned col) const {
304   Optional<unsigned> retRow;
305   int64_t retElem, retConst;
306   for (unsigned row = nRedundant; row < nRow; ++row) {
307     if (skipRow && row == *skipRow)
308       continue;
309     int64_t elem = tableau(row, col);
310     if (elem == 0)
311       continue;
312     if (!unknownFromRow(row).restricted)
313       continue;
314     if (signMatchesDirection(elem, direction))
315       continue;
316     int64_t constTerm = tableau(row, 1);
317 
318     if (!retRow) {
319       retRow = row;
320       retElem = elem;
321       retConst = constTerm;
322       continue;
323     }
324 
325     int64_t diff = retConst * elem - constTerm * retElem;
326     if ((diff == 0 && rowUnknown[row] < rowUnknown[*retRow]) ||
327         (diff != 0 && !signMatchesDirection(diff, direction))) {
328       retRow = row;
329       retElem = elem;
330       retConst = constTerm;
331     }
332   }
333   return retRow;
334 }
335 
336 bool Simplex::isEmpty() const { return empty; }
337 
338 void Simplex::swapRows(unsigned i, unsigned j) {
339   if (i == j)
340     return;
341   tableau.swapRows(i, j);
342   std::swap(rowUnknown[i], rowUnknown[j]);
343   unknownFromRow(i).pos = i;
344   unknownFromRow(j).pos = j;
345 }
346 
347 /// Mark this tableau empty and push an entry to the undo stack.
348 void Simplex::markEmpty() {
349   undoLog.push_back(UndoLogEntry::UnmarkEmpty);
350   empty = true;
351 }
352 
353 /// Add an inequality to the tableau. If coeffs is c_0, c_1, ... c_n, where n
354 /// is the curent number of variables, then the corresponding inequality is
355 /// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} >= 0.
356 ///
357 /// We add the inequality and mark it as restricted. We then try to make its
358 /// sample value non-negative. If this is not possible, the tableau has become
359 /// empty and we mark it as such.
360 void Simplex::addInequality(ArrayRef<int64_t> coeffs) {
361   unsigned conIndex = addRow(coeffs);
362   Unknown &u = con[conIndex];
363   u.restricted = true;
364   LogicalResult result = restoreRow(u);
365   if (failed(result))
366     markEmpty();
367 }
368 
369 /// Add an equality to the tableau. If coeffs is c_0, c_1, ... c_n, where n
370 /// is the curent number of variables, then the corresponding equality is
371 /// c_n + c_0*x_0 + c_1*x_1 + ... + c_{n-1}*x_{n-1} == 0.
372 ///
373 /// We simply add two opposing inequalities, which force the expression to
374 /// be zero.
375 void Simplex::addEquality(ArrayRef<int64_t> coeffs) {
376   addInequality(coeffs);
377   SmallVector<int64_t, 8> negatedCoeffs;
378   for (int64_t coeff : coeffs)
379     negatedCoeffs.emplace_back(-coeff);
380   addInequality(negatedCoeffs);
381 }
382 
383 unsigned Simplex::numVariables() const { return var.size(); }
384 unsigned Simplex::numConstraints() const { return con.size(); }
385 
386 /// Return a snapshot of the curent state. This is just the current size of the
387 /// undo log.
388 unsigned Simplex::getSnapshot() const { return undoLog.size(); }
389 
390 void Simplex::undo(UndoLogEntry entry) {
391   if (entry == UndoLogEntry::RemoveLastConstraint) {
392     Unknown &constraint = con.back();
393     if (constraint.orientation == Orientation::Column) {
394       unsigned column = constraint.pos;
395       Optional<unsigned> row;
396 
397       // Try to find any pivot row for this column that preserves tableau
398       // consistency (except possibly the column itself, which is going to be
399       // deallocated anyway).
400       //
401       // If no pivot row is found in either direction, then the unknown is
402       // unbounded in both directions and we are free to
403       // perform any pivot at all. To do this, we just need to find any row with
404       // a non-zero coefficient for the column.
405       if (Optional<unsigned> maybeRow =
406               findPivotRow({}, Direction::Up, column)) {
407         row = *maybeRow;
408       } else if (Optional<unsigned> maybeRow =
409                      findPivotRow({}, Direction::Down, column)) {
410         row = *maybeRow;
411       } else {
412         // The loop doesn't find a pivot row only if the column has zero
413         // coefficients for every row. But the unknown is a constraint,
414         // so it was added initially as a row. Such a row could never have been
415         // pivoted to a column. So a pivot row will always be found.
416         for (unsigned i = nRedundant; i < nRow; ++i) {
417           if (tableau(i, column) != 0) {
418             row = i;
419             break;
420           }
421         }
422       }
423       assert(row.hasValue() && "No pivot row found!");
424       pivot(*row, column);
425     }
426 
427     // Move this unknown to the last row and remove the last row from the
428     // tableau.
429     swapRows(constraint.pos, nRow - 1);
430     // It is not strictly necessary to shrink the tableau, but for now we
431     // maintain the invariant that the tableau has exactly nRow rows.
432     tableau.resizeVertically(nRow - 1);
433     nRow--;
434     rowUnknown.pop_back();
435     con.pop_back();
436   } else if (entry == UndoLogEntry::UnmarkEmpty) {
437     empty = false;
438   } else if (entry == UndoLogEntry::UnmarkLastRedundant) {
439     nRedundant--;
440   }
441 }
442 
443 /// Rollback to the specified snapshot.
444 ///
445 /// We undo all the log entries until the log size when the snapshot was taken
446 /// is reached.
447 void Simplex::rollback(unsigned snapshot) {
448   while (undoLog.size() > snapshot) {
449     undo(undoLog.back());
450     undoLog.pop_back();
451   }
452 }
453 
454 Optional<Fraction> Simplex::computeRowOptimum(Direction direction,
455                                               unsigned row) {
456   // Keep trying to find a pivot for the row in the specified direction.
457   while (Optional<Pivot> maybePivot = findPivot(row, direction)) {
458     // If findPivot returns a pivot involving the row itself, then the optimum
459     // is unbounded, so we return None.
460     if (maybePivot->row == row)
461       return {};
462     pivot(*maybePivot);
463   }
464 
465   // The row has reached its optimal sample value, which we return.
466   // The sample value is the entry in the constant column divided by the common
467   // denominator for this row.
468   return Fraction(tableau(row, 1), tableau(row, 0));
469 }
470 
471 /// Compute the optimum of the specified expression in the specified direction,
472 /// or None if it is unbounded.
473 Optional<Fraction> Simplex::computeOptimum(Direction direction,
474                                            ArrayRef<int64_t> coeffs) {
475   assert(!empty && "Tableau should not be empty");
476 
477   unsigned snapshot = getSnapshot();
478   unsigned conIndex = addRow(coeffs);
479   unsigned row = con[conIndex].pos;
480   Optional<Fraction> optimum = computeRowOptimum(direction, row);
481   rollback(snapshot);
482   return optimum;
483 }
484 
485 /// Redundant constraints are those that are in row orientation and lie in
486 /// rows 0 to nRedundant - 1.
487 bool Simplex::isMarkedRedundant(unsigned constraintIndex) const {
488   const Unknown &u = con[constraintIndex];
489   return u.orientation == Orientation::Row && u.pos < nRedundant;
490 }
491 
492 /// Mark the specified row redundant.
493 ///
494 /// This is done by moving the unknown to the end of the block of redundant
495 /// rows (namely, to row nRedundant) and incrementing nRedundant to
496 /// accomodate the new redundant row.
497 void Simplex::markRowRedundant(Unknown &u) {
498   assert(u.orientation == Orientation::Row &&
499          "Unknown should be in row position!");
500   swapRows(u.pos, nRedundant);
501   ++nRedundant;
502   undoLog.emplace_back(UndoLogEntry::UnmarkLastRedundant);
503 }
504 
505 /// Find a subset of constraints that is redundant and mark them redundant.
506 void Simplex::detectRedundant() {
507   // It is not meaningful to talk about redundancy for empty sets.
508   if (empty)
509     return;
510 
511   // Iterate through the constraints and check for each one if it can attain
512   // negative sample values. If it can, it's not redundant. Otherwise, it is.
513   // We mark redundant constraints redundant.
514   //
515   // Constraints that get marked redundant in one iteration are not respected
516   // when checking constraints in later iterations. This prevents, for example,
517   // two identical constraints both being marked redundant since each is
518   // redundant given the other one. In this example, only the first of the
519   // constraints that is processed will get marked redundant, as it should be.
520   for (Unknown &u : con) {
521     if (u.orientation == Orientation::Column) {
522       unsigned column = u.pos;
523       Optional<unsigned> pivotRow = findPivotRow({}, Direction::Down, column);
524       // If no downward pivot is returned, the constraint is unbounded below
525       // and hence not redundant.
526       if (!pivotRow)
527         continue;
528       pivot(*pivotRow, column);
529     }
530 
531     unsigned row = u.pos;
532     Optional<Fraction> minimum = computeRowOptimum(Direction::Down, row);
533     if (!minimum || *minimum < Fraction(0, 1)) {
534       // Constraint is unbounded below or can attain negative sample values and
535       // hence is not redundant.
536       restoreRow(u);
537       continue;
538     }
539 
540     markRowRedundant(u);
541   }
542 }
543 
544 bool Simplex::isUnbounded() {
545   if (empty)
546     return false;
547 
548   SmallVector<int64_t, 8> dir(var.size() + 1);
549   for (unsigned i = 0; i < var.size(); ++i) {
550     dir[i] = 1;
551 
552     Optional<Fraction> maybeMax = computeOptimum(Direction::Up, dir);
553     if (!maybeMax)
554       return true;
555 
556     Optional<Fraction> maybeMin = computeOptimum(Direction::Down, dir);
557     if (!maybeMin)
558       return true;
559 
560     dir[i] = 0;
561   }
562   return false;
563 }
564 
565 /// Make a tableau to represent a pair of points in the original tableau.
566 ///
567 /// The product constraints and variables are stored as: first A's, then B's.
568 ///
569 /// The product tableau has row layout:
570 ///   A's redundant rows, B's redundant rows, A's other rows, B's other rows.
571 ///
572 /// It has column layout:
573 ///   denominator, constant, A's columns, B's columns.
574 Simplex Simplex::makeProduct(const Simplex &a, const Simplex &b) {
575   unsigned numVar = a.numVariables() + b.numVariables();
576   unsigned numCon = a.numConstraints() + b.numConstraints();
577   Simplex result(numVar);
578 
579   result.tableau.resizeVertically(numCon);
580   result.empty = a.empty || b.empty;
581 
582   auto concat = [](ArrayRef<Unknown> v, ArrayRef<Unknown> w) {
583     SmallVector<Unknown, 8> result;
584     result.reserve(v.size() + w.size());
585     result.insert(result.end(), v.begin(), v.end());
586     result.insert(result.end(), w.begin(), w.end());
587     return result;
588   };
589   result.con = concat(a.con, b.con);
590   result.var = concat(a.var, b.var);
591 
592   auto indexFromBIndex = [&](int index) {
593     return index >= 0 ? a.numVariables() + index
594                       : ~(a.numConstraints() + ~index);
595   };
596 
597   result.colUnknown.assign(2, nullIndex);
598   for (unsigned i = 2; i < a.nCol; ++i) {
599     result.colUnknown.push_back(a.colUnknown[i]);
600     result.unknownFromIndex(result.colUnknown.back()).pos =
601         result.colUnknown.size() - 1;
602   }
603   for (unsigned i = 2; i < b.nCol; ++i) {
604     result.colUnknown.push_back(indexFromBIndex(b.colUnknown[i]));
605     result.unknownFromIndex(result.colUnknown.back()).pos =
606         result.colUnknown.size() - 1;
607   }
608 
609   auto appendRowFromA = [&](unsigned row) {
610     for (unsigned col = 0; col < a.nCol; ++col)
611       result.tableau(result.nRow, col) = a.tableau(row, col);
612     result.rowUnknown.push_back(a.rowUnknown[row]);
613     result.unknownFromIndex(result.rowUnknown.back()).pos =
614         result.rowUnknown.size() - 1;
615     result.nRow++;
616   };
617 
618   // Also fixes the corresponding entry in rowUnknown and var/con (as the case
619   // may be).
620   auto appendRowFromB = [&](unsigned row) {
621     result.tableau(result.nRow, 0) = b.tableau(row, 0);
622     result.tableau(result.nRow, 1) = b.tableau(row, 1);
623 
624     unsigned offset = a.nCol - 2;
625     for (unsigned col = 2; col < b.nCol; ++col)
626       result.tableau(result.nRow, offset + col) = b.tableau(row, col);
627     result.rowUnknown.push_back(indexFromBIndex(b.rowUnknown[row]));
628     result.unknownFromIndex(result.rowUnknown.back()).pos =
629         result.rowUnknown.size() - 1;
630     result.nRow++;
631   };
632 
633   result.nRedundant = a.nRedundant + b.nRedundant;
634   for (unsigned row = 0; row < a.nRedundant; ++row)
635     appendRowFromA(row);
636   for (unsigned row = 0; row < b.nRedundant; ++row)
637     appendRowFromB(row);
638   for (unsigned row = a.nRedundant; row < a.nRow; ++row)
639     appendRowFromA(row);
640   for (unsigned row = b.nRedundant; row < b.nRow; ++row)
641     appendRowFromB(row);
642 
643   return result;
644 }
645 
646 Optional<SmallVector<int64_t, 8>> Simplex::getSamplePointIfIntegral() const {
647   // The tableau is empty, so no sample point exists.
648   if (empty)
649     return {};
650 
651   SmallVector<int64_t, 8> sample;
652   // Push the sample value for each variable into the vector.
653   for (const Unknown &u : var) {
654     if (u.orientation == Orientation::Column) {
655       // If the variable is in column position, its sample value is zero.
656       sample.push_back(0);
657     } else {
658       // If the variable is in row position, its sample value is the entry in
659       // the constant column divided by the entry in the common denominator
660       // column. If this is not an integer, then the sample point is not
661       // integral so we return None.
662       if (tableau(u.pos, 1) % tableau(u.pos, 0) != 0)
663         return {};
664       sample.push_back(tableau(u.pos, 1) / tableau(u.pos, 0));
665     }
666   }
667   return sample;
668 }
669 
670 /// Given a simplex for a polytope, construct a new simplex whose variables are
671 /// identified with a pair of points (x, y) in the original polytope. Supports
672 /// some operations needed for generalized basis reduction. In what follows,
673 /// dotProduct(x, y) = x_1 * y_1 + x_2 * y_2 + ... x_n * y_n where n is the
674 /// dimension of the original polytope.
675 ///
676 /// This supports adding equality constraints dotProduct(dir, x - y) == 0. It
677 /// also supports rolling back this addition, by maintaining a snapshot stack
678 /// that contains a snapshot of the Simplex's state for each equality, just
679 /// before that equality was added.
680 class GBRSimplex {
681   using Orientation = Simplex::Orientation;
682 
683 public:
684   GBRSimplex(const Simplex &originalSimplex)
685       : simplex(Simplex::makeProduct(originalSimplex, originalSimplex)),
686         simplexConstraintOffset(simplex.numConstraints()) {}
687 
688   /// Add an equality dotProduct(dir, x - y) == 0.
689   /// First pushes a snapshot for the current simplex state to the stack so
690   /// that this can be rolled back later.
691   void addEqualityForDirection(ArrayRef<int64_t> dir) {
692     assert(
693         std::any_of(dir.begin(), dir.end(), [](int64_t x) { return x != 0; }) &&
694         "Direction passed is the zero vector!");
695     snapshotStack.push_back(simplex.getSnapshot());
696     simplex.addEquality(getCoeffsForDirection(dir));
697   }
698 
699   /// Compute max(dotProduct(dir, x - y)) and save the dual variables for only
700   /// the direction equalities to `dual`.
701   Fraction computeWidthAndDuals(ArrayRef<int64_t> dir,
702                                 SmallVectorImpl<int64_t> &dual,
703                                 int64_t &dualDenom) {
704     unsigned snap = simplex.getSnapshot();
705     unsigned conIndex = simplex.addRow(getCoeffsForDirection(dir));
706     unsigned row = simplex.con[conIndex].pos;
707     Optional<Fraction> maybeWidth =
708         simplex.computeRowOptimum(Simplex::Direction::Up, row);
709     assert(maybeWidth.hasValue() && "Width should not be unbounded!");
710     dualDenom = simplex.tableau(row, 0);
711     dual.clear();
712     // The increment is i += 2 because equalities are added as two inequalities,
713     // one positive and one negative. Each iteration processes one equality.
714     for (unsigned i = simplexConstraintOffset; i < conIndex; i += 2) {
715       // The dual variable is the negative of the coefficient of the new row
716       // in the column of the constraint, if the constraint is in a column.
717       // Note that the second inequality for the equality is negated.
718       //
719       // We want the dual for the original equality. If the positive inequality
720       // is in column position, the negative of its row coefficient is the
721       // desired dual. If the negative inequality is in column position, its row
722       // coefficient is the desired dual. (its coefficients are already the
723       // negated coefficients of the original equality, so we don't need to
724       // negate it now.)
725       //
726       // If neither are in column position, we move the negated inequality to
727       // column position. Since the inequality must have sample value zero
728       // (since it corresponds to an equality), we are free to pivot with
729       // any column. Since both the unknowns have sample value before and after
730       // pivoting, no other sample values will change and the tableau will
731       // remain consistent. To pivot, we just need to find a column that has a
732       // non-zero coefficient in this row. There must be one since otherwise the
733       // equality would be 0 == 0, which should never be passed to
734       // addEqualityForDirection.
735       //
736       // After finding a column, we pivot with the column, after which we can
737       // get the dual from the inequality in column position as explained above.
738       if (simplex.con[i].orientation == Orientation::Column) {
739         dual.push_back(-simplex.tableau(row, simplex.con[i].pos));
740       } else {
741         if (simplex.con[i + 1].orientation == Orientation::Row) {
742           unsigned ineqRow = simplex.con[i + 1].pos;
743           // Since it is an equality, the sample value must be zero.
744           assert(simplex.tableau(ineqRow, 1) == 0 &&
745                  "Equality's sample value must be zero.");
746           for (unsigned col = 2; col < simplex.nCol; ++col) {
747             if (simplex.tableau(ineqRow, col) != 0) {
748               simplex.pivot(ineqRow, col);
749               break;
750             }
751           }
752           assert(simplex.con[i + 1].orientation == Orientation::Column &&
753                  "No pivot found. Equality has all-zeros row in tableau!");
754         }
755         dual.push_back(simplex.tableau(row, simplex.con[i + 1].pos));
756       }
757     }
758     simplex.rollback(snap);
759     return *maybeWidth;
760   }
761 
762   /// Remove the last equality that was added through addEqualityForDirection.
763   ///
764   /// We do this by rolling back to the snapshot at the top of the stack, which
765   /// should be a snapshot taken just before the last equality was added.
766   void removeLastEquality() {
767     assert(!snapshotStack.empty() && "Snapshot stack is empty!");
768     simplex.rollback(snapshotStack.back());
769     snapshotStack.pop_back();
770   }
771 
772 private:
773   /// Returns coefficients of the expression 'dot_product(dir, x - y)',
774   /// i.e.,   dir_1 * x_1 + dir_2 * x_2 + ... + dir_n * x_n
775   ///       - dir_1 * y_1 - dir_2 * y_2 - ... - dir_n * y_n,
776   /// where n is the dimension of the original polytope.
777   SmallVector<int64_t, 8> getCoeffsForDirection(ArrayRef<int64_t> dir) {
778     assert(2 * dir.size() == simplex.numVariables() &&
779            "Direction vector has wrong dimensionality");
780     SmallVector<int64_t, 8> coeffs(dir.begin(), dir.end());
781     coeffs.reserve(2 * dir.size());
782     for (int64_t coeff : dir)
783       coeffs.push_back(-coeff);
784     coeffs.push_back(0); // constant term
785     return coeffs;
786   }
787 
788   Simplex simplex;
789   /// The first index of the equality constraints, the index immediately after
790   /// the last constraint in the initial product simplex.
791   unsigned simplexConstraintOffset;
792   /// A stack of snapshots, used for rolling back.
793   SmallVector<unsigned, 8> snapshotStack;
794 };
795 
796 /// Reduce the basis to try and find a direction in which the polytope is
797 /// "thin". This only works for bounded polytopes.
798 ///
799 /// This is an implementation of the algorithm described in the paper
800 /// "An Implementation of Generalized Basis Reduction for Integer Programming"
801 /// by W. Cook, T. Rutherford, H. E. Scarf, D. Shallcross.
802 ///
803 /// Let b_{level}, b_{level + 1}, ... b_n be the current basis.
804 /// Let width_i(v) = max <v, x - y> where x and y are points in the original
805 /// polytope such that <b_j, x - y> = 0 is satisfied for all level <= j < i.
806 ///
807 /// In every iteration, we first replace b_{i+1} with b_{i+1} + u*b_i, where u
808 /// is the integer such that width_i(b_{i+1} + u*b_i) is minimized. Let dual_i
809 /// be the dual variable associated with the constraint <b_i, x - y> = 0 when
810 /// computing width_{i+1}(b_{i+1}). It can be shown that dual_i is the
811 /// minimizing value of u, if it were allowed to be fractional. Due to
812 /// convexity, the minimizing integer value is either floor(dual_i) or
813 /// ceil(dual_i), so we just need to check which of these gives a lower
814 /// width_{i+1} value. If dual_i turned out to be an integer, then u = dual_i.
815 ///
816 /// Now if width_i(b_{i+1}) < 0.75 * width_i(b_i), we swap b_i and (the new)
817 /// b_{i + 1} and decrement i (unless i = level, in which case we stay at the
818 /// same i). Otherwise, we increment i.
819 ///
820 /// We keep f values and duals cached and invalidate them when necessary.
821 /// Whenever possible, we use them instead of recomputing them. We implement the
822 /// algorithm as follows.
823 ///
824 /// In an iteration at i we need to compute:
825 ///   a) width_i(b_{i + 1})
826 ///   b) width_i(b_i)
827 ///   c) the integer u that minimizes width_i(b_{i + 1} + u*b_i)
828 ///
829 /// If width_i(b_i) is not already cached, we compute it.
830 ///
831 /// If the duals are not already cached, we compute width_{i+1}(b_{i+1}) and
832 /// store the duals from this computation.
833 ///
834 /// We call updateBasisWithUAndGetFCandidate, which finds the minimizing value
835 /// of u as explained before, caches the duals from this computation, sets
836 /// b_{i+1} to b_{i+1} + u*b_i, and returns the new value of width_i(b_{i+1}).
837 ///
838 /// Now if width_i(b_{i+1}) < 0.75 * width_i(b_i), we swap b_i and b_{i+1} and
839 /// decrement i, resulting in the basis
840 /// ... b_{i - 1}, b_{i + 1} + u*b_i, b_i, b_{i+2}, ...
841 /// with corresponding f values
842 /// ... width_{i-1}(b_{i-1}), width_i(b_{i+1} + u*b_i), width_{i+1}(b_i), ...
843 /// The values up to i - 1 remain unchanged. We have just gotten the middle
844 /// value from updateBasisWithUAndGetFCandidate, so we can update that in the
845 /// cache. The value at width_{i+1}(b_i) is unknown, so we evict this value from
846 /// the cache. The iteration after decrementing needs exactly the duals from the
847 /// computation of width_i(b_{i + 1} + u*b_i), so we keep these in the cache.
848 ///
849 /// When incrementing i, no cached f values get invalidated. However, the cached
850 /// duals do get invalidated as the duals for the higher levels are different.
851 void Simplex::reduceBasis(Matrix &basis, unsigned level) {
852   const Fraction epsilon(3, 4);
853 
854   if (level == basis.getNumRows() - 1)
855     return;
856 
857   GBRSimplex gbrSimplex(*this);
858   SmallVector<Fraction, 8> width;
859   SmallVector<int64_t, 8> dual;
860   int64_t dualDenom;
861 
862   // Finds the value of u that minimizes width_i(b_{i+1} + u*b_i), caches the
863   // duals from this computation, sets b_{i+1} to b_{i+1} + u*b_i, and returns
864   // the new value of width_i(b_{i+1}).
865   //
866   // If dual_i is not an integer, the minimizing value must be either
867   // floor(dual_i) or ceil(dual_i). We compute the expression for both and
868   // choose the minimizing value.
869   //
870   // If dual_i is an integer, we don't need to perform these computations. We
871   // know that in this case,
872   //   a) u = dual_i.
873   //   b) one can show that dual_j for j < i are the same duals we would have
874   //      gotten from computing width_i(b_{i + 1} + u*b_i), so the correct duals
875   //      are the ones already in the cache.
876   //   c) width_i(b_{i+1} + u*b_i) = min_{alpha} width_i(b_{i+1} + alpha * b_i),
877   //   which
878   //      one can show is equal to width_{i+1}(b_{i+1}). The latter value must
879   //      be in the cache, so we get it from there and return it.
880   auto updateBasisWithUAndGetFCandidate = [&](unsigned i) -> Fraction {
881     assert(i < level + dual.size() && "dual_i is not known!");
882 
883     int64_t u = floorDiv(dual[i - level], dualDenom);
884     basis.addToRow(i, i + 1, u);
885     if (dual[i - level] % dualDenom != 0) {
886       SmallVector<int64_t, 8> candidateDual[2];
887       int64_t candidateDualDenom[2];
888       Fraction widthI[2];
889 
890       // Initially u is floor(dual) and basis reflects this.
891       widthI[0] = gbrSimplex.computeWidthAndDuals(
892           basis.getRow(i + 1), candidateDual[0], candidateDualDenom[0]);
893 
894       // Now try ceil(dual), i.e. floor(dual) + 1.
895       ++u;
896       basis.addToRow(i, i + 1, 1);
897       widthI[1] = gbrSimplex.computeWidthAndDuals(
898           basis.getRow(i + 1), candidateDual[1], candidateDualDenom[1]);
899 
900       unsigned j = widthI[0] < widthI[1] ? 0 : 1;
901       if (j == 0)
902         // Subtract 1 to go from u = ceil(dual) back to floor(dual).
903         basis.addToRow(i, i + 1, -1);
904       dual = std::move(candidateDual[j]);
905       dualDenom = candidateDualDenom[j];
906       return widthI[j];
907     }
908     assert(i + 1 - level < width.size() && "width_{i+1} wasn't saved");
909     // When dual minimizes f_i(b_{i+1} + dual*b_i), this is equal to
910     // width_{i+1}(b_{i+1}).
911     return width[i + 1 - level];
912   };
913 
914   // In the ith iteration of the loop, gbrSimplex has constraints for directions
915   // from `level` to i - 1.
916   unsigned i = level;
917   while (i < basis.getNumRows() - 1) {
918     if (i >= level + width.size()) {
919       // We don't even know the value of f_i(b_i), so let's find that first.
920       // We have to do this first since later we assume that width already
921       // contains values up to and including i.
922 
923       assert((i == 0 || i - 1 < level + width.size()) &&
924              "We are at level i but we don't know the value of width_{i-1}");
925 
926       // We don't actually use these duals at all, but it doesn't matter
927       // because this case should only occur when i is level, and there are no
928       // duals in that case anyway.
929       assert(i == level && "This case should only occur when i == level");
930       width.push_back(
931           gbrSimplex.computeWidthAndDuals(basis.getRow(i), dual, dualDenom));
932     }
933 
934     if (i >= level + dual.size()) {
935       assert(i + 1 >= level + width.size() &&
936              "We don't know dual_i but we know width_{i+1}");
937       // We don't know dual for our level, so let's find it.
938       gbrSimplex.addEqualityForDirection(basis.getRow(i));
939       width.push_back(gbrSimplex.computeWidthAndDuals(basis.getRow(i + 1), dual,
940                                                       dualDenom));
941       gbrSimplex.removeLastEquality();
942     }
943 
944     // This variable stores width_i(b_{i+1} + u*b_i).
945     Fraction widthICandidate = updateBasisWithUAndGetFCandidate(i);
946     if (widthICandidate < epsilon * width[i - level]) {
947       basis.swapRows(i, i + 1);
948       width[i - level] = widthICandidate;
949       // The values of width_{i+1}(b_{i+1}) and higher may change after the
950       // swap, so we remove the cached values here.
951       width.resize(i - level + 1);
952       if (i == level) {
953         dual.clear();
954         continue;
955       }
956 
957       gbrSimplex.removeLastEquality();
958       i--;
959       continue;
960     }
961 
962     // Invalidate duals since the higher level needs to recompute its own duals.
963     dual.clear();
964     gbrSimplex.addEqualityForDirection(basis.getRow(i));
965     i++;
966   }
967 }
968 
969 /// Search for an integer sample point using a branch and bound algorithm.
970 ///
971 /// Each row in the basis matrix is a vector, and the set of basis vectors
972 /// should span the space. Initially this is the identity matrix,
973 /// i.e., the basis vectors are just the variables.
974 ///
975 /// In every level, a value is assigned to the level-th basis vector, as
976 /// follows. Compute the minimum and maximum rational values of this direction.
977 /// If only one integer point lies in this range, constrain the variable to
978 /// have this value and recurse to the next variable.
979 ///
980 /// If the range has multiple values, perform generalized basis reduction via
981 /// reduceBasis and then compute the bounds again. Now we try constraining
982 /// this direction in the first value in this range and "recurse" to the next
983 /// level. If we fail to find a sample, we try assigning the direction the next
984 /// value in this range, and so on.
985 ///
986 /// If no integer sample is found from any of the assignments, or if the range
987 /// contains no integer value, then of course the polytope is empty for the
988 /// current assignment of the values in previous levels, so we return to
989 /// the previous level.
990 ///
991 /// If we reach the last level where all the variables have been assigned values
992 /// already, then we simply return the current sample point if it is integral,
993 /// and go back to the previous level otherwise.
994 ///
995 /// To avoid potentially arbitrarily large recursion depths leading to stack
996 /// overflows, this algorithm is implemented iteratively.
997 Optional<SmallVector<int64_t, 8>> Simplex::findIntegerSample() {
998   if (empty)
999     return {};
1000 
1001   unsigned nDims = var.size();
1002   Matrix basis = Matrix::identity(nDims);
1003 
1004   unsigned level = 0;
1005   // The snapshot just before constraining a direction to a value at each level.
1006   SmallVector<unsigned, 8> snapshotStack;
1007   // The maximum value in the range of the direction for each level.
1008   SmallVector<int64_t, 8> upperBoundStack;
1009   // The next value to try constraining the basis vector to at each level.
1010   SmallVector<int64_t, 8> nextValueStack;
1011 
1012   snapshotStack.reserve(basis.getNumRows());
1013   upperBoundStack.reserve(basis.getNumRows());
1014   nextValueStack.reserve(basis.getNumRows());
1015   while (level != -1u) {
1016     if (level == basis.getNumRows()) {
1017       // We've assigned values to all variables. Return if we have a sample,
1018       // or go back up to the previous level otherwise.
1019       if (auto maybeSample = getSamplePointIfIntegral())
1020         return maybeSample;
1021       level--;
1022       continue;
1023     }
1024 
1025     if (level >= upperBoundStack.size()) {
1026       // We haven't populated the stack values for this level yet, so we have
1027       // just come down a level ("recursed"). Find the lower and upper bounds.
1028       // If there is more than one integer point in the range, perform
1029       // generalized basis reduction.
1030       SmallVector<int64_t, 8> basisCoeffs =
1031           llvm::to_vector<8>(basis.getRow(level));
1032       basisCoeffs.push_back(0);
1033 
1034       int64_t minRoundedUp, maxRoundedDown;
1035       std::tie(minRoundedUp, maxRoundedDown) =
1036           computeIntegerBounds(basisCoeffs);
1037 
1038       // Heuristic: if the sample point is integral at this point, just return
1039       // it.
1040       if (auto maybeSample = getSamplePointIfIntegral())
1041         return *maybeSample;
1042 
1043       if (minRoundedUp < maxRoundedDown) {
1044         reduceBasis(basis, level);
1045         basisCoeffs = llvm::to_vector<8>(basis.getRow(level));
1046         basisCoeffs.push_back(0);
1047         std::tie(minRoundedUp, maxRoundedDown) =
1048             computeIntegerBounds(basisCoeffs);
1049       }
1050 
1051       snapshotStack.push_back(getSnapshot());
1052       // The smallest value in the range is the next value to try.
1053       nextValueStack.push_back(minRoundedUp);
1054       upperBoundStack.push_back(maxRoundedDown);
1055     }
1056 
1057     assert((snapshotStack.size() - 1 == level &&
1058             nextValueStack.size() - 1 == level &&
1059             upperBoundStack.size() - 1 == level) &&
1060            "Mismatched variable stack sizes!");
1061 
1062     // Whether we "recursed" or "returned" from a lower level, we rollback
1063     // to the snapshot of the starting state at this level. (in the "recursed"
1064     // case this has no effect)
1065     rollback(snapshotStack.back());
1066     int64_t nextValue = nextValueStack.back();
1067     nextValueStack.back()++;
1068     if (nextValue > upperBoundStack.back()) {
1069       // We have exhausted the range and found no solution. Pop the stack and
1070       // return up a level.
1071       snapshotStack.pop_back();
1072       nextValueStack.pop_back();
1073       upperBoundStack.pop_back();
1074       level--;
1075       continue;
1076     }
1077 
1078     // Try the next value in the range and "recurse" into the next level.
1079     SmallVector<int64_t, 8> basisCoeffs(basis.getRow(level).begin(),
1080                                         basis.getRow(level).end());
1081     basisCoeffs.push_back(-nextValue);
1082     addEquality(basisCoeffs);
1083     level++;
1084   }
1085 
1086   return {};
1087 }
1088 
1089 /// Compute the minimum and maximum integer values the expression can take. We
1090 /// compute each separately.
1091 std::pair<int64_t, int64_t>
1092 Simplex::computeIntegerBounds(ArrayRef<int64_t> coeffs) {
1093   int64_t minRoundedUp;
1094   if (Optional<Fraction> maybeMin =
1095           computeOptimum(Simplex::Direction::Down, coeffs))
1096     minRoundedUp = ceil(*maybeMin);
1097   else
1098     llvm_unreachable("Tableau should not be unbounded");
1099 
1100   int64_t maxRoundedDown;
1101   if (Optional<Fraction> maybeMax =
1102           computeOptimum(Simplex::Direction::Up, coeffs))
1103     maxRoundedDown = floor(*maybeMax);
1104   else
1105     llvm_unreachable("Tableau should not be unbounded");
1106 
1107   return {minRoundedUp, maxRoundedDown};
1108 }
1109 
1110 void Simplex::print(raw_ostream &os) const {
1111   os << "rows = " << nRow << ", columns = " << nCol << "\n";
1112   if (empty)
1113     os << "Simplex marked empty!\n";
1114   os << "var: ";
1115   for (unsigned i = 0; i < var.size(); ++i) {
1116     if (i > 0)
1117       os << ", ";
1118     var[i].print(os);
1119   }
1120   os << "\ncon: ";
1121   for (unsigned i = 0; i < con.size(); ++i) {
1122     if (i > 0)
1123       os << ", ";
1124     con[i].print(os);
1125   }
1126   os << '\n';
1127   for (unsigned row = 0; row < nRow; ++row) {
1128     if (row > 0)
1129       os << ", ";
1130     os << "r" << row << ": " << rowUnknown[row];
1131   }
1132   os << '\n';
1133   os << "c0: denom, c1: const";
1134   for (unsigned col = 2; col < nCol; ++col)
1135     os << ", c" << col << ": " << colUnknown[col];
1136   os << '\n';
1137   for (unsigned row = 0; row < nRow; ++row) {
1138     for (unsigned col = 0; col < nCol; ++col)
1139       os << tableau(row, col) << '\t';
1140     os << '\n';
1141   }
1142   os << '\n';
1143 }
1144 
1145 void Simplex::dump() const { print(llvm::errs()); }
1146 
1147 } // namespace mlir
1148