From patchwork Thu Oct 19 14:02:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 155583 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2010:b0:403:3b70:6f57 with SMTP id fe16csp405893vqb; Thu, 19 Oct 2023 07:04:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IExblNC+gOfhcbrzaxYhd+0lcd6bajgxcB9I5LWhpdoet0PYpFeH/6qBNLsjNJeqMfDl4kj X-Received: by 2002:ac8:5947:0:b0:413:5dbd:a926 with SMTP id 7-20020ac85947000000b004135dbda926mr2607801qtz.2.1697724278193; Thu, 19 Oct 2023 07:04:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697724278; cv=pass; d=google.com; s=arc-20160816; b=aFskXK5LxWQN6bBBmKPq5E4jVUI4U1JuJ/1zkD3QpDQq4zTi3aX6mPx3xtCTI2tNjN WvVrr2MYVpxgM5Asxg3Eb1YEnq+hXtmBaWAJLth8cRO9thzI3E/CUzHgHbZPlfj7IQOP vO7CZ79oTwl/QwXbFZ0sFWFGK1lIhT9/hDFTyWy+BHiHIFfxjMA1k6k1h29N+Y+mJKrQ KxRn8XPL6Xh8sCyESFipUZvvivOsfKbAUTW8LHC+y6c64wJnbqjqKOdchl+W+trlxVPP JLZDhsSSk+ictkBKHJer049CLVxQwsT3/F/stcPJwDUcIHJOWcon4JJ6rXIJeMTRFYuJ rvFA== 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:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=1oG+C+1Ei/AKxcZLkANxNmQECAOkY8a9sds9EUMncag=; fh=Q6zU0KPK9mkW4WcCQLO/dI9yShCKJrND2oM6Xx6eTlQ=; b=U7qAQbViZaLB8smyD0BcGxJhrTuU9wBt4ZYFkd8ATMU4XUMcw0j0Hjldua057o5Q1f JsvAQ/hNrgekLO2zznTi3VXxYDUMWLR9AVZE+AEG6fetDMRJTtMWMfELG0w3lDeyveqQ q47ou18OV9HBBk29O24U48f0rtAJB4DJ5zbQYaIWzOJsusgk1M2OCkZpZ9Q/uIxwxLAU msNtF3UsqkCVdLLfDQj3U15Lquh7wIYuKizBIOlua1KjJXVYK+iEJg/OQsImAmUn7OI2 PLbLSeYtlguHqlrFrMGFV9221FOJ2v0uyZdx5Eetxh++Dsv9lGDKim2HD9KvELPuRG3S hkvA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@xry111.site header.s=default header.b="dga101//"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=xry111.site Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id s4-20020a05622a018400b00414b35ac56csi1666008qtw.689.2023.10.19.07.04.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 07:04:38 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@xry111.site header.s=default header.b="dga101//"; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=xry111.site Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 581FF386D63E for ; Thu, 19 Oct 2023 14:04:06 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id DB5D83858D33 for ; Thu, 19 Oct 2023 14:03:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DB5D83858D33 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=xry111.site Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=xry111.site ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DB5D83858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=89.208.246.23 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697724220; cv=none; b=UmUvp/+vcDAdjBYwndKEZqxnZjrK7ZSZt605JUF4SjIfqTLi+iHOxl/QSLwVepWkzqHpzL67McWN2/roa49X1laoM3Ay4msJYXKThFGZJRrWAuCTZ4kNYW9RiThbrOjjT72ZaeoOuJuDY4ngIjFOmoR9gQ3u3Lya7m+6qQSIJDo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697724220; c=relaxed/simple; bh=eO0PkOd9MkjQaMMvY9C4/0i2dRthLBJZiJZ8KG2kKtI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=wKvcYfpwmHAjwTtci+Onb8Ef1LxsJBzNtKKSFqMhFq+Q1AKPtbCV80PXj/cK+TjhDuvEheaK2JjicNFVEQIDuoeZaV4YWqZAKQVW7ua7W4jPVXt+5TPnoUnXl6g8emW71IO9ViVhyXdygIcuupqjYLBiKwYofendJ45K2hpj0uw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xry111.site; s=default; t=1697724217; bh=eO0PkOd9MkjQaMMvY9C4/0i2dRthLBJZiJZ8KG2kKtI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dga101//HsbuStiO9zWlJmizbdAmdbRiYjQDc/ZwmMyERjaD9RkhmKJHz/avLb25z sp3xnJ4PGf/uzqxqk6FQD34SOsTBm+sE0PcObDs/iCDEfRmy3jkS3oEHt7xKN8FN70 ym0KphlIqUSYqi/eL5c5CH0/sOCN+HQO4o1GCF2E= Received: from stargazer.. (unknown [IPv6:240e:358:1126:f000:dc73:854d:832e:2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id B278E66AF9; Thu, 19 Oct 2023 10:03:32 -0400 (EDT) From: Xi Ruoyao To: gcc-patches@gcc.gnu.org Cc: chenglulu , i@xen0n.name, xuchenghua@loongson.cn, mengqinggang , Xi Ruoyao Subject: [PATCH 3/5] LoongArch: Use explicit relocs for TLS access with -mexplicit-relocs=auto Date: Thu, 19 Oct 2023 22:02:58 +0800 Message-ID: <20231019140300.50323-4-xry111@xry111.site> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231019140300.50323-1-xry111@xry111.site> References: <20231019140300.50323-1-xry111@xry111.site> MIME-Version: 1.0 X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, KAM_STOCKGEN, LIKELY_SPAM_FROM, SPF_HELO_PASS, SPF_PASS, TXREP 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: 1780192932479510229 X-GMAIL-MSGID: 1780192932479510229 The linker does not know how to relax TLS access for LoongArch, so let's emit machine instructions with explicit relocs for TLS. gcc/ChangeLog: * config/loongarch/loongarch.cc (loongarch_explicit_relocs_p): Return true for TLS symbol types if -mexplicit-relocs=auto. (loongarch_call_tls_get_addr): Replace TARGET_EXPLICIT_RELOCS with la_opt_explicit_relocs != EXPLICIT_RELOCS_NONE. (loongarch_legitimize_tls_address): Likewise. * config/loongarch/loongarch.md (@tls_low): Remove TARGET_EXPLICIT_RELOCS from insn condition. gcc/testsuite/ChangeLog: * gcc.target/loongarch/explicit-relocs-auto-tls-ld-gd.c: New test. * gcc.target/loongarch/explicit-relocs-auto-tls-le-ie.c: New test. --- gcc/config/loongarch/loongarch.cc | 37 ++++++++++++------- gcc/config/loongarch/loongarch.md | 2 +- .../explicit-relocs-auto-tls-ld-gd.c | 9 +++++ .../explicit-relocs-auto-tls-le-ie.c | 6 +++ 4 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/gcc.target/loongarch/explicit-relocs-auto-tls-ld-gd.c create mode 100644 gcc/testsuite/gcc.target/loongarch/explicit-relocs-auto-tls-le-ie.c diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc index c12d77ea144..c782f571abc 100644 --- a/gcc/config/loongarch/loongarch.cc +++ b/gcc/config/loongarch/loongarch.cc @@ -1936,16 +1936,27 @@ loongarch_explicit_relocs_p (enum loongarch_symbol_type type) if (la_opt_explicit_relocs != EXPLICIT_RELOCS_AUTO) return la_opt_explicit_relocs == EXPLICIT_RELOCS_ALWAYS; - /* If we are performing LTO for a final link, and we have the linker - plugin so we know the resolution of the symbols, then all GOT - references are binding to external symbols or preemptable symbols. - So the linker cannot relax them. */ - return (in_lto_p - && !flag_incremental_link - && HAVE_LTO_PLUGIN == 2 - && (!global_options_set.x_flag_use_linker_plugin - || global_options.x_flag_use_linker_plugin) - && type == SYMBOL_GOT_DISP); + switch (type) + { + case SYMBOL_TLS_IE: + case SYMBOL_TLS_LE: + case SYMBOL_TLSGD: + case SYMBOL_TLSLDM: + /* The linker don't know how to relax TLS accesses. */ + return true; + case SYMBOL_GOT_DISP: + /* If we are performing LTO for a final link, and we have the + linker plugin so we know the resolution of the symbols, then + all GOT references are binding to external symbols or + preemptable symbols. So the linker cannot relax them. */ + return (in_lto_p + && !flag_incremental_link + && HAVE_LTO_PLUGIN == 2 + && (!global_options_set.x_flag_use_linker_plugin + || global_options.x_flag_use_linker_plugin)); + default: + return false; + } } /* Returns the number of instructions necessary to reference a symbol. */ @@ -2753,7 +2764,7 @@ loongarch_call_tls_get_addr (rtx sym, enum loongarch_symbol_type type, rtx v0) start_sequence (); - if (TARGET_EXPLICIT_RELOCS) + if (la_opt_explicit_relocs != EXPLICIT_RELOCS_NONE) { /* Split tls symbol to high and low. */ rtx high = gen_rtx_HIGH (Pmode, copy_rtx (loc)); @@ -2918,7 +2929,7 @@ loongarch_legitimize_tls_address (rtx loc) tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM); tmp1 = gen_reg_rtx (Pmode); dest = gen_reg_rtx (Pmode); - if (TARGET_EXPLICIT_RELOCS) + if (la_opt_explicit_relocs != EXPLICIT_RELOCS_NONE) { tmp2 = loongarch_unspec_address (loc, SYMBOL_TLS_IE); tmp3 = gen_reg_rtx (Pmode); @@ -2955,7 +2966,7 @@ loongarch_legitimize_tls_address (rtx loc) tmp1 = gen_reg_rtx (Pmode); dest = gen_reg_rtx (Pmode); - if (TARGET_EXPLICIT_RELOCS) + if (la_opt_explicit_relocs != EXPLICIT_RELOCS_NONE) { tmp2 = loongarch_unspec_address (loc, SYMBOL_TLS_LE); tmp3 = gen_reg_rtx (Pmode); diff --git a/gcc/config/loongarch/loongarch.md b/gcc/config/loongarch/loongarch.md index bec73f1bc91..695c8eb9a6f 100644 --- a/gcc/config/loongarch/loongarch.md +++ b/gcc/config/loongarch/loongarch.md @@ -2257,7 +2257,7 @@ (define_insn "@tls_low" (unspec:P [(mem:P (lo_sum:P (match_operand:P 1 "register_operand" "r") (match_operand:P 2 "symbolic_operand" "")))] UNSPEC_TLS_LOW))] - "TARGET_EXPLICIT_RELOCS" + "" "addi.\t%0,%1,%L2" [(set_attr "type" "arith") (set_attr "mode" "")]) diff --git a/gcc/testsuite/gcc.target/loongarch/explicit-relocs-auto-tls-ld-gd.c b/gcc/testsuite/gcc.target/loongarch/explicit-relocs-auto-tls-ld-gd.c new file mode 100644 index 00000000000..957ff98df62 --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/explicit-relocs-auto-tls-ld-gd.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fPIC -mexplicit-relocs=auto" } */ + +__thread int a __attribute__((visibility("hidden"))); +extern __thread int b __attribute__((visibility("default"))); + +int test() { return a + b; } + +/* { dg-final { scan-assembler-not "la.tls" { target tls_native } } } */ diff --git a/gcc/testsuite/gcc.target/loongarch/explicit-relocs-auto-tls-le-ie.c b/gcc/testsuite/gcc.target/loongarch/explicit-relocs-auto-tls-le-ie.c new file mode 100644 index 00000000000..78898cfc6ab --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/explicit-relocs-auto-tls-le-ie.c @@ -0,0 +1,6 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mexplicit-relocs=auto" } */ + +#include "explicit-relocs-auto-tls-ld-gd.c" + +/* { dg-final { scan-assembler-not "la.tls" { target tls_native } } } */