On Thu, Jan 11, 2024 at 10:24 AM Fangrui Song <mask...@google.com> wrote:
>
> Printing the raw symbol is useful in inline asm (e.g. in C++ to get the
> mangled name).  Similar constraints are available in other targets (e.g.
> "S" for aarch64/riscv, "Cs" for m68k).
>
> There isn't a good way for x86 yet, e.g. "i" doesn't work for
> PIC/-mcmodel=large.  This patch adds "Ws".  Here are possible use cases:
>
> ```
> namespace ns { extern int var; }
> asm (".pushsection .xxx,\"aw\"; .dc.a %0; .popsection" :: "Ws"(&var));
> asm (".reloc ., BFD_RELOC_NONE, %0" :: "Ws"(&var));
> ```
>
> gcc/ChangeLog:
>
>     PR target/105576
>     * config/i386/constraints.md: Define constraint "Ws".
>     * doc/md.texi: Document it.
>
> gcc/testsuite/ChangeLog:
>
>     * gcc.target/i386/asm-raw-symbol.c: New testcase.
>
> ---
>
> This obsoletes 
> https://gcc.gnu.org/pipermail/gcc-patches/2024-January/642580.html
> I initially tried 'z', but Uros requested that a W prefix is used.
> ---
>  gcc/config/i386/constraints.md                 |  4 ++++
>  gcc/doc/md.texi                                |  4 ++++
>  gcc/testsuite/gcc.target/i386/asm-raw-symbol.c | 13 +++++++++++++
>  3 files changed, 21 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.target/i386/asm-raw-symbol.c

Thanks to Uros for "W" prefix suggestion.
If "Ws" sounds a good choice, I'll go ahead with my Clang patch adding
"Ws", even if the GCC patch is still pending:)

https://github.com/protocolbuffers/protobuf/blob/1fe463ce71b6acc60b3aef65d51185e3704cac8b/src/google/protobuf/stubs/common.h

// Strongly references the given variable such that the linker will be forced
// to pull in this variable's translation unit.
template <typename T>
void StrongReference(const T& var) {
  auto volatile unused = &var;
  (void)&unused;  // Use address to avoid an extra load of "unused".
}

is an example that "Ws" constraint (which expands to no instruction)
works better than the volatile address-taken operation.
I am not so familiar with the mechanism, but StrongReference is used
to establish a dependency edge so that the ld --gc-sections will not
garbage `var`.



-- 
宋方睿

Reply via email to