[llvm] [LV] Change opcode of FindLast/FindFirst recurrence to Instruction::Select (PR #162252)

Mel Chen via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 7 02:43:49 PDT 2025


https://github.com/Mel-Chen created https://github.com/llvm/llvm-project/pull/162252

Unlike AnyOf, FindLast/FindFirst are not related to an or operation. Therefore, it’s inappropriate to set the opcode of FindLast/FindFirst to Instruction::Or. Setting it to Instruction::Select is more appropriate.

This change also allows RecurrenceDescriptor::getReductionOpChain to find the recurrence chain for FindLast/FindFirst, which will help support in-loop FindLast/FindFirst reductions in the future.

>From b3bcf16559afd7f7fbbdef97c849ce1f4fcb3906 Mon Sep 17 00:00:00 2001
From: Mel Chen <mel.chen at sifive.com>
Date: Tue, 7 Oct 2025 01:48:01 -0700
Subject: [PATCH] [IVDescriptors] Change opcode of FindLast/FindFirst
 recurrence to Instruction::Select

---
 llvm/lib/Analysis/IVDescriptors.cpp             | 9 +++++----
 llvm/lib/Transforms/Vectorize/LoopVectorize.cpp | 7 ++++++-
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Analysis/IVDescriptors.cpp b/llvm/lib/Analysis/IVDescriptors.cpp
index b8c540ce4b99d..743b183d672a0 100644
--- a/llvm/lib/Analysis/IVDescriptors.cpp
+++ b/llvm/lib/Analysis/IVDescriptors.cpp
@@ -1226,10 +1226,6 @@ unsigned RecurrenceDescriptor::getOpcode(RecurKind Kind) {
   case RecurKind::Mul:
     return Instruction::Mul;
   case RecurKind::AnyOf:
-  case RecurKind::FindFirstIVSMin:
-  case RecurKind::FindFirstIVUMin:
-  case RecurKind::FindLastIVSMax:
-  case RecurKind::FindLastIVUMax:
   case RecurKind::Or:
     return Instruction::Or;
   case RecurKind::And:
@@ -1253,6 +1249,11 @@ unsigned RecurrenceDescriptor::getOpcode(RecurKind Kind) {
   case RecurKind::FMaximumNum:
   case RecurKind::FMinimumNum:
     return Instruction::FCmp;
+  case RecurKind::FindFirstIVSMin:
+  case RecurKind::FindFirstIVUMin:
+  case RecurKind::FindLastIVSMax:
+  case RecurKind::FindLastIVUMax:
+    return Instruction::Select;
   default:
     llvm_unreachable("Unknown recurrence operation");
   }
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index cee08ef94aeb5..6ee2a3a299bd2 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -6515,9 +6515,14 @@ void LoopVectorizationCostModel::collectInLoopReductions() {
     if (RdxDesc.getRecurrenceType() != Phi->getType())
       continue;
 
+    // In-loop AnyOf and FindIV reductions are not yet supported.
+    RecurKind Kind = RdxDesc.getRecurrenceKind();
+    if (RecurrenceDescriptor::isAnyOfRecurrenceKind(Kind) ||
+        RecurrenceDescriptor::isFindIVRecurrenceKind(Kind))
+      continue;
+
     // If the target would prefer this reduction to happen "in-loop", then we
     // want to record it as such.
-    RecurKind Kind = RdxDesc.getRecurrenceKind();
     if (!PreferInLoopReductions && !useOrderedReductions(RdxDesc) &&
         !TTI.preferInLoopReduction(Kind, Phi->getType()))
       continue;



More information about the llvm-commits mailing list