This is an archive of the discontinued LLVM Phabricator instance.

[GlobalIsel][X86] fix legalization of G_CTLZ and G_CTPOP
ClosedPublic

Authored by tschuett on May 17 2023, 8:38 AM.

Details

Summary

Note that the builders are protected by is64Bit().

More fine-grained availibility checks.

Diff Detail

Unit TestsFailed

Event Timeline

tschuett created this revision.May 17 2023, 8:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 17 2023, 8:38 AM
tschuett requested review of this revision.May 17 2023, 8:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 17 2023, 8:38 AM
RKSimon added inline comments.May 17 2023, 8:49 AM
llvm/lib/Target/X86/X86LegalizerInfo.cpp
305

We need to split this - with just s64 legality here and s16/s32 in setLegalizerInfo32bit above

And just widen sub 32 cases to s32

That will need larger refactorings. I have already run into this issue. You cannot have two getActionDefinitionsBuilder with the same operation. If I add popcount to setLegalizerInfo32bit, then the builder will be called twice with the same operation: once for 32 and once for 64 bit.

tschuett added a comment.EditedMay 17 2023, 8:58 AM
if (Subtarget.hasAVX512()) {
} else if (Subtarget.is64Bit()) {
} else if  (Subtarget.is32Bit()) {
}

That will need larger refactorings. I have already run into this issue. You cannot have two getActionDefinitionsBuilder with the same operation. If I add popcount to setLegalizerInfo32bit, then the builder will be called twice with the same operation: once for 32 and once for 64 bit.

It might be separate setLegalizerInfo32bit / setLegalizerInfo64bit isn't actually useful

That is not what currently is happening.

setLegalizerInfo32bit();
setLegalizerInfo64bit();
setLegalizerInfoSSE1();
setLegalizerInfoSSE2();
setLegalizerInfoSSE41();
setLegalizerInfoSSE42();
setLegalizerInfoAVX();
setLegalizerInfoAVX2();
setLegalizerInfoAVX512();
setLegalizerInfoAVX512DQ();
setLegalizerInfoAVX512BW();

Therefore, I propose the if statements over the features.

Btw, I am only interested in correctness and low hanging fruit. Coverage is a different issue.

if (Subtarget.hasAVX512() && Subtarget.is64Bit()) {
// DQ, BW and VLX
} else if (Subtarget.hasAVX2() && Subtarget.is64Bit()) {
} else if (Subtarget.hasAVX() && Subtarget.is64Bit()) {
} else if (Subtarget.hasSSE41() && Subtarget.is64Bit()) {
} else if (Subtarget.hasSSE2() && Subtarget.is64Bit()) {
} else if (Subtarget.hasSSE1() && Subtarget.is64Bit()) {
} else if (Subtarget.is64Bit()) {
} else if (Subtarget.is32Bit()) {
}

Then you don't run in the existing issues of trying to legalize an operation with different features resp. bit width.

RKSimon retitled this revision from [GlobalIsel][X86] fix legalization of G_CTLZ and GTCTPOP to [GlobalIsel][X86] fix legalization of G_CTLZ and G_CTPOP.May 17 2023, 1:27 PM
RKSimon added inline comments.May 17 2023, 1:38 PM
llvm/lib/Target/X86/X86LegalizerInfo.cpp
305

You should be able to just use legalIf() to support this properly

tschuett added inline comments.May 17 2023, 10:03 PM
llvm/lib/Target/X86/X86LegalizerInfo.cpp
305

I will try this in a followup diff. This diff is only about correctness.

tschuett added a comment.EditedMay 17 2023, 11:21 PM

I looked at the AArch64 legalizer. The use if-statements when they have different features.

RKSimon added inline comments.May 18 2023, 2:41 AM
llvm/lib/Target/X86/X86LegalizerInfo.cpp
305

OK - in which case, lets just use the same pattern as was done for {G_SHL, G_LSHR, G_ASHR} etc. - setLegalizerInfo32bit adds a second copy behind a !Subtarget.is64Bit() check

RKSimon accepted this revision.May 25 2023, 2:53 AM

LGTM as the SSE42 -> POPCNT/LZCNT fix - assuming you're going to do the legalIf() followup

This revision is now accepted and ready to land.May 25 2023, 2:53 AM
This revision was landed with ongoing or failed builds.May 25 2023, 7:41 AM
This revision was automatically updated to reflect the committed changes.