public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-4032] c++: Fix handling of __thread/thread_local extern vars declared at function scope [PR102496]
Date: Fri,  1 Oct 2021 08:31:16 +0000 (GMT)	[thread overview]
Message-ID: <20211001083116.EE6243857C7B@sourceware.org> (raw)

https://gcc.gnu.org/g:701075864ac4d1c6cec936d10f9cfc2aeb8c1699

commit r12-4032-g701075864ac4d1c6cec936d10f9cfc2aeb8c1699
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Oct 1 10:30:16 2021 +0200

    c++: Fix handling of __thread/thread_local extern vars declared at function scope [PR102496]
    
    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.
    
    2021-10-01  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/102496
            * name-lookup.c (push_local_extern_decl_alias): Return early even for
            tls vars with non-dependent type when processing_template_decl.  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.

Diff:
---
 gcc/cp/name-lookup.c                  |  7 ++++++-
 gcc/testsuite/g++.dg/tls/pr102496-1.C | 20 ++++++++++++++++++++
 gcc/testsuite/g++.dg/tls/pr102496-2.C |  6 ++++++
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c
index ddee8b390f9..c414a1091b4 100644
--- a/gcc/cp/name-lookup.c
+++ b/gcc/cp/name-lookup.c
@@ -3375,7 +3375,10 @@ set_decl_context_in_fn (tree ctx, tree decl)
 void
 push_local_extern_decl_alias (tree decl)
 {
-  if (dependent_type_p (TREE_TYPE (decl)))
+  if (dependent_type_p (TREE_TYPE (decl))
+      || (processing_template_decl
+	  && VAR_P (decl)
+	  && CP_DECL_THREAD_LOCAL_P (decl)))
     return;
   /* EH specs were not part of the function type prior to c++17, but
      we still can't go pushing dependent eh specs into the namespace.  */
@@ -3471,6 +3474,8 @@ 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, DECL_TLS_MODEL (decl));
 	}
     }
 
diff --git a/gcc/testsuite/g++.dg/tls/pr102496-1.C b/gcc/testsuite/g++.dg/tls/pr102496-1.C
new file mode 100644
index 00000000000..8220e1e663a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tls/pr102496-1.C
@@ -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 N>
+int
+foo ()
+{
+  extern __thread int t1;
+  return t1;
+}
+
+int
+main ()
+{
+  extern __thread int t2;
+  return foo <0> () + t2;
+}
diff --git a/gcc/testsuite/g++.dg/tls/pr102496-2.C b/gcc/testsuite/g++.dg/tls/pr102496-2.C
new file mode 100644
index 00000000000..a71a9cddc34
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tls/pr102496-2.C
@@ -0,0 +1,6 @@
+// PR c++/102496
+// { dg-do compile { target c++11 } }
+// { dg-require-effective-target tls }
+
+__thread int t1;
+__thread int t2;


                 reply	other threads:[~2021-10-01  8:31 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211001083116.EE6243857C7B@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).