Skip to content

Commit aa08b1a

Browse files
authored
[TTI][RISCV] Add cost modelling for intrinsic vp.load.ff (llvm#160470)
Split out from llvm#151300 to isolate TargetTransformInfo cost modelling for fault-only-first loads from VPlan implementation details. This change adds costing support for vp.load.ff independently of the VPlan work. For now, model a vp.load.ff as cost-equivalent to a vp.load.
1 parent 9b270fc commit aa08b1a

File tree

9 files changed

+128
-65
lines changed

9 files changed

+128
-65
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,11 @@ class TargetTransformInfo {
16191619
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput,
16201620
bool UseMaskForCond = false, bool UseMaskForGaps = false) const;
16211621

1622+
/// \return The cost of vp intrinsic vp.load.ff.
1623+
LLVM_ABI InstructionCost getFirstFaultLoadCost(
1624+
Type *DataTy, Align Alignment,
1625+
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput) const;
1626+
16221627
/// A helper function to determine the type of reduction algorithm used
16231628
/// for a given \p Opcode and set of FastMathFlags \p FMF.
16241629
static bool requiresOrderedReduction(std::optional<FastMathFlags> FMF) {

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,12 @@ class TargetTransformInfoImplBase {
885885
return 1;
886886
}
887887

888+
virtual InstructionCost
889+
getFirstFaultLoadCost(Type *DataTy, Align Alignment,
890+
TTI::TargetCostKind CostKind) const {
891+
return InstructionCost::getInvalid();
892+
}
893+
888894
virtual InstructionCost
889895
getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
890896
TTI::TargetCostKind CostKind) const {

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,14 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
17951795
}
17961796
}
17971797

1798+
if (ICA.getID() == Intrinsic::vp_load_ff) {
1799+
Type *RetTy = ICA.getReturnType();
1800+
Type *DataTy = cast<StructType>(RetTy)->getElementType(0);
1801+
Align Alignment;
1802+
if (auto *VPI = dyn_cast_or_null<VPIntrinsic>(ICA.getInst()))
1803+
Alignment = VPI->getPointerAlignment().valueOrOne();
1804+
return thisT()->getFirstFaultLoadCost(DataTy, Alignment, CostKind);
1805+
}
17981806
if (ICA.getID() == Intrinsic::vp_scatter) {
17991807
if (ICA.isTypeBasedOnly()) {
18001808
IntrinsicCostAttributes MaskedScatter(

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,15 @@ InstructionCost TargetTransformInfo::getInterleavedMemoryOpCost(
12181218
return Cost;
12191219
}
12201220

1221+
InstructionCost
1222+
TargetTransformInfo::getFirstFaultLoadCost(Type *DataTy, Align Alignment,
1223+
TTI::TargetCostKind CostKind) const {
1224+
InstructionCost Cost =
1225+
TTIImpl->getFirstFaultLoadCost(DataTy, Alignment, CostKind);
1226+
assert(Cost >= 0 && "TTI should not produce negative costs!");
1227+
return Cost;
1228+
}
1229+
12211230
InstructionCost
12221231
TargetTransformInfo::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
12231232
TTI::TargetCostKind CostKind) const {

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24884,6 +24884,22 @@ bool RISCVTargetLowering::isLegalStridedLoadStore(EVT DataType,
2488424884
return true;
2488524885
}
2488624886

24887+
bool RISCVTargetLowering::isLegalFirstFaultLoad(EVT DataType,
24888+
Align Alignment) const {
24889+
if (!Subtarget.hasVInstructions())
24890+
return false;
24891+
24892+
EVT ScalarType = DataType.getScalarType();
24893+
if (!isLegalElementTypeForRVV(ScalarType))
24894+
return false;
24895+
24896+
if (!Subtarget.enableUnalignedVectorMem() &&
24897+
Alignment < ScalarType.getStoreSize())
24898+
return false;
24899+
24900+
return true;
24901+
}
24902+
2488724903
MachineInstr *
2488824904
RISCVTargetLowering::EmitKCFICheck(MachineBasicBlock &MBB,
2488924905
MachineBasicBlock::instr_iterator &MBBI,

llvm/lib/Target/RISCV/RISCVISelLowering.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,10 @@ class RISCVTargetLowering : public TargetLowering {
425425
/// alignment is legal.
426426
bool isLegalStridedLoadStore(EVT DataType, Align Alignment) const;
427427

428+
/// Return true if a fault-only-first load of the given result type and
429+
/// alignment is legal.
430+
bool isLegalFirstFaultLoad(EVT DataType, Align Alignment) const;
431+
428432
unsigned getMaxSupportedInterleaveFactor() const override { return 8; }
429433

430434
bool fallBackToDAGISel(const Instruction &Inst) const override;

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,17 @@ InstructionCost RISCVTTIImpl::getInterleavedMemoryOpCost(
10701070
return MemCost + ShuffleCost;
10711071
}
10721072

1073+
InstructionCost
1074+
RISCVTTIImpl::getFirstFaultLoadCost(Type *DataTy, Align Alignment,
1075+
TTI::TargetCostKind CostKind) const {
1076+
EVT DataTypeVT = TLI->getValueType(DL, DataTy);
1077+
if (!TLI->isLegalFirstFaultLoad(DataTypeVT, Alignment))
1078+
return BaseT::getFirstFaultLoadCost(DataTy, Alignment, CostKind);
1079+
1080+
return getMemoryOpCost(Instruction::Load, DataTy, Alignment, 0, CostKind,
1081+
{TTI::OK_AnyValue, TTI::OP_None}, nullptr);
1082+
}
1083+
10731084
InstructionCost RISCVTTIImpl::getGatherScatterOpCost(
10741085
unsigned Opcode, Type *DataTy, const Value *Ptr, bool VariableMask,
10751086
Align Alignment, TTI::TargetCostKind CostKind, const Instruction *I) const {

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ class RISCVTTIImpl final : public BasicTTIImplBase<RISCVTTIImpl> {
191191
Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind,
192192
bool UseMaskForCond = false, bool UseMaskForGaps = false) const override;
193193

194+
InstructionCost
195+
getFirstFaultLoadCost(Type *DataTy, Align Alignment,
196+
TTI::TargetCostKind CostKind) const override;
197+
194198
InstructionCost getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
195199
const Value *Ptr, bool VariableMask,
196200
Align Alignment,

0 commit comments

Comments
 (0)