public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH v2] LoongArch: Add support for TLS descriptors
@ 2024-02-29  1:42 mengqinggang
  2024-02-29  6:08 ` Xi Ruoyao
  2024-02-29  6:51 ` Jinyang He
  0 siblings, 2 replies; 6+ messages in thread
From: mengqinggang @ 2024-02-29  1:42 UTC (permalink / raw)
  To: gcc-patches
  Cc: xuchenghua, chenglulu, cailulu, xry111, i.swmail, maskray,
	luweining, wanglei, hejinyang, mengqinggang

Generate la.tls.desc macro instruction for TLS descriptors model.

la.tls.desc expand to
  pcalau12i $a0, %desc_pc_hi20(a)
  ld.d      $a1, $a0, %desc_ld_pc_lo12(a)
  addi.d    $a0, $a0, %desc_add_pc_lo12(a)
  jirl      $ra, $a1, %desc_call(a)

The default is TLS descriptors, but can be configure with
-mtls-dialect={desc,trad}.

gcc/ChangeLog:

	* config.gcc: Add --with_tls to change the TLS flavor.
	* config/loongarch/genopts/loongarch.opt.in: Add -mtls-dialect to
	configure TLS flavor.
	* config/loongarch/loongarch-opts.h (enum loongarch_tls_type): New.
	* config/loongarch/loongarch-protos.h (NUM_SYMBOL_TYPES): New.
	* config/loongarch/loongarch.cc (loongarch_symbol_insns): Add
	instruction sequence length data for TLS DESC.
	(loongarch_legitimize_tls_address): New TLS DESC instruction sequence.
	* config/loongarch/loongarch.h (TARGET_TLS_DESC): New.
	* config/loongarch/loongarch.md (@got_load_tls_desc<mode>): New.
	* config/loongarch/loongarch.opt: Regenerated.
---
Changes v1 -> v2:
- Clobber fcc0-fcc7 registers in got_load_tls_desc template.
- Support --with-tls in configure.

 gcc/config.gcc                                | 15 ++++++-
 gcc/config/loongarch/genopts/loongarch.opt.in | 14 ++++++
 gcc/config/loongarch/loongarch-opts.h         |  6 +++
 gcc/config/loongarch/loongarch-protos.h       |  3 +-
 gcc/config/loongarch/loongarch.cc             | 45 +++++++++++++++----
 gcc/config/loongarch/loongarch.h              |  8 ++++
 gcc/config/loongarch/loongarch.md             | 36 +++++++++++++++
 gcc/config/loongarch/loongarch.opt            | 14 ++++++
 8 files changed, 130 insertions(+), 11 deletions(-)

diff --git a/gcc/config.gcc b/gcc/config.gcc
index a0f9c672308..72a5e992821 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -2546,6 +2546,7 @@ loongarch*-*-linux*)
 	# Force .init_array support.  The configure script cannot always
 	# automatically detect that GAS supports it, yet we require it.
 	gcc_cv_initfini_array=yes
+	with_tls=${with_tls:-desc}
 	;;
 
 loongarch*-*-elf*)
@@ -4987,7 +4988,7 @@ case "${target}" in
 		;;
 
 	loongarch*-*)
-		supported_defaults="abi arch tune fpu simd multilib-default strict-align-lib"
+		supported_defaults="abi arch tune fpu simd multilib-default strict-align-lib tls"
 
 		# Local variables
 		unset \
@@ -5245,6 +5246,18 @@ case "${target}" in
 			with_multilib_list="${abi_base}/${abi_ext}"
 		fi
 
+		# Handle --with-tls.
+		case "$with_tls" in
+		"" \
+		| trad | desc)
+		    # OK
+		    ;;
+		*)
+		    echo "Unknown TLS method used in --with-tls=$with_tls" 1>&2
+		    exit 1
+		    ;;
+		esac
+
 		# Check if the configured default ABI combination is included in
 		# ${with_multilib_list}.
 		loongarch_multilib_list_sane=no
diff --git a/gcc/config/loongarch/genopts/loongarch.opt.in b/gcc/config/loongarch/genopts/loongarch.opt.in
index 02f918053f5..2cc943ef683 100644
--- a/gcc/config/loongarch/genopts/loongarch.opt.in
+++ b/gcc/config/loongarch/genopts/loongarch.opt.in
@@ -262,3 +262,17 @@ default value is 4.
 ; CPUCFG independently, so we use bit flags to specify them.
 TargetVariable
 HOST_WIDE_INT la_isa_evolution = 0
