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

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 27 19:15:02 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Miguel Saldivar (Saldivarcher)

<details>
<summary>Changes</summary>

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

``````````

</details>


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


More information about the llvm-commits mailing list