[llvm] [RISCV][TTI] Support cost of f16 FCmp using zvfhmin in the absence of… (PR #89166)

Shih-Po Hung via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 3 23:03:43 PST 2025


https://github.com/arcbbb updated https://github.com/llvm/llvm-project/pull/89166

>From cc02a5c0b8d1d6ac73747c3d54469708f3145504 Mon Sep 17 00:00:00 2001
From: ShihPo Hung <shihpo.hung at sifive.com>
Date: Thu, 2 Jan 2025 23:27:05 -0800
Subject: [PATCH] [RISCV][TTI] Support cost of f16 FCmp using zvfhmin in the
 absence of zvfh

With zvfhmin, the cost of f16 FCmp is determined by the cost of widening
two
operands from f16 to f32, plus the cost of performing an f32 FCmp
---
 .../Target/RISCV/RISCVTargetTransformInfo.cpp   | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index 850d6244affa50..0595cdc6753685 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -1816,12 +1816,27 @@ InstructionCost RISCVTTIImpl::getCmpSelInstrCost(
     // one which will calculate as:
     // ScalarizeCost + Num * Cost for fixed vector,
     // InvalidCost for scalable vector.
-    if ((ValTy->getScalarSizeInBits() == 16 && !ST->hasVInstructionsF16()) ||
+    if ((ValTy->getScalarSizeInBits() == 16 &&
+         !ST->hasVInstructionsF16Minimal()) ||
         (ValTy->getScalarSizeInBits() == 32 && !ST->hasVInstructionsF32()) ||
         (ValTy->getScalarSizeInBits() == 64 && !ST->hasVInstructionsF64()))
       return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind,
                                        Op1Info, Op2Info, I);
 
+    if ((ValTy->getScalarSizeInBits() == 16) && !ST->hasVInstructionsF16()) {
+      // pre-widening Op1 and Op2 to f32 before comparison
+      VectorType *VecF32Ty =
+          VectorType::get(Type::getFloatTy(ValTy->getContext()),
+                          cast<VectorType>(ValTy)->getElementCount());
+
+      InstructionCost WidenCost =
+          getCastInstrCost(ISD::FP_EXTEND, VecF32Ty, ValTy,
+                           TTI::CastContextHint::None, CostKind);
+      InstructionCost CmpCost = getCmpSelInstrCost(
+          Opcode, VecF32Ty, CondTy, VecPred, CostKind, Op1Info, Op2Info, I);
+      return WidenCost + CmpCost;
+    }
+
     // Assuming vector fp compare and mask instructions are all the same cost
     // until a need arises to differentiate them.
     switch (VecPred) {



More information about the llvm-commits mailing list