This is an archive of the discontinued LLVM Phabricator instance.

[SLP] Unify main/alternate selection for CmpInst instructions
ClosedPublic

Authored by vdmitrie on Sep 7 2022, 9:10 AM.

Details

Summary

Make main/alternate operation selection logic for CmpInst consistent across SLP vectorizer.

Diff Detail

Event Timeline

vdmitrie created this revision.Sep 7 2022, 9:10 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 7 2022, 9:10 AM
vdmitrie requested review of this revision.Sep 7 2022, 9:10 AM
ABataev added inline comments.
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
5876

!*IsMain?

vdmitrie added inline comments.Sep 7 2022, 10:56 AM
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
5876

Nope. The intent is check the optional does not have a value.

ABataev added inline comments.Sep 7 2022, 11:36 AM
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
582–586

Why did you removed this check? I believe it might help to build some nodes, though terminating with gathers.

vdmitrie added inline comments.Sep 7 2022, 12:08 PM
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
582–586

IMO, it is redundant ( i.e. removing it is no-op)
For base instruction "IsMain == true" at line 580 means same as "(BasePred == CurrentPred || BasePred == SwappedCurrentPred) == true".
Before setting the alternate op we don't really care about "AltPred == CurrentPred || AltPred == SwappedCurrentPred" as it the same as base.
When we set alternate op (predicate) we don't care either as it is the first instruction with alternate predicate. We do care only for subsequent instructions to fit either base or alternate flow.
When alternate instruction is already set ("AltIndex != BaseIndex" is true) the check will be covered by "IsAlt == true" at line 585, which is equivalent to "(AltPred == CurrentPred || AltPred == SwappedCurrentPred) == true".

Confusion probably comes from returning value of isCmpSameOrSwapped() routine.
It returns optional which does only have a value when condition Pred == CurrentPredicate || Pred == SwappedCurrentPred is met.

ABataev added inline comments.Sep 7 2022, 12:24 PM
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
582–586

It will be covered if ops are compatible, if they are incompatible, isCmpSameOrSwapped returns None and the functions returns InstructionsState(VL[BaseIndex], nullptr, nullptr);, saying that it is a gather sequence. But we can build a vector node even if there are no compatible operands, the operands will form the gather-based nodes instead.

vdmitrie added inline comments.Sep 7 2022, 1:01 PM
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
582–586

I see what you mean. we make a call to areCompatibleCmpOps and then simply ignore it here. Right?
I'd instead tweaked areCompatibleCmpOps routine if we really miss something useful from that.
Returning the condition will bring inconsistency back. We have to be definite from very beginning about which scalars go to "base" and which to "alternate" and it should have deterministic behavior.

vdmitrie added inline comments.Sep 7 2022, 1:56 PM
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
582–586

Thinking of another possible alternative: current behavior should be retained without sacrificing consistency. We can make isCmpSameOrSwapped to return an optional enum instead:

MatchWithOperands - corresponds to current "true"
MatchWithOperandsSwap - corresponds to current "false"
PredicateMatch
SwappedPredicateMatch

in buildTree_rec () we then also check for SwappedPredicateMatch in order to swap LHS/RHS.

ABataev added inline comments.Sep 7 2022, 2:05 PM
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
582–586

You don’t need to make it optimal then, just add another enum value.

vdmitrie added inline comments.Sep 7 2022, 2:09 PM
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
582–586

Agree. But that is a minor detail.

vdmitrie updated this revision to Diff 458622.Sep 7 2022, 6:54 PM

Reworked patch.

ABataev added inline comments.Sep 12 2022, 7:02 AM
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
490–494

Format comment

4621–4624

Move the comment for the function here too, usually the description is associated with the first declaration.

vdmitrie updated this revision to Diff 459517.Sep 12 2022, 10:50 AM
vdmitrie retitled this revision from [SLP] Unify main/alternate instruction logic for CmpInst instructions - draft to [SLP] Unify main/alternate selection for CmpInst instructions.
vdmitrie edited the summary of this revision. (Show Details)
vdmitrie added reviewers: ABataev, RKSimon.

Rebased.
Addressed comments and few minor cleanups applied.

ABataev accepted this revision.Sep 12 2022, 11:07 AM

LG with a nit

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
492

"swapped, false otherwise."

This revision is now accepted and ready to land.Sep 12 2022, 11:07 AM