[PATCH] D119261: [DependenceAnalysis][PR52170] Conservative crash on overflowed loop backedge

Artem Radzikhovskyy via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 11 12:30:35 PST 2022


artemrad updated this revision to Diff 407989.
artemrad edited the summary of this revision.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D119261/new/

https://reviews.llvm.org/D119261

Files:
  llvm/lib/Analysis/DependenceAnalysis.cpp
  llvm/test/Analysis/DependenceAnalysis/TripCountOverflow.ll


Index: llvm/test/Analysis/DependenceAnalysis/TripCountOverflow.ll
===================================================================
--- /dev/null
+++ llvm/test/Analysis/DependenceAnalysis/TripCountOverflow.ll
@@ -0,0 +1,41 @@
+; RUN: opt %s -disable-output "-passes=print<da>" 2>&1 | FileCheck %s
+
+; [PR52170]
+; Test that in cases where SIV propagation does not produce a linear equation
+; (e.g. the trip count of the post propagation SCEV can overflow) produce a
+; conservative answer - don't crash.
+; CHECK-LABEL: dynamic_trip_count
+; CHECK: da analyze - output [* S]!
+; CHECK: da analyze - output [* *|<]!
+; CHECK: da analyze - output [* *]!
+
+define void @dynamic_trip_count(i64 %n) {
+entry:
+  %array = alloca [2 x [5 x i32]], align 1
+  br label %for.outer
+
+for.outer:
+  %i = phi i32 [ 0, %entry ], [ %i.add, %cleanup ]
+  %i.trunc = trunc i32 %i to i16
+  %i.trunc.3 = add i16 %i.trunc, 3
+  %i.ptr = getelementptr inbounds [2 x [5 x i32]], [2 x [5 x i32]]* %array, i16 0, i16 %i.trunc, i16 %i.trunc.3
+  br label %for.inner
+
+for.inner:
+  %j = phi i32 [ 0, %for.outer ], [ %j.1, %for.inner ]
+  store i32 undef, i32* %i.ptr, align 1
+  %j.trunc = trunc i32 %j to i16
+  %j.ptr = getelementptr inbounds [2 x [5 x i32]], [2 x [5 x i32]]* %array, i16 0, i16 %j.trunc, i16 %i.trunc.3
+  store i32 0, i32* %j.ptr, align 1
+  %j.1 = add nuw nsw i32 %j, 1
+  br i1 false, label %for.inner, label %cleanup
+
+cleanup:
+  %i.add = add nuw nsw i32 %i, 1
+  %i.add.extend = sext i32 %i.add to i64
+  %cmp = icmp slt i64 %i.add.extend, %n
+  br i1 %cmp, label %for.outer, label %other
+
+other:
+  ret void
+}
Index: llvm/lib/Analysis/DependenceAnalysis.cpp
===================================================================
--- llvm/lib/Analysis/DependenceAnalysis.cpp
+++ llvm/lib/Analysis/DependenceAnalysis.cpp
@@ -899,14 +899,7 @@
     return isLoopInvariant(Expr, LoopNest);
   const SCEV *Start = AddRec->getStart();
   const SCEV *Step = AddRec->getStepRecurrence(*SE);
-  const SCEV *UB = SE->getBackedgeTakenCount(AddRec->getLoop());
-  if (!isa<SCEVCouldNotCompute>(UB)) {
-    if (SE->getTypeSizeInBits(Start->getType()) <
-        SE->getTypeSizeInBits(UB->getType())) {
-      if (!AddRec->getNoWrapFlags())
-        return false;
-    }
-  }
+
   if (!isLoopInvariant(Step, LoopNest))
     return false;
   if (IsSrc)
@@ -3296,12 +3289,12 @@
 
   int Size = SrcSubscripts.size();
   LLVM_DEBUG({
-    dbgs() << "\nSrcSubscripts: ";
+    dbgs() << "SrcSubscripts:\n";
     for (int I = 0; I < Size; I++)
-      dbgs() << *SrcSubscripts[I];
-    dbgs() << "\nDstSubscripts: ";
+      dbgs() << "\t" << *SrcSubscripts[I] << "\n";
+    dbgs() << "DstSubscripts:\n";
     for (int I = 0; I < Size; I++)
-      dbgs() << *DstSubscripts[I];
+      dbgs() << "\t" << *DstSubscripts[I] << "\n";
   });
 
   // The delinearization transforms a single-subscript MIV dependence test into


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119261.407989.patch
Type: text/x-patch
Size: 2906 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220211/d2e3d789/attachment.bin>


More information about the llvm-commits mailing list