This is an archive of the discontinued LLVM Phabricator instance.

[ConstantMerge] Don't merge thread_local constants with non-thread_local constants
ClosedPublic

Authored by Amanieu on Apr 12 2021, 10:11 AM.

Diff Detail

Event Timeline

Amanieu created this revision.Apr 12 2021, 10:11 AM
Amanieu requested review of this revision.Apr 12 2021, 10:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2021, 10:11 AM

Don't we just need to ensure that we preserve thread_local attr?

It's a bit more tricky than that:

  • I'm not sure how we should handle the case where we are trying to merge 2 thread_local constants with different TLS models.
  • In general we are better off *not* merging global constants with TLS constants since calculating a TLS address can be expensive.
Amanieu updated this revision to Diff 336902.Apr 12 2021, 10:50 AM

Merge thread_locals only if they use the same TLS mode

Amanieu retitled this revision from [ConstantMerge] Don't merge thread_local constants to [ConstantMerge] Don't merge thread_local constants with non-thread_local constants.Apr 19 2021, 8:10 AM

Ping.

davide edited reviewers, added: fhahn; removed: davide.Apr 27 2021, 11:10 PM

Reading the bug description, it seems that if the TLS constant as an unnamed address we should be able to merge it into a non-TLS constant?

I'm not even sure what it would *mean* to have an unnamed TLS constant: I would expect an earlier pass to convert it to a normal constant rather than having to it in ConstantMerge.

Also, AFAIK neither clang nor rustc will ever generate unnamed TLS constants.

Can we just completely forbid merging thread_local constants (in isUnmergeableGlobal)? I don't really want to think about the implications of merging.

I'm not even sure what it would *mean* to have an unnamed TLS constant: I would expect an earlier pass to convert it to a normal constant rather than having to it in ConstantMerge.

Also, AFAIK neither clang nor rustc will ever generate unnamed TLS constants.

Makes sense, thanks!
Seems like Eli's suggestion would be best then?

I'm not even sure what it would *mean* to have an unnamed TLS constant: I would expect an earlier pass to convert it to a normal constant rather than having to it in ConstantMerge.

Also, AFAIK neither clang nor rustc will ever generate unnamed TLS constants.

No declared objects in C are ever unnamed_addr, but it's completely reasonable to infer it from use patterns, so it's not like this is an invalid combination. But I agree that it's pointless to merge thread-local constants in constant-merging, because yeah, in any situation where it's permitted we should always be promoting the variable to a global instead (and then maybe that enables constant merging on the promoted TLS).

Amanieu updated this revision to Diff 341307.Apr 28 2021, 1:43 PM

Don't merge thread-local constants

This revision is now accepted and ready to land.Apr 28 2021, 2:30 PM
This revision was landed with ongoing or failed builds.Apr 28 2021, 3:44 PM
This revision was automatically updated to reflect the committed changes.