[llvm] [LoopCacheAnalysis] Replace delinearization for fixed size array (PR #164798)

Ryotaro Kasuga via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 19 04:28:09 PST 2025


https://github.com/kasuga-fj updated https://github.com/llvm/llvm-project/pull/164798

>From bd15ed3ab23376303fdb5e1bfefb0cc19bd9e20f Mon Sep 17 00:00:00 2001
From: Ryotaro Kasuga <kasuga.ryotaro at fujitsu.com>
Date: Thu, 16 Oct 2025 11:57:46 +0000
Subject: [PATCH 1/4] [LoopCacheAnalysis] Replace delinearization for fixed
 size array

---
 .../include/llvm/Analysis/LoopCacheAnalysis.h |  3 ++-
 llvm/lib/Analysis/LoopCacheAnalysis.cpp       | 26 ++++++++-----------
 .../interchange-refcost-overflow.ll           |  2 +-
 3 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/llvm/include/llvm/Analysis/LoopCacheAnalysis.h b/llvm/include/llvm/Analysis/LoopCacheAnalysis.h
index 3e22487e5e349..70ccd8aaed20f 100644
--- a/llvm/include/llvm/Analysis/LoopCacheAnalysis.h
+++ b/llvm/include/llvm/Analysis/LoopCacheAnalysis.h
@@ -102,7 +102,8 @@ class IndexedReference {
 
   /// Attempt to delinearize \p AccessFn for fixed-size arrays.
   bool tryDelinearizeFixedSize(const SCEV *AccessFn,
-                               SmallVectorImpl<const SCEV *> &Subscripts);
+                               SmallVectorImpl<const SCEV *> &Subscripts,
+                               const SCEV *ElementSize);
 
   /// Return true if the index reference is invariant with respect to loop \p L.
   bool isLoopInvariant(const Loop &L) const;
diff --git a/llvm/lib/Analysis/LoopCacheAnalysis.cpp b/llvm/lib/Analysis/LoopCacheAnalysis.cpp
index 050c32707596a..c1ce736bb51b0 100644
--- a/llvm/lib/Analysis/LoopCacheAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopCacheAnalysis.cpp
@@ -355,22 +355,18 @@ CacheCostTy IndexedReference::computeRefCost(const Loop &L,
 }
 
 bool IndexedReference::tryDelinearizeFixedSize(
-    const SCEV *AccessFn, SmallVectorImpl<const SCEV *> &Subscripts) {
-  SmallVector<int, 4> ArraySizes;
-  if (!tryDelinearizeFixedSizeImpl(&SE, &StoreOrLoadInst, AccessFn, Subscripts,
-                                   ArraySizes))
+    const SCEV *AccessFn, SmallVectorImpl<const SCEV *> &Subscripts,
+    const SCEV *ElementSize) {
+  const SCEV *Offset = SE.removePointerBase(AccessFn);
+  if (!delinearizeFixedSizeArray(SE, Offset, Subscripts, Sizes, ElementSize)) {
+    Sizes.clear();
     return false;
+  }
 
-  // Populate Sizes with scev expressions to be used in calculations later.
-  for (auto Idx : seq<unsigned>(1, Subscripts.size()))
-    Sizes.push_back(
-        SE.getConstant(Subscripts[Idx]->getType(), ArraySizes[Idx - 1]));
-
-  LLVM_DEBUG({
-    dbgs() << "Delinearized subscripts of fixed-size array\n"
-           << "GEP:" << *getLoadStorePointerOperand(&StoreOrLoadInst)
-           << "\n";
-  });
+  // Drop the last element of Sizes which is the same as ElementSize.
+  assert(!Sizes.empty() && Sizes.back() == ElementSize &&
+         "Expecting the last one to be the element size");
+  Sizes.pop_back();
   return true;
 }
 
@@ -397,7 +393,7 @@ bool IndexedReference::delinearize(const LoopInfo &LI) {
 
     bool IsFixedSize = false;
     // Try to delinearize fixed-size arrays.
-    if (tryDelinearizeFixedSize(AccessFn, Subscripts)) {
+    if (tryDelinearizeFixedSize(AccessFn, Subscripts, ElemSize)) {
       IsFixedSize = true;
       // The last element of Sizes is the element size.
       Sizes.push_back(ElemSize);
diff --git a/llvm/test/Analysis/LoopCacheAnalysis/interchange-refcost-overflow.ll b/llvm/test/Analysis/LoopCacheAnalysis/interchange-refcost-overflow.ll
index 7b6529601da32..30554d08a6181 100644
--- a/llvm/test/Analysis/LoopCacheAnalysis/interchange-refcost-overflow.ll
+++ b/llvm/test/Analysis/LoopCacheAnalysis/interchange-refcost-overflow.ll
@@ -9,8 +9,8 @@
 ;       A[c][d][d] = 0;
 ; }
 
-; CHECK: Loop 'outer.loop' has cost = 9223372036854775807
 ; CHECK: Loop 'inner.loop' has cost = 9223372036854775807
+; CHECK: Loop 'outer.loop' has cost = 10000
 
 @A = local_unnamed_addr global [11 x [11 x [11 x i32]]] zeroinitializer, align 16
 

>From 4c2b0e286b244c2b4689cbf88140f84181b4b59e Mon Sep 17 00:00:00 2001
From: Ryotaro Kasuga <kasuga.ryotaro at fujitsu.com>
Date: Wed, 19 Nov 2025 11:23:48 +0000
Subject: [PATCH 2/4] update test

---
 .../interchange-refcost-overflow.ll           | 32 ++++++++++++-------
 1 file changed, 21 insertions(+), 11 deletions(-)

diff --git a/llvm/test/Analysis/LoopCacheAnalysis/interchange-refcost-overflow.ll b/llvm/test/Analysis/LoopCacheAnalysis/interchange-refcost-overflow.ll
index 30554d08a6181..52a530b2feebb 100644
--- a/llvm/test/Analysis/LoopCacheAnalysis/interchange-refcost-overflow.ll
+++ b/llvm/test/Analysis/LoopCacheAnalysis/interchange-refcost-overflow.ll
@@ -3,35 +3,45 @@
 ; For a loop with a very large iteration count, make sure the cost
 ; calculation does not overflow:
 ;
-; void a(int b) {
-;   for (int c;; c += b)
+; void a() {
+;   for (int c;; c += 2)
 ;     for (long d = 0; d < -3ULL; d += 2ULL)
-;       A[c][d][d] = 0;
+;       for (long e = 0; e < -3ULL; e += 2ULL)
+;         A[c][d][e] = 0;
 ; }
 
+; CHECK: Loop 'outer.loop' has cost = 9223372036854775807
+; CHECK: Loop 'middle.loop' has cost = 9223372036854775807
 ; CHECK: Loop 'inner.loop' has cost = 9223372036854775807
-; CHECK: Loop 'outer.loop' has cost = 10000
 
 @A = local_unnamed_addr global [11 x [11 x [11 x i32]]] zeroinitializer, align 16
 
-define void @foo(i32 noundef %b) {
+define void @foo() {
 entry:
-  %0 = sext i32 %b to i64
   br label %outer.loop
 
 outer.loop:
   %indvars.iv = phi i64 [ %indvars.iv.next, %outer.loop.cleanup ], [ 0, %entry ]
-  br label %inner.loop
+  br label %middle.loop
 
 outer.loop.cleanup:
-  %indvars.iv.next = add nsw i64 %indvars.iv, %0
+  %indvars.iv.next = add i64 %indvars.iv, 2
   br label %outer.loop
 
+middle.loop:
+  %middle.iv = phi i64 [ %middle.iv.next, %middle.loop.cleanup ], [ 0, %outer.loop ]
+  br label %inner.loop
+
+middle.loop.cleanup:
+  %middle.iv.next = add nuw i64 %middle.iv, 2
+  %ec.middle = icmp ult i64 %middle.iv, -5
+  br i1 %ec.middle, label %middle.loop, label %outer.loop.cleanup
+
 inner.loop:
-  %inner.iv = phi i64 [ 0, %outer.loop ], [ %add, %inner.loop ]
-  %arrayidx3 = getelementptr inbounds [11 x [11 x [11 x i32]]], ptr @A, i64 0, i64 %indvars.iv, i64 %inner.iv, i64 %inner.iv
+  %inner.iv = phi i64 [ 0, %middle.loop ], [ %add, %inner.loop ]
+  %arrayidx3 = getelementptr inbounds [11 x [11 x [11 x i32]]], ptr @A, i64 0, i64 %indvars.iv, i64 %middle.iv, i64 %inner.iv
   store i32 0, ptr %arrayidx3, align 4
   %add = add nuw i64 %inner.iv, 2
   %cmp = icmp ult i64 %inner.iv, -5
-  br i1 %cmp, label %inner.loop, label %outer.loop.cleanup
+  br i1 %cmp, label %inner.loop, label %middle.loop.cleanup
 }

>From 1966fe0c61cf27fb58b010b615c785698955530e Mon Sep 17 00:00:00 2001
From: Ryotaro Kasuga <kasuga.ryotaro at fujitsu.com>
Date: Wed, 19 Nov 2025 11:47:52 +0000
Subject: [PATCH 3/4] fix test

---
 llvm/test/Transforms/LoopInterchange/pr43326.ll | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/test/Transforms/LoopInterchange/pr43326.ll b/llvm/test/Transforms/LoopInterchange/pr43326.ll
index cc4f07c722dd9..6b83390f87d72 100644
--- a/llvm/test/Transforms/LoopInterchange/pr43326.ll
+++ b/llvm/test/Transforms/LoopInterchange/pr43326.ll
@@ -1,5 +1,5 @@
 ; RUN: opt < %s -passes=loop-interchange -cache-line-size=64 -pass-remarks-missed='loop-interchange' -pass-remarks-output=%t -S \
-; RUN:     -verify-dom-info -verify-loop-info -verify-loop-lcssa -stats 2>&1
+; RUN:     -verify-dom-info -verify-loop-info -verify-loop-lcssa -loop-interchange-profitabilities=ignore -stats 2>&1
 ; RUN: FileCheck --input-file=%t --check-prefix=REMARKS %s
 
 @a = global i32 0

>From 41a152385ece8c0ac623da9c328ffed0e1c9dab6 Mon Sep 17 00:00:00 2001
From: Ryotaro Kasuga <kasuga.ryotaro at fujitsu.com>
Date: Wed, 19 Nov 2025 12:27:54 +0000
Subject: [PATCH 4/4] adjust comment

---
 llvm/lib/Analysis/LoopCacheAnalysis.cpp | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/LoopCacheAnalysis.cpp b/llvm/lib/Analysis/LoopCacheAnalysis.cpp
index c599d2304e1a0..47b0d7c713cb9 100644
--- a/llvm/lib/Analysis/LoopCacheAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopCacheAnalysis.cpp
@@ -363,9 +363,13 @@ bool IndexedReference::tryDelinearizeFixedSize(
     return false;
   }
 
-  // Drop the last element of Sizes which is the same as ElementSize.
-  assert(!Sizes.empty() && Sizes.back() == ElementSize &&
-         "Expecting the last one to be the element size");
+  // We expect Sizes to have one more element than Subscripts, with the last one
+  // is ElementSize. The last element of Sizes is for ensuring consistency with
+  // the load/store instruction being analyzed. It is not needed for further
+  // analysis.
+  // TODO: Maybe this property should be enforced in delinearizeFixedSizeArray.
+  assert(!Sizes.empty() && Subscripts.size() + 1 == Sizes.size() &&
+         Sizes.back() == ElementSize && "Unexpected delinearization result");
   Sizes.pop_back();
   return true;
 }



More information about the llvm-commits mailing list