[llvm] [VPlan] Handle FindLast in VPIRFlags::printFlags (PR #185857)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 11 04:15:23 PDT 2026


https://github.com/lukel97 updated https://github.com/llvm/llvm-project/pull/185857

>From 7cb5fc631323a350ea38e6bf51423e6500804641 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Wed, 11 Mar 2026 19:00:47 +0800
Subject: [PATCH 1/2] [VPlan] Handle FindLast in VPIRFlags::printFlags

Noticed this when -vplan-print-after-all crashed on a find-last reduction. We don't yet return an opcode for it because there's no in-loop reduction.

I've also added FindIV but couldn't find a way to test it because no recipe is ever created with it for a OperationType::ReductionOp flag. It's used in VPWidenReductionPHI, but only stored in the RecurKind field and not printed
---
 .../lib/Transforms/Vectorize/VPlanRecipes.cpp |  6 ++
 .../LoopVectorize/VPlan/find-last.ll          | 91 +++++++++++++++++++
 2 files changed, 97 insertions(+)
 create mode 100644 llvm/test/Transforms/LoopVectorize/VPlan/find-last.ll

diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index d149723e11fb6..ce5d3d6662964 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -2248,6 +2248,12 @@ void VPIRFlags::printFlags(raw_ostream &O) const {
     case RecurKind::AnyOf:
       O << "any-of";
       break;
+    case RecurKind::FindIV:
+      O << "find-iv";
+      break;
+    case RecurKind::FindLast:
+      O << "find-last";
+      break;
     case RecurKind::SMax:
       O << "smax";
       break;
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/find-last.ll b/llvm/test/Transforms/LoopVectorize/VPlan/find-last.ll
new file mode 100644
index 0000000000000..15beb9ef83842
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/find-last.ll
@@ -0,0 +1,91 @@
+; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -disable-output %s -p loop-vectorize -force-vector-width=4 -vplan-print-after=optimizeFindIVReductions 2>&1 | FileCheck %s
+
+define i32 @find_last_with_select(ptr noalias %a, ptr noalias %b) {
+; CHECK-LABEL: VPlan for loop in 'find_last_with_select'
+; CHECK:  VPlan ' for UF>=1' {
+; CHECK-NEXT:  Live-in vp<[[VP0:%[0-9]+]]> = VF
+; CHECK-NEXT:  Live-in vp<[[VP1:%[0-9]+]]> = VF * UF
+; CHECK-NEXT:  Live-in vp<[[VP2:%[0-9]+]]> = vector-trip-count
+; CHECK-NEXT:  Live-in ir<500> = original trip-count
+; CHECK-EMPTY:
+; CHECK-NEXT:  ir-bb<entry>:
+; CHECK-NEXT:  Successor(s): scalar.ph, vector.ph
+; CHECK-EMPTY:
+; CHECK-NEXT:  vector.ph:
+; CHECK-NEXT:  Successor(s): vector loop
+; CHECK-EMPTY:
+; CHECK-NEXT:  <x1> vector loop: {
+; CHECK-NEXT:    vector.body:
+; CHECK-NEXT:      EMIT vp<[[VP3:%[0-9]+]]> = CANONICAL-INDUCTION ir<0>, vp<%index.next>
+; CHECK-NEXT:      ir<%iv> = WIDEN-INDUCTION nuw nsw ir<0>, ir<1>, vp<[[VP0]]>
+; CHECK-NEXT:      WIDEN-REDUCTION-PHI ir<%rdx> = phi ir<1>, ir<%sel>
+; CHECK-NEXT:      CLONE ir<%gep.a> = getelementptr inbounds ir<%a>, ir<%iv>
+; CHECK-NEXT:      vp<[[VP4:%[0-9]+]]> = vector-pointer inbounds ir<%gep.a>
+; CHECK-NEXT:      WIDEN ir<%load.a> = load vp<[[VP4]]>
+; CHECK-NEXT:      CLONE ir<%gep.b> = getelementptr inbounds ir<%b>, ir<%iv>
+; CHECK-NEXT:      vp<[[VP5:%[0-9]+]]> = vector-pointer inbounds ir<%gep.b>
+; CHECK-NEXT:      WIDEN ir<%load.b> = load vp<[[VP5]]>
+; CHECK-NEXT:      WIDEN ir<%cmp> = icmp slt ir<%load.a>, ir<%load.b>
+; CHECK-NEXT:      WIDEN ir<%sel> = select ir<%cmp>, ir<%rdx>, ir<%load.a>
+; CHECK-NEXT:      CLONE ir<%iv.next> = add nuw nsw ir<%iv>, ir<1>
+; CHECK-NEXT:      CLONE ir<%exitcond> = icmp eq ir<%iv.next>, ir<500>
+; CHECK-NEXT:      EMIT vp<%index.next> = add nuw vp<[[VP3]]>, vp<[[VP1]]>
+; CHECK-NEXT:      EMIT branch-on-count vp<%index.next>, vp<[[VP2]]>
+; CHECK-NEXT:    No successors
+; CHECK-NEXT:  }
+; CHECK-NEXT:  Successor(s): middle.block
+; CHECK-EMPTY:
+; CHECK-NEXT:  middle.block:
+; CHECK-NEXT:    EMIT vp<[[VP7:%[0-9]+]]> = compute-reduction-result (find-last) ir<%sel>
+; CHECK-NEXT:    EMIT vp<[[VP8:%[0-9]+]]> = exiting-iv-value ir<%iv>
+; CHECK-NEXT:    EMIT vp<[[VP9:%[0-9]+]]> = extract-last-part vp<[[VP7]]>
+; CHECK-NEXT:    EMIT vp<[[VP10:%[0-9]+]]> = extract-last-lane vp<[[VP9]]>
+; CHECK-NEXT:    EMIT vp<[[VP11:%[0-9]+]]> = extract-last-part vp<[[VP7]]>
+; CHECK-NEXT:    EMIT vp<[[VP12:%[0-9]+]]> = extract-last-lane vp<[[VP11]]>
+; CHECK-NEXT:    EMIT vp<%cmp.n> = icmp eq ir<500>, vp<[[VP2]]>
+; CHECK-NEXT:    EMIT branch-on-cond vp<%cmp.n>
+; CHECK-NEXT:  Successor(s): ir-bb<exit>, scalar.ph
+; CHECK-EMPTY:
+; CHECK-NEXT:  ir-bb<exit>:
+; CHECK-NEXT:    IR   %sel.lcssa = phi i32 [ %sel, %loop ] (extra operand: vp<[[VP7]]> from middle.block)
+; CHECK-NEXT:  No successors
+; CHECK-EMPTY:
+; CHECK-NEXT:  scalar.ph:
+; CHECK-NEXT:    EMIT-SCALAR vp<%bc.resume.val> = phi [ vp<[[VP8]]>, middle.block ], [ ir<0>, ir-bb<entry> ]
+; CHECK-NEXT:    EMIT-SCALAR vp<%bc.merge.rdx> = phi [ vp<[[VP7]]>, middle.block ], [ ir<1>, ir-bb<entry> ]
+; CHECK-NEXT:  Successor(s): ir-bb<loop>
+; CHECK-EMPTY:
+; CHECK-NEXT:  ir-bb<loop>:
+; CHECK-NEXT:    IR   %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ] (extra operand: vp<%bc.resume.val> from scalar.ph)
+; CHECK-NEXT:    IR   %rdx = phi i32 [ 1, %entry ], [ %sel, %loop ] (extra operand: vp<%bc.merge.rdx> from scalar.ph)
+; CHECK-NEXT:    IR   %gep.a = getelementptr inbounds i32, ptr %a, i64 %iv
+; CHECK-NEXT:    IR   %load.a = load i32, ptr %gep.a, align 4
+; CHECK-NEXT:    IR   %gep.b = getelementptr inbounds i32, ptr %b, i64 %iv
+; CHECK-NEXT:    IR   %load.b = load i32, ptr %gep.b, align 4
+; CHECK-NEXT:    IR   %cmp = icmp slt i32 %load.a, %load.b
+; CHECK-NEXT:    IR   %sel = select i1 %cmp, i32 %rdx, i32 %load.a
+; CHECK-NEXT:    IR   %iv.next = add nuw nsw i64 %iv, 1
+; CHECK-NEXT:    IR   %exitcond = icmp eq i64 %iv.next, 500
+; CHECK-NEXT:  No successors
+; CHECK-NEXT:  }
+;
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  %rdx = phi i32 [ 1, %entry ], [ %sel, %loop ]
+  %gep.a = getelementptr inbounds i32, ptr %a, i64 %iv
+  %load.a = load i32, ptr %gep.a, align 4
+  %gep.b = getelementptr inbounds i32, ptr %b, i64 %iv
+  %load.b = load i32, ptr %gep.b, align 4
+  %cmp = icmp slt i32 %load.a, %load.b
+  %sel = select i1 %cmp, i32 %rdx, i32 %load.a
+  %iv.next = add nuw nsw i64 %iv, 1
+  %exitcond = icmp eq i64 %iv.next, 500
+  br i1 %exitcond, label %exit, label %loop
+
+exit:
+  ret i32 %sel
+}

>From 31bd684959ab8ef290618e26fa5f97f45418f33e Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Wed, 11 Mar 2026 19:15:04 +0800
Subject: [PATCH 2/2] Remove FindIV

---
 llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index ce5d3d6662964..b472204e7a491 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -2248,9 +2248,6 @@ void VPIRFlags::printFlags(raw_ostream &O) const {
     case RecurKind::AnyOf:
       O << "any-of";
       break;
-    case RecurKind::FindIV:
-      O << "find-iv";
-      break;
     case RecurKind::FindLast:
       O << "find-last";
       break;



More information about the llvm-commits mailing list