Skip to content

Commit 554eaec

Browse files
authored
[RISCV][TTI] Recognize CONCAT_VECTORS if a shufflevector mask is multiple insert subvector. (#110457)
1 parent 574266c commit 554eaec

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,49 @@ 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+
unsigned NumSlides = Log2_32(E / SubVecSize);
370+
// The cost of extraction from a subvector is 0 if the index is 0.
371+
for (unsigned I = 0; I != NumSlides; ++I) {
372+
unsigned InsertIndex = SubVecSize * (1 << I);
373+
FixedVectorType *SubTp = FixedVectorType::get(
374+
cast<FixedVectorType>(Tp)->getElementType(), InsertIndex);
375+
FixedVectorType *DesTp =
376+
FixedVectorType::getDoubleElementsVectorType(SubTp);
377+
std::pair<InstructionCost, MVT> DesLT = getTypeLegalizationCost(DesTp);
378+
// Add the cost of whole vector register move because the destination
379+
// vector register group for vslideup cannot overlap the source.
380+
Cost += DesLT.first * TLI->getLMULCost(DesLT.second);
381+
Cost += getShuffleCost(TTI::SK_InsertSubvector, DesTp, {}, CostKind,
382+
InsertIndex, SubTp);
383+
}
384+
return Cost;
385+
}
386+
return InstructionCost::getInvalid();
387+
}
388+
346389
static VectorType *getVRGatherIndexType(MVT DataVT, const RISCVSubtarget &ST,
347390
LLVMContext &C) {
348391
assert((DataVT.getScalarSizeInBits() != 8 ||
@@ -394,6 +437,10 @@ InstructionCost RISCVTTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
394437
LT.second, CostKind);
395438
}
396439
}
440+
if (InstructionCost Cost =
441+
isMultipleInsertSubvector(Tp, Mask, CostKind);
442+
Cost.isValid())
443+
return Cost;
397444
}
398445
// vrgather + cost of generating the mask constant.
399446
// We model this for an unknown mask with a single vrgather.

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.h

+6
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)),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
2+
; RUN: opt < %s -passes="print<cost-model>" 2>&1 -disable-output -S -mtriple=riscv64 -mattr=+v | FileCheck %s
3+
4+
define void @test() {
5+
; CHECK-LABEL: 'test'
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>
7+
; CHECK-NEXT: Cost Model: Found an estimated cost of 6 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 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>
9+
; 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>
10+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
11+
;
12+
entry:
13+
%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>
14+
%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>
15+
%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>
16+
%3 = shufflevector <2 x i1> poison, <2 x i1> poison, <4 x i32> <i32 0, i32 1, i32 0, i32 1>
17+
ret void
18+
}

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

+1-1
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

+2-2
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

@@ -47,7 +47,7 @@ declare <4 x float> @llvm.fmuladd.v4f32(<4 x float>, <4 x float>, <4 x float>)
4747
; YAML: Function: test2
4848
; YAML: Args:
4949
; YAML: - String: 'Stores SLP vectorized with cost '
50-
; YAML: - Cost: '16'
50+
; YAML: - Cost: '12'
5151
; YAML: - String: ' and with tree size '
5252
; YAML: - TreeSize: '5'
5353

0 commit comments

Comments
 (0)