Skip to content

Commit f40c694

Browse files
authored
[TTI] Use MemIntrinsicCostAttributes for getExpandCompressMemoryOpCost (#168677)
- Following #168029. This is a step toward a unified interface for masked/gather-scatter/strided/expand-compress cost modeling. - Replace the ad-hoc parameter list with a single attributes object. API change: ``` - InstructionCost getExpandCompressMemoryOpCost(Opcode, DataTy, - VariableMask, Alignment, - CostKind, Inst); + InstructionCost getExpandCompressMemoryOpCost(MemIntrinsicCostAttributes, + CostKind); ``` Notes: - NFCI intended: callers populate MemIntrinsicCostAttributes with same information as before.
1 parent 0c6d7a4 commit f40c694

File tree

6 files changed

+50
-33
lines changed

6 files changed

+50
-33
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,20 @@ struct HardwareLoopInfo {
125125

126126
/// Information for memory intrinsic cost model.
127127
class MemIntrinsicCostAttributes {
128+
/// Optional context instruction, if one exists, e.g. the
129+
/// load/store to transform to the intrinsic.
130+
const Instruction *I = nullptr;
131+
128132
/// Vector type of the data to be loaded or stored.
129133
Type *DataTy = nullptr;
130134

131135
/// ID of the memory intrinsic.
132136
Intrinsic::ID IID;
133137

138+
/// True when the memory access is predicated with a mask
139+
/// that is not a compile-time constant.
140+
bool VariableMask = true;
141+
134142
/// Address space of the pointer.
135143
unsigned AddressSpace = 0;
136144

@@ -143,8 +151,16 @@ class MemIntrinsicCostAttributes {
143151
: DataTy(DataTy), IID(Id), AddressSpace(AddressSpace),
144152
Alignment(Alignment) {}
145153

154+
LLVM_ABI MemIntrinsicCostAttributes(Intrinsic::ID Id, Type *DataTy,
155+
bool VariableMask, Align Alignment,
156+
const Instruction *I = nullptr)
157+
: I(I), DataTy(DataTy), IID(Id), VariableMask(VariableMask),
158+
Alignment(Alignment) {}
159+
146160
Intrinsic::ID getID() const { return IID; }
161+
const Instruction *getInst() const { return I; }
147162
Type *getDataType() const { return DataTy; }
163+
bool getVariableMask() const { return VariableMask; }
148164
unsigned getAddressSpace() const { return AddressSpace; }
149165
Align getAlignment() const { return Alignment; }
150166
};
@@ -1612,17 +1628,9 @@ class TargetTransformInfo {
16121628
const Instruction *I = nullptr) const;
16131629

16141630
/// \return The cost of Expand Load or Compress Store operation
1615-
/// \p Opcode - is a type of memory access Load or Store
1616-
/// \p Src - a vector type of the data to be loaded or stored
1617-
/// \p VariableMask - true when the memory access is predicated with a mask
1618-
/// that is not a compile-time constant
1619-
/// \p Alignment - alignment of single element
1620-
/// \p I - the optional original context instruction, if one exists, e.g. the
1621-
/// load/store to transform or the call to the gather/scatter intrinsic
16221631
LLVM_ABI InstructionCost getExpandCompressMemoryOpCost(
1623-
unsigned Opcode, Type *DataTy, bool VariableMask, Align Alignment,
1624-
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput,
1625-
const Instruction *I = nullptr) const;
1632+
const MemIntrinsicCostAttributes &MICA,
1633+
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput) const;
16261634

16271635
/// \return The cost of strided memory operations.
16281636
/// \p Opcode - is a type of memory access Load or Store

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -859,9 +859,9 @@ class TargetTransformInfoImplBase {
859859
return 1;
860860
}
861861

862-
virtual InstructionCost getExpandCompressMemoryOpCost(
863-
unsigned Opcode, Type *DataTy, bool VariableMask, Align Alignment,
864-
TTI::TargetCostKind CostKind, const Instruction *I = nullptr) const {
862+
virtual InstructionCost
863+
getExpandCompressMemoryOpCost(const MemIntrinsicCostAttributes &MICA,
864+
TTI::TargetCostKind CostKind) const {
865865
return 1;
866866
}
867867

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,10 +1580,14 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
15801580
}
15811581

15821582
InstructionCost
1583-
getExpandCompressMemoryOpCost(unsigned Opcode, Type *DataTy,
1584-
bool VariableMask, Align Alignment,
1585-
TTI::TargetCostKind CostKind,
1586-
const Instruction *I = nullptr) const override {
1583+
getExpandCompressMemoryOpCost(const MemIntrinsicCostAttributes &MICA,
1584+
TTI::TargetCostKind CostKind) const override {
1585+
unsigned Opcode = MICA.getID() == Intrinsic::masked_expandload
1586+
? Instruction::Load
1587+
: Instruction::Store;
1588+
Type *DataTy = MICA.getDataType();
1589+
bool VariableMask = MICA.getVariableMask();
1590+
Align Alignment = MICA.getAlignment();
15871591
// Treat expand load/compress store as gather/scatter operation.
15881592
// TODO: implement more precise cost estimation for these intrinsics.
15891593
return getCommonMaskedMemoryOpCost(Opcode, DataTy, Alignment, VariableMask,
@@ -1964,15 +1968,17 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
19641968
const Value *Mask = Args[2];
19651969
Align Alignment = I->getParamAlign(1).valueOrOne();
19661970
return thisT()->getExpandCompressMemoryOpCost(
1967-
Instruction::Store, Data->getType(), !isa<Constant>(Mask), Alignment,
1968-
CostKind, I);
1971+
MemIntrinsicCostAttributes(IID, Data->getType(), !isa<Constant>(Mask),
1972+
Alignment, I),
1973+
CostKind);
19691974
}
19701975
case Intrinsic::masked_expandload: {
19711976
const Value *Mask = Args[1];
19721977
Align Alignment = I->getParamAlign(0).valueOrOne();
1973-
return thisT()->getExpandCompressMemoryOpCost(Instruction::Load, RetTy,
1974-
!isa<Constant>(Mask),
1975-
Alignment, CostKind, I);
1978+
return thisT()->getExpandCompressMemoryOpCost(
1979+
MemIntrinsicCostAttributes(IID, RetTy, !isa<Constant>(Mask),
1980+
Alignment, I),
1981+
CostKind);
19761982
}
19771983
case Intrinsic::experimental_vp_strided_store: {
19781984
const Value *Data = Args[0];

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,10 +1209,9 @@ InstructionCost TargetTransformInfo::getGatherScatterOpCost(
12091209
}
12101210

12111211
InstructionCost TargetTransformInfo::getExpandCompressMemoryOpCost(
1212-
unsigned Opcode, Type *DataTy, bool VariableMask, Align Alignment,
1213-
TTI::TargetCostKind CostKind, const Instruction *I) const {
1214-
InstructionCost Cost = TTIImpl->getExpandCompressMemoryOpCost(
1215-
Opcode, DataTy, VariableMask, Alignment, CostKind, I);
1212+
const MemIntrinsicCostAttributes &MICA,
1213+
TTI::TargetCostKind CostKind) const {
1214+
InstructionCost Cost = TTIImpl->getExpandCompressMemoryOpCost(MICA, CostKind);
12161215
assert(Cost >= 0 && "TTI should not produce negative costs!");
12171216
return Cost;
12181217
}

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,15 +1146,20 @@ InstructionCost RISCVTTIImpl::getGatherScatterOpCost(
11461146
}
11471147

11481148
InstructionCost RISCVTTIImpl::getExpandCompressMemoryOpCost(
1149-
unsigned Opcode, Type *DataTy, bool VariableMask, Align Alignment,
1150-
TTI::TargetCostKind CostKind, const Instruction *I) const {
1149+
const MemIntrinsicCostAttributes &MICA,
1150+
TTI::TargetCostKind CostKind) const {
1151+
unsigned Opcode = MICA.getID() == Intrinsic::masked_expandload
1152+
? Instruction::Load
1153+
: Instruction::Store;
1154+
Type *DataTy = MICA.getDataType();
1155+
bool VariableMask = MICA.getVariableMask();
1156+
Align Alignment = MICA.getAlignment();
11511157
bool IsLegal = (Opcode == Instruction::Store &&
11521158
isLegalMaskedCompressStore(DataTy, Alignment)) ||
11531159
(Opcode == Instruction::Load &&
11541160
isLegalMaskedExpandLoad(DataTy, Alignment));
11551161
if (!IsLegal || CostKind != TTI::TCK_RecipThroughput)
1156-
return BaseT::getExpandCompressMemoryOpCost(Opcode, DataTy, VariableMask,
1157-
Alignment, CostKind, I);
1162+
return BaseT::getExpandCompressMemoryOpCost(MICA, CostKind);
11581163
// Example compressstore sequence:
11591164
// vsetivli zero, 8, e32, m2, ta, ma (ignored)
11601165
// vcompress.vm v10, v8, v0

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,8 @@ class RISCVTTIImpl final : public BasicTTIImplBase<RISCVTTIImpl> {
197197
const Instruction *I) const override;
198198

199199
InstructionCost
200-
getExpandCompressMemoryOpCost(unsigned Opcode, Type *Src, bool VariableMask,
201-
Align Alignment, TTI::TargetCostKind CostKind,
202-
const Instruction *I = nullptr) const override;
200+
getExpandCompressMemoryOpCost(const MemIntrinsicCostAttributes &MICA,
201+
TTI::TargetCostKind CostKind) const override;
203202

204203
InstructionCost getStridedMemoryOpCost(unsigned Opcode, Type *DataTy,
205204
const Value *Ptr, bool VariableMask,

0 commit comments

Comments
 (0)