[llvm] [DA] Move isDirectionNegative and normalize into LoopInterchange (PR #206959)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 1 05:47:23 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Ryotaro Kasuga (kasuga-fj)

<details>
<summary>Changes</summary>

`Dependence::isDirectionNegative` returns whether a `Dependence` object is considered "negative". `Dependence::normalize` reverses a dependence if it is negative and converts it into a "normal form". The definitions of "negative" and "normal form" are problematic, because reversing a dependence does not necessarily change the result of `isDirectionNegative`. For example, `isDirectionNegative` returns false for both `[* <]` and `[* >]`.

I don't think a dependence can be assigned a well-defined "sign", and therefore I don't think such ambiguous concepts should be exposed as public APIs. In fact, the current behavior negatively affects LoopInterchange, as demonstrated by the test case added in #<!-- -->206958.

Ideally, these methods should simply be removed. However, they are currently used by loop interchange. Therefore, for now, this patch moves these APIs into LoopInterchange.

---
Full diff: https://github.com/llvm/llvm-project/pull/206959.diff


5 Files Affected:

- (modified) llvm/include/llvm/Analysis/DependenceAnalysis.h (-20) 
- (modified) llvm/lib/Analysis/DependenceAnalysis.cpp (-43) 
- (modified) llvm/lib/Transforms/Scalar/LoopInterchange.cpp (+48-1) 
- (modified) llvm/test/Analysis/DependenceAnalysis/Banerjee.ll (+4-4) 
- (added) llvm/test/Transforms/LoopInterchange/dependency-all-neg-eq.ll (+83) 


``````````diff
diff --git a/llvm/include/llvm/Analysis/DependenceAnalysis.h b/llvm/include/llvm/Analysis/DependenceAnalysis.h
index 490fd4520746f..258a5f0e8ab79 100644
--- a/llvm/include/llvm/Analysis/DependenceAnalysis.h
+++ b/llvm/include/llvm/Analysis/DependenceAnalysis.h
@@ -165,20 +165,10 @@ class LLVM_ABI Dependence {
     return nullptr;
   }
 
-  /// Check if the direction vector is negative. A negative direction
-  /// vector means Src and Dst are reversed in the actual program.
-  virtual bool isDirectionNegative() const { return false; }
-
   /// Negate the dependence by swapping the source and destination, and
   /// reversing the direction and distance information.
   virtual void negate(ScalarEvolution &SE) {}
 
-  /// If the direction vector is negative, normalize the direction
-  /// vector to make it non-negative. Normalization is done by reversing
-  /// Src and Dst, plus reversing the dependence directions and distances
-  /// in the vector.
-  virtual bool normalize(ScalarEvolution *SE) { return false; }
-
   /// inSameSDLoops - Returns true if this level is an SameSD level, i.e.,
   /// performed across two separate loop nests that have the Same Iteration and
   /// Depth.
@@ -275,18 +265,8 @@ class LLVM_ABI FullDependence final : public Dependence {
   /// particular common or SameSD level.
   const SCEV *getDistance(unsigned Level, bool SameSD = false) const override;
 
-  /// Check if the direction vector is negative. A negative direction
-  /// vector means Src and Dst are reversed in the actual program.
-  bool isDirectionNegative() const override;
-
   void negate(ScalarEvolution &SE) override;
 
-  /// If the direction vector is negative, normalize the direction
-  /// vector to make it non-negative. Normalization is done by reversing
-  /// Src and Dst, plus reversing the dependence directions and distances
-  /// in the vector.
-  bool normalize(ScalarEvolution *SE) override;
-
   /// inSameSDLoops - Returns true if this level is an SameSD level, i.e.,
   /// performed across two separate loop nests that have the Same Iteration and
   /// Depth.
diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index 9d5a555fb8998..f0d1c60c2cfa4 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -329,9 +329,6 @@ static void dumpExampleDependence(raw_ostream &OS, DependenceInfo *DA,
             }
 #endif
 
-            // Normalize negative direction vectors if required by clients.
-            if (NormalizeResults && D->normalize(&SE))
-              OS << "normalized - ";
             D->dump(OS);
           } else
             OS << "none!\n";
@@ -401,34 +398,6 @@ FullDependence::FullDependence(Instruction *Source, Instruction *Destination,
     DV = std::make_unique<DVEntry[]>(CommonLevels);
 }
 
-// FIXME: in some cases the meaning of a negative direction vector
-// may not be straightforward, e.g.,
-// for (int i = 0; i < 32; ++i) {
-//   Src:    A[i] = ...;
-//   Dst:    use(A[31 - i]);
-// }
-// The dependency is
-//   flow { Src[i] -> Dst[31 - i] : when i >= 16 } and
-//   anti { Dst[i] -> Src[31 - i] : when i < 16 },
-// -- hence a [<>].
-// As long as a dependence result contains '>' ('<>', '<=>', "*"), it
-// means that a reversed/normalized dependence needs to be considered
-// as well. Nevertheless, current isDirectionNegative() only returns
-// true with a '>' or '>=' dependency for ease of canonicalizing the
-// dependency vector, since the reverse of '<>', '<=>' and "*" is itself.
-bool FullDependence::isDirectionNegative() const {
-  for (unsigned Level = 1; Level <= Levels; ++Level) {
-    unsigned char Direction = DV[Level - 1].Direction;
-    if (Direction == Dependence::DVEntry::EQ)
-      continue;
-    if (Direction == Dependence::DVEntry::GT ||
-        Direction == Dependence::DVEntry::GE)
-      return true;
-    return false;
-  }
-  return false;
-}
-
 void FullDependence::negate(ScalarEvolution &SE) {
   std::swap(Src, Dst);
   for (unsigned Level = 1; Level <= Levels; ++Level) {
@@ -447,18 +416,6 @@ void FullDependence::negate(ScalarEvolution &SE) {
   }
 }
 
-bool FullDependence::normalize(ScalarEvolution *SE) {
-  if (!isDirectionNegative())
-    return false;
-
-  LLVM_DEBUG(dbgs() << "Before normalizing negative direction vectors:\n";
-             dump(dbgs()););
-  negate(*SE);
-  LLVM_DEBUG(dbgs() << "After normalizing negative direction vectors:\n";
-             dump(dbgs()););
-  return true;
-}
-
 // The rest are simple getters that hide the implementation.
 
 // getDirection - Returns the direction associated with a particular common or
diff --git a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
index 96a4e4d54c6f8..c139a28e10d05 100644
--- a/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopInterchange.cpp
@@ -166,6 +166,53 @@ static bool inThisOrder(const Instruction *Src, const Instruction *Dst) {
 }
 #endif
 
+/// Check if the direction vector is negative. A negative direction
+/// vector means Src and Dst are reversed in the actual program.
+///
+/// FIXME: in some cases the meaning of a negative direction vector
+/// may not be straightforward, e.g.,
+/// for (int i = 0; i < 32; ++i) {
+///   Src:    A[i] = ...;
+///   Dst:    use(A[31 - i]);
+/// }
+/// The dependency is
+///   flow { Src[i] -> Dst[31 - i] : when i >= 16 } and
+///   anti { Dst[i] -> Src[31 - i] : when i < 16 },
+/// -- hence a [<>].
+/// As long as a dependence result contains '>' ('<>', '<=>', "*"), it
+/// means that a reversed/normalized dependence needs to be considered
+/// as well. Nevertheless, current isDirectionNegative() only returns
+/// true with a '>' or '>=' dependency for ease of canonicalizing the
+/// dependency vector, since the reverse of '<>', '<=>' and "*" is itself.
+static bool isDirectionNegative(const Dependence &D) {
+  for (unsigned Level = 1; Level <= D.getLevels(); ++Level) {
+    unsigned char Direction = D.getDirection(Level);
+    if (Direction == Dependence::DVEntry::EQ)
+      continue;
+    if (Direction == Dependence::DVEntry::GT ||
+        Direction == Dependence::DVEntry::GE)
+      return true;
+    return false;
+  }
+  return false;
+}
+
+/// If the direction vector is negative, normalize the direction
+/// vector to make it non-negative. Normalization is done by reversing
+/// Src and Dst, plus reversing the dependence directions and distances
+/// in the vector.
+bool normalize(Dependence *D, ScalarEvolution *SE) {
+  if (!isDirectionNegative(*D))
+    return false;
+
+  LLVM_DEBUG(dbgs() << "Before normalizing negative direction vectors:\n";
+             D->dump(dbgs()););
+  D->negate(*SE);
+  LLVM_DEBUG(dbgs() << "After normalizing negative direction vectors:\n";
+             D->dump(dbgs()););
+  return true;
+}
+
 static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
                                      Loop *L, DependenceInfo *DI,
                                      ScalarEvolution *SE,
@@ -230,7 +277,7 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
         assert(D->isOrdered() && "Expected an output, flow or anti dep.");
         // If the direction vector is negative, normalize it to
         // make it non-negative.
-        if (D->normalize(SE))
+        if (normalize(&*D, SE))
           LLVM_DEBUG(dbgs() << "Negative dependence vector normalized.\n");
         LLVM_DEBUG(StringRef DepType =
                        D->isFlow() ? "flow" : D->isAnti() ? "anti" : "output";
diff --git a/llvm/test/Analysis/DependenceAnalysis/Banerjee.ll b/llvm/test/Analysis/DependenceAnalysis/Banerjee.ll
index bb89ad55554ff..de7546337e4c7 100644
--- a/llvm/test/Analysis/DependenceAnalysis/Banerjee.ll
+++ b/llvm/test/Analysis/DependenceAnalysis/Banerjee.ll
@@ -306,7 +306,7 @@ define void @banerjee3(ptr %A, ptr %B, i64 %m, i64 %n) nounwind uwtable ssp {
 ; NORMALIZE-NEXT:  Src: store i64 0, ptr %arrayidx, align 8 --> Dst: store i64 0, ptr %arrayidx, align 8
 ; NORMALIZE-NEXT:    da analyze - none!
 ; NORMALIZE-NEXT:  Src: store i64 0, ptr %arrayidx, align 8 --> Dst: %0 = load i64, ptr %arrayidx7, align 8
-; NORMALIZE-NEXT:    da analyze - normalized - anti [< <]!
+; NORMALIZE-NEXT:    da analyze - flow [> >]!
 ; NORMALIZE-NEXT:  Src: store i64 0, ptr %arrayidx, align 8 --> Dst: store i64 %0, ptr %B.addr.11, align 8
 ; NORMALIZE-NEXT:    da analyze - confused!
 ; NORMALIZE-NEXT:  Src: %0 = load i64, ptr %arrayidx7, align 8 --> Dst: %0 = load i64, ptr %arrayidx7, align 8
@@ -561,7 +561,7 @@ define void @banerjee6(ptr %A, ptr %B, i64 %m, i64 %n) nounwind uwtable ssp {
 ; NORMALIZE-NEXT:  Src: store i64 0, ptr %arrayidx, align 8 --> Dst: store i64 0, ptr %arrayidx, align 8
 ; NORMALIZE-NEXT:    da analyze - none!
 ; NORMALIZE-NEXT:  Src: store i64 0, ptr %arrayidx, align 8 --> Dst: %0 = load i64, ptr %arrayidx7, align 8
-; NORMALIZE-NEXT:    da analyze - normalized - anti [<= <>]!
+; NORMALIZE-NEXT:    da analyze - flow [=> <>]!
 ; NORMALIZE-NEXT:  Src: store i64 0, ptr %arrayidx, align 8 --> Dst: store i64 %0, ptr %B.addr.11, align 8
 ; NORMALIZE-NEXT:    da analyze - confused!
 ; NORMALIZE-NEXT:  Src: %0 = load i64, ptr %arrayidx7, align 8 --> Dst: %0 = load i64, ptr %arrayidx7, align 8
@@ -646,7 +646,7 @@ define void @banerjee7(ptr %A, ptr %B, i64 %m, i64 %n) nounwind uwtable ssp {
 ; NORMALIZE-NEXT:  Src: store i64 0, ptr %arrayidx, align 8 --> Dst: store i64 0, ptr %arrayidx, align 8
 ; NORMALIZE-NEXT:    da analyze - none!
 ; NORMALIZE-NEXT:  Src: store i64 0, ptr %arrayidx, align 8 --> Dst: %0 = load i64, ptr %arrayidx7, align 8
-; NORMALIZE-NEXT:    da analyze - normalized - anti [< =>]!
+; NORMALIZE-NEXT:    da analyze - flow [> <=]!
 ; NORMALIZE-NEXT:  Src: store i64 0, ptr %arrayidx, align 8 --> Dst: store i64 %0, ptr %B.addr.11, align 8
 ; NORMALIZE-NEXT:    da analyze - confused!
 ; NORMALIZE-NEXT:  Src: %0 = load i64, ptr %arrayidx7, align 8 --> Dst: %0 = load i64, ptr %arrayidx7, align 8
@@ -731,7 +731,7 @@ define void @banerjee8(ptr %A, ptr %B, i64 %m, i64 %n) nounwind uwtable ssp {
 ; NORMALIZE-NEXT:  Src: store i64 0, ptr %arrayidx, align 8 --> Dst: store i64 0, ptr %arrayidx, align 8
 ; NORMALIZE-NEXT:    da analyze - none!
 ; NORMALIZE-NEXT:  Src: store i64 0, ptr %arrayidx, align 8 --> Dst: %0 = load i64, ptr %arrayidx7, align 8
-; NORMALIZE-NEXT:    da analyze - normalized - anti [< <>]!
+; NORMALIZE-NEXT:    da analyze - flow [> <>]!
 ; NORMALIZE-NEXT:  Src: store i64 0, ptr %arrayidx, align 8 --> Dst: store i64 %0, ptr %B.addr.11, align 8
 ; NORMALIZE-NEXT:    da analyze - confused!
 ; NORMALIZE-NEXT:  Src: %0 = load i64, ptr %arrayidx7, align 8 --> Dst: %0 = load i64, ptr %arrayidx7, align 8
diff --git a/llvm/test/Transforms/LoopInterchange/dependency-all-neg-eq.ll b/llvm/test/Transforms/LoopInterchange/dependency-all-neg-eq.ll
new file mode 100644
index 0000000000000..ab32ae55fc61d
--- /dev/null
+++ b/llvm/test/Transforms/LoopInterchange/dependency-all-neg-eq.ll
@@ -0,0 +1,83 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=loop-interchange -loop-interchange-profitabilities=ignore -S | FileCheck %s
+
+; for (i = 0; i < 100; i++)
+;   for (j = 0; j < 255; j++)
+;     for (k = 0; k < 256; k++)
+;       A[j + 1][k] = A[j][k] + 1;
+;
+; The above loop nest has a direction vector of [* > =]. In principle, it is
+; legal to interchange the j-loop and the k-loop, but at the moment the
+; legality check doesn't handle such cases.
+;
+; FIXME: The direction vector of [* > =] should be normalized to [* < =] at the
+; beginning.
+;
+
+define void @all_neg_eq(ptr %A) {
+; CHECK-LABEL: define void @all_neg_eq(
+; CHECK-SAME: ptr [[A:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    br label %[[FOR_I_HEADER:.*]]
+; CHECK:       [[FOR_I_HEADER]]:
+; CHECK-NEXT:    [[I:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[I_INC:%.*]], %[[FOR_I_LATCH:.*]] ]
+; CHECK-NEXT:    br label %[[FOR_J_HEADER:.*]]
+; CHECK:       [[FOR_J_HEADER]]:
+; CHECK-NEXT:    [[J:%.*]] = phi i64 [ 0, %[[FOR_I_HEADER]] ], [ [[J_INC:%.*]], %[[FOR_J_LATCH:.*]] ]
+; CHECK-NEXT:    [[J_INC]] = add i64 [[J]], 1
+; CHECK-NEXT:    br label %[[FOR_K:.*]]
+; CHECK:       [[FOR_K]]:
+; CHECK-NEXT:    [[K:%.*]] = phi i64 [ 0, %[[FOR_J_HEADER]] ], [ [[K_INC:%.*]], %[[FOR_K]] ]
+; CHECK-NEXT:    [[PTR_LOAD:%.*]] = getelementptr [256 x i8], ptr [[A]], i64 [[J]], i64 [[K]]
+; CHECK-NEXT:    [[LD:%.*]] = load i8, ptr [[PTR_LOAD]], align 1
+; CHECK-NEXT:    [[ADD:%.*]] = add i8 [[LD]], 1
+; CHECK-NEXT:    [[PTR_STORE:%.*]] = getelementptr [256 x i8], ptr [[A]], i64 [[J_INC]], i64 [[K]]
+; CHECK-NEXT:    store i8 [[ADD]], ptr [[PTR_STORE]], align 1
+; CHECK-NEXT:    [[K_INC]] = add i64 [[K]], 1
+; CHECK-NEXT:    [[EC_K:%.*]] = icmp eq i64 [[K_INC]], 256
+; CHECK-NEXT:    br i1 [[EC_K]], label %[[FOR_J_LATCH]], label %[[FOR_K]]
+; CHECK:       [[FOR_J_LATCH]]:
+; CHECK-NEXT:    [[EC_J:%.*]] = icmp eq i64 [[J_INC]], 255
+; CHECK-NEXT:    br i1 [[EC_J]], label %[[FOR_I_LATCH]], label %[[FOR_J_HEADER]]
+; CHECK:       [[FOR_I_LATCH]]:
+; CHECK-NEXT:    [[I_INC]] = add i64 [[I]], 1
+; CHECK-NEXT:    [[EC_I:%.*]] = icmp eq i64 [[I_INC]], 100
+; CHECK-NEXT:    br i1 [[EC_I]], label %[[EXIT:.*]], label %[[FOR_I_HEADER]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %for.i.header
+
+for.i.header:
+  %i = phi i64 [ 0, %entry ], [ %i.inc, %for.i.latch ]
+  br label %for.j.header
+
+for.j.header:
+  %j = phi i64 [ 0, %for.i.header ], [ %j.inc, %for.j.latch ]
+  %j.inc = add i64 %j, 1
+  br label %for.k
+
+for.k:
+  %k = phi i64 [ 0, %for.j.header ], [ %k.inc, %for.k ]
+  %ptr.load = getelementptr [256 x i8], ptr %A, i64 %j, i64 %k
+  %ld = load i8, ptr %ptr.load
+  %add = add i8 %ld, 1
+  %ptr.store = getelementptr [256 x i8], ptr %A, i64 %j.inc, i64 %k
+  store i8 %add, ptr %ptr.store
+  %k.inc = add i64 %k, 1
+  %ec.k = icmp eq i64 %k.inc, 256
+  br i1 %ec.k, label %for.j.latch, label %for.k
+
+for.j.latch:
+  %ec.j = icmp eq i64 %j.inc, 255
+  br i1 %ec.j, label %for.i.latch, label %for.j.header
+
+for.i.latch:
+  %i.inc = add i64 %i, 1
+  %ec.i = icmp eq i64 %i.inc, 100
+  br i1 %ec.i, label %exit, label %for.i.header
+
+exit:
+  ret void
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/206959


More information about the llvm-commits mailing list