[llvm] [LAA] Avoid unprofitable unit-stride versioning (PR #225331)
David Sherwood via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 29 00:59:25 PDT 2026
================
@@ -3244,42 +3245,33 @@ void LoopAccessInfo::collectStridedAccess(Value *MemAccess) {
// of various possible stride specializations, considering the alternatives
// of using gather/scatters (if available).
- const SCEV *MaxBTC = PSE->getSymbolicMaxBackedgeTakenCount();
-
- // Match the types so we can compare the stride and the MaxBTC.
- // The Stride can be positive/negative, so we sign extend Stride;
- // The backedgeTakenCount is non-negative, so we zero extend MaxBTC.
- const DataLayout &DL = TheLoop->getHeader()->getDataLayout();
- uint64_t StrideTypeSizeBits = DL.getTypeSizeInBits(StrideExpr->getType());
- uint64_t BETypeSizeBits = DL.getTypeSizeInBits(MaxBTC->getType());
- const SCEV *CastedStride = StrideExpr;
- const SCEV *CastedBECount = MaxBTC;
ScalarEvolution *SE = PSE->getSE();
- if (BETypeSizeBits >= StrideTypeSizeBits)
- CastedStride = SE->getNoopOrSignExtend(StrideExpr, MaxBTC->getType());
- else
- CastedBECount = SE->getZeroExtendExpr(MaxBTC, StrideExpr->getType());
- const SCEV *StrideMinusBETaken = SE->getMinusSCEV(CastedStride, CastedBECount);
- // Since TripCount == BackEdgeTakenCount + 1, checking:
- // "Stride >= TripCount" is equivalent to checking:
- // Stride - MaxBTC> 0
- if (SE->isKnownPositive(StrideMinusBETaken)) {
- LLVM_DEBUG(
- dbgs() << "LAA: Stride>=TripCount; No point in versioning as the "
- "Stride==1 predicate will imply that the loop executes "
- "at most once.\n");
+ if (!SE->isAvailableAtLoopEntry(StrideExpr, TheLoop))
----------------
david-arm wrote:
This looks like it could be committed in a separate PR and would apply equally to the existing code. I'm just curious in what situations we create a stride predicate when the stride is unavailable? I can see that `isAvailableAtLoopEntry` tests for loop invariance of the stride, but I'm quite surprised this wasn't rejected already by `getStrideFromPointer` since it looks for affine add-rec pointer SCEVs. See the code in `getStrideFromPointer`:
```
if (!match(V, m_scev_AffineAddRec(m_SCEV(), m_SCEV(V), m_SpecificLoop(Lp))))
return nullptr;
```
The other thing checked by `isAvailableAtLoopEntry` is that the stride properly dominates the loop header. Do any of the tests in this PR explicitly expose a case where dominance is a problem?
https://github.com/llvm/llvm-project/pull/225331
More information about the llvm-commits
mailing list