public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] i386: Add "Ws" constraint for symbolic address/label reference [PR105576]
@ 2024-01-11 18:24 Fangrui Song
  2024-01-17  1:38 ` Fangrui Song
  2024-01-17  7:46 ` Uros Bizjak
  0 siblings, 2 replies; 4+ messages in thread
From: Fangrui Song @ 2024-01-11 18:24 UTC (permalink / raw)
  To: Jan Hubicka, Uros Bizjak, gcc-patches; +Cc: Fangrui Song

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

diff --git a/gcc/config/i386/constraints.md b/gcc/config/i386/constraints.md
index 0c6e662df25..280e4c8e36c 100644
--- a/gcc/config/i386/constraints.md
+++ b/gcc/config/i386/constraints.md
@@ -348,6 +348,10 @@ (define_constraint "Wf"
    to double word size."
   (match_operand 0 "x86_64_dwzext_immediate_operand"))
 
+(define_constraint "Ws"
+  "A symbolic reference or label reference."
+  (match_code "const,symbol_ref,label_ref"))
+
 (define_constraint "Z"
   "32-bit unsigned integer constant, or a symbolic reference known
    to fit that range (for immediate operands in zero-extending x86-64
diff --git a/gcc/doc/md.texi b/gcc/doc/md.texi
index 47a87d6ceec..b0c61925120 100644
--- a/gcc/doc/md.texi
+++ b/gcc/doc/md.texi
@@ -4275,6 +4275,10 @@ require non-@code{VOIDmode} immediate operands).
 128-bit integer constant where both the high and low 64-bit word
 satisfy the @code{e} constraint.
 
+@item Ws
+A symbolic reference or label reference.
+You can use the @code{%p} modifier to print the raw symbol.
+
 @item Z
 32-bit unsigned integer constant, or a symbolic reference known
 to fit that range (for immediate operands in zero-extending x86-64
diff --git a/gcc/testsuite/gcc.target/i386/asm-raw-symbol.c b/gcc/testsuite/gcc.target/i386/asm-raw-symbol.c
new file mode 100644
index 00000000000..b7854567dd9
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/asm-raw-symbol.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+
+extern int var;
+
+void
+func (void)
+{
+  __asm__ ("@ %p0" : : "Ws" (func));
+  __asm__ ("@ %p0" : : "Ws" (&var + 1));
+}
+
+/* { dg-final { scan-assembler "@ func" } } */
+/* { dg-final { scan-assembler "@ var\\+4" } } */
-- 
2.43.0.275.g3460e3d667-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] i386: Add "Ws" constraint for symbolic address/label reference [PR105576]
  2024-01-11 18:24 [PATCH] i386: Add "Ws" constraint for symbolic address/label reference [PR105576] Fangrui Song
@ 2024-01-17  1:38 ` Fangrui Song
  2024-01-17  7:46 ` Uros Bizjak
  1 sibling, 0 replies; 4+ messages in thread
From: Fangrui Song @ 2024-01-17  1:38 UTC (permalink / raw)
  To: Jan Hubicka, Uros Bizjak, gcc-patches

On Thu, Jan 11, 2024 at 10:24 AM Fangrui Song <maskray@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`.



-- 
宋方睿

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] i386: Add "Ws" constraint for symbolic address/label reference [PR105576]
  2024-01-11 18:24 [PATCH] i386: Add "Ws" constraint for symbolic address/label reference [PR105576] Fangrui Song
  2024-01-17  1:38 ` Fangrui Song
@ 2024-01-17  7:46 ` Uros Bizjak
  2024-01-30 23:22   ` H.J. Lu
  1 sibling, 1 reply; 4+ messages in thread
From: Uros Bizjak @ 2024-01-17  7:46 UTC (permalink / raw)
  To: Fangrui Song; +Cc: Jan Hubicka, gcc-patches

On Thu, Jan 11, 2024 at 7:24 PM Fangrui Song <maskray@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.

OK.

Thanks,
Uros.

>
> ---
>
> 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
>
> diff --git a/gcc/config/i386/constraints.md b/gcc/config/i386/constraints.md
> index 0c6e662df25..280e4c8e36c 100644
> --- a/gcc/config/i386/constraints.md
> +++ b/gcc/config/i386/constraints.md
> @@ -348,6 +348,10 @@ (define_constraint "Wf"
>     to double word size."
>    (match_operand 0 "x86_64_dwzext_immediate_operand"))
>
> +(define_constraint "Ws"
> +  "A symbolic reference or label reference."
> +  (match_code "const,symbol_ref,label_ref"))
> +
>  (define_constraint "Z"
>    "32-bit unsigned integer constant, or a symbolic reference known
>     to fit that range (for immediate operands in zero-extending x86-64
> diff --git a/gcc/doc/md.texi b/gcc/doc/md.texi
> index 47a87d6ceec..b0c61925120 100644
> --- a/gcc/doc/md.texi
> +++ b/gcc/doc/md.texi
> @@ -4275,6 +4275,10 @@ require non-@code{VOIDmode} immediate operands).
>  128-bit integer constant where both the high and low 64-bit word
>  satisfy the @code{e} constraint.
>
> +@item Ws
> +A symbolic reference or label reference.
> +You can use the @code{%p} modifier to print the raw symbol.
> +
>  @item Z
>  32-bit unsigned integer constant, or a symbolic reference known
>  to fit that range (for immediate operands in zero-extending x86-64
> diff --git a/gcc/testsuite/gcc.target/i386/asm-raw-symbol.c b/gcc/testsuite/gcc.target/i386/asm-raw-symbol.c
> new file mode 100644
> index 00000000000..b7854567dd9
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/i386/asm-raw-symbol.c
> @@ -0,0 +1,13 @@
> +/* { dg-do compile } */
> +
> +extern int var;
> +
> +void
> +func (void)
> +{
> +  __asm__ ("@ %p0" : : "Ws" (func));
> +  __asm__ ("@ %p0" : : "Ws" (&var + 1));
> +}
> +
> +/* { dg-final { scan-assembler "@ func" } } */
> +/* { dg-final { scan-assembler "@ var\\+4" } } */
> --
> 2.43.0.275.g3460e3d667-goog
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] i386: Add "Ws" constraint for symbolic address/label reference [PR105576]
  2024-01-17  7:46 ` Uros Bizjak
@ 2024-01-30 23:22   ` H.J. Lu
  0 siblings, 0 replies; 4+ messages in thread
From: H.J. Lu @ 2024-01-30 23:22 UTC (permalink / raw)
  To: Uros Bizjak; +Cc: Fangrui Song, Jan Hubicka, gcc-patches

On Tue, Jan 16, 2024 at 11:47 PM Uros Bizjak <ubizjak@gmail.com> wrote:
>
> On Thu, Jan 11, 2024 at 7:24 PM Fangrui Song <maskray@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.
>
> OK.

Hi Fangrui,

I pushed it for you with indentation changes in the commit log.

Thanks.

> Thanks,
> Uros.
>
> >
> > ---
> >
> > 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
> >
> > diff --git a/gcc/config/i386/constraints.md b/gcc/config/i386/constraints.md
> > index 0c6e662df25..280e4c8e36c 100644
> > --- a/gcc/config/i386/constraints.md
> > +++ b/gcc/config/i386/constraints.md
> > @@ -348,6 +348,10 @@ (define_constraint "Wf"
> >     to double word size."
> >    (match_operand 0 "x86_64_dwzext_immediate_operand"))
> >
> > +(define_constraint "Ws"
> > +  "A symbolic reference or label reference."
> > +  (match_code "const,symbol_ref,label_ref"))
> > +
> >  (define_constraint "Z"
> >    "32-bit unsigned integer constant, or a symbolic reference known
> >     to fit that range (for immediate operands in zero-extending x86-64
> > diff --git a/gcc/doc/md.texi b/gcc/doc/md.texi
> > index 47a87d6ceec..b0c61925120 100644
> > --- a/gcc/doc/md.texi
> > +++ b/gcc/doc/md.texi
> > @@ -4275,6 +4275,10 @@ require non-@code{VOIDmode} immediate operands).
> >  128-bit integer constant where both the high and low 64-bit word
> >  satisfy the @code{e} constraint.
> >
> > +@item Ws
> > +A symbolic reference or label reference.
> > +You can use the @code{%p} modifier to print the raw symbol.
> > +
> >  @item Z
> >  32-bit unsigned integer constant, or a symbolic reference known
> >  to fit that range (for immediate operands in zero-extending x86-64
> > diff --git a/gcc/testsuite/gcc.target/i386/asm-raw-symbol.c b/gcc/testsuite/gcc.target/i386/asm-raw-symbol.c
> > new file mode 100644
> > index 00000000000..b7854567dd9
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.target/i386/asm-raw-symbol.c
> > @@ -0,0 +1,13 @@
> > +/* { dg-do compile } */
> > +
> > +extern int var;
> > +
> > +void
> > +func (void)
> > +{
> > +  __asm__ ("@ %p0" : : "Ws" (func));
> > +  __asm__ ("@ %p0" : : "Ws" (&var + 1));
> > +}
> > +
> > +/* { dg-final { scan-assembler "@ func" } } */
> > +/* { dg-final { scan-assembler "@ var\\+4" } } */
> > --
> > 2.43.0.275.g3460e3d667-goog
> >



-- 
H.J.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-01-30 23:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-11 18:24 [PATCH] i386: Add "Ws" constraint for symbolic address/label reference [PR105576] Fangrui Song
2024-01-17  1:38 ` Fangrui Song
2024-01-17  7:46 ` Uros Bizjak
2024-01-30 23:22   ` H.J. Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).