This is an archive of the discontinued LLVM Phabricator instance.

[SLP] For vectorizing chains in basic block, decide order of PHI nodes based on their result use.
AbandonedPublic

Authored by skc7 on Oct 22 2022, 9:19 PM.

Details

Summary

SLPVectorize pass uses the order of vectorizable phi nodes in the basic block to form a tree entry and decides the mask of the resulting shuffle vector.
If tree entry has insertelement instruction sequence, they will be also converted to shufflevector with mask based on insertelement instruction order in BB.

In the BB, if the result of vectorizable phi nodes is used by insertelement instructions, the resulting masks of shuffle vectors after vectorizing these two tree entries can mismatch. This happens if the order of phi nodes and the order of use of results of phi nodes mismatch.

There will be a scope for optimizing these resulting shuffle vectors in the subsequent passes in the pipeline, if their shuffle masks match.

This change decides the order of phinodes in the tree entry, based on the order of use of the results of phi nodes in the BB. This will make sure the resulting mask of shuffle vectors are same.

Diff Detail

Event Timeline

skc7 created this revision.Oct 22 2022, 9:19 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 22 2022, 9:19 PM
skc7 requested review of this revision.Oct 22 2022, 9:19 PM
ABataev added inline comments.Oct 23 2022, 4:58 AM
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
12439

Remove extra parens

12440

Can this be a non-instruction user? Rather doubt, do it should be a cast

12446

Just return FirstUserOfPhi1->comesBefore(FirstUserOfPhi2);

llvm/test/Transforms/SLPVectorizer/AMDGPU/phi-result-use-order.ll
1

Precommit this test in a separate patch.

skc7 updated this revision to Diff 469988.EditedOct 23 2022, 9:09 AM

Changes made as per review comments.
Precommit for test on phinodes order: D136553

ABataev added inline comments.Oct 23 2022, 9:53 AM
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
12442

FirstUserOfPhi1 && FirstUserOfPhi2 is always true, remove it from the condition.

skc7 updated this revision to Diff 469996.Oct 23 2022, 11:30 AM

Changes as per review comments. Fix the test from D136553.

ABataev added inline comments.Oct 23 2022, 11:43 AM
llvm/test/Transforms/SLPVectorizer/AMDGPU/phi-result-use-order.ll
76

Do I understand it correct that you're trying to avoid reversing here and above?

skc7 added inline comments.Oct 23 2022, 9:12 PM
llvm/test/Transforms/SLPVectorizer/AMDGPU/phi-result-use-order.ll
76

Yes, the order here is dependent on the order of phinodes currently.

ABataev added inline comments.Oct 24 2022, 4:16 AM
llvm/test/Transforms/SLPVectorizer/AMDGPU/phi-result-use-order.ll
76

Then you need to do a bit different thing than you're doing. You need to extend the analysis in BoUpSLP::getReorderingData to check that the buildvector sequence is built in the identity order.

skc7 added inline comments.Oct 25 2022, 10:13 AM
llvm/test/Transforms/SLPVectorizer/AMDGPU/phi-result-use-order.ll
76

Sorting of phis happen using PHICompare before building the tree and entries. Do we need to extend getReorderingData() for phis? And again do the analysis on the initially sorted phis from the tree entry.

ABataev added inline comments.Oct 25 2022, 10:23 AM
llvm/test/Transforms/SLPVectorizer/AMDGPU/phi-result-use-order.ll
76

It is not sorting, I would call it rather selection of the most profitable ones.
The actual reordering of the built graph happens in BoUpSLP::reorderTopToBottom and BoUpSLP::reorderBottomToTop. Yes, you need to extend getReorderingData() to support phis (check the incoming values/users for best order if they are not checked yet.). But you need to check the extractelement/insertelement indices rather than the order of the users.

skc7 added inline comments.Oct 26 2022, 5:01 AM
llvm/test/Transforms/SLPVectorizer/AMDGPU/phi-result-use-order.ll
76

Thanks @ABataev for review. I have created D136757 to extend getReorderingData() for phis.

skc7 abandoned this revision.Nov 23 2022, 9:10 PM