public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] LoongArch: Emit R_LARCH_RELAX for TLS IE with non-extreme code model to allow the IE to LE linker relaxation
@ 2024-02-29  7:11 Xi Ruoyao
  2024-03-07  2:43 ` mengqinggang
  0 siblings, 1 reply; 5+ messages in thread
From: Xi Ruoyao @ 2024-02-29  7:11 UTC (permalink / raw)
  To: gcc-patches; +Cc: chenglulu, i, xuchenghua, mengqinggang, Xi Ruoyao

In Binutils we need to make IE to LE relaxation only allowed when there
is an R_LARCH_RELAX after R_LARCH_TLE_IE_PC_{HI20,LO12} so an invalid
"partial" relaxation won't happen with the extreme code model.  So if we
are emitting %ie_pc_{hi20,lo12} in a non-extreme code model, emit an
R_LARCH_RELAX to allow the relaxation.  The IE to LE relaxation does not
require the pcalau12i and the ld instruction to be adjacent, so we don't
need to limit ourselves to use the macro.

For the distro maintainers backporting changes: this change depends on
r14-8721, without r14-8721 R_LARCH_RELAX can be emitted mistakenly in
the extreme code model.

gcc/ChangeLog:

	* config/loongarch/loongarch.cc (loongarch_print_operand_reloc):
	Support 'Q' for R_LARCH_RELAX for TLS IE.
	(loongarch_output_move): Use 'Q' to print R_LARCH_RELAX for TLS
	IE.
	* config/loongarch/loongarch.md (ld_from_got<mode>): Likewise.

gcc/testsuite/ChangeLog:

	* gcc.target/loongarch/tls-ie-relax.c: New test.
	* gcc.target/loongarch/tls-ie-norelax.c: New test.
	* gcc.target/loongarch/tls-ie-extreme.c: New test.
---

Bootstrapped & regtested on loongarch64-linux-gnu.  Ok for trunk?

 gcc/config/loongarch/loongarch.cc                 | 15 ++++++++++++++-
 gcc/config/loongarch/loongarch.md                 |  2 +-
 .../gcc.target/loongarch/tls-ie-extreme.c         |  5 +++++
 .../gcc.target/loongarch/tls-ie-norelax.c         |  5 +++++
 gcc/testsuite/gcc.target/loongarch/tls-ie-relax.c | 11 +++++++++++
 5 files changed, 36 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/loongarch/tls-ie-extreme.c
 create mode 100644 gcc/testsuite/gcc.target/loongarch/tls-ie-norelax.c
 create mode 100644 gcc/testsuite/gcc.target/loongarch/tls-ie-relax.c

diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
index 0428b6e65d5..70e31bb831c 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -4981,7 +4981,7 @@ loongarch_output_move (rtx dest, rtx src)
 	  if (type == SYMBOL_TLS_LE)
 	    return "lu12i.w\t%0,%h1";
 	  else
-	    return "pcalau12i\t%0,%h1";
+	    return "%Q1pcalau12i\t%0,%h1";
 	}
 
       if (src_code == CONST_INT)
@@ -6145,6 +6145,7 @@ loongarch_print_operand_reloc (FILE *file, rtx op, bool hi64_part,
    'L'  Print the low-part relocation associated with OP.
    'm'	Print one less than CONST_INT OP in decimal.
    'N'	Print the inverse of the integer branch condition for comparison OP.
+   'Q'  Print R_LARCH_RELAX for TLS IE.
    'r'  Print address 12-31bit relocation associated with OP.
    'R'  Print address 32-51bit relocation associated with OP.
    'T'	Print 'f' for (eq:CC ...), 't' for (ne:CC ...),
@@ -6282,6 +6283,18 @@ loongarch_print_operand (FILE *file, rtx op, int letter)
 					    letter);
       break;
 
+    case 'Q':
+      if (!TARGET_LINKER_RELAXATION)
+	break;
+
+      if (code == HIGH)
+	op = XEXP (op, 0);
+
+      if (loongarch_classify_symbolic_expression (op) == SYMBOL_TLS_IE)
+	fprintf (file, ".reloc\t.,R_LARCH_RELAX\n\t");
+
+      break;
+
     case 'r':
       loongarch_print_operand_reloc (file, op, false /* hi64_part */,
 				     true /* lo_reloc */);
diff --git a/gcc/config/loongarch/loongarch.md b/gcc/config/loongarch/loongarch.md
index f3b5c641fce..525e1e82183 100644
--- a/gcc/config/loongarch/loongarch.md
+++ b/gcc/config/loongarch/loongarch.md
@@ -2620,7 +2620,7 @@ (define_insn "@ld_from_got<mode>"
 				(match_operand:P 2 "symbolic_operand")))]
 	UNSPEC_LOAD_FROM_GOT))]
   ""
-  "ld.<d>\t%0,%1,%L2"
+  "%Q2ld.<d>\t%0,%1,%L2"
   [(set_attr "type" "move")]
 )
 
diff --git a/gcc/testsuite/gcc.target/loongarch/tls-ie-extreme.c b/gcc/testsuite/gcc.target/loongarch/tls-ie-extreme.c
new file mode 100644
index 00000000000..00c545a3e8c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/tls-ie-extreme.c
@@ -0,0 +1,5 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=loongarch64 -mabi=lp64d -mcmodel=extreme -mexplicit-relocs=auto -mrelax" } */
+/* { dg-final { scan-assembler-not "R_LARCH_RELAX" { target tls_native } } } */
+
+#include "tls-ie-relax.c"
diff --git a/gcc/testsuite/gcc.target/loongarch/tls-ie-norelax.c b/gcc/testsuite/gcc.target/loongarch/tls-ie-norelax.c
new file mode 100644
index 00000000000..dd6bf3634a4
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/tls-ie-norelax.c
@@ -0,0 +1,5 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mcmodel=normal -mexplicit-relocs -mno-relax" } */
+/* { dg-final { scan-assembler-not "R_LARCH_RELAX" { target tls_native } } } */
+
+#include "tls-ie-relax.c"
diff --git a/gcc/testsuite/gcc.target/loongarch/tls-ie-relax.c b/gcc/testsuite/gcc.target/loongarch/tls-ie-relax.c
new file mode 100644
index 00000000000..e9f7569b1da
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/tls-ie-relax.c
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -mcmodel=normal -mexplicit-relocs -mrelax" } */
+/* { dg-final { scan-assembler-times "R_LARCH_RELAX" 2 { target tls_native } } } */
+
+extern __thread int errno;
+
+void
+unimplemented (void)
+{
+  errno = -38;
+}
-- 
2.44.0


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

* Re: [PATCH] LoongArch: Emit R_LARCH_RELAX for TLS IE with non-extreme code model to allow the IE to LE linker relaxation
  2024-02-29  7:11 [PATCH] LoongArch: Emit R_LARCH_RELAX for TLS IE with non-extreme code model to allow the IE to LE linker relaxation Xi Ruoyao
@ 2024-03-07  2:43 ` mengqinggang
  2024-03-07  2:56   ` Xi Ruoyao
  0 siblings, 1 reply; 5+ messages in thread
From: mengqinggang @ 2024-03-07  2:43 UTC (permalink / raw)
  To: Xi Ruoyao, gcc-patches; +Cc: chenglulu, i, xuchenghua

Hi,

Whether to add an option to control the generation of R_LARCH_RELAX,
similar to as -mrelax/-mno-relax.


在 2024/2/29 下午3:11, Xi Ruoyao 写道:
> In Binutils we need to make IE to LE relaxation only allowed when there
> is an R_LARCH_RELAX after R_LARCH_TLE_IE_PC_{HI20,LO12} so an invalid
> "partial" relaxation won't happen with the extreme code model.  So if we
> are emitting %ie_pc_{hi20,lo12} in a non-extreme code model, emit an
> R_LARCH_RELAX to allow the relaxation.  The IE to LE relaxation does not
> require the pcalau12i and the ld instruction to be adjacent, so we don't
> need to limit ourselves to use the macro.
>
> For the distro maintainers backporting changes: this change depends on
> r14-8721, without r14-8721 R_LARCH_RELAX can be emitted mistakenly in
> the extreme code model.
>
> gcc/ChangeLog:
>
> 	* config/loongarch/loongarch.cc (loongarch_print_operand_reloc):
> 	Support 'Q' for R_LARCH_RELAX for TLS IE.
> 	(loongarch_output_move): Use 'Q' to print R_LARCH_RELAX for TLS
> 	IE.
> 	* config/loongarch/loongarch.md (ld_from_got<mode>): Likewise.
>
> gcc/testsuite/ChangeLog:
>
> 	* gcc.target/loongarch/tls-ie-relax.c: New test.
> 	* gcc.target/loongarch/tls-ie-norelax.c: New test.
> 	* gcc.target/loongarch/tls-ie-extreme.c: New test.
> ---
>
> Bootstrapped & regtested on loongarch64-linux-gnu.  Ok for trunk?
>
>   gcc/config/loongarch/loongarch.cc                 | 15 ++++++++++++++-
>   gcc/config/loongarch/loongarch.md                 |  2 +-
>   .../gcc.target/loongarch/tls-ie-extreme.c         |  5 +++++
>   .../gcc.target/loongarch/tls-ie-norelax.c         |  5 +++++
>   gcc/testsuite/gcc.target/loongarch/tls-ie-relax.c | 11 +++++++++++
>   5 files changed, 36 insertions(+), 2 deletions(-)
>   create mode 100644 gcc/testsuite/gcc.target/loongarch/tls-ie-extreme.c
>   create mode 100644 gcc/testsuite/gcc.target/loongarch/tls-ie-norelax.c
>   create mode 100644 gcc/testsuite/gcc.target/loongarch/tls-ie-relax.c
>
> diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
> index 0428b6e65d5..70e31bb831c 100644
> --- a/gcc/config/loongarch/loongarch.cc
> +++ b/gcc/config/loongarch/loongarch.cc
> @@ -4981,7 +4981,7 @@ loongarch_output_move (rtx dest, rtx src)
>   	  if (type == SYMBOL_TLS_LE)
>   	    return "lu12i.w\t%0,%h1";
>   	  else
> -	    return "pcalau12i\t%0,%h1";
> +	    return "%Q1pcalau12i\t%0,%h1";
>   	}
>   
>         if (src_code == CONST_INT)
> @@ -6145,6 +6145,7 @@ loongarch_print_operand_reloc (FILE *file, rtx op, bool hi64_part,
>      'L'  Print the low-part relocation associated with OP.
>      'm'	Print one less than CONST_INT OP in decimal.
>      'N'	Print the inverse of the integer branch condition for comparison OP.
> +   'Q'  Print R_LARCH_RELAX for TLS IE.
>      'r'  Print address 12-31bit relocation associated with OP.
>      'R'  Print address 32-51bit relocation associated with OP.
>      'T'	Print 'f' for (eq:CC ...), 't' for (ne:CC ...),
> @@ -6282,6 +6283,18 @@ loongarch_print_operand (FILE *file, rtx op, int letter)
>   					    letter);
>         break;
>   
> +    case 'Q':
> +      if (!TARGET_LINKER_RELAXATION)
> +	break;
> +
> +      if (code == HIGH)
> +	op = XEXP (op, 0);
> +
> +      if (loongarch_classify_symbolic_expression (op) == SYMBOL_TLS_IE)
> +	fprintf (file, ".reloc\t.,R_LARCH_RELAX\n\t");
> +
> +      break;
> +
>       case 'r':
>         loongarch_print_operand_reloc (file, op, false /* hi64_part */,
>   				     true /* lo_reloc */);
> diff --git a/gcc/config/loongarch/loongarch.md b/gcc/config/loongarch/loongarch.md
> index f3b5c641fce..525e1e82183 100644
> --- a/gcc/config/loongarch/loongarch.md
> +++ b/gcc/config/loongarch/loongarch.md
> @@ -2620,7 +2620,7 @@ (define_insn "@ld_from_got<mode>"
>   				(match_operand:P 2 "symbolic_operand")))]
>   	UNSPEC_LOAD_FROM_GOT))]
>     ""
> -  "ld.<d>\t%0,%1,%L2"
> +  "%Q2ld.<d>\t%0,%1,%L2"
>     [(set_attr "type" "move")]
>   )
>   
> diff --git a/gcc/testsuite/gcc.target/loongarch/tls-ie-extreme.c b/gcc/testsuite/gcc.target/loongarch/tls-ie-extreme.c
> new file mode 100644
> index 00000000000..00c545a3e8c
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/loongarch/tls-ie-extreme.c
> @@ -0,0 +1,5 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -march=loongarch64 -mabi=lp64d -mcmodel=extreme -mexplicit-relocs=auto -mrelax" } */
> +/* { dg-final { scan-assembler-not "R_LARCH_RELAX" { target tls_native } } } */
> +
> +#include "tls-ie-relax.c"
> diff --git a/gcc/testsuite/gcc.target/loongarch/tls-ie-norelax.c b/gcc/testsuite/gcc.target/loongarch/tls-ie-norelax.c
> new file mode 100644
> index 00000000000..dd6bf3634a4
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/loongarch/tls-ie-norelax.c
> @@ -0,0 +1,5 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -mcmodel=normal -mexplicit-relocs -mno-relax" } */
> +/* { dg-final { scan-assembler-not "R_LARCH_RELAX" { target tls_native } } } */
> +
> +#include "tls-ie-relax.c"
> diff --git a/gcc/testsuite/gcc.target/loongarch/tls-ie-relax.c b/gcc/testsuite/gcc.target/loongarch/tls-ie-relax.c
> new file mode 100644
> index 00000000000..e9f7569b1da
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/loongarch/tls-ie-relax.c
> @@ -0,0 +1,11 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -mcmodel=normal -mexplicit-relocs -mrelax" } */
> +/* { dg-final { scan-assembler-times "R_LARCH_RELAX" 2 { target tls_native } } } */
> +
> +extern __thread int errno;
> +
> +void
> +unimplemented (void)
> +{
> +  errno = -38;
> +}


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

* Re: [PATCH] LoongArch: Emit R_LARCH_RELAX for TLS IE with non-extreme code model to allow the IE to LE linker relaxation
  2024-03-07  2:43 ` mengqinggang
