Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions mlir/lib/Analysis/Presburger/Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,7 @@ void Matrix<T>::fillRow(unsigned row, const T &value) {
}

// moveColumns is implemented by moving the columns adjacent to the source range
// to their final position. When moving right (i.e. dstPos > srcPos), the range
// of the adjacent columns is [srcPos + num, dstPos + num). When moving left
// (i.e. dstPos < srcPos) the range of the adjacent columns is [dstPos, srcPos).
// First, zeroed out columns are inserted in the final positions of the adjacent
// columns. Then, the adjacent columns are moved to their final positions by
// swapping them with the zeroed columns. Finally, the now zeroed adjacent
// columns are deleted.
// to their final position.
template <typename T>
void Matrix<T>::moveColumns(unsigned srcPos, unsigned num, unsigned dstPos) {
if (num == 0)
Expand All @@ -276,23 +270,25 @@ void Matrix<T>::moveColumns(unsigned srcPos, unsigned num, unsigned dstPos) {
assert(dstPos + num <= getNumColumns() &&
"move destination range exceeds matrix columns");

unsigned insertCount = offset > 0 ? offset : -offset;
unsigned finalAdjStart = offset > 0 ? srcPos : srcPos + num;
unsigned curAdjStart = offset > 0 ? srcPos + num : dstPos;
// TODO: This can be done using std::rotate.
// Insert new zero columns in the positions where the adjacent columns are to
// be moved.
insertColumns(finalAdjStart, insertCount);
// Update curAdjStart if insertion of new columns invalidates it.
if (finalAdjStart < curAdjStart)
curAdjStart += insertCount;

// Swap the adjacent columns with inserted zero columns.
for (unsigned i = 0; i < insertCount; ++i)
swapColumns(finalAdjStart + i, curAdjStart + i);

// Delete the now redundant zero columns.
removeColumns(curAdjStart, insertCount);
unsigned numRows = getNumRows();
unsigned numCols = getNumReservedColumns();

if (offset > 0) {
// shift the matrix left, see
// https://en.cppreference.com/w/cpp/algorithm/rotate.html.
for (unsigned i = 0; i < numRows; ++i) {
auto begin = data.begin() + i * numCols + srcPos;
auto end = data.begin() + i * numCols + dstPos + num;
std::rotate(begin, begin + num, end);
}
} else {
// shift the matrix right.
for (unsigned i = 0; i < numRows; ++i) {
auto begin = data.begin() + i * numCols + srcPos + num;
auto end = data.begin() + i * numCols + dstPos;
std::rotate(end, begin - num, begin);
}
}
}

template <typename T>
Expand Down
Loading