[llvm] [SLP] Cluster SortedBases before sorting. (PR #101144)

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 30 10:21:45 PDT 2024


================
@@ -4843,25 +4843,40 @@ static bool clusterSortPtrAccesses(ArrayRef<Value *> VL, Type *ElemTy,
     return false;
 
   // If we have a better order, also sort the base pointers by increasing
-  // (variable) values if possible, to try and keep the order more regular.
-  SmallVector<std::pair<Value *, Value *>> SortedBases;
-  for (auto &Base : Bases)
-    SortedBases.emplace_back(Base.first,
-                             Base.first->stripInBoundsConstantOffsets());
-  llvm::stable_sort(SortedBases, [](std::pair<Value *, Value *> V1,
-                                    std::pair<Value *, Value *> V2) {
-    const Value *V = V2.second;
-    while (auto *Gep = dyn_cast<GetElementPtrInst>(V)) {
-      if (Gep->getOperand(0) == V1.second)
-        return true;
-      V = Gep->getOperand(0);
+  // (variable) values if possible, to try and keep the order more regular. In
+  // order to create a valid strict-weak order we cluster by the Root of gep
+  // chains and sort within each.
+  SmallVector<std::tuple<Value *, Value *, Value *>> SortedBases;
+  for (auto &Base : Bases) {
+    Value *Strip = Base.first->stripInBoundsConstantOffsets();
+    Value *Root = Strip;
+    while (auto *Gep = dyn_cast<GetElementPtrInst>(Root))
+      Root = Gep->getOperand(0);
+    SortedBases.emplace_back(Base.first, Strip, Root);
+  }
+  if (SortedBases.size() <= 16) {
+    auto Begin = SortedBases.begin();
+    auto End = SortedBases.end();
+    while (Begin != End) {
+      Value *Root = std::get<2>(*Begin);
+      auto Mid = std::stable_partition(
+          Begin, End, [&Root](auto V) { return std::get<2>(V) == Root; });
+      std::stable_sort(Begin, Mid, [](auto V1, auto V2) {
+        const Value *V = std::get<1>(V2);
+        while (auto *Gep = dyn_cast<GetElementPtrInst>(V)) {
+          if (Gep->getOperand(0) == std::get<1>(V1))
+            return true;
+          V = Gep->getOperand(0);
+        }
----------------
alexey-bataev wrote:

I mean, instead of a loop in a sort function, do some kind of analysis before or use standard functions, like getUnderlying....

https://github.com/llvm/llvm-project/pull/101144


More information about the llvm-commits mailing list