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: 1851783 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=xry111.site header.i=@xry111.site header.a=rsa-sha256 header.s=default header.b=dga101//; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SB8b037Ppz23jP for ; Fri, 20 Oct 2023 01:03:56 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 630E33856943 for ; Thu, 19 Oct 2023 14:03:53 +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+incoming=patchwork.ozlabs.org@gcc.gnu.org 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 } } } */