From d337d11809673b4925187c84f2770639b599e36d Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 19 Nov 2024 14:31:16 +0100 Subject: [PATCH] [BasicAA][LAA] Don't use same-block phis in cross iteration mode In 4de3184f07fd8c548125d315dd306d4afa7c9698 we exposed BasicAA's cross-iteration mode for use in LAA, so we can handle selects with equal conditions correctly (where the select condition is not actually equal across iterations). However, if we replace the selects with equivalent phis, the issue still exists. In the phi case, we effectively still have an assumption that the condition(s) that control which phi arg is used will be the same across iterations. Fix this by disabling this case in cross-iteration mode. --- llvm/lib/Analysis/BasicAliasAnalysis.cpp | 5 +++-- llvm/test/Analysis/LoopAccessAnalysis/select-dependence.ll | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp index 1dcdad01f4c80..08bb11bb07ac5 100644 --- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp +++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp @@ -1447,9 +1447,10 @@ AliasResult BasicAAResult::aliasPHI(const PHINode *PN, LocationSize PNSize, return AliasResult::NoAlias; // If the values are PHIs in the same block, we can do a more precise // as well as efficient check: just check for aliases between the values - // on corresponding edges. + // on corresponding edges. Don't do this if we are analyzing across + // iterations, as we may pick a different phi entry in different iterations. if (const PHINode *PN2 = dyn_cast(V2)) - if (PN2->getParent() == PN->getParent()) { + if (PN2->getParent() == PN->getParent() && !AAQI.MayBeCrossIteration) { std::optional Alias; for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { AliasResult ThisAlias = AAQI.AAR.alias( diff --git a/llvm/test/Analysis/LoopAccessAnalysis/select-dependence.ll b/llvm/test/Analysis/LoopAccessAnalysis/select-dependence.ll index 7d8a25f022e7d..5b01efd821c47 100644 --- a/llvm/test/Analysis/LoopAccessAnalysis/select-dependence.ll +++ b/llvm/test/Analysis/LoopAccessAnalysis/select-dependence.ll @@ -44,8 +44,13 @@ exit: define void @test_phi(ptr noalias %x, ptr noalias %y, ptr noalias %z) { ; CHECK-LABEL: 'test_phi' ; CHECK-NEXT: loop: -; CHECK-NEXT: Memory dependences are safe +; CHECK-NEXT: Report: unsafe dependent memory operations in loop. Use #pragma clang loop distribute(enable) to allow loop distribution to attempt to isolate the offending operations into a separate loop +; CHECK-NEXT: Unsafe indirect dependence. ; CHECK-NEXT: Dependences: +; CHECK-NEXT: IndirectUnsafe: +; CHECK-NEXT: %load = load double, ptr %gep.sel, align 8 -> +; CHECK-NEXT: store double %load, ptr %gep.sel2, align 8 +; CHECK-EMPTY: ; CHECK-NEXT: Run-time memory checks: ; CHECK-NEXT: Grouped accesses: ; CHECK-EMPTY: