[llvm] 45c1ce3 - [RISCV] Add cost model for select and integer compare instructions.

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 30 20:49:51 PDT 2022


Author: jacquesguan
Date: 2022-08-31T11:32:58+08:00
New Revision: 45c1ce321dce7741bee914109266c36f58ba8f3d

URL: https://github.com/llvm/llvm-project/commit/45c1ce321dce7741bee914109266c36f58ba8f3d
DIFF: https://github.com/llvm/llvm-project/commit/45c1ce321dce7741bee914109266c36f58ba8f3d.diff

LOG: [RISCV] Add cost model for select and integer compare instructions.

This patch adds cost model for vector select and integer compare instructions.

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
    llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h
    llvm/test/Analysis/CostModel/RISCV/rvv-select.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index 01e5aeaae8c4a..59b7cb608b9f4 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -682,6 +682,59 @@ InstructionCost RISCVTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
                                        CostKind, OpInfo, I);
 }
 
+InstructionCost RISCVTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
+                                                 Type *CondTy,
+                                                 CmpInst::Predicate VecPred,
+                                                 TTI::TargetCostKind CostKind,
+                                                 const Instruction *I) {
+  if (CostKind != TTI::TCK_RecipThroughput)
+    return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind,
+                                     I);
+
+  if (isa<FixedVectorType>(ValTy) && !ST->useRVVForFixedLengthVectors())
+    return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind,
+                                     I);
+
+  // Skip if scalar size of ValTy is bigger than ELEN.
+  if (ValTy->isVectorTy() && ValTy->getScalarSizeInBits() > ST->getELEN())
+    return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind,
+                                     I);
+
+  if (Opcode == Instruction::Select && ValTy->isVectorTy()) {
+    std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(ValTy);
+    if (CondTy->isVectorTy()) {
+      if (ValTy->getScalarSizeInBits() == 1) {
+        // vmandn.mm v8, v8, v9
+        // vmand.mm v9, v0, v9
+        // vmor.mm v0, v9, v8
+        return LT.first * 3;
+      }
+      // vselect and max/min are supported natively.
+      return LT.first * 1;
+    }
+
+    // vmv.v.x v10, a0
+    // vmsne.vi v0, v10, 0
+    // vmerge.vvm v8, v9, v8, v0
+    return LT.first * 3;
+  }
+
+  if ((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) &&
+      ValTy->isVectorTy()) {
+    std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(ValTy);
+
+    // Support natively.
+    if (CmpInst::isIntPredicate(VecPred))
+      return LT.first * 1;
+
+    // TODO: Add cost for fp vector compare instruction.
+  }
+
+  // TODO: Add cost for scalar type.
+
+  return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind, I);
+}
+
 void RISCVTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
                                            TTI::UnrollingPreferences &UP,
                                            OptimizationRemarkEmitter *ORE) {

diff  --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h
index 14e232e50556f..b6e72a36f0193 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h
@@ -140,6 +140,11 @@ class RISCVTTIImpl : public BasicTTIImplBase<RISCVTTIImpl> {
                   TTI::OperandValueInfo OpdInfo = {TTI::OK_AnyValue, TTI::OP_None},
                   const Instruction *I = nullptr);
 
+  InstructionCost getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
+                                     CmpInst::Predicate VecPred,
+                                     TTI::TargetCostKind CostKind,
+                                     const Instruction *I = nullptr);
+
   bool isElementTypeLegalForScalableVector(Type *Ty) const {
     return TLI->isLegalElementTypeForRVV(Ty);
   }

