From patchwork Fri Sep 8 10:49:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tatsuyuki Ishi X-Patchwork-Id: 137715 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ab0a:0:b0:3f2:4152:657d with SMTP id m10csp455606vqo; Fri, 8 Sep 2023 03:50:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG46GF2WeS7UxCFghYEcKyANO7LINlVXKoRiIiB54hCwSpFZdt+wAicw0hi4M76RImaOTKl X-Received: by 2002:a05:651c:215:b0:2b9:eaa7:c23f with SMTP id y21-20020a05651c021500b002b9eaa7c23fmr1446788ljn.49.1694170217240; Fri, 08 Sep 2023 03:50:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694170217; cv=none; d=google.com; s=arc-20160816; b=e+5e1Qdjlml3nqi6XvtOXPwAfWsVgKHq3erDY5Q/3ddkjWNUOwEE3V0FFhyB9W3CgK IjN1j+HqVY7DIPdt4Pvm/nWSzNUX/NXDC8JMR/GiNJ1hM2cN5ds2r+i0rKguGcdbmtFe 1YAjPLKdt3DsYiKqfEDW4mxclxKwqIp9UdTaut7uzrNtls5O2/rj8XVBXi0eh2JbCtMP F0dS4xoRlX7OhNrlvq2+BsmPY0eN4KX6EU1YFug1JXA0wTb6b64iV12TcXSWWH7n3CTg H/J+gMtdsl1Yzc6/LbWhEjpv6NgGHKiZvmh5dotIDytWDOnKOWzaGneMYTikDw79aPs2 C++Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from: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:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=T0w7/r+AThdzbCSTtZLjmI2WkkDXfu4ZlklztarEJJg=; fh=rXj1zY6wpSpZbabboY/7ZV6qL66KGKoCX1Dw9c0aWOE=; b=MIRUUh3zLa+pUAjpqu1ovvF1xhUt5f1bmsA5f8A8Wblbew2Bgm/5yyG4SoCKz/SUyw 9hD3bmcDGilGCTOey7pk6y+iZX25bz6dCAKeeq8cNd3AThELtla6fsXM4meSBWxmPtfV 2l+g5AlNH1k4wzAYKcNcGlAdL+um42N2wymwA8PRNEtJ1Z7Jw9WdV+b5PIIE+NbRowjR VwByPf8bCzjy4jB78Jqxrm1OElHz3dPmse76QPl6lc5aDh6hc/e3Ih6SB4V6qDke3++7 P8yOEnywkaiQK15ZZ+qYhln4C9bSWaz1spoP92QzlASwOGa7dpoCdGUZt916/tJTg77g 3+pA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=PUOkXBT5; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id qx26-20020a170906fcda00b0099cad5caa77si1062807ejb.22.2023.09.08.03.50.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Sep 2023 03:50:17 -0700 (PDT) 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; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=PUOkXBT5; 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"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0D46B3858404 for ; Fri, 8 Sep 2023 10:50:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0D46B3858404 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1694170216; bh=T0w7/r+AThdzbCSTtZLjmI2WkkDXfu4ZlklztarEJJg=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=PUOkXBT5pGAuGiO+6qn9bwRj928ywzvPkwPPOToOOb667KYatEdzI1bn05/Hg4hVy 2tmot/igsBU3pD+DYojr/+gPnOdIXwf1y2Rdb1eHpWWWaOoLVotAFMNy/a0H/Z1HB6 EeG2gbVxAwygPuLVxO+py5IngaB666bRHZpBf7i0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x62d.google.com (mail-pl1-x62d.google.com [IPv6:2607:f8b0:4864:20::62d]) by sourceware.org (Postfix) with ESMTPS id 0A8163857B93 for ; Fri, 8 Sep 2023 10:49:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0A8163857B93 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1befe39630bso3815365ad.0 for ; Fri, 08 Sep 2023 03:49:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694170169; x=1694774969; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=T0w7/r+AThdzbCSTtZLjmI2WkkDXfu4ZlklztarEJJg=; b=mn8dH5kka+sAIY9Z1902NNCe/AYHUn2MOQPGghp3613QhCgP/S3UGI6Be+H0QQM4SH IDIdWy16aprA9WPMP8rhZkBOUapPkavzhhbUbM3rHW/vZmpsi830GoutiUkFpNg5/hDw EFNw0oTeDAgMXIJo2+p9pbNmVE86lV/R6xYxdFC5UaH86SoD83sF6UoWCfz/HvtNtUuE afbpVS/4WsSC299alUUwzZ6LTYjbctqq62a9HkG8a9hcy7kanANCKZmyuUb2MURdcbgp 6vZk6CNPrYVhVBUxf9lwlVRs+5mx0Bz1DFWfxbdM1M/Ax6cl3njQbBlAwf8HL8h22sUQ Z6xg== X-Gm-Message-State: AOJu0YwO1Sl+UZMrwUeDIMTvvbi8dHM6INYyYiGJrz0GFL7bhqA5n7Ag +VnfdS5LXzIGjmSq3YjQ62pXkINS+YiHNG8n X-Received: by 2002:a17:903:41c2:b0:1c0:ecbb:182e with SMTP id u2-20020a17090341c200b001c0ecbb182emr2305422ple.2.1694170168600; Fri, 08 Sep 2023 03:49:28 -0700 (PDT) Received: from localhost (zz20184013906F627101.userreverse.dion.ne.jp. [111.98.113.1]) by smtp.gmail.com with ESMTPSA id f2-20020a170902ff0200b001b83dc8649dsm1293167plj.250.2023.09.08.03.49.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Sep 2023 03:49:27 -0700 (PDT) To: ishitatsuyuki@gmail.com Cc: gcc-patches@gcc.gnu.org, rui314@gmail.com, ruiu@bluewhale.systems, kito.cheng@gmail.com Subject: [PATCH v2] RISC-V: Implement TLS Descriptors. Date: Fri, 8 Sep 2023 19:49:23 +0900 Message-ID: <20230908104923.31154-1-ishitatsuyuki@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20230817181308.122802-2-ishitatsuyuki@gmail.com> References: <20230817181308.122802-2-ishitatsuyuki@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, 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: , X-Patchwork-Original-From: Tatsuyuki Ishi via Gcc-patches From: Tatsuyuki Ishi Reply-To: Tatsuyuki Ishi Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774501063388314626 X-GMAIL-MSGID: 1776466229653016399 This implements TLS Descriptors (TLSDESC) as specified in [1]. In TLSDESC instruction sequence, the first instruction relocates against the target TLS variable, while subsequent instructions relocates against the address of the first. Such usage of labels are not well-supported within GCC. Due to this, the 4-instruction sequence is implemented as a single RTX insn. The default remains to be the traditional TLS model, but can be configured with --with_tls={trad,desc}. The choice can be revisited once toolchain and libc support ships. [1]: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/373. gcc/Changelog: * config/riscv/riscv.opt: Add -mtls-dialect to configure TLS flavor. * config.gcc: Add --with_tls configuration option to change the default TLS flavor. * config/riscv/riscv.h: Add TARGET_TLSDESC determined from -mtls-dialect and with_tls defaults. * config/riscv/riscv-opts.h: Define enum riscv_tls_type for the two TLS flavors. * config/riscv/riscv-protos.h: Define SYMBOL_TLSDESC symbol type. * config/riscv/riscv.md: Add instruction sequence for TLSDESC. * config/riscv/riscv.cc (riscv_symbol_insns): Add instruction sequence length data for TLSDESC. (riscv_legitimize_tls_address): Add lowering of TLSDESC. --- No regression in gcc tests for rv64gc, tested alongside the binutils and glibc implementation. Tested with --with_tls=desc. v2: Add with_tls configuration option, and a few readability improvements. Added Changelog. Thanks Kito Cheng for the review. This contribution is made on behalf of Blue Whale Systems, which has copyright assignment on file with the FSF. gcc/config.gcc | 15 ++++++++++++++- gcc/config/riscv/riscv-opts.h | 6 ++++++ gcc/config/riscv/riscv-protos.h | 5 +++-- gcc/config/riscv/riscv.cc | 24 ++++++++++++++++++++---- gcc/config/riscv/riscv.h | 9 +++++++-- gcc/config/riscv/riscv.md | 21 ++++++++++++++++++++- gcc/config/riscv/riscv.opt | 14 ++++++++++++++ 7 files changed, 84 insertions(+), 10 deletions(-) diff --git a/gcc/config.gcc b/gcc/config.gcc index 415e0e1ebc5..86df7b89016 100644 --- a/gcc/config.gcc +++ b/gcc/config.gcc @@ -2434,6 +2434,7 @@ riscv*-*-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:-trad} ;; riscv*-*-elf* | riscv*-*-rtems*) tm_file="elfos.h newlib-stdint.h ${tm_file} riscv/elf.h" @@ -2476,6 +2477,7 @@ riscv*-*-freebsd*) # 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:-trad} ;; loongarch*-*-linux*) @@ -4566,7 +4568,7 @@ case "${target}" in ;; riscv*-*-*) - supported_defaults="abi arch tune riscv_attribute isa_spec" + supported_defaults="abi arch tune riscv_attribute isa_spec tls" case "${target}" in riscv-* | riscv32*) xlen=32 ;; @@ -4694,6 +4696,17 @@ case "${target}" in ;; esac 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 # Handle --with-multilib-list. if test "x${with_multilib_list}" != xdefault; then diff --git a/gcc/config/riscv/riscv-opts.h b/gcc/config/riscv/riscv-opts.h index 378a17699cd..db03f35430a 100644 --- a/gcc/config/riscv/riscv-opts.h +++ b/gcc/config/riscv/riscv-opts.h @@ -319,4 +319,10 @@ enum riscv_entity #define TARGET_VECTOR_VLS \ (TARGET_VECTOR && riscv_autovec_preference == RVV_SCALABLE) +/* TLS types. */ +enum riscv_tls_type { + TLS_TRADITIONAL, + TLS_DESCRIPTORS +}; + #endif /* ! GCC_RISCV_OPTS_H */ diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h index 472c00dc439..9b7471f7591 100644 --- a/gcc/config/riscv/riscv-protos.h +++ b/gcc/config/riscv/riscv-protos.h @@ -33,9 +33,10 @@ enum riscv_symbol_type { SYMBOL_TLS, SYMBOL_TLS_LE, SYMBOL_TLS_IE, - SYMBOL_TLS_GD + SYMBOL_TLS_GD, + SYMBOL_TLSDESC, }; -#define NUM_SYMBOL_TYPES (SYMBOL_TLS_GD + 1) +#define NUM_SYMBOL_TYPES (SYMBOL_TLSDESC + 1) /* Classifies an address. diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 49062bef9fc..c158e224aaa 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -799,6 +799,7 @@ static int riscv_symbol_insns (enum riscv_symbol_type type) case SYMBOL_ABSOLUTE: return 2; /* LUI + the reference. */ case SYMBOL_PCREL: return 2; /* AUIPC + the reference. */ case SYMBOL_TLS_LE: return 3; /* LUI + ADD TP + the reference. */ + case SYMBOL_TLSDESC: return 6; /* 4-instruction call + ADD TP + the reference. */ case SYMBOL_GOT_DISP: return 3; /* AUIPC + LD GOT + the reference. */ default: gcc_unreachable (); } @@ -1734,7 +1735,7 @@ riscv_call_tls_get_addr (rtx sym, rtx result) static rtx riscv_legitimize_tls_address (rtx loc) { - rtx dest, tp, tmp; + rtx dest, tp, tmp, a0; enum tls_model model = SYMBOL_REF_TLS_MODEL (loc); #if 0 @@ -1750,9 +1751,24 @@ riscv_legitimize_tls_address (rtx loc) /* Rely on section anchors for the optimization that LDM TLS provides. The anchor's address is loaded with GD TLS. */ case TLS_MODEL_GLOBAL_DYNAMIC: - tmp = gen_rtx_REG (Pmode, GP_RETURN); - dest = gen_reg_rtx (Pmode); - emit_libcall_block (riscv_call_tls_get_addr (loc, tmp), dest, tmp, loc); + if (TARGET_TLSDESC) + { + static unsigned seqno; + tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM); + a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST); + dest = gen_reg_rtx (Pmode); + + emit_insn (gen_tlsdesc (Pmode, loc, GEN_INT (seqno))); + emit_insn (gen_add3_insn (dest, a0, tp)); + seqno++; + } + else + { + tmp = gen_rtx_REG (Pmode, GP_RETURN); + dest = gen_reg_rtx (Pmode); + emit_libcall_block (riscv_call_tls_get_addr (loc, tmp), dest, tmp, + loc); + } break; case TLS_MODEL_INITIAL_EXEC: diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h index e18a0081297..faea78f5f4c 100644 --- a/gcc/config/riscv/riscv.h +++ b/gcc/config/riscv/riscv.h @@ -59,6 +59,7 @@ extern const char *riscv_multi_lib_check (int argc, const char **argv); --with-abi is ignored if -mabi is specified. --with-tune is ignored if -mtune or -mcpu is specified. --with-isa-spec is ignored if -misa-spec is specified. + --with-tls is ignored if -mtls-dialect is specified. But using default -march/-mtune value if -mcpu don't have valid option. */ #define OPTION_DEFAULT_SPECS \ @@ -68,8 +69,9 @@ extern const char *riscv_multi_lib_check (int argc, const char **argv); {"arch", "%{!march=*:" \ " %{!mcpu=*:-march=%(VALUE)}" \ " %{mcpu=*:%:riscv_expand_arch_from_cpu(%* %(VALUE))}}" }, \ - {"abi", "%{!mabi=*:-mabi=%(VALUE)}" }, \ - {"isa_spec", "%{!misa-spec=*:-misa-spec=%(VALUE)}" }, \ + {"abi", "%{!mabi=*:-mabi=%(VALUE)}" }, \ + {"isa_spec", "%{!misa-spec=*:-misa-spec=%(VALUE)}" }, \ + {"tls", "%{!mtls-dialect=*:-mtls-dialect=%(VALUE)}"}, \ #ifdef IN_LIBGCC2 #undef TARGET_64BIT @@ -1122,4 +1124,7 @@ extern void riscv_remove_unneeded_save_restore_calls (void); #define OPTIMIZE_MODE_SWITCHING(ENTITY) (TARGET_VECTOR) #define NUM_MODES_FOR_MODE_SWITCHING {VXRM_MODE_NONE, riscv_vector::FRM_NONE} +/* Check TLS Descriptors mechanism is selected. */ +#define TARGET_TLSDESC (riscv_tls_dialect == TLS_DESCRIPTORS) + #endif /* ! GCC_RISCV_H */ diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md index b456fa6abb3..aaef9cd429b 100644 --- a/gcc/config/riscv/riscv.md +++ b/gcc/config/riscv/riscv.md @@ -47,7 +47,7 @@ UNSPEC_TLS_LE UNSPEC_TLS_IE UNSPEC_TLS_GD - + UNSPEC_TLSDESC ;; High part of PC-relative address. UNSPEC_AUIPC @@ -121,6 +121,7 @@ (T1_REGNUM 6) (S0_REGNUM 8) (S1_REGNUM 9) + (A0_REGNUM 10) (S2_REGNUM 18) (S3_REGNUM 19) (S4_REGNUM 20) @@ -1869,6 +1870,24 @@ [(set_attr "got" "load") (set_attr "mode" "")]) +(define_insn "@tlsdesc" + [(set (reg:P A0_REGNUM) + (unspec:P + [(match_operand:P 0 "symbolic_operand" "") + (match_operand:P 1 "const_int_operand")] + UNSPEC_TLSDESC)) + (clobber (reg:SI T0_REGNUM))] + "TARGET_TLSDESC" + { + return ".LT%1: auipc\ta0, %%tlsdesc_hi(%0)\;" + "\tt0,%%tlsdesc_load_lo(.LT%1)(a0)\;" + "addi\ta0,a0,%%tlsdesc_add_lo(.LT%1)\;" + "jalr\tt0,t0,%%tlsdesc_call(.LT%1)"; + } + [(set_attr "type" "multi") + (set_attr "length" "16") + (set_attr "mode" "")]) + (define_insn "auipc" [(set (match_operand:P 0 "register_operand" "=r") (unspec:P diff --git a/gcc/config/riscv/riscv.opt b/gcc/config/riscv/riscv.opt index 6304efebfd5..9ba690f8497 100644 --- a/gcc/config/riscv/riscv.opt +++ b/gcc/config/riscv/riscv.opt @@ -311,3 +311,17 @@ Enum(riscv_autovec_lmul) String(m8) Value(RVV_M8) -param=riscv-autovec-lmul= Target RejectNegative Joined Enum(riscv_autovec_lmul) Var(riscv_autovec_lmul) Init(RVV_M1) -param=riscv-autovec-lmul= Set the RVV LMUL of auto-vectorization in the RISC-V port. + +Enum +Name(tls_type) Type(enum riscv_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(riscv_tls_dialect) Init(TLS_TRADITIONAL) Save +Specify TLS dialect.