Skip to content

Conversation

sebpop
Copy link
Contributor

@sebpop sebpop commented Sep 1, 2025

No description provided.

@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Sep 1, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 1, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Sebastian Pop (sebpop)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/Analysis/DependenceAnalysis.cpp (+34-12)
diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index f33e04e804e3d..da86a8d2cc9c0 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -3419,13 +3419,24 @@ bool DependenceInfo::tryDelinearizeFixedSize(
       size_t SSize = Subscripts.size();
       for (size_t I = 1; I < SSize; ++I) {
         const SCEV *S = Subscripts[I];
-        if (!isKnownNonNegative(S, Ptr))
+        if (!isKnownNonNegative(S, Ptr)) {
+          LLVM_DEBUG({
+            dbgs() << "Check failed: !isKnownNonNegative(S, Ptr)\n";
+            dbgs() << "  S: " << *S << "\n" << "  Ptr: " << *Ptr << "\n";
+          });
           return false;
+        }
         if (auto *SType = dyn_cast<IntegerType>(S->getType())) {
           const SCEV *Range = SE->getConstant(
               ConstantInt::get(SType, DimensionSizes[I - 1], false));
-          if (!isKnownLessThan(S, Range))
+          if (!isKnownLessThan(S, Range)) {
+            LLVM_DEBUG({
+              dbgs() << "Check failed: !isKnownLessThan(S, Range)\n";
+              dbgs() << "  S: " << *S << "\n"
+                     << "  Range: " << *Range << "\n";
+            });
             return false;
+          }
         }
       }
       return true;
@@ -3433,6 +3444,7 @@ bool DependenceInfo::tryDelinearizeFixedSize(
 
     if (!AllIndicesInRange(SrcSizes, SrcSubscripts, SrcPtr) ||
         !AllIndicesInRange(DstSizes, DstSubscripts, DstPtr)) {
+      LLVM_DEBUG(dbgs() << "Check failed: AllIndicesInRange.\n");
       SrcSubscripts.clear();
       DstSubscripts.clear();
       return false;
@@ -3500,17 +3512,27 @@ bool DependenceInfo::tryDelinearizeParametricSize(
   // to the dependency checks.
   if (!DisableDelinearizationChecks)
     for (size_t I = 1; I < Size; ++I) {
-      if (!isKnownNonNegative(SrcSubscripts[I], SrcPtr))
-        return false;
-
-      if (!isKnownLessThan(SrcSubscripts[I], Sizes[I - 1]))
-        return false;
-
-      if (!isKnownNonNegative(DstSubscripts[I], DstPtr))
-        return false;
+      bool SNN = isKnownNonNegative(SrcSubscripts[I], SrcPtr);
+      bool DNN = isKnownNonNegative(DstSubscripts[I], DstPtr);
+      bool SLT = isKnownLessThan(SrcSubscripts[I], Sizes[I - 1]);
+      bool DLT = isKnownLessThan(DstSubscripts[I], Sizes[I - 1]);
+      if (SNN && DNN && SLT && DLT)
+        continue;
 
-      if (!isKnownLessThan(DstSubscripts[I], Sizes[I - 1]))
-        return false;
+      LLVM_DEBUG({
+        dbgs() << "Delinearization checks failed: can't prove the following\n";
+        if (!SNN)
+          dbgs() << "  isKnownNonNegative(" << *SrcSubscripts[I] << ")\n";
+        if (!DNN)
+          dbgs() << "  isKnownNonNegative(" << *DstSubscripts[I] << ")\n";
+        if (!SLT)
+          dbgs() << "  isKnownLessThan(" << *SrcSubscripts[I] << ", "
+                 << *Sizes[I - 1] << ")\n";
+        if (!DLT)
+          dbgs() << "  isKnownLessThan(" << *DstSubscripts[I] << ", "
+                 << *Sizes[I - 1] << ")\n";
+      });
+      return false;
     }
 
   return true;

@sebpop sebpop requested review from nikic and sjoerdmeijer September 1, 2025 18:32
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.

Are these new debug messages really necessary? DA already has a large number of debug messages, and I don't think they are well organized. If possible, I'd prefer not to add much more.

@sjoerdmeijer
Copy link
Collaborator

Are these new debug messages really necessary? DA already has a large number of debug messages, and I don't think they are well organized. If possible, I'd prefer not to add much more.

If other debug messages need cleaning up, that's unrelated to this change.

I think it is super useful to understand why delinearization fails and so these debug messages seem useful. I will let @sebpop answer your question, but I am happy to LGTM this.

@nikic nikic changed the title improve debug messages in dependence analysis [DependenceAnalysis] Improve debug messages Sep 2, 2025
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

(This looks fine to me, but I also don't work on DA, so I can't comment on what's useful or not.)

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.

Personally, I don't think this is super useful, but I don't have any strong objections either.

}
}
return true;
};

if (!AllIndicesInRange(SrcSizes, SrcSubscripts, SrcPtr) ||
!AllIndicesInRange(DstSizes, DstSubscripts, DstPtr)) {
LLVM_DEBUG(dbgs() << "Check failed: AllIndicesInRange.\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is redundant.

Copy link
Contributor

Choose a reason for hiding this comment

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

Currently, when the range check fails, the debug output looks like:

Check failed: !isKnownLessThan(S, Range)
  S: {0,+,1}<nuw><nsw><%loop.j>
  Range: 256
Check failed: AllIndicesInRange.

I think the last line is redundant.

Comment on lines +3524 to +3534
if (!SNN)
dbgs() << " isKnownNonNegative(" << *SrcSubscripts[I] << ")\n";
if (!DNN)
dbgs() << " isKnownNonNegative(" << *DstSubscripts[I] << ")\n";
if (!SLT)
dbgs() << " isKnownLessThan(" << *SrcSubscripts[I] << ", "
<< *Sizes[I - 1] << ")\n";
if (!DLT)
dbgs() << " isKnownLessThan(" << *DstSubscripts[I] << ", "
<< *Sizes[I - 1] << ")\n";
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you unify the format? The messages in tryDelinearizeFixedSize dump like

Check failed: !isKnownNonNegative(S, Ptr)
  S: ...
  Ptr: ...

But this one prints

  isKnownNonNegative(..., ...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Format doesn't matter.
It's not like we're using a parser for the debug messages.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not talking about parsing debug outputs. The point here is to prevent an increase in messy or unorganized logging.

After posting my earlier comment, I realized that these messages should probably be placed inside isKnownNonNegative and isKnownLessThan, respectively.

@sebpop
Copy link
Contributor Author

sebpop commented Sep 2, 2025

Personally, I don't think this is super useful, but I don't have any strong objections either.

Debug messages are supposed to tell at a glance without starting a debugger why something is discarded, etc.
Very subjective, although this helped me narrow down why things were not working the way they supposed to be.

@sebpop sebpop merged commit 9f8988a into llvm:main Sep 2, 2025
11 checks passed
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.

This PR hasn't been approved by anyone yet. In general, you should wait for at least one approval before landing.

}
}
return true;
};

if (!AllIndicesInRange(SrcSizes, SrcSubscripts, SrcPtr) ||
!AllIndicesInRange(DstSizes, DstSubscripts, DstPtr)) {
LLVM_DEBUG(dbgs() << "Check failed: AllIndicesInRange.\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

Currently, when the range check fails, the debug output looks like:

Check failed: !isKnownLessThan(S, Range)
  S: {0,+,1}<nuw><nsw><%loop.j>
  Range: 256
Check failed: AllIndicesInRange.

I think the last line is redundant.

Comment on lines +3524 to +3534
if (!SNN)
dbgs() << " isKnownNonNegative(" << *SrcSubscripts[I] << ")\n";
if (!DNN)
dbgs() << " isKnownNonNegative(" << *DstSubscripts[I] << ")\n";
if (!SLT)
dbgs() << " isKnownLessThan(" << *SrcSubscripts[I] << ", "
<< *Sizes[I - 1] << ")\n";
if (!DLT)
dbgs() << " isKnownLessThan(" << *DstSubscripts[I] << ", "
<< *Sizes[I - 1] << ")\n";
});
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not talking about parsing debug outputs. The point here is to prevent an increase in messy or unorganized logging.

After posting my earlier comment, I realized that these messages should probably be placed inside isKnownNonNegative and isKnownLessThan, respectively.

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.

5 participants