Skip to content

Commit 12bcea3

Browse files
authored
[RISCV][TTI] Recognize CONCAT_VECTORS if a shufflevector mask is multiple insert subvector. (#111459)
reference: #110457
1 parent c742a5d commit 12bcea3

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

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

346+
static bool isRepeatedConcatMask(ArrayRef<int> Mask, int &SubVectorSize) {
347+
unsigned Size = Mask.size();
348+
if (!isPowerOf2_32(Size))
349+
return false;
350+
for (unsigned I = 0; I != Size; ++I) {
351+
if (static_cast<unsigned>(Mask[I]) == I)
352+
continue;
353+
if (Mask[I] != 0)
354+
return false;
355+
if (Size % I != 0)
356+
return false;
357+
for (unsigned J = I + 1; J != Size; ++J)
358+
// Check the pattern is repeated.
359+
if (static_cast<unsigned>(Mask[J]) != J % I)
360+
return false;
361+
SubVectorSize = I;
362+
return true;
363+
}
364+
// That means Mask is <0, 1, 2, 3>. This is not a concatenation.
365+
return false;
366+
}
367+
346368
static VectorType *getVRGatherIndexType(MVT DataVT, const RISCVSubtarget &ST,
347369
LLVMContext &C) {
348370
assert((DataVT.getScalarSizeInBits() != 8 ||
@@ -394,6 +416,29 @@ InstructionCost RISCVTTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
394416
LT.second, CostKind);
395417
}
396418
}
419+
int SubVectorSize;
420+
if (LT.second.getScalarSizeInBits() != 1 &&
421+
isRepeatedConcatMask(Mask, SubVectorSize)) {
422+
InstructionCost Cost = 0;
423+
unsigned NumSlides = Log2_32(Mask.size() / SubVectorSize);
424+
// The cost of extraction from a subvector is 0 if the index is 0.
425+
for (unsigned I = 0; I != NumSlides; ++I) {
426+
unsigned InsertIndex = SubVectorSize * (1 << I);
427+
FixedVectorType *SubTp =
428+
FixedVectorType::get(Tp->getElementType(), InsertIndex);
429+
FixedVectorType *DestTp =
430+
FixedVectorType::getDoubleElementsVectorType(SubTp);
431+
std::pair<InstructionCost, MVT> DestLT =
432+
getTypeLegalizationCost(DestTp);
433+
// Add the cost of whole vector register move because the
434+
// destination vector register group for vslideup cannot overlap the
435+
// source.
436+
Cost += DestLT.first * TLI->getLMULCost(DestLT.second);
437+
Cost += getShuffleCost(TTI::SK_InsertSubvector, DestTp, {},
438+
CostKind, InsertIndex, SubTp);
439+
}
440+
return Cost;
441+
}
397442
}
398443
// vrgather + cost of generating the mask constant.
399444
// We model this for an unknown mask with a single vrgather.
Lines changed: 18 additions & 0 deletions
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

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: '0'
11+
; YAML-NEXT: - Cost: '-2'
1212
; YAML-NEXT: - String: ' and with tree size '
1313
; YAML-NEXT: - TreeSize: '9'
1414

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

Lines changed: 2 additions & 2 deletions
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)