[PATCH] D96838: Add GNU attribute 'retain'

John McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 21 19:32:31 PST 2021


rjmccall added a comment.

In D96838#2578083 <https://reviews.llvm.org/D96838#2578083>, @MaskRay wrote:

> In D96838#2578073 <https://reviews.llvm.org/D96838#2578073>, @rjmccall wrote:
>
>> GCC 11 hasn't been released yet, so can we still engage with GCC about the semantics of this attribute?  This is a very low-level attribute, and I don't understand why it was made separate instead of just having `used` add the appropriate section flag on targets that support it.
>
> GCC did overload `used` with the linker garbage collection semantics. Then after discussions (e.g. https://gcc.gnu.org/pipermail/gcc-patches/2021-February/565478.html) they decided to pick the earliest suggestion: add `retain`.
>
> This makes sense to me: `used` can emit a function which is only used by inline assembly, but the user intention can still be that the whole thing can be GCable.

There's definitely some merit to distinguishing the cases here — `used` doesn't just force the object to be emitted, it also forces the compiler to treat it as if there might be accesses to the variable / function that it can't see.  Thus, for example, a non-const `used` variable has to be assumed to be potentially mutated in arbitrary ways.  I guess my concern is that this new attribute still doesn't feel like it covers the use cases very well, especially because `used` does have long-established semantics of affecting the linker on non-ELF targets.  Basically, to get "full strength" semantics that affect the compiler and linker on all targets, you have to add a new attribute; to get "partial strength" semantics that only affect the compiler, you can use the old attribute on ELF, but there's no way to spell it on non-ELF; and if you wanted to get "weak" semantics (e.g. to force the symbol to be preserved through linking without broadly disabling the optimizer), there's no way to spell that anywhere.  Seems like if we're going to the trouble of adding an attribute, that's not the situation we want to end up in.

> IIUC macOS's always enabled `.subsections_via_symbols` means always -ffunction-sections/-fdata-sections, so there is no GC preciseness concern.

Yes, the MachO linker will aggressively dead-strip, and `used` prevents that.

> However, the current Windows behavior (`.drectve` include a linker option to force retaining the whole section) can unnecessarily retain the full section, in -fno-function-sections or -fno-data-sections mode.

But that Windows behavior is a limitation of the format rather than an intent to provide different semantics on different targets, I'd say.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D96838/new/

https://reviews.llvm.org/D96838



More information about the cfe-commits mailing list