@ 2024-03-07  2:56   ` Xi Ruoyao
  2024-03-07  4:05     ` mengqinggang
  0 siblings, 1 reply; 5+ messages in thread
From: Xi Ruoyao @ 2024-03-07  2:56 UTC (permalink / raw)
  To: mengqinggang, gcc-patches; +Cc: chenglulu, i, xuchenghua

On Thu, 2024-03-07 at 10:43 +0800, mengqinggang wrote:
> Hi,
> 
> Whether to add an option to control the generation of R_LARCH_RELAX,
> similar to as -mrelax/-mno-relax.

There are already -mrelax and -mno-relax, they can be checked in the
compiler code with TARGET_LINKER_RELAXATION.

/* snip */

> > +    case 'Q':
> > +      if (!TARGET_LINKER_RELAXATION)
> > +	     break;

So with -mno-relax we'll break early here, then no R_LARCH_RELAX will be
printed.

> > +      if (code == HIGH)
> > +	     op = XEXP (op, 0);
> > +
> > +      if (loongarch_classify_symbolic_expression (op) == SYMBOL_TLS_IE)
> > +	     fprintf (file, ".reloc\t.,R_LARCH_RELAX\n\t");
> > +
> > +      break;

The tls-ie-norelax.c test case also checks for -mno-relax:

