[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 09:33:53 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/3] [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/3] [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
>From b1e68321008fd5027526f77793f683e049417dee Mon Sep 17 00:00:00 2001
From: Soumik15630m <soumik15630m at gmail.com>
Date: Tue, 20 Jan 2026 22:28:28 +0530
Subject: [PATCH 3/3] [SLP] Handle out-of-bounds extractelement indices in
getExtractIndex directly - a global fix
---
.../Transforms/Vectorize/SLPVectorizer.cpp | 84 ++++++++++++++-----
.../X86/crash-on-out-of-bound-extract.ll | 13 +--
2 files changed, 66 insertions(+), 31 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 8125eb27a2e98..1c50dbd9b1902 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -902,7 +902,16 @@ static std::optional<unsigned> getExtractIndex(const Instruction *E) {
auto *CI = dyn_cast<ConstantInt>(E->getOperand(1));
if (!CI)
return std::nullopt;
- return CI->getZExtValue();
+ // Check if the index is out of bound - we can get the source vector from operand 0
+ unsigned Idx = CI->getZExtValue();
+ if (auto *VecTy = dyn_cast<FixedVectorType>(E->getOperand(0)->getType()))
+ {
+ if (Idx >= VecTy->getNumElements())
+ {
+ return std::nullopt;
+ }
+ }
+ return Idx;
}
auto *EI = cast<ExtractValueInst>(E);
if (EI->getNumIndices() != 1)
@@ -7990,7 +7999,17 @@ BoUpSLP::getReorderingData(const TreeEntry &TE, bool TopToBottom,
if (Idx == PoisonMaskElem)
continue;
Value *V = TE.Scalars[ReorderMask[Idx]];
+ // UPDATION: to go with the new implementation of getExtractIndex()
+ if (isa<PoisonValue>(V))
+ {
+ continue; // safeguard to not cast them to instruction
+ }
+
std::optional<unsigned> EI = getExtractIndex(cast<Instruction>(V));
+ if (!EI)
+ {
+ continue; // checking to the optional has value or not
+ }
Idx = std::distance(ReorderMask.begin(), find(ReorderMask, *EI));
}
}
@@ -12483,10 +12502,22 @@ bool BoUpSLP::canReuseExtract(ArrayRef<Value *> VL,
if (isa<UndefValue>(EE->getIndexOperand()))
continue;
std::optional<unsigned> Idx = getExtractIndex(Inst);
+ // UPDATION: with new implementation of getExtractIndex
+
if (!Idx)
+ /* If getExtractIndex returns nullopt,it's either varaible or OOB
+ * if it is an ExtractElement with a constant index , it was OOB.- so 'undef' and we can continue
+ */
+ if (auto *EE = dyn_cast<ExtractElementInst>(Inst))
+ {
+ if (isa<ConstantInt>(EE->getIndexOperand()))
+ {
+ continue;
+ }
+ }
return false;
const unsigned ExtIdx = *Idx;
- if (ExtIdx >= NElts)
+ if (ExtIdx >= NElts) // redundant now( helper handles OOB) but nothing hurts to keep it - it's safe
continue;
Indices[I] = ExtIdx;
if (MinIdx > ExtIdx)
@@ -14374,7 +14405,8 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
for (auto [I, V] :
enumerate(ArrayRef(VL).slice(Part * SliceSize, Limit))) {
// Ignore non-extractelement scalars.
- if (isa<UndefValue>(V) ||
+ // Skip undefs and poison values explicitly
+ if (isa<UndefValue>(V) || isa<PoisonValue>(V) ||
(!SubMask.empty() && SubMask[I] == PoisonMaskElem))
continue;
// If all users of instruction are going to be vectorized and this
@@ -14383,7 +14415,7 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
// vectorized tree.
// Also, avoid adjusting the cost for extractelements with multiple uses
// in different graph entries.
- auto *EE = cast<ExtractElementInst>(V);
+ auto *EE = dyn_cast<ExtractElementInst>(V); // using dyncast instead of cast to rule out the crash if v is not an extract elementinst
VecBase = EE->getVectorOperand();
UniqueBases.insert(VecBase);
ArrayRef<TreeEntry *> VEs = R.getTreeEntries(V);
@@ -14407,9 +14439,10 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
}) ||
(!VEs.empty() && !is_contained(VEs, E)))
continue;
+ // handling the optional return of getExtractIndex
std::optional<unsigned> EEIdx = getExtractIndex(EE);
if (!EEIdx)
- continue;
+ continue; // Skip OOB indices (as free or poison)
unsigned Idx = *EEIdx;
// Take credit for instruction that will become dead.
if (EE->hasOneUse() || !PrevNodeFound) {
@@ -15009,6 +15042,7 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
return InstructionCost(TTI::TCC_Free);
auto *I = cast<Instruction>(UniqueValues[Idx]);
+ //Initialize SrcVecty
if (!SrcVecTy) {
if (ShuffleOrOp == Instruction::ExtractElement) {
auto *EE = cast<ExtractElementInst>(I);
@@ -15024,30 +15058,37 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
SrcVecTy = getWidenedType(OrigScalarTy, NumElts);
}
}
+ // UPDATION: to implement the change in getExtractIndex
+ // need index for both the optimization check and the bitmask
+ std::optional<unsigned> EI = getExtractIndex(I);
+
if (I->hasOneUse()) {
Instruction *Ext = I->user_back();
if ((isa<SExtInst>(Ext) || isa<ZExtInst>(Ext)) &&
all_of(Ext->users(), IsaPred<GetElementPtrInst>)) {
// Use getExtractWithExtendCost() to calculate the cost of
// extractelement/ext pair.
- InstructionCost Cost = TTI->getExtractWithExtendCost(
- Ext->getOpcode(), Ext->getType(), SrcVecTy, *getExtractIndex(I),
- CostKind);
- // Subtract the cost of s|zext which is subtracted separately.
- Cost -= TTI->getCastInstrCost(
- Ext->getOpcode(), Ext->getType(), I->getType(),
- TTI::getCastContextHint(Ext), CostKind, Ext);
- return Cost;
+ if (EI) // checks if Ei is valid not OOB
+ {
+ InstructionCost Cost = TTI->getExtractWithExtendCost(
+ Ext->getOpcode(), Ext->getType(), SrcVecTy, *getExtractIndex(I),
+ CostKind);
+ // Subtract the cost of s|zext which is subtracted separately.
+ Cost -= TTI->getCastInstrCost(
+ Ext->getOpcode(), Ext->getType(), I->getType(),
+ TTI::getCastContextHint(Ext), CostKind, Ext);
+ return Cost;
+ }
}
}
if (DemandedElts.isZero())
DemandedElts = APInt::getZero(getNumElements(SrcVecTy));
- 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())
+ //UPDATION: to implement the new implementation of getExtractIndex
+ if (EI)
{
- DemandedElts.setBit(ExtIdx);
+ DemandedElts.setBit(*EI);
}
// DemandedElts.setBit(*getExtractIndex(I));
return InstructionCost(TTI::TCC_Free);
@@ -17301,14 +17342,15 @@ BoUpSLP::tryToGatherSingleRegisterExtractElements(
continue;
std::optional<unsigned> Idx = getExtractIndex(EI);
// Undefined index.
- if (!Idx) {
- UndefVectorExtracts.push_back(I);
- continue;
- }
- if (Idx >= VecTy->getNumElements()) {
+ if (!Idx) { // it covers the new implement of getExtractIndex because it returns nullopt for OOB
UndefVectorExtracts.push_back(I);
continue;
}
+ // The following check is redundant we already handle this in getExtractIndex it self
+ // if (Idx >= VecTy->getNumElements()) {
+ // UndefVectorExtracts.push_back(I);
+ // continue;
+ // }
SmallBitVector ExtractMask(VecTy->getNumElements(), true);
ExtractMask.reset(*Idx);
if (isUndefVector(EI->getVectorOperand(), ExtractMask).all()) {
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
index d971f537f6330..7bf5e20bd60e1 100644
--- 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
@@ -1,12 +1,5 @@
-; RUN: opt -passes=slp-vectorizer -S < %s | FileCheck %s
-
+; RUN: opt -passes=slp-vectorizer -S < %s > /dev/null
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
@@ -19,7 +12,7 @@ entry:
%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
+ %v2 = insertelement <4 x i32> %v1, i32 %d2, i64 2
+ %v3 = insertelement <4 x i32> %v2, i32 %d3, i64 3
ret <4 x i32> %v3
}
\ No newline at end of file
More information about the llvm-commits
mailing list