[llvm] 4c1f74a - [LoopFlatten] Fix invalid assertion (PR49571)

Ta-Wei Tu via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 24 03:08:34 PDT 2021


Author: Ta-Wei Tu
Date: 2021-03-24T18:08:27+08:00
New Revision: 4c1f74a76ce8c0e4ee2f04faacd561be7a76cba8

URL: https://github.com/llvm/llvm-project/commit/4c1f74a76ce8c0e4ee2f04faacd561be7a76cba8
DIFF: https://github.com/llvm/llvm-project/commit/4c1f74a76ce8c0e4ee2f04faacd561be7a76cba8.diff

LOG: [LoopFlatten] Fix invalid assertion (PR49571)

The `InductionPHI` is not necessarily the increment instruction, as
demonstrated in pr49571.ll.
This patch removes the assertion and instead bails out from the
`LoopFlatten` pass if that happens.

This fixes https://bugs.llvm.org/show_bug.cgi?id=49571

Reviewed By: SjoerdMeijer

Differential Revision: https://reviews.llvm.org/D99252

Added: 
    llvm/test/Transforms/LoopFlatten/pr49571.ll

Modified: 
    llvm/lib/Transforms/Scalar/LoopFlatten.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/LoopFlatten.cpp b/llvm/lib/Transforms/Scalar/LoopFlatten.cpp
index fa2e46b1aba8..319d9b4c752a 100644
--- a/llvm/lib/Transforms/Scalar/LoopFlatten.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopFlatten.cpp
@@ -176,8 +176,11 @@ static bool findLoopComponents(
   LLVM_DEBUG(dbgs() << "Found limit: "; Limit->dump());
 
   assert(InductionPHI->getNumIncomingValues() == 2);
-  assert(InductionPHI->getIncomingValueForBlock(Latch) == Increment &&
-         "PHI value is not increment inst");
+
+  if (InductionPHI->getIncomingValueForBlock(Latch) != Increment) {
+    LLVM_DEBUG(dbgs() << "PHI value is not increment inst");
+    return false;
+  }
 
   auto *CI = dyn_cast<ConstantInt>(
       InductionPHI->getIncomingValueForBlock(L->getLoopPreheader()));

diff  --git a/llvm/test/Transforms/LoopFlatten/pr49571.ll b/llvm/test/Transforms/LoopFlatten/pr49571.ll
new file mode 100644
index 000000000000..afd1fb2d8ded
--- /dev/null
+++ b/llvm/test/Transforms/LoopFlatten/pr49571.ll
@@ -0,0 +1,24 @@
+; RUN: opt < %s -S -loop-flatten -verify-loop-info -verify-dom-info -verify-scev -verify | FileCheck %s
+
+; CHECK-LABEL: @main
+
+define dso_local void @main() {
+entry:
+  br label %for.cond
+
+for.cond:                                         ; preds = %for.end, %entry
+  br label %for.body
+
+for.body:                                         ; preds = %for.inc, %for.cond
+  %a.03 = phi i32 [ 0, %for.cond ], [ %inc, %for.inc ]
+  br label %for.inc
+
+for.inc:                                          ; preds = %for.body
+  %0 = add i32 %a.03, 1
+  %cmp = icmp slt i32 %0, 10
+  %inc = add nsw i32 %a.03, 1
+  br i1 %cmp, label %for.body, label %for.end
+
+for.end:                                          ; preds = %for.inc
+  br label %for.cond
+}


        


More information about the llvm-commits mailing list