Skip to content

Commit 93736a3

Browse files
committed
Wrapping test need to be done only if runtime checks present
Code form for llvm#128061 (comment).
1 parent 7e105fb commit 93736a3

File tree

6 files changed

+32
-22
lines changed

6 files changed

+32
-22
lines changed

llvm/include/llvm/Analysis/Loads.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,15 @@ LLVM_ABI bool isSafeToLoadUnconditionally(
8383
LLVM_ABI bool isDereferenceableAndAlignedInLoop(
8484
LoadInst *LI, Loop *L, ScalarEvolution &SE, DominatorTree &DT,
8585
AssumptionCache *AC = nullptr,
86-
SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr);
86+
SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr,
87+
bool ShouldCheckWrapping = true);
8788

8889
/// Return true if the loop \p L cannot fault on any iteration and only
8990
/// contains read-only memory accesses.
9091
LLVM_ABI bool isDereferenceableReadOnlyLoop(
9192
Loop *L, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
92-
SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr);
93+
SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr,
94+
bool ShouldCheckWrapping = true);
9395

9496
/// Return true if we know that executing a load from this value cannot trap.
9597
///

llvm/include/llvm/Analysis/LoopAccessAnalysis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ LLVM_ABI std::pair<const SCEV *, const SCEV *> getStartAndEndForAccess(
922922
const Loop *Lp, const SCEV *PtrExpr, Type *AccessTy, const SCEV *BTC,
923923
const SCEV *MaxBTC, ScalarEvolution *SE,
924924
DenseMap<std::pair<const SCEV *, Type *>,
925-
std::pair<const SCEV *, const SCEV *>> *PointerBounds);
925+
std::pair<const SCEV *, const SCEV *>> *PointerBounds, bool ShouldCheckWrapping = true);
926926

