Skip to content

[mlir][presburger] Avoid redundant zero-initialization in insertColumns#199911

Open
linuxlonelyeagle wants to merge 1 commit into
llvm:mainfrom
linuxlonelyeagle:avoid-redundant-zero-init
Open

[mlir][presburger] Avoid redundant zero-initialization in insertColumns#199911
linuxlonelyeagle wants to merge 1 commit into
llvm:mainfrom
linuxlonelyeagle:avoid-redundant-zero-init

Conversation

@linuxlonelyeagle
Copy link
Copy Markdown
Member

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).

@llvmorg-github-actions
Copy link
Copy Markdown

llvmorg-github-actions Bot commented May 27, 2026

@llvm/pr-subscribers-mlir-presburger

@llvm/pr-subscribers-mlir

Author: lonely eagle (linuxlonelyeagle)

Changes

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).


Full diff: https://github.com/llvm/llvm-project/pull/199911.diff

1 Files Affected:

  • (modified) mlir/lib/Analysis/Presburger/Matrix.cpp (+4-4)
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant