- Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][presburger] Implement moveColumns using std::rotate #168243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
linuxlonelyeagle wants to merge 1 commit into llvm:main Choose a base branch from linuxlonelyeagle:imporve-moveColumns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+20 −24
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Member
| @llvm/pr-subscribers-mlir Author: lonely eagle (linuxlonelyeagle) ChangesFull diff: https://github.com/llvm/llvm-project/pull/168243.diff 1 Files Affected:
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> |
Member
| @llvm/pr-subscribers-mlir-presburger Author: lonely eagle (linuxlonelyeagle) ChangesFull diff: https://github.com/llvm/llvm-project/pull/168243.diff 1 Files Affected:
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> |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
No description provided.