From patchwork Sat Mar 18 15:09:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 71626 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp331509wrt; Sat, 18 Mar 2023 08:10:59 -0700 (PDT) X-Google-Smtp-Source: AK7set9xBjwzp6E2ZgoCsdNZJWtWuXrzVG3+y9WrqSBKy5ApYP0C50F+jwK023k4O76QkSIa1cuQ X-Received: by 2002:a17:907:d686:b0:927:8285:18a3 with SMTP id wf6-20020a170907d68600b00927828518a3mr3268296ejc.0.1679152259736; Sat, 18 Mar 2023 08:10:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679152259; cv=none; d=google.com; s=arc-20160816; b=YXYlv3//HQQfS8ZWWBzm2FLQA9WyOjT6XbhYw366Nko98hM2eYhgIiiadtW8MLBA4p so3Itva0q205DTePAIFsmfQIM51O1g3Wh2yzasbl5NVBzBZvXH+YNG9lFY3skwkeJUrg KrAibNpKVgC0MoOl/LGuPiZJDJ7woS4NUBthPH15lOaFvI1tY0nk/gTVRAVpvG+q5Rv2 2KRzRbH6PRMXbkiepMIFT7azL1IuE7ZsfYxB8KQSXpKSNQTEHjiY80ecC35Aj/+KIe3n ToDWBabzsx5ak8zDjcQI3p1hgrWHNMzm+RovNqcnygE8xQxVyglolqcsnopr+g4tbadv gYhA== 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-disposition:in-reply-to:mime-version:references:message-id :subject:cc:to:date:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=4deEjVBASiORde0fSjQUAcIASOwQVTfOZALLjOcBydw=; b=nRP5xDTVcLl3VVzD2fxOGlls1+c6U7omYTTPFWodbVj90A5d69SmkPsDLAXcbP48FU DoMmLynP4LnDIkJR6LWGBrE/FdfVed0rRwTXe/HkeNYd4/A7U8XdwxaRBrU2YfbbsBGw k0ReYm3SoHj7ZzD0AdeLGVkGd+jSbFrCHL2gaplXmbpViyyCto95P48CEOkvUIc400p5 Y7uDoBhbxZUWhqZSqn7CGTNrEjiDIu03FLtVL+yB1N1ZWE1HtVpRXuknRgRz726CgPBX pNhNgUxy6VEWDINXKW/xWJ/iEyRVJir/xlu7uEQ3jEkHBG0uAUY5bRlE3Uao3yB4OZPf tAjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="RYLD0/uf"; 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 sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id du12-20020a17090772cc00b00931f00ca4e3si5064570ejc.779.2023.03.18.08.10.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 08:10:59 -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="RYLD0/uf"; 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 97E413850208 for ; Sat, 18 Mar 2023 15:10:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 97E413850208 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679152247; bh=4deEjVBASiORde0fSjQUAcIASOwQVTfOZALLjOcBydw=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=RYLD0/uf0S5qUWy2TmBC/Nk2iJYI7L4mhzrQlGz/HMtqH5ntT23ZA0kYy+7SfZbhE FvlCXOaviRv5/jlWny4YaH8q5mr0ZbIEUP3W16JxqQG6j/sVBF1Gymtg4jkTv5qGZh /1++vLcSjlSGitvAmmUw1T+q6cK8ZSlOu5huOJW0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id D61B83858C50 for ; Sat, 18 Mar 2023 15:10:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D61B83858C50 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-657-4bnLhDP3PWGZ_98UzVWrgg-1; Sat, 18 Mar 2023 11:10:00 -0400 X-MC-Unique: 4bnLhDP3PWGZ_98UzVWrgg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BC57E85A5B1 for ; Sat, 18 Mar 2023 15:09:59 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8062440D1C5; Sat, 18 Mar 2023 15:09:58 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 32IF9u7d3160540 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 18 Mar 2023 16:09:57 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 32IF9unq3160539; Sat, 18 Mar 2023 16:09:56 +0100 Date: Sat, 18 Mar 2023 16:09:55 +0100 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++, v2: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164] Message-ID: References: MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760718759800985145?= X-GMAIL-MSGID: =?utf-8?q?1760718759800985145?= On Sat, Mar 18, 2023 at 01:54:58PM +0100, Jakub Jelinek via Gcc-patches wrote: > The patch is mostly about DECL_EXTERNAL cases, the others are supposedly > handled by the var_definition_p code there (or at least I assumed; > testcases certainly test only DECL_EXTERNAL). > I guess it could be done in cp_finish_decl, maybe better next to the > /* A reference will be modified here, as it is initialized. */ > if (! DECL_EXTERNAL (decl) > && TREE_READONLY (decl) > && TYPE_REF_P (type)) > { > was_readonly = 1; > TREE_READONLY (decl) = 0; > } > spot, but we'd need to export the decl2.cc helpers for it, > because not all DECL_THREAD_LOCAL_P vars need to be treated that way. > if (VAR_P (decl) > && CP_DECL_THREAD_LOCAL_P (decl) > && var_needs_tls_wrapper (decl) > && (!DECL_EXTERNAL (decl) || flag_extern_tls_init)) > TREE_READONLY (decl) = 0; > where var_needs_tls_wrapper would need to be exported from decl2.cc. > Though, var_needs_tls_wrapper -> var_defined_without_dynamic_init > needs > DECL_NONTRIVIALLY_INITIALIZED_P/DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P, > so perhaps that is accurate only closer to the end of cp_finish_decl? Here it is in patch form, tested so far on tls.exp: 2023-03-18 Jakub Jelinek PR c++/109164 * cp-tree.h (var_needs_tls_wrapper): Declare. * decl2.cc (var_needs_tls_wrapper): No longer static. * decl.cc (cp_finish_decl): Clear TREE_READONLY on TLS variables for which a TLS wrapper will be needed. * g++.dg/tls/thread_local13.C: New test. * g++.dg/tls/thread_local13-aux.cc: New file. * g++.dg/tls/thread_local14.C: New test. * g++.dg/tls/thread_local14-aux.cc: New file. Jakub --- gcc/cp/cp-tree.h.jj 2023-03-17 08:37:07.542937058 +0100 +++ gcc/cp/cp-tree.h 2023-03-18 16:02:46.771230806 +0100 @@ -6989,6 +6989,7 @@ extern void copy_linkage (tree, tree); extern tree get_guard (tree); extern tree get_guard_cond (tree, bool); extern tree set_guard (tree); +extern bool var_needs_tls_wrapper (tree); extern tree maybe_get_tls_wrapper_call (tree); extern void mark_needed (tree); extern bool decl_needed_p (tree); --- gcc/cp/decl2.cc.jj 2023-03-17 16:09:01.749244271 +0100 +++ gcc/cp/decl2.cc 2023-03-18 15:48:31.340642324 +0100 @@ -3623,7 +3623,7 @@ var_defined_without_dynamic_init (tree v /* Returns true iff VAR is a variable that needs uses to be wrapped for possible dynamic initialization. */ -static bool +bool var_needs_tls_wrapper (tree var) { return (!error_operand_p (var) --- gcc/cp/decl.cc.jj 2023-03-18 15:47:32.198500421 +0100 +++ gcc/cp/decl.cc 2023-03-18 16:00:04.565584266 +0100 @@ -8706,6 +8706,18 @@ cp_finish_decl (tree decl, tree init, bo if (!decl_maybe_constant_destruction (decl, type)) TREE_READONLY (decl) = 0; } + else if (VAR_P (decl) + && CP_DECL_THREAD_LOCAL_P (decl) + && (!DECL_EXTERNAL (decl) || flag_extern_tls_init) + && (was_readonly || TREE_READONLY (decl)) + && var_needs_tls_wrapper (decl)) + { + /* TLS variables need dynamic initialization by the TLS wrapper + function, we don't want to hoist accesses to it before the + wrapper. */ + was_readonly = 0; + TREE_READONLY (decl) = 0; + } make_rtl_for_nonlocal_decl (decl, init, asmspec); --- gcc/testsuite/g++.dg/tls/thread_local13.C.jj 2023-03-18 15:47:50.934228583 +0100 +++ gcc/testsuite/g++.dg/tls/thread_local13.C 2023-03-18 15:47:50.934228583 +0100 @@ -0,0 +1,21 @@ +// PR c++/109164 +// { dg-do run { target c++11 } } +// { dg-options "-O2" } +// { dg-add-options tls } +// { dg-require-effective-target tls_runtime } +// { dg-additional-sources "thread_local13-aux.cc" } + +struct S { virtual void foo (); int s; }; +extern thread_local S &t; +bool bar (); + +bool +baz () +{ + while (1) + { + t.foo (); + if (!bar ()) + return false; + } +} --- gcc/testsuite/g++.dg/tls/thread_local13-aux.cc.jj 2023-03-18 15:47:50.934228583 +0100 +++ gcc/testsuite/g++.dg/tls/thread_local13-aux.cc 2023-03-18 15:47:50.934228583 +0100 @@ -0,0 +1,35 @@ +// PR c++/109164 + +struct S { virtual void foo (); int s; }; +extern bool baz (); + +void +S::foo () +{ + if (s != 42) + __builtin_abort (); +} + +S s; + +S & +qux () +{ + s.s = 42; + return s; +} + +thread_local S &t = qux (); + +bool +bar () +{ + return false; +} + +int +main () +{ + if (baz ()) + __builtin_abort (); +} --- gcc/testsuite/g++.dg/tls/thread_local14.C.jj 2023-03-18 15:47:50.934228583 +0100 +++ gcc/testsuite/g++.dg/tls/thread_local14.C 2023-03-18 15:47:50.934228583 +0100 @@ -0,0 +1,19 @@ +// PR c++/109164 +// { dg-do run { target c++11 } } +// { dg-options "-O2" } +// { dg-add-options tls } +// { dg-require-effective-target tls_runtime } +// { dg-additional-sources "thread_local14-aux.cc" } + +extern thread_local const int t; +bool bar (int); + +bool +baz () +{ + while (1) + { + if (!bar (t)) + return false; + } +} --- gcc/testsuite/g++.dg/tls/thread_local14-aux.cc.jj 2023-03-18 15:47:50.934228583 +0100 +++ gcc/testsuite/g++.dg/tls/thread_local14-aux.cc 2023-03-18 15:47:50.934228583 +0100 @@ -0,0 +1,26 @@ +// PR c++/109164 + +extern bool baz (); + +int +qux () +{ + return 42; +} + +extern thread_local const int t = qux (); + +bool +bar (int x) +{ + if (x != 42) + __builtin_abort (); + return false; +} + +int +main () +{ + if (baz ()) + __builtin_abort (); +}