]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Don't upgrade TLS model if TLS model isn't set.
authorH.J. Lu <hjl.tools@gmail.com>
Wed, 10 Sep 2025 19:47:47 +0000 (12:47 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 11 Sep 2025 12:33:40 +0000 (05:33 -0700)
Don't upgrade TLS model when cplus_decl_attributes is called on a thread
local variable whose TLS model isn't set yet.

gcc/cp/

PR c++/121889
* decl2.cc (cplus_decl_attributes): Don't upgrade TLS model if
TLS model isn't set yet.

gcc/testsuite/

PR c++/121889
* g++.dg/tls/pr121889.C: New test.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
gcc/cp/decl2.cc
gcc/testsuite/g++.dg/tls/pr121889.C [new file with mode: 0644]

index c9cca7b0270b3ca12fc4b227a8fdd2d05652eaa8..4564fdc45e01202bfa0edbb4cd1a067f411627bf 100644 (file)
@@ -2018,9 +2018,13 @@ cplus_decl_attributes (tree *decl, tree attributes, int flags)
     {
       // tls_model attribute can set a stronger TLS access model.
       tls_model model = DECL_TLS_MODEL (*decl);
-      tls_model default_model = decl_default_tls_model (*decl);
-      if (default_model > model)
-       set_decl_tls_model (*decl, default_model);
+      // Don't upgrade TLS model if TLS model isn't set yet.
+      if (model != TLS_MODEL_NONE)
+       {
+         tls_model default_model = decl_default_tls_model (*decl);
+         if (default_model > model)
+           set_decl_tls_model (*decl, default_model);
+       }
     }
 }
 \f
diff --git a/gcc/testsuite/g++.dg/tls/pr121889.C b/gcc/testsuite/g++.dg/tls/pr121889.C
new file mode 100644 (file)
index 0000000..881855a
--- /dev/null
@@ -0,0 +1,9 @@
+// { dg-do compile { target c++11 } }
+// { dg-require-effective-target tls }
+// { dg-options "-O2" }
+// { dg-add-options tls }
+
+extern void pthread_key_create();
+static __typeof(pthread_key_create) __gthrw___pthread_key_create
+    __attribute__((__weakref__("")));
+template <class> void zeta_imp_odd_integer() { thread_local int digits; }