Skip to content

Commit 0ee8f69

Browse files
authored
[VectorCombine] Fix invalid shuffle cost argument of foldShuffleOfSelects (#130281)
In the previous code (#128032), it specified the destination vector as the getShuffleCost argument. Because the shuffle mask specifies the indices of the two vectors specified as elements, the maximum value is twice the size of the source vector. This causes a problem if the destination vector is smaller than the source vector and specify an index in the mask that exceeds the size of the destination vector. Fix the problem by correcting the previous code, which was using wrong argument in the Cost calculation. Fixes #130250
1 parent 3ed4daf commit 0ee8f69

File tree

4 files changed

+359
-168
lines changed

4 files changed

+359
-168
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,6 @@ bool VectorCombine::foldShuffleOfSelects(Instruction &I) {
20372037
m_Mask(Mask))))
20382038
return false;
20392039

2040-
auto *DstVecTy = dyn_cast<FixedVectorType>(I.getType());
20412040
auto *C1VecTy = dyn_cast<FixedVectorType>(C1->getType());
20422041
auto *C2VecTy = dyn_cast<FixedVectorType>(C2->getType());
20432042
if (!C1VecTy || !C2VecTy || C1VecTy != C2VecTy)
@@ -2051,24 +2050,26 @@ bool VectorCombine::foldShuffleOfSelects(Instruction &I) {
20512050
(SI0FOp->getFastMathFlags() != SI1FOp->getFastMathFlags())))
20522051
return false;
20532052

2053+
auto *SrcVecTy = dyn_cast<FixedVectorType>(T1->getType());
2054+
auto *DstVecTy = dyn_cast<FixedVectorType>(I.getType());
20542055
auto SK = TargetTransformInfo::SK_PermuteTwoSrc;
20552056
auto SelOp = Instruction::Select;
20562057
InstructionCost OldCost = TTI.getCmpSelInstrCost(
2057-
SelOp, T1->getType(), C1VecTy, CmpInst::BAD_ICMP_PREDICATE, CostKind);
2058-
OldCost += TTI.getCmpSelInstrCost(SelOp, T2->getType(), C2VecTy,
2058+
SelOp, SrcVecTy, C1VecTy, CmpInst::BAD_ICMP_PREDICATE, CostKind);
2059+
OldCost += TTI.getCmpSelInstrCost(SelOp, SrcVecTy, C2VecTy,
20592060
CmpInst::BAD_ICMP_PREDICATE, CostKind);
2060-
OldCost += TTI.getShuffleCost(SK, DstVecTy, Mask, CostKind, 0, nullptr,
2061+
OldCost += TTI.getShuffleCost(SK, SrcVecTy, Mask, CostKind, 0, nullptr,
20612062
{I.getOperand(0), I.getOperand(1)}, &I);
20622063

2063-
auto *C1C2VecTy = cast<FixedVectorType>(
2064-
toVectorTy(Type::getInt1Ty(I.getContext()), DstVecTy->getNumElements()));
20652064
InstructionCost NewCost =
2066-
TTI.getShuffleCost(SK, C1C2VecTy, Mask, CostKind, 0, nullptr, {C1, C2});
2065+
TTI.getShuffleCost(SK, C1VecTy, Mask, CostKind, 0, nullptr, {C1, C2});
20672066
NewCost +=
2068-
TTI.getShuffleCost(SK, DstVecTy, Mask, CostKind, 0, nullptr, {T1, T2});
2067+
TTI.getShuffleCost(SK, SrcVecTy, Mask, CostKind, 0, nullptr, {T1, T2});
20692068
NewCost +=
2070-
TTI.getShuffleCost(SK, DstVecTy, Mask, CostKind, 0, nullptr, {F1, F2});
2071-
NewCost += TTI.getCmpSelInstrCost(SelOp, DstVecTy, DstVecTy,
2069+
TTI.getShuffleCost(SK, SrcVecTy, Mask, CostKind, 0, nullptr, {F1, F2});
2070+
auto *C1C2ShuffledVecTy = cast<FixedVectorType>(
2071+
toVectorTy(Type::getInt1Ty(I.getContext()), DstVecTy->getNumElements()));
2072+
NewCost += TTI.getCmpSelInstrCost(SelOp, DstVecTy, C1C2ShuffledVecTy,
20722073
CmpInst::BAD_ICMP_PREDICATE, CostKind);
20732074

20742075
LLVM_DEBUG(dbgs() << "Found a shuffle feeding two selects: " << I

0 commit comments

Comments
 (0)