[llvm] [LoopInterchange] Increment isDirectionNegative check to next level for scalar direction (PR #78951)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 23 22:33:25 PST 2024


https://github.com/ShivaChen updated https://github.com/llvm/llvm-project/pull/78951

>From 5aad00cb4be34d60f89736218327a92fc6020ca5 Mon Sep 17 00:00:00 2001
From: Shiva Chen <shiva.chen at imgtec.com>
Date: Mon, 8 Jan 2024 07:30:24 +0000
Subject: [PATCH 1/3] Add pr71519.ll

---
 .../Transforms/LoopInterchange/pr71519.ll     | 59 +++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100644 llvm/test/Transforms/LoopInterchange/pr71519.ll

diff --git a/llvm/test/Transforms/LoopInterchange/pr71519.ll b/llvm/test/Transforms/LoopInterchange/pr71519.ll
new file mode 100644
index 00000000000000..a69e5ace680b23
--- /dev/null
+++ b/llvm/test/Transforms/LoopInterchange/pr71519.ll
@@ -0,0 +1,59 @@
+; REQUIRES: asserts
+; RUN: opt < %s -passes=loop-interchange -cache-line-size=64 -verify-dom-info -verify-loop-info \
+; RUN:     -S -debug 2>&1 | FileCheck %s
+
+ at aa = global [256 x [256 x float]] zeroinitializer, align 64
+ at bb = global [256 x [256 x float]] zeroinitializer, align 64
+
+;;  for (int nl = 0; nl < 10000000/256; nl++)
+;;    for (int i = 0; i < 256; ++i)
+;;      for (int j = 1; j < 256; j++)
+;;        aa[j][i] = aa[j - 1][i] + bb[j][i];
+
+; CHECK: Found anti dependency between Src and Dst
+; CHECK:  Src:  %1 = load float, ptr %arrayidx10, align 4
+; CHECK:  Dst:  store float %add, ptr %arrayidx18, align 4
+; CHECK: Processing InnerLoopId = 2 and OuterLoopId = 1
+; CHECK: Not interchanging loops. Cannot prove legality.
+
+define float @s231() {
+entry:
+  br label %for.cond1.preheader
+
+; Loop:
+for.cond1.preheader:                              ; preds = %entry, %for.cond.cleanup3
+  %nl.036 = phi i32 [ 0, %entry ], [ %inc23, %for.cond.cleanup3 ]
+  br label %for.cond5.preheader
+
+for.cond.cleanup3:                                ; preds = %for.cond.cleanup7
+  %inc23 = add nuw nsw i32 %nl.036, 1
+  %exitcond41 = icmp ne i32 %inc23, 39062
+  br i1 %exitcond41, label %for.cond1.preheader, label %for.cond.cleanup
+
+for.cond.cleanup7:                                ; preds = %for.body8
+  %indvars.iv.next39 = add nuw nsw i64 %indvars.iv38, 1
+  %exitcond40 = icmp ne i64 %indvars.iv.next39, 256
+  br i1 %exitcond40, label %for.cond5.preheader, label %for.cond.cleanup3
+
+for.body8:                                        ; preds = %for.cond5.preheader, %for.body8
+  %indvars.iv = phi i64 [ 1, %for.cond5.preheader ], [ %indvars.iv.next, %for.body8 ]
+  %0 = add nsw i64 %indvars.iv, -1
+  %arrayidx10 = getelementptr inbounds [256 x [256 x float]], ptr @aa, i64 0, i64 %0, i64 %indvars.iv38
+  %1 = load float, ptr %arrayidx10, align 4
+  %arrayidx14 = getelementptr inbounds [256 x [256 x float]], ptr @bb, i64 0, i64 %indvars.iv, i64 %indvars.iv38
+  %2 = load float, ptr %arrayidx14, align 4
+  %add = fadd fast float %2, %1
+  %arrayidx18 = getelementptr inbounds [256 x [256 x float]], ptr @aa, i64 0, i64 %indvars.iv, i64 %indvars.iv38
+  store float %add, ptr %arrayidx18, align 4
+  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
+  %exitcond = icmp ne i64 %indvars.iv.next, 256
+  br i1 %exitcond, label %for.body8, label %for.cond.cleanup7
+
+for.cond5.preheader:                              ; preds = %for.cond1.preheader, %for.cond.cleanup7
+  %indvars.iv38 = phi i64 [ 0, %for.cond1.preheader ], [ %indvars.iv.next39, %for.cond.cleanup7 ]
+  br label %for.body8
+
+; Exit blocks
+for.cond.cleanup:                                 ; preds = %for.cond.cleanup3
+  ret float undef
+}

>From ba11c1fcbc6a6f03683aa4fd4f75ae74ddc6a2f5 Mon Sep 17 00:00:00 2001
From: Shiva Chen <shiva.chen at imgtec.com>
Date: Thu, 18 Jan 2024 11:24:35 +0000
Subject: [PATCH 2/3] [LoopInterchange] Skip scalar in isDirectionNegative

---
 llvm/lib/Analysis/DependenceAnalysis.cpp        | 2 +-
 llvm/test/Transforms/LoopInterchange/pr71519.ll | 9 +++++++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 1bce9aae09bb26..7d25049e03692f 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -290,7 +290,7 @@ FullDependence::FullDependence(Instruction *Source, Instruction *Destination,
 bool FullDependence::isDirectionNegative() const {
   for (unsigned Level = 1; Level <= Levels; ++Level) {
     unsigned char Direction = DV[Level - 1].Direction;
-    if (Direction == Dependence::DVEntry::EQ)
+    if (Direction == Dependence::DVEntry::EQ || isScalar(Level))
       continue;
     if (Direction == Dependence::DVEntry::GT ||
         Direction == Dependence::DVEntry::GE)
diff --git a/llvm/test/Transforms/LoopInterchange/pr71519.ll b/llvm/test/Transforms/LoopInterchange/pr71519.ll
index a69e5ace680b23..c364d89873a5dc 100644
--- a/llvm/test/Transforms/LoopInterchange/pr71519.ll
+++ b/llvm/test/Transforms/LoopInterchange/pr71519.ll
@@ -10,11 +10,16 @@
 ;;      for (int j = 1; j < 256; j++)
 ;;        aa[j][i] = aa[j - 1][i] + bb[j][i];
 
-; CHECK: Found anti dependency between Src and Dst
+; CHECK: Before normalizing negative direction vectors:
+; CHECK: consistent anti [S 0 -1]!
+; CHECK: After normalizing negative direction vectors:
+; CHECK: consistent flow [S 0 1]!
+; CHECK: Negative dependence vector normalized.
+; CHECK: Found flow dependency between Src and Dst
 ; CHECK:  Src:  %1 = load float, ptr %arrayidx10, align 4
 ; CHECK:  Dst:  store float %add, ptr %arrayidx18, align 4
 ; CHECK: Processing InnerLoopId = 2 and OuterLoopId = 1
-; CHECK: Not interchanging loops. Cannot prove legality.
+; CHECK: Loops interchanged.
 
 define float @s231() {
 entry:

>From 33e5c6fd4251c56526ea7fe414779e2f83f36afc Mon Sep 17 00:00:00 2001
From: Shiva Chen <shiva.chen at imgtec.com>
Date: Wed, 24 Jan 2024 06:29:38 +0000
Subject: [PATCH 3/3] Clear the Direction of undistributed loop to NONE

---
 llvm/lib/Analysis/DependenceAnalysis.cpp        | 12 +++++++++++-
 llvm/test/Transforms/LoopInterchange/pr71519.ll |  4 ++--
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 7d25049e03692f..e7b48940f8fd1d 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -290,7 +290,8 @@ FullDependence::FullDependence(Instruction *Source, Instruction *Destination,
 bool FullDependence::isDirectionNegative() const {
   for (unsigned Level = 1; Level <= Levels; ++Level) {
     unsigned char Direction = DV[Level - 1].Direction;
-    if (Direction == Dependence::DVEntry::EQ || isScalar(Level))
+    if (Direction == Dependence::DVEntry::EQ ||
+        Direction == Dependence::DVEntry::NONE)
       continue;
     if (Direction == Dependence::DVEntry::GT ||
         Direction == Dependence::DVEntry::GE)
@@ -678,6 +679,8 @@ void Dependence::dump(raw_ostream &OS) const {
       const SCEV *Distance = getDistance(II);
       if (Distance)
         OS << *Distance;
+      else if (getDirection(II) == DVEntry::NONE)
+        OS << "N";
       else if (isScalar(II))
         OS << "S";
       else {
@@ -3657,6 +3660,7 @@ DependenceInfo::depends(Instruction *Src, Instruction *Dst,
     }
   }
 
+  SmallBitVector DistributedLoops;
   for (unsigned P = 0; P < Pairs; ++P) {
     Pair[P].Loops.resize(MaxLevels + 1);
     Pair[P].GroupLoops.resize(MaxLevels + 1);
@@ -3674,8 +3678,14 @@ DependenceInfo::depends(Instruction *Src, Instruction *Dst,
     LLVM_DEBUG(dbgs() << "\tclass = " << Pair[P].Classification << "\n");
     LLVM_DEBUG(dbgs() << "\tloops = ");
     LLVM_DEBUG(dumpSmallBitVector(Pair[P].Loops));
+    DistributedLoops |= Pair[P].Loops;
   }
 
+  // Clear the Direction of undistributed loop to NONE.
+  for (unsigned II = 1; II < CommonLevels; ++II)
+    if (!DistributedLoops.test(II))
+      Result.DV[II - 1].Direction = Dependence::DVEntry::NONE;
+
   SmallBitVector Separable(Pairs);
   SmallBitVector Coupled(Pairs);
 
diff --git a/llvm/test/Transforms/LoopInterchange/pr71519.ll b/llvm/test/Transforms/LoopInterchange/pr71519.ll
index c364d89873a5dc..e8bfd6d7115a95 100644
--- a/llvm/test/Transforms/LoopInterchange/pr71519.ll
+++ b/llvm/test/Transforms/LoopInterchange/pr71519.ll
@@ -11,9 +11,9 @@
 ;;        aa[j][i] = aa[j - 1][i] + bb[j][i];
 
 ; CHECK: Before normalizing negative direction vectors:
-; CHECK: consistent anti [S 0 -1]!
+; CHECK: consistent anti [N 0 -1]!
 ; CHECK: After normalizing negative direction vectors:
-; CHECK: consistent flow [S 0 1]!
+; CHECK: consistent flow [N 0 1]!
 ; CHECK: Negative dependence vector normalized.
 ; CHECK: Found flow dependency between Src and Dst
 ; CHECK:  Src:  %1 = load float, ptr %arrayidx10, align 4



More information about the llvm-commits mailing list