[llvm] [NVPTX] Improve modeling of inline PTX (PR #130675)

Alex MacLean via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 25 10:06:33 PDT 2025


================
@@ -483,6 +484,34 @@ NVPTXTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
   return std::nullopt;
 }
 
+InstructionCost
+NVPTXTTIImpl::getInstructionCost(const User *U,
+                                 ArrayRef<const Value *> Operands,
+                                 TTI::TargetCostKind CostKind) {
+  if (const auto *CI = dyn_cast<CallInst>(U))
+    if (const auto *IA = dyn_cast<InlineAsm>(CI->getCalledOperand())) {
+      // Without this implementation getCallCost() would return the number
+      // of arguments+1 as the cost. Because the cost-model assumes it is a call
+      // since it is classified as a call in the IR. A better cost model would
+      // be to return the number of asm instructions embedded in the asm
+      // string.
+      auto &AsmStr = IA->getAsmString();
+      SmallVector<StringRef, 4> AsmPieces;
+      SplitString(AsmStr, AsmPieces, ";\n");
+
+      const unsigned InstCount = count_if(AsmPieces, [](StringRef AsmInst) {
+        AsmInst = AsmInst.trim();
+        // This is pretty course but does a reasonably good job of identifying
----------------
AlexMaclean wrote:

Oops, fixed

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


More information about the llvm-commits mailing list