-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[SLP]Remove emission of vector_insert/vector_extract intrinsics #148007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexey-bataev
merged 4 commits into
main
from
users/alexey-bataev/spr/slpremove-emission-of-vector_insertvector_extract-instrinsics
Jul 11, 2025
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5815,26 +5815,30 @@ static InstructionCost getExtractWithExtendCost( | |
| static Value *createInsertVector( | ||
| IRBuilderBase &Builder, Value *Vec, Value *V, unsigned Index, | ||
| function_ref<Value *(Value *, Value *, ArrayRef<int>)> Generator = {}) { | ||
| if (isa<PoisonValue>(Vec) && isa<PoisonValue>(V)) | ||
| return Vec; | ||
| const unsigned SubVecVF = getNumElements(V->getType()); | ||
| if (Index % SubVecVF == 0) { | ||
| Vec = Builder.CreateInsertVector(Vec->getType(), Vec, V, Index); | ||
| // Create shuffle, insertvector requires that index is multiple of | ||
| // the subvector length. | ||
| const unsigned VecVF = getNumElements(Vec->getType()); | ||
| SmallVector<int> Mask(VecVF, PoisonMaskElem); | ||
| if (isa<PoisonValue>(Vec)) { | ||
| auto *Begin = std::next(Mask.begin(), Index); | ||
| std::iota(Begin, std::next(Begin, getNumElements(V->getType())), 0); | ||
| Vec = Builder.CreateShuffleVector(V, Mask); | ||
| return Vec; | ||
| } | ||
| std::iota(Mask.begin(), Mask.end(), 0); | ||
| std::iota(std::next(Mask.begin(), Index), | ||
| std::next(Mask.begin(), Index + SubVecVF), VecVF); | ||
| if (Generator) { | ||
| Vec = Generator(Vec, V, Mask); | ||
alexey-bataev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| // Create shuffle, insertvector requires that index is multiple of | ||
| // the subvector length. | ||
| const unsigned VecVF = getNumElements(Vec->getType()); | ||
| SmallVector<int> Mask(VecVF, PoisonMaskElem); | ||
| std::iota(Mask.begin(), Mask.end(), 0); | ||
| for (unsigned I : seq<unsigned>(SubVecVF)) | ||
| Mask[I + Index] = I + VecVF; | ||
| if (Generator) { | ||
| Vec = Generator(Vec, V, Mask); | ||
| } else { | ||
| // 1. Resize V to the size of Vec. | ||
| SmallVector<int> ResizeMask(VecVF, PoisonMaskElem); | ||
| std::iota(ResizeMask.begin(), std::next(ResizeMask.begin(), SubVecVF), 0); | ||
| V = Builder.CreateShuffleVector(V, ResizeMask); | ||
| Vec = Builder.CreateShuffleVector(Vec, V, Mask); | ||
| } | ||
| // 1. Resize V to the size of Vec. | ||
| SmallVector<int> ResizeMask(VecVF, PoisonMaskElem); | ||
| std::iota(ResizeMask.begin(), std::next(ResizeMask.begin(), SubVecVF), 0); | ||
| V = Builder.CreateShuffleVector(V, ResizeMask); | ||
| Vec = Builder.CreateShuffleVector(Vec, V, Mask); | ||
| } | ||
| return Vec; | ||
| } | ||
|
|
@@ -5844,11 +5848,6 @@ static Value *createInsertVector( | |
| /// using default shuffle. | ||
| static Value *createExtractVector(IRBuilderBase &Builder, Value *Vec, | ||
| unsigned SubVecVF, unsigned Index) { | ||
| if (Index % SubVecVF == 0) { | ||
| VectorType *SubVecTy = | ||
| getWidenedType(Vec->getType()->getScalarType(), SubVecVF); | ||
| return Builder.CreateExtractVector(SubVecTy, Vec, Index); | ||
| } | ||
| // Create shuffle, extract_subvector requires that index is multiple of | ||
| // the subvector length. | ||
| SmallVector<int> Mask(SubVecVF, PoisonMaskElem); | ||
|
|
@@ -16275,8 +16274,8 @@ Value *BoUpSLP::gather( | |
| assert(SLPReVec && "FixedVectorType is not expected."); | ||
| Vec = | ||
| createInsertVector(Builder, Vec, Scalar, Pos * getNumElements(VecTy)); | ||
| auto *II = dyn_cast<IntrinsicInst>(Vec); | ||
| if (!II || II->getIntrinsicID() != Intrinsic::vector_insert) | ||
| auto *II = dyn_cast<Instruction>(Vec); | ||
| if (!II) | ||
| return Vec; | ||
| InsElt = II; | ||
| } else { | ||
|
|
@@ -16296,6 +16295,27 @@ Value *BoUpSLP::gather( | |
| if (auto *SI = dyn_cast<Instruction>(Scalar)) | ||
| UserOp = SI; | ||
| } else { | ||
| if (V->getType()->isVectorTy()) { | ||
| if (auto *SV = dyn_cast<ShuffleVectorInst>(InsElt); | ||
| SV && SV->getOperand(0) != V && SV->getOperand(1) != V) { | ||
| // Find shufflevector, caused by resize. | ||
| auto FindOperand = [&](Value *Vec, Value *V) -> Instruction * { | ||
|
||
| if (auto *SV = dyn_cast<ShuffleVectorInst>(Vec)) { | ||
| if (SV->getOperand(0) == V) | ||
| return SV; | ||
| if (SV->getOperand(1) == V) | ||
| return SV; | ||
| } | ||
| return nullptr; | ||
| }; | ||
| if (Instruction *User = FindOperand(SV->getOperand(0), V)) | ||
| InsElt = User; | ||
| else if (Instruction *User = FindOperand(SV->getOperand(1), V)) | ||
| InsElt = User; | ||
| assert(InsElt && | ||
alexey-bataev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "Failed to find shufflevector, caused by resize."); | ||
| } | ||
| } | ||
| UserOp = InsElt; | ||
| } | ||
| if (UserOp) { | ||
|
|
@@ -16864,10 +16884,13 @@ class BoUpSLP::ShuffleInstructionBuilder final : public BaseShuffleAnalysis { | |
| V, SimplifyQuery(*R.DL)); | ||
| })); | ||
| unsigned InsertionIndex = Idx * getNumElements(ScalarTy); | ||
| Type *OrigScalarTy = ScalarTy; | ||
alexey-bataev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ScalarTy = ScalarTy->getScalarType(); | ||
| Vec = createInsertVector( | ||
| Builder, Vec, V, InsertionIndex, | ||
| std::bind(&ShuffleInstructionBuilder::createShuffle, this, _1, _2, | ||
| _3)); | ||
| ScalarTy = OrigScalarTy; | ||
| if (!CommonMask.empty()) { | ||
| std::iota(std::next(CommonMask.begin(), Idx), | ||
| std::next(CommonMask.begin(), Idx + E->getVectorFactor()), | ||
|
|
||
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.