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: 66561 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 83FB1384F037 for ; Sat, 18 Mar 2023 15:10:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 83FB1384F037 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679152232; 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=kYZaTIecn53MIXJg/sjD5gIU07gNkpPYhZaIpUyRFBFXMi0qIUsQlNzAhV9l7IcGb MIvxM3AAlDytpJTbiypKQydjx7HISuwoe+Zq/dRdj09snJbUA2u4nVd/eXoW3nvWH+ qBn+Wd9/H1srSwc2AA0vmimTpMizYNkbSbqbDY0Q= 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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 (); +}