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 <ja...@redhat.com> 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. --- 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 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-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; Jakub