[llvm] [DA] disambiguate evolution of base addresses (PR #116628)
Sebastian Pop via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 12 13:45:08 PST 2024
https://github.com/sebpop updated https://github.com/llvm/llvm-project/pull/116628
>From 555628f263782eea7bab7095c283939ec2377291 Mon Sep 17 00:00:00 2001
From: Sebastian Pop <spop at nvidia.com>
Date: Thu, 31 Oct 2024 18:47:55 +0000
Subject: [PATCH] [DA] disambiguate evolution of base addresses
This patch fixes two bugs:
https://github.com/llvm/llvm-project/issues/41488
https://github.com/llvm/llvm-project/issues/53942
The dependence analysis assumes that the base address of array accesses is
invariant across loop iterations. In both bugs the base address evolves
following loop iterations: the base address flip-flops between two different
memory objects.
Based on the scalar evolution of base addresses, the patch adds code to separate
the 3 alias cases {must, no, may}-alias where the base addresses are identical
at every iteration, never the same, and unknown.
---
llvm/include/llvm/Analysis/AliasAnalysis.h | 3 +
.../llvm/Analysis/DependenceAnalysis.h | 8 +-
llvm/lib/Analysis/DependenceAnalysis.cpp | 8 +-
.../DependenceAnalysis/FlipFlopBaseAddress.ll | 136 ++++++++++++++++++
4 files changed, 148 insertions(+), 7 deletions(-)
create mode 100644 llvm/test/Analysis/DependenceAnalysis/FlipFlopBaseAddress.ll
diff --git a/llvm/include/llvm/Analysis/AliasAnalysis.h b/llvm/include/llvm/Analysis/AliasAnalysis.h
index acc580f92b40a3..abf09f05cf47a0 100644
--- a/llvm/include/llvm/Analysis/AliasAnalysis.h
+++ b/llvm/include/llvm/Analysis/AliasAnalysis.h
@@ -660,6 +660,9 @@ class BatchAAResults {
MemoryEffects getMemoryEffects(const CallBase *Call) {
return AA.getMemoryEffects(Call, AAQI);
}
+ bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
+ return alias(LocA, LocB) == AliasResult::NoAlias;
+ }
bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
return alias(LocA, LocB) == AliasResult::MustAlias;
}
diff --git a/llvm/include/llvm/Analysis/DependenceAnalysis.h b/llvm/include/llvm/Analysis/DependenceAnalysis.h
index f0a09644e0f4b6..9f9b28e1e3a847 100644
--- a/llvm/include/llvm/Analysis/DependenceAnalysis.h
+++ b/llvm/include/llvm/Analysis/DependenceAnalysis.h
@@ -40,12 +40,12 @@
#define LLVM_ANALYSIS_DEPENDENCEANALYSIS_H
#include "llvm/ADT/SmallBitVector.h"
+#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Pass.h"
namespace llvm {
- class AAResults;
template <typename T> class ArrayRef;
class Loop;
class LoopInfo;
@@ -294,7 +294,9 @@ namespace llvm {
public:
DependenceInfo(Function *F, AAResults *AA, ScalarEvolution *SE,
LoopInfo *LI)
- : AA(AA), SE(SE), LI(LI), F(F) {}
+ : BAA(*AA), SE(SE), LI(LI), F(F) {
+ BAA.enableCrossIterationMode();
+ }
/// Handle transitive invalidation when the cached analysis results go away.
bool invalidate(Function &F, const PreservedAnalyses &PA,
@@ -355,7 +357,7 @@ namespace llvm {
Function *getFunction() const { return F; }
private:
- AAResults *AA;
+ BatchAAResults BAA;
ScalarEvolution *SE;
LoopInfo *LI;
Function *F;
diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index a4a98ea0bae146..03f3fafb850322 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -712,7 +712,7 @@ void Dependence::dump(raw_ostream &OS) const {
// tbaa, non-overlapping regions etc), then it is known there is no dependecy.
// Otherwise the underlying objects are checked to see if they point to
// different identifiable objects.
-static AliasResult underlyingObjectsAlias(AAResults *AA,
+static AliasResult underlyingObjectsAlias(BatchAAResults &BAA,
const DataLayout &DL,
const MemoryLocation &LocA,
const MemoryLocation &LocB) {
@@ -722,7 +722,7 @@ static AliasResult underlyingObjectsAlias(AAResults *AA,
MemoryLocation::getBeforeOrAfter(LocA.Ptr, LocA.AATags);
MemoryLocation LocBS =
MemoryLocation::getBeforeOrAfter(LocB.Ptr, LocB.AATags);
- if (AA->isNoAlias(LocAS, LocBS))
+ if (BAA.isNoAlias(LocAS, LocBS))
return AliasResult::NoAlias;
// Check the underlying objects are the same
@@ -3606,7 +3606,7 @@ DependenceInfo::depends(Instruction *Src, Instruction *Dst,
Value *SrcPtr = getLoadStorePointerOperand(Src);
Value *DstPtr = getLoadStorePointerOperand(Dst);
- switch (underlyingObjectsAlias(AA, F->getDataLayout(),
+ switch (underlyingObjectsAlias(BAA, F->getDataLayout(),
MemoryLocation::get(Dst),
MemoryLocation::get(Src))) {
case AliasResult::MayAlias:
@@ -4033,7 +4033,7 @@ const SCEV *DependenceInfo::getSplitIteration(const Dependence &Dep,
Value *SrcPtr = getLoadStorePointerOperand(Src);
Value *DstPtr = getLoadStorePointerOperand(Dst);
assert(underlyingObjectsAlias(
- AA, F->getDataLayout(), MemoryLocation::get(Dst),
+ BAA, F->getDataLayout(), MemoryLocation::get(Dst),
MemoryLocation::get(Src)) == AliasResult::MustAlias);
// establish loop nesting levels
diff --git a/llvm/test/Analysis/DependenceAnalysis/FlipFlopBaseAddress.ll b/llvm/test/Analysis/DependenceAnalysis/FlipFlopBaseAddress.ll
new file mode 100644
index 00000000000000..2d80b2493a389b
--- /dev/null
+++ b/llvm/test/Analysis/DependenceAnalysis/FlipFlopBaseAddress.ll
@@ -0,0 +1,136 @@
+; RUN: opt < %s -disable-output "-passes=print<da>" -aa-pipeline=basic-aa 2>&1 \
+; RUN: | FileCheck %s
+
+; Check that dependence analysis correctly handles flip-flop of base addresses.
+; Bug 41488 - https://github.com/llvm/llvm-project/issues/41488
+
+; CHECK-LABEL: bug41488_test1
+; CHECK-NOT: da analyze - none!
+
+define float @bug41488_test1() {
+entry:
+ %g = alloca float, align 4
+ %h = alloca float, align 4
+ br label %for.body
+
+for.body:
+ %p = phi float* [ %g, %entry ], [ %q, %for.body ]
+ %q = phi float* [ %h, %entry ], [ %p, %for.body ]
+ %0 = load float, float* %p, align 4
+ store float undef, float* %q, align 4
+ %branch_cond = fcmp ugt float %0, 0.0
+ br i1 %branch_cond, label %for.cond.cleanup, label %for.body
+
+for.cond.cleanup:
+ ret float undef
+}
+
+; CHECK-LABEL: bug41488_test2
+; CHECK-NOT: da analyze - none!
+
+define void @bug41488_test2(i32 %n) {
+entry:
+ %g = alloca float, align 4
+ %h = alloca float, align 4
+ br label %for.body
+
+for.body:
+ %i = phi i32 [0, %entry ], [ %inc, %for.body ]
+ %p = phi float* [ %g, %entry ], [ %q, %for.body ]
+ %q = phi float* [ %h, %entry ], [ %p, %for.body ]
+ %0 = load float, float* %p, align 4
+ store float 0.0, float* %q, align 4
+ %inc = add nuw i32 %i, 1
+ %branch_cond = icmp ult i32 %i, %n
+ br i1 %branch_cond, label %for.body, label %for.cond.cleanup
+
+for.cond.cleanup:
+ ret void
+}
+
+; Bug 53942 - https://github.com/llvm/llvm-project/issues/53942
+; CHECK-LABEL: bug53942_foo
+; CHECK-NOT: da analyze - none!
+
+define void @bug53942_foo(i32 noundef %n, ptr noalias nocapture noundef writeonly %A, ptr noalias nocapture noundef %B) {
+entry:
+ %cmp8 = icmp sgt i32 %n, 1
+ br i1 %cmp8, label %for.body.preheader, label %for.cond.cleanup
+
+for.body.preheader: ; preds = %entry
+ %wide.trip.count = zext nneg i32 %n to i64
+ br label %for.body
+
+for.cond.cleanup: ; preds = %for.body, %entry
+ ret void
+
+for.body: ; preds = %for.body.preheader, %for.body
+ %indvars.iv = phi i64 [ 1, %for.body.preheader ], [ %indvars.iv.next, %for.body ]
+ %ptr1.011 = phi ptr [ %A, %for.body.preheader ], [ %ptr2.09, %for.body ]
+ %ptr2.09 = phi ptr [ %B, %for.body.preheader ], [ %ptr1.011, %for.body ]
+ %.pre = load double, ptr %B, align 8
+ %arrayidx2 = getelementptr inbounds double, ptr %ptr1.011, i64 %indvars.iv
+ store double %.pre, ptr %arrayidx2, align 8
+ %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
+ %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
+ br i1 %exitcond.not, label %for.cond.cleanup, label %for.body
+}
+
+
+; Bug 53942 - https://github.com/llvm/llvm-project/issues/53942
+; CHECK-LABEL: bug53942_bar
+; CHECK-NOT: da analyze - none!
+
+define void @bug53942_bar(i32 noundef %n, ptr noalias noundef %A, ptr noalias noundef %B) {
+entry:
+ br label %for.cond
+
+for.cond: ; preds = %for.inc, %entry
+ %i.0 = phi i32 [ 1, %entry ], [ %inc, %for.inc ]
+ %cmp = icmp slt i32 %i.0, %n
+ br i1 %cmp, label %for.body, label %for.cond.cleanup
+
+for.cond.cleanup: ; preds = %for.cond
+ br label %for.end
+
+for.body: ; preds = %for.cond
+ %and = and i32 %i.0, 2
+ %tobool.not = icmp eq i32 %and, 0
+ br i1 %tobool.not, label %cond.false, label %cond.true
+
+cond.true: ; preds = %for.body
+ br label %cond.end
+
+cond.false: ; preds = %for.body
+ br label %cond.end
+
+cond.end: ; preds = %cond.false, %cond.true
+ %cond = phi ptr [ %A, %cond.true ], [ %B, %cond.false ]
+ %and1 = and i32 %i.0, 2
+ %tobool2.not = icmp eq i32 %and1, 0
+ br i1 %tobool2.not, label %cond.false4, label %cond.true3
+
+cond.true3: ; preds = %cond.end
+ br label %cond.end5
+
+cond.false4: ; preds = %cond.end
+ br label %cond.end5
+
+cond.end5: ; preds = %cond.false4, %cond.true3
+ %cond6 = phi ptr [ %B, %cond.true3 ], [ %A, %cond.false4 ]
+ %sub = add nsw i32 %i.0, -1
+ %idxprom = sext i32 %sub to i64
+ %arrayidx = getelementptr inbounds double, ptr %cond6, i64 %idxprom
+ %0 = load double, ptr %arrayidx, align 8
+ %idxprom7 = zext nneg i32 %i.0 to i64
+ %arrayidx8 = getelementptr inbounds double, ptr %cond, i64 %idxprom7
+ store double %0, ptr %arrayidx8, align 8
+ br label %for.inc
+
+for.inc: ; preds = %cond.end5
+ %inc = add nuw nsw i32 %i.0, 1
+ br label %for.cond
+
+for.end: ; preds = %for.cond.cleanup
+ ret void
+}
More information about the llvm-commits
mailing list