Skip to content

Commit ee9862d

Browse files
committed
[DA] Dependence analysis does not handle array accesses of different sizes
This fixes bug #16183 where the elements of a same array are accesses as i32 and i64. This is not handled by the array dependence analysis.
1 parent f7ba5a4 commit ee9862d

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

llvm/lib/Analysis/DependenceAnalysis.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3602,14 +3602,10 @@ DependenceInfo::depends(Instruction *Src, Instruction *Dst,
36023602
return std::make_unique<Dependence>(Src, Dst);
36033603
}
36043604

3605-
assert(isLoadOrStore(Src) && "instruction is not load or store");
3606-
assert(isLoadOrStore(Dst) && "instruction is not load or store");
3607-
Value *SrcPtr = getLoadStorePointerOperand(Src);
3608-
Value *DstPtr = getLoadStorePointerOperand(Dst);
3605+
const MemoryLocation &DstLoc = MemoryLocation::get(Dst);
3606+
const MemoryLocation &SrcLoc = MemoryLocation::get(Src);
36093607

3610-
switch (underlyingObjectsAlias(AA, F->getDataLayout(),
3611-
MemoryLocation::get(Dst),
3612-
MemoryLocation::get(Src))) {
3608+
switch (underlyingObjectsAlias(AA, F->getDataLayout(), DstLoc, SrcLoc)) {
36133609
case AliasResult::MayAlias:
36143610
case AliasResult::PartialAlias:
36153611
// cannot analyse objects if we don't understand their aliasing.
@@ -3623,16 +3619,15 @@ DependenceInfo::depends(Instruction *Src, Instruction *Dst,
36233619
break; // The underlying objects alias; test accesses for dependence.
36243620
}
36253621

3626-
// establish loop nesting levels
3627-
establishNestingLevels(Src, Dst);
3628-
LLVM_DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n");
3629-
LLVM_DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n");
3630-
3631-
FullDependence Result(Src, Dst, PossiblyLoopIndependent, CommonLevels);
3632-
++TotalArrayPairs;
3622+
if (DstLoc.Size != SrcLoc.Size) {
3623+
// The dependence test gets confused if the size of the memory accesses
3624+
// differ.
3625+
LLVM_DEBUG(dbgs() << "can't analyze must alias with different sizes\n");
3626+
return std::make_unique<Dependence>(Src, Dst);
3627+
}
36333628

3634-
unsigned Pairs = 1;
3635-
SmallVector<Subscript, 2> Pair(Pairs);
3629+
Value *SrcPtr = getLoadStorePointerOperand(Src);
3630+
Value *DstPtr = getLoadStorePointerOperand(Dst);
36363631
const SCEV *SrcSCEV = SE->getSCEV(SrcPtr);
36373632
const SCEV *DstSCEV = SE->getSCEV(DstPtr);
36383633
LLVM_DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n");
@@ -3647,6 +3642,17 @@ DependenceInfo::depends(Instruction *Src, Instruction *Dst,
36473642
LLVM_DEBUG(dbgs() << "can't analyze SCEV with different pointer base\n");
36483643
return std::make_unique<Dependence>(Src, Dst);
36493644
}
3645+
3646+
// establish loop nesting levels
3647+
establishNestingLevels(Src, Dst);
3648+
LLVM_DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n");
3649+
LLVM_DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n");
3650+
3651+
FullDependence Result(Src, Dst, PossiblyLoopIndependent, CommonLevels);
3652+
++TotalArrayPairs;
3653+
3654+
unsigned Pairs = 1;
3655+
SmallVector<Subscript, 2> Pair(Pairs);
36503656
Pair[0].Src = SrcSCEV;
36513657
Pair[0].Dst = DstSCEV;
36523658

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt < %s -disable-output "-passes=print<da>" -aa-pipeline=basic-aa 2>&1 \
3+
; RUN: | FileCheck %s
4+
5+
; The dependence test does not handle array accesses of different sizes: i32 and i64.
6+
; Bug 16183 - https://github.com/llvm/llvm-project/issues/16183
7+
8+
define i64 @bug16183_alias(ptr nocapture %A) {
9+
; CHECK-LABEL: 'bug16183_alias'
10+
; CHECK-NEXT: Src: store i32 2, ptr %arrayidx, align 4 --> Dst: store i32 2, ptr %arrayidx, align 4
11+
; CHECK-NEXT: da analyze - none!
12+
; CHECK-NEXT: Src: store i32 2, ptr %arrayidx, align 4 --> Dst: %0 = load i64, ptr %A, align 8
13+
; CHECK-NEXT: da analyze - confused!
14+
; CHECK-NEXT: Src: %0 = load i64, ptr %A, align 8 --> Dst: %0 = load i64, ptr %A, align 8
15+
; CHECK-NEXT: da analyze - none!
16+
;
17+
entry:
18+
%arrayidx = getelementptr inbounds i32, ptr %A, i64 1
19+
store i32 2, ptr %arrayidx, align 4
20+
%0 = load i64, ptr %A, align 8
21+
ret i64 %0
22+
}

0 commit comments

Comments
 (0)