[llvm] [RISCV][TTI] Fix potential crash of using dyn_cast() in getIntrinsicInstrCost() NFC. (PR #109379)

Elvis Wang via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 19 23:12:04 PDT 2024


https://github.com/ElvisWang123 created https://github.com/llvm/llvm-project/pull/109379

This patch fix the potential crash about using dyn_cast in `vp_cmp` which is  same as #109313.

Check if the IntrinsicCostAttrubute contains underlying instruction first and cast to the VPCmpIntrinsic.

>From 5e5d244b738d9f5f51649b4eae8d301dcc756e4c Mon Sep 17 00:00:00 2001
From: Elvis Wang <elvis.wang at sifive.com>
Date: Thu, 19 Sep 2024 19:05:57 -0700
Subject: [PATCH] [RISCV] Fix may dyn_cast(nullptr) in getIntrinsicInstrCost()
 NFC.

Using dyn_cast in vp_cmp may cause potential failure same as #109313.

Check if the IntrinsicCostAttrubute contains underlying instruction
first and cast to the VPCmpIntrinsic.
---
 llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index 5d280b44630aef..d1cf7fc2991058 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -1039,12 +1039,12 @@ RISCVTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
   case Intrinsic::vp_fcmp: {
     Intrinsic::ID IID = ICA.getID();
     std::optional<unsigned> FOp = VPIntrinsic::getFunctionalOpcodeForVP(IID);
-    auto *UI = dyn_cast<VPCmpIntrinsic>(ICA.getInst());
-
     // We can only handle vp_cmp intrinsics with underlying instructions.
-    if (!UI)
+    if(!ICA.getInst())
       break;
+
     assert(FOp);
+    auto *UI = cast<VPCmpIntrinsic>(ICA.getInst());
     return getCmpSelInstrCost(*FOp, ICA.getArgTypes()[0], ICA.getReturnType(),
                               UI->getPredicate(), CostKind);
   }



More information about the llvm-commits mailing list