[llvm] 21bf59c - [RISCV] Add cost model for mask vector extend and truncate instruction.

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 10 19:55:57 PDT 2022


Author: jacquesguan
Date: 2022-08-11T10:55:43+08:00
New Revision: 21bf59c92af11f2f1e4482e530993c1fb51b9d88

URL: https://github.com/llvm/llvm-project/commit/21bf59c92af11f2f1e4482e530993c1fb51b9d88
DIFF: https://github.com/llvm/llvm-project/commit/21bf59c92af11f2f1e4482e530993c1fb51b9d88.diff

LOG: [RISCV] Add cost model for mask vector extend and truncate instruction.

As extending from or truncating to mask vector do not use the same instructions as the normal cast, this path changed it to 2 which is the number of instructions we used.

Differential Revision: https://reviews.llvm.org/D131552

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
    llvm/test/Analysis/CostModel/RISCV/cast.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index feb4e8f571186..914631a023c3e 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -290,8 +290,24 @@ InstructionCost RISCVTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
     switch (ISD) {
     case ISD::SIGN_EXTEND:
     case ISD::ZERO_EXTEND:
+      if (Src->getScalarSizeInBits() == 1) {
+        // We do not use vsext/vzext to extend from mask vector.
+        // Instead we use the following instructions to extend from mask vector:
+        // vmv.v.i v8, 0
+        // vmerge.vim v8, v8, -1, v0
+        return 2;
+      }
       return 1;
     case ISD::TRUNCATE:
+      if (Dst->getScalarSizeInBits() == 1) {
+        // We do not use several vncvt to truncate to mask vector. So we could
+        // not use PowDiff to calculate it.
+        // Instead we use the following instructions to truncate to mask vector:
+        // vand.vi v8, v8, 1
+        // vmsne.vi v0, v8, 0
+        return 2;
+      }
+      [[fallthrough]];
     case ISD::FP_EXTEND:
     case ISD::FP_ROUND:
       // Counts of narrow/widen instructions.

diff  --git a/llvm/test/Analysis/CostModel/RISCV/cast.ll b/llvm/test/Analysis/CostModel/RISCV/cast.ll
index 49ffdb08c7d75..ddcc92919ef48 100644
--- a/llvm/test/Analysis/CostModel/RISCV/cast.ll
+++ b/llvm/test/Analysis/CostModel/RISCV/cast.ll
@@ -16,6 +16,14 @@ define void @ext() {
 ; RISCV32-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %z4i16i64 = zext <4 x i16> undef to <4 x i64>
 ; RISCV32-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %s4i32i64 = sext <4 x i32> undef to <4 x i64>
 ; RISCV32-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %z4i32i64 = zext <4 x i32> undef to <4 x i64>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i8 = sext <4 x i1> undef to <4 x i8>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z4i1i8 = zext <4 x i1> undef to <4 x i8>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i16 = sext <4 x i1> undef to <4 x i16>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z4i1i16 = zext <4 x i1> undef to <4 x i16>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i32 = sext <4 x i1> undef to <4 x i32>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z4i1i32 = zext <4 x i1> undef to <4 x i32>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i64 = sext <4 x i1> undef to <4 x i64>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z4i1i64 = zext <4 x i1> undef to <4 x i64>
 ; RISCV32-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %s8i8i16 = sext <8 x i8> undef to <8 x i16>
 ; RISCV32-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %z8i8i16 = zext <8 x i8> undef to <8 x i16>
 ; RISCV32-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %s8i8i32 = sext <8 x i8> undef to <8 x i32>
@@ -26,6 +34,14 @@ define void @ext() {
 ; RISCV32-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %z8i16i32 = zext <8 x i16> undef to <8 x i32>
 ; RISCV32-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %s8i16i64 = sext <8 x i16> undef to <8 x i64>
 ; RISCV32-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %z8i16i64 = zext <8 x i16> undef to <8 x i64>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s8i1i8 = sext <8 x i1> undef to <8 x i8>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z8i1i8 = zext <8 x i1> undef to <8 x i8>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s8i1i16 = sext <8 x i1> undef to <8 x i16>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z8i1i16 = zext <8 x i1> undef to <8 x i16>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s8i1i32 = sext <8 x i1> undef to <8 x i32>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z8i1i32 = zext <8 x i1> undef to <8 x i32>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s8i1i64 = sext <8 x i1> undef to <8 x i64>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z8i1i64 = zext <8 x i1> undef to <8 x i64>
 ; RISCV32-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: ret void
 ;
 ; RISCV64-LABEL: 'ext'
@@ -41,6 +57,14 @@ define void @ext() {
 ; RISCV64-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %z4i16i64 = zext <4 x i16> undef to <4 x i64>
 ; RISCV64-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %s4i32i64 = sext <4 x i32> undef to <4 x i64>
 ; RISCV64-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %z4i32i64 = zext <4 x i32> undef to <4 x i64>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i8 = sext <4 x i1> undef to <4 x i8>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z4i1i8 = zext <4 x i1> undef to <4 x i8>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i16 = sext <4 x i1> undef to <4 x i16>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z4i1i16 = zext <4 x i1> undef to <4 x i16>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i32 = sext <4 x i1> undef to <4 x i32>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z4i1i32 = zext <4 x i1> undef to <4 x i32>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i64 = sext <4 x i1> undef to <4 x i64>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z4i1i64 = zext <4 x i1> undef to <4 x i64>
 ; RISCV64-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %s8i8i16 = sext <8 x i8> undef to <8 x i16>
 ; RISCV64-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %z8i8i16 = zext <8 x i8> undef to <8 x i16>
 ; RISCV64-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %s8i8i32 = sext <8 x i8> undef to <8 x i32>
@@ -51,6 +75,14 @@ define void @ext() {
 ; RISCV64-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %z8i16i32 = zext <8 x i16> undef to <8 x i32>
 ; RISCV64-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %s8i16i64 = sext <8 x i16> undef to <8 x i64>
 ; RISCV64-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %z8i16i64 = zext <8 x i16> undef to <8 x i64>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s8i1i8 = sext <8 x i1> undef to <8 x i8>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z8i1i8 = zext <8 x i1> undef to <8 x i8>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s8i1i16 = sext <8 x i1> undef to <8 x i16>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z8i1i16 = zext <8 x i1> undef to <8 x i16>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s8i1i32 = sext <8 x i1> undef to <8 x i32>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z8i1i32 = zext <8 x i1> undef to <8 x i32>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s8i1i64 = sext <8 x i1> undef to <8 x i64>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %z8i1i64 = zext <8 x i1> undef to <8 x i64>
 ; RISCV64-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: ret void
 ;
   %s4i8i16 = sext <4 x i8>  undef to <4 x i16>
@@ -65,6 +97,14 @@ define void @ext() {
   %z4i16i64 = zext <4 x i16> undef to <4 x i64>
   %s4i32i64 = sext <4 x i32> undef to <4 x i64>
   %z4i32i64 = zext <4 x i32> undef to <4 x i64>
+  %s4i1i8 = sext <4 x i1>  undef to <4 x i8>
+  %z4i1i8 = zext <4 x i1>  undef to <4 x i8>
+  %s4i1i16 = sext <4 x i1>  undef to <4 x i16>
+  %z4i1i16 = zext <4 x i1>  undef to <4 x i16>
+  %s4i1i32 = sext <4 x i1>  undef to <4 x i32>
+  %z4i1i32 = zext <4 x i1>  undef to <4 x i32>
+  %s4i1i64 = sext <4 x i1>  undef to <4 x i64>
+  %z4i1i64 = zext <4 x i1>  undef to <4 x i64>
 
   %s8i8i16 = sext <8 x i8>  undef to <8 x i16>
   %z8i8i16 = zext <8 x i8>  undef to <8 x i16>
@@ -76,6 +116,14 @@ define void @ext() {
   %z8i16i32 = zext <8 x i16> undef to <8 x i32>
   %s8i16i64 = sext <8 x i16> undef to <8 x i64>
   %z8i16i64 = zext <8 x i16> undef to <8 x i64>
+  %s8i1i8 = sext <8 x i1>  undef to <8 x i8>
+  %z8i1i8 = zext <8 x i1>  undef to <8 x i8>
+  %s8i1i16 = sext <8 x i1>  undef to <8 x i16>
+  %z8i1i16 = zext <8 x i1>  undef to <8 x i16>
+  %s8i1i32 = sext <8 x i1>  undef to <8 x i32>
+  %z8i1i32 = zext <8 x i1>  undef to <8 x i32>
+  %s8i1i64 = sext <8 x i1>  undef to <8 x i64>
+  %z8i1i64 = zext <8 x i1>  undef to <8 x i64>
   ret void
 }
 
@@ -87,6 +135,10 @@ define void @trunc() {
 ; RISCV32-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %s4i16i32 = trunc <4 x i32> undef to <4 x i16>
 ; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i16i64 = trunc <4 x i64> undef to <4 x i16>
 ; RISCV32-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %s4i32i64 = trunc <4 x i64> undef to <4 x i32>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i8 = trunc <4 x i8> undef to <4 x i1>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i16 = trunc <4 x i16> undef to <4 x i1>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i32 = trunc <4 x i32> undef to <4 x i1>
+; RISCV32-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i64 = trunc <4 x i64> undef to <4 x i1>
 ; RISCV32-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: ret void
 ;
 ; RISCV64-LABEL: 'trunc'
@@ -96,6 +148,10 @@ define void @trunc() {
 ; RISCV64-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %s4i16i32 = trunc <4 x i32> undef to <4 x i16>
 ; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i16i64 = trunc <4 x i64> undef to <4 x i16>
 ; RISCV64-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %s4i32i64 = trunc <4 x i64> undef to <4 x i32>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i8 = trunc <4 x i8> undef to <4 x i1>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i16 = trunc <4 x i16> undef to <4 x i1>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i32 = trunc <4 x i32> undef to <4 x i1>
+; RISCV64-NEXT:  Cost Model: Found an estimated cost of 2 for instruction: %s4i1i64 = trunc <4 x i64> undef to <4 x i1>
 ; RISCV64-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: ret void
 ;
   %s4i8i16 = trunc <4 x i16> undef to <4 x i8>
@@ -104,6 +160,10 @@ define void @trunc() {
   %s4i16i32 = trunc <4 x i32> undef to <4 x i16>
   %s4i16i64 = trunc <4 x i64> undef to <4 x i16>
   %s4i32i64 = trunc <4 x i64> undef to <4 x i32>
+  %s4i1i8 = trunc <4 x i8> undef to <4 x i1>
+  %s4i1i16 = trunc <4 x i16> undef to <4 x i1>
+  %s4i1i32 = trunc <4 x i32> undef to <4 x i1>
+  %s4i1i64 = trunc <4 x i64> undef to <4 x i1>
   ret void
 }
 


        


More information about the llvm-commits mailing list