[SLP][NFC] Fix getInsertExtractIndex for ExtractElement and use unsigned indices#208317
Merged
Conversation
Created using spr 1.3.7
alexey-bataev
deleted the
users/alexey-bataev/spr/slpnfc-fix-getinsertextractindex-for-extractelement-and-use-unsigned-indices
branch
July 8, 2026 20:31
|
@llvm/pr-subscribers-vectorizers @llvm/pr-subscribers-llvm-transforms Author: Alexey Bataev (alexey-bataev) ChangesFull diff: https://github.com/llvm/llvm-project/pull/208317.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index ee909f3fd1b43..30fa465e5178a 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -716,21 +716,25 @@ static std::optional<unsigned> getInsertExtractIndex(const Value *Inst,
static_assert(std::is_same_v<T, InsertElementInst> ||
std::is_same_v<T, ExtractElementInst>,
"unsupported T");
- int Index = Offset;
- if (const auto *IE = dyn_cast<T>(Inst)) {
- const auto *VT = dyn_cast<FixedVectorType>(IE->getType());
- if (!VT)
- return std::nullopt;
- const auto *CI = dyn_cast<ConstantInt>(IE->getOperand(2));
- if (!CI)
- return std::nullopt;
- if (CI->getValue().uge(VT->getNumElements()))
- return std::nullopt;
- Index *= VT->getNumElements();
- Index += CI->getZExtValue();
- return Index;
- }
- return std::nullopt;
+ const auto *IE = dyn_cast<T>(Inst);
+ if (!IE)
+ return std::nullopt;
+ // InsertElement: result is the vector, index is op 2.
+ // ExtractElement: result is scalar, vector is op 0, index is op 1.
+ constexpr bool IsInsert = std::is_same_v<T, InsertElementInst>;
+ Type *VecTy = IsInsert ? IE->getType() : IE->getOperand(0)->getType();
+ const auto *VT = dyn_cast<FixedVectorType>(VecTy);
+ if (!VT)
+ return std::nullopt;
+ const auto *CI = dyn_cast<ConstantInt>(IE->getOperand(IsInsert ? 2 : 1));
+ if (!CI)
+ return std::nullopt;
+ if (CI->getValue().uge(VT->getNumElements()))
+ return std::nullopt;
+ unsigned Index = Offset;
+ Index *= VT->getNumElements();
+ Index += CI->getZExtValue();
+ return Index;
}
/// \returns inserting or extracting index of InsertElement, ExtractElement or
@@ -743,7 +747,7 @@ static std::optional<unsigned> getElementIndex(const Value *Inst,
if (auto Index = getInsertExtractIndex<ExtractElementInst>(Inst, Offset))
return Index;
- int Index = Offset;
+ unsigned Index = Offset;
const auto *IV = dyn_cast<InsertValueInst>(Inst);
if (!IV)
|
llvm-upstreamsync Bot
pushed a commit
to qualcomm/cpullvm-toolchain
that referenced
this pull request
Jul 8, 2026
…d use unsigned indices Reviewers: Pull Request: llvm/llvm-project#208317
llvm-sync Bot
pushed a commit
to arm/arm-toolchain
that referenced
this pull request
Jul 8, 2026
…d use unsigned indices Reviewers: Pull Request: llvm/llvm-project#208317
llvm-upstream-sync Bot
pushed a commit
to sriyalamar/cpullvm-toolchain
that referenced
this pull request
Jul 8, 2026
…d use unsigned indices Reviewers: Pull Request: llvm/llvm-project#208317
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.