Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
104 changes: 103 additions & 1 deletion llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,109 @@ InstructionCost RISCVTTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
// First, handle cases where having a fixed length vector enables us to
// give a more accurate cost than falling back to generic scalable codegen.
// TODO: Each of these cases hints at a modeling gap around scalable vectors.
if (isa<FixedVectorType>(Tp)) {
if (ST->hasVInstructions() && isa<FixedVectorType>(Tp)) {
MVT LegalVT = LT.second;
InstructionCost NumOfDests = InstructionCost::getInvalid();
const auto VLen = ST->getRealVLen();
if (VLen && LegalVT.isFixedLengthVector() && !Mask.empty()) {
MVT ElemVT = LegalVT.getVectorElementType();
unsigned ElemsPerVReg = *VLen / ElemVT.getFixedSizeInBits();
LegalVT = getTypeLegalizationCost(
FixedVectorType::get(Tp->getElementType(), ElemsPerVReg))
.second;
NumOfDests = divideCeil(Mask.size(), LegalVT.getVectorNumElements());
}
if (NumOfDests.isValid() && NumOfDests > 1 &&
LegalVT.isFixedLengthVector() &&
LegalVT.getVectorElementType().getSizeInBits() ==
Tp->getElementType()->getPrimitiveSizeInBits() &&
LegalVT.getVectorNumElements() <
Tp->getElementCount().getFixedValue()) {
unsigned VecTySize = DL.getTypeStoreSize(Tp);
unsigned LegalVTSize = LegalVT.getStoreSize();
// Number of source vectors after legalization:
unsigned NumOfSrcs = divideCeil(VecTySize, LegalVTSize);
// Number of destination vectors after legalization:

auto *SingleOpTy = FixedVectorType::get(Tp->getElementType(),
LegalVT.getVectorNumElements());

// Try to perform better estimation of the permutation.
// 1. Split the source/destination vectors into real registers.
// 2. Do the mask analysis to identify which real registers are
// permuted. If more than 1 source registers are used for the
// destination register building, the cost for this destination register
// is (Number_of_source_register - 1) * Cost_PermuteTwoSrc. If only one
// source register is used, build mask and calculate the cost as a cost
// of PermuteSingleSrc.
// Also, for the single register permute we try to identify if the
// destination register is just a copy of the source register or the
// copy of the previous destination register (the cost is
// TTI::TCC_Basic). If the source register is just reused, the cost for
// this operation is 0.
unsigned E = *NumOfDests.getValue();
unsigned NormalizedVF =
LegalVT.getVectorNumElements() * std::max(NumOfSrcs, E);
unsigned NumOfSrcRegs = NormalizedVF / LegalVT.getVectorNumElements();
unsigned NumOfDestRegs = NormalizedVF / LegalVT.getVectorNumElements();
SmallVector<int> NormalizedMask(NormalizedVF, PoisonMaskElem);
copy(Mask, NormalizedMask.begin());
unsigned PrevSrcReg = 0;
ArrayRef<int> PrevRegMask;
InstructionCost Cost = 0;
SmallBitVector ExtractedRegs(2 * NumOfSrcRegs);
processShuffleMasks(
NormalizedMask, NumOfSrcRegs, NumOfDestRegs, NumOfDestRegs, []() {},
[&](ArrayRef<int> RegMask, unsigned SrcReg, unsigned DestReg) {
if (ExtractedRegs.test(SrcReg)) {
Cost += getShuffleCost(TTI::SK_ExtractSubvector, Tp, {}, CostKind,
(SrcReg % NumOfSrcRegs) *
SingleOpTy->getNumElements(),
SingleOpTy);
ExtractedRegs.set(SrcReg);
}
if (!ShuffleVectorInst::isIdentityMask(RegMask, RegMask.size())) {
// Check if the previous register can be just copied to the next
// one.
if (PrevRegMask.empty() || PrevSrcReg != SrcReg ||
PrevRegMask != RegMask) {
Cost += getShuffleCost(TTI::SK_PermuteSingleSrc, SingleOpTy,
RegMask, CostKind, 0, nullptr);
} else {
// Just a copy of previous destination register.
Cost += TTI::TCC_Free;
}
return;
}
if (SrcReg != DestReg &&
any_of(RegMask, [](int I) { return I != PoisonMaskElem; })) {
// Just a copy of the source register.
Cost += TTI::TCC_Free;
}
PrevSrcReg = SrcReg;
PrevRegMask = RegMask;
ExtractedRegs.set(DestReg);
},
[&](ArrayRef<int> RegMask, unsigned Idx1, unsigned Idx2) {
if (ExtractedRegs.test(Idx1)) {
Cost += getShuffleCost(TTI::SK_ExtractSubvector, Tp, {}, CostKind,
(Idx1 % NumOfSrcRegs) *
SingleOpTy->getNumElements(),
SingleOpTy);
ExtractedRegs.set(Idx1);
}
if (ExtractedRegs.test(Idx2)) {
Cost += getShuffleCost(TTI::SK_ExtractSubvector, Tp, {}, CostKind,
(Idx2 % NumOfSrcRegs) *
SingleOpTy->getNumElements(),
SingleOpTy);
ExtractedRegs.set(Idx2);
}
Cost += getShuffleCost(TTI::SK_PermuteTwoSrc, SingleOpTy, RegMask,
CostKind, 0, nullptr);
});
return Cost;
}
switch (Kind) {
default:
break;
Expand Down
Loading
Loading