-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[DA] handle memory accesses with different offsets and strides #123436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 20 commits
238bbca
164a0fa
a479bdf
9213c32
caf4f8d
468652d
108d224
c62e1f4
8dcc5a0
92f6b4f
0eae7f0
60ba4e6
8c69ebf
a80c878
bf9fcfd
3dbba8e
d9846e9
b954282
9a110df
297ff44
861ef01
4972776
338668f
869b5ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -187,7 +187,7 @@ static void dumpExampleDependence(raw_ostream &OS, DependenceInfo *DA, | |
| if (DstI->mayReadOrWriteMemory()) { | ||
| OS << "Src:" << *SrcI << " --> Dst:" << *DstI << "\n"; | ||
| OS << " da analyze - "; | ||
| if (auto D = DA->depends(&*SrcI, &*DstI)) { | ||
| if (auto D = DA->depends(&*SrcI, &*DstI, true)) { | ||
sebpop marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Normalize negative direction vectors if required by clients. | ||
| if (NormalizeResults && D->normalize(&SE)) | ||
| OS << "normalized - "; | ||
|
|
@@ -199,13 +199,17 @@ static void dumpExampleDependence(raw_ostream &OS, DependenceInfo *DA, | |
| OS << "!\n"; | ||
| } | ||
| } | ||
| } | ||
| else | ||
| } else | ||
| OS << "none!\n"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| SCEVUnionPredicate Assumptions = DA->getRuntimeAssumptions(); | ||
| if (!Assumptions.isAlwaysTrue()) { | ||
| OS << "Runtime Assumptions:\n"; | ||
| Assumptions.print(OS, 0); | ||
| } | ||
| } | ||
|
|
||
| void DependenceAnalysisWrapperPass::print(raw_ostream &OS, | ||
|
|
@@ -264,9 +268,10 @@ bool Dependence::isScalar(unsigned level) const { | |
| // FullDependence methods | ||
|
|
||
| FullDependence::FullDependence(Instruction *Source, Instruction *Destination, | ||
| const SCEVUnionPredicate &Assumes, | ||
| bool PossiblyLoopIndependent, | ||
| unsigned CommonLevels) | ||
| : Dependence(Source, Destination), Levels(CommonLevels), | ||
| : Dependence(Source, Destination, Assumes), Levels(CommonLevels), | ||
| LoopIndependent(PossiblyLoopIndependent) { | ||
| Consistent = true; | ||
| if (CommonLevels) | ||
|
|
@@ -706,6 +711,12 @@ void Dependence::dump(raw_ostream &OS) const { | |
| OS << " splitable"; | ||
| } | ||
| OS << "!\n"; | ||
|
|
||
| SCEVUnionPredicate Assumptions = getRuntimeAssumptions(); | ||
| if (!Assumptions.isAlwaysTrue()) { | ||
| OS << " Runtime Assumptions:\n"; | ||
| Assumptions.print(OS, 2); | ||
| } | ||
| } | ||
|
|
||
| // Returns NoAlias/MayAliass/MustAlias for two memory locations based upon their | ||
|
|
@@ -3569,6 +3580,10 @@ bool DependenceInfo::invalidate(Function &F, const PreservedAnalyses &PA, | |
| Inv.invalidate<LoopAnalysis>(F, PA); | ||
| } | ||
|
|
||
| SCEVUnionPredicate DependenceInfo::getRuntimeAssumptions() const { | ||
| return SCEVUnionPredicate(Assumptions, *SE); | ||
| } | ||
|
|
||
| // depends - | ||
| // Returns NULL if there is no dependence. | ||
| // Otherwise, return a Dependence with as many details as possible. | ||
|
|
@@ -3581,7 +3596,9 @@ bool DependenceInfo::invalidate(Function &F, const PreservedAnalyses &PA, | |
| // Care is required to keep the routine below, getSplitIteration(), | ||
| // up to date with respect to this routine. | ||
| std::unique_ptr<Dependence> | ||
| DependenceInfo::depends(Instruction *Src, Instruction *Dst) { | ||
| DependenceInfo::depends(Instruction *Src, Instruction *Dst, | ||
| bool UnderRuntimeAssumptions) { | ||
sebpop marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| SmallVector<const SCEVPredicate *, 4> Assume; | ||
| bool PossiblyLoopIndependent = true; | ||
| if (Src == Dst) | ||
| PossiblyLoopIndependent = false; | ||
|
|
@@ -3593,22 +3610,20 @@ DependenceInfo::depends(Instruction *Src, Instruction *Dst) { | |
| if (!isLoadOrStore(Src) || !isLoadOrStore(Dst)) { | ||
| // can only analyze simple loads and stores, i.e., no calls, invokes, etc. | ||
| LLVM_DEBUG(dbgs() << "can only handle simple loads and stores\n"); | ||
| return std::make_unique<Dependence>(Src, Dst); | ||
| return std::make_unique<Dependence>(Src, Dst, | ||
| SCEVUnionPredicate(Assume, *SE)); | ||
| } | ||
|
|
||
| assert(isLoadOrStore(Src) && "instruction is not load or store"); | ||
| assert(isLoadOrStore(Dst) && "instruction is not load or store"); | ||
| Value *SrcPtr = getLoadStorePointerOperand(Src); | ||
| Value *DstPtr = getLoadStorePointerOperand(Dst); | ||
| const MemoryLocation &DstLoc = MemoryLocation::get(Dst); | ||
sebpop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const MemoryLocation &SrcLoc = MemoryLocation::get(Src); | ||
|
|
||
| switch (underlyingObjectsAlias(AA, F->getDataLayout(), | ||
| MemoryLocation::get(Dst), | ||
| MemoryLocation::get(Src))) { | ||
| switch (underlyingObjectsAlias(AA, F->getDataLayout(), DstLoc, SrcLoc)) { | ||
| case AliasResult::MayAlias: | ||
| case AliasResult::PartialAlias: | ||
| // cannot analyse objects if we don't understand their aliasing. | ||
| LLVM_DEBUG(dbgs() << "can't analyze may or partial alias\n"); | ||
| return std::make_unique<Dependence>(Src, Dst); | ||
| return std::make_unique<Dependence>(Src, Dst, | ||
| SCEVUnionPredicate(Assume, *SE)); | ||
| case AliasResult::NoAlias: | ||
| // If the objects noalias, they are distinct, accesses are independent. | ||
| LLVM_DEBUG(dbgs() << "no alias\n"); | ||
|
|
@@ -3617,30 +3632,80 @@ DependenceInfo::depends(Instruction *Src, Instruction *Dst) { | |
| break; // The underlying objects alias; test accesses for dependence. | ||
| } | ||
|
|
||
| // establish loop nesting levels | ||
| establishNestingLevels(Src, Dst); | ||
| LLVM_DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n"); | ||
| LLVM_DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n"); | ||
|
|
||
| FullDependence Result(Src, Dst, PossiblyLoopIndependent, CommonLevels); | ||
| ++TotalArrayPairs; | ||
| if (DstLoc.Size != SrcLoc.Size) { | ||
Meinersbur marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
sebpop marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // The dependence test gets confused if the size of the memory accesses | ||
| // differ. | ||
| LLVM_DEBUG(dbgs() << "can't analyze must alias with different sizes\n"); | ||
| return std::make_unique<Dependence>(Src, Dst, | ||
| SCEVUnionPredicate(Assume, *SE)); | ||
| } | ||
|
|
||
| unsigned Pairs = 1; | ||
| SmallVector<Subscript, 2> Pair(Pairs); | ||
| Value *SrcPtr = getLoadStorePointerOperand(Src); | ||
| Value *DstPtr = getLoadStorePointerOperand(Dst); | ||
| const SCEV *SrcSCEV = SE->getSCEV(SrcPtr); | ||
| const SCEV *DstSCEV = SE->getSCEV(DstPtr); | ||
| LLVM_DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n"); | ||
| LLVM_DEBUG(dbgs() << " DstSCEV = " << *DstSCEV << "\n"); | ||
| if (SE->getPointerBase(SrcSCEV) != SE->getPointerBase(DstSCEV)) { | ||
| const SCEV *SrcBase = SE->getPointerBase(SrcSCEV); | ||
| const SCEV *DstBase = SE->getPointerBase(DstSCEV); | ||
| if (SrcBase != DstBase) { | ||
| // If two pointers have different bases, trying to analyze indexes won't | ||
| // work; we can't compare them to each other. This can happen, for example, | ||
| // if one is produced by an LCSSA PHI node. | ||
| // | ||
| // We check this upfront so we don't crash in cases where getMinusSCEV() | ||
| // returns a SCEVCouldNotCompute. | ||
| LLVM_DEBUG(dbgs() << "can't analyze SCEV with different pointer base\n"); | ||
| return std::make_unique<Dependence>(Src, Dst); | ||
| return std::make_unique<Dependence>(Src, Dst, | ||
| SCEVUnionPredicate(Assume, *SE)); | ||
| } | ||
|
|
||
| uint64_t EltSize = SrcLoc.Size.toRaw(); | ||
| assert(EltSize == DstLoc.Size.toRaw() && "Array element size differ"); | ||
sebpop marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const SCEV *SrcEv = SE->getMinusSCEV(SrcSCEV, SrcBase); | ||
| const SCEV *DstEv = SE->getMinusSCEV(DstSCEV, DstBase); | ||
|
|
||
| if (Src != Dst) { | ||
| // Check that memory access offsets are multiples of element sizes. | ||
| if (!SE->isKnownMultipleOf(SrcEv, EltSize, Assume) || | ||
| !SE->isKnownMultipleOf(DstEv, EltSize, Assume)) { | ||
| LLVM_DEBUG(dbgs() << "can't analyze SCEV with different offsets\n"); | ||
| return std::make_unique<Dependence>(Src, Dst, | ||
| SCEVUnionPredicate(Assume, *SE)); | ||
| } | ||
| } | ||
|
|
||
| if (!Assume.empty()) { | ||
| if (!UnderRuntimeAssumptions) | ||
| return std::make_unique<Dependence>(Src, Dst, | ||
| SCEVUnionPredicate(Assume, *SE)); | ||
| if (Assumptions.empty()) { | ||
| Assumptions.append(Assume.begin(), Assume.end()); | ||
sebpop marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| // Add non-redundant assumptions. | ||
|
||
| unsigned N = Assumptions.size(); | ||
| for (const SCEVPredicate *P : Assume) { | ||
| bool Implied = false; | ||
| for (unsigned I = 0; I != N && !Implied; I++) | ||
| if (Assumptions[I]->implies(P, *SE)) | ||
| Implied = true; | ||
| if (!Implied) | ||
| Assumptions.push_back(P); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| establishNestingLevels(Src, Dst); | ||
| LLVM_DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n"); | ||
| LLVM_DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n"); | ||
|
|
||
| FullDependence Result(Src, Dst, SCEVUnionPredicate(Assume, *SE), | ||
| PossiblyLoopIndependent, CommonLevels); | ||
| ++TotalArrayPairs; | ||
|
|
||
| unsigned Pairs = 1; | ||
| SmallVector<Subscript, 2> Pair(Pairs); | ||
| Pair[0].Src = SrcSCEV; | ||
| Pair[0].Dst = DstSCEV; | ||
|
|
||
|
|
@@ -4034,7 +4099,7 @@ const SCEV *DependenceInfo::getSplitIteration(const Dependence &Dep, | |
| // establish loop nesting levels | ||
| establishNestingLevels(Src, Dst); | ||
|
|
||
| FullDependence Result(Src, Dst, false, CommonLevels); | ||
| FullDependence Result(Src, Dst, Dep.Assumptions, false, CommonLevels); | ||
|
|
||
| unsigned Pairs = 1; | ||
| SmallVector<Subscript, 2> Pair(Pairs); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10971,6 +10971,52 @@ bool ScalarEvolution::isKnownToBeAPowerOfTwo(const SCEV *S, bool OrZero, | |
| return all_of(Mul->operands(), NonRecursive) && (OrZero || isKnownNonZero(S)); | ||
| } | ||
|
|
||
| bool ScalarEvolution::isKnownMultipleOf( | ||
| const SCEV *S, uint64_t M, | ||
| SmallVectorImpl<const SCEVPredicate *> &Assumptions) { | ||
| if (M == 0) | ||
| return false; | ||
|
Comment on lines
+10977
to
+10978
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: What if
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This function can always return false, since it is returns true of if it is a known multiple. The case
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, this is exactly what I'm concerned about.
I see, that's make sense to me. |
||
| if (M == 1) | ||
| return true; | ||
|
|
||
| // Recursively check AddRec operands. | ||
sebpop marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(S)) | ||
| return isKnownMultipleOf(AddRec->getStart(), M, Assumptions) && | ||
| isKnownMultipleOf(AddRec->getStepRecurrence(*this), M, Assumptions); | ||
|
|
||
| // For a constant, check that "S % M == 0". | ||
| if (auto *Cst = dyn_cast<SCEVConstant>(S)) { | ||
| APInt C = Cst->getAPInt(); | ||
| return C.urem(M) == 0; | ||
| } | ||
|
|
||
| // Basic tests have failed. | ||
| // Check "S % M == 0" at compile time and record runtime Assumptions. | ||
| auto *STy = dyn_cast<IntegerType>(S->getType()); | ||
| const SCEV *SmodM = | ||
| getURemExpr(S, getConstant(ConstantInt::get(STy, M, false))); | ||
| const SCEV *Zero = getZero(STy); | ||
|
|
||
| // Check whether "S % M == 0" is known at compile time. | ||
| if (isKnownPredicate(ICmpInst::ICMP_EQ, SmodM, Zero)) | ||
| return true; | ||
|
|
||
| // Check whether "S % M != 0" is known at compile time. | ||
| if (isKnownPredicate(ICmpInst::ICMP_NE, SmodM, Zero)) | ||
| return false; | ||
|
|
||
| const SCEVPredicate *P = getComparePredicate(ICmpInst::ICMP_EQ, SmodM, Zero); | ||
|
|
||
| // Detect redundant predicates. | ||
| for (auto *A : Assumptions) | ||
| if (A->implies(P, *this)) | ||
sebpop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return true; | ||
|
|
||
| // Only record non-redundant predicates. | ||
| Assumptions.push_back(P); | ||
sebpop marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return true; | ||
| } | ||
|
|
||
| std::pair<const SCEV *, const SCEV *> | ||
| ScalarEvolution::SplitIntoInitAndPostInc(const Loop *L, const SCEV *S) { | ||
| // Compute SCEV on entry of loop L. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 5 | ||
| ; RUN: opt < %s -disable-output "-passes=print<da>" -aa-pipeline=basic-aa 2>&1 \ | ||
| ; RUN: | FileCheck %s | ||
|
|
||
| ; The dependence test does not handle array accesses of different sizes: i32 and i64. | ||
| ; Bug 16183 - https://github.com/llvm/llvm-project/issues/16183 | ||
|
|
||
| define i64 @bug16183_alias(ptr nocapture %A) { | ||
| ; CHECK-LABEL: 'bug16183_alias' | ||
| ; CHECK-NEXT: Src: store i32 2, ptr %arrayidx, align 4 --> Dst: store i32 2, ptr %arrayidx, align 4 | ||
| ; CHECK-NEXT: da analyze - none! | ||
| ; CHECK-NEXT: Src: store i32 2, ptr %arrayidx, align 4 --> Dst: %0 = load i64, ptr %A, align 8 | ||
| ; CHECK-NEXT: da analyze - confused! | ||
| ; CHECK-NEXT: Src: %0 = load i64, ptr %A, align 8 --> Dst: %0 = load i64, ptr %A, align 8 | ||
| ; CHECK-NEXT: da analyze - none! | ||
| ; | ||
| entry: | ||
| %arrayidx = getelementptr inbounds i32, ptr %A, i64 1 | ||
| store i32 2, ptr %arrayidx, align 4 | ||
| %0 = load i64, ptr %A, align 8 | ||
| ret i64 %0 | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.