From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id C30063858D28 for ; Fri, 26 Jan 2024 03:35:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C30063858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C30063858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706240110; cv=none; b=QadxsIzCISk6++q7FDkdBWQCD3h/VeEHyAE5LMvuR6X1qvGpBlx5nC8QVaRYbx2zWaJu2rTQYm5iFU12jZlq5sUWHNKa78UuYvrx+khGAzjupMyGRgQZG3864hnLsxvLh0U80d8o/1P3YWrsYaXR1ohzeHILgK0xzGQxATkJuLQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706240110; c=relaxed/simple; bh=POfB5vC8OTe1ZS+NfpglkK3C8nEwl7somwo96hZtWIY=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=NSIAvmv2DiDg3/luaiV3m3mxVjT5Sn/Bki0/zVVHcgUd7LyNaCWWXJrV50cPD2/TWnPANDllrhH9BWT5ActufB2P92XozaTphCnUdIQCfdD5D0y1Pbo4TCwapKKXbPKbIo4QG7+dbaJnkMnKrsYwdG5tmr0fhdu/A0lDq13Dyq4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706240108; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=b00X6yGOOxeBVwUBLWhW5w60UCPGVBVf9by/Y6Br8ls=; b=Usaa+PlFKudLllW+1vU8k/K5gGrovqkVnfArOHXdoc2CLwkGScDYrbVKWIsjWkPaJSujKS DPmijEwblVi1+SFROJ8OFtBIHEm0P/gW61YtomoBx8MyYNDqcIMD++dYT7uFVeTxbIvrJG 5tj6jO+JVYUaGUQ9x7zjKNHf0dn73qw= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-410-xwohwcaiMXCMy-4ctPOuTQ-1; Thu, 25 Jan 2024 22:35:04 -0500 X-MC-Unique: xwohwcaiMXCMy-4ctPOuTQ-1 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-68b382f111dso4498206d6.3 for ; Thu, 25 Jan 2024 19:35:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706240103; x=1706844903; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=b00X6yGOOxeBVwUBLWhW5w60UCPGVBVf9by/Y6Br8ls=; b=g48hruVFhrn5BIJg55aRqtnJVfB6d8Us469Ad5TT9te6TbB5ZVj/SqS4YZW70g8dan mFmrDW+VPDA+UFytYW6IBUqBy3DkTs8cRahCatzfJ21VAfYjRmTbHwaMmhUJf5dodhZD GydTerwEaS476nvioR/UKedXXRrbO8cWWlCjJuSP9qb5GPCFNPGN6yI4k0F5OYzybUsf 1AMtNkzgRa8RDiweHq1aDOLVKyMQpy2O/gK/RG9R8xwBwbLBOVoAk0DZ0RZgO3TzPK64 xCkdiiiuP/sE25zKIe1uFSbw64dGAjT/r9VXruQRdEMFbmLqHNovLbrCN+S2Uq2/D7CI 8D4A== X-Gm-Message-State: AOJu0YzzLE2cU9KntLaIarq2Ybs56QHWFEphcwArGWmJBG5ddByD8Mmm wWWjaE7my7B2lGNVJach0YIhiS6mjeCbc3FN1PvGpHxXwWqE8qp0Wfwf6jpllyoBisBC+B8/nCW 2JRmX9GDXpIhU3GeN7Ie07Ii67XzrWQu8GMDzuJLVEGmD7Zs4M+qE90o= X-Received: by 2002:a05:6214:481:b0:685:f7e3:39ac with SMTP id pt1-20020a056214048100b00685f7e339acmr720005qvb.78.1706240103606; Thu, 25 Jan 2024 19:35:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IF9aucxjakjABHGcdcLdg4ETBi5Q9CB2P6sC0y2bFDbFSKiEKQeVueAeqw0FsWHhcijGAP1GQ== X-Received: by 2002:a05:6214:481:b0:685:f7e3:39ac with SMTP id pt1-20020a056214048100b00685f7e339acmr719985qvb.78.1706240102493; Thu, 25 Jan 2024 19:35:02 -0800 (PST) Received: from [192.168.1.108] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id w19-20020a0562140b3300b00680b1090832sm153518qvj.146.2024.01.25.19.35.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 25 Jan 2024 19:35:01 -0800 (PST) Message-ID: Date: Thu, 25 Jan 2024 22:35:00 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4] c++/modules: Emit definitions of ODR-used static members imported from modules [PR112899] To: Nathaniel Shead Cc: Patrick Palka , gcc-patches@gcc.gnu.org, Nathan Sidwell References: <659490fd.170a0220.1ce2e.503a@mx.google.com> <36bed0fe-5564-60ed-df91-240d78819add@idea> <65aba461.a70a0220.7fd5c.ad24@mx.google.com> <473bdd7b-d352-4608-b472-843191c8d5f5@redhat.com> <65b318ed.170a0220.e1152.0933@mx.google.com> From: Jason Merrill In-Reply-To: <65b318ed.170a0220.e1152.0933@mx.google.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,RCVD_IN_SORBS_WEB,SPF_HELO_NONE,SPF_NONE,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 List-Id: On 1/25/24 21:28, Nathaniel Shead wrote: > On Wed, Jan 24, 2024 at 03:24:42PM -0500, Jason Merrill wrote: >> On 1/20/24 05:45, Nathaniel Shead wrote: >>> I also included >>> your change to only add class variable templates to 'pending_statics' >>> (and the normal 'static_decl's for non-class otherwise) as otherwise I >>> could imagine that they would cause issues with this later too. >> >> That seems wrong; the 'static_decls' vec is just for checking that >> static/inline variables got defined. >> >> pending_statics has been used for template instantiations for a long time, >> for non-module code; let's not mess with that in a modules patch. >> > > OK, makes sense. > >>> I know that there's been discussion about the correct ABI for inline >>> declarations, but personally I'd like to have this fixed for normal uses >>> in GCC14 at least, and we can revisit the specific cases where various >>> kinds of declarations are emitted in stage 1. >> >> Makes sense. >> >>> P.S. As I go to send this, I wonder if maybe something like >>> 'note_static_member_variable' would be a clearer choice of name than >>> 'note_static_storage_variable'? >> >> Let's call it note_vague_linkage_variable, to go with _fn just above. >> > > Sounds good. > >>> -- >8 -- >>> >>> Static data members marked 'inline' should be emitted in TUs where they >>> are ODR-used. We need to make sure that statics imported from modules >>> are correctly added to the 'pending_statics' map so that they get >>> emitted if needed, otherwise the attached testcase fails to link. >> >> What about non-member variables marked inline, and non-member variable >> template instantiations? >> >> Jason >> > > Non-member variables marked inline are already handled by 'static_decls' > via 'add_module_namespace_decl' and 'add_decl_to_level' during > stream-in, and then are later emitted from 'wrapup_namespace_globals'. > > I'd assumed that non-member variable template instantiations would also > be handled by here, but that turns out not to be the case, since the > instantiations themselves are not (of course) namespace-scope decls. > I've added a case to the tests for this. > > Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? OK. > -- >8 -- > > Static data members marked 'inline' should be emitted in TUs where they > are ODR-used. We need to make sure that inlines imported from modules > are correctly added to the 'pending_statics' map so that they get > emitted if needed, otherwise the attached testcase fails to link. > > PR c++/112899 > > gcc/cp/ChangeLog: > > * cp-tree.h (note_variable_template_instantiation): Rename to... > (note_vague_linkage_variable): ...this. > * decl2.cc (note_variable_template_instantiation): Rename to... > (note_vague_linkage_variable): ...this. > * pt.cc (instantiate_decl): Rename usage of above function. > * module.cc (trees_in::read_var_def): Remember pending statics > that we stream in. > > gcc/testsuite/ChangeLog: > > * g++.dg/modules/init-4_a.C: New test. > * g++.dg/modules/init-4_b.C: New test. > * g++.dg/modules/init-6_a.H: New test. > * g++.dg/modules/init-6_b.C: New test. > > Signed-off-by: Nathaniel Shead > Reviewed-by: Patrick Palka > Reviewed-by: Jason Merrill --- > gcc/cp/cp-tree.h | 2 +- > gcc/cp/decl2.cc | 4 ++-- > gcc/cp/module.cc | 5 +++++ > gcc/cp/pt.cc | 2 +- > gcc/testsuite/g++.dg/modules/init-4_a.C | 9 +++++++++ > gcc/testsuite/g++.dg/modules/init-4_b.C | 11 +++++++++++ > gcc/testsuite/g++.dg/modules/init-6_a.H | 20 ++++++++++++++++++++ > gcc/testsuite/g++.dg/modules/init-6_b.C | 9 +++++++++ > 8 files changed, 58 insertions(+), 4 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/modules/init-4_a.C > create mode 100644 gcc/testsuite/g++.dg/modules/init-4_b.C > create mode 100644 gcc/testsuite/g++.dg/modules/init-6_a.H > create mode 100644 gcc/testsuite/g++.dg/modules/init-6_b.C > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > index 60e6dafc549..f46b448ce0d 100644 > --- a/gcc/cp/cp-tree.h > +++ b/gcc/cp/cp-tree.h > @@ -7151,7 +7151,7 @@ extern tree maybe_get_tls_wrapper_call (tree); > extern void mark_needed (tree); > extern bool decl_needed_p (tree); > extern void note_vague_linkage_fn (tree); > -extern void note_variable_template_instantiation (tree); > +extern void note_vague_linkage_variable (tree); > extern tree build_artificial_parm (tree, tree, tree); > extern bool possibly_inlined_p (tree); > extern int parm_index (tree); > diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc > index 91d4e2e5ea4..f569d4045ec 100644 > --- a/gcc/cp/decl2.cc > +++ b/gcc/cp/decl2.cc > @@ -947,10 +947,10 @@ note_vague_linkage_fn (tree decl) > vec_safe_push (deferred_fns, decl); > } > > -/* As above, but for variable template instantiations. */ > +/* As above, but for variables. */ > > void > -note_variable_template_instantiation (tree decl) > +note_vague_linkage_variable (tree decl) > { > vec_safe_push (pending_statics, decl); > } > diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc > index f26b2265bce..6176801b7a7 100644 > --- a/gcc/cp/module.cc > +++ b/gcc/cp/module.cc > @@ -11789,6 +11789,11 @@ trees_in::read_var_def (tree decl, tree maybe_template) > DECL_INITIALIZED_P (decl) = true; > if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup)) > DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; > + if (DECL_IMPLICIT_INSTANTIATION (decl) > + || (DECL_CLASS_SCOPE_P (decl) > + && !DECL_VTABLE_OR_VTT_P (decl) > + && !DECL_TEMPLATE_INFO (decl))) > + note_vague_linkage_variable (decl); > } > DECL_INITIAL (decl) = init; > if (!dyn_init) > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > index 74013533b0f..f5bf159a879 100644 > --- a/gcc/cp/pt.cc > +++ b/gcc/cp/pt.cc > @@ -27300,7 +27300,7 @@ instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p) > { > set_instantiating_module (d); > if (variable_template_p (gen_tmpl)) > - note_variable_template_instantiation (d); > + note_vague_linkage_variable (d); > instantiate_body (td, args, d, false); > } > > diff --git a/gcc/testsuite/g++.dg/modules/init-4_a.C b/gcc/testsuite/g++.dg/modules/init-4_a.C > new file mode 100644 > index 00000000000..e0eb97b474e > --- /dev/null > +++ b/gcc/testsuite/g++.dg/modules/init-4_a.C > @@ -0,0 +1,9 @@ > +// PR c++/112899 > +// { dg-additional-options "-fmodules-ts" } > +// { dg-module-cmi M } > + > +export module M; > + > +export struct A { > + static constexpr int x = -1; > +}; > diff --git a/gcc/testsuite/g++.dg/modules/init-4_b.C b/gcc/testsuite/g++.dg/modules/init-4_b.C > new file mode 100644 > index 00000000000..d28017a1d14 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/modules/init-4_b.C > @@ -0,0 +1,11 @@ > +// PR c++/112899 > +// { dg-module-do run } > +// { dg-additional-options "-fmodules-ts" } > + > +import M; > + > +int main() { > + const int& x = A::x; > + if (x != -1) > + __builtin_abort(); > +} > diff --git a/gcc/testsuite/g++.dg/modules/init-6_a.H b/gcc/testsuite/g++.dg/modules/init-6_a.H > new file mode 100644 > index 00000000000..1c3fe4535d4 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/modules/init-6_a.H > @@ -0,0 +1,20 @@ > +// { dg-additional-options "-fmodule-header" } > +// { dg-module-cmi {} } > + > +template > +struct __from_chars_alnum_to_val_table { > + static inline int value = 42; > +}; > + > +inline unsigned char > +__from_chars_alnum_to_val() { > + return __from_chars_alnum_to_val_table::value; > +} > + > +template > +static inline int nonclass_value = 42; > + > +inline unsigned char > +get_nonclass_val() { > + return nonclass_value; > +} > diff --git a/gcc/testsuite/g++.dg/modules/init-6_b.C b/gcc/testsuite/g++.dg/modules/init-6_b.C > new file mode 100644 > index 00000000000..d704968ec37 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/modules/init-6_b.C > @@ -0,0 +1,9 @@ > +// { dg-module-do link } > +// { dg-additional-options "-fmodules-ts" } > + > +import "init-6_a.H"; > + > +int main() { > + __from_chars_alnum_to_val(); > + get_nonclass_val(); > +}