[PATCH] D97446: Change some addUsedGlobal to addUsedOrCompilerUsedGlobal

Fangrui Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 13 13:43:14 PST 2022


MaskRay added a comment.

In D97446#3241617 <https://reviews.llvm.org/D97446#3241617>, @JonChesterfield wrote:

>> Modulo optimizer bugs, __attribute__((used)) hasn't changed semantics.
>> If your downstream project does not handle llvm.compiler.used, maybe handle it now :)
>
> There seems to be some confusion here. The 'downstream project' is openmp, which has worked around this regression in D117211 <https://reviews.llvm.org/D117211> and D117231 <https://reviews.llvm.org/D117231>.
>
> Before this patch, `__attribute__((used))` mapped onto llvm.used, and the variables so annotated made it all the way to the compiled artefact. After this patch, it is mapped onto llvm.compiler.used, gets hit by an internalisation pass and ends up in the compiled output but missing from the symbol table. That is itself presumably a bug, as the linker should have completely discarded it, with much the same effect.

For your comment it appears an issue in the internalisation pass. It is orthogonal to this patch.
Do you have a reduced example with reproduce instructions for the issue? I know very little about OpenMP.

> With this patch applied, what's the remaining use case for `__attribute__((used))`? It can no longer be used to keep something in the final executable, so it seems s/used/retain/g is the recommendation for programs that previously used that attribute.
>
> It's possible that the check in internalize that skips over llvm.used and not llvm.compiler.used is itself a bug, and the intent is for llvm.used to be identical to llvm.compiler.used, but if that's the case we should delete the llvm.compiler.used array.

A `__attribute__((used))` object can be referenced by a relocation. The relocation (from A to B) establishes a dependency relation in the linker: if the section defining A is retained, the section defining B should be retained as well.
This can be used by inline assembly (the compiler may not know the symbol references in assembly), and some code doing manual relocations.

llvm.compiler.used (the underlying mechanism of `__attribute__((used))`) is useful in instrumentations.
There are quite few cases that the compiler does not fully discard definitions and has to defer it to the linker.
I have changed some instrumentations (PGO/SanitizerCoverage/other sanitizers) to downgrade llvm.used to llvm.compiler.used to improve section based linker garbage collection for all of PE-COFF, Mach-O, and ELF.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D97446



More information about the cfe-commits mailing list