[PATCH] D97447: Add GNU attribute 'retain'

Fangrui Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 24 23:40:50 PST 2021


MaskRay created this revision.
MaskRay added reviewers: aaron.ballman, rjmccall, rnk, rsmith.
Herald added a subscriber: pengfei.
MaskRay requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

For ELF targets, GCC 11 will set SHF_GNU_RETAIN on the section of a
`__attribute__((retain))` function/variable to prevent linker garbage
collection. (See AttrDocs.td for the linker support).

This patch adds `retain` functions/variables to the `llvm.used` list, which has
the desired linker GC semantics. Note: `retain` does not imply `used`,
so an unused function/variable can be dropped by Sema.

Before 'retain' was introduced, previous ELF solutions require inline asm or
linker tricks, e.g.  `asm volatile(".reloc 0, R_X86_64_NONE, target");`
(architecture dependent) or define a non-local symbol in the section and use
`ld -u`. There was no elegant source-level solution.

As of this patch, llvm.used and llvm.compiler.used have the same
behaviors for ELF. `__attribute__((retain))` does not set `SHF_GNU_RETAIN` yet.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97447

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/test/CodeGen/attr-retain.c
  clang/test/CodeGenCXX/attr-retain.cpp
  clang/test/Sema/attr-retain.c

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97447.326299.patch
Type: text/x-patch
Size: 10404 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210225/6f489b5a/attachment-0001.bin>


More information about the cfe-commits mailing list