aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorNathaniel Shead <nathanieloshead@gmail.com>2024-01-11 16:49:39 +1100
committerNathaniel Shead <nathanieloshead@gmail.com>2024-01-17 09:40:42 +1100
commit14338386970bc6c2d46b81181f48622fdf25d705 (patch)
tree3a3a5e7272a1290aef1a2f540a1d7bbe7393fa4d /gcc
parentfe1649eea76f193732d22ec022a35326523eb37a (diff)
downloadgcc-14338386970bc6c2d46b81181f48622fdf25d705.zip
gcc-14338386970bc6c2d46b81181f48622fdf25d705.tar.gz
gcc-14338386970bc6c2d46b81181f48622fdf25d705.tar.bz2
c++: Support thread_local statics in header modules [PR113292]
Currently, thread_locals in header modules cause ICEs. This patch makes the required changes for them to work successfully. This requires additionally writing the DECL_TLS_MODEL for thread-local variables to the module interface, and the TLS wrapper function needs to have its DECL_BEFRIENDING_CLASSES written too as this is used to retrieve what VAR_DECL it's a wrapper for when emitting a definition at end of TU processing. PR c++/113292 gcc/cp/ChangeLog: * decl2.cc (get_tls_wrapper_fn): Set DECL_CONTEXT. (c_parse_final_cleanups): Suppress warning for no definition of TLS wrapper functions in header modules. * module.cc (trees_out::lang_decl_vals): Write wrapped variable for TLS wrapper functions. (trees_in::lang_decl_vals): Read it. (trees_out::decl_value): Write TLS model for thread-local vars. (trees_in::decl_value): Read it for new decls. Remember to emit definitions of TLS wrapper functions later. gcc/testsuite/ChangeLog: * g++.dg/modules/pr113292_a.H: New test. * g++.dg/modules/pr113292_b.C: New test. * g++.dg/modules/pr113292_c.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/decl2.cc10
-rw-r--r--gcc/cp/module.cc22
-rw-r--r--gcc/testsuite/g++.dg/modules/pr113292_a.H34
-rw-r--r--gcc/testsuite/g++.dg/modules/pr113292_b.C13
-rw-r--r--gcc/testsuite/g++.dg/modules/pr113292_c.C11
5 files changed, 86 insertions, 4 deletions
diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index fdc52a0..91d4e2e 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -3897,6 +3897,7 @@ get_tls_wrapper_fn (tree var)
TREE_PUBLIC (fn) = TREE_PUBLIC (var);
DECL_ARTIFICIAL (fn) = true;
DECL_IGNORED_P (fn) = 1;
+ DECL_CONTEXT (fn) = DECL_CONTEXT (var);
/* The wrapper is inline and emitted everywhere var is used. */
DECL_DECLARED_INLINE_P (fn) = true;
if (TREE_PUBLIC (var))
@@ -5326,10 +5327,11 @@ c_parse_final_cleanups (void)
#pragma interface, etc.) we decided not to emit the
definition here. */
&& !DECL_INITIAL (decl)
- /* A defaulted fn in a header module can be synthesized on
- demand later. (In non-header modules we should have
- synthesized it above.) */
- && !(DECL_DEFAULTED_FN (decl) && header_module_p ())
+ /* A defaulted fn or TLS wrapper in a header module can be
+ synthesized on demand later. (In non-header modules we
+ should have synthesized it above.) */
+ && !(header_module_p ()
+ && (DECL_DEFAULTED_FN (decl) || decl_tls_wrapper_p (decl)))
/* Don't complain if the template was defined. */
&& !(DECL_TEMPLATE_INSTANTIATION (decl)
&& DECL_INITIAL (DECL_TEMPLATE_RESULT
diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index aa75e28..350ad15 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -6950,6 +6950,9 @@ trees_out::lang_decl_vals (tree t)
if (streaming_p ())
wi (lang->u.fn.u5.fixed_offset);
}
+ else if (decl_tls_wrapper_p (t))
+ /* The wrapped variable. */
+ WT (lang->u.fn.befriending_classes);
else
WT (lang->u.fn.u5.cloned_function);
@@ -7029,6 +7032,8 @@ trees_in::lang_decl_vals (tree t)
RT (lang->u.fn.befriending_classes);
lang->u.fn.u5.fixed_offset = wi ();
}
+ else if (decl_tls_wrapper_p (t))
+ RT (lang->u.fn.befriending_classes);
else
RT (lang->u.fn.u5.cloned_function);
@@ -7928,6 +7933,9 @@ trees_out::decl_value (tree decl, depset *dep)
decl, cloned_p ? "" : "not ");
}
+ if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
+ u (decl_tls_model (decl));
+
if (streaming_p ())
dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
TREE_CODE (decl), decl);
@@ -8275,6 +8283,13 @@ trees_in::decl_value ()
look like templates. */
if (!install_implicit_member (inner))
set_overrun ();
+
+ /* When importing a TLS wrapper from a header unit, we haven't
+ actually emitted its definition yet. Remember it so we can
+ do this later. */
+ if (state->is_header ()
+ && decl_tls_wrapper_p (decl))
+ note_vague_linkage_fn (decl);
}
else
{
@@ -8358,6 +8373,13 @@ trees_in::decl_value ()
}
}
+ if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
+ {
+ enum tls_model model = tls_model (u ());
+ if (is_new)
+ set_decl_tls_model (decl, model);
+ }
+
if (!NAMESPACE_SCOPE_P (inner)
&& ((TREE_CODE (inner) == TYPE_DECL
&& !is_typedef
diff --git a/gcc/testsuite/g++.dg/modules/pr113292_a.H b/gcc/testsuite/g++.dg/modules/pr113292_a.H
new file mode 100644
index 0000000..90ece2e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/pr113292_a.H
@@ -0,0 +1,34 @@
+// PR c++/113292
+// { dg-additional-options "-fmodule-header" }
+// { dg-module-cmi {} }
+
+struct test {
+ static const test& get_instance() {
+ return instance;
+ }
+ static thread_local test instance;
+};
+
+
+template <typename T>
+struct test_template {
+ static const test_template& get_instance() {
+ return instance;
+ }
+ static thread_local test_template instance;
+
+ template <typename U>
+ static const test_template& get_template_instance() {
+ return template_instance<U>;
+ }
+
+ template <typename U>
+ static thread_local test_template template_instance;
+};
+
+template <typename T>
+thread_local test_template<T> test_template<T>::instance;
+
+template <typename T>
+template <typename U>
+thread_local test_template<T> test_template<T>::template_instance;
diff --git a/gcc/testsuite/g++.dg/modules/pr113292_b.C b/gcc/testsuite/g++.dg/modules/pr113292_b.C
new file mode 100644
index 0000000..fc582a5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/pr113292_b.C
@@ -0,0 +1,13 @@
+// PR c++/113292
+// { dg-additional-options "-fmodules-ts" }
+
+import "pr113292_a.H";
+
+// provide a definition of 'instance' so things link
+thread_local test test::instance;
+
+void instantiate() {
+ auto& instance = test::get_instance();
+ auto& t_instance = test_template<int>::get_instance();
+ auto& tt_instance = test_template<int>::get_template_instance<double>();
+};
diff --git a/gcc/testsuite/g++.dg/modules/pr113292_c.C b/gcc/testsuite/g++.dg/modules/pr113292_c.C
new file mode 100644
index 0000000..aa3f32a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/pr113292_c.C
@@ -0,0 +1,11 @@
+// PR c++/113292
+// { dg-module-do link }
+// { dg-additional-options "-fmodules-ts" }
+
+import "pr113292_a.H";
+
+int main() {
+ auto& instance = test::get_instance();
+ auto& t_instance = test_template<int>::get_instance();
+ auto& tt_instance = test_template<int>::get_template_instance<double>();
+}