[PATCH] D75333: Refactor to distinguish the reason for not interchanging loops.

Masakazu Ueno via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 28 01:21:32 PST 2020


masakazu.ueno created this revision.
masakazu.ueno added reviewers: fhahn, karthikthecool, mkuper, TylerNowicki, mcrosier, t.p.northover, RKSimon, dcaballe, hsaito, rengolin.
masakazu.ueno created this object with visibility "All Users".
masakazu.ueno added a project: LLVM.

Modified to distinguish between combinations that cannot be loop-interchanged and combinations that can be loop-interchanged but do not, due to profitability.

This modification/refactoring is required for the modification in https://reviews.llvm.org/D71689.


Repository:
  rL LLVM

https://reviews.llvm.org/D75333

Files:
  lib/Transforms/Scalar/LoopInterchange.cpp


Index: lib/Transforms/Scalar/LoopInterchange.cpp
===================================================================
--- lib/Transforms/Scalar/LoopInterchange.cpp
+++ lib/Transforms/Scalar/LoopInterchange.cpp
@@ -51,6 +51,12 @@
 
 #define DEBUG_TYPE "loop-interchange"
 
+enum class LoopInterchangeResult {
+  NotLegal,      // Loops are not legal to interchange.
+  NotProfitable, // Loops are not profitable to interchange.
+  Optimized,
+};
+
 STATISTIC(LoopsInterchanged, "Number of loops interchanged");
 
 static cl::opt<int> LoopInterchangeCostThreshold(
@@ -529,10 +535,11 @@
     unsigned SelecLoopId = selectLoopForInterchange(LoopList);
     // Move the selected loop outwards to the best possible position.
     for (unsigned i = SelecLoopId; i > 0; i--) {
-      bool Interchanged =
+      LoopInterchangeResult Interchanged =
           processLoop(LoopList, i, i - 1, LoopNestExit, DependencyMatrix);
-      if (!Interchanged)
-        return Changed;
+      if (Interchanged == LoopInterchangeResult::NotLegal ||
+          Interchanged == LoopInterchangeResult::NotProfitable) return Changed;
+
       // Loops interchanged reflect the same in LoopList
       std::swap(LoopList[i - 1], LoopList[i]);
 
@@ -542,14 +549,14 @@
       LLVM_DEBUG(dbgs() << "Dependence after interchange\n");
       printDepMatrix(DependencyMatrix);
 #endif
-      Changed |= Interchanged;
+      Changed |= (Interchanged == LoopInterchangeResult::Optimized);
     }
     return Changed;
   }
 
-  bool processLoop(LoopVector LoopList, unsigned InnerLoopId,
-                   unsigned OuterLoopId, BasicBlock *LoopNestExit,
-                   std::vector<std::vector<char>> &DependencyMatrix) {
+  LoopInterchangeResult processLoop(LoopVector LoopList, unsigned InnerLoopId,
+                                    unsigned OuterLoopId, BasicBlock *LoopNestExit,
+                                    std::vector<std::vector<char>> &DependencyMatrix) {
     LLVM_DEBUG(dbgs() << "Processing Inner Loop Id = " << InnerLoopId
                       << " and OuterLoopId = " << OuterLoopId << "\n");
     Loop *InnerLoop = LoopList[InnerLoopId];
@@ -558,13 +565,13 @@
     LoopInterchangeLegality LIL(OuterLoop, InnerLoop, SE, ORE);
     if (!LIL.canInterchangeLoops(InnerLoopId, OuterLoopId, DependencyMatrix)) {
       LLVM_DEBUG(dbgs() << "Not interchanging loops. Cannot prove legality.\n");
-      return false;
+      return LoopInterchangeResult::NotLegal;
     }
     LLVM_DEBUG(dbgs() << "Loops are legal to interchange\n");
     LoopInterchangeProfitability LIP(OuterLoop, InnerLoop, SE, ORE);
     if (!LIP.isProfitable(InnerLoopId, OuterLoopId, DependencyMatrix)) {
       LLVM_DEBUG(dbgs() << "Interchanging loops not profitable.\n");
-      return false;
+      return LoopInterchangeResult::NotProfitable;
     }
 
     ORE->emit([&]() {
@@ -579,7 +586,7 @@
     LIT.transform();
     LLVM_DEBUG(dbgs() << "Loops interchanged.\n");
     LoopsInterchanged++;
-    return true;
+    return LoopInterchangeResult::Optimized;
   }
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75333.247170.patch
Type: text/x-patch
Size: 3040 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200228/f3b23b6e/attachment.bin>


More information about the llvm-commits mailing list