This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Add cost model for insertelement/extractelement of vector type that should be splitted.
Needs ReviewPublic

Authored by jacquesguan on Sep 13 2022, 8:47 PM.

Details

Summary

For the vector that should be splitted, legalizer would use stack store/load to make it. This patch adds cost model by counting stack store/load and scalar store/load cost.

Diff Detail

Event Timeline

jacquesguan created this revision.Sep 13 2022, 8:47 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 13 2022, 8:47 PM
jacquesguan requested review of this revision.Sep 13 2022, 8:47 PM

This patch adds the unapproved part of https://reviews.llvm.org/D133007.

Does the proposed costing match the current lowering? I haven't looked.

I would expect the lowering to be able to do the following:

  • For a constant index w/ fixed vectors, emit a single extract/insert into a particular split register value.
  • For a constant index w/ scalable vectors, emit a single instruction when index < minimum VLEN implied VLMAX.
  • Use a sequence of masked slidedowns to extract an arbitrary index from an arbitrary set of vector registers. Or possibly a vcompress instead.

The spill/load lowering is legal, but rather sub-optimal.

update condition.

Does the proposed costing match the current lowering? I haven't looked.

I would expect the lowering to be able to do the following:

  • For a constant index w/ fixed vectors, emit a single extract/insert into a particular split register value.
  • For a constant index w/ scalable vectors, emit a single instruction when index < minimum VLEN implied VLMAX.
  • Use a sequence of masked slidedowns to extract an arbitrary index from an arbitrary set of vector registers. Or possibly a vcompress instead.

The spill/load lowering is legal, but rather sub-optimal.

I made some mistake before. Now the legalizer will do the following action:

For constant index:

  1. All fixed length vector would lower to a single extract/insert
  2. For scalable vector, if we could make sure that the index is in the first splitted vector, lower to a single extract/insert; otherwise use stack spill and load/store.

For non constant index:
All would use stack spill and load/store.

reames requested changes to this revision.Sep 28 2022, 11:37 AM

Can you add some test coverage specifically for the insert/extract case as opposed to relying on updates in lowering of more complicated constructs?

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
1456

I believe that for a scalable vector, this effectively returns the M in <vscale x M x iN>. While correct, this is smaller than the maximum index we can prove in the first vector.

(e.g. for +v, M is 1 for i64, but provably valid indexes are 0 and 1)

Here's an alternative:

unsigned EltSize = VT.getScalarSizeInBits();
unsigned MinSize = VT.getSizeInBits().getKnownMinValue();
unsigned VectorBitsMin = Subtarget.getRealMinVLen();
unsigned MinVLMAX =
    RISCVTargetLowering::computeVLMAX(VectorBitsMin, EltSize, MinSize);
This revision now requires changes to proceed.Sep 28 2022, 11:37 AM

rebase test.

Can you add some test coverage specifically for the insert/extract case as opposed to relying on updates in lowering of more complicated constructs?

Done, I added in https://reviews.llvm.org/D135534.

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
1456

Yes, I agree with you, but this cost should obey DAGTypeLegalizer's action which just check with the value of M.

craig.topper added inline comments.Oct 24 2022, 11:42 PM
llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
1452

konwn -> known

1453

could -> can

1476

ReLoad -> Reload

Address comment.

llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
1452

Done.

1453

Done.

1476

Done.

evandro removed a subscriber: evandro.Jun 8 2023, 10:35 AM

This patch no longer applies cleanly

rebase main.