diff  --git a/llvm/test/Analysis/CostModel/RISCV/rvv-select.ll b/llvm/test/Analysis/CostModel/RISCV/rvv-select.ll
index 0460c229471c5..fb9f53db24244 100644
--- a/llvm/test/Analysis/CostModel/RISCV/rvv-select.ll
+++ b/llvm/test/Analysis/CostModel/RISCV/rvv-select.ll
@@ -6,18 +6,18 @@
 define void @select() {
 ; CHECK-LABEL: 'select'
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %1 = select i1 undef, i1 undef, i1 undef
-; CHECK-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %2 = select <1 x i1> undef, <1 x i1> undef, <1 x i1> undef
-; CHECK-NEXT:  Cost Model: Found an estimated cost of 4 for instruction: %3 = select <2 x i1> undef, <2 x i1> undef, <2 x i1> undef
-; CHECK-NEXT:  Cost Model: Found an estimated cost of 8 for instruction: %4 = select <4 x i1> undef, <4 x i1> undef, <4 x i1> undef
-; CHECK-NEXT:  Cost Model: Found an estimated cost of 16 for instruction: %5 = select <8 x i1> undef, <8 x i1> undef, <8 x i1> undef
-; CHECK-NEXT:  Cost Model: Found an estimated cost of 32 for instruction: %6 = select <16 x i1> undef, <16 x i1> undef, <16 x i1> undef
-; CHECK-NEXT:  Cost Model: Found an estimated cost of 64 for instruction: %7 = select <32 x i1> undef, <32 x i1> undef, <32 x i1> undef
-; CHECK-NEXT:  Cost Model: Invalid cost for instruction: %8 = select <vscale x 1 x i1> undef, <vscale x 1 x i1> undef, <vscale x 1 x i1> undef
-; CHECK-NEXT:  Cost Model: Invalid cost for instruction: %9 = select <vscale x 2 x i1> undef, <vscale x 2 x i1> undef, <vscale x 2 x i1> undef
-; CHECK-NEXT:  Cost Model: Invalid cost for instruction: %10 = select <vscale x 4 x i1> undef, <vscale x 4 x i1> undef, <vscale x 4 x i1> undef
-; CHECK-NEXT:  Cost Model: Invalid cost for instruction: %11 = select <vscale x 8 x i1> undef, <vscale x 8 x i1> undef, <vscale x 8 x i1> undef
-; CHECK-NEXT:  Cost Model: Invalid cost for instruction: %12 = select <vscale x 16 x i1> undef, <vscale x 16 x i1> undef, <vscale x 16 x i1> undef
-; CHECK-NEXT:  Cost Model: Invalid cost for instruction: %13 = select <vscale x 32 x i1> undef, <vscale x 32 x i1> undef, <vscale x 32 x i1> undef
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 3 for instruction: %2 = select <1 x i1> undef, <1 x i1> undef, <1 x i1> undef
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 3 for instruction: %3 = select <2 x i1> undef, <2 x i1> undef, <2 x i1> undef
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 3 for instruction: %4 = select <4 x i1> undef, <4 x i1> undef, <4 x i1> undef
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 3 for instruction: %5 = select <8 x i1> undef, <8 x i1> undef, <8 x i1> undef
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 3 for instruction: %6 = select <16 x i1> undef, <16 x i1> undef, <16 x i1> undef
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 6 for instruction: %7 = select <32 x i1> undef, <32 x i1> undef, <32 x i1> undef
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 3 for instruction: %8 = select <vscale x 1 x i1> undef, <vscale x 1 x i1> undef, <vscale x 1 x i1> undef
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 3 for instruction: %9 = select <vscale x 2 x i1> undef, <vscale x 2 x i1> undef, <vscale x 2 x i1> undef
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 3 for instruction: %10 = select <vscale x 4 x i1> undef, <vscale x 4 x i1> undef, <vscale x 4 x i1> undef
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 3 for instruction: %11 = select <vscale x 8 x i1> undef, <vscale x 8 x i1> undef, <vscale x 8 x i1> undef
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 3 for instruction: %12 = select <vscale x 16 x i1> undef, <vscale x 16 x i1> undef, <vscale x 16 x i1> undef
+; CHECK-NEXT:  Cost Model: Found an estimated cost of 3 for instruction: %13 = select <vscale x 32 x i1> undef, <vscale x 32 x i1> undef, <vscale x 32 x i1> undef
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %14 = select i1 undef, i8 undef, i8 undef
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %15 = select <1 x i1> undef, <1 x i8> undef, <1 x i8> undef
 ; CHECK-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %16 = select <2 x i1> undef, <2 x i8> undef, <2 x i8> undef


        


More information about the llvm-commits mailing list