Bug type: crash
Component: LoopVectorize
Bug location: llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp: VPlanTransforms::handleFindLastReductions
Issue title: [LV] crash with PGO
Labels: vectorizers
crash
Reported by: Andarwinux

[test.zip](https://github.com/user-attachments/files/24619392/test.zip)

```
clang -O3 -c mov.i -fprofile-use=pgo.profdata 
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /build/install/llvmbin/clang -O3 -c mov.i -fprofile-use=pgo.profdata
1.      <eof> parser at end of file
2.      Optimizer
3.      Running pass "function<eager-inv>(drop-unnecessary-assumes,float2int,lower-constant-intrinsics,chr,loop(loop-rotate<header-duplication;no-prepare-for-lto>,loop-deletion),loop-distribute,inject-tli-mappings,loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>,drop-unnecessary-assumes,infer-alignment,loop-load-elim,instcombine<max-iterations=1;no-verify-fixpoint>,simplifycfg<bonus-inst-threshold=1;forward-switch-cond;switch-range-to-icmp;switch-to-arithmetic;switch-to-lookup;no-keep-loops;hoist-common-insts;no-hoist-loads-stores-with-cond-faulting;sink-common-insts;speculate-blocks;simplify-cond-branch;no-speculate-unpredictables>,slp-vectorizer,vector-combine,instcombine<max-iterations=1;no-verify-fixpoint>,loop-unroll<O3>,transform-warning,sroa<preserve-cfg>,infer-alignment,instcombine<max-iterations=1;no-verify-fixpoint>,loop-mssa(licm<allowspeculation>),alignment-from-assumptions,loop-sink,instsimplify,div-rem-pairs,tailcallelim,simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;switch-to-arithmetic;no-switch-to-lookup;keep-loops;no-hoist-common-insts;hoist-loads-stores-with-cond-faulting;no-sink-common-insts;speculate-blocks;simplify-cond-branch;speculate-unpredictables>)" on module "src/libavformat/mov.c"
4.      Running pass "loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>" on function "mov_read_header"
#0 0x00005ce47e8a4eee PrintStackTraceSignalHandler(void*) (.llvm.14462126317361604948) Signals.cpp:0:0
```

Comments: author: Andarwinux
body: [mov.ll.txt](https://github.com/user-attachments/files/24631971/mov.ll.txt)

```
clang -c -O3 -x ir mov.ll.txt

PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /build/install/llvmbin/clang -c -O3 -x ir mov.ll.txt
1.      Optimizer
2.      Running pass "function<eager-inv>(drop-unnecessary-assumes,float2int,lower-constant-intrinsics,chr,loop(loop-rotate<header-duplication;no-prepare-for-lto>,loop-deletion),loop-distribute,inject-tli-mappings,loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>,drop-unnecessary-assumes,infer-alignment,loop-load-elim,instcombine<max-iterations=1;no-verify-fixpoint>,simplifycfg<bonus-inst-threshold=1;forward-switch-cond;switch-range-to-icmp;switch-to-arithmetic;switch-to-lookup;no-keep-loops;hoist-common-insts;no-hoist-loads-stores-with-cond-faulting;sink-common-insts;speculate-blocks;simplify-cond-branch;no-speculate-unpredictables>,slp-vectorizer,vector-combine,instcombine<max-iterations=1;no-verify-fixpoint>,loop-unroll<O3>,transform-warning,sroa<preserve-cfg>,infer-alignment,instcombine<max-iterations=1;no-verify-fixpoint>,loop-mssa(licm<allowspeculation>),alignment-from-assumptions,loop-sink,instsimplify,div-rem-pairs,tailcallelim,simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;switch-to-arithmetic;no-switch-to-lookup;keep-loops;no-hoist-common-insts;hoist-loads-stores-with-cond-faulting;no-sink-common-insts;speculate-blocks;simplify-cond-branch;speculate-unpredictables>)" on module "mov.ll.txt"
3.      Running pass "loop-vectorize<no-interleave-forced-only;no-vectorize-forced-only;>" on function "mov_read_header"
#0 0x00005d671aee592e PrintStackTraceSignalHandler(void*) (.llvm.14462126317361604948) Signals.cpp:0:0
```
author: Andarwinux
body: 2abd6d6d7ac54d40302dfc946d01572a12963ee5 @huntergr-arm

Fix patch:
commit 808a6ba345ce6e898e9acc4c5eda60165806e967
Author: Florian Hahn <flo@fhahn.com>
Date:   Thu Jan 15 09:06:49 2026 +0000

    [VPlan] Bail out when rdx result cannot be found in handleFindLast.
    
    Turn assertion from 2abd6d6d7ac
    (https://github.com/llvm/llvm-project/pull/158088) into a bail
    out to prevent crash when tail-folding.
    
    Fixes https://github.com/llvm/llvm-project/issues/175990.

diff --git a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
index 7ee133545eeb..aa3437dfb8a9 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
@@ -1361,7 +1361,9 @@ bool VPlanTransforms::handleFindLastReductions(VPlan &Plan) {
     // extract.last.active intrinsic.
     auto *RdxResult =
         findUserOf<VPInstruction::ComputeReductionResult>(DataSelect);
-    assert(RdxResult && "Unable to find reduction result recipe");
+    // TODO: Handle tail-folding.
+    if (!RdxResult)
+      return false;
     Builder.setInsertPoint(RdxResult);
     auto *ExtractLastActive =
         Builder.createNaryOp(VPInstruction::ExtractLastActive,


Regression tests:
file: llvm/test/Transforms/LoopVectorize/iv-select-cmp-fold-tail.ll
commands: opt -p loop-vectorize -prefer-predicate-over-epilogue=predicate-dont-vectorize -force-vector-width=4 -S %s
tests: test_name: <module>
test_body: 
define i32 @find_last_trunc_iv(ptr %src, i64 %n) {
;
entry:
  br label %loop

loop:
  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
  %rdx = phi i32 [ 0, %entry ], [ %rdx.next, %loop ]
  %gep.src = getelementptr inbounds i32, ptr %src, i64 %iv
  %l = load i32, ptr %gep.src
  %cmp103 = icmp eq i32 %l, 0
  %0 = trunc i64 %iv to i32
  %rdx.next = select i1 %cmp103, i32 %0, i32 %rdx
  %iv.next = add i64 %iv, 1
  %ec = icmp eq i64 %iv, %n
  br i1 %ec, label %exit, label %loop

exit:
  ret i32 %rdx.next
}