Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Analysis] Add cost model for experimental.cttz.elts intrinsic #90720

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions llvm/include/llvm/CodeGen/BasicTTIImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/TargetTransformInfoImpl.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/CodeGen/ISDOpcodes.h"
#include "llvm/CodeGen/TargetLowering.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
Expand Down Expand Up @@ -1758,6 +1759,53 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
CmpInst::ICMP_ULT, CostKind);
return Cost;
}
case Intrinsic::experimental_cttz_elts: {
EVT ArgType = getTLI()->getValueType(DL, ICA.getArgTypes()[0], true);

// If we're not expanding the intrinsic then we assume this is cheap
// to implement.
if (!getTLI()->shouldExpandCttzElements(ArgType))
return getTypeLegalizationCost(RetTy).first;

// TODO: The costs below reflect the expansion code in
// SelectionDAGBuilder, but we may want to sacrifice some accuracy in
// favour of compile time.

// Find the smallest "sensible" element type to use for the expansion.
bool ZeroIsPoison = !cast<ConstantInt>(Args[1])->isZero();
ConstantRange VScaleRange(APInt(64, 1), APInt::getZero(64));
if (isa<ScalableVectorType>(ICA.getArgTypes()[0]) && I && I->getCaller())
VScaleRange = getVScaleRange(I->getCaller(), 64);

unsigned EltWidth = getTLI()->getBitWidthForCttzElements(
RetTy, ArgType.getVectorElementCount(), ZeroIsPoison, &VScaleRange);
Type *NewEltTy = IntegerType::getIntNTy(RetTy->getContext(), EltWidth);

// Create the new vector type & get the vector length
Type *NewVecTy = VectorType::get(
NewEltTy, cast<VectorType>(Args[0]->getType())->getElementCount());

IntrinsicCostAttributes StepVecAttrs(Intrinsic::experimental_stepvector,
NewVecTy, {}, FMF);
InstructionCost Cost =
thisT()->getIntrinsicInstrCost(StepVecAttrs, CostKind);

Cost +=
thisT()->getArithmeticInstrCost(Instruction::Sub, NewVecTy, CostKind);
Cost += thisT()->getCastInstrCost(Instruction::SExt, NewVecTy,
Args[0]->getType(),
TTI::CastContextHint::None, CostKind);
Cost +=
thisT()->getArithmeticInstrCost(Instruction::And, NewVecTy, CostKind);

IntrinsicCostAttributes ReducAttrs(Intrinsic::vector_reduce_umax,
NewEltTy, NewVecTy, FMF, I, 1);
Cost += thisT()->getTypeBasedIntrinsicInstrCost(ReducAttrs, CostKind);
Cost +=
thisT()->getArithmeticInstrCost(Instruction::Sub, NewEltTy, CostKind);

return Cost;
}
}

// VP Intrinsics should have the same cost as their non-vp counterpart.
Expand Down
6 changes: 6 additions & 0 deletions llvm/include/llvm/CodeGen/TargetLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,12 @@ class TargetLoweringBase {
/// expanded using generic code in SelectionDAGBuilder.
virtual bool shouldExpandCttzElements(EVT VT) const { return true; }

/// Return the minimum number of bits required to hold the maximum possible
/// number of trailing zero vector elements.
unsigned getBitWidthForCttzElements(Type *RetTy, ElementCount EC,
bool ZeroIsPoison,
const ConstantRange *VScaleRange) const;

// Return true if op(vecreduce(x), vecreduce(y)) should be reassociated to
// vecreduce(op(x, y)) for the reduction opcode RedOpc.
virtual bool shouldReassociateReduction(unsigned RedOpc, EVT VT) const {
Expand Down
19 changes: 7 additions & 12 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7861,20 +7861,15 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
Op = DAG.getSetCC(DL, OpVT, Op, AllZero, ISD::SETNE);
}

// Find the smallest "sensible" element type to use for the expansion.
ConstantRange CR(
APInt(64, OpVT.getVectorElementCount().getKnownMinValue()));
if (OpVT.isScalableVT())
CR = CR.umul_sat(getVScaleRange(I.getCaller(), 64));

// If the zero-is-poison flag is set, we can assume the upper limit
// of the result is VF-1.
if (!cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero())
CR = CR.subtract(APInt(64, 1));

unsigned EltWidth = I.getType()->getScalarSizeInBits();
EltWidth = std::min(EltWidth, (unsigned)CR.getActiveBits());
EltWidth = std::max(llvm::bit_ceil(EltWidth), (unsigned)8);
bool ZeroIsPoison =
!cast<ConstantSDNode>(getValue(I.getOperand(1)))->isZero();
ConstantRange VScaleRange(1, true); // Dummy value.
if (isa<ScalableVectorType>(I.getOperand(0)->getType()))
VScaleRange = getVScaleRange(I.getCaller(), 64);
unsigned EltWidth = TLI.getBitWidthForCttzElements(
I.getType(), OpVT.getVectorElementCount(), ZeroIsPoison, &VScaleRange);

MVT NewEltTy = MVT::getIntegerVT(EltWidth);

Expand Down
18 changes: 18 additions & 0 deletions llvm/lib/CodeGen/TargetLoweringBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,24 @@ bool TargetLoweringBase::isFreeAddrSpaceCast(unsigned SrcAS,
return TM.isNoopAddrSpaceCast(SrcAS, DestAS);
}

unsigned TargetLoweringBase::getBitWidthForCttzElements(
Type *RetTy, ElementCount EC, bool ZeroIsPoison,
const ConstantRange *VScaleRange) const {
// Find the smallest "sensible" element type to use for the expansion.
ConstantRange CR(APInt(64, EC.getKnownMinValue()));
if (EC.isScalable())
CR = CR.umul_sat(*VScaleRange);

if (ZeroIsPoison)
CR = CR.subtract(APInt(64, 1));

unsigned EltWidth = RetTy->getScalarSizeInBits();
EltWidth = std::min(EltWidth, (unsigned)CR.getActiveBits());
EltWidth = std::max(llvm::bit_ceil(EltWidth), (unsigned)8);

return EltWidth;
}

void TargetLoweringBase::setJumpIsExpensive(bool isExpensive) {
// If the command-line option was specified, ignore this request.
if (!JumpIsExpensiveOverride.getNumOccurrences())
Expand Down
Loading
Loading