[llvm] [VPlan] Collect FOR PHIs before sinking/hoisting recurrence users (PR #194671)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 28 09:29:28 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-vectorizers

@llvm/pr-subscribers-llvm-transforms

Author: Elio (xiongzile)

<details>
<summary>Changes</summary>

Avoid iterating over HeaderVPBB->phis() while potentially mutating the underlying VPBasicBlock. Collect all VPFirstOrderRecurrencePHIRecipe instances first, then process them in a separate loop.

This prevents iterator invalidation when sinking or hoisting recurrence users, and makes the transformation more robust.

Fixes: https://github.com/llvm/llvm-project/issues/194618

---
Full diff: https://github.com/llvm/llvm-project/pull/194671.diff


1 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp (+6-4) 


``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
index 345bb2ef27a4f..391be24effd78 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
@@ -869,11 +869,13 @@ static bool hoistPreviousBeforeFORUsers(VPFirstOrderRecurrencePHIRecipe *FOR,
 /// fails.
 static bool tryToSinkOrHoistRecurrenceUsers(VPBasicBlock *HeaderVPBB,
                                             VPDominatorTree &VPDT) {
-  for (VPRecipeBase &R : HeaderVPBB->phis()) {
-    auto *FOR = dyn_cast<VPFirstOrderRecurrencePHIRecipe>(&R);
-    if (!FOR)
-      continue;
+  SmallVector<VPFirstOrderRecurrencePHIRecipe *> FORs;
+
+  for (VPRecipeBase &R : HeaderVPBB->phis())
+    if (auto *FOR = dyn_cast<VPFirstOrderRecurrencePHIRecipe>(&R))
+      FORs.push_back(FOR);
 
+  for (VPFirstOrderRecurrencePHIRecipe *FOR : FORs) {
     // Follow through FOR phi chains to find the actual Previous recipe.
     // Fixed-order recurrences do not contain cycles, so this loop is
     // guaranteed to terminate.

``````````

</details>


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


More information about the llvm-commits mailing list