+
+Enum
+Name(tls_type) Type(enum loongarch_tls_type)
+The possible TLS dialects:
+
+EnumValue
+Enum(tls_type) String(trad) Value(TLS_TRADITIONAL)
+
+EnumValue
+Enum(tls_type) String(desc) Value(TLS_DESCRIPTORS)
+
+mtls-dialect=
+Target RejectNegative Joined Enum(tls_type) Var(loongarch_tls_dialect) Init(TLS_DESCRIPTORS) Save
+Specify TLS dialect.
diff --git a/gcc/config/loongarch/loongarch-opts.h b/gcc/config/loongarch/loongarch-opts.h
index 586e67e65ee..a08ab6fac10 100644
--- a/gcc/config/loongarch/loongarch-opts.h
+++ b/gcc/config/loongarch/loongarch-opts.h
@@ -134,4 +134,10 @@ struct loongarch_flags {
 #define HAVE_AS_TLS_LE_RELAXATION 0
 #endif
 
+/* TLS types.  */
+enum loongarch_tls_type {
+  TLS_TRADITIONAL,
+  TLS_DESCRIPTORS
+};
+
 #endif /* LOONGARCH_OPTS_H */
diff --git a/gcc/config/loongarch/loongarch-protos.h b/gcc/config/loongarch/loongarch-protos.h
index 1fdfda9af01..6b417a3c371 100644
--- a/gcc/config/loongarch/loongarch-protos.h
+++ b/gcc/config/loongarch/loongarch-protos.h
@@ -53,8 +53,9 @@ enum loongarch_symbol_type {
   SYMBOL_TLS_LE,
   SYMBOL_TLSGD,
   SYMBOL_TLSLDM,
+  SYMBOL_TLS_DESC,
 };
-#define NUM_SYMBOL_TYPES (SYMBOL_TLSLDM + 1)
+#define NUM_SYMBOL_TYPES (SYMBOL_TLS_DESC + 1)
 
 /* Routines implemented in loongarch.cc.  */
 extern rtx loongarch_emit_move (rtx, rtx);
diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
index 0428b6e65d5..b4e43f1d037 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -2030,6 +2030,9 @@ loongarch_symbol_insns (enum loongarch_symbol_type type, machine_mode mode)
     case SYMBOL_TLSLDM:
       return 3;
 
+    case SYMBOL_TLS_DESC:
+      return 4; /* pcalau12i, addi.d, ld.d, jirl.  */
+
     case SYMBOL_PCREL64:
       return 5;
 
@@ -2930,24 +2933,48 @@ loongarch_call_tls_get_addr (rtx sym, enum loongarch_symbol_type type, rtx v0)
 static rtx
 loongarch_legitimize_tls_address (rtx loc)
 {
-  rtx dest, tp, tmp, tmp1, tmp2, tmp3;
+  rtx dest, tp, tmp, tmp1, tmp2, tmp3, a0; /* a1? */
   enum tls_model model = SYMBOL_REF_TLS_MODEL (loc);
   rtx_insn *insn;
 
   switch (model)
     {
     case TLS_MODEL_LOCAL_DYNAMIC:
-      tmp = gen_rtx_REG (Pmode, GP_RETURN);
-      dest = gen_reg_rtx (Pmode);
-      insn = loongarch_call_tls_get_addr (loc, SYMBOL_TLSLDM, tmp);
-      emit_libcall_block (insn, dest, tmp, loc);
+      if (TARGET_TLS_DESC)
+	{
+	  a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
+	  dest = gen_reg_rtx (Pmode);
+	  tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
+
+	  emit_insn (gen_got_load_tls_desc (Pmode, a0, loc));
+	  emit_insn (gen_add3_insn (dest, a0, tp));
+	}
+      else
+	{
+	  tmp = gen_rtx_REG (Pmode, GP_RETURN);
+	  dest = gen_reg_rtx (Pmode);
+	  insn = loongarch_call_tls_get_addr (loc, SYMBOL_TLSLDM, tmp);
+	  emit_libcall_block (insn, dest, tmp, loc);
+	}
       break;
 
     case TLS_MODEL_GLOBAL_DYNAMIC:
-      tmp = gen_rtx_REG (Pmode, GP_RETURN);
-      dest = gen_reg_rtx (Pmode);
-      insn = loongarch_call_tls_get_addr (loc, SYMBOL_TLSGD, tmp);
-      emit_libcall_block (insn, dest, tmp, loc);
+      if (TARGET_TLS_DESC)
+	{
+	  a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
+	  dest = gen_reg_rtx (Pmode);
+	  tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
+
+	  emit_insn (gen_got_load_tls_desc (Pmode, a0, loc));
+	  emit_insn (gen_add3_insn (dest, a0, tp));
+	}
+      else
+	{
+	  tmp = gen_rtx_REG (Pmode, GP_RETURN);
+	  dest = gen_reg_rtx (Pmode);
+	  insn = loongarch_call_tls_get_addr (loc, SYMBOL_TLSGD, tmp);
+	  emit_libcall_block (insn, dest, tmp, loc);
+	}
       break;
 
     case TLS_MODEL_INITIAL_EXEC:
diff --git a/gcc/config/loongarch/loongarch.h b/gcc/config/loongarch/loongarch.h
index 8b453ab3140..262d3488120 100644
--- a/gcc/config/loongarch/loongarch.h
+++ b/gcc/config/loongarch/loongarch.h
@@ -25,6 +25,14 @@ along with GCC; see the file COPYING3.  If not see
 
 #define TARGET_SUPPORTS_WIDE_INT 1
 
+/* Support for configure-time default option. The rules are:
+   --with-tls is ignored if -mtls-dialect is specified.  */
+#define OPTION_DEFAULT_SPECS \
+  {"tls", "%{!mtls-dialect=*:-mtls-dialect=%(VALUE)}"}, \
+
+/* Check TLS Descriptors mechanism is selected.  */
+#define TARGET_TLS_DESC (loongarch_tls_dialect == TLS_DESCRIPTORS)
+
 /* Macros to silence warnings about numbers being signed in traditional
    C and unsigned in ISO C when compiled on 32-bit hosts.  */
 
diff --git a/gcc/config/loongarch/loongarch.md b/gcc/config/loongarch/loongarch.md
index dffa41b0bf5..f525c45a398 100644
--- a/gcc/config/loongarch/loongarch.md
+++ b/gcc/config/loongarch/loongarch.md
@@ -52,6 +52,11 @@ (define_c_enum "unspec" [
 
   ;; TLS
   UNSPEC_TLS
+  UNSPEC_TLS_GD
+  UNSPEC_TLS_LD
+  UNSPEC_TLS_LE
+  UNSPEC_TLS_IE
+  UNSPEC_TLS_DESC
 
   ;; Stack tie
   UNSPEC_TIE
@@ -123,10 +128,21 @@ (define_c_enum "unspecv" [
 (define_constants
   [(RETURN_ADDR_REGNUM		1)
    (TP_REGNUM			2)
+   (A0_REGNUM			4)
+   (A1_REGNUM			5)
    (T0_REGNUM			12)
    (T1_REGNUM			13)
    (S0_REGNUM			23)
 
+   (FCC0_REGNUM			64)
+   (FCC1_REGNUM			65)
+   (FCC2_REGNUM			66)
+   (FCC3_REGNUM			67)
+   (FCC4_REGNUM			68)
+   (FCC5_REGNUM			69)
+   (FCC6_REGNUM			70)
+   (FCC7_REGNUM			71)
+
    ;; Return path styles
    (NORMAL_RETURN		0)
    (SIBCALL_RETURN		1)
@@ -2760,6 +2776,26 @@ (define_insn "store_word<mode>"
 
 ;; Thread-Local Storage
 
+(define_insn "@got_load_tls_desc<mode>"
+  [(set (match_operand:P 0 "register_operand" "=r")
+	(unspec:P
+	    [(match_operand:P 1 "symbolic_operand" "")]
+	    UNSPEC_TLS_DESC))
+    (clobber (reg:SI FCC0_REGNUM))
+    (clobber (reg:SI FCC1_REGNUM))
+    (clobber (reg:SI FCC2_REGNUM))
+    (clobber (reg:SI FCC3_REGNUM))
+    (clobber (reg:SI FCC4_REGNUM))
+    (clobber (reg:SI FCC5_REGNUM))
+    (clobber (reg:SI FCC6_REGNUM))
+    (clobber (reg:SI FCC7_REGNUM))
+    (clobber (reg:SI A1_REGNUM))
+    (clobber (reg:SI RETURN_ADDR_REGNUM))]
+  "TARGET_TLS_DESC"
+  "la.tls.desc\t%0,%1"
+  [(set_attr "got" "load")
+   (set_attr "mode" "<MODE>")])
+
 (define_insn "@load_tls<mode>"
   [(set (match_operand:P 0 "register_operand" "=r")
 	(unspec:P
diff --git a/gcc/config/loongarch/loongarch.opt b/gcc/config/loongarch/loongarch.opt
index f10fcdd968c..04846ae86c0 100644
--- a/gcc/config/loongarch/loongarch.opt
+++ b/gcc/config/loongarch/loongarch.opt
@@ -271,6 +271,20 @@ default value is 4.
 TargetVariable
 HOST_WIDE_INT la_isa_evolution = 0
 
+Enum
+Name(tls_type) Type(enum loongarch_tls_type)
+The possible TLS dialects:
+
+EnumValue
+Enum(tls_type) String(trad) Value(TLS_TRADITIONAL)
+
+EnumValue
+Enum(tls_type) String(desc) Value(TLS_DESCRIPTORS)
+
+mtls-dialect=
+Target RejectNegative Joined Enum(tls_type) Var(loongarch_tls_dialect) Init(TLS_DESCRIPTORS) Save
+Specify TLS dialect.
+
 mfrecipe
 Target Mask(ISA_FRECIPE) Var(la_isa_evolution)
 Support frecipe.{s/d} and frsqrte.{s/d} instructions.
-- 
2.36.0


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

* Re: [PATCH v2] LoongArch: Add support for TLS descriptors
  2024-02-29  1:42 [PATCH v2] LoongArch: Add support for TLS descriptors mengqinggang
@ 2024-02-29  6:08 ` Xi Ruoyao
  2024-02-29  6:14   ` Xi Ruoyao
  2024-03-01  9:39   ` mengqinggang
  2024-02-29  6:51 ` Jinyang He
  1 sibling, 2 replies; 6+ messages in thread
From: Xi Ruoyao @ 2024-02-29  6:08 UTC (permalink / raw)
  To: mengqinggang, gcc-patches
  Cc: xuchenghua, chenglulu, cailulu, i.swmail, maskray, luweining,
	wanglei, hejinyang

On Thu, 2024-02-29 at 09:42 +0800, mengqinggang wrote:
> Generate la.tls.desc macro instruction for TLS descriptors model.
> 
> la.tls.desc expand to
>   pcalau12i $a0, %desc_pc_hi20(a)
>   ld.d      $a1, $a0, %desc_ld_pc_lo12(a)
>   addi.d    $a0, $a0, %desc_add_pc_lo12(a)
>   jirl      $ra, $a1, %desc_call(a)
> 
> The default is TLS descriptors, but can be configure with
> -mtls-dialect={desc,trad}.

Please keep trad as the default for now.  Glibc-2.40 will be released
after GCC 14.1 but we don't want to end up in a situation where the
default configuration of the latest GCC release creating something not
working with latest Glibc release.

And there's also musl libc we need to take into account.

Or you can write some autoconf test for if the assembler supports
tlsdesc and check TARGET_GLIBC_MAJOR & TARGET_GLIBC_MINOR for Glibc
version to decide if enable desc by default.  If you want this but don't
have time to implement you can leave trad the default and I'll take care
of this.

/* snip */

> +(define_insn "@got_load_tls_desc<mode>"
> +  [(set (match_operand:P 0 "register_operand" "=r")
> +	(unspec:P
> +	    [(match_operand:P 1 "symbolic_operand" "")]
> +	    UNSPEC_TLS_DESC))
> +    (clobber (reg:SI FCC0_REGNUM))
> +    (clobber (reg:SI FCC1_REGNUM))
> +    (clobber (reg:SI FCC2_REGNUM))
> +    (clobber (reg:SI FCC3_REGNUM))
> +    (clobber (reg:SI FCC4_REGNUM))
> +    (clobber (reg:SI FCC5_REGNUM))
> +    (clobber (reg:SI FCC6_REGNUM))
> +    (clobber (reg:SI FCC7_REGNUM))
> +    (clobber (reg:SI A1_REGNUM))
> +    (clobber (reg:SI RETURN_ADDR_REGNUM))]

Ok, the clobber list is correct.

> +  "TARGET_TLS_DESC"
> +  "la.tls.desc\t%0,%1"

With -mexplicit-relocs=always we should emit %desc_pc_lo12 etc. instead
of la.tls.desc.  As we don't want to add too many code we can just hard
code the 4 instructions here instead of splitting this insn, just
something like

{ return TARGET_EXPLICIT_RELOCS_ALWAS ? "......" : "la.tls.desc\t%0,%1"; }

> +  [(set_attr "got" "load")
> +   (set_attr "mode" "<MODE>")])

We need (set_attr "length" "16") in this list as this actually expands
into 16 bytes.


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

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

* Re: [PATCH v2] LoongArch: Add support for TLS descriptors
  2024-02-29  6:08 ` Xi Ruoyao
@ 2024-02-29  6:14   ` Xi Ruoyao
  2024-03-01  9:39   ` mengqinggang
  1 sibling, 0 replies; 6+ messages in thread
From: Xi Ruoyao @ 2024-02-29  6:14 UTC (permalink / raw)
  To: mengqinggang, gcc-patches
  Cc: xuchenghua, chenglulu, cailulu, i.swmail, maskray, luweining,
	wanglei, hejinyang

On Thu, 2024-02-29 at 14:08 +0800, Xi Ruoyao wrote:
> > +  "TARGET_TLS_DESC"
> > +  "la.tls.desc\t%0,%1"
> 
> With -mexplicit-relocs=always we should emit %desc_pc_lo12 etc. instead
> of la.tls.desc.  As we don't want to add too many code we can just hard
> code the 4 instructions here instead of splitting this insn, just
> something like
> 
> { return TARGET_EXPLICIT_RELOCS_ALWAS ? "......" : "la.tls.desc\t%0,%1"; }

And if -mcmodel=extreme we should use a 3-operand la.tls.desc.  Or if we
don't want to support this we can just error out if -mcmodel=extreme -
mtls-dialect=desc.

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

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

* Re: [PATCH v2] LoongArch: Add support for TLS descriptors
  2024-02-29  1:42 [PATCH v2] LoongArch: Add support for TLS descriptors mengqinggang
  2024-02-29  6:08 ` Xi Ruoyao
@ 2024-02-29  6:51 ` Jinyang He
  1 sibling, 0 replies; 6+ messages in thread
From: Jinyang He @ 2024-02-29  6:51 UTC (permalink / raw)
  To: mengqinggang, gcc-patches
  Cc: xuchenghua, chenglulu, cailulu, xry111, i.swmail, maskray,
	luweining, wanglei

On 2024-02-29 09:42, mengqinggang wrote:

> Generate la.tls.desc macro instruction for TLS descriptors model.
>
> la.tls.desc expand to
>    pcalau12i $a0, %desc_pc_hi20(a)
>    ld.d      $a1, $a0, %desc_ld_pc_lo12(a)
>    addi.d    $a0, $a0, %desc_add_pc_lo12(a)
>    jirl      $ra, $a1, %desc_call(a)

Sorry for I might miss something before. Just some confusing.
In binutils `la.tls.desc` has been resolved as

#define INSN_LA_TLS_DESC64              \
   "pcalau12i $r4,%%desc_pc_hi20(%2);"   \
   "addi.d $r4,$r4,%%desc_pc_lo12(%2);"  \
   "ld.d $r1,$r4,%%desc_ld(%2);"         \
   "jirl $r1,$r1,%%desc_call(%2);",      \

Should is need to be consistent with binutils?


>
> The default is TLS descriptors, but can be configure with
> -mtls-dialect={desc,trad}.
>
> gcc/ChangeLog:
>
> 	* config.gcc: Add --with_tls to change the TLS flavor.
> 	* config/loongarch/genopts/loongarch.opt.in: Add -mtls-dialect to
> 	configure TLS flavor.
> 	* config/loongarch/loongarch-opts.h (enum loongarch_tls_type): New.
> 	* config/loongarch/loongarch-protos.h (NUM_SYMBOL_TYPES): New.
> 	* config/loongarch/loongarch.cc (loongarch_symbol_insns): Add
> 	instruction sequence length data for TLS DESC.
> 	(loongarch_legitimize_tls_address): New TLS DESC instruction sequence.
> 	* config/loongarch/loongarch.h (TARGET_TLS_DESC): New.
> 	* config/loongarch/loongarch.md (@got_load_tls_desc<mode>): New.
> 	* config/loongarch/loongarch.opt: Regenerated.
> ---
> Changes v1 -> v2:
> - Clobber fcc0-fcc7 registers in got_load_tls_desc template.
> - Support --with-tls in configure.
>
>   gcc/config.gcc                                | 15 ++++++-
>   gcc/config/loongarch/genopts/loongarch.opt.in | 14 ++++++
>   gcc/config/loongarch/loongarch-opts.h         |  6 +++
>   gcc/config/loongarch/loongarch-protos.h       |  3 +-
>   gcc/config/loongarch/loongarch.cc             | 45 +++++++++++++++----
>   gcc/config/loongarch/loongarch.h              |  8 ++++
>   gcc/config/loongarch/loongarch.md             | 36 +++++++++++++++
>   gcc/config/loongarch/loongarch.opt            | 14 ++++++
>   8 files changed, 130 insertions(+), 11 deletions(-)
>
> diff --git a/gcc/config.gcc b/gcc/config.gcc
> index a0f9c672308..72a5e992821 100644
> --- a/gcc/config.gcc
> +++ b/gcc/config.gcc
> @@ -2546,6 +2546,7 @@ loongarch*-*-linux*)
>   	# Force .init_array support.  The configure script cannot always
>   	# automatically detect that GAS supports it, yet we require it.
>   	gcc_cv_initfini_array=yes
> +	with_tls=${with_tls:-desc}
>   	;;
>   
>   loongarch*-*-elf*)
> @@ -4987,7 +4988,7 @@ case "${target}" in
>   		;;
>   
>   	loongarch*-*)
> -		supported_defaults="abi arch tune fpu simd multilib-default strict-align-lib"
> +		supported_defaults="abi arch tune fpu simd multilib-default strict-align-lib tls"
>   
>   		# Local variables
>   		unset \
> @@ -5245,6 +5246,18 @@ case "${target}" in
>   			with_multilib_list="${abi_base}/${abi_ext}"
>   		fi
>   
> +		# Handle --with-tls.
> +		case "$with_tls" in
> +		"" \
> +		| trad | desc)
> +		    # OK
> +		    ;;
> +		*)
> +		    echo "Unknown TLS method used in --with-tls=$with_tls" 1>&2
> +		    exit 1
> +		    ;;
> +		esac
> +
>   		# Check if the configured default ABI combination is included in
>   		# ${with_multilib_list}.
>   		loongarch_multilib_list_sane=no
> diff --git a/gcc/config/loongarch/genopts/loongarch.opt.in b/gcc/config/loongarch/genopts/loongarch.opt.in
> index 02f918053f5..2cc943ef683 100644
> --- a/gcc/config/loongarch/genopts/loongarch.opt.in
> +++ b/gcc/config/loongarch/genopts/loongarch.opt.in
> @@ -262,3 +262,17 @@ default value is 4.
>   ; CPUCFG independently, so we use bit flags to specify them.
>   TargetVariable
>   HOST_WIDE_INT la_isa_evolution = 0
> +
> +Enum
> +Name(tls_type) Type(enum loongarch_tls_type)
> +The possible TLS dialects:
> +
> +EnumValue
> +Enum(tls_type) String(trad) Value(TLS_TRADITIONAL)
> +
> +EnumValue
> +Enum(tls_type) String(desc) Value(TLS_DESCRIPTORS)
> +
> +mtls-dialect=
> +Target RejectNegative Joined Enum(tls_type) Var(loongarch_tls_dialect) Init(TLS_DESCRIPTORS) Save
> +Specify TLS dialect.
> diff --git a/gcc/config/loongarch/loongarch-opts.h b/gcc/config/loongarch/loongarch-opts.h
> index 586e67e65ee..a08ab6fac10 100644
> --- a/gcc/config/loongarch/loongarch-opts.h
> +++ b/gcc/config/loongarch/loongarch-opts.h
> @@ -134,4 +134,10 @@ struct loongarch_flags {
>   #define HAVE_AS_TLS_LE_RELAXATION 0
>   #endif
>   
> +/* TLS types.  */
> +enum loongarch_tls_type {
> +  TLS_TRADITIONAL,
> +  TLS_DESCRIPTORS
> +};
> +
>   #endif /* LOONGARCH_OPTS_H */
> diff --git a/gcc/config/loongarch/loongarch-protos.h b/gcc/config/loongarch/loongarch-protos.h
> index 1fdfda9af01..6b417a3c371 100644
> --- a/gcc/config/loongarch/loongarch-protos.h
> +++ b/gcc/config/loongarch/loongarch-protos.h
> @@ -53,8 +53,9 @@ enum loongarch_symbol_type {
>     SYMBOL_TLS_LE,
>     SYMBOL_TLSGD,
>     SYMBOL_TLSLDM,
> +  SYMBOL_TLS_DESC,
>   };
> -#define NUM_SYMBOL_TYPES (SYMBOL_TLSLDM + 1)
> +#define NUM_SYMBOL_TYPES (SYMBOL_TLS_DESC + 1)
>   
>   /* Routines implemented in loongarch.cc.  */
>   extern rtx loongarch_emit_move (rtx, rtx);
> diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
> index 0428b6e65d5..b4e43f1d037 100644
> --- a/gcc/config/loongarch/loongarch.cc
> +++ b/gcc/config/loongarch/loongarch.cc
> @@ -2030,6 +2030,9 @@ loongarch_symbol_insns (enum loongarch_symbol_type type, machine_mode mode)
>       case SYMBOL_TLSLDM:
>         return 3;
>   
> +    case SYMBOL_TLS_DESC:
> +      return 4; /* pcalau12i, addi.d, ld.d, jirl.  */
> +
>       case SYMBOL_PCREL64:
>         return 5;
>   
> @@ -2930,24 +2933,48 @@ loongarch_call_tls_get_addr (rtx sym, enum loongarch_symbol_type type, rtx v0)
>   static rtx
>   loongarch_legitimize_tls_address (rtx loc)
>   {
> -  rtx dest, tp, tmp, tmp1, tmp2, tmp3;
> +  rtx dest, tp, tmp, tmp1, tmp2, tmp3, a0; /* a1? */
>     enum tls_model model = SYMBOL_REF_TLS_MODEL (loc);
>     rtx_insn *insn;
>   
>     switch (model)
>       {
>       case TLS_MODEL_LOCAL_DYNAMIC:
> -      tmp = gen_rtx_REG (Pmode, GP_RETURN);
> -      dest = gen_reg_rtx (Pmode);
> -      insn = loongarch_call_tls_get_addr (loc, SYMBOL_TLSLDM, tmp);
> -      emit_libcall_block (insn, dest, tmp, loc);
> +      if (TARGET_TLS_DESC)
> +	{
> +	  a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
> +	  dest = gen_reg_rtx (Pmode);
> +	  tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
> +
> +	  emit_insn (gen_got_load_tls_desc (Pmode, a0, loc));
> +	  emit_insn (gen_add3_insn (dest, a0, tp));
> +	}
> +      else
> +	{
> +	  tmp = gen_rtx_REG (Pmode, GP_RETURN);
> +	  dest = gen_reg_rtx (Pmode);
> +	  insn = loongarch_call_tls_get_addr (loc, SYMBOL_TLSLDM, tmp);
> +	  emit_libcall_block (insn, dest, tmp, loc);
> +	}
>         break;
>   
>       case TLS_MODEL_GLOBAL_DYNAMIC:
> -      tmp = gen_rtx_REG (Pmode, GP_RETURN);
> -      dest = gen_reg_rtx (Pmode);
> -      insn = loongarch_call_tls_get_addr (loc, SYMBOL_TLSGD, tmp);
> -      emit_libcall_block (insn, dest, tmp, loc);
> +      if (TARGET_TLS_DESC)
> +	{
> +	  a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
> +	  dest = gen_reg_rtx (Pmode);
> +	  tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
> +
> +	  emit_insn (gen_got_load_tls_desc (Pmode, a0, loc));
> +	  emit_insn (gen_add3_insn (dest, a0, tp));
> +	}
> +      else
> +	{
> +	  tmp = gen_rtx_REG (Pmode, GP_RETURN);
> +	  dest = gen_reg_rtx (Pmode);
> +	  insn = loongarch_call_tls_get_addr (loc, SYMBOL_TLSGD, tmp);
> +	  emit_libcall_block (insn, dest, tmp, loc);
> +	}
>         break;
>   
>       case TLS_MODEL_INITIAL_EXEC:
> diff --git a/gcc/config/loongarch/loongarch.h b/gcc/config/loongarch/loongarch.h
> index 8b453ab3140..262d3488120 100644
> --- a/gcc/config/loongarch/loongarch.h
> +++ b/gcc/config/loongarch/loongarch.h
> @@ -25,6 +25,14 @@ along with GCC; see the file COPYING3.  If not see
>   
>   #define TARGET_SUPPORTS_WIDE_INT 1
>   
> +/* Support for configure-time default option. The rules are:
> +   --with-tls is ignored if -mtls-dialect is specified.  */
> +#define OPTION_DEFAULT_SPECS \
> +  {"tls", "%{!mtls-dialect=*:-mtls-dialect=%(VALUE)}"}, \
> +
> +/* Check TLS Descriptors mechanism is selected.  */
> +#define TARGET_TLS_DESC (loongarch_tls_dialect == TLS_DESCRIPTORS)
> +
>   /* Macros to silence warnings about numbers being signed in traditional
>      C and unsigned in ISO C when compiled on 32-bit hosts.  */
>   
> diff --git a/gcc/config/loongarch/loongarch.md b/gcc/config/loongarch/loongarch.md
> index dffa41b0bf5..f525c45a398 100644
> --- a/gcc/config/loongarch/loongarch.md
> +++ b/gcc/config/loongarch/loongarch.md
> @@ -52,6 +52,11 @@ (define_c_enum "unspec" [
>   
>     ;; TLS
>     UNSPEC_TLS
> +  UNSPEC_TLS_GD
> +  UNSPEC_TLS_LD
> +  UNSPEC_TLS_LE
> +  UNSPEC_TLS_IE
> +  UNSPEC_TLS_DESC
>   
>     ;; Stack tie
>     UNSPEC_TIE
> @@ -123,10 +128,21 @@ (define_c_enum "unspecv" [
>   (define_constants
>     [(RETURN_ADDR_REGNUM		1)
>      (TP_REGNUM			2)
> +   (A0_REGNUM			4)
> +   (A1_REGNUM			5)
>      (T0_REGNUM			12)
>      (T1_REGNUM			13)
>      (S0_REGNUM			23)
>   
> +   (FCC0_REGNUM			64)
> +   (FCC1_REGNUM			65)
> +   (FCC2_REGNUM			66)
> +   (FCC3_REGNUM			67)
> +   (FCC4_REGNUM			68)
> +   (FCC5_REGNUM			69)
> +   (FCC6_REGNUM			70)
> +   (FCC7_REGNUM			71)
> +
>      ;; Return path styles
>      (NORMAL_RETURN		0)
>      (SIBCALL_RETURN		1)
> @@ -2760,6 +2776,26 @@ (define_insn "store_word<mode>"
>   
>   ;; Thread-Local Storage
>   
> +(define_insn "@got_load_tls_desc<mode>"
> +  [(set (match_operand:P 0 "register_operand" "=r")
> +	(unspec:P
> +	    [(match_operand:P 1 "symbolic_operand" "")]
> +	    UNSPEC_TLS_DESC))
> +    (clobber (reg:SI FCC0_REGNUM))
> +    (clobber (reg:SI FCC1_REGNUM))
> +    (clobber (reg:SI FCC2_REGNUM))
> +    (clobber (reg:SI FCC3_REGNUM))
> +    (clobber (reg:SI FCC4_REGNUM))
> +    (clobber (reg:SI FCC5_REGNUM))
> +    (clobber (reg:SI FCC6_REGNUM))
> +    (clobber (reg:SI FCC7_REGNUM))
> +    (clobber (reg:SI A1_REGNUM))
And does its clobber need to set A1_REGNUM? I saw the
_dl_tlsdesc_{return,undefweak,dynamic} in [1] and they didn't break $a1.

[1] https://sourceware.org/pipermail/libc-alpha/2024-February/155068.html


Thanks,

Jinyang

> +    (clobber (reg:SI RETURN_ADDR_REGNUM))]
> +  "TARGET_TLS_DESC"
> +  "la.tls.desc\t%0,%1"
> +  [(set_attr "got" "load")
> +   (set_attr "mode" "<MODE>")])
> +
>   (define_insn "@load_tls<mode>"
>     [(set (match_operand:P 0 "register_operand" "=r")
>   	(unspec:P
> diff --git a/gcc/config/loongarch/loongarch.opt b/gcc/config/loongarch/loongarch.opt
> index f10fcdd968c..04846ae86c0 100644
> --- a/gcc/config/loongarch/loongarch.opt
> +++ b/gcc/config/loongarch/loongarch.opt
> @@ -271,6 +271,20 @@ default value is 4.
>   TargetVariable
>   HOST_WIDE_INT la_isa_evolution = 0
>   
> +Enum
> +Name(tls_type) Type(enum loongarch_tls_type)
> +The possible TLS dialects:
> +
> +EnumValue
> +Enum(tls_type) String(trad) Value(TLS_TRADITIONAL)
> +
> +EnumValue
> +Enum(tls_type) String(desc) Value(TLS_DESCRIPTORS)
> +
> +mtls-dialect=
> +Target RejectNegative Joined Enum(tls_type) Var(loongarch_tls_dialect) Init(TLS_DESCRIPTORS) Save
> +Specify TLS dialect.
> +
>   mfrecipe
>   Target Mask(ISA_FRECIPE) Var(la_isa_evolution)
>   Support frecipe.{s/d} and frsqrte.{s/d} instructions.


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

* Re: [PATCH v2] LoongArch: Add support for TLS descriptors
  2024-02-29  6:08 ` Xi Ruoyao
  2024-02-29  6:14   ` Xi Ruoyao
@ 2024-03-01  9:39   ` mengqinggang
  2024-03-07  8:08     ` chenglulu
  1 sibling, 1 reply; 6+ messages in thread
From: mengqinggang @ 2024-03-01  9:39 UTC (permalink / raw)
  To: Xi Ruoyao, gcc-patches
  Cc: xuchenghua, chenglulu, cailulu, i.swmail, maskray, luweining,
	wanglei, hejinyang

Thanks, I try to send a new version patch next week.


在 2024/2/29 下午2:08, Xi Ruoyao 写道:
> On Thu, 2024-02-29 at 09:42 +0800, mengqinggang wrote:
>> Generate la.tls.desc macro instruction for TLS descriptors model.
>>
>> la.tls.desc expand to
>>    pcalau12i $a0, %desc_pc_hi20(a)
>>    ld.d      $a1, $a0, %desc_ld_pc_lo12(a)
>>    addi.d    $a0, $a0, %desc_add_pc_lo12(a)
>>    jirl      $ra, $a1, %desc_call(a)
>>
>> The default is TLS descriptors, but can be configure with
>> -mtls-dialect={desc,trad}.
> Please keep trad as the default for now.  Glibc-2.40 will be released
> after GCC 14.1 but we don't want to end up in a situation where the
> default configuration of the latest GCC release creating something not
> working with latest Glibc release.
>
> And there's also musl libc we need to take into account.
>
> Or you can write some autoconf test for if the assembler supports
> tlsdesc and check TARGET_GLIBC_MAJOR & TARGET_GLIBC_MINOR for Glibc
> version to decide if enable desc by default.  If you want this but don't
> have time to implement you can leave trad the default and I'll take care
> of this.
>
> /* snip */
>
>> +(define_insn "@got_load_tls_desc<mode>"
>> +  [(set (match_operand:P 0 "register_operand" "=r")
>> +	(unspec:P
>> +	    [(match_operand:P 1 "symbolic_operand" "")]
>> +	    UNSPEC_TLS_DESC))
>> +    (clobber (reg:SI FCC0_REGNUM))
>> +    (clobber (reg:SI FCC1_REGNUM))
>> +    (clobber (reg:SI FCC2_REGNUM))
>> +    (clobber (reg:SI FCC3_REGNUM))
>> +    (clobber (reg:SI FCC4_REGNUM))
>> +    (clobber (reg:SI FCC5_REGNUM))
>> +    (clobber (reg:SI FCC6_REGNUM))
>> +    (clobber (reg:SI FCC7_REGNUM))
>> +    (clobber (reg:SI A1_REGNUM))
>> +    (clobber (reg:SI RETURN_ADDR_REGNUM))]
> Ok, the clobber list is correct.
>
>> +  "TARGET_TLS_DESC"
>> +  "la.tls.desc\t%0,%1"
> With -mexplicit-relocs=always we should emit %desc_pc_lo12 etc. instead
> of la.tls.desc.  As we don't want to add too many code we can just hard
> code the 4 instructions here instead of splitting this insn, just
> something like
>
> { return TARGET_EXPLICIT_RELOCS_ALWAS ? "......" : "la.tls.desc\t%0,%1"; }
>
>> +  [(set_attr "got" "load")
>> +   (set_attr "mode" "<MODE>")])
> We need (set_attr "length" "16") in this list as this actually expands
> into 16 bytes.
>
>


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

* Re: [PATCH v2] LoongArch: Add support for TLS descriptors
  2024-03-01  9:39   ` mengqinggang
@ 2024-03-07  8:08     ` chenglulu
  0 siblings, 0 replies; 6+ messages in thread
From: chenglulu @ 2024-03-07  8:08 UTC (permalink / raw)
  To: mengqinggang, Xi Ruoyao, gcc-patches
  Cc: xuchenghua, cailulu, i.swmail, maskray, luweining, wanglei, hejinyang

[-- Attachment #1: Type: text/plain, Size: 2674 bytes --]


在 2024/3/1 下午5:39, mengqinggang 写道:
> Thanks, I try to send a new version patch next week.
>
>
> 在 2024/2/29 下午2:08, Xi Ruoyao 写道:
>> On Thu, 2024-02-29 at 09:42 +0800, mengqinggang wrote:
>>> Generate la.tls.desc macro instruction for TLS descriptors model.
>>>
>>> la.tls.desc expand to
>>>    pcalau12i $a0, %desc_pc_hi20(a)
>>>    ld.d      $a1, $a0, %desc_ld_pc_lo12(a)
>>>    addi.d    $a0, $a0, %desc_add_pc_lo12(a)
>>>    jirl      $ra, $a1, %desc_call(a)
>>>
>>> The default is TLS descriptors, but can be configure with
>>> -mtls-dialect={desc,trad}.
>> Please keep trad as the default for now.  Glibc-2.40 will be released
>> after GCC 14.1 but we don't want to end up in a situation where the
>> default configuration of the latest GCC release creating something not
>> working with latest Glibc release.
>>
>> And there's also musl libc we need to take into account.
>>
>> Or you can write some autoconf test for if the assembler supports
>> tlsdesc and check TARGET_GLIBC_MAJOR & TARGET_GLIBC_MINOR for Glibc
>> version to decide if enable desc by default.  If you want this but don't
>> have time to implement you can leave trad the default and I'll take care
>> of this.

I think the implementation of the options also needs to be tweaked.

I've modified a version and the patch is attached.

And here you need to add test cases.

Thanks.

>>
>>
>> /* snip */
>>
>>> +(define_insn "@got_load_tls_desc<mode>"
>>> +  [(set (match_operand:P 0 "register_operand" "=r")
>>> +    (unspec:P
>>> +        [(match_operand:P 1 "symbolic_operand" "")]
>>> +        UNSPEC_TLS_DESC))
>>> +    (clobber (reg:SI FCC0_REGNUM))
>>> +    (clobber (reg:SI FCC1_REGNUM))
>>> +    (clobber (reg:SI FCC2_REGNUM))
>>> +    (clobber (reg:SI FCC3_REGNUM))
>>> +    (clobber (reg:SI FCC4_REGNUM))
>>> +    (clobber (reg:SI FCC5_REGNUM))
>>> +    (clobber (reg:SI FCC6_REGNUM))
>>> +    (clobber (reg:SI FCC7_REGNUM))
>>> +    (clobber (reg:SI A1_REGNUM))
>>> +    (clobber (reg:SI RETURN_ADDR_REGNUM))]
>> Ok, the clobber list is correct.
>>
>>> +  "TARGET_TLS_DESC"
>>> +  "la.tls.desc\t%0,%1"
>> With -mexplicit-relocs=always we should emit %desc_pc_lo12 etc. instead
>> of la.tls.desc.  As we don't want to add too many code we can just hard
>> code the 4 instructions here instead of splitting this insn, just
>> something like
>>
>> { return TARGET_EXPLICIT_RELOCS_ALWAS ? "......" : 
>> "la.tls.desc\t%0,%1"; }
>>
>>> +  [(set_attr "got" "load")
>>> +   (set_attr "mode" "<MODE>")])
>> We need (set_attr "length" "16") in this list as this actually expands
>> into 16 bytes.
>>
>>

[-- Attachment #2: LoongArch-tls-desc-modify.patch --]
[-- Type: text/x-patch, Size: 7670 bytes --]

diff --git a/gcc/config.gcc b/gcc/config.gcc
index 01ddc1a92f6..955e74d3bf9 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -2547,7 +2547,6 @@ loongarch*-*-linux*)
 	# Force .init_array support.  The configure script cannot always
 	# automatically detect that GAS supports it, yet we require it.
 	gcc_cv_initfini_array=yes
-	with_tls=${with_tls:-desc}
 	;;
 
 loongarch*-*-elf*)
@@ -5924,6 +5923,11 @@ case ${target} in
 		lasx)    tm_defines="$tm_defines DEFAULT_ISA_EXT_SIMD=ISA_EXT_SIMD_LASX" ;;
 		esac
 
+		case ${with_tls} in
+		"" | trad)	tm_defines="$tm_defines DEFAULT_TLS_TYPE=TLS_TRADITIONAL" ;;
+		desc)		tm_defines="$tm_defines DEFAULT_TLS_TYPE=TLS_DESCRIPTORS" ;;
+		esac
+
 		tmake_file="loongarch/t-loongarch $tmake_file"
 		;;
 
diff --git a/gcc/config/loongarch/genopts/loongarch.opt.in b/gcc/config/loongarch/genopts/loongarch.opt.in
index 2cc943ef683..7de107c3e3d 100644
--- a/gcc/config/loongarch/genopts/loongarch.opt.in
+++ b/gcc/config/loongarch/genopts/loongarch.opt.in
@@ -264,7 +264,7 @@ TargetVariable
 HOST_WIDE_INT la_isa_evolution = 0
 
 Enum
-Name(tls_type) Type(enum loongarch_tls_type)
+Name(tls_type) Type(int)
 The possible TLS dialects:
 
 EnumValue
@@ -274,5 +274,5 @@ EnumValue
 Enum(tls_type) String(desc) Value(TLS_DESCRIPTORS)
 
 mtls-dialect=
-Target RejectNegative Joined Enum(tls_type) Var(loongarch_tls_dialect) Init(TLS_DESCRIPTORS) Save
+Target RejectNegative Joined Enum(tls_type) Var(la_opt_tls_dialect) Init(M_OPT_UNSET) Save
 Specify TLS dialect.
diff --git a/gcc/config/loongarch/loongarch-def.h b/gcc/config/loongarch/loongarch-def.h
index 2dbf006d013..48d60e2b456 100644
--- a/gcc/config/loongarch/loongarch-def.h
+++ b/gcc/config/loongarch/loongarch-def.h
@@ -175,6 +175,7 @@ struct loongarch_target
   int cpu_arch;	    /* CPU_ */
   int cpu_tune;	    /* same */
   int cmodel;	    /* CMODEL_ */
+  int tls_dialect;  /* TLS_ */
 };
 
 /* CPU model */
@@ -188,6 +189,12 @@ enum {
   N_TUNE_TYPES	    = 5
 };
 
+/* TLS types.  */
+enum {
+  TLS_TRADITIONAL = 0,
+  TLS_DESCRIPTORS = 1
+};
+
 /* CPU model properties */
 extern loongarch_def_array<const char *, N_ARCH_TYPES>
   loongarch_cpu_strings;
diff --git a/gcc/config/loongarch/loongarch-driver.cc b/gcc/config/loongarch/loongarch-driver.cc
index 62658f531ad..8c4ed34698b 100644
--- a/gcc/config/loongarch/loongarch-driver.cc
+++ b/gcc/config/loongarch/loongarch-driver.cc
@@ -45,7 +45,7 @@ la_driver_init (int argc ATTRIBUTE_UNUSED, const char **argv ATTRIBUTE_UNUSED)
   /* Initialize all fields of la_target.  */
   loongarch_init_target (&la_target, M_OPT_UNSET, M_OPT_UNSET, M_OPT_UNSET,
 			 M_OPT_UNSET, M_OPT_UNSET, M_OPT_UNSET, M_OPT_UNSET,
-			 0, 0);
+			 M_OPT_UNSET, 0, 0);
   return "";
 }
 
diff --git a/gcc/config/loongarch/loongarch-opts.cc b/gcc/config/loongarch/loongarch-opts.cc
index 7eeac43ed2f..0ec55b78466 100644
--- a/gcc/config/loongarch/loongarch-opts.cc
+++ b/gcc/config/loongarch/loongarch-opts.cc
@@ -139,6 +139,7 @@ void
 loongarch_init_target (struct loongarch_target *target,
 		       int cpu_arch, int cpu_tune, int fpu, int simd,
 		       int abi_base, int abi_ext, int cmodel,
+		       int tls_dialect,
 		       HOST_WIDE_INT isa_evolution,
 		       HOST_WIDE_INT isa_evolution_set)
 {
@@ -153,6 +154,7 @@ loongarch_init_target (struct loongarch_target *target,
   target->abi.base = abi_base;
   target->abi.ext = abi_ext;
   target->cmodel = cmodel;
+  target->tls_dialect = tls_dialect;
 }
 
 
@@ -174,7 +176,8 @@ loongarch_config_target (struct loongarch_target *target,
   obstack_init (&msg_obstack);
 
   struct {
-    int arch, tune, fpu, simd, abi_base, abi_ext, cmodel, abi_flt;
+    int arch, tune, fpu, simd, abi_base, abi_ext, cmodel,
+	tls_dialect, abi_flt;
   } constrained = {
       M_OPT_ABSENT (target->cpu_arch)	  ? 0 : 1,
       M_OPT_ABSENT (target->cpu_tune)	  ? 0 : 1,
@@ -183,6 +186,7 @@ loongarch_config_target (struct loongarch_target *target,
       M_OPT_ABSENT (target->abi.base)	  ? 0 : 1,
       M_OPT_ABSENT (target->abi.ext)	  ? 0 : 1,
       M_OPT_ABSENT (target->cmodel)	  ? 0 : 1,
+      M_OPT_ABSENT (target->tls_dialect)  ? 0 : 1,
       M_OPT_ABSENT (target->abi.base)	  ? 0 : 1,
   };
 
@@ -551,6 +555,9 @@ fallback:
       gcc_unreachable ();
     }
 
+  t.tls_dialect = constrained.tls_dialect ? target->tls_dialect
+	  : DEFAULT_TLS_TYPE;
+
   /* Cleanup and return.  */
   obstack_free (&msg_obstack, NULL);
   *target = t;
diff --git a/gcc/config/loongarch/loongarch-opts.h b/gcc/config/loongarch/loongarch-opts.h
index a08ab6fac10..88e23fcee4b 100644
--- a/gcc/config/loongarch/loongarch-opts.h
+++ b/gcc/config/loongarch/loongarch-opts.h
@@ -35,6 +35,7 @@ void
 loongarch_init_target (struct loongarch_target *target,
 		       int cpu_arch, int cpu_tune, int fpu, int simd,
 		       int abi_base, int abi_ext, int cmodel,
+		       int tls_dialect,
 		       HOST_WIDE_INT isa_evolutions,
 		       HOST_WIDE_INT isa_evolutions_set);
 
@@ -83,6 +84,7 @@ struct loongarch_flags {
 #define TARGET_ABI_LP64		  (la_target.abi.base == ABI_BASE_LP64D	\
 				   || la_target.abi.base == ABI_BASE_LP64F \
 				   || la_target.abi.base == ABI_BASE_LP64S)
+#define TARGET_TLS_DESC		  (la_target.tls_dialect == TLS_DESCRIPTORS)
 
 #define ISA_HAS_LSX \
   (la_target.isa.simd == ISA_EXT_SIMD_LSX \
@@ -134,10 +136,4 @@ struct loongarch_flags {
 #define HAVE_AS_TLS_LE_RELAXATION 0
 #endif
 
-/* TLS types.  */
-enum loongarch_tls_type {
-  TLS_TRADITIONAL,
-  TLS_DESCRIPTORS
-};
-
 #endif /* LOONGARCH_OPTS_H */
diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc
index b4e43f1d037..a7b757cdb53 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -7674,7 +7674,8 @@ loongarch_option_override_internal (struct gcc_options *opts,
   loongarch_init_target (&la_target,
 			 la_opt_cpu_arch, la_opt_cpu_tune, la_opt_fpu,
 			 la_opt_simd, la_opt_abi_base, la_opt_abi_ext,
-			 la_opt_cmodel, opts->x_la_isa_evolution,
+			 la_opt_cmodel, la_opt_tls_dialect,
+			 opts->x_la_isa_evolution,
 			 opts_set->x_la_isa_evolution);
 
   /* Handle target-specific options: compute defaults/conflicts etc.  */
diff --git a/gcc/config/loongarch/loongarch.h b/gcc/config/loongarch/loongarch.h
index 5a7220ae504..bf2351f0968 100644
--- a/gcc/config/loongarch/loongarch.h
+++ b/gcc/config/loongarch/loongarch.h
@@ -25,14 +25,6 @@ along with GCC; see the file COPYING3.  If not see
 
 #define TARGET_SUPPORTS_WIDE_INT 1
 
-/* Support for configure-time default option. The rules are:
-   --with-tls is ignored if -mtls-dialect is specified.  */
-#define OPTION_DEFAULT_SPECS \
-  {"tls", "%{!mtls-dialect=*:-mtls-dialect=%(VALUE)}"}, \
-
-/* Check TLS Descriptors mechanism is selected.  */
-#define TARGET_TLS_DESC (loongarch_tls_dialect == TLS_DESCRIPTORS)
-
 /* Macros to silence warnings about numbers being signed in traditional
    C and unsigned in ISO C when compiled on 32-bit hosts.  */
 
diff --git a/gcc/config/loongarch/loongarch.opt b/gcc/config/loongarch/loongarch.opt
index 04846ae86c0..1c54ab6ae43 100644
--- a/gcc/config/loongarch/loongarch.opt
+++ b/gcc/config/loongarch/loongarch.opt
@@ -272,7 +272,7 @@ TargetVariable
 HOST_WIDE_INT la_isa_evolution = 0
 
 Enum
-Name(tls_type) Type(enum loongarch_tls_type)
+Name(tls_type) Type(int)
 The possible TLS dialects:
 
 EnumValue
@@ -282,7 +282,7 @@ EnumValue
 Enum(tls_type) String(desc) Value(TLS_DESCRIPTORS)
 
 mtls-dialect=
-Target RejectNegative Joined Enum(tls_type) Var(loongarch_tls_dialect) Init(TLS_DESCRIPTORS) Save
+Target RejectNegative Joined Enum(tls_type) Var(la_opt_tls_dialect) Init(M_OPT_UNSET) Save
 Specify TLS dialect.
 
 mfrecipe

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

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-29  1:42 [PATCH v2] LoongArch: Add support for TLS descriptors mengqinggang
2024-02-29  6:08 ` Xi Ruoyao
2024-02-29  6:14   ` Xi Ruoyao
2024-03-01  9:39   ` mengqinggang
2024-03-07  8:08     ` chenglulu
2024-02-29  6:51 ` Jinyang He

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