This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner][SelectionDAG] Apply CombineStoreAndExtract on constant values

Authored by tingwang on Mar 22 2023, 12:13 AM.


Group Reviewers
Restricted Project

Given there is a constant vector and constant scalar both are stored under the same root node, if the scalar happens to bit wise match some specific vector element, then backend may reuse vector element for the scalar store.

It seems inevitable that I need to search through store values. That kind of search is already conducted in getStoreMergeCandidates(), so I think it maybe a good place to push some addition search logic.

I added shouldReuseConstVectorForExtractStore() to bail out early if backend does not need this kind of combine.

One hack I feel like have to do is to get EXTRACT_VECTOR_ELT node from DAG under some condition. Without that I'm afraid cannot form the pattern for instruction selection. Let me know if there is any other way to achieve this in DAG. Thank you!

Diff Detail

Event Timeline

tingwang created this revision.Mar 22 2023, 12:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 22 2023, 12:13 AM
tingwang requested review of this revision.Mar 22 2023, 12:13 AM

Some test case to sanity check constants are stored as expected:

I built them separately and linked with -O3, and verified results identical on LE/BE before and after this patch.

tingwang planned changes to this revision.Mar 22 2023, 1:27 AM

Saw error in bootstrap.

tingwang updated this revision to Diff 507982.Mar 24 2023, 12:45 AM

(1) Do not combine when store node generates additional value(s).
(2) Add getCombineStoreAndExtractIdx() to directly query the index, and do not need to loop through all elements.
(3) If the constant value to be stored is referenced by node other than current Store and the vector node, do not combine.
(4) Do not combine if the constant value is -1U.
(5) Add check to make sure the to-be-extracted vector type is legal.

Bootstrap is clean with this version.

tingwang abandoned this revision.Apr 5 2023, 5:25 PM

Not useful, since dependent is abandoned.