[llvm] [AArch64][CostModel] Add cost model for pdep/pext instructions (PR #207657)
Yashwant Singh via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 8 00:11:44 PDT 2026
https://github.com/yashssh updated https://github.com/llvm/llvm-project/pull/207657
>From 9d2ab443e1c2ca91c925cdf73cfdc7253d4566da Mon Sep 17 00:00:00 2001
From: Yashwant Singh <yashwants at nvidia.com>
Date: Sun, 5 Jul 2026 22:41:15 -0700
Subject: [PATCH 1/2] [AArch64][CostModel] Add cost model for pdep/pext
instructions
---
.../Target/AArch64/AArch64ISelLowering.cpp | 5 +-
llvm/lib/Target/AArch64/AArch64Subtarget.h | 8 ++++
.../AArch64/AArch64TargetTransformInfo.cpp | 23 +++++++++
.../Analysis/CostModel/AArch64/pdep-pext.ll | 47 +++++++++++++++++++
4 files changed, 79 insertions(+), 4 deletions(-)
create mode 100644 llvm/test/Analysis/CostModel/AArch64/pdep-pext.ll
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 7df8aa045dbf9..f72c975e07660 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -2161,10 +2161,7 @@ AArch64TargetLowering::AArch64TargetLowering(const TargetMachine &TM,
}
// Map generic PEXT/PDEP to SVE2 bitperm BEXT/BDEP instructions.
- if (Subtarget->hasSVEBitPerm() &&
- (Subtarget->isSVEAvailable() ||
- (Subtarget->isSVEorStreamingSVEAvailable() &&
- Subtarget->hasSSVE_BitPerm()))) {
+ if (Subtarget->isSVEBitPermAvailable()) {
for (auto VT : {MVT::nxv16i8, MVT::nxv8i16, MVT::nxv4i32, MVT::nxv2i64}) {
setOperationAction({ISD::PEXT, ISD::PDEP}, VT, Custom);
}
diff --git a/llvm/lib/Target/AArch64/AArch64Subtarget.h b/llvm/lib/Target/AArch64/AArch64Subtarget.h
index b8303224a8976..9404ec2643a54 100644
--- a/llvm/lib/Target/AArch64/AArch64Subtarget.h
+++ b/llvm/lib/Target/AArch64/AArch64Subtarget.h
@@ -237,6 +237,14 @@ class AArch64Subtarget final : public AArch64GenSubtargetInfo {
return isSVEAvailable() || (isSVEorStreamingSVEAvailable() && hasSME2());
}
+ /// Returns true if SVE bit-permute instructions (bext/bdep/bgrp) can be
+ /// emitted.
+ bool isSVEBitPermAvailable() const {
+ return hasSVEBitPerm() &&
+ (isSVEAvailable() ||
+ (isSVEorStreamingSVEAvailable() && hasSSVE_BitPerm()));
+ }
+
unsigned getMinVectorRegisterBitWidth() const {
// Don't assume any minimum vector size when PSTATE.SM may not be 0, because
// we don't yet support streaming-compatible codegen support that we trust
diff --git a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
index edd95625e13ea..be031d2c7b5af 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
@@ -863,6 +863,29 @@ AArch64TTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
}
break;
}
+ case Intrinsic::pdep:
+ case Intrinsic::pext: {
+ unsigned BW = RetTy->getScalarSizeInBits();
+ if (ST->isSVEBitPermAvailable() && BW <= 64) {
+ Type *VecTy = VectorType::get(RetTy, 64 / BW, false);
+ InstructionCost Cost =
+ getVectorInstrCost(Instruction::InsertElement, VecTy, CostKind, 0,
+ nullptr, nullptr) +
+ getVectorInstrCost(Instruction::InsertElement, VecTy, CostKind, 0,
+ nullptr, nullptr) +
+ 1 +
+ getVectorInstrCost(Instruction::ExtractElement, VecTy, CostKind, 0,
+ nullptr, nullptr);
+ return Cost;
+ }
+ // Without bit-permute, pdep/pext expand to the Hacker's Delight scalar
+ // sequence: O(log2(BW)) parallel-prefix stages, each a CLMUL-by-all-ones
+ // (~log2(BW) shift-xors) plus a handful of scalar ALU ops. That is
+ // O(log2(BW)^2) work; cost ~ LogBW * (LogBW + 7) matches the emitted
+ // sequence (~48/61/79 insns for i16/i32/i64).
+ unsigned LogBW = Log2_32_Ceil(BW);
+ return getTypeLegalizationCost(RetTy).first * LogBW * (LogBW + 7);
+ }
case Intrinsic::ctpop: {
if (!ST->hasNEON()) {
// 32-bit or 64-bit ctpop without NEON is 12 instructions.
diff --git a/llvm/test/Analysis/CostModel/AArch64/pdep-pext.ll b/llvm/test/Analysis/CostModel/AArch64/pdep-pext.ll
new file mode 100644
index 0000000000000..1960a2d1514b7
--- /dev/null
+++ b/llvm/test/Analysis/CostModel/AArch64/pdep-pext.ll
@@ -0,0 +1,47 @@
+; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
+; RUN: opt < %s -passes="print<cost-model>" 2>&1 -disable-output -mtriple=aarch64-- | FileCheck %s --check-prefixes=CHECK-NO-BITPERM
+; RUN: opt < %s -passes="print<cost-model>" 2>&1 -disable-output -mtriple=aarch64-- -mattr=+sve2-bitperm | FileCheck %s --check-prefixes=CHECK-BITPERM
+
+define void @pdep() {
+; CHECK-NO-BITPERM-LABEL: 'pdep'
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 30 for instruction: %i8 = call i8 @llvm.pdep.i8(i8 poison, i8 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 44 for instruction: %i16 = call i16 @llvm.pdep.i16(i16 poison, i16 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 60 for instruction: %i32 = call i32 @llvm.pdep.i32(i32 poison, i32 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 78 for instruction: %i64 = call i64 @llvm.pdep.i64(i64 poison, i64 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
+;
+; CHECK-BITPERM-LABEL: 'pdep'
+; CHECK-BITPERM-NEXT: Cost Model: Found an estimated cost of 7 for instruction: %i8 = call i8 @llvm.pdep.i8(i8 poison, i8 poison)
+; CHECK-BITPERM-NEXT: Cost Model: Found an estimated cost of 7 for instruction: %i16 = call i16 @llvm.pdep.i16(i16 poison, i16 poison)
+; CHECK-BITPERM-NEXT: Cost Model: Found an estimated cost of 7 for instruction: %i32 = call i32 @llvm.pdep.i32(i32 poison, i32 poison)
+; CHECK-BITPERM-NEXT: Cost Model: Found an estimated cost of 7 for instruction: %i64 = call i64 @llvm.pdep.i64(i64 poison, i64 poison)
+; CHECK-BITPERM-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
+;
+ %i8 = call i8 @llvm.pdep.i8(i8 poison, i8 poison)
+ %i16 = call i16 @llvm.pdep.i16(i16 poison, i16 poison)
+ %i32 = call i32 @llvm.pdep.i32(i32 poison, i32 poison)
+ %i64 = call i64 @llvm.pdep.i64(i64 poison, i64 poison)
+ ret void
+}
+
+define void @pext() {
+; CHECK-NO-BITPERM-LABEL: 'pext'
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 30 for instruction: %i8 = call i8 @llvm.pext.i8(i8 poison, i8 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 44 for instruction: %i16 = call i16 @llvm.pext.i16(i16 poison, i16 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 60 for instruction: %i32 = call i32 @llvm.pext.i32(i32 poison, i32 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 78 for instruction: %i64 = call i64 @llvm.pext.i64(i64 poison, i64 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
+;
+; CHECK-BITPERM-LABEL: 'pext'
+; CHECK-BITPERM-NEXT: Cost Model: Found an estimated cost of 7 for instruction: %i8 = call i8 @llvm.pext.i8(i8 poison, i8 poison)
+; CHECK-BITPERM-NEXT: Cost Model: Found an estimated cost of 7 for instruction: %i16 = call i16 @llvm.pext.i16(i16 poison, i16 poison)
+; CHECK-BITPERM-NEXT: Cost Model: Found an estimated cost of 7 for instruction: %i32 = call i32 @llvm.pext.i32(i32 poison, i32 poison)
+; CHECK-BITPERM-NEXT: Cost Model: Found an estimated cost of 7 for instruction: %i64 = call i64 @llvm.pext.i64(i64 poison, i64 poison)
+; CHECK-BITPERM-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
+;
+ %i8 = call i8 @llvm.pext.i8(i8 poison, i8 poison)
+ %i16 = call i16 @llvm.pext.i16(i16 poison, i16 poison)
+ %i32 = call i32 @llvm.pext.i32(i32 poison, i32 poison)
+ %i64 = call i64 @llvm.pext.i64(i64 poison, i64 poison)
+ ret void
+}
>From fbcfdec0834def8e3cf35cf1efdc7a27de71448b Mon Sep 17 00:00:00 2001
From: Yashwant Singh <yashwants at nvidia.com>
Date: Wed, 8 Jul 2026 00:10:20 -0700
Subject: [PATCH 2/2] Address review comments
---
llvm/include/llvm/CodeGen/BasicTTIImpl.h | 6 ++++++
.../AArch64/AArch64TargetTransformInfo.cpp | 10 ++--------
.../test/Analysis/CostModel/AArch64/pdep-pext.ll | 16 ++++++++--------
3 files changed, 16 insertions(+), 16 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/BasicTTIImpl.h b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
index 46e18593430a2..a98fed8b958dd 100644
--- a/llvm/include/llvm/CodeGen/BasicTTIImpl.h
+++ b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
@@ -2739,6 +2739,12 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
case Intrinsic::bitreverse:
ISD = ISD::BITREVERSE;
break;
+ case Intrinsic::pdep:
+ case Intrinsic::pext:
+ ISD = IID == Intrinsic::pdep ? ISD::PDEP : ISD::PEXT;
+ // When not legal/custom, pdep/pext expand to a long inline sequence
+ SingleCallCost = 20;
+ break;
case Intrinsic::ucmp:
ISD = ISD::UCMP;
break;
diff --git a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
index be031d2c7b5af..4a7e1119c245d 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
@@ -866,7 +866,7 @@ AArch64TTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
case Intrinsic::pdep:
case Intrinsic::pext: {
unsigned BW = RetTy->getScalarSizeInBits();
- if (ST->isSVEBitPermAvailable() && BW <= 64) {
+ if (!RetTy->isVectorTy() && ST->isSVEBitPermAvailable() && BW <= 64) {
Type *VecTy = VectorType::get(RetTy, 64 / BW, false);
InstructionCost Cost =
getVectorInstrCost(Instruction::InsertElement, VecTy, CostKind, 0,
@@ -878,13 +878,7 @@ AArch64TTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
nullptr, nullptr);
return Cost;
}
- // Without bit-permute, pdep/pext expand to the Hacker's Delight scalar
- // sequence: O(log2(BW)) parallel-prefix stages, each a CLMUL-by-all-ones
- // (~log2(BW) shift-xors) plus a handful of scalar ALU ops. That is
- // O(log2(BW)^2) work; cost ~ LogBW * (LogBW + 7) matches the emitted
- // sequence (~48/61/79 insns for i16/i32/i64).
- unsigned LogBW = Log2_32_Ceil(BW);
- return getTypeLegalizationCost(RetTy).first * LogBW * (LogBW + 7);
+ break;
}
case Intrinsic::ctpop: {
if (!ST->hasNEON()) {
diff --git a/llvm/test/Analysis/CostModel/AArch64/pdep-pext.ll b/llvm/test/Analysis/CostModel/AArch64/pdep-pext.ll
index 1960a2d1514b7..90d38dfc698d7 100644
--- a/llvm/test/Analysis/CostModel/AArch64/pdep-pext.ll
+++ b/llvm/test/Analysis/CostModel/AArch64/pdep-pext.ll
@@ -4,10 +4,10 @@
define void @pdep() {
; CHECK-NO-BITPERM-LABEL: 'pdep'
-; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 30 for instruction: %i8 = call i8 @llvm.pdep.i8(i8 poison, i8 poison)
-; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 44 for instruction: %i16 = call i16 @llvm.pdep.i16(i16 poison, i16 poison)
-; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 60 for instruction: %i32 = call i32 @llvm.pdep.i32(i32 poison, i32 poison)
-; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 78 for instruction: %i64 = call i64 @llvm.pdep.i64(i64 poison, i64 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 20 for instruction: %i8 = call i8 @llvm.pdep.i8(i8 poison, i8 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 20 for instruction: %i16 = call i16 @llvm.pdep.i16(i16 poison, i16 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 20 for instruction: %i32 = call i32 @llvm.pdep.i32(i32 poison, i32 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 20 for instruction: %i64 = call i64 @llvm.pdep.i64(i64 poison, i64 poison)
; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
;
; CHECK-BITPERM-LABEL: 'pdep'
@@ -26,10 +26,10 @@ define void @pdep() {
define void @pext() {
; CHECK-NO-BITPERM-LABEL: 'pext'
-; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 30 for instruction: %i8 = call i8 @llvm.pext.i8(i8 poison, i8 poison)
-; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 44 for instruction: %i16 = call i16 @llvm.pext.i16(i16 poison, i16 poison)
-; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 60 for instruction: %i32 = call i32 @llvm.pext.i32(i32 poison, i32 poison)
-; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 78 for instruction: %i64 = call i64 @llvm.pext.i64(i64 poison, i64 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 20 for instruction: %i8 = call i8 @llvm.pext.i8(i8 poison, i8 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 20 for instruction: %i16 = call i16 @llvm.pext.i16(i16 poison, i16 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 20 for instruction: %i32 = call i32 @llvm.pext.i32(i32 poison, i32 poison)
+; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 20 for instruction: %i64 = call i64 @llvm.pext.i64(i64 poison, i64 poison)
; CHECK-NO-BITPERM-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
;
; CHECK-BITPERM-LABEL: 'pext'
More information about the llvm-commits
mailing list