Skip to content

Commit 2740273

Browse files
authored
[MLIR][Presburger] Make printing aligned to assist in debugging (#107648)
Hello Arjun! Please allow me to contribute this patch as it helps me debugging significantly! When the 1's and 0's don't line up when debugging farkas lemma of numerous polyhedrons using simplex lexmin solver, it is truly straining on the eyes. Hopefully this patch can help others! The unfortunate part is the lack of testcase as I'm not sure how to add testcase for debug dumps. :) However, you can add this testcase to the SimplexTest.cpp to witness the nice printing! ```c++ TEST(SimplexTest, DumpTest) { int COLUMNS = 2; int ROWS = 2; LexSimplex simplex(COLUMNS * 2); IntMatrix m1(ROWS, COLUMNS * 2 + 1); // Adding LHS columns. for (int i = 0; i < ROWS; i++) { // an arbitrary formula to test all kinds of integers for (int j = 0; j < COLUMNS; j++) m1(i, j) = i + (2 << (i % 3)) * (-1 * ((i + j) % 2)); } // Adding RHS columns. for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) m1(i, j + COLUMNS) = j - (3 << (j % 4)) * (-1 * ((i + j * 2) % 2)); } for (int i = 0; i < m1.getNumRows(); i++) { ArrayRef<DynamicAPInt> curRow = m1.getRow(i); simplex.addInequality(curRow); } IntegerRelation rel = parseRelationFromSet("(x, y, z)[] : (z - x - 17 * y == 0, x - 11 * z >= 1)",2); simplex.dump(); m1.dump(); rel.dump(); } ``` ``` rows = 2, columns = 7 var: c3, c4, c5, c6 con: r0 [>=0], r1 [>=0] r0: -1, r1: -2 c0: denom, c1: const, c2: 2147483647, c3: 0, c4: 1, c5: 2, c6: 3 1 0 1 0 -2 0 1 1 0 -8 -3 1 3 7 0 -2 0 1 0 -3 1 3 7 0 Domain: 2, Range: 1, Symbols: 0, Locals: 0 2 constraints -1 -17 1 0 = 0 1 0 -11 -1 >= 0 ```
1 parent 5e80fc8 commit 2740273

File tree

4 files changed

+84
-11
lines changed

4 files changed

+84
-11
lines changed

mlir/include/mlir/Analysis/Presburger/Utils.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
#include "llvm/ADT/DynamicAPInt.h"
1818
#include "llvm/ADT/STLExtras.h"
1919
#include "llvm/ADT/SmallBitVector.h"
20+
#include "llvm/Support/raw_ostream.h"
2021
#include <optional>
22+
#include <string>
2123

