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

lonely eagle llvmlistbot at llvm.org
Mon Dec 1 17:53:03 PST 2025


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

>From 3401dd23da410728627b8ce93b5ee5aea6ea42c8 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Sat, 15 Nov 2025 21:26:35 +0000
Subject: [PATCH 1/4] Implement moveColumns using std::rotate.

---
 mlir/lib/Analysis/Presburger/Matrix.cpp | 44 +++++++++++--------------
 1 file changed, 20 insertions(+), 24 deletions(-)

diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index bb6056487512a..7933bec8d6da1 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -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)
@@ -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>

>From aeb6c1c48c1f4bbddd809514043db0f1dea3f491 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Sat, 22 Nov 2025 11:18:23 +0000
Subject: [PATCH 2/4] update code and rebase main.

---
 mlir/lib/Analysis/Presburger/Matrix.cpp | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 7933bec8d6da1..01c607c09f2b3 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -271,23 +271,21 @@ void Matrix<T>::moveColumns(unsigned srcPos, unsigned num, unsigned dstPos) {
          "move destination range exceeds matrix columns");
 
   unsigned numRows = getNumRows();
-  unsigned numCols = getNumReservedColumns();
 
   if (offset > 0) {
-    // shift the matrix left, see
-    // https://en.cppreference.com/w/cpp/algorithm/rotate.html.
+    // 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) {
-      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);
+      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);
   }
 }
 

>From eeb2ca4698b18cf577d8b59b6c4b19d077cb483e Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Mon, 1 Dec 2025 17:40:54 +0000
Subject: [PATCH 3/4] update code.

---
 mlir/lib/Analysis/Presburger/Matrix.cpp | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 01c607c09f2b3..7be1475d83ae3 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -261,8 +261,7 @@ void Matrix<T>::moveColumns(unsigned srcPos, unsigned num, unsigned dstPos) {
   if (num == 0)
     return;
 
-  int offset = dstPos - srcPos;
-  if (offset == 0)
+  if (dstPos == srcPos)
     return;
 
   assert(srcPos + num <= getNumColumns() &&
@@ -272,18 +271,16 @@ void Matrix<T>::moveColumns(unsigned srcPos, unsigned num, unsigned dstPos) {
 
   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).
+  if (dstPos > srcPos) {
+    // std::rotate(start, middle, end) permutes the elements of [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).
+  // std::rotate(start, middle, end) permutes the elements of [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);
   }

>From cb4fc5e21efec7394d9eac160b970dc372a2f8da Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Tue, 2 Dec 2025 01:52:47 +0000
Subject: [PATCH 4/4] change comment.

---
 mlir/lib/Analysis/Presburger/Matrix.cpp | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 7be1475d83ae3..83a2c280c3d4e 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -270,17 +270,15 @@ void Matrix<T>::moveColumns(unsigned srcPos, unsigned num, unsigned dstPos) {
          "move destination range exceeds matrix columns");
 
   unsigned numRows = getNumRows();
-
+  // std::rotate(start, middle, end) permutes the elements of [start, end] to
+  // [middle, end) + [start, middle). NOTE: &at(i, srcPos + num) will trigger an
+  // assert.
   if (dstPos > srcPos) {
-    // std::rotate(start, middle, end) permutes the elements of [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;
   }
-  // std::rotate(start, middle, end) permutes the elements of [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);
   }



More information about the Mlir-commits mailing list