Skip to content

Conversation

@Saldivarcher
Copy link
Contributor

If there is not an exit block, we should not try unify the loops. Instead we should just return.

Fixes #165252

If there is not an exit block, we should not try unify the loops.
Instead we should just return.

Fixes llvm#165252
@llvmbot
Copy link
Member

llvmbot commented Oct 28, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Miguel Saldivar (Saldivarcher)

Changes

If there is not an exit block, we should not try unify the loops. Instead we should just return.

Fixes #165252


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/UnifyLoopExits.cpp (+4)
  • (added) llvm/test/Transforms/Util/pr165252.ll (+15)
diff --git a/llvm/lib/Transforms/Utils/UnifyLoopExits.cpp b/llvm/lib/Transforms/Utils/UnifyLoopExits.cpp
index 9f338dbc78cff..1163bfa5b795d 100644
--- a/llvm/lib/Transforms/Utils/UnifyLoopExits.cpp
+++ b/llvm/lib/Transforms/Utils/UnifyLoopExits.cpp
@@ -150,6 +150,10 @@ static bool unifyLoopExits(DominatorTree &DT, LoopInfo &LI, Loop *L) {
   SmallVector<BasicBlock *, 8> ExitingBlocks;
   L->getExitingBlocks(ExitingBlocks);
 
+  // No exit blocks, so nothing to do. Just return.
+  if (ExitingBlocks.empty())
+    return false;
+
   // Redirect exiting edges through a control flow hub.
   ControlFlowHub CHub;
   for (auto *BB : ExitingBlocks) {
diff --git a/llvm/test/Transforms/Util/pr165252.ll b/llvm/test/Transforms/Util/pr165252.ll
new file mode 100644
index 0000000000000..bab1a23c6ce57
--- /dev/null
+++ b/llvm/test/Transforms/Util/pr165252.ll
@@ -0,0 +1,15 @@
+; REQUIRES: asserts
+; RUN: opt -passes=unify-loop-exits -S %s
+
+define void @test() {
+entry:
+  br i1 true, label %end, label %Loop
+
+Loop:
+  %V = phi i32 [0, %entry], [%V1, %Loop]
+  %V1 = add i32 %V, 1
+  br label %Loop
+
+end:
+  ret void
+}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unify-loop-exits pass: Assertion `!empty() && "Cannot call front() on empty SetVector!"' failed.

2 participants