[Mlir-commits] [mlir] [mlir][presburger] Avoid redundant zero-initialization in insertColumns (PR #199911)
lonely eagle
llvmlistbot at llvm.org
Wed May 27 02:31:02 PDT 2026
https://github.com/linuxlonelyeagle created https://github.com/llvm/llvm-project/pull/199911
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).
>From 127d5e95de47c95a4215eb4362215b17f093e6a2 Mon Sep 17 00:00:00 2001
From: linuxlonelyeagle <2020382038 at qq.com>
Date: Wed, 27 May 2026 09:23:43 +0000
Subject: [PATCH] Avoid redundant zero-initialization in insertColumns.
---
mlir/lib/Analysis/Presburger/Matrix.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
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.
More information about the Mlir-commits
mailing list