From patchwork Thu Jan 11 06:12:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 83829 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 333FE38582AC for ; Thu, 11 Jan 2024 06:13:23 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by sourceware.org (Postfix) with ESMTPS id E6AF83858D38 for ; Thu, 11 Jan 2024 06:12:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E6AF83858D38 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E6AF83858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704953569; cv=none; b=atmIwF+1QXvrOaGN+VLGDuR4LP6kw6spkQSRrSDsT6AgLHMsQDlvpm4qTefrJX+KlDfoWTW51HcB9Ez4j0v5ITidJtwvl4rUdrVy8/9K3JksJM3tyVyfx9lKnLl21mDSjM1uMKy5cSuzkSoFqclWXjs30RnoV2UFh7x5Fa6Yy48= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704953569; c=relaxed/simple; bh=kYtlyvKPPjEFcUma0yH2qzuztHhtPpFexaY6GB3ROzM=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=vTeP1mspi7rh5ocHlO4AMRE2pW07IifZOyw6HLpoEFTCZ+3Nl7X1RWlwEfed2mGjef/r/0L0PU2eMLtnG2UDAT12LR/hzDXIAYhu4ztyfoWM3X/BbY5lUDUmnODseZM37WmObif42IYjnEr7/XSPX97YeqcwA0quTXPET/npv2c= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-6d9bba6d773so4267428b3a.1 for ; Wed, 10 Jan 2024 22:12:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704953566; x=1705558366; darn=gcc.gnu.org; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=bj7m+C/Zo4Iy6DlOm6Cmh5KmJZorh3UVJDtY9qc8I3M=; b=a5eufjH39JhYmB0RZ7GEnp9lX7Wtzf57j68/i/gqdvErvneFba8XiR61tCylkTScKm xSUOMThfdZL6RWAp8dRh0tnAqwh7UCPx/T3pntcB7U/o4UpGdM2cUyquwJZPHsJg2XuO mTA5JpYCCfwW7qKChPpB0P7TqpZ5vsciQBX/zzje6+pI9b/680dI5e9vua+VDYpmw+1w kE0bzXDwbKzRCfYW2y85COkj04FomuaMAR6QzelgMCIwcezR2EeAWPpdobTIqKdsOW6f 124q5v1DDE/S1dvhLXDjcf0jGoo04BYQA6NeT1LTfjevLnullJxLFkpFgc06+xkxmhrh 39AQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704953566; x=1705558366; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bj7m+C/Zo4Iy6DlOm6Cmh5KmJZorh3UVJDtY9qc8I3M=; b=GDMakWgNQz5ObUQwWiBnk3dop9mNfAQ0LQeK77zVvzo5PB3Wp6L0L7CiBrbGacrw64 eUie5UWw8D0Kt3XHpaE2TK+EPRNEj/3pTNLPN8tIFmmahMSbFVfAjz35UV0Uz6DoYIAc 0TIk5M0VcX4+tEGOP4JzmjEHTQsc9G9gfxulAFU1lrZzLZpyl/KvG/V5KEQZbsZ9gTPR U7GkABwdvmOXgV1QT9MgJ1ei6Vi5SEAaolWra31zPNUkXnuabflGV5rDhFpxUa8+Vh2S 3JiOBtA8dYWqmcbrK56iWbHR4yHTYmF1Uc5DfKcdO8cuB0thTVwaCefShE5VGsemTTgE 8ciA== X-Gm-Message-State: AOJu0YzynSZsaBjTsXFI+Yz1r3ZKVIQj+gBUoQBioiEB9fg2XNyfFfkw y7gzEtK5m3+8EFv1dzp9drmY8JvJ4fw= X-Google-Smtp-Source: AGHT+IHjL806pkYNxSxkApyrNJrCUvPyBj99S7Is4XLc6i3p/zqjNezdIgqi/WmIcYdr1+uERp94Ug== X-Received: by 2002:a05:6a00:2f5a:b0:6da:c743:4c34 with SMTP id ff26-20020a056a002f5a00b006dac7434c34mr781915pfb.66.1704953565710; Wed, 10 Jan 2024 22:12:45 -0800 (PST) Received: from Thaum. (123-243-206-49.tpgi.com.au. [123.243.206.49]) by smtp.gmail.com with ESMTPSA id o16-20020a056a00215000b006d9abe929desm339186pfk.67.2024.01.10.22.12.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jan 2024 22:12:45 -0800 (PST) Message-ID: <659f86dd.050a0220.c8110.0a09@mx.google.com> X-Google-Original-Message-ID: Date: Thu, 11 Jan 2024 17:12:41 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH] c++/modules: Support thread_local statics in header modules [PR113292] MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE, URIBL_BLACK autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Bootstrapped and regtested on x86_64-pc-linux-gnu. OK for trunk? -- >8 -- Currently, thread_locals in header modules cause ICEs. This patch makes the required changes for them to work successfully. Functions exported by a module need DECL_CONTEXT to be set, so we inherit it from the variable we're wrapping. We additionally require 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 --- gcc/cp/decl2.cc | 10 ++++--- gcc/cp/module.cc | 22 +++++++++++++++ gcc/testsuite/g++.dg/modules/pr113292_a.H | 34 +++++++++++++++++++++++ gcc/testsuite/g++.dg/modules/pr113292_b.C | 13 +++++++++ gcc/testsuite/g++.dg/modules/pr113292_c.C | 11 ++++++++ 5 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/pr113292_a.H create mode 100644 gcc/testsuite/g++.dg/modules/pr113292_b.C create mode 100644 gcc/testsuite/g++.dg/modules/pr113292_c.C diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc index fb996561f1b..ab348f8ecb7 100644 --- a/gcc/cp/decl2.cc +++ b/gcc/cp/decl2.cc @@ -3860,6 +3860,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)) @@ -5289,10 +5290,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 9bb6d2643d8..e66838b2e6e 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -6948,6 +6948,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); @@ -7027,6 +7030,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); @@ -7926,6 +7931,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); @@ -8273,6 +8281,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 { @@ -8356,6 +8371,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 00000000000..90ece2ec3f3 --- /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 +struct test_template { + static const test_template& get_instance() { + return instance; + } + static thread_local test_template instance; + + template + static const test_template& get_template_instance() { + return template_instance; + } + + template + static thread_local test_template template_instance; +}; + +template +thread_local test_template test_template::instance; + +template +template +thread_local test_template test_template::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 00000000000..fc582a5a0cf --- /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::get_instance(); + auto& tt_instance = test_template::get_template_instance(); +}; 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 00000000000..aa3f32ae818 --- /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::get_instance(); + auto& tt_instance = test_template::get_template_instance(); +}