This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Use vmerge for INSERT_VECTOR_ELT w/small constant indices
Needs ReviewPublic

Authored by reames on Aug 4 2023, 9:44 AM.

Details

Summary

Currently, we default to using a vmv.s.x and vslide1up sequence for inserting elements into a vector. This lowering has a couple of downsides. First, it requires a temporary register to hold the scalar-as-vector. Second, for inserts into the middle of a vector, the VL chosen needs to be Idx + 1. This causes VL toggles since these odd VLs are unlikely to be sharable.

Instead, we can use a vmerge.vx to perform the insert. This avoids the need for the temporary register and odd VLs, but requires the population of a mask register. For the moment, restrict usage to when we can use a single vmv.v.i to populate the mask - i.e. indices less than 5. If we like the direction of this patch, this restriction can be lifted by using a vmseq(vid, index) sequence, but I'll defer that to later work.

Diff Detail

Event Timeline

reames created this revision.Aug 4 2023, 9:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 4 2023, 9:44 AM
reames requested review of this revision.Aug 4 2023, 9:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 4 2023, 9:44 AM
reames updated this revision to Diff 547800.Aug 7 2023, 8:03 AM

Rebase over inverted dependency.

luke added inline comments.Aug 9 2023, 3:44 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
6939

Could we still use vmv.v.i on scalable vectors in theory? Since Idx isn't scaled by vscale for insert_vector_elt.

reames added inline comments.Aug 22 2023, 7:46 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
6939

Not without having a bound on the size of the scalable vector, we need all the other lanes to be zero. (Hm, maybe we could use a zero splat followed by an insert if the lane was less than XLEN even for a scalable vector.)

However, we can use a vmseq(vid, index) sequence for the scalable case. This is probably better.