[llvm] Don't collect from phi nodes which are not fully constructed yet (PR #140806)

Manuel Drehwald via llvm-commits llvm-commits at lists.llvm.org
Tue May 20 14:26:46 PDT 2025


https://github.com/ZuseZ4 created https://github.com/llvm/llvm-project/pull/140806

This fixes two issues reported for Enzyme+Rust in https://github.com/EnzymeAD/rust/issues/196,
based on help by @wsmoses 

I have minimized one of the issues into a minimal IR reproducer, which crashes Enzyme when it calls llvm's scev.
https://github.com/EnzymeAD/Enzyme/issues/2310#issuecomment-2869004751

However, I'm unsure how to make a unit test for it, since it seems like opt seems to automatically verify input while parsing.

So if I either remove a branch towards a bb with a phi node (but not the corresponding entry from the phi node), or vice versa, I get

> PHINode should have one entry for each predecessor of its parent basic block!

cc @nikic since we discussed it last week

>From e6e051da4aec5aba8bef85a4bd6a0bf1a97e64fc Mon Sep 17 00:00:00 2001
From: Manuel Drehwald <git at manuel.drehwald.info>
Date: Tue, 20 May 2025 16:43:00 -0400
Subject: [PATCH] Don't collect from phi nodes which are not fully constructed
 yet

---
 llvm/lib/Analysis/ScalarEvolution.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 5e01c29615fab..9a35eb1312df0 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -15823,7 +15823,9 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
       Depth < MaxLoopGuardCollectionDepth) {
     SmallDenseMap<const BasicBlock *, LoopGuards> IncomingGuards;
     for (auto &Phi : Pair.second->phis())
-      collectFromPHI(SE, Guards, Phi, VisitedBlocks, IncomingGuards, Depth);
+      // We don't collect from PHIs that are under construction.
+      if (Pair.second->hasNPredecessors(Phi.getNumIncomingValues()))
+        collectFromPHI(SE, Guards, Phi, VisitedBlocks, IncomingGuards, Depth);
   }
 
   // Now apply the information from the collected conditions to



More information about the llvm-commits mailing list