[mlir][presburger] Avoid redundant zero-initialization in insertColumns#199911
Open
linuxlonelyeagle wants to merge 1 commit into
Open
[mlir][presburger] Avoid redundant zero-initialization in insertColumns#199911linuxlonelyeagle wants to merge 1 commit into
linuxlonelyeagle wants to merge 1 commit into
Conversation
|
@llvm/pr-subscribers-mlir-presburger @llvm/pr-subscribers-mlir Author: lonely eagle (linuxlonelyeagle) ChangesWhen insertColumns does not trigger a physical reallocation, the inner loop needlessly loops up to nReservedColumns - 1. This causes massive redundant zero-writes on trailing columns that are already zero. This patch truncates the inner loop start boundary to nColumns - 1, when the reserved capacity is unchanged, optimizing the non-realloc path from O(nRows * nReservedColumns) to O(nRows * nColumns). Full diff: https://github.com/llvm/llvm-project/pull/199911.diff 1 Files Affected:
diff --git a/mlir/lib/Analysis/Presburger/Matrix.cpp b/mlir/lib/Analysis/Presburger/Matrix.cpp
index 89d8a15422d95..7c55caa767c45 100644
--- a/mlir/lib/Analysis/Presburger/Matrix.cpp
+++ b/mlir/lib/Analysis/Presburger/Matrix.cpp
@@ -160,17 +160,17 @@ void Matrix<T>::insertColumns(unsigned pos, unsigned count) {
}
nColumns += count;
+ int colStart = nColumns - 1;
+ if (oldNReservedColumns != nReservedColumns)
+ colStart = nReservedColumns - 1;
for (int ri = nRows - 1; ri >= 0; --ri) {
- for (int ci = nReservedColumns - 1; ci >= 0; --ci) {
+ for (int ci = colStart; ci >= 0; --ci) {
unsigned r = ri;
unsigned c = ci;
T &dest = data[r * nReservedColumns + c];
if (c >= nColumns) { // NOLINT
// Out of bounds columns are zero-initialized. NOLINT because clang-tidy
// complains about this branch being the same as the c >= pos one.
- //
- // TODO: this case can be skipped if the number of reserved columns
- // didn't change.
dest = 0;
} else if (c >= pos + count) {
// Shift the data occuring after the inserted columns.
|
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
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.
When insertColumns does not trigger a physical reallocation, the inner loop needlessly loops up to nReservedColumns - 1. This causes massive redundant zero-writes on trailing columns that are already zero. This patch truncates the inner loop start boundary to nColumns - 1, when the reserved capacity is unchanged, optimizing the non-realloc path from O(nRows * nReservedColumns) to O(nRows * nColumns).