[llvm] [AArch64][CostModel] Fix stale costs for "ctpop" Intrinsic (PR #191769)
Yashwant Singh via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 13 00:57:15 PDT 2026
https://github.com/yashssh created https://github.com/llvm/llvm-project/pull/191769
https://reviews.llvm.org/D103952 Introduced codegen based costs
for ctpop intrinsic variants. However, with recent compiler the
codegen is more optimised but the costs have remained the same.
Old codegen: https://godbolt.org/z/YGeEGbqMY
New codegen: https://godbolt.org/z/jr3GjT9EK
>From fdbd71ff4fd0bc88552a9014145fdb0f0fc3b046 Mon Sep 17 00:00:00 2001
From: Yashwant Singh <yashwants at nvidia.com>
Date: Sun, 12 Apr 2026 22:17:33 -0700
Subject: [PATCH] [AArch64][CostModel] Fix stale costs for "ctpop" Intrinsics
https://reviews.llvm.org/D103952 Introudced codegen based costs
for ctpop intrinsic variants. However, with recent compiler the
codegen looks much different but the costs have remained the same.
Old codegen: https://godbolt.org/z/YGeEGbqMY
New codegen: https://godbolt.org/z/jr3GjT9EK
---
.../Target/AArch64/AArch64TargetTransformInfo.cpp | 13 +++++++++----
llvm/test/Analysis/CostModel/AArch64/ctpop.ll | 4 ++--
2 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
index 734339e5c7a05..9fb9758c96197 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
@@ -811,15 +811,20 @@ AArch64TTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
{ISD::CTPOP, MVT::v2i32, 3},
{ISD::CTPOP, MVT::v4i16, 2},
{ISD::CTPOP, MVT::v8i8, 1},
- {ISD::CTPOP, MVT::i32, 5},
+ {ISD::CTPOP, MVT::i32, 4},
};
+ EVT VT = TLI->getValueType(DL, RetTy, true);
+ // i8 ctpop can avoid the uaddlv after cnt since only one byte is active,
+ // making it cheaper than the legalized i32 cost.
+ if (VT == MVT::i8)
+ return 4;
+
auto LT = getTypeLegalizationCost(RetTy);
MVT MTy = LT.second;
if (const auto *Entry = CostTableLookup(CtpopCostTbl, ISD::CTPOP, MTy)) {
- // Extra cost of +1 when illegal vector types are legalized by promoting
+ // Extra cost of +1 when illegal vector/scalar types are legalized by promoting
// the integer type.
- int ExtraCost = MTy.isVector() && MTy.getScalarSizeInBits() !=
- RetTy->getScalarSizeInBits()
+ int ExtraCost = MTy.getScalarSizeInBits() != RetTy->getScalarSizeInBits()
? 1
: 0;
return LT.first * Entry->Cost + ExtraCost;
diff --git a/llvm/test/Analysis/CostModel/AArch64/ctpop.ll b/llvm/test/Analysis/CostModel/AArch64/ctpop.ll
index 013432991f5ae..1a48d9d37d03e 100644
--- a/llvm/test/Analysis/CostModel/AArch64/ctpop.ll
+++ b/llvm/test/Analysis/CostModel/AArch64/ctpop.ll
@@ -16,7 +16,7 @@ define i64 @test_ctpop_i64(i64 %a) {
define i32 @test_ctpop_i32(i32 %a) {
; CHECK-LABEL: 'test_ctpop_i32'
-; CHECK-NEXT: Cost Model: Found costs of 5 for: %ctpop = call i32 @llvm.ctpop.i32(i32 %a)
+; CHECK-NEXT: Cost Model: Found costs of 4 for: %ctpop = call i32 @llvm.ctpop.i32(i32 %a)
; CHECK-NEXT: Cost Model: Found costs of RThru:0 CodeSize:1 Lat:1 SizeLat:1 for: ret i32 %ctpop
;
%ctpop = call i32 @llvm.ctpop.i32(i32 %a)
@@ -34,7 +34,7 @@ define i16 @test_ctpop_i16(i16 %a) {
define i8 @test_ctpop_i8(i8 %a) {
; CHECK-LABEL: 'test_ctpop_i8'
-; CHECK-NEXT: Cost Model: Found costs of 5 for: %ctpop = call i8 @llvm.ctpop.i8(i8 %a)
+; CHECK-NEXT: Cost Model: Found costs of 4 for: %ctpop = call i8 @llvm.ctpop.i8(i8 %a)
; CHECK-NEXT: Cost Model: Found costs of RThru:0 CodeSize:1 Lat:1 SizeLat:1 for: ret i8 %ctpop
;
%ctpop = call i8 @llvm.ctpop.i8(i8 %a)
More information about the llvm-commits
mailing list