On Thu, Sep 30, 2021 at 08:06:52AM -0400, Jason Merrill wrote: > Hmm, what if decl has the tls_model attribute? > > We could decide not to push the alias for a thread-local variable when > processing_template_decl, like we don't if the type is dependent; in either > case we'll push it at instantiation time.
So like this (assuming it passes full bootstrap/regtest, so far it passed tls.exp)? 2021-09-28 Jakub Jelinek <ja...@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. --- gcc/cp/name-lookup.c.jj 2021-09-29 10:07:28.838061585 +0200 +++ gcc/cp/name-lookup.c 2021-09-30 17:30:46.010100552 +0200 @@ -3375,7 +3375,10 @@ set_decl_context_in_fn (tree ctx, tree d 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)); } } --- gcc/testsuite/g++.dg/tls/pr102496-1.C.jj 2021-09-30 17:22:47.867769063 +0200 +++ gcc/testsuite/g++.dg/tls/pr102496-1.C 2021-09-30 17:22:47.867769063 +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 N> +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-30 17:22:47.867769063 +0200 +++ gcc/testsuite/g++.dg/tls/pr102496-2.C 2021-09-30 17:22:47.867769063 +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; Jakub