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: 71379 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp477978wrt; Fri, 17 Mar 2023 10:52:28 -0700 (PDT) X-Google-Smtp-Source: AK7set/mnejgCGfYk6HNknTDhvpfFuirPm/d8mQe05AiOl1BT7/vNVTBg2whPKBTJWTdqy7p0/bI X-Received: by 2002:a17:906:f204:b0:879:bff:55c with SMTP id gt4-20020a170906f20400b008790bff055cmr296747ejb.1.1679075548397; Fri, 17 Mar 2023 10:52:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679075548; cv=none; d=google.com; s=arc-20160816; b=NyCiwnWRC2k7X1wcRspWeKiDEjMYsQg93r4Y0HvvLI5BHZuftWnbWLypkvPAp9NeYR F6HyliIHeHuN6mQDiYDH9fWZIja8rkNq7QvEYsSKjsYupX6v/FMFuiHoA5qA23RRdaLz zhjvluQ+V2LKIYXNXFsnO4l6CYMFmK/0pZRBEYPor8NSnVQ7/z+48Afw5eL4m45rbsFZ Q29Koru7Qhc/IZHJrNlt2cx6OkISY1cF5LVI44tF+5WFUk4i+b2wAy47ao9c0f1ud9Jz +ywueCmcqRXOnjW5W2oqHvGTdUZzSyizvZn4NjGjddzt2Af5iOszdN1ZptVdnnneaSpW 3xxg== 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:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=sl7PII8ULd1KMLImUHb1ze8JRibPMBbaxFgyugXAnws=; b=N2r+dsLrf+7AidEkQoLvyuCet52ZbTv6xL00Qp0whouljBT13+UiKxKEzKxLj2lCm3 AnjcwCGSzINek07gOqd0nqIyvnDYITn4Zy3BzJT+/iaX4mzC5a/n4ppz4RLIdId6ZivR SEoZ+fslHAe4o3w1mjmWa722S8iCH4n5tclWFe9d+B50KQLXPMQmXh3i/gu8yO4Drv+Q GVgIyteQ9lJB9etbM6cdCn/Oss9vGfIvlXJPJ511PZuLpqNgdNbWrpJRvtHYuoOelvw/ oABTgYXpxodC9UA5bC3kpbF3q0QuBI0pxKziss3KqHw0xjQ0zY7jBi1C8A1jZHBFBBXH QuBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=eqB+Ej+v; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id r9-20020aa7cfc9000000b004fd26753791si2959053edy.649.2023.03.17.10.52.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Mar 2023 10:52:28 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=eqB+Ej+v; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 33DAB38515C9 for ; Fri, 17 Mar 2023 17:52:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 33DAB38515C9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679075547; bh=sl7PII8ULd1KMLImUHb1ze8JRibPMBbaxFgyugXAnws=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=eqB+Ej+vvMOvbTJIhXvlELeg1j/AnNUXT9+OcFmd+oLxjLqdEDa/hgLHnaP8omqQ8 HJgbkuJnwUxeYMBYWGzUK+W+gZhop1xDVDiDQAW86zPXG2kTRV6CJurg4u64cOi4kq TXmWXrGHHZc+W1MitYhsVCzj4uMOnzM8t5LQp95E= 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760638322074223468?= X-GMAIL-MSGID: =?utf-8?q?1760638322074223468?= 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 (); +}