927927
class LoopAccessInfoManager {
928928
/// The cache.

llvm/include/llvm/Transforms/Vectorize/LoopVectorizationLegality.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ class LoopVectorizationLegality {
542542
/// The list above is not based on theoretical limitations of vectorization,
543543
/// but simply a statement that more work is needed to support these
544544
/// additional cases safely.
545-
bool isVectorizableEarlyExitLoop();
545+
bool isVectorizableEarlyExitLoop(const bool NeedRuntimeChecks);
546546

547547
/// Return true if all of the instructions in the block can be speculatively
548548
/// executed, and record the loads/stores that require masking.

llvm/lib/Analysis/Loads.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
288288

289289
bool llvm::isDereferenceableAndAlignedInLoop(
290290
LoadInst *LI, Loop *L, ScalarEvolution &SE, DominatorTree &DT,
291-
AssumptionCache *AC, SmallVectorImpl<const SCEVPredicate *> *Predicates) {
291+
AssumptionCache *AC, SmallVectorImpl<const SCEVPredicate *> *Predicates,
292+
bool ShouldCheckWrapping) {
292293
const Align Alignment = LI->getAlign();
293294
auto &DL = LI->getDataLayout();
294295
Value *Ptr = LI->getPointerOperand();
@@ -341,8 +342,9 @@ bool llvm::isDereferenceableAndAlignedInLoop(
341342
? SE.getPredicatedConstantMaxBackedgeTakenCount(L, *Predicates)
342343
: SE.getConstantMaxBackedgeTakenCount(L);
343344
}
344-
const auto &[AccessStart, AccessEnd] = getStartAndEndForAccess(
345-
L, PtrScev, LI->getType(), BECount, MaxBECount, &SE, nullptr);
345+
const auto &[AccessStart, AccessEnd] =
346+
getStartAndEndForAccess(L, PtrScev, LI->getType(), BECount, MaxBECount,
347+
&SE, nullptr, ShouldCheckWrapping);
346348
if (isa<SCEVCouldNotCompute>(AccessStart) ||
347349
isa<SCEVCouldNotCompute>(AccessEnd))
348350
return false;
@@ -850,11 +852,13 @@ bool llvm::canReplacePointersIfEqual(const Value *From, const Value *To,
850852

851853
bool llvm::isDereferenceableReadOnlyLoop(
852854
Loop *L, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
853-
SmallVectorImpl<const SCEVPredicate *> *Predicates) {
855+
SmallVectorImpl<const SCEVPredicate *> *Predicates,
856+
bool ShouldCheckWrapping) {
854857
for (BasicBlock *BB : L->blocks()) {
855858
for (Instruction &I : *BB) {
856859
if (auto *LI = dyn_cast<LoadInst>(&I)) {
857-
if (!isDereferenceableAndAlignedInLoop(LI, L, *SE, *DT, AC, Predicates))
860+
if (!isDereferenceableAndAlignedInLoop(LI, L, *SE, *DT, AC, Predicates,
861+
ShouldCheckWrapping))
858862
return false;
859863
} else if (I.mayReadFromMemory() || I.mayWriteToMemory() || I.mayThrow())
860864
return false;

llvm/lib/Analysis/LoopAccessAnalysis.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ std::pair<const SCEV *, const SCEV *> llvm::getStartAndEndForAccess(
273273
const Loop *Lp, const SCEV *PtrExpr, Type *AccessTy, const SCEV *BTC,
274274
const SCEV *MaxBTC, ScalarEvolution *SE,
275275
DenseMap<std::pair<const SCEV *, Type *>,
276-
std::pair<const SCEV *, const SCEV *>> *PointerBounds) {
276+
std::pair<const SCEV *, const SCEV *>> *PointerBounds,
277+
bool ShouldCheckWrapping) {
277278
std::pair<const SCEV *, const SCEV *> *PtrBoundsPair;
278279
if (PointerBounds) {
279280
auto [Iter, Ins] = PointerBounds->insert(
@@ -308,8 +309,8 @@ std::pair<const SCEV *, const SCEV *> llvm::getStartAndEndForAccess(
308309
// sets ScEnd to the maximum unsigned value for the type. Note that LAA
309310
// separately checks that accesses cannot not wrap, so unsigned max
310311
// represents an upper bound.
311-
if (evaluatePtrAddRecAtMaxBTCWillNotWrap(AR, MaxBTC, EltSizeSCEV, *SE,
312-
DL)) {
312+
if (!ShouldCheckWrapping || evaluatePtrAddRecAtMaxBTCWillNotWrap(
313+
AR, MaxBTC, EltSizeSCEV, *SE, DL)) {
313314
ScEnd = AR->evaluateAtIteration(MaxBTC, *SE);
314315
} else {
315316
ScEnd = SE->getAddExpr(

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,8 @@ bool LoopVectorizationLegality::canVectorizeLoopNestCFG(
16431643
return Result;
16441644
}
16451645

1646-
bool LoopVectorizationLegality::isVectorizableEarlyExitLoop() {
1646+
bool LoopVectorizationLegality::isVectorizableEarlyExitLoop(
1647+
const bool NeedRuntimeChecks) {
16471648
BasicBlock *LatchBB = TheLoop->getLoopLatch();
16481649
if (!LatchBB) {
16491650
reportVectorizationFailure("Loop does not have a latch",
@@ -1851,6 +1852,16 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
18511852
return false;
18521853
}
18531854

1855+
// Go over each instruction and look at memory deps.
1856+
if (!canVectorizeMemory()) {
1857+
LLVM_DEBUG(dbgs() << "LV: Can't vectorize due to memory conflicts\n");
1858+
if (DoExtraAnalysis)
1859+
Result = false;
1860+
else
1861+
return false;
1862+
}
1863+
1864+
auto NeedRuntimeChecks = LAI->getRuntimePointerChecking()->Need;
18541865
if (isa<SCEVCouldNotCompute>(PSE.getBackedgeTakenCount())) {
18551866
if (TheLoop->getExitingBlock()) {
18561867
reportVectorizationFailure("Cannot vectorize uncountable loop",
@@ -1860,7 +1871,7 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
18601871
else
18611872
return false;
18621873
} else {
1863-
if (!isVectorizableEarlyExitLoop()) {
1874+
if (!isVectorizableEarlyExitLoop(NeedRuntimeChecks)) {
18641875
UncountableEdge = std::nullopt;
18651876
if (DoExtraAnalysis)
18661877
Result = false;
@@ -1870,14 +1881,6 @@ bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
18701881
}
18711882
}
18721883

1873-
// Go over each instruction and look at memory deps.
1874-
if (!canVectorizeMemory()) {
1875-
LLVM_DEBUG(dbgs() << "LV: Can't vectorize due to memory conflicts\n");
1876-
if (DoExtraAnalysis)
1877-
Result = false;
1878-
else
1879-
return false;
1880-
}
18811884

18821885
if (Result) {
18831886
LLVM_DEBUG(dbgs() << "LV: We can vectorize this loop"

0 commit comments

Comments
 (0)