Skip to content

Commit 7d867c6

Browse files
[LoopVersioningLICM] Only mark pointers with generated checks as noalias (llvm#135168)
Currently when we version a loop all loads and stores have the noalias metadata added to them. If there were some pointers that could not be analysed, and thus we could not generate runtime aliasing checks for, then we should not mark loads and stores using these pointers as noalias. This is done by getting rid of setNoAliasToLoop and instead using annotateLoopWithNoAlias, as that already correctly handles partial alias information. This does result in slightly different aliasing metadata being generated, but it looks like it's more precise. Currently this doesn't result in any change to the transforms that LoopVersioningLICM does, as LoopAccessAnalysis discards all results if it couldn't analyse every pointer leading to no loop versioning happening, but an upcoming patch will change that and we need this first otherwise we incorrectly mark some pointers as noalias even when they aren't.
1 parent 9e27db0 commit 7d867c6

File tree

4 files changed

+327
-49
lines changed

4 files changed

+327
-49
lines changed

llvm/lib/Transforms/Scalar/LoopVersioningLICM.cpp

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ struct LoopVersioningLICM {
164164
bool legalLoopInstructions();
165165
bool legalLoopMemoryAccesses();
166166
bool isLoopAlreadyVisited();
167-
void setNoAliasToLoop(Loop *VerLoop);
168167
bool instructionSafeForVersioning(Instruction *I);
169168
};
170169

@@ -344,6 +343,13 @@ bool LoopVersioningLICM::instructionSafeForVersioning(Instruction *I) {
344343
}
345344
LoadAndStoreCounter++;
346345
Value *Ptr = St->getPointerOperand();
346+
// Don't allow stores that we don't have runtime checks for, as we won't be
347+
// able to mark them noalias meaning they would prevent any code motion.
348+
auto &Pointers = LAI->getRuntimePointerChecking()->Pointers;
349+
if (!any_of(Pointers, [&](auto &P) { return P.PointerValue == Ptr; })) {
350+
LLVM_DEBUG(dbgs() << " Found a store without a runtime check.\n");
351+
return false;
352+
}
347353
// Check loop invariant.
348354
if (SE->isLoopInvariant(SE->getSCEV(Ptr), CurLoop))
349355
InvariantCounter++;
@@ -361,6 +367,13 @@ bool LoopVersioningLICM::legalLoopInstructions() {
361367
InvariantCounter = 0;
362368
IsReadOnlyLoop = true;
363369
using namespace ore;
370+
// Get LoopAccessInfo from current loop via the proxy.
371+
LAI = &LAIs.getInfo(*CurLoop);
372+
// Check LoopAccessInfo for need of runtime check.
373+
if (LAI->getRuntimePointerChecking()->getChecks().empty()) {
374+
LLVM_DEBUG(dbgs() << " LAA: Runtime check not found !!\n");
375+
return false;
376+
}
364377
// Iterate over loop blocks and instructions of each block and check
365378
// instruction safety.
366379
for (auto *Block : CurLoop->getBlocks())
@@ -374,13 +387,6 @@ bool LoopVersioningLICM::legalLoopInstructions() {
374387
return false;
375388
}
376389
}
377-
// Get LoopAccessInfo from current loop via the proxy.
378-
LAI = &LAIs.getInfo(*CurLoop);
379-
// Check LoopAccessInfo for need of runtime check.
380-
if (LAI->getRuntimePointerChecking()->getChecks().empty()) {
381-
LLVM_DEBUG(dbgs() << " LAA: Runtime check not found !!\n");
382-
return false;
383-
}
384390
// Number of runtime-checks should be less then RuntimeMemoryCheckThreshold
385391
if (LAI->getNumRuntimePointerChecks() >
386392
VectorizerParams::RuntimeMemoryCheckThreshold) {
@@ -501,41 +507,6 @@ bool LoopVersioningLICM::isLegalForVersioning() {
501507
return true;
502508
}
503509

504-
/// Update loop with aggressive aliasing assumptions.
505-
/// It marks no-alias to any pairs of memory operations by assuming
506-
/// loop should not have any must-alias memory accesses pairs.
507-
/// During LoopVersioningLICM legality we ignore loops having must
508-
/// aliasing memory accesses.
509-
void LoopVersioningLICM::setNoAliasToLoop(Loop *VerLoop) {
510-
// Get latch terminator instruction.
511-
Instruction *I = VerLoop->getLoopLatch()->getTerminator();
512-
// Create alias scope domain.
513-
MDBuilder MDB(I->getContext());
514-
MDNode *NewDomain = MDB.createAnonymousAliasScopeDomain("LVDomain");
515-
StringRef Name = "LVAliasScope";
516-
MDNode *NewScope = MDB.createAnonymousAliasScope(NewDomain, Name);
517-
SmallVector<Metadata *, 4> Scopes{NewScope}, NoAliases{NewScope};
518-
// Iterate over each instruction of loop.
519-
// set no-alias for all load & store instructions.
520-
for (auto *Block : CurLoop->getBlocks()) {
521-
for (auto &Inst : *Block) {
522-
// Only interested in instruction that may modify or read memory.
523-
if (!Inst.mayReadFromMemory() && !Inst.mayWriteToMemory())
524-
continue;
525-
// Set no-alias for current instruction.
526-
Inst.setMetadata(
527-
LLVMContext::MD_noalias,
528-
MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_noalias),
529-
MDNode::get(Inst.getContext(), NoAliases)));
530-
// set alias-scope for current instruction.
531-
Inst.setMetadata(
532-
LLVMContext::MD_alias_scope,
533-
MDNode::concatenate(Inst.getMetadata(LLVMContext::MD_alias_scope),
534-
MDNode::get(Inst.getContext(), Scopes)));
535-
}
536-
}
537-
}
538-
539510
bool LoopVersioningLICM::run(DominatorTree *DT) {
540511
// Do not do the transformation if disabled by metadata.
541512
if (hasLICMVersioningTransformation(CurLoop) & TM_Disable)
@@ -563,7 +534,7 @@ bool LoopVersioningLICM::run(DominatorTree *DT) {
563534
addStringMetadataToLoop(LVer.getVersionedLoop(),
564535
"llvm.mem.parallel_loop_access");
565536
// Update version loop with aggressive aliasing assumption.
566-
setNoAliasToLoop(LVer.getVersionedLoop());
537+
LVer.annotateLoopWithNoAlias();
567538
Changed = true;
568539
}
569540
return Changed;

0 commit comments

Comments
 (0)