[Mlir-commits] [mlir] [mlir][presburger] Implement moveColumns using std::rotate (PR #168243)

lonely eagle llvmlistbot at llvm.org
Mon Dec 1 09:42:52 PST 2025


================
@@ -276,23 +270,23 @@ 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();
+
+  if (offset > 0) {
+    // Shift the matrix left, because start < end, that std::rotate(start,
+    // middle, end) turns the range [start, end] to [middle, end) + [start,
+    // middle).
+    for (unsigned i = 0; i < numRows; ++i) {
+      std::rotate(&at(i, srcPos), &at(i, srcPos) + num, &at(i, dstPos) + num);
+    }
+    return;
+  }
+  // Shift the matrix right. because end < start, that std::rotate(start,
+  // middle, end) turns the range [start, end] to [middle, start) + [end,
+  // middle).
+  for (unsigned i = 0; i < numRows; ++i) {
+    std::rotate(&at(i, dstPos), &at(i, srcPos), &at(i, srcPos) + num);
+  }
----------------
linuxlonelyeagle wrote:

It will trigger `assert(column < nColumns && "Column outside of range");`.

https://github.com/llvm/llvm-project/pull/168243


More information about the Mlir-commits mailing list