-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[DA] Check monotonicity for subscripts #154527
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
Open
kasuga-fj
wants to merge
10
commits into
llvm:main
Choose a base branch
from
kasuga-fj:da-monotonic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a0d05ec
[DA] Check monotonicity for subscripts
kasuga-fj 75639b9
Address review comments
kasuga-fj 5063efc
Fix format
kasuga-fj 4dad091
Merge branch 'main' into feature/da-monotonic
kasuga-fj 8986cd4
Revise impl and add test
kasuga-fj dcb3b19
remove minmax calculator
kasuga-fj e93be2e
Merge branch 'main' into feature/da-monotonic
kasuga-fj 72b2902
Introduce NoSignedWrap to MonotonicityType
kasuga-fj b5ca793
fix gep-based inferrence
kasuga-fj 65ccc61
Merge branch 'main' into feature/da-monotonic
kasuga-fj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3500,16 +3500,28 @@ bool DependenceInfo::tryDelinearizeParametricSize( | |
// to the dependency checks. | ||
if (!DisableDelinearizationChecks) | ||
for (size_t I = 1; I < Size; ++I) { | ||
if (!isKnownNonNegative(SrcSubscripts[I], SrcPtr)) | ||
const Loop *OutermostLoop = | ||
LI->getLoopFor(Src->getParent())->getOutermostLoop(); | ||
IntegerType *Ty = cast<IntegerType>(Sizes[I - 1]->getType()); | ||
if (!Ty) | ||
return false; | ||
|
||
if (!isKnownLessThan(SrcSubscripts[I], Sizes[I - 1])) | ||
const SCEV *SrcMin = nullptr, *SrcMax = nullptr; | ||
if (!isMonotonicSCEV(SrcSubscripts[I], SrcMin, SrcMax, SE, OutermostLoop, | ||
Ty, SrcPtr)) | ||
return false; | ||
|
||
if (!isKnownNonNegative(DstSubscripts[I], DstPtr)) | ||
if (!SE->isKnownNonNegative(SrcMin)) | ||
return false; | ||
if (!SE->isKnownPredicate(CmpInst::ICMP_SLT, SrcMax, Sizes[I - 1])) | ||
return false; | ||
|
||
if (!isKnownLessThan(DstSubscripts[I], Sizes[I - 1])) | ||
const SCEV *DstMin = nullptr, *DstMax = nullptr; | ||
if (!isMonotonicSCEV(DstSubscripts[I], DstMin, DstMax, SE, OutermostLoop, | ||
Ty, DstPtr)) | ||
return false; | ||
if (!SE->isKnownNonNegative(DstMin)) | ||
return false; | ||
if (!SE->isKnownPredicate(CmpInst::ICMP_SLT, DstMax, Sizes[I - 1])) | ||
return false; | ||
} | ||
|
||
|
@@ -3548,6 +3560,102 @@ SCEVUnionPredicate DependenceInfo::getRuntimeAssumptions() const { | |
return SCEVUnionPredicate(Assumptions, *SE); | ||
} | ||
|
||
bool DependenceInfo::isMonotonicSCEV(const SCEV *Expr, const SCEV *&Min, | ||
const SCEV *&Max, ScalarEvolution *SE, | ||
const Loop *OutermostLoop, IntegerType *Ty, | ||
const Value *Ptr) const { | ||
const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr); | ||
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. Consider using the 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. Thanks, it completely slipped my mind. |
||
if (!AddRec) { | ||
if (!SE->isLoopInvariant(Expr, OutermostLoop)) | ||
return false; | ||
const SCEV *Init = Expr ? Expr : SE->getZero(Ty); | ||
Min = Init; | ||
Max = Init; | ||
return true; | ||
} | ||
|
||
if (!AddRec->isAffine()) | ||
return false; | ||
|
||
// TODO: Support cast? | ||
auto *AddRecTy = dyn_cast<IntegerType>(AddRec->getType()); | ||
if (!AddRecTy || AddRecTy->getBitWidth() != Ty->getBitWidth()) | ||
return false; | ||
|
||
if (!isMonotonicSCEV(AddRec->getStart(), Min, Max, SE, OutermostLoop, Ty)) | ||
return false; | ||
|
||
const SCEV *Coeff = AddRec->getStepRecurrence(*SE); | ||
const Loop *L = AddRec->getLoop(); | ||
|
||
// Bail out if the coefficient can be zero. | ||
if (!SE->isKnownNonZero(Coeff)) | ||
return false; | ||
if (!SE->isLoopInvariant(Coeff, OutermostLoop)) | ||
return false; | ||
|
||
bool IsKnownCoeffPositive = SE->isKnownPositive(Coeff); | ||
bool IsKnownCoeffNegative = SE->isKnownNegative(Coeff); | ||
|
||
bool IsNoWrap = AddRec->hasNoUnsignedWrap(); | ||
if (Ptr) { | ||
// TODO: This seems incorrect. Maybe we should check the reachability from | ||
// the GEP to the target instruction. | ||
auto *GEP = dyn_cast<GetElementPtrInst>(Ptr); | ||
if (GEP && GEP->hasNoUnsignedSignedWrap()) | ||
IsNoWrap = true; | ||
} | ||
|
||
if (!IsNoWrap) { | ||
if (!IsKnownCoeffNegative) | ||
// If the coefficient can be positive value, ensure that the AddRec is | ||
// monotonically increasing. | ||
if (!SE->isKnownOnEveryIteration(ICmpInst::ICMP_SGE, AddRec, | ||
AddRec->getStart())) | ||
return false; | ||
|
||
if (!IsKnownCoeffPositive) | ||
// If the coefficient can be positive value, ensure that the AddRec is | ||
// monotonically decreasing. | ||
if (!SE->isKnownOnEveryIteration(ICmpInst::ICMP_SLE, AddRec, | ||
AddRec->getStart())) | ||
return false; | ||
} | ||
|
||
const SCEV *BTC = SE->getBackedgeTakenCount(L); | ||
if (!BTC || !SE->isLoopInvariant(BTC, OutermostLoop)) | ||
return false; | ||
|
||
const SCEVAddRecExpr *MinAddRec = dyn_cast<SCEVAddRecExpr>( | ||
SE->getAddRecExpr(Min, Coeff, L, AddRec->getNoWrapFlags())); | ||
if (!MinAddRec) | ||
return false; | ||
|
||
const SCEVAddRecExpr *MaxAddRec = dyn_cast<SCEVAddRecExpr>( | ||
SE->getAddRecExpr(Max, Coeff, L, AddRec->getNoWrapFlags())); | ||
if (!MaxAddRec) | ||
return false; | ||
|
||
// If we know the coefficient is positive, the maximum value of AddRec is | ||
// MaxLast. Similarly, if we know the coefficient is negative, the minimum | ||
// value of AddRec is MinLast. If we don't know the sign of the coefficient, | ||
// use SMin/SMax. | ||
const SCEV *MinLast = MinAddRec->evaluateAtIteration(BTC, *SE); | ||
const SCEV *MaxLast = MaxAddRec->evaluateAtIteration(BTC, *SE); | ||
if (IsKnownCoeffPositive) { | ||
Max = MaxLast; | ||
} else if (IsKnownCoeffNegative) { | ||
Min = MinLast; | ||
} else { | ||
assert(!IsKnownCoeffPositive && !IsKnownCoeffNegative && | ||
"Unexpected coefficient sign"); | ||
Min = SE->getSMinExpr(Min, MinLast); | ||
Max = SE->getSMaxExpr(Max, MaxLast); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// depends - | ||
// Returns NULL if there is no dependence. | ||
// Otherwise, return a Dependence with as many details as possible. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document parameters?
Consider adding "signed" to the name. Currently DA assumes everything is a signed SCEV, but maybe we change that one day.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separate the implementation, and added "signed" to the name.