Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions llvm/lib/Transforms/Scalar/LoopInterchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,22 @@ static void interChangeDependencies(CharMatrix &DepMatrix, unsigned FromIndx,
std::swap(DepMatrix[I][ToIndx], DepMatrix[I][FromIndx]);
}

// After interchanging, check if the direction vector is valid.
// Check if a direction vector is lexicographically positive. Return true if it
// is positive, nullopt if it is "zero", othrewise false.
// [Theorem] A permutation of the loops in a perfect nest is legal if and only
// if the direction matrix, after the same permutation is applied to its
// columns, has no ">" direction as the leftmost non-"=" direction in any row.
static bool isLexicographicallyPositive(std::vector<char> &DV) {
for (unsigned char Direction : DV) {
static std::optional<bool> isLexicographicallyPositive(std::vector<char> &DV,
unsigned Begin,
unsigned End) {
ArrayRef<char> DVRef(DV);
for (unsigned char Direction : DVRef.slice(Begin, End - Begin)) {
if (Direction == '<')
return true;
if (Direction == '>' || Direction == '*')
return false;
}
return true;
return std::nullopt;
}

// Checks if it is legal to interchange 2 loops.
Expand All @@ -256,10 +260,19 @@ static bool isLegalToInterChangeLoops(CharMatrix &DepMatrix,
// Create temporary DepVector check its lexicographical order
// before and after swapping OuterLoop vs InnerLoop
Cur = DepMatrix[Row];
if (!isLexicographicallyPositive(Cur))

// If the direction vector is lexicographically positive due to an element
// to the left of OuterLoopId, it is still positive after exchanging the two
// loops. In such a case we can skip the subsequent check.
if (isLexicographicallyPositive(Cur, 0, OuterLoopId) == true)
continue;

// Check if the direction vector is lexicographically positive (or zero)
// for both before/after exchanged.
if (isLexicographicallyPositive(Cur, 0, Cur.size()) == false)
return false;
std::swap(Cur[InnerLoopId], Cur[OuterLoopId]);
if (!isLexicographicallyPositive(Cur))
if (isLexicographicallyPositive(Cur, 0, Cur.size()) == false)
return false;
}
return true;
Expand Down
Loading