[llvm] [BasicAA][LAA] Don't use same-block phis in cross iteration mode (PR #116802)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 19 05:47:23 PST 2024
https://github.com/nikic created https://github.com/llvm/llvm-project/pull/116802
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 phi handling in cross-iteration mode.
(I'm not entirely sure whether this is also needed when BasicAA enables cross-iteration mode during internal phi recursion, but I wouldn't be surprised if that's the case.)
>From d337d11809673b4925187c84f2770639b599e36d Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
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 1dcdad01f4c809..08bb11bb07ac54 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<PHINode>(V2))
- if (PN2->getParent() == PN->getParent()) {
+ if (PN2->getParent() == PN->getParent() && !AAQI.MayBeCrossIteration) {
std::optional<AliasResult> 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 7d8a25f022e7d0..5b01efd821c474 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:
More information about the llvm-commits
mailing list