Skip to content

Commit

Permalink
[Analysis] Add cost model for experimental.cttz.elts intrinsic (llvm#…
Browse files Browse the repository at this point in the history
…90720)

In PR llvm#88385 I've added support for auto-vectorisation of some early
exit loops, which requires using the experimental.cttz.elts to calculate
final indices in the early exit block. We need a more accurate cost
model for this intrinsic to better reflect the cost of work required in
the early exit block. I've tried to accurately represent the expansion
code for the intrinsic when the target does not have efficient lowering
for it. It's quite tricky to model because you need to first figure out
what types will actually be used in the expansion. The type used can
have a significant effect on the cost if you end up using illegal vector
types.

Tests added here:

  Analysis/CostModel/AArch64/cttz_elts.ll
  Analysis/CostModel/RISCV/cttz_elts.ll
  • Loading branch information
david-arm authored May 9, 2024
1 parent febd89c commit b52fa94
Show file tree
Hide file tree
Showing 6 changed files with 437 additions and 12 deletions.
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

0 comments on commit b52fa94

Please sign in to comment.