[llvm] [LAA] refactor sortPtrAccesses (NFC) (PR #92256)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Thu May 16 04:04:06 PDT 2024


https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/92256

>From 8ff93bd680d99b8c1a8d528c079bb50f0acfaab0 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <r at artagnon.com>
Date: Tue, 14 May 2024 17:37:12 +0100
Subject: [PATCH 1/2] [LAA] refactor sortPtrAccesses (NFC)

Use the destructuring syntax in C++ to make sortPtrAccesses a little
more readable.
---
 llvm/lib/Analysis/LoopAccessAnalysis.cpp | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index d071e53324408..8ab0fcdec8521 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -1646,22 +1646,19 @@ bool llvm::sortPtrAccesses(ArrayRef<Value *> VL, Type *ElemTy,
 
     // Check if the pointer with the same offset is found.
     int64_t Offset = *Diff;
-    auto Res = Offsets.emplace(Offset, Cnt);
-    if (!Res.second)
+    auto [It, IsInserted] = Offsets.emplace(Offset, Cnt++);
+    if (!IsInserted)
       return false;
     // Consecutive order if the inserted element is the last one.
-    IsConsecutive = IsConsecutive && std::next(Res.first) == Offsets.end();
-    ++Cnt;
+    IsConsecutive = IsConsecutive && std::next(It) == Offsets.end();
   }
   SortedIndices.clear();
+  Cnt = 0;
   if (!IsConsecutive) {
     // Fill SortedIndices array only if it is non-consecutive.
     SortedIndices.resize(VL.size());
-    Cnt = 0;
-    for (const std::pair<int64_t, int> &Pair : Offsets) {
-      SortedIndices[Cnt] = Pair.second;
-      ++Cnt;
-    }
+    for (const auto &[_, Off] : Offsets)
+      SortedIndices[Cnt++] = Off;
   }
   return true;
 }
@@ -2656,7 +2653,7 @@ void LoopAccessInfo::analyzeLoop(AAResults *AA, LoopInfo *LI,
                                SymbolicStrides, UncomputablePtr, false);
   if (!CanDoRTIfNeeded) {
     auto *I = dyn_cast_or_null<Instruction>(UncomputablePtr);
-    recordAnalysis("CantIdentifyArrayBounds", I) 
+    recordAnalysis("CantIdentifyArrayBounds", I)
         << "cannot identify array bounds";
     LLVM_DEBUG(dbgs() << "LAA: We can't vectorize because we can't find "
                       << "the array bounds.\n");

>From 5c557ec582c879af59f587e6af932106ef9a34a4 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <r at artagnon.com>
Date: Thu, 16 May 2024 12:03:25 +0100
Subject: [PATCH 2/2] [LAA] use llvm::enumerate

---
 llvm/lib/Analysis/LoopAccessAnalysis.cpp | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Analysis/LoopAccessAnalysis.cpp b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
index 8ab0fcdec8521..fcf60b30c3a0c 100644
--- a/llvm/lib/Analysis/LoopAccessAnalysis.cpp
+++ b/llvm/lib/Analysis/LoopAccessAnalysis.cpp
@@ -1636,9 +1636,8 @@ bool llvm::sortPtrAccesses(ArrayRef<Value *> VL, Type *ElemTy,
   auto Compare = llvm::less_first();
   std::set<DistOrdPair, decltype(Compare)> Offsets(Compare);
   Offsets.emplace(0, 0);
-  int Cnt = 1;
   bool IsConsecutive = true;
-  for (auto *Ptr : VL.drop_front()) {
+  for (auto [Idx, Ptr] : drop_begin(enumerate(VL))) {
     std::optional<int> Diff = getPointersDiff(ElemTy, Ptr0, ElemTy, Ptr, DL, SE,
                                               /*StrictCheck=*/true);
     if (!Diff)
@@ -1646,19 +1645,18 @@ bool llvm::sortPtrAccesses(ArrayRef<Value *> VL, Type *ElemTy,
 
     // Check if the pointer with the same offset is found.
     int64_t Offset = *Diff;
-    auto [It, IsInserted] = Offsets.emplace(Offset, Cnt++);
+    auto [It, IsInserted] = Offsets.emplace(Offset, Idx);
     if (!IsInserted)
       return false;
     // Consecutive order if the inserted element is the last one.
     IsConsecutive = IsConsecutive && std::next(It) == Offsets.end();
   }
   SortedIndices.clear();
-  Cnt = 0;
   if (!IsConsecutive) {
     // Fill SortedIndices array only if it is non-consecutive.
     SortedIndices.resize(VL.size());
-    for (const auto &[_, Off] : Offsets)
-      SortedIndices[Cnt++] = Off;
+    for (auto [Idx, Off] : enumerate(Offsets))
+      SortedIndices[Idx] = Off.second;
   }
   return true;
 }



More information about the llvm-commits mailing list