[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:28:47 PDT 2026
https://github.com/xiongzile created https://github.com/llvm/llvm-project/pull/194671
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
>From 55c4c92da9b1632a395e23c1cd0f5b295bb97d88 Mon Sep 17 00:00:00 2001
From: Zile Xiong <xiongzile99 at gmail.com>
Date: Wed, 29 Apr 2026 00:25:23 +0800
Subject: [PATCH] [VPlan] Collect FOR PHIs before sinking/hoisting recurrence
users
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
---
llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
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.
More information about the llvm-commits
mailing list