[llvm] [NVPTX] Improve modeling of inline PTX (PR #130675)
Alex MacLean via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 25 10:16:00 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
+ // things that look like instructions, possibly with a predicate ("@").
+ return !AsmInst.empty() && (AsmInst[0] == '@' || isAlpha(AsmInst[0]) ||
+ AsmInst.find(".pragma") != StringRef::npos);
+ });
+ return InstCount * TargetTransformInfo::TCC_Basic;
----------------
AlexMaclean wrote:
Yea, this estimate is going to be inaccurate in some cases. In general, our cost model for NVPTX is pretty lacking. I think that this makes inline assembly roughly consistent with other instructions. In most cases, exotic and costly instructions will likely have side-effects or memory clobbers which should prevent most of the optimizations that would rely on this cost heuristic.
https://github.com/llvm/llvm-project/pull/130675
More information about the llvm-commits
mailing list