This is an archive of the discontinued LLVM Phabricator instance.

[X86] isTargetShuffleEquivalent - attempt to match SM_SentinelZero shuffle mask elements using known bits
ClosedPublic

Authored by RKSimon on Jul 6 2022, 7:59 AM.

Details

Summary

If the combined shuffle mask contains required zero elements, we don't currently have much chance of matching them against the expected source vector. This patch uses the SelectionDAG::MaskedVectorIsZero wrapper to attempt to determine if the element we want to use is already known to be zero.

This attempts to address some of the regressions uncovered by D129150 where we more aggressively fold shuffles as AND / 'clear' masks which results in more combined shuffles using SM_SentinelZero.

Diff Detail

Event Timeline

RKSimon created this revision.Jul 6 2022, 7:59 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 6 2022, 7:59 AM
RKSimon requested review of this revision.Jul 6 2022, 7:59 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 6 2022, 7:59 AM
RKSimon updated this revision to Diff 442991.Jul 7 2022, 10:52 AM
RKSimon edited the summary of this revision. (Show Details)

Delayed MaskedVectorIsZero calls to the end of the test if all other shuffle mask elements matched, so we only need to call each input vector once at most.

deadalnix added inline comments.
llvm/lib/Target/X86/X86ISelLowering.cpp
11790

ExpectedIdx >= 0 ? I find Yoda style conditions to be a bit confusing.

11812

Wouldn't that make sense to check for isNullValue in MaskedVectorIsZero?

I'm going to be honest here, I'm not sure I understand this fully, so I don't feel confident to accept.

RKSimon added inline comments.Jul 8 2022, 7:13 AM
llvm/lib/Target/X86/X86ISelLowering.cpp
11790

Sure - no preference tbh (and I'm never very consistent either).

11812

Doesn't work unfortunately (I agree its tidier), value tracking tends to have things like this:

KnownBits Known(BitWidth);   // Don't know anything
...
if (!DemandedElts)
  return Known;  // No demanded elts, better to assume we don't know anything.
RKSimon updated this revision to Diff 443241.Jul 8 2022, 7:17 AM

flip condition

I'm going to be honest here, I'm not sure I understand this fully, so I don't feel confident to accept.

np - thanks for taking a look!

deadalnix added inline comments.Jul 8 2022, 7:31 AM
llvm/lib/Target/X86/X86ISelLowering.cpp
11795

signed/unsigned comparison.

RKSimon updated this revision to Diff 443286.Jul 8 2022, 10:44 AM

fixed signed/unsigned mismatch

ping - any objections?

spatel added inline comments.Jul 11 2022, 6:20 AM
llvm/lib/Target/X86/X86ISelLowering.cpp
11818

What do you think about adding a clause above this and below the first clause like:

// If the mask is more defined than expected, the shuffles are not the same.
if (ExpectedIdx == SM_SentinelUndef)
  return false;

I think that would then allow simplifying the later checks (converting to asserts).

RKSimon added inline comments.Jul 11 2022, 6:39 AM
llvm/lib/Target/X86/X86ISelLowering.cpp
11818

We assert that ExpectedMask is isUndefOrZeroOrInRange above, although tbh I'm not certain how many callers ever have sentinal values in expected mask.

spatel accepted this revision.Jul 11 2022, 6:57 AM

LGTM

llvm/lib/Target/X86/X86ISelLowering.cpp
11818

Ok - maybe we can tighten up the logic with asserts as a follow-up. Also, I misread the constant-on-left compares ("Yoda conditions") on first reading. :)

This revision is now accepted and ready to land.Jul 11 2022, 6:57 AM
RKSimon updated this revision to Diff 443627.Jul 11 2022, 7:04 AM

Tighten up the ExpectedMask assertion to always be in range - we're never giving it a target shuffle mask that has sentinels at all - allowing to remove some of the confusing bounds checks.

This revision was landed with ongoing or failed builds.Jul 11 2022, 7:30 AM
This revision was automatically updated to reflect the committed changes.
chapuni added inline comments.
llvm/lib/Target/X86/X86ISelLowering.cpp
11802

isInRange is unused with -Asserts.

I ran into the same issue today.

cheers - I'll deal with it shortly