[llvm] [LoopInfo] Do not enqueue unreachable nodes; fix infinite loop (PR #214832)

Pranav Kant via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 7 14:52:33 PDT 2026


https://github.com/pranavk updated https://github.com/llvm/llvm-project/pull/214832

>From 6894c5d52e82baf31359b28bc2920f28f2f4e455 Mon Sep 17 00:00:00 2001
From: Pranav Kant <prka at google.com>
Date: Fri, 7 Aug 2026 10:36:43 -0700
Subject: [PATCH] [LoopInfo] Do not enqueue unreachable nodes; fix infinite
 loop

Before this patch, we were enqueing unreachable nodes reaching into
the latch nodes. Since unreachable nodes haven't been visited in the
original DFS algorithm, we end up in an infinite loop within enqueue

This patch restores isReachableFromEntry check from now removed discoverAndMapSubloop.
This should avoid infinite loop.

It's a regression from #212000 (5d582e4003b1cab59900d16fa3db9c3a9d16b462)
---
 llvm/include/llvm/Support/GenericLoopInfoImpl.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Support/GenericLoopInfoImpl.h b/llvm/include/llvm/Support/GenericLoopInfoImpl.h
index c294583eb77fd..d73fed2b28636 100644
--- a/llvm/include/llvm/Support/GenericLoopInfoImpl.h
+++ b/llvm/include/llvm/Support/GenericLoopInfoImpl.h
@@ -664,7 +664,9 @@ void LoopInfoBase<BlockT, LoopT>::analyze(
       // Whatever reaches a latch without passing the header is in the loop.
       for (unsigned I = 0; I != Worklist.size(); ++I)
         for (BlockT *Pred : inverse_children<BlockT *>(Worklist[I]))
-          enqueue(Pred);
+          //  Do not enqueue any unreachable nodes
+          if (Blocks[num(Pred)])
+            enqueue(Pred);
       // Without a backedge the header forms no loop at all.
       Info[H].Pos = HasBackedge ? IsHeader : OffPath;
       // Partition the header's blocks: the loop keeps the ones the traversal



More information about the llvm-commits mailing list