git: 71ed2382e0ad - releng/15.2 - Merge commit 72a20b8e2987 from llvm git (by Gabriel Baraldi):

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Mon, 16 Nov 2026 03:17:47 UTC
The branch releng/15.2 has been updated by cperciva:

URL: https://cgit.FreeBSD.org/src/commit/?id=71ed2382e0adf46adde686ad259144f7ef4e1ed4

commit 71ed2382e0adf46adde686ad259144f7ef4e1ed4
Author:     Piotr Kubaj <pkubaj@FreeBSD.org>
AuthorDate: 2026-11-09 18:58:07 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2026-11-16 03:17:36 +0000

    Merge commit 72a20b8e2987 from llvm git (by Gabriel Baraldi):
    
      [SLPVectorizer] Check std::optional coming out of getPointersDiff (#175784)
    
      Fixes https://github.com/llvm/llvm-project/issues/175768
      There are other unchecked uses std::optional in this pass but I couldn't
      figure out a test that triggers them
    
    This fixes a clang crash in the SLP vectorizer, hit when building
    audio/csound on powerpc64le. The hardened libc++ traps on the disengaged
    optional, so clang dies with exit code 133. The second hunk is adapted to
    llvm 21, where analyzeConstantStrideCandidate() is still isStridedLoad().
    
    Approved by:    re (cperciva)
    Reviewed by:    dim
    MFC after:      3 days
    
    Differential Revision:  https://reviews.freebsd.org/D61354
    
    (cherry picked from commit e56090afb68b6c036d6b54c7b66d762d98bd96d0)
    (cherry picked from commit a778669c1d339b1f94c752003fc688d9a34e404a)
---
 .../lib/Transforms/Vectorize/SLPVectorizer.cpp     | 32 +++++++++++++++++-----
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/contrib/llvm-project/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/contrib/llvm-project/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 2d50981690f4..90f9f44fc0ec 100644
--- a/contrib/llvm-project/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/contrib/llvm-project/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -6076,6 +6076,12 @@ static bool isStridedLoad(ArrayRef<Value *> VL, ArrayRef<Value *> PointerOps,
       Ptr0 = PointerOps[Order.front()];
       PtrN = PointerOps[Order.back()];
     }
+    // sortPtrAccesses only validates getPointersDiff for pairs relative to
+    // PointerOps[0], so compute every distance through PointerOps[0] (as in
+    // canVectorizeLoads); only differences are used below.
+    std::optional<int64_t> Diff0 =
+        getPointersDiff(ScalarTy, PointerOps[0], ScalarTy, Ptr0, DL, SE);
+    assert(Diff0 && "sortPtrAccesses should have validated this pointer");
     // Iterate through all pointers and check if all distances are
     // unique multiple of Dist.
     SmallSet<int64_t, 4> Dists;
@@ -6083,8 +6089,12 @@ static bool isStridedLoad(ArrayRef<Value *> VL, ArrayRef<Value *> PointerOps,
       int64_t Dist = 0;
       if (Ptr == PtrN)
         Dist = Diff;
-      else if (Ptr != Ptr0)
-        Dist = *getPointersDiff(ScalarTy, Ptr0, ScalarTy, Ptr, DL, SE);
+      else if (Ptr != Ptr0) {
+        std::optional<int64_t> DistN =
+            getPointersDiff(ScalarTy, PointerOps[0], ScalarTy, Ptr, DL, SE);
+        assert(DistN && "sortPtrAccesses should have validated this pointer");
+        Dist = *DistN - *Diff0;
+      }
       // If the strides are not the same or repeated, we can't
       // vectorize.
       if (((Dist / Stride) * Stride) != Dist || !Dists.insert(Dist).second)
@@ -6162,10 +6172,18 @@ BoUpSLP::canVectorizeLoads(ArrayRef<Value *> VL, const Value *VL0,
       Ptr0 = PointerOps[Order.front()];
       PtrN = PointerOps[Order.back()];
     }
-    std::optional<int64_t> Diff =
-        getPointersDiff(ScalarTy, Ptr0, ScalarTy, PtrN, *DL, *SE);
+    // sortPtrAccesses validates getPointersDiff for all pointers relative to
+    // PointerOps[0], so compute the span using PointerOps[0] as intermediate:
+    //   Diff = offset(PtrN) - offset(Ptr0) relative to PointerOps[0]
+    std::optional<int64_t> Diff0 =
+        getPointersDiff(ScalarTy, PointerOps[0], ScalarTy, Ptr0, *DL, *SE);
+    std::optional<int64_t> DiffN =
+        getPointersDiff(ScalarTy, PointerOps[0], ScalarTy, PtrN, *DL, *SE);
+    assert(Diff0 && DiffN &&
+           "sortPtrAccesses should have validated these pointers");
+    int64_t Diff = *DiffN - *Diff0;
     // Check that the sorted loads are consecutive.
-    if (static_cast<uint64_t>(*Diff) == Sz - 1)
+    if (static_cast<uint64_t>(Diff) == Sz - 1)
       return LoadsState::Vectorize;
     if (isMaskedLoadCompress(VL, PointerOps, Order, *TTI, *DL, *SE, *AC, *DT,
                              *TLI, [&](Value *V) {
@@ -6174,7 +6192,7 @@ BoUpSLP::canVectorizeLoads(ArrayRef<Value *> VL, const Value *VL0,
                              }))
       return LoadsState::CompressVectorize;
     // Simple check if not a strided access - clear order.
-    bool IsPossibleStrided = *Diff % (Sz - 1) == 0;
+    bool IsPossibleStrided = Diff % (Sz - 1) == 0;
     // Try to generate strided load node.
     auto IsAnyPointerUsedOutGraph =
         IsPossibleStrided && any_of(PointerOps, [&](Value *V) {
@@ -6184,7 +6202,7 @@ BoUpSLP::canVectorizeLoads(ArrayRef<Value *> VL, const Value *VL0,
         });
     if (IsPossibleStrided &&
         isStridedLoad(VL, PointerOps, Order, *TTI, *DL, *SE,
-                      IsAnyPointerUsedOutGraph, *Diff))
+                      IsAnyPointerUsedOutGraph, Diff))
       return LoadsState::StridedVectorize;
   }
   if (!TTI->isLegalMaskedGather(VecTy, CommonAlignment) ||