2224
namespace mlir {
2325
namespace presburger {
@@ -292,6 +294,54 @@ std::vector<Fraction> multiplyPolynomials(ArrayRef<Fraction> a,
292294

293295
bool isRangeZero(ArrayRef<Fraction> arr);
294296

297+
/// Example usage:
298+
/// Print .12, 3.4, 56.7
299+
/// preAlign = ".", minSpacing = 1,
300+
/// .12 .12
301+
/// 3.4 3.4
302+
/// 56.7 56.7
303+
struct PrintTableMetrics {
304+
// If unknown, set to 0 and pass the struct into updatePrintMetrics.
305+
unsigned maxPreIndent;
306+
unsigned maxPostIndent;
307+
std::string preAlign;
308+
};
309+
310+
/// Iterate over each val in the table and update 'm' where
311+
/// .maxPreIndent and .maxPostIndent are initialized to 0.
312+
/// class T is any type that can be handled by llvm::raw_string_ostream.
313+
template <class T>
314+
void updatePrintMetrics(T val, PrintTableMetrics &m) {
315+
std::string str;
316+
llvm::raw_string_ostream(str) << val;
317+
if (str.empty())
318+
return;
319+
unsigned preIndent = str.find(m.preAlign);
320+
preIndent = (preIndent != std::string::npos) ? preIndent + 1 : 0;
321+
m.maxPreIndent = std::max(m.maxPreIndent, preIndent);
322+
m.maxPostIndent =
323+
std::max(m.maxPostIndent, (unsigned int)(str.length() - preIndent));
324+
}
325+
326+
/// Print val in the table with metrics specified in 'm'.
327+
template <class T>
328+
void printWithPrintMetrics(raw_ostream &os, T val, unsigned minSpacing,
329+
const PrintTableMetrics &m) {
330+
std::string str;
331+
llvm::raw_string_ostream(str) << val;
332+
unsigned preIndent;
333+
if (!str.empty()) {
334+
preIndent = str.find(m.preAlign);
335+
preIndent = (preIndent != std::string::npos) ? preIndent + 1 : 0;
336+
} else {
337+
preIndent = 0;
338+
}
339+
for (unsigned i = 0; i < (minSpacing + m.maxPreIndent - preIndent); ++i)
340+
os << " ";
341+
os << str;
342+
for (unsigned i = 0; i < m.maxPostIndent - (str.length() - preIndent); ++i)
343+
os << " ";
344+
}
295345
} // namespace presburger
296346
} // namespace mlir
297347

mlir/lib/Analysis/Presburger/IntegerRelation.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
#include <cassert>
3333
#include <functional>
3434
#include <memory>
35+
#include <numeric>
3536
#include <optional>
37+
#include <sstream>
38+
#include <string>
3639
#include <utility>
3740
#include <vector>
3841

@@ -2589,19 +2592,26 @@ void IntegerRelation::mergeAndCompose(const IntegerRelation &other) {
25892592
void IntegerRelation::print(raw_ostream &os) const {
25902593
assert(hasConsistentState());
25912594
printSpace(os);
2595+
PrintTableMetrics ptm = {0, 0, "-"};
2596+
for (unsigned i = 0, e = getNumEqualities(); i < e; ++i)
2597+
for (unsigned j = 0, f = getNumCols(); j < f; ++j)
2598+
updatePrintMetrics<DynamicAPInt>(atEq(i, j), ptm);
2599+
for (unsigned i = 0, e = getNumInequalities(); i < e; ++i)
2600+
for (unsigned j = 0, f = getNumCols(); j < f; ++j)
2601+
updatePrintMetrics<DynamicAPInt>(atIneq(i, j), ptm);
2602+
// Print using PrintMetrics.
2603+
unsigned MIN_SPACING = 1;
25922604
for (unsigned i = 0, e = getNumEqualities(); i < e; ++i) {
2593-
os << " ";
25942605
for (unsigned j = 0, f = getNumCols(); j < f; ++j) {
2595-
os << atEq(i, j) << "\t";
2606+
printWithPrintMetrics<DynamicAPInt>(os, atEq(i, j), MIN_SPACING, ptm);
25962607
}
2597-
os << "= 0\n";
2608+
os << " = 0\n";
25982609
}
25992610
for (unsigned i = 0, e = getNumInequalities(); i < e; ++i) {
2600-
os << " ";
26012611
for (unsigned j = 0, f = getNumCols(); j < f; ++j) {
2602-
os << atIneq(i, j) << "\t";
2612+
printWithPrintMetrics<DynamicAPInt>(os, atIneq(i, j), MIN_SPACING, ptm);
26032613
}
2604-
os << ">= 0\n";
2614+
os << " >= 0\n";
26052615
}
26062616
os << '\n';
26072617
}

mlir/lib/Analysis/Presburger/Matrix.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,16 @@ Matrix<T> Matrix<T>::getSubMatrix(unsigned fromRow, unsigned toRow,
398398

399399
template <typename T>
400400
void Matrix<T>::print(raw_ostream &os) const {
401-
for (unsigned row = 0; row < nRows; ++row) {
401+
PrintTableMetrics ptm = {0, 0, "-"};
402+
for (unsigned row = 0; row < nRows; ++row)
402403
for (unsigned column = 0; column < nColumns; ++column)
403-
os << at(row, column) << ' ';
404-
os << '\n';
404+
updatePrintMetrics<T>(at(row, column), ptm);
405+
unsigned MIN_SPACING = 1;
406+
for (unsigned row = 0; row < nRows; ++row) {
407+
for (unsigned column = 0; column < nColumns; ++column) {
408+
printWithPrintMetrics<T>(os, at(row, column), MIN_SPACING, ptm);
409+
}
410+
os << "\n";
405411
}
406412
}
407413

mlir/lib/Analysis/Presburger/Simplex.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,9 +2153,16 @@ void SimplexBase::print(raw_ostream &os) const {
21532153
for (unsigned col = 2, e = getNumColumns(); col < e; ++col)
21542154
os << ", c" << col << ": " << colUnknown[col];
21552155
os << '\n';
2156-
for (unsigned row = 0, numRows = getNumRows(); row < numRows; ++row) {
2156+
PrintTableMetrics ptm = {0, 0, "-"};
2157+
for (unsigned row = 0, numRows = getNumRows(); row < numRows; ++row)
21572158
for (unsigned col = 0, numCols = getNumColumns(); col < numCols; ++col)
2158-
os << tableau(row, col) << '\t';
2159+
updatePrintMetrics<DynamicAPInt>(tableau(row, col), ptm);
2160+
unsigned MIN_SPACING = 1;
2161+
for (unsigned row = 0, numRows = getNumRows(); row < numRows; ++row) {
2162+
for (unsigned col = 0, numCols = getNumColumns(); col < numCols; ++col) {
2163+
printWithPrintMetrics<DynamicAPInt>(os, tableau(row, col), MIN_SPACING,
2164+
ptm);
2165+
}
21592166
os << '\n';
21602167
}
21612168
os << '\n';

0 commit comments

Comments
 (0)