This is an archive of the discontinued LLVM Phabricator instance.

[GlobalOpt] Use generic type when converting malloc to global
ClosedPublic

Authored by nikic on Jan 12 2022, 1:18 AM.

Details

Summary

The malloc to global transform currently determines the type of the global by looking at bitcasts of the malloc. This is limited (the transform fails if there are multiple different types) and incompatible with opaque pointers.

My initial approach was to construct an appropriate struct type based on usage in loads/stores (and I can go back to that if desired). What this patch does instead is to always create an [i8 x AllocSize] global, without trying to guess types at all.

This does mean that other transforms that require a certain global type may break. I fixed one such case in D117034, which is the only one that came up in tests. From a philosophical perspective, I think that fixing transforms to not depend on global type is generally preferable.

Diff Detail

Event Timeline

nikic created this revision.Jan 12 2022, 1:18 AM
nikic requested review of this revision.Jan 12 2022, 1:18 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 12 2022, 1:18 AM
reames added a subscriber: reames.Jan 13 2022, 2:32 PM

Ok, we collided. Clearly we both thought this was reasonable. I think I like my version (D117249) slightly better due to the use of getObjectSize. Can you land your test and approve that one?

nikic updated this revision to Diff 399930.Jan 14 2022, 1:40 AM

Use getObjectSize().

nikic added a comment.Jan 14 2022, 1:50 AM

I've marked this as dependent on D117223, see the globalsra-generic-type.ll test there for why. Without that change, we would not be able to perform global SRA on the produced global. There are other transforms that might fail, but I think having global SRA should go a long way towards avoiding regressions, as it will end up splitting the global into natural types, and all other optimizations should apply at that point.

@reames I can't precommit the test because it currently crashes. I've folded the getObjectSize() change into here.

nikic updated this revision to Diff 399971.Jan 14 2022, 6:45 AM

As the SRA patch is now a dependency, update tests accordingly (malloc-promote-opaque-ptr.ll is affected -- the non-overlapping cases get decomposed into individual globals).

reames accepted this revision.Jan 14 2022, 8:10 AM

LGTM with minor comments.

llvm/lib/Transforms/IPO/GlobalOpt.cpp
1078

I think we also want the size > 0 guard. I don't believe that case is reachable for any of our current allocators, but if we generalizing..

1175

Please add back the first part of this comment.

This revision is now accepted and ready to land.Jan 14 2022, 8:10 AM
nikic added inline comments.Jan 14 2022, 8:17 AM
llvm/lib/Transforms/IPO/GlobalOpt.cpp
1175

Probably got lost in the diff, but this comment has been moved to the top of the function, as an overall description of the transform.

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