This is an archive of the discontinued LLVM Phabricator instance.

[LTT] Handle merged llvm.assume when dropping type tests
ClosedPublic

Authored by tejohnson on May 24 2021, 10:06 PM.

Details

Summary

When the lower type test pass is invoked a second time with
DropTypeTests set to true, it expects that all remaining type tests feed
assume instructions, which are removed along with the type tests.

In some cases the llvm.assume might have been merged with another one,
i.e. from a builtin_assume instruction, in which case the type test
would actually feed a phi that in turn feeds the merged assume
instruction. In this case we can simply replace that operand of the phi
with "true" before removing the type test.

Diff Detail

Event Timeline

tejohnson created this revision.May 24 2021, 10:06 PM
tejohnson requested review of this revision.May 24 2021, 10:06 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 24 2021, 10:06 PM
wmi accepted this revision.May 25 2021, 3:35 PM

LGTM.

This revision is now accepted and ready to land.May 25 2021, 3:35 PM
MaskRay added inline comments.Feb 28 2022, 9:36 PM
llvm/test/Transforms/LowerTypeTests/drop_type_test_phi.ll
38

I am a bit puzzled by the comment "If this type test is only used by llvm.assume instructions": this test has exactly one assume. Where did the merge happen?

tejohnson added inline comments.Mar 3 2022, 10:17 AM
llvm/test/Transforms/LowerTypeTests/drop_type_test_phi.ll
38

The comment you are referring to is from LowerTypeTestsModule::lower() the first time we invoke LTT, which happens before the optimization passes in the LTO backends (right after importing and WPD). There is a second invocation of LTT which simply drops the remaining type test sequences later on, and that is what is being fixed by this patch. It is after a number of standard optimization passes. It has been awhile since I fixed this but so I don't recall exactly which pass was combining the llvm.assume instructions, but it could have been GVN or InstCombine? There are no special restrictions on being able to combine llvm.assume in general.

However, you raise a good point, because the code you referred to will I believe cause the type tests to be removed earlier than desired on the first pass if it already feeds a phi (e.g. if the assume merging happened during the pre-LTO link optimization passes), so that should be loosened up a bit. IIRC the merging of assumes in my case happened after ThinLTO function importing, so didn't hit that case. Let me look at adding a patch to that code to handle the phi case. BTW this is not a correctness issue, it just means they wouldn't be around for subsequent use for optimization.

Herald added a project: Restricted Project. · View Herald TranscriptMar 3 2022, 10:17 AM