[PATCH] D137461: [LoopInterchange] rewrite validDepInterchange() to ensure Lexicographically Order

MengXuan Cai via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 4 13:16:25 PDT 2022


Narutoworld created this revision.
Narutoworld added a reviewer: congzhe.
Narutoworld added projects: LLVM, LoopOptWG.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Narutoworld requested review of this revision.
Herald added a subscriber: llvm-commits.

Rewrite `validDepInterchange()` function to strictly follow Lexicographically Order.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137461

Files:
  llvm/lib/Transforms/Scalar/LoopInterchange.cpp


Index: llvm/lib/Transforms/Scalar/LoopInterchange.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LoopInterchange.cpp
+++ llvm/lib/Transforms/Scalar/LoopInterchange.cpp
@@ -182,48 +182,21 @@
     std::swap(DepMatrix[I][ToIndx], DepMatrix[I][FromIndx]);
 }
 
-// Checks if no dependence exist in the dependency matrix in Row before Column.
-static bool containsNoDependence(CharMatrix &DepMatrix, unsigned Row,
-                                 unsigned Column) {
-  for (unsigned i = 0; i < Column; ++i) {
-    if (DepMatrix[Row][i] != '=' && DepMatrix[Row][i] != 'S' &&
-        DepMatrix[Row][i] != 'I')
+static bool isLexicographicallyPositive(std::vector<char> &DV) {
+  for (unsigned Level = 0; Level < DV.size(); ++Level) {
+    unsigned char Direction = DV[Level];
+    // Check DepVector from outter loop to inner loop
+    // Direction could be S, < , > , =, I
+    // Ignore S, =, I
+    // lexiographically positive means the first non (=, I, S) is <
+    if (Direction == '<')
+      return true;
+    if (Direction == '>')
       return false;
   }
   return true;
 }
 
-static bool validDepInterchange(CharMatrix &DepMatrix, unsigned Row,
-                                unsigned OuterLoopId, char InnerDep,
-                                char OuterDep) {
-  if (InnerDep == OuterDep)
-    return true;
-
-  // It is legal to interchange if and only if after interchange no row has a
-  // '>' direction as the leftmost non-'='.
-
-  if (InnerDep == '=' || InnerDep == 'S' || InnerDep == 'I')
-    return true;
-
-  if (InnerDep == '<')
-    return true;
-
-  if (InnerDep == '>') {
-    // If OuterLoopId represents outermost loop then interchanging will make the
-    // 1st dependency as '>'
-    if (OuterLoopId == 0)
-      return false;
-
-    // If all dependencies before OuterloopId are '=','S'or 'I'. Then
-    // interchanging will result in this row having an outermost non '='
-    // dependency of '>'
-    if (!containsNoDependence(DepMatrix, Row, OuterLoopId))
-      return true;
-  }
-
-  return false;
-}
-
 // Checks if it is legal to interchange 2 loops.
 // [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
@@ -238,7 +211,10 @@
     char OuterDep = DepMatrix[Row][OuterLoopId];
     if (InnerDep == '*' || OuterDep == '*')
       return false;
-    if (!validDepInterchange(DepMatrix, Row, OuterLoopId, InnerDep, OuterDep))
+    // Create temporary DepVector and swap OutterLoop vs InnerLoop
+    std::vector<char> Cur = DepMatrix[Row];
+    std::swap(Cur[InnerLoopId], Cur[OuterLoopId]);
+    if (!isLexicographicallyPositive(Cur))
       return false;
   }
   return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137461.473325.patch
Type: text/x-patch
Size: 2770 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221104/af933143/attachment.bin>


More information about the llvm-commits mailing list