[llvm] [LV] - Fix crash in epilogue vectorization when AnyOf reduction has no ComputeReductionResult. (PR #213632)
Luke Lau via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 6 03:04:05 PDT 2026
================
@@ -7624,14 +7624,19 @@ static SmallVector<Instruction *> preparePlanForEpilogueVectorLoop(
// TODO: Move setting of resume values to prepareToExecute.
if (auto *ReductionPhi = dyn_cast<VPReductionPHIRecipe>(&R)) {
// Find the reduction result by searching users of the phi or its backedge
- // value.
+ // value, looking through intermediate recipes.
auto IsReductionResult = [](VPRecipeBase *R) {
auto *VPI = dyn_cast<VPInstruction>(R);
return VPI && VPI->getOpcode() == VPInstruction::ComputeReductionResult;
};
- auto *RdxResult = cast<VPInstruction>(
- vputils::findRecipe(ReductionPhi->getBackedgeValue(), IsReductionResult));
- assert(RdxResult && "expected to find reduction result");
+ auto *RdxResult = dyn_cast_or_null<VPInstruction>(vputils::findRecipe(
+ ReductionPhi->getBackedgeValue(), IsReductionResult));
+ // If the ComputeReductionResult was optimized away (e.g., the exit value
+ // was simplified to the start value), the reduction does not contribute
+ // to the exit value. Skip updating the start value for the epilogue,
+ // keeping it as the identity value.
+ if (!RdxResult)
+ continue;
----------------
lukel97 wrote:
I don't think we should try to delete the dead cycles in preparePlanForEpilogueVectorLoop, if anything we would want to do it in removeDeadRecipes. We need to add a test case for predicated reductions, something like this
```llvm
define i32 @anyof_blend_chain_epilogue(i1 %c0, i1 %c1, i32 %n) {
entry:
br label %loop.header
loop.header:
%iv = phi i32 [ 0, %entry ], [ %iv.next, %loop.latch ]
%rdx = phi i32 [ 0, %entry ], [ %rdx.next, %loop.latch ]
br i1 %c0, label %loop.latch, label %if.outer
if.outer:
br i1 %c1, label %if.merge, label %if.inner
if.inner:
%c = icmp eq i32 %iv, 0
%sel = select i1 %c, i32 0, i32 %rdx
br label %if.merge
if.merge:
%blend1 = phi i32 [ %sel, %if.inner ], [ %rdx, %if.outer ]
br label %loop.latch
loop.latch:
%rdx.next = phi i32 [ %blend1, %if.merge ], [ %rdx, %loop.header ]
%iv.next = add i32 %iv, 1
%ec = icmp slt i32 %iv.next, %n
br i1 %ec, label %loop.header, label %exit
exit:
ret i32 %rdx.next
}
```
https://github.com/llvm/llvm-project/pull/213632
More information about the llvm-commits
mailing list