Skip to content

[DA] Improve code in getSplitIteration (NFC) #146137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 30, 2025

Conversation

artagnon
Copy link
Contributor

Prefer early-continue over deeply nested loops.

Prefer early-continue over deeply nested loops.
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Jun 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 27, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Ramkumar Ramachandra (artagnon)

Changes

Prefer early-continue over deeply nested loops.


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

1 Files Affected:

  • (modified) llvm/lib/Analysis/DependenceAnalysis.cpp (+55-57)
diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index c1b1d002c9979..ee57cb31006f2 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -4188,67 +4188,65 @@ const SCEV *DependenceInfo::getSplitIteration(const Dependence &Dep,
     }
   }
 
-  if (Coupled.count()) {
-    // test coupled subscript groups
-    SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
-    for (unsigned II = 0; II <= MaxLevels; ++II)
-      Constraints[II].setAny(SE);
-    for (unsigned SI : Coupled.set_bits()) {
-      SmallBitVector Group(Pair[SI].Group);
-      SmallBitVector Sivs(Pairs);
-      SmallBitVector Mivs(Pairs);
-      SmallBitVector ConstrainedLevels(MaxLevels + 1);
-      for (unsigned SJ : Group.set_bits()) {
-        if (Pair[SJ].Classification == Subscript::SIV)
-          Sivs.set(SJ);
-        else
-          Mivs.set(SJ);
+  assert(!Coupled.empty() && "coupled expected non-empty");
+
+  // test coupled subscript groups
+  SmallVector<Constraint, 4> Constraints(MaxLevels + 1);
+  for (unsigned II = 0; II <= MaxLevels; ++II)
+    Constraints[II].setAny(SE);
+  for (unsigned SI : Coupled.set_bits()) {
+    SmallBitVector Group(Pair[SI].Group);
+    SmallBitVector Sivs(Pairs);
+    SmallBitVector Mivs(Pairs);
+    SmallBitVector ConstrainedLevels(MaxLevels + 1);
+    for (unsigned SJ : Group.set_bits()) {
+      if (Pair[SJ].Classification == Subscript::SIV)
+        Sivs.set(SJ);
+      else
+        Mivs.set(SJ);
+    }
+    while (Sivs.any()) {
+      bool Changed = false;
+      for (unsigned SJ : Sivs.set_bits()) {
+        // SJ is an SIV subscript that's part of the current coupled group
+        unsigned Level;
+        const SCEV *SplitIter = nullptr;
+        (void)testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level, Result, NewConstraint,
+                      SplitIter);
+        if (Level == SplitLevel && SplitIter)
+          return SplitIter;
+        ConstrainedLevels.set(Level);
+        if (intersectConstraints(&Constraints[Level], &NewConstraint))
+          Changed = true;
+        Sivs.reset(SJ);
       }
-      while (Sivs.any()) {
-        bool Changed = false;
-        for (unsigned SJ : Sivs.set_bits()) {
-          // SJ is an SIV subscript that's part of the current coupled group
-          unsigned Level;
-          const SCEV *SplitIter = nullptr;
-          (void) testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level,
-                         Result, NewConstraint, SplitIter);
-          if (Level == SplitLevel && SplitIter)
-            return SplitIter;
-          ConstrainedLevels.set(Level);
-          if (intersectConstraints(&Constraints[Level], &NewConstraint))
-            Changed = true;
-          Sivs.reset(SJ);
-        }
-        if (Changed) {
-          // propagate, possibly creating new SIVs and ZIVs
-          for (unsigned SJ : Mivs.set_bits()) {
-            // SJ is an MIV subscript that's part of the current coupled group
-            if (propagate(Pair[SJ].Src, Pair[SJ].Dst,
-                          Pair[SJ].Loops, Constraints, Result.Consistent)) {
-              Pair[SJ].Classification =
-                classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()),
-                             Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()),
-                             Pair[SJ].Loops);
-              switch (Pair[SJ].Classification) {
-              case Subscript::ZIV:
-                Mivs.reset(SJ);
-                break;
-              case Subscript::SIV:
-                Sivs.set(SJ);
-                Mivs.reset(SJ);
-                break;
-              case Subscript::RDIV:
-              case Subscript::MIV:
-                break;
-              default:
-                llvm_unreachable("bad subscript classification");
-              }
-            }
-          }
+      if (!Changed)
+        continue;
+      // propagate, possibly creating new SIVs and ZIVs
+      for (unsigned SJ : Mivs.set_bits()) {
+        // SJ is an MIV subscript that's part of the current coupled group
+        if (!propagate(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, Constraints,
+                       Result.Consistent))
+          continue;
+        Pair[SJ].Classification = classifyPair(
+            Pair[SJ].Src, LI->getLoopFor(Src->getParent()), Pair[SJ].Dst,
+            LI->getLoopFor(Dst->getParent()), Pair[SJ].Loops);
+        switch (Pair[SJ].Classification) {
+        case Subscript::ZIV:
+          Mivs.reset(SJ);
+          break;
+        case Subscript::SIV:
+          Sivs.set(SJ);
+          Mivs.reset(SJ);
+          break;
+        case Subscript::RDIV:
+        case Subscript::MIV:
+          break;
+        default:
+          llvm_unreachable("bad subscript classification");
         }
       }
     }
   }
   llvm_unreachable("somehow reached end of routine");
-  return nullptr;
 }

Copy link
Contributor

@kasuga-fj kasuga-fj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with minor suggestions.

continue;
// propagate, possibly creating new SIVs and ZIVs
for (unsigned SJ : Mivs.set_bits()) {
// SJ is an MIV subscript that's part of the current coupled group
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this comment should be moved to the end of the if-statement, or revised to describe the early continue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think SJ is referred to in the next line, so I'd leave this as-is.

@artagnon artagnon merged commit d4fdfc3 into llvm:main Jun 30, 2025
9 checks passed
@artagnon artagnon deleted the da-splititer-nfc branch June 30, 2025 12:50
rlavaee pushed a commit to rlavaee/llvm-project that referenced this pull request Jul 1, 2025
Prefer early-continue over deeply nested loops.
rlavaee pushed a commit to rlavaee/llvm-project that referenced this pull request Jul 1, 2025
Prefer early-continue over deeply nested loops.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis Includes value tracking, cost tables and constant folding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants