-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[SLP][NFC]Extract a check for strided loads into separate function, NFC #134876
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 1 commit into
main
from
users/alexey-bataev/spr/slpnfcextract-a-check-for-strided-loads-into-separate-function-nfc
Apr 8, 2025
Merged
[SLP][NFC]Extract a check for strided loads into separate function, NFC #134876
alexey-bataev
merged 1 commit into
main
from
users/alexey-bataev/spr/slpnfcextract-a-check-for-strided-loads-into-separate-function-nfc
Apr 8, 2025
Conversation
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
Created using spr 1.3.5
@llvm/pr-subscribers-vectorizers @llvm/pr-subscribers-llvm-transforms Author: Alexey Bataev (alexey-bataev) ChangesFull diff: https://github.com/llvm/llvm-project/pull/134876.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 0e6f7e8435e3a..06c1840529eb0 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -5597,6 +5597,71 @@ static bool isMaskedLoadCompress(
return TotalVecCost < GatherCost;
}
+/// Checks if strided loads can be generated out of \p VL loads with pointers \p
+/// PointerOps:
+/// 1. Target with strided load support is detected.
+/// 2. The number of loads is greater than MinProfitableStridedLoads, or the
+/// potential stride <= MaxProfitableLoadStride and the potential stride is
+/// power-of-2 (to avoid perf regressions for the very small number of loads)
+/// and max distance > number of loads, or potential stride is -1.
+/// 3. The loads are ordered, or number of unordered loads <=
+/// MaxProfitableUnorderedLoads, or loads are in reversed order. (this check is
+/// to avoid extra costs for very expensive shuffles).
+/// 4. Any pointer operand is an instruction with the users outside of the
+/// current graph (for masked gathers extra extractelement instructions
+/// might be required).
+static bool isStridedLoad(ArrayRef<Value *> VL, ArrayRef<Value *> PointerOps,
+ ArrayRef<unsigned> Order,
+ const TargetTransformInfo &TTI, const DataLayout &DL,
+ ScalarEvolution &SE,
+ const bool IsAnyPointerUsedOutGraph, const int Diff) {
+ const unsigned Sz = VL.size();
+ const unsigned AbsoluteDiff = std::abs(Diff);
+ Type *ScalarTy = VL.front()->getType();
+ auto *VecTy = getWidenedType(ScalarTy, Sz);
+ if (IsAnyPointerUsedOutGraph ||
+ (AbsoluteDiff > Sz &&
+ (Sz > MinProfitableStridedLoads ||
+ (AbsoluteDiff <= MaxProfitableLoadStride * Sz &&
+ AbsoluteDiff % Sz == 0 && has_single_bit(AbsoluteDiff / Sz)))) ||
+ Diff == -(static_cast<int>(Sz) - 1)) {
+ int Stride = Diff / static_cast<int>(Sz - 1);
+ if (Diff != Stride * static_cast<int>(Sz - 1))
+ return false;
+ Align Alignment =
+ cast<LoadInst>(Order.empty() ? VL.front() : VL[Order.front()])
+ ->getAlign();
+ if (!TTI.isLegalStridedLoadStore(VecTy, Alignment))
+ return false;
+ Value *Ptr0;
+ Value *PtrN;
+ if (Order.empty()) {
+ Ptr0 = PointerOps.front();
+ PtrN = PointerOps.back();
+ } else {
+ Ptr0 = PointerOps[Order.front()];
+ PtrN = PointerOps[Order.back()];
+ }
+ // Iterate through all pointers and check if all distances are
+ // unique multiple of Dist.
+ SmallSet<int, 4> Dists;
+ for (Value *Ptr : PointerOps) {
+ int Dist = 0;
+ if (Ptr == PtrN)
+ Dist = Diff;
+ else if (Ptr != Ptr0)
+ Dist = *getPointersDiff(ScalarTy, Ptr0, ScalarTy, Ptr, DL, SE);
+ // If the strides are not the same or repeated, we can't
+ // vectorize.
+ if (((Dist / Stride) * Stride) != Dist || !Dists.insert(Dist).second)
+ break;
+ }
+ if (Dists.size() == Sz)
+ return true;
+ }
+ return false;
+}
+
BoUpSLP::LoadsState
BoUpSLP::canVectorizeLoads(ArrayRef<Value *> VL, const Value *VL0,
SmallVectorImpl<unsigned> &Order,
@@ -5670,59 +5735,17 @@ BoUpSLP::canVectorizeLoads(ArrayRef<Value *> VL, const Value *VL0,
return LoadsState::Vectorize;
// Simple check if not a strided access - clear order.
bool IsPossibleStrided = *Diff % (Sz - 1) == 0;
- // Try to generate strided load node if:
- // 1. Target with strided load support is detected.
- // 2. The number of loads is greater than MinProfitableStridedLoads,
- // or the potential stride <= MaxProfitableLoadStride and the
- // potential stride is power-of-2 (to avoid perf regressions for the very
- // small number of loads) and max distance > number of loads, or potential
- // stride is -1.
- // 3. The loads are ordered, or number of unordered loads <=
- // MaxProfitableUnorderedLoads, or loads are in reversed order.
- // (this check is to avoid extra costs for very expensive shuffles).
- // 4. Any pointer operand is an instruction with the users outside of the
- // current graph (for masked gathers extra extractelement instructions
- // might be required).
+ // Try to generate strided load node.
auto IsAnyPointerUsedOutGraph =
IsPossibleStrided && any_of(PointerOps, [&](Value *V) {
return isa<Instruction>(V) && any_of(V->users(), [&](User *U) {
return !isVectorized(U) && !MustGather.contains(U);
});
});
- const unsigned AbsoluteDiff = std::abs(*Diff);
if (IsPossibleStrided &&
- (IsAnyPointerUsedOutGraph ||
- (AbsoluteDiff > Sz &&
- (Sz > MinProfitableStridedLoads ||
- (AbsoluteDiff <= MaxProfitableLoadStride * Sz &&
- AbsoluteDiff % Sz == 0 && has_single_bit(AbsoluteDiff / Sz)))) ||
- *Diff == -(static_cast<int>(Sz) - 1))) {
- int Stride = *Diff / static_cast<int>(Sz - 1);
- if (*Diff == Stride * static_cast<int>(Sz - 1)) {
- Align Alignment =
- cast<LoadInst>(Order.empty() ? VL.front() : VL[Order.front()])
- ->getAlign();
- if (TTI->isLegalStridedLoadStore(VecTy, Alignment)) {
- // Iterate through all pointers and check if all distances are
- // unique multiple of Dist.
- SmallSet<int, 4> Dists;
- for (Value *Ptr : PointerOps) {
- int Dist = 0;
- if (Ptr == PtrN)
- Dist = *Diff;
- else if (Ptr != Ptr0)
- Dist = *getPointersDiff(ScalarTy, Ptr0, ScalarTy, Ptr, *DL, *SE);
- // If the strides are not the same or repeated, we can't
- // vectorize.
- if (((Dist / Stride) * Stride) != Dist ||
- !Dists.insert(Dist).second)
- break;
- }
- if (Dists.size() == Sz)
- return LoadsState::StridedVectorize;
- }
- }
- }
+ isStridedLoad(VL, PointerOps, Order, *TTI, *DL, *SE,
+ IsAnyPointerUsedOutGraph, *Diff))
+ return LoadsState::StridedVectorize;
bool IsMasked;
unsigned InterleaveFactor;
SmallVector<int> CompressMask;
|
RKSimon
approved these changes
Apr 8, 2025
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.
LGTM - cheers
llvm-sync bot
pushed a commit
to arm/arm-toolchain
that referenced
this pull request
Apr 8, 2025
…function, NFC Reviewers: hiraditya, RKSimon Reviewed By: RKSimon Pull Request: llvm/llvm-project#134876
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.