[llvm] 31a552d - [LoopCacheAnalysis] Replace delinearization for fixed size array (#164798)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 21 06:06:50 PST 2025
Author: Ryotaro Kasuga
Date: 2025-11-21T14:06:45Z
New Revision: 31a552def53964025ccc507c6c90002a5d8d7e25
URL: https://github.com/llvm/llvm-project/commit/31a552def53964025ccc507c6c90002a5d8d7e25
DIFF: https://github.com/llvm/llvm-project/commit/31a552def53964025ccc507c6c90002a5d8d7e25.diff
LOG: [LoopCacheAnalysis] Replace delinearization for fixed size array (#164798)
This patch replaces the delinearization function used in
LoopCacheAnalysis, switching from one that depends on type information
in GEPs to one that does not. Once this patch and
https://github.com/llvm/llvm-project/pull/161822 are landed, we can
delete `tryDelinearizeFixedSize` from Delienarization, which is an
optimization heuristic guided by GEP type information. After Polly
eliminates its use of `getIndexExpressionsFromGEP`, we will be able to
completely delete GEP-driven heuristics from Delinearization.
Added:
Modified:
llvm/include/llvm/Analysis/LoopCacheAnalysis.h
llvm/lib/Analysis/LoopCacheAnalysis.cpp
llvm/test/Analysis/LoopCacheAnalysis/interchange-refcost-overflow.ll
llvm/test/Transforms/LoopInterchange/pr43326.ll
Removed:
################################################################################
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 424a7fe3721bb..e0e2be8e35929 100644
--- a/llvm/lib/Analysis/LoopCacheAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopCacheAnalysis.cpp
@@ -355,22 +355,22 @@ 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";
- });
+ // We expect Sizes and Subscipts have the same number of elements, and the
+ // last element of Sizes is ElementSize. It 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() == Sizes.size() &&
+ Sizes.back() == ElementSize && "Unexpected delinearization result");
+ Sizes.pop_back();
return true;
}
@@ -397,7 +397,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..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
@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
}
diff --git a/llvm/test/Transforms/LoopInterchange/pr43326.ll b/llvm/test/Transforms/LoopInterchange/pr43326.ll
index 666f11d4969a0..c224bd3cd93ab 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
More information about the llvm-commits
mailing list