This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Lower vector CTLZ_ZERO_UNDEF/CTTZ_ZERO_UNDEF by converting to FP and extracting the exponent.
ClosedPublic

Authored by craig.topper on Oct 15 2021, 11:37 AM.

Details

Summary

If we have a large enough floating point type that can exactly
represent the integer value, we can convert the value to FP and
use the exponent to calculate the leading/trailing zeros.

The exponent will contain log2 of the value plus the exponent bias.
We can then remove the bias and convert from log2 to leading/trailing
zeros.

This doesn't work for zero since the exponent of zero is zero so we
can only do this for CTLZ_ZERO_UNDEF/CTTZ_ZERO_UNDEF. If we need
a value for zero we can use a vmseq and a vmerge to handle it.

We need to be careful to make sure the floating point type is legal.
If it isn't we'll continue using the integer expansion. We could split the vector
and concatenate the results but that needs some additional work and evaluation.

Diff Detail

Event Timeline

craig.topper created this revision.Oct 15 2021, 11:37 AM
craig.topper requested review of this revision.Oct 15 2021, 11:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 15 2021, 11:37 AM
Herald added a subscriber: MaskRay. · View Herald Transcript
craig.topper added inline comments.Oct 15 2021, 11:40 AM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
7067–7068

This is needed to get VSELECT for vectors.

7117–7118

This isn't tested by this patch but makes it consistent with CTLZ.

Fix a bad comment. Use Src SDValue instead of calling getOperand. clang-format.

Add support for CTTZ_ZERO_UNDEF as well.

craig.topper retitled this revision from [RISCV] Lower fixed vector CTLZ_ZERO_UNDEF by converting to FP and extracting the exponent. to [RISCV] Lower fixed vector CTLZ_ZERO_UNDEF/CTTZ_ZERO_UNDEF by converting to FP and extracting the exponent..Oct 15 2021, 1:40 PM
craig.topper edited the summary of this revision. (Show Details)

Rebase after fixing scalarization of ctlz/cttz.

Remove splitting if FP type isn't legal. Some cases it generated worse code. Need to re-evaluate.

I had to make an additional change to TargetLowering.cpp to prevent an infinite loop.

craig.topper edited the summary of this revision. (Show Details)Oct 20 2021, 9:38 AM

Fix bad check prefixes

Sorry for not being very prompt on this one. I'll try and think about it in more detail later.

In the meantime, what is it about this that doesn't/shouldn't apply to scalable vectors?

Support scalable vectors.
Check legal types in RISCVTargetLowering constructor. Fixes unneeded vmerges.

craig.topper retitled this revision from [RISCV] Lower fixed vector CTLZ_ZERO_UNDEF/CTTZ_ZERO_UNDEF by converting to FP and extracting the exponent. to [RISCV] Lower vector CTLZ_ZERO_UNDEF/CTTZ_ZERO_UNDEF by converting to FP and extracting the exponent..Nov 15 2021, 10:49 AM

It makes sense to me. I'll let @frasercrmck chime in.

LGTM too other than nits. Thanks, @craig.topper!

llvm/lib/Target/RISCV/RISCVISelLowering.cpp
621

Could maybe update this comment to use the actual ISD names for both nodes we're dealing with.

849

Same here as above.

2326

This comment seems stale since we're no longer falling back.

This revision was not accepted when it landed; it landed in state Needs Review.Nov 17 2021, 10:31 AM
This revision was automatically updated to reflect the committed changes.
llvm/lib/Target/RISCV/RISCVISelLowering.cpp