[llvm] [SimplifyCFG] Fix `SimplifyCFG` pass to skip folding when both blocks contain convergence intrinsics. (PR #166452)

Steven Perron via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 19 07:18:32 PST 2025


================
@@ -230,6 +230,15 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU,
   // Don't break self-loops.
   if (PredBB == BB) return false;
 
+  // Don't break if both the basic block and the predecessor contain convergent
+  // intrinsics.
+  for (Instruction &I : *BB)
+    if (isa<ConvergenceControlInst>(I)) {
+      for (Instruction &I : *PredBB)
+        if (isa<ConvergenceControlInst>(I))
+          return false;
+    }
----------------
s-perron wrote:

This is a move expensive check because it has to traverse potentially two basic blocks. We should move it later. I would make it the last check before deciding to merge.

Also the way this is written is a little unclear. First if we know that there can be at most one convergence token per basic block, we could make this two separate checks and take advantage of short-circuit evaluation:

```
bool HasConvergenceToken(const BasicBlock *BB) {                                                                                                                                                                                                                                               
  for (const Instruction &I : *BB)                                                                                                                                                                                                                                                             
    if (isa<ConvergenceControlInst>(I))                                                                                                                                                                                                                                                        
      return true;                                                                                                                                                                                                                                                                             
  return false;                                                                                                                                                                                                                                                                                
} 
```

Then the check becomes:

```
if (HasConvergenceToken(BB) && HasConvervenceToken(PredBB))
  return false; 
```

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


More information about the llvm-commits mailing list