-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[SLP]Initial support for non-power-of-2 (but still whole register) number of elements in operands. #107273
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]Initial support for non-power-of-2 (but still whole register) number of elements in operands. #107273
Changes from 5 commits
f927502
a79159b
0a78a26
b8b1527
7c68dbf
affdc53
13c2844
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -260,6 +260,20 @@ static FixedVectorType *getWidenedType(Type *ScalarTy, unsigned VF) { | |
| VF * getNumElements(ScalarTy)); | ||
| } | ||
|
|
||
| /// Returns the number of elements of the given type \p Ty, not less than \p Sz, | ||
| /// which forms type, which splits by \p TTI into whole vector types during | ||
| /// legalization. | ||
| static unsigned getFullVectorNumberOfElements(const TargetTransformInfo &TTI, | ||
| Type *Ty, unsigned Sz) { | ||
| if (!isValidElementType(Ty)) | ||
| return bit_ceil(Sz); | ||
| // Find the number of elements, which forms full vectors. | ||
| const unsigned NumParts = TTI.getNumberOfParts(getWidenedType(Ty, Sz)); | ||
| if (NumParts == 0 || NumParts >= Sz) | ||
| return bit_ceil(Sz); | ||
| return bit_ceil(divideCeil(Sz, NumParts)) * NumParts; | ||
| } | ||
|
|
||
| static void transformScalarShuffleIndiciesToVector(unsigned VecTyNumElements, | ||
| SmallVectorImpl<int> &Mask) { | ||
| // The ShuffleBuilder implementation use shufflevector to splat an "element". | ||
|
|
@@ -394,7 +408,7 @@ static bool isVectorLikeInstWithConstOps(Value *V) { | |
| /// total number of elements \p Size and number of registers (parts) \p | ||
| /// NumParts. | ||
| static unsigned getPartNumElems(unsigned Size, unsigned NumParts) { | ||
| return PowerOf2Ceil(divideCeil(Size, NumParts)); | ||
| return std::min<unsigned>(Size, bit_ceil(divideCeil(Size, NumParts))); | ||
| } | ||
|
|
||
| /// Returns correct remaining number of elements, considering total amount \p | ||
|
|
@@ -1223,6 +1237,22 @@ static bool doesNotNeedToSchedule(ArrayRef<Value *> VL) { | |
| (all_of(VL, isUsedOutsideBlock) || all_of(VL, areAllOperandsNonInsts)); | ||
| } | ||
|
|
||
| /// Returns true if widened type of \p Ty elements with size \p Sz represents | ||
| /// full vector type, i.e. adding extra element results in extra parts upon type | ||
| /// legalization. | ||
| static bool hasFullVectorsOnly(const TargetTransformInfo &TTI, Type *Ty, | ||
| unsigned Sz) { | ||
| if (Sz <= 1) | ||
| return false; | ||
| if (!isValidElementType(Ty) && !isa<FixedVectorType>(Ty)) | ||
| return false; | ||
| if (has_single_bit(Sz)) | ||
| return true; | ||
| const unsigned NumParts = TTI.getNumberOfParts(getWidenedType(Ty, Sz)); | ||
| return NumParts > 0 && NumParts < Sz && has_single_bit(Sz / NumParts) && | ||
| Sz % NumParts == 0; | ||
| } | ||
|
|
||
| namespace slpvectorizer { | ||
|
|
||
| /// Bottom Up SLP Vectorizer. | ||
|
|
@@ -2466,7 +2496,9 @@ class BoUpSLP { | |
| } | ||
| // TODO: Check if we can remove a check for non-power-2 number of | ||
| // scalars after full support of non-power-2 vectorization. | ||
| return UniqueValues.size() != 2 && has_single_bit(UniqueValues.size()); | ||
| return UniqueValues.size() != 2 && | ||
| hasFullVectorsOnly(*R.TTI, (*UniqueValues.begin())->getType(), | ||
|
||
| UniqueValues.size()); | ||
| }; | ||
|
|
||
| // If the initial strategy fails for any of the operand indexes, then we | ||
|
|
@@ -3275,8 +3307,9 @@ class BoUpSLP { | |
| SmallVectorImpl<Value *> *AltScalars = nullptr) const; | ||
|
|
||
| /// Return true if this is a non-power-of-2 node. | ||
| bool isNonPowOf2Vec() const { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Request: Please leave the original method, and add a new one called isAllowedNonPowOfTwo. |
||
| bool IsNonPowerOf2 = !has_single_bit(Scalars.size()); | ||
| bool hasNonWholeRegisterElems(const TargetTransformInfo &TTI) const { | ||
| bool IsNonPowerOf2 = !hasFullVectorsOnly( | ||
| TTI, getValueType(Scalars.front()), Scalars.size()); | ||
| assert((!IsNonPowerOf2 || ReuseShuffleIndices.empty()) && | ||
| "Reshuffling not supported with non-power-of-2 vectors yet."); | ||
| return IsNonPowerOf2; | ||
|
|
@@ -3390,7 +3423,8 @@ class BoUpSLP { | |
| Last->State = EntryState; | ||
| // FIXME: Remove once support for ReuseShuffleIndices has been implemented | ||
| // for non-power-of-two vectors. | ||
| assert((has_single_bit(VL.size()) || ReuseShuffleIndices.empty()) && | ||
| assert((hasFullVectorsOnly(*TTI, getValueType(VL.front()), VL.size()) || | ||
|
||
| ReuseShuffleIndices.empty()) && | ||
| "Reshuffling scalars not yet supported for nodes with padding"); | ||
| Last->ReuseShuffleIndices.append(ReuseShuffleIndices.begin(), | ||
| ReuseShuffleIndices.end()); | ||
|
|
@@ -5200,7 +5234,7 @@ BoUpSLP::getReorderingData(const TreeEntry &TE, bool TopToBottom) { | |
| // node. | ||
| if (!TE.ReuseShuffleIndices.empty()) { | ||
| // FIXME: Support ReuseShuffleIndices for non-power-of-two vectors. | ||
| assert(!TE.isNonPowOf2Vec() && | ||
| assert(!TE.hasNonWholeRegisterElems(*TTI) && | ||
|
||
| "Reshuffling scalars not yet supported for nodes with padding"); | ||
|
|
||
| if (isSplat(TE.Scalars)) | ||
|
|
@@ -5431,7 +5465,7 @@ BoUpSLP::getReorderingData(const TreeEntry &TE, bool TopToBottom) { | |
|
|
||
| // FIXME: Remove the non-power-of-two check once findReusedOrderedScalars | ||
| // has been auditted for correctness with non-power-of-two vectors. | ||
| if (!TE.isNonPowOf2Vec()) | ||
| if (!TE.hasNonWholeRegisterElems(*TTI)) | ||
|
||
| if (std::optional<OrdersType> CurrentOrder = findReusedOrderedScalars(TE)) | ||
| return CurrentOrder; | ||
| } | ||
|
|
@@ -5584,15 +5618,18 @@ void BoUpSLP::reorderTopToBottom() { | |
| }); | ||
|
|
||
| // Reorder the graph nodes according to their vectorization factor. | ||
| for (unsigned VF = VectorizableTree.front()->getVectorFactor(); VF > 1; | ||
| VF = bit_ceil(VF) / 2) { | ||
| for (unsigned VF = VectorizableTree.front()->getVectorFactor(); | ||
| !VFToOrderedEntries.empty() && VF > 1; VF -= 2 - (VF & 1U)) { | ||
| auto It = VFToOrderedEntries.find(VF); | ||
| if (It == VFToOrderedEntries.end()) | ||
| continue; | ||
| // Try to find the most profitable order. We just are looking for the most | ||
| // used order and reorder scalar elements in the nodes according to this | ||
| // mostly used order. | ||
| ArrayRef<TreeEntry *> OrderedEntries = It->second.getArrayRef(); | ||
| // Delete VF entry upon exit. | ||
| auto Cleanup = make_scope_exit([&]() { VFToOrderedEntries.erase(It); }); | ||
|
|
||
| // All operands are reordered and used only in this node - propagate the | ||
| // most used order to the user node. | ||
| MapVector<OrdersType, unsigned, | ||
|
|
@@ -6574,7 +6611,7 @@ BoUpSLP::TreeEntry::EntryState BoUpSLP::getScalarsVectorizationState( | |
| case Instruction::ExtractElement: { | ||
| bool Reuse = canReuseExtract(VL, VL0, CurrentOrder); | ||
| // FIXME: Vectorizing is not supported yet for non-power-of-2 ops. | ||
| if (!has_single_bit(VL.size())) | ||
| if (!hasFullVectorsOnly(*TTI, VL0->getType(), VL.size())) | ||
|
||
| return TreeEntry::NeedToGather; | ||
| if (Reuse || !CurrentOrder.empty()) | ||
| return TreeEntry::Vectorize; | ||
|
|
@@ -6980,33 +7017,36 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth, | |
| UniqueValues.emplace_back(V); | ||
| } | ||
| size_t NumUniqueScalarValues = UniqueValues.size(); | ||
| if (NumUniqueScalarValues == VL.size()) { | ||
| bool IsFullVectors = hasFullVectorsOnly( | ||
| *TTI, UniqueValues.front()->getType(), NumUniqueScalarValues); | ||
| if (NumUniqueScalarValues == VL.size() && | ||
| (VectorizeNonPowerOf2 || IsFullVectors)) { | ||
| ReuseShuffleIndices.clear(); | ||
| } else { | ||
| // FIXME: Reshuffing scalars is not supported yet for non-power-of-2 ops. | ||
| if ((UserTreeIdx.UserTE && UserTreeIdx.UserTE->isNonPowOf2Vec()) || | ||
| !llvm::has_single_bit(VL.size())) { | ||
| if ((UserTreeIdx.UserTE && | ||
| UserTreeIdx.UserTE->hasNonWholeRegisterElems(*TTI)) || | ||
| !has_single_bit(VL.size())) { | ||
| LLVM_DEBUG(dbgs() << "SLP: Reshuffling scalars not yet supported " | ||
| "for nodes with padding.\n"); | ||
| newTreeEntry(VL, std::nullopt /*not vectorized*/, S, UserTreeIdx); | ||
| return false; | ||
| } | ||
| LLVM_DEBUG(dbgs() << "SLP: Shuffle for reused scalars.\n"); | ||
| if (NumUniqueScalarValues <= 1 || | ||
| (UniquePositions.size() == 1 && all_of(UniqueValues, | ||
| [](Value *V) { | ||
| return isa<UndefValue>(V) || | ||
| !isConstant(V); | ||
| })) || | ||
| !llvm::has_single_bit<uint32_t>(NumUniqueScalarValues)) { | ||
| if (NumUniqueScalarValues <= 1 || !IsFullVectors || | ||
| (UniquePositions.size() == 1 && all_of(UniqueValues, [](Value *V) { | ||
| return isa<UndefValue>(V) || !isConstant(V); | ||
| }))) { | ||
| if (DoNotFail && UniquePositions.size() > 1 && | ||
| NumUniqueScalarValues > 1 && S.MainOp->isSafeToRemove() && | ||
| all_of(UniqueValues, [=](Value *V) { | ||
| return isa<ExtractElementInst>(V) || | ||
| areAllUsersVectorized(cast<Instruction>(V), | ||
| UserIgnoreList); | ||
| })) { | ||
| unsigned PWSz = PowerOf2Ceil(UniqueValues.size()); | ||
| // Find the number of elements, which forms full vectors. | ||
| unsigned PWSz = getFullVectorNumberOfElements( | ||
| *TTI, UniqueValues.front()->getType(), UniqueValues.size()); | ||
| if (PWSz == VL.size()) { | ||
| ReuseShuffleIndices.clear(); | ||
| } else { | ||
|
|
@@ -9101,9 +9141,6 @@ class BoUpSLP::ShuffleCostEstimator : public BaseShuffleAnalysis { | |
| return nullptr; | ||
| Value *VecBase = nullptr; | ||
| ArrayRef<Value *> VL = E->Scalars; | ||
| // If the resulting type is scalarized, do not adjust the cost. | ||
| if (NumParts == VL.size()) | ||
| return nullptr; | ||
| // Check if it can be considered reused if same extractelements were | ||
| // vectorized already. | ||
| bool PrevNodeFound = any_of( | ||
|
|
@@ -9756,7 +9793,7 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals, | |
| InsertMask[Idx] = I + 1; | ||
| } | ||
| unsigned VecScalarsSz = PowerOf2Ceil(NumElts); | ||
| if (NumOfParts > 0) | ||
| if (NumOfParts > 0 && NumOfParts < NumElts) | ||
| VecScalarsSz = PowerOf2Ceil((NumElts + NumOfParts - 1) / NumOfParts); | ||
| unsigned VecSz = (1 + OffsetEnd / VecScalarsSz - OffsetBeg / VecScalarsSz) * | ||
| VecScalarsSz; | ||
|
|
@@ -10985,7 +11022,9 @@ InstructionCost BoUpSLP::getTreeCost(ArrayRef<Value *> VectorizedVals) { | |
| // Keep original scalar if number of externally used instructions in | ||
| // the same entry is not power of 2. It may help to do some extra | ||
| // vectorization for now. | ||
| KeepScalar = ScalarUsesCount <= 1 || !has_single_bit(ScalarUsesCount); | ||
| KeepScalar = | ||
| ScalarUsesCount <= 1 || | ||
| !hasFullVectorsOnly(*TTI, EU.Scalar->getType(), ScalarUsesCount); | ||
| } | ||
| if (KeepScalar) { | ||
| ExternalUsesAsOriginalScalar.insert(EU.Scalar); | ||
|
|
@@ -11682,7 +11721,7 @@ BoUpSLP::isGatherShuffledEntry( | |
| if (TE == VectorizableTree.front().get()) | ||
| return {}; | ||
| // FIXME: Gathering for non-power-of-2 nodes not implemented yet. | ||
| if (TE->isNonPowOf2Vec()) | ||
| if (TE->hasNonWholeRegisterElems(*TTI)) | ||
| return {}; | ||
| Mask.assign(VL.size(), PoisonMaskElem); | ||
| assert(TE->UserTreeIndices.size() == 1 && | ||
|
|
@@ -16999,7 +17038,7 @@ bool SLPVectorizerPass::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R, | |
| for (unsigned I = NextInst; I < MaxInst; ++I) { | ||
| unsigned ActualVF = std::min(MaxInst - I, VF); | ||
|
|
||
| if (!has_single_bit(ActualVF)) | ||
| if (!hasFullVectorsOnly(*TTI, ScalarTy, ActualVF)) | ||
| continue; | ||
|
|
||
| if (MaxVFOnly && ActualVF < MaxVF) | ||
|
|
||
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.
I don't think there's any requirement that a power of two VL is a full register? Consider SEW=64, VL=2 on RISCV. With zvl256b this is at most half of a register. At zvl128b, it might be a full register (or might not.)
Uh oh!
There was an error while loading. Please reload this page.
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.
Cust currently SLP supports power-of-2 elements, no matter if it uses full registers or not. This check just checks for what we currently have in the compiler by default, it is not new requirement. I'll rename the function.