This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Support OR/XOR/AND reductions on vector masks
ClosedPublic

Authored by frasercrmck on Apr 7 2021, 3:39 AM.

Details

Summary

This patch adds RVV codegen support for OR/XOR/AND reductions for both
scalable- and fixed-length vector types. There are a few possible
codegen strategies for each -- vmfirst.m, vmsbf.m, and vmsif.m could be
used to some extent -- but the vpopc.m instruction was chosen since it
produces the scalar result in one instruction, after which scalar
instructions can finish off the computation.

The reductions are lowered identically for both scalable- and
fixed-length vectors, although some alternate strategies may be more
optimal on fixed-length vectors since it's cheaper to get the length of
those types.

Other reduction types were not deemed to be relevant for mask vectors.

Diff Detail

Event Timeline

frasercrmck created this revision.Apr 7 2021, 3:39 AM
frasercrmck requested review of this revision.Apr 7 2021, 3:39 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 7 2021, 3:39 AM
craig.topper added inline comments.Apr 7 2021, 11:19 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
3032

Should this be ~x rather than !x

3041

This doesn't make sense to me. I was thinking xor should be (vpopc & 1) != 0. Meaning it's 1 when there is an odd number of set bits.

frasercrmck marked an inline comment as done.Apr 7 2021, 11:30 AM
frasercrmck added inline comments.
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
3032

Er yes, of course. Thanks.

3041

Hmm yes I think I've somehow got some wires crossed. Would we assert that vector length is even or does that assumption run deep enough already?

craig.topper added inline comments.Apr 7 2021, 11:37 AM
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
3041

Doesn't (vpopc & 1) != 0 still work for odd VL as long as the vpopc was given the odd VL and only summed VL bits?

frasercrmck marked 3 inline comments as done.Apr 7 2021, 11:44 AM
frasercrmck added inline comments.
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
3041

Oh dear, yes, I think I'd better just stop for the day! Should we generate != 0 in even though the & 1 suffices? It's optimized out but it's arguably more readable.

frasercrmck marked an inline comment as done.
  • fix comment
  • actually generate correct code for xor
This revision is now accepted and ready to land.Apr 7 2021, 12:06 PM
This revision was automatically updated to reflect the committed changes.