This is an archive of the discontinued LLVM Phabricator instance.

[X86] `detectAVGPattern()`: support basic case of PAVG chaining (PR52131)
ClosedPublic

Authored by lebedev.ri on Oct 11 2021, 11:55 AM.

Details

Summary

As noted in https://github.com/halide/Halide/pull/6302,
we hilariously fail to match PAVG if we even as much
as look at it the wrong way.

In this particular case, the problem stems from the fact that
PAVG root (def) is a trunc, and leafs (uses) are zext's,
and InstCombine really loves to get rid of both of these,
for example replace them with a bit mask. So we may not have
said zext.

Instead of checking for that + type match,
i think we should rely on the actual active type,
as per the knownbits.

Diff Detail

Event Timeline

lebedev.ri created this revision.Oct 11 2021, 11:55 AM
lebedev.ri requested review of this revision.Oct 11 2021, 11:55 AM
craig.topper added inline comments.
llvm/lib/Target/X86/X86ISelLowering.cpp
46855

Will this introduce a potentially costly truncate if the input is just an AND with the right mask and not another pavg pattern? Can you add a test for that?

srj added a subscriber: srj.Oct 11 2021, 12:13 PM

Address review note.

RKSimon added inline comments.Oct 11 2021, 1:19 PM
llvm/lib/Target/X86/X86ISelLowering.cpp
46758

Is it worth adding a KnownBits get/countMaxActiveBits wrapper?

craig.topper added inline comments.Oct 11 2021, 1:29 PM
llvm/lib/Target/X86/X86ISelLowering.cpp
46758

Looking at other uses of countMinLeadingZeros() in X86ISelLowering we often write it more like this

return Known.countMinLeadingZeros() >= (Known.getBitWidth() - ScalarVT.getSizeInBits());
lebedev.ri marked 2 inline comments as done.

Use KnownBits::countMaxActiveBits().

RKSimon added inline comments.Oct 12 2021, 1:59 AM
llvm/lib/Target/X86/X86ISelLowering.cpp
46855

Also, it'd be better if we didn't create trunc nodes until we've confimed both ops are valid.

lebedev.ri marked an inline comment as done.

Don't create TRUNC unless we will commit to the transform.

llvm/lib/Target/X86/X86ISelLowering.cpp
46792–46795

I will precommit this

RKSimon accepted this revision.Oct 12 2021, 9:41 AM

LGTM

This revision is now accepted and ready to land.Oct 12 2021, 9:41 AM

LGTM

Thank you for the review!
I don't believe this handles everything, so follow-ups are likely.