[llvm] [IfConversion] Fix duplicate successor error after if-converter. (PR #134485)

Afanasyev Ivan via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 5 01:02:28 PDT 2025


https://github.com/ivafanas created https://github.com/llvm/llvm-project/pull/134485

Bug is detected for `Simple` case when `FalseBB` is a successor of both blocks `BB` and `TrueBB`.

It is a mistake to add `FalseBB` as successor of `BB` after copy-and-merging of `TrueBB` into `BB`. `MachineVerifier` reports an error on duplicate successors and predecessors in this case.

Approximate CFG to trigger the bug:

```
    BB
   |  \       /
   |   \     /
   |    TrueBB
   |   /     \
   |  /       \
  FalseBB      ...
```

It is important that branch in `TrueBB` is **not analyzable**.

Bug is triggered on the custom out-of-tree backend. Unfortunately, I'm not familiar good enough with other backends to write a good .mir test for this with not analyzable branch. Sorry for that. But fix seems too trivial.

>From a9500e4e7b63c05b072bd70c31ccfcb694c93d4c Mon Sep 17 00:00:00 2001
From: Ivan Afanasyev <ivafanas at gmail.com>
Date: Sat, 5 Apr 2025 14:50:04 +0700
Subject: [PATCH] [IfConversion] Fix duplicate successor error after
 if-converter.

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

diff --git a/llvm/lib/CodeGen/IfConversion.cpp b/llvm/lib/CodeGen/IfConversion.cpp
index 400418e9331e6..0fe78d953ff17 100644
--- a/llvm/lib/CodeGen/IfConversion.cpp
+++ b/llvm/lib/CodeGen/IfConversion.cpp
@@ -2200,7 +2200,9 @@ void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
       // Fallthrough edge can't be transferred.
       if (Succ == FallThrough)
         continue;
-      ToBBI.BB->addSuccessor(Succ);
+
+      if (!ToBBI.BB->isSuccessor(Succ))
+        ToBBI.BB->addSuccessor(Succ);
     }
   }
 



More information about the llvm-commits mailing list