> > +/* { dg-do compile } */
> > +/* { dg-options "-O2 -mcmodel=normal -mexplicit-relocs -mno-relax" } */
> > +/* { dg-final { scan-assembler-not "R_LARCH_RELAX" { target tls_native } } } */

i.e. -mno-relax is used compiling this test case, and the compiled
assembly code should not contain R_LARCH_RELAX.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

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

* Re: [PATCH] LoongArch: Emit R_LARCH_RELAX for TLS IE with non-extreme code model to allow the IE to LE linker relaxation
  2024-03-07  2:56   ` Xi Ruoyao
@ 2024-03-07  4:05     ` mengqinggang
  2024-03-07  5:03       ` chenglulu
  0 siblings, 1 reply; 5+ messages in thread
From: mengqinggang @ 2024-03-07  4:05 UTC (permalink / raw)
  To: Xi Ruoyao, gcc-patches; +Cc: chenglulu, i, xuchenghua

Hi,

Thanks, this patch is LGTM.


在 2024/3/7 上午10:56, Xi Ruoyao 写道:
> On Thu, 2024-03-07 at 10:43 +0800, mengqinggang wrote:
>> Hi,
>>
>> Whether to add an option to control the generation of R_LARCH_RELAX,
>> similar to as -mrelax/-mno-relax.
> There are already -mrelax and -mno-relax, they can be checked in the
> compiler code with TARGET_LINKER_RELAXATION.
>
> /* snip */
>
>>> +    case 'Q':
>>> +      if (!TARGET_LINKER_RELAXATION)
>>> +	     break;
> So with -mno-relax we'll break early here, then no R_LARCH_RELAX will be
> printed.
>
>>> +      if (code == HIGH)
>>> +	     op = XEXP (op, 0);
>>> +
>>> +      if (loongarch_classify_symbolic_expression (op) == SYMBOL_TLS_IE)
>>> +	     fprintf (file, ".reloc\t.,R_LARCH_RELAX\n\t");
>>> +
>>> +      break;
> The tls-ie-norelax.c test case also checks for -mno-relax:
>
>>> +/* { dg-do compile } */
>>> +/* { dg-options "-O2 -mcmodel=normal -mexplicit-relocs -mno-relax" } */
>>> +/* { dg-final { scan-assembler-not "R_LARCH_RELAX" { target tls_native } } } */
> i.e. -mno-relax is used compiling this test case, and the compiled
> assembly code should not contain R_LARCH_RELAX.
>


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

* Re: [PATCH] LoongArch: Emit R_LARCH_RELAX for TLS IE with non-extreme code model to allow the IE to LE linker relaxation
  2024-03-07  4:05     ` mengqinggang
