LoongArch: Get __tls_get_addr address through got table when disable plt.

Message ID 20220811113527.286471-1-chenglulu@loongson.cn
State New
Headers
Series LoongArch: Get __tls_get_addr address through got table when disable plt. |

Commit Message

Lulu Cheng Aug. 11, 2022, 11:35 a.m. UTC
  thread.c:

__attribute__ ((tls_model ("global-dynamic"))) __thread int a;

void
test (void)
{
  a = 10;
}

Compile the tests with -fno-plt, error message is as follows:

thread.c: In function 'test':
thread.c:7:1: error: unrecognizable insn:
    7 | }
      | ^
(call_insn/u 7 6 8 2 (parallel [
            (set (reg:DI 4 $r4)
                (call (mem:SI (symbol_ref:DI ("__tls_get_addr") [flags 0x41] <function_decl 0x7f30ce305400 __tls_get_addr>) [0  S4 A8])
                    (const_int 0 [0])))
            (clobber (reg:SI 1 $r1))
        ]) "thread.c":5:5 -1
     (expr_list:REG_EH_REGION (const_int -2147483648 [0xffffffff80000000])
        (nil))
    (expr_list (use (reg:DI 4 $r4))
        (nil)))
during RTL pass: vregs
thread.c:7:1: internal compiler error: in extract_insn, at recog.cc:2791


-------------------------

Fix bug, ICE with tls gd var with -fno-plt.

gcc/ChangeLog:

	* config/loongarch/loongarch.cc (loongarch_call_tls_get_addr):
	Get __tls_get_addr address through got table when disable plt.

gcc/testsuite/ChangeLog:

	* gcc.target/loongarch/tls-gd-noplt.c: New test.
---
 gcc/config/loongarch/loongarch.cc                 | 14 ++++++++++++--
 gcc/testsuite/gcc.target/loongarch/tls-gd-noplt.c | 12 ++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/loongarch/tls-gd-noplt.c
  

Comments

Lulu Cheng Aug. 18, 2022, 2:11 a.m. UTC | #1
2022/8/11 δΈ‹εˆ7:35, Lulu Cheng 写道:
> thread.c:
>
> __attribute__ ((tls_model ("global-dynamic"))) __thread int a;
>
> void
> test (void)
> {
>    a = 10;
> }
>
> Compile the tests with -fno-plt, error message is as follows:
>
> thread.c: In function 'test':
> thread.c:7:1: error: unrecognizable insn:
>      7 | }
>        | ^
> (call_insn/u 7 6 8 2 (parallel [
>              (set (reg:DI 4 $r4)
>                  (call (mem:SI (symbol_ref:DI ("__tls_get_addr") [flags 0x41] <function_decl 0x7f30ce305400 __tls_get_addr>) [0  S4 A8])
>                      (const_int 0 [0])))
>              (clobber (reg:SI 1 $r1))
>          ]) "thread.c":5:5 -1
>       (expr_list:REG_EH_REGION (const_int -2147483648 [0xffffffff80000000])
>          (nil))
>      (expr_list (use (reg:DI 4 $r4))
>          (nil)))
> during RTL pass: vregs
> thread.c:7:1: internal compiler error: in extract_insn, at recog.cc:2791
>
>
> -------------------------
>
> Fix bug, ICE with tls gd var with -fno-plt.
>
> gcc/ChangeLog:
>
> 	* config/loongarch/loongarch.cc (loongarch_call_tls_get_addr):
> 	Get __tls_get_addr address through got table when disable plt.
>
> gcc/testsuite/ChangeLog:
>
> 	* gcc.target/loongarch/tls-gd-noplt.c: New test.
> ---
>   gcc/config/loongarch/loongarch.cc                 | 14 ++++++++++++--
>   gcc/testsuite/gcc.target/loongarch/tls-gd-noplt.c | 12 ++++++++++++
>   2 files changed, 24 insertions(+), 2 deletions(-)
>   create mode 100644 gcc/testsuite/gcc.target/loongarch/tls-gd-noplt.c
>
> diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
> index 5c9a33c14f7..8d8232e5805 100644
> --- a/gcc/config/loongarch/loongarch.cc
> +++ b/gcc/config/loongarch/loongarch.cc
> @@ -2297,8 +2297,18 @@ loongarch_call_tls_get_addr (rtx sym, enum loongarch_symbol_type type, rtx v0)
>     else
>       gcc_unreachable ();
>   
> -  insn = emit_call_insn (gen_call_value_internal (v0, loongarch_tls_symbol,
> -						  const0_rtx));
> +  if (flag_plt)
> +    insn = emit_call_insn (gen_call_value_internal (v0, loongarch_tls_symbol,
> +						    const0_rtx));
> +  else
> +    {
> +      rtx dest = gen_reg_rtx (Pmode);
> +      rtx high = gen_reg_rtx (Pmode);
> +      loongarch_emit_move (high, gen_rtx_HIGH (Pmode, loongarch_tls_symbol));
> +      emit_insn (gen_ld_from_got (Pmode, dest, high, loongarch_tls_symbol));
> +      insn = emit_call_insn (gen_call_value_internal (v0, dest, const0_rtx));
> +    }
> +
>     RTL_CONST_CALL_P (insn) = 1;
>     use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
>     insn = get_insns ();
> diff --git a/gcc/testsuite/gcc.target/loongarch/tls-gd-noplt.c b/gcc/testsuite/gcc.target/loongarch/tls-gd-noplt.c
> new file mode 100644
> index 00000000000..a71bb48676d
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/loongarch/tls-gd-noplt.c
> @@ -0,0 +1,12 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fno-plt" } */
> +/* { dg-final { scan-assembler "pcalau12i\t.*%got_pc_hi20\\(__tls_get_addr\\)" } } */
> +
> +__attribute__ ((tls_model ("global-dynamic"))) __thread int a;
> +
> +void
> +test (void)
> +{
> +  a = 10;
> +}
> +


Pushed r13-2104.
  

Patch

diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
index 5c9a33c14f7..8d8232e5805 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -2297,8 +2297,18 @@  loongarch_call_tls_get_addr (rtx sym, enum loongarch_symbol_type type, rtx v0)
   else
     gcc_unreachable ();
 
-  insn = emit_call_insn (gen_call_value_internal (v0, loongarch_tls_symbol,
-						  const0_rtx));
+  if (flag_plt)
+    insn = emit_call_insn (gen_call_value_internal (v0, loongarch_tls_symbol,
+						    const0_rtx));
+  else
+    {
+      rtx dest = gen_reg_rtx (Pmode);
+      rtx high = gen_reg_rtx (Pmode);
+      loongarch_emit_move (high, gen_rtx_HIGH (Pmode, loongarch_tls_symbol));
+      emit_insn (gen_ld_from_got (Pmode, dest, high, loongarch_tls_symbol));
+      insn = emit_call_insn (gen_call_value_internal (v0, dest, const0_rtx));
+    }
+
   RTL_CONST_CALL_P (insn) = 1;
   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
   insn = get_insns ();
diff --git a/gcc/testsuite/gcc.target/loongarch/tls-gd-noplt.c b/gcc/testsuite/gcc.target/loongarch/tls-gd-noplt.c
new file mode 100644
index 00000000000..a71bb48676d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/tls-gd-noplt.c
@@ -0,0 +1,12 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-plt" } */
+/* { dg-final { scan-assembler "pcalau12i\t.*%got_pc_hi20\\(__tls_get_addr\\)" } } */
+
+__attribute__ ((tls_model ("global-dynamic"))) __thread int a;
+
+void
+test (void)
+{
+  a = 10;
+}
+