From patchwork Wed Sep 29 08:36:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 45545 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 18B9B3858005 for ; Wed, 29 Sep 2021 08:37:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 18B9B3858005 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1632904677; bh=iQtNEyOjbB600jB/KlMn1UuBGIDfaB03URaU021s030=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=efcdXPhn9uzwMWCKIlI5sJmEguueuaMDsdyPXB0uEsm0iA1JJbcs3QwJiZriUa8kA 4uTCFo+eqipXI4pDrzQAQozn9FmUWCaDVmwP0hQqwqrE4YHg2TKJSQGu+guP+P09QB IqCovHaM+bCP89AyUAiYITlBTsgaIg4xKV/UmAOI= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 2E3443858015 for ; Wed, 29 Sep 2021 08:36:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2E3443858015 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-475-OyqdVPjfOemM4AsgC-wubw-1; Wed, 29 Sep 2021 04:36:44 -0400 X-MC-Unique: OyqdVPjfOemM4AsgC-wubw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8964F1006AAB for ; Wed, 29 Sep 2021 08:36:43 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.193.109]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1EA1719C79; Wed, 29 Sep 2021 08:36:42 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 18T8aeKN356744 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 29 Sep 2021 10:36:40 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 18T8aeFr356743; Wed, 29 Sep 2021 10:36:40 +0200 Date: Wed, 29 Sep 2021 10:36:40 +0200 To: Jason Merrill Subject: [PATCH] c++: Fix handling of __thread/thread_local extern vars declared at function scope [PR102496] Message-ID: <20210929083640.GL304296@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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 Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi! The introduction of push_local_extern_decl_alias in r11-3699-g4e62aca0e0520e4ed2532f2d8153581190621c1a broke tls vars, while the decl they are created for has the tls model set properly, nothing sets it for the alias that is actually used, so accesses to it are done as if they were normal variables. This is then diagnosed at link time if the definition of the extern vars is __thread/thread_local. Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/11.3? 2021-09-28 Jakub Jelinek PR c++/102496 * name-lookup.c: Include varasm.h. (push_local_extern_decl_alias): For CP_DECL_THREAD_LOCAL_P vars call set_decl_tls_model on alias. * g++.dg/tls/pr102496-1.C: New test. * g++.dg/tls/pr102496-2.C: New test. Jakub --- gcc/cp/name-lookup.c.jj 2021-09-28 15:51:54.035601004 +0200 +++ gcc/cp/name-lookup.c 2021-09-28 16:22:51.169954638 +0200 @@ -36,6 +36,7 @@ along with GCC; see the file COPYING3. If not see #include "c-family/known-headers.h" #include "c-family/c-spellcheck.h" #include "bitmap.h" +#include "varasm.h" static cxx_binding *cxx_binding_make (tree value, tree type); static cp_binding_level *innermost_nonclass_level (void); @@ -3471,6 +3472,10 @@ push_local_extern_decl_alias (tree decl) push_nested_namespace (ns); alias = do_pushdecl (alias, /* hiding= */true); pop_nested_namespace (ns); + if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl)) + set_decl_tls_model (alias, processing_template_decl + ? decl_default_tls_model (decl) + : DECL_TLS_MODEL (decl)); } } --- gcc/testsuite/g++.dg/tls/pr102496-1.C.jj 2021-09-28 16:25:47.330522476 +0200 +++ gcc/testsuite/g++.dg/tls/pr102496-1.C 2021-09-28 16:28:44.888071020 +0200 @@ -0,0 +1,20 @@ +// PR c++/102496 +// { dg-do link { target c++11 } } +// { dg-require-effective-target tls } +// { dg-add-options tls } +// { dg-additional-sources pr102496-2.C } + +template +int +foo () +{ + extern __thread int t1; + return t1; +} + +int +main () +{ + extern __thread int t2; + return foo <0> () + t2; +} --- gcc/testsuite/g++.dg/tls/pr102496-2.C.jj 2021-09-28 16:25:43.815571005 +0200 +++ gcc/testsuite/g++.dg/tls/pr102496-2.C 2021-09-28 16:28:54.132943380 +0200 @@ -0,0 +1,6 @@ +// PR c++/102496 +// { dg-do compile { target c++11 } } +// { dg-require-effective-target tls } + +__thread int t1; +__thread int t2;