public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Nathaniel Shead <nshead@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-8987] c++: Defer emitting inline variables [PR113708]
Date: Wed, 14 Feb 2024 19:08:11 +0000 (GMT)	[thread overview]
Message-ID: <20240214190812.12B8B3858C36@sourceware.org> (raw)

https://gcc.gnu.org/g:dd9d14f7d53de07beff06004922a2bff20ece671

commit r14-8987-gdd9d14f7d53de07beff06004922a2bff20ece671
Author: Nathaniel Shead <nathanieloshead@gmail.com>
Date:   Wed Feb 14 12:26:03 2024 +1100

    c++: Defer emitting inline variables [PR113708]
    
    Inline variables are vague-linkage, and may or may not need to be
    emitted in any TU that they are part of, similarly to e.g. template
    instantiations.
    
    Currently 'import_export_decl' assumes that inline variables have
    already been emitted when it comes to end-of-TU processing, and so
    crashes when importing non-trivially-initialised variables from a
    module, as they have not yet been finalised.
    
    This patch fixes this by ensuring that inline variables are always
    deferred till end-of-TU processing, unifying the behaviour for module
    and non-module code.
    
            PR c++/113708
    
    gcc/cp/ChangeLog:
    
            * decl.cc (make_rtl_for_nonlocal_decl): Defer inline variables.
            * decl2.cc (import_export_decl): Support inline variables.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/debug/dwarf2/inline-var-1.C: Reference 'a' to ensure it
            is emitted.
            * g++.dg/debug/dwarf2/inline-var-3.C: Likewise.
            * g++.dg/modules/init-7_a.H: New test.
            * g++.dg/modules/init-7_b.C: New test.
    
    Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>

Diff:
---
 gcc/cp/decl.cc                                   | 4 ++++
 gcc/cp/decl2.cc                                  | 7 +++++--
 gcc/testsuite/g++.dg/debug/dwarf2/inline-var-1.C | 2 ++
 gcc/testsuite/g++.dg/debug/dwarf2/inline-var-3.C | 2 ++
 gcc/testsuite/g++.dg/modules/init-7_a.H          | 6 ++++++
 gcc/testsuite/g++.dg/modules/init-7_b.C          | 6 ++++++
 6 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index d19d09adde43..e47f694e4e56 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -7954,6 +7954,10 @@ make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
       && DECL_IMPLICIT_INSTANTIATION (decl))
     defer_p = 1;
 
+  /* Defer vague-linkage variables.  */
+  if (DECL_INLINE_VAR_P (decl))
+    defer_p = 1;
+
   /* If we're not deferring, go ahead and assemble the variable.  */
   if (!defer_p)
     rest_of_decl_compilation (decl, toplev, at_eof);
diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index f569d4045ecc..1dddbaab38b0 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -3360,7 +3360,9 @@ import_export_decl (tree decl)
 
      * implicit instantiations of function templates
 
-     * inline function
+     * inline functions
+
+     * inline variables
 
      * implicit instantiations of static data members of class
        templates
@@ -3383,6 +3385,7 @@ import_export_decl (tree decl)
 		|| DECL_DECLARED_INLINE_P (decl));
   else
     gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl)
+		|| DECL_INLINE_VAR_P (decl)
 		|| DECL_VTABLE_OR_VTT_P (decl)
 		|| DECL_TINFO_P (decl));
   /* Check that a definition of DECL is available in this translation
@@ -3511,7 +3514,7 @@ import_export_decl (tree decl)
 	   this entity as undefined in this translation unit.  */
 	import_p = true;
     }
-  else if (DECL_FUNCTION_MEMBER_P (decl))
+  else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (decl))
     {
       if (!DECL_DECLARED_INLINE_P (decl))
 	{
diff --git a/gcc/testsuite/g++.dg/debug/dwarf2/inline-var-1.C b/gcc/testsuite/g++.dg/debug/dwarf2/inline-var-1.C
index 85f74a91521f..7ec20afc065c 100644
--- a/gcc/testsuite/g++.dg/debug/dwarf2/inline-var-1.C
+++ b/gcc/testsuite/g++.dg/debug/dwarf2/inline-var-1.C
@@ -8,6 +8,8 @@
 // { dg-final { scan-assembler-times " DW_AT_\[^\n\r]*linkage_name" 7 } }
 
 inline int a;
+int& ar = a;
+
 struct S
 {
   static inline double b = 4.0;
diff --git a/gcc/testsuite/g++.dg/debug/dwarf2/inline-var-3.C b/gcc/testsuite/g++.dg/debug/dwarf2/inline-var-3.C
index 6345b5e2ec84..bb40a2295512 100644
--- a/gcc/testsuite/g++.dg/debug/dwarf2/inline-var-3.C
+++ b/gcc/testsuite/g++.dg/debug/dwarf2/inline-var-3.C
@@ -11,6 +11,8 @@
 // { dg-final { scan-assembler-times " DW_AT_\[^\n\r]*linkage_name" 7 } }
 
 inline int a;
+int& ar = a;
+
 struct S
 {
   static inline double b = 4.0;
diff --git a/gcc/testsuite/g++.dg/modules/init-7_a.H b/gcc/testsuite/g++.dg/modules/init-7_a.H
new file mode 100644
index 000000000000..7a0bb721c307
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/init-7_a.H
@@ -0,0 +1,6 @@
+// PR c++/113708
+// { dg-additional-options "-fmodule-header" }
+// { dg-module-cmi {} }
+
+inline int f() { return 42; }
+inline int a = f();
diff --git a/gcc/testsuite/g++.dg/modules/init-7_b.C b/gcc/testsuite/g++.dg/modules/init-7_b.C
new file mode 100644
index 000000000000..58bb0620ca51
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/init-7_b.C
@@ -0,0 +1,6 @@
+// PR c++/113708
+// { dg-module-do link }
+// { dg-additional-options "-fmodules-ts" }
+
+import "init-7_a.H";
+int main() { a; }

                 reply	other threads:[~2024-02-14 19:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240214190812.12B8B3858C36@sourceware.org \
    --to=nshead@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).