-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[DA] Extract duplicated logic from exactSIVtest and exactRDIVtest (NFC) #152712
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1531,6 +1531,47 @@ static APInt ceilingOfQuotient(const APInt &A, const APInt &B) { | |
return Q; | ||
} | ||
|
||
/// Given an affine expression of the form A*k + B, where k is an arbitrary | ||
/// integer, infer the possible range of k based on the known range of the | ||
/// affine expression. If we know A*k + B is non-negative, i.e., | ||
/// | ||
/// A*k + B >= 0 | ||
/// | ||
/// we can derive the following inequalities for k when A is positive: | ||
/// | ||
/// k >= -B / A | ||
/// | ||
/// Since k is an integer, it means k is greater than or equal to the | ||
/// ceil(-B / A). Similar logic applies when A is negative, or the upper bound | ||
/// of the affine expression is passed via \p UB. | ||
/// | ||
/// Preconditions: \p A is non-zero, and we know A*k + B is non-negative. | ||
static std::pair<std::optional<APInt>, std::optional<APInt>> | ||
inferDomainOfAffine(const APInt &A, const APInt &B, | ||
const std::optional<APInt> &UB) { | ||
kasuga-fj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::optional<APInt> TL, TU; | ||
if (A.sgt(0)) { | ||
TL = ceilingOfQuotient(-B, A); | ||
LLVM_DEBUG(dbgs() << "\t Possible TL = " << *TL << "\n"); | ||
// New bound check - modification to Banerjee's e3 check | ||
if (UB) { | ||
// TODO?: Overflow check for UB - B | ||
TU = floorOfQuotient(*UB - B, A); | ||
LLVM_DEBUG(dbgs() << "\t Possible TU = " << *TU << "\n"); | ||
} | ||
} else { | ||
TU = floorOfQuotient(-B, A); | ||
LLVM_DEBUG(dbgs() << "\t Possible TU = " << *TU << "\n"); | ||
// New bound check - modification to Banerjee's e3 check | ||
if (UB) { | ||
// TODO?: Overflow check for UB - B | ||
TL = ceilingOfQuotient(*UB - B, A); | ||
LLVM_DEBUG(dbgs() << "\t Possible TL = " << *TL << "\n"); | ||
} | ||
} | ||
return std::make_pair(TL, TU); | ||
} | ||
|
||
// exactSIVtest - | ||
// When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*i], | ||
// where i is an induction variable, c1 and c2 are loop invariant, and a1 | ||
|
@@ -1590,14 +1631,12 @@ bool DependenceInfo::exactSIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff, | |
LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n"); | ||
|
||
// since SCEV construction normalizes, LM = 0 | ||
APInt UM(Bits, 1, true); | ||
bool UMValid = false; | ||
std::optional<APInt> UM; | ||
// UM is perhaps unavailable, let's check | ||
if (const SCEVConstant *CUB = | ||
collectConstantUpperBound(CurLoop, Delta->getType())) { | ||
UM = CUB->getAPInt(); | ||
LLVM_DEBUG(dbgs() << "\t UM = " << UM << "\n"); | ||
UMValid = true; | ||
LLVM_DEBUG(dbgs() << "\t UM = " << *UM << "\n"); | ||
} | ||
|
||
APInt TU(APInt::getSignedMaxValue(Bits)); | ||
|
@@ -1609,44 +1648,33 @@ bool DependenceInfo::exactSIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff, | |
LLVM_DEBUG(dbgs() << "\t TX = " << TX << "\n"); | ||
LLVM_DEBUG(dbgs() << "\t TY = " << TY << "\n"); | ||
|
||
SmallVector<APInt, 2> TLVec, TUVec; | ||
APInt TB = BM.sdiv(G); | ||
if (TB.sgt(0)) { | ||
TLVec.push_back(ceilingOfQuotient(-TX, TB)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n"); | ||
// New bound check - modification to Banerjee's e3 check | ||
if (UMValid) { | ||
TUVec.push_back(floorOfQuotient(UM - TX, TB)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n"); | ||
} | ||
} else { | ||
TUVec.push_back(floorOfQuotient(-TX, TB)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n"); | ||
// New bound check - modification to Banerjee's e3 check | ||
if (UMValid) { | ||
TLVec.push_back(ceilingOfQuotient(UM - TX, TB)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n"); | ||
} | ||
} | ||
|
||
APInt TA = AM.sdiv(G); | ||
if (TA.sgt(0)) { | ||
if (UMValid) { | ||
TUVec.push_back(floorOfQuotient(UM - TY, TA)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n"); | ||
} | ||
// New bound check - modification to Banerjee's e3 check | ||
TLVec.push_back(ceilingOfQuotient(-TY, TA)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n"); | ||
} else { | ||
if (UMValid) { | ||
TLVec.push_back(ceilingOfQuotient(UM - TY, TA)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n"); | ||
} | ||
// New bound check - modification to Banerjee's e3 check | ||
TUVec.push_back(floorOfQuotient(-TY, TA)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n"); | ||
} | ||
|
||
// At this point, we have the following equations: | ||
// | ||
// TA*i0 - TB*i1 = TC | ||
// | ||
// Also, we know that the all pairs of (i0, i1) can be expressed as: | ||
// | ||
// (TX + k*TB, TY + k*TA) | ||
// | ||
// where k is an arbitrary integer. | ||
Comment on lines
+1669
to
+1677
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. Disclaimer: I do not have access to the original book Dependence Analysis for Supercomputing, so these comments are based solely on my interpretation of the code. Please note that this comment is very likely to be incorrect. |
||
auto [TL0, TU0] = inferDomainOfAffine(TB, TX, UM); | ||
auto [TL1, TU1] = inferDomainOfAffine(TA, TY, UM); | ||
|
||
auto CreateVec = [](const std::optional<APInt> &V0, | ||
const std::optional<APInt> &V1) { | ||
SmallVector<APInt, 2> Vec; | ||
if (V0) | ||
Vec.push_back(*V0); | ||
if (V1) | ||
Vec.push_back(*V1); | ||
return Vec; | ||
}; | ||
|
||
SmallVector<APInt, 2> TLVec = CreateVec(TL0, TL1); | ||
SmallVector<APInt, 2> TUVec = CreateVec(TU0, TU1); | ||
|
||
LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n"); | ||
LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n"); | ||
|
@@ -1967,24 +1995,20 @@ bool DependenceInfo::exactRDIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff, | |
LLVM_DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n"); | ||
|
||
// since SCEV construction seems to normalize, LM = 0 | ||
APInt SrcUM(Bits, 1, true); | ||
bool SrcUMvalid = false; | ||
std::optional<APInt> SrcUM; | ||
// SrcUM is perhaps unavailable, let's check | ||
if (const SCEVConstant *UpperBound = | ||
collectConstantUpperBound(SrcLoop, Delta->getType())) { | ||
SrcUM = UpperBound->getAPInt(); | ||
LLVM_DEBUG(dbgs() << "\t SrcUM = " << SrcUM << "\n"); | ||
SrcUMvalid = true; | ||
LLVM_DEBUG(dbgs() << "\t SrcUM = " << *SrcUM << "\n"); | ||
} | ||
|
||
APInt DstUM(Bits, 1, true); | ||
bool DstUMvalid = false; | ||
std::optional<APInt> DstUM; | ||
// UM is perhaps unavailable, let's check | ||
if (const SCEVConstant *UpperBound = | ||
collectConstantUpperBound(DstLoop, Delta->getType())) { | ||
DstUM = UpperBound->getAPInt(); | ||
LLVM_DEBUG(dbgs() << "\t DstUM = " << DstUM << "\n"); | ||
DstUMvalid = true; | ||
LLVM_DEBUG(dbgs() << "\t DstUM = " << *DstUM << "\n"); | ||
} | ||
|
||
APInt TU(APInt::getSignedMaxValue(Bits)); | ||
|
@@ -1996,47 +2020,39 @@ bool DependenceInfo::exactRDIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff, | |
LLVM_DEBUG(dbgs() << "\t TX = " << TX << "\n"); | ||
LLVM_DEBUG(dbgs() << "\t TY = " << TY << "\n"); | ||
|
||
SmallVector<APInt, 2> TLVec, TUVec; | ||
APInt TB = BM.sdiv(G); | ||
if (TB.sgt(0)) { | ||
TLVec.push_back(ceilingOfQuotient(-TX, TB)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n"); | ||
if (SrcUMvalid) { | ||
TUVec.push_back(floorOfQuotient(SrcUM - TX, TB)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n"); | ||
} | ||
} else { | ||
TUVec.push_back(floorOfQuotient(-TX, TB)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n"); | ||
if (SrcUMvalid) { | ||
TLVec.push_back(ceilingOfQuotient(SrcUM - TX, TB)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n"); | ||
} | ||
} | ||
|
||
APInt TA = AM.sdiv(G); | ||
if (TA.sgt(0)) { | ||
TLVec.push_back(ceilingOfQuotient(-TY, TA)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n"); | ||
if (DstUMvalid) { | ||
TUVec.push_back(floorOfQuotient(DstUM - TY, TA)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n"); | ||
} | ||
} else { | ||
TUVec.push_back(floorOfQuotient(-TY, TA)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TU = " << TUVec.back() << "\n"); | ||
if (DstUMvalid) { | ||
TLVec.push_back(ceilingOfQuotient(DstUM - TY, TA)); | ||
LLVM_DEBUG(dbgs() << "\t Possible TL = " << TLVec.back() << "\n"); | ||
} | ||
} | ||
|
||
if (TLVec.empty() || TUVec.empty()) | ||
return false; | ||
// At this point, we have the following equations: | ||
// | ||
// TA*i - TB*j = TC | ||
// | ||
// Also, we know that the all pairs of (i, j) can be expressed as: | ||
// | ||
// (TX + k*TB, TY + k*TA) | ||
// | ||
// where k is an arbitrary integer. | ||
auto [TL0, TU0] = inferDomainOfAffine(TB, TX, SrcUM); | ||
auto [TL1, TU1] = inferDomainOfAffine(TA, TY, DstUM); | ||
|
||
LLVM_DEBUG(dbgs() << "\t TA = " << TA << "\n"); | ||
LLVM_DEBUG(dbgs() << "\t TB = " << TB << "\n"); | ||
|
||
auto CreateVec = [](const std::optional<APInt> &V0, | ||
const std::optional<APInt> &V1) { | ||
SmallVector<APInt, 2> Vec; | ||
if (V0) | ||
Vec.push_back(*V0); | ||
if (V1) | ||
Vec.push_back(*V1); | ||
return Vec; | ||
}; | ||
|
||
SmallVector<APInt, 2> TLVec = CreateVec(TL0, TL1); | ||
SmallVector<APInt, 2> TUVec = CreateVec(TU0, TU1); | ||
if (TLVec.empty() || TUVec.empty()) | ||
return false; | ||
|
||
TL = APIntOps::smax(TLVec.front(), TLVec.back()); | ||
TU = APIntOps::smin(TUVec.front(), TUVec.back()); | ||
LLVM_DEBUG(dbgs() << "\t TL = " << TL << "\n"); | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.