@ 2024-03-07  5:03       ` chenglulu
  0 siblings, 0 replies; 5+ messages in thread
From: chenglulu @ 2024-03-07  5:03 UTC (permalink / raw)
  To: mengqinggang, Xi Ruoyao, gcc-patches; +Cc: i, xuchenghua


在 2024/3/7 下午12:05, mengqinggang 写道:
> Hi,
>
> Thanks, this patch is LGTM.

I don't have a problem either.

Thanks.

>
>
> 在 2024/3/7 上午10:56, Xi Ruoyao 写道:
>> On Thu, 2024-03-07 at 10:43 +0800, mengqinggang wrote:
>>> Hi,
>>>
>>> Whether to add an option to control the generation of R_LARCH_RELAX,
>>> similar to as -mrelax/-mno-relax.
>> There are already -mrelax and -mno-relax, they can be checked in the
>> compiler code with TARGET_LINKER_RELAXATION.
>>
>> /* snip */
>>
>>>> +    case 'Q':
>>>> +      if (!TARGET_LINKER_RELAXATION)
>>>> +         break;
>> So with -mno-relax we'll break early here, then no R_LARCH_RELAX will be
>> printed.
>>
>>>> +      if (code == HIGH)
>>>> +         op = XEXP (op, 0);
>>>> +
>>>> +      if (loongarch_classify_symbolic_expression (op) == 
>>>> SYMBOL_TLS_IE)
>>>> +         fprintf (file, ".reloc\t.,R_LARCH_RELAX\n\t");
>>>> +
>>>> +      break;
>> The tls-ie-norelax.c test case also checks for -mno-relax:
>>
>>>> +/* { dg-do compile } */
>>>> +/* { dg-options "-O2 -mcmodel=normal -mexplicit-relocs -mno-relax" 
>>>> } */
>>>> +/* { dg-final { scan-assembler-not "R_LARCH_RELAX" { target 
>>>> tls_native } } } */
>> i.e. -mno-relax is used compiling this test case, and the compiled
>> assembly code should not contain R_LARCH_RELAX.
>>


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

end of thread, other threads:[~2024-03-07  5:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-29  7:11 [PATCH] LoongArch: Emit R_LARCH_RELAX for TLS IE with non-extreme code model to allow the IE to LE linker relaxation Xi Ruoyao
2024-03-07  2:43 ` mengqinggang
2024-03-07  2:56   ` Xi Ruoyao
2024-03-07  4:05     ` mengqinggang
2024-03-07  5:03       ` chenglulu

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).