Skip to content

Commit 2a50dac

Browse files
committed
[RISCV][TTI]Fix the cost estimation for long select shuffle.
The code was broken completely. Need to iterate over the whole mask and process the submasks correctly, check if they form full indentity and adjust indices correctly. Fixes #106126
1 parent 296ffc1 commit 2a50dac

File tree

2 files changed

+456
-9
lines changed

2 files changed

+456
-9
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -447,21 +447,30 @@ InstructionCost RISCVTTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
447447
auto *SubVecTy = FixedVectorType::get(Tp->getElementType(), SubVF);
448448

449449
InstructionCost Cost = 0;
450-
for (unsigned I = 0; I < NumRegs; ++I) {
450+
for (unsigned I = 0, NumSrcRegs = divideCeil(Mask.size(), SubVF);
451+
I < NumSrcRegs; ++I) {
451452
bool IsSingleVector = true;
452453
SmallVector<int> SubMask(SubVF, PoisonMaskElem);
453-
transform(Mask.slice(I * SubVF,
454-
I == NumRegs - 1 ? Mask.size() % SubVF : SubVF),
455-
SubMask.begin(), [&](int I) {
456-
bool SingleSubVector = I / VF == 0;
457-
IsSingleVector &= SingleSubVector;
458-
return (SingleSubVector ? 0 : 1) * SubVF + I % VF;
459-
});
454+
transform(
455+
Mask.slice(I * SubVF,
456+
I == NumSrcRegs - 1 ? Mask.size() % SubVF : SubVF),
457+
SubMask.begin(), [&](int I) -> int {
458+
if (I == PoisonMaskElem)
459+
return PoisonMaskElem;
460+
bool SingleSubVector = I / VF == 0;
461+
IsSingleVector &= SingleSubVector;
462+
return (SingleSubVector ? 0 : 1) * SubVF + (I % VF) % SubVF;
463+
});
464+
if (all_of(enumerate(SubMask), [](auto &&P) {
465+
return P.value() == PoisonMaskElem ||
466+
static_cast<unsigned>(P.value()) == P.index();
467+
}))
468+
continue;
460469
Cost += getShuffleCost(IsSingleVector ? TTI::SK_PermuteSingleSrc
461470
: TTI::SK_PermuteTwoSrc,
462471
SubVecTy, SubMask, CostKind, 0, nullptr);
463-
return Cost;
464472
}
473+
return Cost;
465474
}
466475
break;
467476
}

0 commit comments

Comments
 (0)