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

Alexander Kornienko via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 9 23:09:28 PDT 2026


https://github.com/alexfh 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 1/4] [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

>From 13a970ce2459ccad69ab506eed6361ecc9f7934f Mon Sep 17 00:00:00 2001
From: Pranav Kant <prka at google.com>
Date: Fri, 7 Aug 2026 15:03:31 -0700
Subject: [PATCH 2/4] Add a unit test

---
 llvm/unittests/Analysis/LoopInfoTest.cpp | 42 ++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/llvm/unittests/Analysis/LoopInfoTest.cpp b/llvm/unittests/Analysis/LoopInfoTest.cpp
index 5321f72aa1784..35f8f370a8daa 100644
--- a/llvm/unittests/Analysis/LoopInfoTest.cpp
+++ b/llvm/unittests/Analysis/LoopInfoTest.cpp
@@ -1646,3 +1646,45 @@ TEST(LoopInfoTest, TokenLCSSA) {
         InnerLoop->isRecursivelyLCSSAForm(DT, LI, /*IgnoreTokens*/ false));
   });
 }
+
+TEST(LoopInfoTest, UnreachableBlock) {
+  const char *ModuleStr =
+      "define void @irreducible_loop(i1 %c1, i1 %c2) {\n"
+      "dummy:\n"
+      "  br label %entry\n"
+      "entry:\n"
+      "  br i1 %c1, label %loop1, label %side_entry\n"
+      "dead_block:\n"
+      "  br label %latch1\n"
+      "loop1:\n"
+      "  br i1 %c2, label %body1, label %mid\n"
+      "body1:\n"
+      "  br label %latch1\n"
+      "latch1:\n"
+      "  br label %loop1\n"
+      "mid:\n"
+      "  br label %latch2\n"
+      "latch2:\n"
+      "  br label %loop1\n"
+      "side_entry:\n"
+      "  br label %mid\n"
+      "}\n";
+
+  LLVMContext Context;
+  SMDiagnostic Err;
+  std::unique_ptr<Module> M = parseAssemblyString(ModuleStr, Err, Context);
+  ASSERT_TRUE(M);
+  Function *F = M->getFunction("irreducible_loop");
+  ASSERT_TRUE(F);
+
+  // Delete 'dummy' block so that block 0 is deleted and no block in the CFG has index 0.
+  BasicBlock *Dummy = &F->getEntryBlock();
+  Dummy->eraseFromParent();
+
+  DominatorTree DT(*F);
+  // This used to hang infinitely in analyze() due to an unvisited block reaching a latch.
+  LoopInfo LI(DT);
+
+  // Basic verification that a loop was found.
+  EXPECT_FALSE(LI.empty());
+}

>From 7937f83e4977701f027eaca1c8ad746a6b034cf3 Mon Sep 17 00:00:00 2001
From: Pranav Kant <prka at google.com>
Date: Fri, 7 Aug 2026 15:09:19 -0700
Subject: [PATCH 3/4] clang-format

---
 llvm/unittests/Analysis/LoopInfoTest.cpp | 47 ++++++++++++------------
 1 file changed, 24 insertions(+), 23 deletions(-)

diff --git a/llvm/unittests/Analysis/LoopInfoTest.cpp b/llvm/unittests/Analysis/LoopInfoTest.cpp
index 35f8f370a8daa..b909e434f7a5f 100644
--- a/llvm/unittests/Analysis/LoopInfoTest.cpp
+++ b/llvm/unittests/Analysis/LoopInfoTest.cpp
@@ -1648,27 +1648,26 @@ TEST(LoopInfoTest, TokenLCSSA) {
 }
 
 TEST(LoopInfoTest, UnreachableBlock) {
-  const char *ModuleStr =
-      "define void @irreducible_loop(i1 %c1, i1 %c2) {\n"
-      "dummy:\n"
-      "  br label %entry\n"
-      "entry:\n"
-      "  br i1 %c1, label %loop1, label %side_entry\n"
-      "dead_block:\n"
-      "  br label %latch1\n"
-      "loop1:\n"
-      "  br i1 %c2, label %body1, label %mid\n"
-      "body1:\n"
-      "  br label %latch1\n"
-      "latch1:\n"
-      "  br label %loop1\n"
-      "mid:\n"
-      "  br label %latch2\n"
-      "latch2:\n"
-      "  br label %loop1\n"
-      "side_entry:\n"
-      "  br label %mid\n"
-      "}\n";
+  const char *ModuleStr = "define void @irreducible_loop(i1 %c1, i1 %c2) {\n"
+                          "dummy:\n"
+                          "  br label %entry\n"
+                          "entry:\n"
+                          "  br i1 %c1, label %loop1, label %side_entry\n"
+                          "dead_block:\n"
+                          "  br label %latch1\n"
+                          "loop1:\n"
+                          "  br i1 %c2, label %body1, label %mid\n"
+                          "body1:\n"
+                          "  br label %latch1\n"
+                          "latch1:\n"
+                          "  br label %loop1\n"
+                          "mid:\n"
+                          "  br label %latch2\n"
+                          "latch2:\n"
+                          "  br label %loop1\n"
+                          "side_entry:\n"
+                          "  br label %mid\n"
+                          "}\n";
 
   LLVMContext Context;
   SMDiagnostic Err;
@@ -1677,12 +1676,14 @@ TEST(LoopInfoTest, UnreachableBlock) {
   Function *F = M->getFunction("irreducible_loop");
   ASSERT_TRUE(F);
 
-  // Delete 'dummy' block so that block 0 is deleted and no block in the CFG has index 0.
+  // Delete 'dummy' block so that block 0 is deleted and no block in the CFG has
+  // index 0.
   BasicBlock *Dummy = &F->getEntryBlock();
   Dummy->eraseFromParent();
 
   DominatorTree DT(*F);
-  // This used to hang infinitely in analyze() due to an unvisited block reaching a latch.
+  // This used to hang infinitely in analyze() due to an unvisited block
+  // reaching a latch.
   LoopInfo LI(DT);
 
   // Basic verification that a loop was found.

>From db26f51b721d5d288453903cbc7ab006e435a4b1 Mon Sep 17 00:00:00 2001
From: Alexander Kornienko <alexfh at google.com>
Date: Mon, 10 Aug 2026 08:09:18 +0200
Subject: [PATCH 4/4] Fix whitespace and punctuation in a comment.

Co-authored-by: Alexis Engelke <mail at aengelke.net>
---
 llvm/include/llvm/Support/GenericLoopInfoImpl.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Support/GenericLoopInfoImpl.h b/llvm/include/llvm/Support/GenericLoopInfoImpl.h
index d73fed2b28636..8cc0ce82c83ec 100644
--- a/llvm/include/llvm/Support/GenericLoopInfoImpl.h
+++ b/llvm/include/llvm/Support/GenericLoopInfoImpl.h
@@ -664,7 +664,7 @@ 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]))
-          //  Do not enqueue any unreachable nodes
+          // Do not enqueue any unreachable nodes.
           if (Blocks[num(Pred)])
             enqueue(Pred);
       // Without a backedge the header forms no loop at all.



More information about the llvm-commits mailing list