Skip to content

Commit 22cb3db

Browse files
committed
[RISCV][TTI] Recognize CONCAT_VECTORS if a shufflevector mask is a
multiple insert subvector.
1 parent ab1faf5 commit 22cb3db

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,40 @@ RISCVTTIImpl::getConstantPoolLoadCost(Type *Ty, TTI::TargetCostKind CostKind) {
343343
/*AddressSpace=*/0, CostKind);
344344
}
345345

346+
InstructionCost
347+
RISCVTTIImpl::isMultipleInsertSubvector(VectorType *Tp, ArrayRef<int> Mask,
348+
TTI::TargetCostKind CostKind) {
349+
if (!isa<FixedVectorType>(Tp))
350+
return InstructionCost::getInvalid();
351+
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(Tp);
352+
if (LT.second.getScalarSizeInBits() == 1)
353+
return InstructionCost::getInvalid();
354+
// Try to guess SubTp.
355+
for (unsigned SubVecSize = 1, E = Mask.size(); SubVecSize < E;
356+
SubVecSize <<= 1) {
357+
if (E % SubVecSize != 0)
358+
continue;
359+
SmallVector<int> RepeatedPattern(createSequentialMask(0, SubVecSize, 0));
360+
bool Skip = false;
361+
for (unsigned I = 0; I != E; I += SubVecSize)
362+
if (!Mask.slice(I, SubVecSize).equals(RepeatedPattern)) {
363+
Skip = true;
364+
break;
365+
}
366+
if (Skip)
367+
continue;
368+
InstructionCost Cost = 0;
369+
FixedVectorType *SubTp = FixedVectorType::get(
370+
cast<FixedVectorType>(Tp)->getElementType(), SubVecSize);
371+
// The cost of extraction from a subvector is 0 if the index is 0.
372+
for (unsigned I = 0; I != E; I += SubVecSize)
373+
Cost +=
374+
getShuffleCost(TTI::SK_InsertSubvector, Tp, {}, CostKind, I, SubTp);
375+
return Cost;
376+
}
377+
return InstructionCost::getInvalid();
378+
}
379+
346380
static VectorType *getVRGatherIndexType(MVT DataVT, const RISCVSubtarget &ST,
347381
LLVMContext &C) {
348382
assert((DataVT.getScalarSizeInBits() != 8 ||
@@ -394,6 +428,10 @@ InstructionCost RISCVTTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
394428
LT.second, CostKind);
395429
}
396430
}
431+
if (InstructionCost Cost =
432+
isMultipleInsertSubvector(Tp, Mask, CostKind);
433+
Cost.isValid())
434+
return Cost;
397435
}
398436
// vrgather + cost of generating the mask constant.
399437
// We model this for an unknown mask with a single vrgather.

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ class RISCVTTIImpl : public BasicTTIImplBase<RISCVTTIImpl> {
5555
/// type.
5656
InstructionCost getConstantPoolLoadCost(Type *Ty,
5757
TTI::TargetCostKind CostKind);
58+
59+
/// Return the cost if a shufflevector can be consist of multiple vslideup.
60+
/// Otherwise, return InstructionCost::getInvalid().
61+
InstructionCost isMultipleInsertSubvector(VectorType *Tp, ArrayRef<int> Mask,
62+
TTI::TargetCostKind CostKind);
63+
5864
public:
5965
explicit RISCVTTIImpl(const RISCVTargetMachine *TM, const Function &F)
6066
: BaseT(TM, F.getDataLayout()), ST(TM->getSubtargetImpl(F)),

llvm/test/Analysis/CostModel/RISCV/fixed-vector-insert-subvector.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
define void @test() {
55
; CHECK-LABEL: 'test'
6-
; CHECK-NEXT: Cost Model: Found an estimated cost of 22 for instruction: %0 = shufflevector <8 x float> poison, <8 x float> poison, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
6+
; CHECK-NEXT: Cost Model: Found an estimated cost of 8 for instruction: %0 = shufflevector <8 x float> poison, <8 x float> poison, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
77
; CHECK-NEXT: Cost Model: Found an estimated cost of 8 for instruction: %1 = shufflevector <4 x i16> poison, <4 x i16> poison, <16 x i32> <i32 0, i32 1, i32 2, i32 3, i32 0, i32 1, i32 2, i32 3, i32 0, i32 1, i32 2, i32 3, i32 0, i32 1, i32 2, i32 3>
8-
; CHECK-NEXT: Cost Model: Found an estimated cost of 8 for instruction: %2 = shufflevector <4 x float> poison, <4 x float> poison, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 0, i32 1, i32 2, i32 3>
8+
; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %2 = shufflevector <4 x float> poison, <4 x float> poison, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 0, i32 1, i32 2, i32 3>
99
; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %3 = shufflevector <2 x i1> poison, <2 x i1> poison, <4 x i32> <i32 0, i32 1, i32 0, i32 1>
1010
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
1111
;

llvm/test/Transforms/SLPVectorizer/RISCV/remarks-insert-into-small-vector.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
; YAML-NEXT: Function: test
99
; YAML-NEXT: Args:
1010
; YAML-NEXT: - String: 'Stores SLP vectorized with cost '
11-
; YAML-NEXT: - Cost: '2'
11+
; YAML-NEXT: - Cost: '0'
1212
; YAML-NEXT: - String: ' and with tree size '
1313
; YAML-NEXT: - TreeSize: '7'
1414

llvm/test/Transforms/SLPVectorizer/RISCV/revec-getGatherCost.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
; YAML: Function: test1
99
; YAML: Args:
1010
; YAML: - String: 'Stores SLP vectorized with cost '
11-
; YAML: - Cost: '6'
11+
; YAML: - Cost: '4'
1212
; YAML: - String: ' and with tree size '
1313
; YAML: - TreeSize: '5'
1414

0 commit comments

Comments
 (0)