This is an archive of the discontinued LLVM Phabricator instance.

Add !retain metadata to retain global values under linker garbage collection
AbandonedPublic

Authored by MaskRay on Feb 16 2021, 10:34 PM.

Details

Summary

!retain may be attached to a function/global variable definition.
Its section will be marked as a linker GC root.

On ELF platforms, we set the SHF_GNU_RETAIN flag.

We don't restrict the feature to ELFOSABI_GNU and ELFOSABI_FREEBSD for
now. See the rationale in D95749.

The integrated assembler has supported SHF_GNU_RETAIN since D95730.
GNU as>=2.36 supports section flag 'R'.
We don't need to worry about GNU ld support because older GNU ld just ignores
the unknown SHF_GNU_RETAIN.

On COFF platforms, we add /INCLUDE: linker options similar to how we currently
handle llvm.used (99f479abcf2c9b36daad04eb91cd0aafa659bb1d). Retaining local
linkage symbols is not representable due to the limitation.

Diff Detail

Event Timeline

MaskRay created this revision.Feb 16 2021, 10:34 PM
MaskRay requested review of this revision.Feb 16 2021, 10:34 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 16 2021, 10:34 PM

Have you considered using llvm.used instead of introducing new metadata type? __attribute__((used)) is lowered to llvm.used so this should be equivalent.

MaskRay added a comment.EditedFeb 17 2021, 12:49 AM

Have you considered using llvm.used instead of introducing new metadata type? __attribute__((used)) is lowered to llvm.used so this should be equivalent.

I have considered it. It is true that clang places __attribute__((used)) definitions in llvm.uses, but it adds a bunch of other stuff (I am reading the call sites of addUsedGlobal to figure out what I may have missed in D96838).
Second, I am concerned that the separate section semantics (copied from GCC) may not be desired for some llvm.uses use cases.

Please see the updated description of D96838. In GCC, 'used' no longer have the special semantics. 'used' and 'retain are entirely orthogonal for ELF.

phosek accepted this revision.Feb 20 2021, 5:17 PM

LGTM

llvm/docs/LangRef.rst
6562–6563

Would it be possible to expand this sentence a bit? I'm not quite sure what you mean by "loosing linker GC precision".

llvm/test/CodeGen/X86/elf-retain.ll
2

Can you include top-level comment describing the purpose of this test?

This revision is now accepted and ready to land.Feb 20 2021, 5:17 PM
MaskRay updated this revision to Diff 325273.Feb 20 2021, 7:50 PM

improve doc

MaskRay marked 2 inline comments as done.Feb 20 2021, 7:56 PM
MaskRay updated this revision to Diff 325543.Feb 22 2021, 1:06 PM
MaskRay retitled this revision from Add !retain metadata to represent SHF_GNU_RETAIN on ELF platforms to Add !retain metadata to retain global values under linker garbage collection.
MaskRay edited the summary of this revision. (Show Details)

Support COFF

rnk accepted this revision.Feb 24 2021, 1:06 PM
rnk added inline comments.
llvm/docs/LangRef.rst
6572

I think this is fine for now, but we should investigate the other implementation strategy (non-comdat unique sections) in the future to lift this limitation. What you've done here has parity with llvm.used, so we don't need to improve on that right now.

llvm/test/CodeGen/X86/coff-retain.ll
5

Maybe start by looking for the .drectve section to establish context for the CHECKs

MaskRay added a comment.EditedFeb 24 2021, 1:17 PM

@rnk I posted https://lists.llvm.org/pipermail/llvm-dev/2021-February/148760.html . This patch implements approach 2. If we do approach 1 (has support from @compnerd and @dblaikie), we don't need this metadata. What do you think of the tradeoff?

rnk added a comment.Feb 24 2021, 1:21 PM

@rnk I posted https://lists.llvm.org/pipermail/llvm-dev/2021-February/148760.html . This patch implements approach 2. If we do approach 1 (has support from @compnerd and @dblaikie), we don't need this metadata. What do you think of the tradeoff?

Proposal 1 of just keeping llvm.used and changing the semantics of the source level attribute seems fine to me as well.

MaskRay abandoned this revision.Feb 24 2021, 9:15 PM