Skip to content

Conversation

@scui-ibm
Copy link
Contributor

The type sizes of backedge taken counts for two loops can be different and this is to fix the crash in haveSameSD (#165014).

@scui-ibm scui-ibm self-assigned this Oct 24, 2025
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Oct 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 24, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Shimin Cui (scui-ibm)

Changes

The type sizes of backedge taken counts for two loops can be different and this is to fix the crash in haveSameSD (#165014).


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

2 Files Affected:

  • (modified) llvm/lib/Analysis/DependenceAnalysis.cpp (+12-3)
  • (added) llvm/test/Analysis/DependenceAnalysis/same-sd-for-diff-becount-type-loops.ll (+68)
diff --git a/llvm/lib/Analysis/DependenceAnalysis.cpp b/llvm/lib/Analysis/DependenceAnalysis.cpp
index a572eefddd20e..3e9b9ee77640f 100644
--- a/llvm/lib/Analysis/DependenceAnalysis.cpp
+++ b/llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -1131,9 +1131,18 @@ bool DependenceInfo::haveSameSD(const Loop *SrcLoop,
   if (SE->hasLoopInvariantBackedgeTakenCount(DstLoop))
     DstUP = SE->getBackedgeTakenCount(DstLoop);
 
-  if (SrcUB != nullptr && DstUP != nullptr &&
-      SE->isKnownPredicate(ICmpInst::ICMP_EQ, SrcUB, DstUP))
-    return true;
+  if (SrcUB != nullptr && DstUP != nullptr) {
+    unsigned SrcBitWidth = SE->getTypeSizeInBits(SrcUB->getType());
+    unsigned DstBitWidth = SE->getTypeSizeInBits(DstUP->getType());
+    if (SrcBitWidth < DstBitWidth) {
+      SrcUB = SE->getZeroExtendExpr(SrcUB, DstUP->getType());
+    } else if (SrcBitWidth > DstBitWidth) {
+      DstUP = SE->getZeroExtendExpr(DstUP, SrcUB->getType());
+    }
+
+    if (SE->isKnownPredicate(ICmpInst::ICMP_EQ, SrcUB, DstUP))
+      return true;
+  }
 
   return false;
 }
diff --git a/llvm/test/Analysis/DependenceAnalysis/same-sd-for-diff-becount-type-loops.ll b/llvm/test/Analysis/DependenceAnalysis/same-sd-for-diff-becount-type-loops.ll
new file mode 100644
index 0000000000000..66880b5a553ec
--- /dev/null
+++ b/llvm/test/Analysis/DependenceAnalysis/same-sd-for-diff-becount-type-loops.ll
@@ -0,0 +1,68 @@
+; RUN: opt < %s -disable-output "-passes=print<da>" -aa-pipeline=basic-aa 2>&1 | FileCheck %s
+
+define void @f1() {
+; CHECK-LABEL: 'f1'
+; CHECK-NEXT:  Src:  store i32 0, ptr null, align 4 --> Dst:  store i32 0, ptr null, align 4
+; CHECK-NEXT:    da analyze - consistent output [S]!
+; CHECK-NEXT:  Src:  store i32 0, ptr null, align 4 --> Dst:  %2 = load i32, ptr null, align 4
+; CHECK-NEXT:    da analyze - consistent flow [|<]!
+; CHECK-NEXT:  Src:  %2 = load i32, ptr null, align 4 --> Dst:  %2 = load i32, ptr null, align 4
+; CHECK-NEXT:    da analyze - consistent input [S]!
+;
+entry:
+  br label %for.1.header
+
+for.1.header:                                     ; preds = %for.2.end, %entry
+  br label %for.1.body
+
+for.1.body:                                       ; preds = %for.1.body, %whiledo
+  %0 = phi i32 [ 0, %for.1.header ], [ 1, %for.1.body ]
+  store i32 0, ptr null, align 4
+  %1 = icmp ult i32 %0, 1
+  br i1 %1, label %for.1.body, label %for.1.end
+
+for.1.end:                                        ; preds = %for.1.body
+  br label %for.2.body
+
+for.2.body:                                       ; preds = %for.2.body, %for.1.end
+  %2 = load i32, ptr null, align 4
+  br i1 false, label %for.2.body, label %exit
+
+exit:                                             ; preds = %for.2.body
+  ret void
+}
+
+define void @f2() {
+; CHECK-LABEL: 'f2'
+; CHECK-NEXT:  Src:  store i32 0, ptr null, align 4 --> Dst:  store i32 0, ptr null, align 4
+; CHECK-NEXT:    da analyze - consistent output [S]!
+; CHECK-NEXT:  Src:  store i32 0, ptr null, align 4 --> Dst:  %3 = load i32, ptr null, align 4
+; CHECK-NEXT:    da analyze - flow [|<] / assuming 1 loop level(s) fused:  [S|<]!
+; CHECK-NEXT:  Src:  %3 = load i32, ptr null, align 4 --> Dst:  %3 = load i32, ptr null, align 4
+; CHECK-NEXT:    da analyze - consistent input [S]!
+;
+entry:
+  br label %for.1.header
+
+for.1.header:                                     ; preds = %for.2.end, %entry
+  br label %for.1.body
+
+for.1.body:                                       ; preds = %for.1.body, %whiledo
+  %0 = phi i32 [ 0, %for.1.header ], [ 1, %for.1.body ]
+  store i32 0, ptr null, align 4
+  %1 = icmp ult i32 %0, 1
+  br i1 %1, label %for.1.body, label %for.1.end
+
+for.1.end:                                        ; preds = %for.1.body
+  br label %for.2.body
+
+for.2.body:                                       ; preds = %for.2.body, %for.1.end
+  %2 = phi i64 [ 0, %for.1.end ], [ %4, %for.2.body ]
+  %3 = load i32, ptr null, align 4
+  %4 = add nuw nsw i64 %2, 1
+  %5 = icmp ult i64 %4, 2
+  br i1 %5, label %for.2.body, label %exit
+
+exit:                                             ; preds = %for.2.body
+  ret void
+}

SE->isKnownPredicate(ICmpInst::ICMP_EQ, SrcUB, DstUP))
return true;
if (SrcUB != nullptr && DstUP != nullptr) {
unsigned SrcBitWidth = SE->getTypeSizeInBits(SrcUB->getType());
Copy link
Contributor

Choose a reason for hiding this comment

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

Could use SE.getWiderType/getNoopOrZeroExtend

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 module @fhahn 's comment. Thanks.

@scui-ibm scui-ibm merged commit 616f3b5 into llvm:main Oct 27, 2025
10 checks passed
@scui-ibm scui-ibm deleted the same-sd-two-loops branch October 27, 2025 19:31
dvbuka pushed a commit to dvbuka/llvm-project that referenced this pull request Oct 27, 2025
…lvm#165021)

The type sizes of backedge taken counts for two loops can be different
and this is to fix the crash in haveSameSD
(llvm#165014).

---------

Co-authored-by: Shimin Cui <scui@xlperflep9.rtp.raleigh.ibm.com>
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