This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Optimize BUILD_VECTOR sequences that reveal hidden splats
ClosedPublic

Authored by frasercrmck on Mar 23 2021, 9:20 AM.

Details

Summary

This patch adds further optimization techniques to RVV BUILD_VECTOR
lowering. It teaches the compiler to find splats of larger vector
element types "hidden" in smaller ones. For example, a v4i8 build_vector
(0x1, 0x2, 0x1, 0x2) could be splat as v2i16 0x0201. This is generally
more optimal than the dominant-element BUILD_VECTORs and so takes
priority.

This optimization is currently limited to all-constant-or-undef
BUILD_VECTORs as those were found to be the most common. There's no
reason this couldn't be extended to other BUILD_VECTORs, but the
additional bit-manipulation instructions may require more sophisticated
heuristics.

There are some cases where the materialization of the larger constant
takes more scalar instructions than it does to build the vector with
vector instructions. We could add heuristics to try and catch this.

Diff Detail

Event Timeline

frasercrmck created this revision.Mar 23 2021, 9:20 AM
frasercrmck requested review of this revision.Mar 23 2021, 9:20 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 23 2021, 9:20 AM
craig.topper added inline comments.Mar 23 2021, 2:19 PM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
1274

Do we need to sign extend SplatValue if the ViaIntVT is 32 and XLen is 64 bit to guarantee optimal constant materialization?

  • rv64: sext from 32 to 64
  • add test to cover this case
frasercrmck marked an inline comment as done.Mar 24 2021, 3:22 AM
frasercrmck added inline comments.
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
1274

Ah yes of course. Thanks! Done, with a test to show its benefit.

This revision is now accepted and ready to land.Mar 24 2021, 5:13 PM
This revision was automatically updated to reflect the committed changes.
frasercrmck marked an inline comment as done.