[llvm] [llvm][UnifyLoopExits] Avoid optimization if no exit block is found (PR #165343)

Miguel Saldivar via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 27 19:14:24 PDT 2025


https://github.com/Saldivarcher created https://github.com/llvm/llvm-project/pull/165343

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

Fixes #165252

>From 66e4ab23c82aa48fe9b863f59f6bd5a1e19eb6ef Mon Sep 17 00:00:00 2001
From: Miguel Saldivar <saldivarcher at gmail.com>
Date: Mon, 27 Oct 2025 19:10:38 -0700
Subject: [PATCH] [llvm][UnifyLoopExits] Avoid optimization if no exit block is
 found

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

Fixes #165252
---
 llvm/lib/Transforms/Utils/UnifyLoopExits.cpp |  4 ++++
 llvm/test/Transforms/Util/pr165252.ll        | 15 +++++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 llvm/test/Transforms/Util/pr165252.ll

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
+}



More information about the llvm-commits mailing list