From patchwork Thu Feb 29 01:42:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: mengqinggang X-Patchwork-Id: 208159 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp119052dyb; Wed, 28 Feb 2024 17:44:01 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW6Arb6tEu58g4xKIlKPd5wbv3VA1GMfnGZ3Q8TF+FqCd9Zdmh2VQ7Rj6FUuthImyG50iEJYvsnPcWGzYDFi7uJrHrYfA== X-Google-Smtp-Source: AGHT+IFO9lFcbV7bVdzLewoPGJ8z7DVboVEBmh7PCm16gfcJJmj8/AnQa5dqDDb3bBrXqb12LLsL X-Received: by 2002:a0c:f8ca:0:b0:690:30fc:1a99 with SMTP id h10-20020a0cf8ca000000b0069030fc1a99mr845395qvo.5.1709171040838; Wed, 28 Feb 2024 17:44:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709171040; cv=pass; d=google.com; s=arc-20160816; b=r03aEsz+05k47760BApXwSdPNm4NFO/qmxIlmI7fLLPJXx0xF48hapBKeLF7TfLqKx epx58lobWXNADCAlKaxRrdS8rHYNUaJwSMA3kA0/ILll5ZLVKMu6MktIpA5T5NN+UkI2 h/EgRDea40nnPN2J/HCEo2+7jT3ZrKUIhhGnicG3q0VadCyufr1sRDgiQaPTnyLHq3z6 cT3JK7y2wrb2r/u/VNpdkc96JfCBp3ygLhONeVcX6/Gm0XRaI+I8WNvch5SyIC7+2yZg cKEIZLsT6KePEHfCoEmAjqpa2AQgEG8jd00U9/KdW6Jr8zPCYYs66sIFTCBzL0L6IqmI Js9g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:arc-filter :dmarc-filter:delivered-to; bh=BwwKQBHiB8eu/XRDTL0tYJ5BBVioIgrn1y3TE/HAZcM=; fh=gO5v8bfGm+Qjb/3ONqpkwE6dFAru39W7XMU1skTkIws=; b=Q3iOdhKYYUTT+f5V1vtTtff6ttGFkhiJAgDCID3RFw/godmn5K8miiSAg6g/alsbaZ YdXuDdIvoBkjSqEAltvCPIEosYpi3h0jnZ7WzxHRkjKN7ZCGhAstpwzOtY3FY/e6mhqD axlt+GP9+fmcjigelJdIIPrLNyltrhy0Q6p+iAArRjl9c9fnh6kFTapl34Os+rj6cbxF WG7YsNgEjdRwCrrhE9In1qjHRxwQnOhOsVMzS6nEII2mVcKEMYr3txIfgbKlvZgvPJM5 IsmNcKbnuz8tVqa9WxalfN8Ofy8LvFXqhtu/uWhY6G7oQGQHCuwYts+12XzfCyFQub89 CK8A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id c4-20020a056214224400b0068f99fe364dsi409800qvc.483.2024.02.28.17.44.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 17:44:00 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 836BF3858415 for ; Thu, 29 Feb 2024 01:44:00 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id 6C9A73858CD1 for ; Thu, 29 Feb 2024 01:43:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6C9A73858CD1 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 6C9A73858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709170993; cv=none; b=tDkVkGjoMtcbP0GA53dgrEbpk8eI+Fm+ZSQYN3z0tK+lrzqCD70vOaLPz0u6iCAV2TSoo1iglL3ENnkayoCZhEBi/tDVda+kWDHFNHrL7svYJ+z6B4x7BY9JXl2jcIqvYvcJlYGm7By/aNDjThc7nx8Z0SrrIRnTE97cp1Vh9mU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709170993; c=relaxed/simple; bh=lOVozjCkU7RUbD3JSrFRBTzcx6QJZhItlL96hlza0wQ=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=a3/OPd44n05seyxqU1LzsY9RYPTy3IqNf0WM2sj2Yqs7aSjDLMnQqgfzGN84n3K2S+G23IXDCPe53R/5ta3CVhbvUjMjCyCZXAdxY0r05uGG0tO8XeY5AEgbiaRSr0M2z5zIKdY4ypoKqQ9J7HqKeJXRA8DHAz2dCM20TpA141U= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from loongson.cn (unknown [10.2.6.5]) by gateway (Coremail) with SMTP id _____8Bxnusm4d9lSKkSAA--.46845S3; Thu, 29 Feb 2024 09:43:03 +0800 (CST) Received: from 5.5.5 (unknown [10.2.6.5]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxnhMi4d9lhEVKAA--.11681S2; Thu, 29 Feb 2024 09:42:58 +0800 (CST) From: mengqinggang To: gcc-patches@gcc.gnu.org Cc: xuchenghua@loongson.cn, chenglulu@loongson.cn, cailulu@loongson.cn, xry111@xry111.site, i.swmail@xen0n.name, maskray@google.com, luweining@loongson.cn, wanglei@loongson.cn, hejinyang@loongson.cn, mengqinggang@loongson.cn Subject: [PATCH v2] LoongArch: Add support for TLS descriptors Date: Thu, 29 Feb 2024 09:42:57 +0800 Message-Id: <20240229014257.3553274-1-mengqinggang@loongson.cn> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-CM-TRANSID: AQAAf8BxnhMi4d9lhEVKAA--.11681S2 X-CM-SenderInfo: 5phqw15lqjwttqj6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoW3Aw4kWF18KF48uFyDXF4kZrc_yoWDWF4xpr y7Zw13Jr48GrsxWr9rJa4rXwsxGFZIgr42qay3trWkCa17XryUZF1xKr9xXFyjqws8XrWa vr1Fka12qF48AacCm3ZEXasCq-sJn29KB7ZKAUJUUUU5529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUkYb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4j6r4UJwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYIkI8VC2zVCFFI0UMc 02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUXVWUAwAv7VC2z280aVAF wI0_Jr0_Gr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JMxAIw28IcxkI7V AKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCj r7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6x IIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAI w20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x 0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU1QVy3UUUUU== X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_STOCKGEN, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792195733587967606 X-GMAIL-MSGID: 1792195733587967606 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): 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" ;; Thread-Local Storage +(define_insn "@got_load_tls_desc" + [(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" "")]) + (define_insn "@load_tls" [(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.