This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] try to fold a pair of insertelements into one insertelement
ClosedPublic

Authored by spatel on Dec 8 2022, 12:44 PM.

Details

Summary

This replaces patches that tried to convert related patterns to shuffles (D138872, D138873, D138874 - reverted/abandoned) but caused codegen problems and were questionable as a canonicalization because an insertelement is a simpler op than a shuffle.

This detects a larger pattern -- insert-of-insert -- and replaces with another insert, so this hopefully does not cause any problems.

As noted by TODO items in the code and tests, this could go a lot further. But this is enough to reduce the motivating test from issue #17113.

Example proofs:
https://alive2.llvm.org/ce/z/NnUv3a

I drafted a version of this for AggressiveInstCombine, but it seems that would uncover yet another phase ordering gap. If we do generalize this to handle the full range of potential patterns, that may be worth looking at again.

Diff Detail

Event Timeline

spatel created this revision.Dec 8 2022, 12:44 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 8 2022, 12:44 PM
spatel requested review of this revision.Dec 8 2022, 12:44 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 8 2022, 12:44 PM

LGTM - @dmgreen does this look better to you?

dmgreen accepted this revision.Dec 11 2022, 11:51 PM

LGTM. Thanks for coming up with and working on the alternative.

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
1528

I believe in DAG we canonicalize to increasing insert indices, without extra uses. Not sure if that would be useful here too.

This revision is now accepted and ready to land.Dec 11 2022, 11:51 PM
spatel added inline comments.Dec 12 2022, 6:30 AM
llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
1528

Thanks! That sounds like a good transform for IR too. I'll look at that as a follow-up.

This revision was landed with ongoing or failed builds.Dec 12 2022, 7:55 AM
This revision was automatically updated to reflect the committed changes.