[llvm] [SLP] Fix crash on extractelement with out-of-bounds index.....Fixes … (PR #176918)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 20 05:16:36 PST 2026


https://github.com/Soumik15630 updated https://github.com/llvm/llvm-project/pull/176918

>From 46ecbe0d4c347001df3e0251b8d3a37a3f12e89e Mon Sep 17 00:00:00 2001
From: Soumik15630m <soumik15630m at gmail.com>
Date: Tue, 20 Jan 2026 18:15:37 +0530
Subject: [PATCH 1/2] [SLP] Fix crash on extractelement with out-of-bounds
 index.....Fixes #176780. The cose modeling logic was attempting to set a bit
 in APInt for an out-of-bounds index, causing an assertion failure. This patch
 ignores OOB indices as they produce poison- which is already handled.

---
 llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 762b394f8ea8a..8125eb27a2e98 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -15042,7 +15042,14 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
       }
       if (DemandedElts.isZero())
         DemandedElts = APInt::getZero(getNumElements(SrcVecTy));
-      DemandedElts.setBit(*getExtractIndex(I));
+      unsigned ExtIdx = *getExtractIndex(I);
+      //Out-of-bounds extractelement produces poison.So it do no corresponds to a concrete vector lane,
+      // so instead of treating it as an invalid pattern we can avoid marking any demanded element
+      if (ExtIdx  < DemandedElts.getBitWidth())
+      {
+        DemandedElts.setBit(ExtIdx);
+      }
+      // DemandedElts.setBit(*getExtractIndex(I));
       return InstructionCost(TTI::TCC_Free);
     };
     auto GetVectorCost = [&, &TTI = *TTI](InstructionCost CommonCost) {

>From e95278fdcfa6991e1c1e7bbea085924e63eeac6a Mon Sep 17 00:00:00 2001
From: Soumik15630m <soumik15630m at gmail.com>
Date: Tue, 20 Jan 2026 18:29:49 +0530
Subject: [PATCH 2/2] [SLP] Fix crash on extractelement with out-of-bounds
 index....Added the regression test for out-of-bound-extract

---
 .../X86/crash-on-out-of-bound-extract.ll      | 25 +++++++++++++++++++
 1 file changed, 25 insertions(+)
 create mode 100644 llvm/test/Transforms/SLPVectorizer/X86/crash-on-out-of-bound-extract.ll

diff --git a/llvm/test/Transforms/SLPVectorizer/X86/crash-on-out-of-bound-extract.ll b/llvm/test/Transforms/SLPVectorizer/X86/crash-on-out-of-bound-extract.ll
new file mode 100644
index 0000000000000..d971f537f6330
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/X86/crash-on-out-of-bound-extract.ll
@@ -0,0 +1,25 @@
+; RUN: opt -passes=slp-vectorizer -S < %s | FileCheck %s
+
+define <4 x i32> @test(<4 x i32> %A){
+; CHECK-LABEL: @test(
+; CHECK-NEXT: entry:
+; CHECK-NEXT:   [[TMP0:%.*]] = shufflevector <4 x i32> [[A:%.*]], <4 x i32> poison, <4 x i32> <i32 0, i32 1, i32 3, i32 1>
+; CHECK-NEXT:   [[TMP1:%.*]] = sdiv <4 x i32> [[A]], [[TMP0]]
+; CHECK-NEXT:   ret <4 x i32> [[TMP1]]
+;
+entry:
+    %e0 = extractelement <4 x i32> %A, i64 0
+    %e1 = extractelement <4 x i32> %A, i64 1
+    %e2 = extractelement <4 x i32> %A, i64 2
+    %e3 = extractelement <4 x i32> %A, i64 3
+    %oob = extractelement <4 x i32> %A, i64 4
+    %d0 = sdiv i32 %e0, %e0
+    %d1 = sdiv i32 %oob, %e1
+    %d2 = sdiv i32 %e2, %e3
+    %d3 = sdiv i32 %oob, %e1
+    %v0 = insertelement <4 x i32> poison, i32 %d0, i64 0
+    %v1 = insertelement <4 x i32> %v0, i32 %d1, i64 1
+    %v2 = insertelement <4 x i32> %v1, i32 %d1, i64 2
+    %v3 = insertelement <4 x i32> %v2, i32 %d1, i64 3
+    ret <4 x i32> %v3
+}
\ No newline at end of file



More information about the llvm-commits mailing list