-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
[SLP] Compute a shuffle mask for SK_Broadcast shuffle #85327
Conversation
This is the first of a couple of small patches to compute shuffle masks for the couple of cases where we call getShuffleCost without one. My goal is to add an invariant that all calls to getShuffleCost for fixed length vectors have a mask.
@llvm/pr-subscribers-llvm-transforms Author: Philip Reames (preames) ChangesThis is the first of a couple of small patches to compute shuffle masks for the couple of cases where we call getShuffleCost without one. My goal is to add an invariant that all calls to getShuffleCost for fixed length vectors have a mask. Full diff: https://github.com/llvm/llvm-project/pull/85327.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index b4cce680e2876f..885015df1f3b81 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -7744,16 +7744,22 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
bool NeedShuffle =
count(VL, *It) > 1 &&
(VL.front() != *It || !all_of(VL.drop_front(), UndefValue::classof));
+ if (!NeedShuffle)
+ return TTI.getVectorInstrCost(Instruction::InsertElement, VecTy,
+ CostKind, std::distance(VL.begin(), It),
+ PoisonValue::get(VecTy), *It);
+
+ SmallVector<int> ShuffleMask(VL.size(), -1);
+ transform(VL, ShuffleMask.begin(), [](Value *V) {
+ return !isa<UndefValue>(V);
+ });
InstructionCost InsertCost = TTI.getVectorInstrCost(
- Instruction::InsertElement, VecTy, CostKind,
- NeedShuffle ? 0 : std::distance(VL.begin(), It),
+ Instruction::InsertElement, VecTy, CostKind, 0,
PoisonValue::get(VecTy), *It);
return InsertCost +
- (NeedShuffle ? TTI.getShuffleCost(
- TargetTransformInfo::SK_Broadcast, VecTy,
- /*Mask=*/std::nullopt, CostKind, /*Index=*/0,
- /*SubTp=*/nullptr, /*Args=*/*It)
- : TTI::TCC_Free);
+ TTI.getShuffleCost(TargetTransformInfo::SK_Broadcast, VecTy,
+ ShuffleMask, CostKind, /*Index=*/0,
+ /*SubTp=*/nullptr, /*Args=*/*It);
}
return GatherCost +
(all_of(Gathers, UndefValue::classof)
|
You can test this locally with the following command:git-clang-format --diff 33960c90258ed78b9b877b1a43e219d1cbc2efce c0cd3151de14e736af1048c298d6968514c7a6e0 -- llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp View the diff from clang-format here.diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 0abd56b32c..3b311eeb50 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -7751,15 +7751,15 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis {
SmallVector<int> ShuffleMask(VL.size(), PoisonMaskElem);
transform(VL, ShuffleMask.begin(), [](Value *V) {
- return isa<PoisonValue>(V) ? PoisonMaskElem : 0;
+ return isa<PoisonValue>(V) ? PoisonMaskElem : 0;
});
- InstructionCost InsertCost = TTI.getVectorInstrCost(
- Instruction::InsertElement, VecTy, CostKind, 0,
- PoisonValue::get(VecTy), *It);
- return InsertCost +
- TTI.getShuffleCost(TargetTransformInfo::SK_Broadcast, VecTy,
- ShuffleMask, CostKind, /*Index=*/0,
- /*SubTp=*/nullptr, /*Args=*/*It);
+ InstructionCost InsertCost =
+ TTI.getVectorInstrCost(Instruction::InsertElement, VecTy, CostKind, 0,
+ PoisonValue::get(VecTy), *It);
+ return InsertCost + TTI.getShuffleCost(TargetTransformInfo::SK_Broadcast,
+ VecTy, ShuffleMask, CostKind,
+ /*Index=*/0,
+ /*SubTp=*/nullptr, /*Args=*/*It);
}
return GatherCost +
(all_of(Gathers, UndefValue::classof)
|
Co-authored-by: Alexey Bataev <a.bataev@gmx.com>
Co-authored-by: Alexey Bataev <a.bataev@gmx.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LG
This is the first of a couple of small patches to compute shuffle masks for the couple of cases where we call getShuffleCost without one. My goal is to add an invariant that all calls to getShuffleCost for fixed length vectors have a mask.