This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Lower the shufflevector equivalent of vector.splice
ClosedPublic

Authored by craig.topper on Feb 4 2022, 2:38 PM.

Details

Summary

We can lower a vector splice to a vslidedown and a vslideup.

The majority of the matching code here came from X86's code for matching
PALIGNR and VPALIGND/Q.

The slidedown and slideup lowering don't really require it to be concatenation,
but it happened to be an interesting pattern with existing analysis code I
could use.

This helps with cases where the scalar loop optimizer forwarded a load
result from a previous loop iteration. For example, this happens if the
loop uses x[i] and x[i+1] on the same iteration. The scalar optimizer
will forward x[i+1] load from the previous loop to satisfy x[i] on this
loop. When this get vectorized it results in one element of a vector
being forwarded from the previous loop to be concatenated with elements
loaded on this iteration.

Whether that's more efficient than doing a shifted loaded or reloading
the single scalar and using vslide1up is an interesting question.
But that's not something the backend can help with.

Diff Detail

Event Timeline

craig.topper created this revision.Feb 4 2022, 2:38 PM
craig.topper requested review of this revision.Feb 4 2022, 2:38 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 4 2022, 2:38 PM
khchen added inline comments.Feb 9 2022, 9:56 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
2549

return 0?

craig.topper added inline comments.Feb 9 2022, 10:44 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
2549

Good catch! X86 used -1 and returned an int, but it would never return 0. Which I initially copied before changing to 0.

I'll add a negative test for this.

craig.topper retitled this revision from [RISCV] Recognize shuffles that rotate one vector or the concatenation of two vectors. to [RISCV] Lower the shufflevector equivalent of vector.splice.Feb 9 2022, 10:52 AM
craig.topper edited the summary of this revision. (Show Details)

Switch back to X86's implementation of returning -1.

Returning 0 could be misinterpreted as an identify rotation. So use to be more clear.

The previously identified bug where I returned -1 was hidden because the result
was casted to int in the caller so it failed the > 0 check in the caller.

khchen accepted this revision.Feb 9 2022, 11:29 PM
khchen added inline comments.
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
2575

I'm wondering is there any cases which have only one value for valves?
I don't get it because X86 did the assignment if one of values not found but we return -1 here.

This revision is now accepted and ready to land.Feb 9 2022, 11:29 PM
craig.topper added inline comments.Feb 9 2022, 11:40 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
2575

It would happen for

<1, 2, 3, 4, 5, 6, 7, -1> or <-1, 0, 1, 2, 3, 4, 5, 6>

Those cases should be handled as a single slidedown or slideup. We already have matchShuffleAsSlideDown for the slidedown case, we don't have matchShuffleAsSlideUp yet.

X86 has a single instruction with two vector operands. I'm not sure why they wanted to use the same input twice instead of making the unused one undef.

khchen added inline comments.Feb 10 2022, 1:31 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
2575

-1 is invalid shufflevector operands showed in my local machine,
I tried to give undef (ex. <1, 2, 3, undef> and changed this piece of code same with x86,
The result seems like correct.

Why I did that because I was thinking is it possible to have an unify function for both x86 and risc-v because they are almost same except here and assert checking.

anyway, it could be another RFC patch.

This revision was landed with ongoing or failed builds.Feb 10 2022, 9:39 AM
This revision was automatically updated to reflect the committed changes.