From patchwork Thu Aug 17 18:12:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tatsuyuki Ishi X-Patchwork-Id: 135934 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp887181vqi; Thu, 17 Aug 2023 11:14:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFOVm6838BvBAtAjZVgcQCvPMHKLWnaDlziK2HzxoweyPB9eYiljaRPeHwIb+8t2xIR7kG4 X-Received: by 2002:a17:906:cc1:b0:99e:46b:6a58 with SMTP id l1-20020a1709060cc100b0099e046b6a58mr140500ejh.37.1692296088842; Thu, 17 Aug 2023 11:14:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692296088; cv=none; d=google.com; s=arc-20160816; b=OrHjR4U50qVT52i7otHDg7VdCqMlxdN3flLN+zd39Mygw1V6320w0e1qnlZ3ef6zRD Rxie7ZgQLdRsOU7hniJxjEegKrdyST8CGdsdQNMpD3e2rrjTjwe9jM4pATvN5xS2YUKR Ss2SVrcxfLviMFAPzalWUwC+USEvWV3F4PVWtB19ZDLhqvTp+U/C+qSz6lcceu66O3fh Yy6s0lu0Fn5wYl0qXCpkb0Mizcn+O57JMiLKeQWal4ZBCMv+p6Q9uhlHtCBkW46F0elg 9zN8f5fp57kYA7pKL7avIa6a6QhQTpuAV2VZZkggwUCHhWglpUcq+pAJ8UIgSPMBtD7t e7cQ== 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:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=iHD9GoucxAjqa+IVSypj83FFd/iHWEzGzy8K6KSBgaI=; fh=72gswZhYQtiuDtn4oqkxbPWqRBvlfp35UNgBulzu4Sk=; b=tXi8mjJcQ+1qnPQLOaZ7LXZPru6RAo0l9715/Pwb7wDjTpJehxEgjoaJO0ymk1ZquW +8lHO8yZOeu7wC527JKu9MRGwmwzduWdVWuvFPtCSgtTYqNHSCHmdWnfXW4ePyVMuwkH qOaPuhqH6AYm29o117zUlN6KLpxptEhGZxCF7/Yccum3vg2qhrF7QLxy6eBzlLoYZ3gP 8K7Y1JUEcytaSZWrUeHO3tjWNc36AjTvg4KhvFayZt4hXZRZW1X4yVtHt6ia3fuREtrL w5Cx/TPTNpUy06MoCH2hXHpmyKUUk+LyJ5I2n699Lvmkymux4oEF6TNS+Cx4OQCznbnX jA2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=dE6W+cD2; 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 x3-20020a170906804300b00996e0a8a2dfsi58194ejw.78.2023.08.17.11.14.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Aug 2023 11:14:48 -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=dE6W+cD2; 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 C6856385417F for ; Thu, 17 Aug 2023 18:14:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C6856385417F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692296087; bh=iHD9GoucxAjqa+IVSypj83FFd/iHWEzGzy8K6KSBgaI=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=dE6W+cD2J87yiWSfrw+vjQYgTtu/xcVW3ae/Dn37V9Oa70iwsdGKn+FJbzfQKAFZP bFiG+PufJZSrGfTpcuMpZ2lnYq6veqaX1koNi27r9ir3F+/TUT2x4GyVVjyw+LUHCO kMjgm2sPc6yGfk3q9giO58ptuIXX78mkCXGjwnPI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id 9A4203858D35 for ; Thu, 17 Aug 2023 18:14:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9A4203858D35 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-68781a69befso18069b3a.0 for ; Thu, 17 Aug 2023 11:14:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692296042; x=1692900842; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=iHD9GoucxAjqa+IVSypj83FFd/iHWEzGzy8K6KSBgaI=; b=dFMBz1T3Q9vHqX1SJr8rXD3N+i4nYXQayrhCU7ks6tGVEMWlwHwCS5mrc0RElM2VR1 zAFYH/pdI9xkY2KGsRYHK9B3WRr4Q846RSfKeqEFR04o96ggqic2vwMIMQVvMUcFWhES NGQgbc01Y5LklSE+zyZt4KDvtHFYjrXzONt58y0cn8ohilRILSFytPHm/2fytzM7Bgyu 2sqxpUjsO8KD8PRRK1NgikzxLZ2dCBQ2LLbORzK0gmxZqQSs7cnk+2JZOvLrOdTi1E5v KWCqG2+MFMy9mB0fKMkbe3pix5RlXHP+8HNiOM35ALkgOrnwZX+6izVmNWkSqL9QPnks rlOQ== X-Gm-Message-State: AOJu0Yydm+RlH1+hbJ9v32rVRNte/tbJpumSkK27aTcUFKMhtEWpOBdB SMkRO6Q3h7dWsXrzL8izKPOQ4IvJSyiZ2MK/ X-Received: by 2002:a05:6a00:27ab:b0:67d:308b:97ef with SMTP id bd43-20020a056a0027ab00b0067d308b97efmr308944pfb.2.1692296042105; Thu, 17 Aug 2023 11:14:02 -0700 (PDT) Received: from localhost (zz20184013906F627101.userreverse.dion.ne.jp. [111.98.113.1]) by smtp.gmail.com with ESMTPSA id n16-20020a62e510000000b00687fcb2ba7csm57735pff.103.2023.08.17.11.14.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Aug 2023 11:14:01 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: rui314@gmail.com, ruiu@bluewhale.systems, Tatsuyuki Ishi Subject: [PATCH] RISC-V: Implement TLS Descriptors. Date: Fri, 18 Aug 2023 03:12:51 +0900 Message-ID: <20230817181308.122802-2-ishitatsuyuki@gmail.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Spam-Status: No, score=-10.0 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.29 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: 1774501063388314626 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. For now, keep defaulting to the traditional TLS model, but this can be revisited once toolchain and libc support ships. [1]: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/373 --- No regression in binutils and gcc tests for rv64gc, tested alongside the binutils and glibc implementation (posted at the same time). During testing, the default TLS dialect was changed to TLSDESC. This contribution is made on behalf of Blue Whale Systems, which has copyright assignment on file with the FSF. gcc/config/riscv/riscv-opts.h | 6 ++++++ gcc/config/riscv/riscv-protos.h | 5 +++-- gcc/config/riscv/riscv.cc | 34 +++++++++++++++++++++++++++++---- gcc/config/riscv/riscv.h | 3 +++ gcc/config/riscv/riscv.md | 22 ++++++++++++++++++++- gcc/config/riscv/riscv.opt | 14 ++++++++++++++ 6 files changed, 77 insertions(+), 7 deletions(-) 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..4ff0adbbb1e 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 (); } @@ -1601,6 +1602,16 @@ static rtx riscv_tls_add_tp_le (rtx dest, rtx base, rtx sym) return gen_tls_add_tp_lesi (dest, base, tp, sym); } +/* Instruction sequence to call the TLS Descriptor resolver. */ + +static rtx riscv_tlsdesc (rtx sym, rtx seqno) +{ + if (Pmode == DImode) + return gen_tlsdescdi (sym, seqno); + else + return gen_tlsdescsi (sym, seqno); +} + /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise it appears in a MEM of that mode. Return true if ADDR is a legitimate constant in that context and can be split into high and low parts. @@ -1734,7 +1745,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 +1761,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 (riscv_tlsdesc (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..7cf1365ec08 100644 --- a/gcc/config/riscv/riscv.h +++ b/gcc/config/riscv/riscv.h @@ -1122,4 +1122,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..bddd92323ad 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,14 @@ (T1_REGNUM 6) (S0_REGNUM 8) (S1_REGNUM 9) + (A0_REGNUM 10) + (A1_REGNUM 11) + (A2_REGNUM 12) + (A3_REGNUM 13) + (A4_REGNUM 14) + (A5_REGNUM 15) + (A6_REGNUM 16) + (A7_REGNUM 17) (S2_REGNUM 18) (S3_REGNUM 19) (S4_REGNUM 20) @@ -1869,6 +1877,18 @@ [(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" + ".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 "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..40b3ebf2a99 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. \ No newline at end of file