Skip to content

Commit 310d453

Browse files
committed
[RISCV][TTI] Cost a subvector insert at a register boundary with exact vlen
If we have exact vlen knowledge, we can figure out which indices correspond to register boundaries. Our lowering will use this knowledge to replace the vslideup.vi with a sub-register insert when the subvec passthru is undef. One case where the subvec passthru is known undef is when the subvec completely fills the subregister, and that's the easiest case to recognize during costing. Note: This is cost modeling a lowering which hasn't landed yet, see llvm#84107. This change will not land until after that one does. This is another piece split off llvm#80164
1 parent 35db929 commit 310d453

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,22 @@ InstructionCost RISCVTTIImpl::getShuffleCost(TTI::ShuffleKind Kind,
469469
return LT.first *
470470
getRISCVInstructionCost(RISCV::VSLIDEDOWN_VI, LT.second, CostKind);
471471
case TTI::SK_InsertSubvector:
472+
// If we're inserting a subvector of *exactly* m1 size at a sub-register
473+
// boundary this is a subregister insert at worst and won't require the
474+
// slideup. We require the subvec to to be exactly VLEN as otherwise
475+
// we'd have to account for tail elements in the m1 container if any.
476+
// TODO: Extend for aligned m2, m4 inserts
477+
// TODO: Extend for scalable subvector types
478+
if (std::pair<InstructionCost, MVT> SubLT = getTypeLegalizationCost(SubTp);
479+
SubLT.second.isValid() && SubLT.second.isFixedLengthVector()) {
480+
const unsigned MinVLen = ST->getRealMinVLen();
481+
const unsigned MaxVLen = ST->getRealMaxVLen();
482+
if (MinVLen == MaxVLen &&
483+
SubLT.second.getScalarSizeInBits() * Index % MinVLen == 0 &&
484+
SubLT.second.getSizeInBits() == MinVLen)
485+
return TTI::TCC_Free;
486+
}
487+
472488
// Example sequence:
473489
// vsetivli zero, 4, e8, mf2, tu, ma (ignored)
474490
// vslideup.vi v8, v9, 2

llvm/test/Analysis/CostModel/RISCV/shuffle-insert_subvector.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,15 +527,15 @@ define void @fixed_m1_in_m2_notail(<8 x i32> %src, <8 x i32> %passthru) vscale_r
527527
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %2 = shufflevector <8 x i32> %src, <8 x i32> %passthru, <8 x i32> <i32 0, i32 8, i32 9, i32 10, i32 11, i32 5, i32 6, i32 7>
528528
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %3 = shufflevector <8 x i32> %src, <8 x i32> %passthru, <8 x i32> <i32 0, i32 1, i32 8, i32 9, i32 10, i32 11, i32 6, i32 7>
529529
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %4 = shufflevector <8 x i32> %src, <8 x i32> %passthru, <8 x i32> <i32 0, i32 1, i32 2, i32 8, i32 9, i32 10, i32 11, i32 7>
530-
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %5 = shufflevector <8 x i32> %src, <8 x i32> %passthru, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
530+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %5 = shufflevector <8 x i32> %src, <8 x i32> %passthru, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
531531
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void
532532
;
533533
; SIZE-LABEL: 'fixed_m1_in_m2_notail'
534534
; SIZE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %1 = shufflevector <8 x i32> %src, <8 x i32> %passthru, <8 x i32> <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7>
535535
; SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %2 = shufflevector <8 x i32> %src, <8 x i32> %passthru, <8 x i32> <i32 0, i32 8, i32 9, i32 10, i32 11, i32 5, i32 6, i32 7>
536536
; SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %3 = shufflevector <8 x i32> %src, <8 x i32> %passthru, <8 x i32> <i32 0, i32 1, i32 8, i32 9, i32 10, i32 11, i32 6, i32 7>
537537
; SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %4 = shufflevector <8 x i32> %src, <8 x i32> %passthru, <8 x i32> <i32 0, i32 1, i32 2, i32 8, i32 9, i32 10, i32 11, i32 7>
538-
; SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %5 = shufflevector <8 x i32> %src, <8 x i32> %passthru, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
538+
; SIZE-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %5 = shufflevector <8 x i32> %src, <8 x i32> %passthru, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11>
539539
; SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void
540540
;
541541
shufflevector <8 x i32> %src, <8 x i32> %passthru, <8 x i32> <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7>

0 commit comments

Comments
 (0)