Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions llvm/include/llvm/Analysis/LoopAccessAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ class MemoryDepChecker {
return MaxSafeVectorWidthInBits;
}

/// Return safe power-of-2 number of elements, which do not prevent store-load
/// forwarding and safe to operate simultaneously.
std::optional<uint64_t> getStoreLoadForwardSafeVF() const {
return MaxStoreLoadForwardSafeVF;
}

/// In same cases when the dependency check fails we can still
/// vectorize the loop with a dynamic array access check.
bool shouldRetryWithRuntimeCheck() const {
Expand Down Expand Up @@ -304,6 +310,10 @@ class MemoryDepChecker {
/// restrictive.
uint64_t MaxSafeVectorWidthInBits = -1U;

/// Maximum number of elements (power-of-2 and non-power-of-2), which do not
/// prevent store-load forwarding and safe to operate simultaneously.
std::optional<uint64_t> MaxStoreLoadForwardSafeVF;

/// If we see a non-constant dependence distance we can still try to
/// vectorize this loop with runtime checks.
bool FoundNonConstantDistanceDependence = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,12 @@ class LoopVectorizationLegality {
return hasUncountableEarlyExit() ? getUncountableEdge()->second : nullptr;
}

/// Return safe power-of-2 number of elements, which do not prevent store-load
/// forwarding and safe to operate simultaneously.
std::optional<unsigned> getMaxStoreLoadForwardSafeVFPowerOf2() const {
return LAI->getDepChecker().getStoreLoadForwardSafeVF();
}

/// Returns true if vector representation of the instruction \p I
/// requires mask.
bool isMaskRequired(const Instruction *I) const {
Expand Down
35 changes: 23 additions & 12 deletions llvm/lib/Analysis/LoopAccessAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1752,31 +1752,34 @@ bool MemoryDepChecker::couldPreventStoreLoadForward(uint64_t Distance,
// cause any slowdowns.
const uint64_t NumItersForStoreLoadThroughMemory = 8 * TypeByteSize;
// Maximum vector factor.
uint64_t MaxVFWithoutSLForwardIssues = std::min(
VectorizerParams::MaxVectorWidth * TypeByteSize, MinDepDistBytes);
uint64_t MaxVFWithoutSLForwardIssuesPowerOf2 = std::min(
VectorizerParams::MaxVectorWidth * TypeByteSize,
MaxStoreLoadForwardSafeVF.value_or(std::numeric_limits<uint64_t>::max()));

// Compute the smallest VF at which the store and load would be misaligned.
for (uint64_t VF = 2 * TypeByteSize; VF <= MaxVFWithoutSLForwardIssues;
VF *= 2) {
for (uint64_t VF = 2 * TypeByteSize;
VF <= MaxVFWithoutSLForwardIssuesPowerOf2; VF *= 2) {
// If the number of vector iteration between the store and the load are
// small we could incur conflicts.
if (Distance % VF && Distance / VF < NumItersForStoreLoadThroughMemory) {
MaxVFWithoutSLForwardIssues = (VF >> 1);
MaxVFWithoutSLForwardIssuesPowerOf2 = (VF >> 1);
break;
}
}

if (MaxVFWithoutSLForwardIssues < 2 * TypeByteSize) {
if (MaxVFWithoutSLForwardIssuesPowerOf2 < 2 * TypeByteSize) {
LLVM_DEBUG(
dbgs() << "LAA: Distance " << Distance
<< " that could cause a store-load forwarding conflict\n");
return true;
}

if (MaxVFWithoutSLForwardIssues < MinDepDistBytes &&
MaxVFWithoutSLForwardIssues !=
if (MaxVFWithoutSLForwardIssuesPowerOf2 <
MaxStoreLoadForwardSafeVF.value_or(
std::numeric_limits<uint64_t>::max()) &&
MaxVFWithoutSLForwardIssuesPowerOf2 !=
VectorizerParams::MaxVectorWidth * TypeByteSize)
MinDepDistBytes = MaxVFWithoutSLForwardIssues;
MinDepDistBytes = MaxVFWithoutSLForwardIssuesPowerOf2;
return false;
}

Expand Down Expand Up @@ -2236,8 +2239,9 @@ MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
return Dependence::BackwardVectorizableButPreventsForwarding;
}

// An update to MinDepDistBytes requires an update to MaxSafeVectorWidthInBits
// since there is a backwards dependency.
// An update to MinDepDistBytes requires an update to
// MaxStoreLoadForwardSafeVF/MaxSafeVectorWidthInBits since there is a
// backwards dependency.
uint64_t MaxVF = MinDepDistBytes / *CommonStride;
LLVM_DEBUG(dbgs() << "LAA: Positive min distance " << MinDistance
<< " with max VF = " << MaxVF << '\n');
Expand All @@ -2250,7 +2254,11 @@ MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
return Dependence::Unknown;
}

MaxSafeVectorWidthInBits = std::min(MaxSafeVectorWidthInBits, MaxVFInBits);
if (IsTrueDataDependence && EnableForwardingConflictDetection && ConstDist)
MaxStoreLoadForwardSafeVF =
std::min(MaxStoreLoadForwardSafeVF.value_or(MaxVFInBits), MaxVFInBits);
else
MaxSafeVectorWidthInBits = std::min(MaxSafeVectorWidthInBits, MaxVFInBits);
return Dependence::BackwardVectorizable;
}

Expand Down Expand Up @@ -3001,6 +3009,9 @@ void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {
if (!DC.isSafeForAnyVectorWidth())
OS << " with a maximum safe vector width of "
<< DC.getMaxSafeVectorWidthInBits() << " bits";
if (std::optional<unsigned> SLDist = DC.getStoreLoadForwardSafeVF())
OS << ", with a maximum safe store-load forward width of " << *SLDist
<< " bits";
if (PtrRtChecking->Need)
OS << " with run-time checks";
OS << "\n";
Expand Down
69 changes: 53 additions & 16 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1428,8 +1428,11 @@ class LoopVectorizationCostModel {
/// Selects and saves TailFoldingStyle for 2 options - if IV update may
/// overflow or not.
/// \param IsScalableVF true if scalable vector factors enabled.
/// \param CanTailFoldPowOf2 true if tail folding with power-of-2
/// safe distance can be enabled.
/// \param UserIC User specific interleave count.
void setTailFoldingStyles(bool IsScalableVF, unsigned UserIC) {
void setTailFoldingStyles(bool IsScalableVF, bool CanTailFoldPowOf2,
unsigned UserIC) {
assert(!ChosenTailFoldingStyle && "Tail folding must not be selected yet.");
if (!Legal->canFoldTailByMasking()) {
ChosenTailFoldingStyle =
Expand All @@ -1438,17 +1441,25 @@ class LoopVectorizationCostModel {
}

if (!ForceTailFoldingStyle.getNumOccurrences()) {
ChosenTailFoldingStyle = std::make_pair(
TTI.getPreferredTailFoldingStyle(/*IVUpdateMayOverflow=*/true),
TTI.getPreferredTailFoldingStyle(/*IVUpdateMayOverflow=*/false));
if (!CanTailFoldPowOf2)
ChosenTailFoldingStyle =
std::make_pair(TailFoldingStyle::None, TailFoldingStyle::None);
else
ChosenTailFoldingStyle = std::make_pair(
TTI.getPreferredTailFoldingStyle(/*IVUpdateMayOverflow=*/true),
TTI.getPreferredTailFoldingStyle(/*IVUpdateMayOverflow=*/false));
return;
}

// Set styles when forced.
ChosenTailFoldingStyle = std::make_pair(ForceTailFoldingStyle.getValue(),
ForceTailFoldingStyle.getValue());
if (ForceTailFoldingStyle != TailFoldingStyle::DataWithEVL)
if (ForceTailFoldingStyle != TailFoldingStyle::DataWithEVL) {
if (!CanTailFoldPowOf2)
ChosenTailFoldingStyle =
std::make_pair(TailFoldingStyle::None, TailFoldingStyle::None);
return;
}
// Override forced styles if needed.
// FIXME: use actual opcode/data type for analysis here.
// FIXME: Investigate opportunity for fixed vector factor.
Expand All @@ -1459,6 +1470,11 @@ class LoopVectorizationCostModel {
!EnableVPlanNativePath &&
Legal->getFixedOrderRecurrences().empty();
if (!EVLIsLegal) {
if (!CanTailFoldPowOf2) {
ChosenTailFoldingStyle =
std::make_pair(TailFoldingStyle::None, TailFoldingStyle::None);
return;
}
// If for some reason EVL mode is unsupported, fallback to
// DataWithoutLaneMask to try to vectorize the loop with folded tail
// in a generic way.
Expand Down Expand Up @@ -3835,7 +3851,9 @@ bool LoopVectorizationCostModel::isScalableVectorizationAllowed() {
return false;
}

if (!Legal->isSafeForAnyVectorWidth() && !getMaxVScale(*TheFunction, TTI)) {
if ((!Legal->isSafeForAnyVectorWidth() ||
Legal->getMaxStoreLoadForwardSafeVFPowerOf2()) &&
!getMaxVScale(*TheFunction, TTI)) {
reportVectorizationInfo("The target does not provide maximum vscale value "
"for safe distance analysis.",
"ScalableVFUnfeasible", ORE, TheLoop);
Expand All @@ -3853,7 +3871,8 @@ LoopVectorizationCostModel::getMaxLegalScalableVF(unsigned MaxSafeElements) {

auto MaxScalableVF = ElementCount::getScalable(
std::numeric_limits<ElementCount::ScalarTy>::max());
if (Legal->isSafeForAnyVectorWidth())
if (Legal->isSafeForAnyVectorWidth() &&
!Legal->getMaxStoreLoadForwardSafeVFPowerOf2())
return MaxScalableVF;

std::optional<unsigned> MaxVScale = getMaxVScale(*TheFunction, TTI);
Expand All @@ -3879,13 +3898,22 @@ FixedScalableVFPair LoopVectorizationCostModel::computeFeasibleMaxVF(
// It is computed by MaxVF * sizeOf(type) * 8, where type is taken from
// the memory accesses that is most restrictive (involved in the smallest
// dependence distance).
unsigned MaxSafeElements =
llvm::bit_floor(Legal->getMaxSafeVectorWidthInBits() / WidestType);

auto MaxSafeFixedVF = ElementCount::getFixed(MaxSafeElements);
auto MaxSafeScalableVF = getMaxLegalScalableVF(MaxSafeElements);
if (!Legal->isSafeForAnyVectorWidth())
this->MaxSafeElements = MaxSafeElements;
unsigned MaxSafeElements = Legal->getMaxSafeVectorWidthInBits() / WidestType;
if (Legal->isSafeForAnyVectorWidth())
MaxSafeElements = bit_ceil(MaxSafeElements);
else
MaxSafeElements = bit_floor(MaxSafeElements);
unsigned MaxSafeElementsPowerOf2 = MaxSafeElements;
if (std::optional<unsigned> SLDist =
Legal->getMaxStoreLoadForwardSafeVFPowerOf2())
MaxSafeElementsPowerOf2 =
std::min(MaxSafeElementsPowerOf2, *SLDist / WidestType);
auto MaxSafeFixedVF = ElementCount::getFixed(MaxSafeElementsPowerOf2);
auto MaxSafeScalableVF = getMaxLegalScalableVF(MaxSafeElementsPowerOf2);

if (!Legal->isSafeForAnyVectorWidth() ||
Legal->getMaxStoreLoadForwardSafeVFPowerOf2())
this->MaxSafeElements = MaxSafeElementsPowerOf2;

LLVM_DEBUG(dbgs() << "LV: The max safe fixed VF is: " << MaxSafeFixedVF
<< ".\n");
Expand Down Expand Up @@ -4113,14 +4141,16 @@ LoopVectorizationCostModel::computeMaxVF(ElementCount UserVF, unsigned UserIC) {
LLVM_DEBUG(dbgs() << "LV: No tail will remain for any chosen VF.\n");
return MaxFactors;
}
MaxPowerOf2RuntimeVF.reset();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to the PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, without it the test are failed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But is this only due to the code below added by the patch?

  if (MaxPowerOf2RuntimeVF) {
     // Accept MaxFixedVF if we do not have a tail.
     LLVM_DEBUG(dbgs() << "LV: No tail will remain for any chosen VF.\n");
     return MaxFactors;
   }

It is not clear to me why MaxPowerOf2RuntimeVF set would mean no tail remains, the only place we can guarantee no tail at the moment is the code just above here, which checks against TC?

}

// If we don't know the precise trip count, or if the trip count that we
// found modulo the vectorization factor is not zero, try to fold the tail
// by masking.
// FIXME: look for a smaller MaxVF that does divide TC rather than masking.
bool ContainsScalableVF = MaxFactors.ScalableVF.isNonZero();
setTailFoldingStyles(ContainsScalableVF, UserIC);
setTailFoldingStyles(ContainsScalableVF, !MaxPowerOf2RuntimeVF.has_value(),
UserIC);
if (foldTailByMasking()) {
if (getTailFoldingStyle() == TailFoldingStyle::DataWithEVL) {
LLVM_DEBUG(
Expand All @@ -4138,6 +4168,12 @@ LoopVectorizationCostModel::computeMaxVF(ElementCount UserVF, unsigned UserIC) {
return MaxFactors;
}

if (MaxPowerOf2RuntimeVF) {
// Accept MaxFixedVF if we do not have a tail.
LLVM_DEBUG(dbgs() << "LV: No tail will remain for any chosen VF.\n");
return MaxFactors;
}

// If there was a tail-folding hint/switch, but we can't fold the tail by
// masking, fallback to a vectorization with a scalar epilogue.
if (ScalarEpilogueStatus == CM_ScalarEpilogueNotNeededUsePredicate) {
Expand Down Expand Up @@ -4913,7 +4949,8 @@ LoopVectorizationCostModel::selectInterleaveCount(ElementCount VF,
}

// We used the distance for the interleave count.
if (!Legal->isSafeForAnyVectorWidth())
if (!Legal->isSafeForAnyVectorWidth() ||
Legal->getMaxStoreLoadForwardSafeVFPowerOf2())
return 1;

// We don't attempt to perform interleaving for loops with uncountable early
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
; for (i = 0; i < n; i++)
; A[i + 4] = A[i] * 2;

; CHECK: Memory dependences are safe with a maximum safe vector width of 64 bits
; CHECK: Memory dependences are safe, with a maximum safe store-load forward width of 64 bits

target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.10.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ for.body: ; preds = %entry, %for.body
define void @vectorizable_Read_Write(ptr nocapture %A) {
; CHECK-LABEL: 'vectorizable_Read_Write'
; CHECK-NEXT: for.body:
; CHECK-NEXT: Memory dependences are safe with a maximum safe vector width of 64 bits
; CHECK-NEXT: Memory dependences are safe, with a maximum safe store-load forward width of 64 bits
; CHECK-NEXT: Dependences:
; CHECK-NEXT: BackwardVectorizable:
; CHECK-NEXT: %0 = load i32, ptr %arrayidx, align 4 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ define void @vector_reverse_i64(ptr nocapture noundef writeonly %A, ptr nocaptur
; CHECK-NEXT: LV: Found trip count: 0
; CHECK-NEXT: LV: Found maximum trip count: 4294967295
; CHECK-NEXT: LV: Scalable vectorization is available
; CHECK-NEXT: LV: The max safe fixed VF is: 67108864.
; CHECK-NEXT: LV: The max safe fixed VF is: 134217728.
; CHECK-NEXT: LV: The max safe scalable VF is: vscale x 4294967295.
; CHECK-NEXT: LV: Found uniform instruction: %cmp = icmp ugt i64 %indvars.iv, 1
; CHECK-NEXT: LV: Found uniform instruction: %arrayidx = getelementptr inbounds i32, ptr %B, i64 %idxprom
Expand Down Expand Up @@ -270,7 +270,7 @@ define void @vector_reverse_f32(ptr nocapture noundef writeonly %A, ptr nocaptur
; CHECK-NEXT: LV: Found trip count: 0
; CHECK-NEXT: LV: Found maximum trip count: 4294967295
; CHECK-NEXT: LV: Scalable vectorization is available
; CHECK-NEXT: LV: The max safe fixed VF is: 67108864.
; CHECK-NEXT: LV: The max safe fixed VF is: 134217728.
; CHECK-NEXT: LV: The max safe scalable VF is: vscale x 4294967295.
; CHECK-NEXT: LV: Found uniform instruction: %cmp = icmp ugt i64 %indvars.iv, 1
; CHECK-NEXT: LV: Found uniform instruction: %arrayidx = getelementptr inbounds float, ptr %B, i64 %idxprom
Expand Down
Loading