This is an archive of the discontinued LLVM Phabricator instance.

[CodeGenPrepare] Unmerging GEPs across indirect branches must respect types
AbandonedPublic

Authored by momo5502 on Aug 29 2023, 7:51 AM.

Details

Summary

The optimization in CodeGenPrepare, where GEPs are unmerged across indirect branches must respect the types of both GEPs and their sizes when adjusting the indices.

The sample here shows the bug:

https://godbolt.org/z/fvYvvGGjE

The value %elementValuePtr addresses the second field of the %struct.Blub. It is therefore a GEP with index 1 and type i8.
The value %nextArrayElement addresses the next array element. It is therefore a GEP with index 1 and type %struct.Blub.

Both values point to completely different addresses, even if the indices are the same, due to the types being different.
However, after CodeGenPrepare has run, %nextArrayElement is a bitcast from %elementValuePtr, meaning both were treated as equal.

The cause for this is that the unmerging optimization does not take types into consideration.
It sees both GEPs have %currentArrayElement as source operand and therefore tries to rewrite %nextArrayElement in terms of %elementValuePtr.
It changes the index to the difference of the two GEPs. As both indices are 1, the difference is 0. As the indices are 0 the GEP is later replaced with a simple bitcast in CodeGenPrepare.

Before adjusting the indices, the types of the GEPs have to be aligned and the indices scaled accordingly for the optimization to be correct.
Due to the size of the struct being 16 and the %elementValuePtr pointing to offset 1, the correct index for the unmerged %nextArrayElement would be 15.

I adjusted the optimization and added a simple test case that asserts the correct index is preserved.
I assume this bug emerged from the opaque pointer change as GEPs like %elementValuePtr that access the struct field based of type i8 did not naturally occur before.

Diff Detail

Event Timeline

momo5502 created this revision.Aug 29 2023, 7:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 29 2023, 7:51 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
momo5502 requested review of this revision.Aug 29 2023, 7:51 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 29 2023, 7:51 AM
momo5502 edited the summary of this revision. (Show Details)Aug 29 2023, 7:52 AM
momo5502 added reviewers: hjyamauchi, hfinkel.
momo5502 edited the summary of this revision. (Show Details)Aug 29 2023, 8:33 AM
momo5502 abandoned this revision.Sep 21 2023, 10:57 PM

Will be continued as a GitHub PR