[PATCH] D107159: [DA] control compile-time spent by MIV tests

Bardia Mahjour via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 30 07:10:50 PDT 2021


bmahjour created this revision.
bmahjour added reviewers: Meinersbur, efriedma, reames, artemrad, dmgreen, etiotto.
Herald added a subscriber: hiraditya.
bmahjour requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Function `exploreDirections()` in `DependenceAnalysis` implements a recursive algorithm for refining direction vectors. This algorithm has worst-case complexity of `O(3^(n+1))` where `n` is the number of common loop levels.  It essentially performs a search over a complete ternary tree of dependence directions. The algorithm tries to prune the search space by checking `testBounds` before recursing for each direction, but since the delta usually falls within the sum of lower and upper bounds the search space ends up being the full tree most of the time.

The problem can be illustrated with a deeply nested loop. For exampley DA takes more than a minute to analyze a 12 level deep loop nest containing a single load and a store (see example below and the attached IR) on a 3.1 GHz P8 <https://reviews.llvm.org/P8> machine with 600 GB of RAM.

  for (int i1 = 0; i1 < n; i1++)
    for (int i2 = 0; i2 < n; i2++)
      ...
         for (int i12 = 0; i12 < n; i12++)
             A[i1 + i2 + ... + i12] = B[i1 + i2 ... + i12];

F18235286: time1.simp.ll <https://reviews.llvm.org/F18235286>

The compile-time increases exponentially as the depth of the loop nest increases.

In this patch I'm adding a threshold to control the amount of time we spend in doing MIV tests (which most of the time end up resulting in over pessimistic direction vectors anyway).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D107159

Files:
  llvm/lib/Analysis/DependenceAnalysis.cpp


Index: llvm/lib/Analysis/DependenceAnalysis.cpp
===================================================================
--- llvm/lib/Analysis/DependenceAnalysis.cpp
+++ llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -119,6 +119,11 @@
         "dependence vectors for languages that allow the subscript of one "
         "dimension to underflow or overflow into another dimension."));
 
+static cl::opt<unsigned> MIVMaxLevelThreshold(
+    "da-miv-max-level-threshold", cl::init(9), cl::Hidden, cl::ZeroOrMore,
+    cl::desc("Maximum depth allowed for the recursive algorithm used to "
+             "explore MIV direction vectors."));
+
 //===----------------------------------------------------------------------===//
 // basics
 
@@ -2602,6 +2607,19 @@
                                            const SmallBitVector &Loops,
                                            unsigned &DepthExpanded,
                                            const SCEV *Delta) const {
+  // This algorithm has worst case complexity of O(3^n), where 'n' is the number
+  // of common loop levels. To avoid excessive compile-time, pessimize all the
+  // results and immediately return when the number of common levels is beyond
+  // the given threshold.
+  if (CommonLevels > MIVMaxLevelThreshold) {
+    LLVM_DEBUG(dbgs() << "Number of common levels exceeded the threshold. MIV "
+                         "direction exploration is terminated.\n");
+    for (unsigned K = 1; K <= CommonLevels; ++K)
+      if (Loops[K])
+        Bound[K].DirSet = Dependence::DVEntry::ALL;
+    return 1;
+  }
+
   if (Level > CommonLevels) {
     // record result
     LLVM_DEBUG(dbgs() << "\t[");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107159.363060.patch
Type: text/x-patch
Size: 1659 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210730/81f630d6/attachment.bin>


More information about the llvm-commits mailing list