From patchwork Fri Mar 17 17:51:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1758350 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=QqVfuNRQ; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PdWsx6dxQz1yWs for ; Sat, 18 Mar 2023 04:52:05 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0389E38515C9 for ; Fri, 17 Mar 2023 17:52:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0389E38515C9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679075523; bh=sl7PII8ULd1KMLImUHb1ze8JRibPMBbaxFgyugXAnws=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=QqVfuNRQtN+9e2BKryu67yivw4opoMF2lhiaoG4XtLbsScjhdZN/0zoPzvEyYroZL T02libGMUapN9Xj4R13Zwe9ehN4k719m1ZcejccVEkJFcyBXdG7fNCgLQ2B7TR1zYQ oNwVatTf36RLHSrxtZ8ZH+BQ673ECMF34wxtLAvo= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id EB162385559F for ; Fri, 17 Mar 2023 17:51:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EB162385559F 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-356-FjBZoU-aN5KAWN0peBxM8g-1; Fri, 17 Mar 2023 13:51:40 -0400 X-MC-Unique: FjBZoU-aN5KAWN0peBxM8g-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CA9FC96DC81 for ; Fri, 17 Mar 2023 17:51:39 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8ED891121315; Fri, 17 Mar 2023 17:51:39 +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 32HHpb9I3137312 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 17 Mar 2023 18:51:37 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 32HHpals3137311; Fri, 17 Mar 2023 18:51:36 +0100 Date: Fri, 17 Mar 2023 18:51:36 +0100 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Drop TREE_READONLY on vars (possibly) initialized by tls wrapper [PR109164] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Hi! The following two testcases are miscompiled, because we keep TREE_READONLY on the vars even when they are (possibly) dynamically initialized by a TLS wrapper function. Normally cp_finish_decl drops TREE_READONLY from vars which need dynamic initialization, but for TLS we do this kind of initialization upon every access to those variables. Keeping them TREE_READONLY means e.g. PRE can hoist loads from those before loops which contain the TLS wrapper calls, so we can access the TLS variables before they are initialized. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-03-17 Jakub Jelinek PR c++/109164 * decl2.cc (get_tls_wrapper_fn): Clear TREE_READONLY on variables for which a TLS wrapper is added. * 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/decl2.cc.jj 2023-03-07 21:20:31.800491531 +0100 +++ gcc/cp/decl2.cc 2023-03-17 12:20:11.960678291 +0100 @@ -3773,6 +3773,12 @@ get_tls_wrapper_fn (tree var) DECL_BEFRIENDING_CLASSES (fn) = var; set_global_binding (fn); + + /* The variable now needs dynamic initialization by the wrapper + function, we don't want to hoist accesses to it before the + wrapper. */ + if (TREE_READONLY (var)) + TREE_READONLY (var) = 0; } return fn; } --- gcc/testsuite/g++.dg/tls/thread_local13.C.jj 2023-03-17 12:28:24.692427351 +0100 +++ gcc/testsuite/g++.dg/tls/thread_local13.C 2023-03-17 12:30:34.505519746 +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-17 12:28:28.721368058 +0100 +++ gcc/testsuite/g++.dg/tls/thread_local13-aux.cc 2023-03-17 12:37:53.952070861 +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-17 12:35:48.951905245 +0100 +++ gcc/testsuite/g++.dg/tls/thread_local14.C 2023-03-17 12:49:03.456249628 +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-17 12:36:58.724881322 +0100 +++ gcc/testsuite/g++.dg/tls/thread_local14-aux.cc 2023-03-17 12:48:53.914389421 +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 (); +}