[llvm] [RISCV][TTI] Add checks for invalid cast operations (PR #88854)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 16 01:16:37 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-risc-v

Author: Shih-Po Hung (arcbbb)

<details>
<summary>Changes</summary>

In issue #<!-- -->88802, the LV cost model would query the cost of the TRUNC for source type 2xi1 and destination type 2xi32. This patch adds an early exit check to prevent invalid operations.

---
Full diff: https://github.com/llvm/llvm-project/pull/88854.diff


1 Files Affected:

- (modified) llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp (+10) 


``````````diff
diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index 38304ff90252f0..c4f1c275f63b65 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -956,6 +956,9 @@ InstructionCost RISCVTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
     return getRISCVInstructionCost(Op, DstLT.second, CostKind);
   }
   case ISD::TRUNCATE:
+    // Early return for invalid operation
+    if (Dst->getScalarSizeInBits() >= Src->getScalarSizeInBits())
+      break;
     if (Dst->getScalarSizeInBits() == 1) {
       // We do not use several vncvt to truncate to mask vector. So we could
       // not use PowDiff to calculate it.
@@ -968,6 +971,13 @@ InstructionCost RISCVTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
     [[fallthrough]];
   case ISD::FP_EXTEND:
   case ISD::FP_ROUND: {
+    // Early return for invalid operation
+    if ((ISD == ISD::FP_ROUND) &&
+        Dst->getScalarSizeInBits() >= Src->getScalarSizeInBits())
+      break;
+    if ((ISD == ISD::FP_EXTEND) &&
+        Src->getScalarSizeInBits() >= Dst->getScalarSizeInBits())
+      break;
     // Counts of narrow/widen instructions.
     unsigned SrcEltSize = Src->getScalarSizeInBits();
     unsigned DstEltSize = Dst->getScalarSizeInBits();

``````````

</details>


https://github.com/llvm/llvm-project/pull/88854


More information about the llvm-commits mailing list