[llvm] [CodeGen] Expand fmaximum/fminimum reductions (PR #195169)

Prajwal KP via llvm-commits llvm-commits at lists.llvm.org
Fri May 1 06:25:18 PDT 2026


https://github.com/Prajwal-kp-18 updated https://github.com/llvm/llvm-project/pull/195169

>From f9fcb716fc39310ec600f41e2b0a9bdea09fb3df Mon Sep 17 00:00:00 2001
From: Prajwal <prajwal.kp.1817 at gmail.com>
Date: Fri, 1 May 2026 01:32:42 +0530
Subject: [PATCH 1/3] [CodeGen] Expand fmaximum/fminimum reductions

---
 llvm/lib/CodeGen/ExpandReductions.cpp | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/llvm/lib/CodeGen/ExpandReductions.cpp b/llvm/lib/CodeGen/ExpandReductions.cpp
index 2bc5faee78f3c..329237563bc24 100644
--- a/llvm/lib/CodeGen/ExpandReductions.cpp
+++ b/llvm/lib/CodeGen/ExpandReductions.cpp
@@ -49,6 +49,8 @@ bool expandReductions(Function &F, const TargetTransformInfo *TTI,
       case Intrinsic::vector_reduce_umin:
       case Intrinsic::vector_reduce_fmax:
       case Intrinsic::vector_reduce_fmin:
+      case Intrinsic::vector_reduce_fmaximum:
+      case Intrinsic::vector_reduce_fminimum:
         if (TTI->shouldExpandReduction(II))
           Worklist.push_back(II);
 
@@ -158,6 +160,19 @@ bool expandReductions(Function &F, const TargetTransformInfo *TTI,
       Rdx = getShuffleReduction(Builder, Vec, RdxOpcode, RS, RK);
       break;
     }
+    case Intrinsic::vector_reduce_fmaximum:
+    case Intrinsic::vector_reduce_fminimum: {
+      // We require "nnan" to use a shuffle reduction; "nsz" is implied by the
+      // semantics of the reduction.
+      Value *Vec = II->getArgOperand(0);
+      if (!isPowerOf2_32(
+              cast<FixedVectorType>(Vec->getType())->getNumElements()) ||
+          !FMF.noNaNs())
+        continue;
+      unsigned RdxOpcode = getArithmeticReductionInstruction(ID);
+      Rdx = getShuffleReduction(Builder, Vec, RdxOpcode, RS, RK);
+      break;
+    }
     }
     II->replaceAllUsesWith(Rdx);
     II->eraseFromParent();

>From ef8674f8d8c0ad8ec187afcbee901814688c6d6a Mon Sep 17 00:00:00 2001
From: Prajwal <prajwal.kp.1817 at gmail.com>
Date: Fri, 1 May 2026 17:26:23 +0530
Subject: [PATCH 2/3] [CodeGen] Fix getArithmeticReductionInstruction for
 fmaximum/fminimum

---
 llvm/lib/Transforms/Utils/LoopUtils.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp
index 9c7d10629adf6..47c87a32c5bff 100644
--- a/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -1165,6 +1165,8 @@ unsigned llvm::getArithmeticReductionInstruction(Intrinsic::ID RdxID) {
     return Instruction::ICmp;
   case Intrinsic::vector_reduce_fmax:
   case Intrinsic::vector_reduce_fmin:
+  case Intrinsic::vector_reduce_fmaximum:
+  case Intrinsic::vector_reduce_fminimum:
     return Instruction::FCmp;
   default:
     llvm_unreachable("Unexpected ID");
@@ -1256,6 +1258,10 @@ RecurKind llvm::getMinMaxReductionRecurKind(Intrinsic::ID RdxID) {
     return RecurKind::FMax;
   case Intrinsic::vector_reduce_fmin:
     return RecurKind::FMin;
+  case Intrinsic::vector_reduce_fmaximum:
+    return RecurKind::FMaximum;
+  case Intrinsic::vector_reduce_fminimum:
+    return RecurKind::FMinimum;
   default:
     return RecurKind::None;
   }

>From 8c00a7e1e00a188f089f22896f6bf13f78cef784 Mon Sep 17 00:00:00 2001
From: Prajwal <prajwal.kp.1817 at gmail.com>
Date: Fri, 1 May 2026 18:54:58 +0530
Subject: [PATCH 3/3] [WebAssembly] Return false from shouldExpandReduction for
 fmaximum/fminimum

---
 .../WebAssembly/WebAssemblyTargetTransformInfo.cpp     | 10 ++++++++++
 .../WebAssembly/WebAssemblyTargetTransformInfo.h       |  2 ++
 2 files changed, 12 insertions(+)

diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.cpp
index 1bd3a6b950944..3c4cb2cd32eff 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.cpp
@@ -475,6 +475,16 @@ InstructionCost WebAssemblyTTIImpl::getPartialReductionCost(
   return Invalid;
 }
 
+bool WebAssemblyTTIImpl::shouldExpandReduction(const IntrinsicInst *II) const {
+  switch (II->getIntrinsicID()) {
+  case Intrinsic::vector_reduce_fmaximum:
+  case Intrinsic::vector_reduce_fminimum:
+    return false;
+  default:
+    return BaseT::shouldExpandReduction(II);
+  }
+}
+
 TTI::ReductionShuffle WebAssemblyTTIImpl::getPreferredExpandedReductionShuffle(
     const IntrinsicInst *II) const {
 
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.h b/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.h
index 221f32609b2de..af2529c3d82e1 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.h
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetTransformInfo.h
@@ -105,6 +105,8 @@ class WebAssemblyTTIImpl final : public BasicTTIImplBase<WebAssemblyTTIImpl> {
       TTI::TargetCostKind CostKind,
       std::optional<FastMathFlags> FMF) const override;
 
+  bool shouldExpandReduction(const IntrinsicInst *II) const override;
+
   TTI::ReductionShuffle
   getPreferredExpandedReductionShuffle(const IntrinsicInst *II) const override;
 



More information about the llvm-commits mailing list