Tested on x86_64-darwin, Windows (by the OP) and x86_64/aarch64-linux, OK for
trunk / 16?
thanks
Iain

--- 8< ---

At -Os, -fdeclone-ctor-dtor is enabled, which splits a cdtor into a unified
body (C4) plus C1/C2 thunks.

A TU that inlines a thunk therefore materialises a call to a C4 whose own
offline body is DECL_EXTERNAL (but emitted by nobody), and the link fails with
an undefined reference.

This fires on targets without support for COMDAT or where ONE_ONLY decls are
not marked as DECL_WEAK.  The solution is to decline the inline of such thunks
if they would leave a reference to an un-emitted C4.

Some testcase input from an LLM.
Assisted-by: Claude Opus 5.5

        PR c++/125359

gcc/ChangeLog:

        * cgraph.h: Add flag for no external decl.
        * ipa-inline.cc (can_inline_edge_p): Refuse to inline thunks that point
        to an absent target.
        * lto-cgraph.cc (lto_output_node): Handle  no_external_definition.
        (lto_output_varpool_node): Likewise.
        (input_overwrite_node): Likewise.
        (input_varpool_node): Likewise.

gcc/cp/ChangeLog:

        * optimize.cc (maybe_thunk_body): Mark cases where a thunkee will
        have no availabile def, and therefore the thunk should not be inlined.

gcc/testsuite/ChangeLog:

        * g++.dg/opt/declone6-aux.cc: New test.
        * g++.dg/opt/declone6.C: New test.
        * g++.dg/opt/declone6.h: New test.

Signed-off-by: Iain Sandoe <[email protected]>
---
 gcc/cgraph.h                                 |  6 +-
 gcc/cp/optimize.cc                           | 12 ++++
 gcc/ipa-inline.cc                            | 14 ++++
 gcc/lto-cgraph.cc                            |  4 ++
 gcc/testsuite/g++.dg/opt/declone-cdtor-lto.C | 69 ++++++++++++++++++++
 gcc/testsuite/g++.dg/opt/declone6-aux.cc     |  8 +++
 gcc/testsuite/g++.dg/opt/declone6.C          | 61 +++++++++++++++++
 gcc/testsuite/g++.dg/opt/declone6.h          | 26 ++++++++
 8 files changed, 199 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/opt/declone-cdtor-lto.C
 create mode 100644 gcc/testsuite/g++.dg/opt/declone6-aux.cc
 create mode 100644 gcc/testsuite/g++.dg/opt/declone6.C
 create mode 100644 gcc/testsuite/g++.dg/opt/declone6.h

diff --git a/gcc/cgraph.h b/gcc/cgraph.h
index cad7fb3c127..2bed061b3ff 100644
--- a/gcc/cgraph.h
+++ b/gcc/cgraph.h
@@ -149,7 +149,8 @@ public:
       must_remain_in_tu_name (false), must_remain_in_tu_body (false),
       unique_name (false), implicit_section (false), body_removed (false),
       semantic_interposition (flag_semantic_interposition),
-      used_from_other_partition (false), in_other_partition (false),
+      used_from_other_partition (false), no_external_definition (false),
+      in_other_partition (false),
       address_taken (false), in_init_priority_hash (false),
       need_lto_streaming (false), offloadable (false), ifunc_resolver (false),
       next_sharing_asm_name (NULL),
@@ -622,6 +623,9 @@ public:
 
   /* Set when variable is used from other LTRANS partition.  */
   unsigned used_from_other_partition : 1;
+  /* Set when no definition of this symbol is guaranteed to exist in any
+     translation unit.  */
+  unsigned no_external_definition : 1;
   /* Set when function is available in the other LTRANS partition.
      During WPA output it is used to mark nodes that are present in
      multiple partitions.  */
diff --git a/gcc/cp/optimize.cc b/gcc/cp/optimize.cc
index 0afeefd5704..83a3d7ba804 100644
--- a/gcc/cp/optimize.cc
+++ b/gcc/cp/optimize.cc
@@ -295,6 +295,8 @@ maybe_thunk_body (tree fn, bool force)
   DECL_ABSTRACT_P (fn) = false;
   if (!DECL_WEAK (fn))
     {
+      if (DECL_ONE_ONLY (fn))
+       symtab_node::get_create (fn)->no_external_definition = true;
       TREE_PUBLIC (fn) = false;
       DECL_EXTERNAL (fn) = false;
       DECL_INTERFACE_KNOWN (fn) = true;
@@ -327,6 +329,16 @@ maybe_thunk_body (tree fn, bool force)
       DECL_WEAK (fn) = false;
       DECL_COMDAT (fn) = false;
     }
+  else
+    {
+      /* Neither case above could claim the unified body for this TU, so keep
+        vague linkage and may end up DECL_EXTERNAL.  However, no other TU
+        will necessarily emit it.  */
+      symtab_node::get_create (fn)->no_external_definition = true;
+      /* It is an implementation detail of this TU.  */
+      DECL_VISIBILITY (fn) = VISIBILITY_HIDDEN;
+      DECL_VISIBILITY_SPECIFIED (fn) = 1;
+    }
 
   /* Find the vtt_parm, if present.  */
   for (vtt_parmno = -1, parmno = 0, fn_parm = DECL_ARGUMENTS (fn);
diff --git a/gcc/ipa-inline.cc b/gcc/ipa-inline.cc
index ba8bf11b66e..012c5f4301a 100644
--- a/gcc/ipa-inline.cc
+++ b/gcc/ipa-inline.cc
@@ -463,6 +463,20 @@ can_inline_edge_p (struct cgraph_edge *e, bool report,
       e->inline_failed = CIF_MUST_REMAIN_IN_TU;
       inlinable = false;
     }
+  /* Do not inline a body that still calls a target with no guaranteed
+     definition: the call would be duplicated into the caller, while the
+     callee's own body is DECL_EXTERNAL and so emitted by nobody.
+     However, allow always-inline.  */
+  if (inlinable && !DECL_DISREGARD_INLINE_LIMITS (callee->decl))
+    for (cgraph_edge *e2 = callee->callees; e2; e2 = e2->next_callee)
+      if (e2->callee->no_external_definition
+         && DECL_EXTERNAL (e2->callee->decl))
+       {
+         e->inline_failed = CIF_BODY_NOT_AVAILABLE;
+         inlinable = false;
+         break;
+       }
+
   if (!inlinable && report)
     report_inline_failed_reason (e);
   return inlinable;
diff --git a/gcc/lto-cgraph.cc b/gcc/lto-cgraph.cc
index b3644e47bec..f3b0d9ecd65 100644
--- a/gcc/lto-cgraph.cc
+++ b/gcc/lto-cgraph.cc
@@ -542,6 +542,7 @@ lto_output_node (struct lto_simple_output_block *ob, struct 
cgraph_node *node,
   bp_pack_value (&bp, node->must_remain_in_tu_body, 1);
   bp_pack_value (&bp, node->unique_name, 1);
   bp_pack_value (&bp, node->body_removed, 1);
+  bp_pack_value (&bp, node->no_external_definition, 1);
   bp_pack_value (&bp, node->semantic_interposition, 1);
   bp_pack_value (&bp, node->implicit_section, 1);
   bp_pack_value (&bp, node->address_taken, 1);
@@ -636,6 +637,7 @@ lto_output_varpool_node (struct lto_simple_output_block 
*ob, varpool_node *node,
                 node->body_removed
                 || (!encode_initializer_p && !node->alias && node->definition),
                 1);
+  bp_pack_value (&bp, node->no_external_definition, 1);
   bp_pack_value (&bp, node->semantic_interposition, 1);
   bp_pack_value (&bp, node->implicit_section, 1);
   bp_pack_value (&bp, node->writeonly, 1);
@@ -1269,6 +1271,7 @@ input_overwrite_node (struct lto_file_decl_data 
*file_data,
   node->must_remain_in_tu_body = bp_unpack_value (bp, 1);
   node->unique_name = bp_unpack_value (bp, 1);
   node->body_removed = bp_unpack_value (bp, 1);
+  node->no_external_definition = bp_unpack_value (bp, 1);
   node->semantic_interposition = bp_unpack_value (bp, 1);
   node->implicit_section = bp_unpack_value (bp, 1);
   node->address_taken = bp_unpack_value (bp, 1);
@@ -1479,6 +1482,7 @@ input_varpool_node (struct lto_file_decl_data *file_data,
   node->must_remain_in_tu_body = bp_unpack_value (&bp, 1);
   node->unique_name = bp_unpack_value (&bp, 1);
   node->body_removed = bp_unpack_value (&bp, 1);
+  node->no_external_definition = bp_unpack_value (&bp, 1);
   node->semantic_interposition = bp_unpack_value (&bp, 1);
   node->implicit_section = bp_unpack_value (&bp, 1);
   node->writeonly = bp_unpack_value (&bp, 1);
diff --git a/gcc/testsuite/g++.dg/opt/declone-cdtor-lto.C 
b/gcc/testsuite/g++.dg/opt/declone-cdtor-lto.C
new file mode 100644
index 00000000000..59ad5e698f6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/declone-cdtor-lto.C
@@ -0,0 +1,69 @@
+// PR c++/125359
+
+// Check that we do not leave a call to a unified cdtor in an LTO build.
+// { dg-do run { target c++14 } }
+// { dg-require-effective-target lto }
+// { dg-options "-Os -flto -fstrict-aliasing" }
+// { dg-skip-if "requires hosted libstdc++ for cassert" { ! hostedlib } }
+
+#include <cassert>
+#include <memory>
+#include <string>
+
+class Base
+{
+public:
+  Base* previous = nullptr;
+  Base* next = nullptr;
+  Base* target = nullptr;
+};
+
+class Target : public Base
+{
+public:
+  __attribute__((always_inline)) ~Target()
+  {
+    while (this->next)
+    {
+      Base* n = this->next;
+
+      if (n->previous)
+        n->previous->next = n->next;
+      if (n->next)
+        n->next->previous = n->previous;
+      n->previous = nullptr;
+      n->next = nullptr;
+      n->target = nullptr;
+    }
+  }
+};
+
+template <typename T>
+class TargetWithData final : public Target
+{
+public:
+  TargetWithData(T data)
+    : data(data)
+  {}
+  T data;
+};
+
+void test()
+{
+  Base ptr;
+  {
+    auto data = 
std::make_unique<TargetWithData<std::string>>(std::string("asdf"));
+    ptr.target = &*data;
+    ptr.previous = &*data;
+    data->next = &ptr;
+
+    assert(ptr.target != nullptr);
+  }
+  assert(ptr.target == nullptr);
+}
+
+int main(int, char**)
+{
+  test();
+  return 0;
+}
diff --git a/gcc/testsuite/g++.dg/opt/declone6-aux.cc 
b/gcc/testsuite/g++.dg/opt/declone6-aux.cc
new file mode 100644
index 00000000000..3cbe4de1234
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/declone6-aux.cc
@@ -0,0 +1,8 @@
+// Emitter for declone6.C.  It includes the same header, so it sees
+// the `extern template' declaration, and then overrides it with an explicit
+// instantiation def -- which [temp.explicit] permits provided the
+// definition follows the declaration, as it does here.
+
+#include "declone6.h"
+
+template struct B<int>;                        // the override: emit them HERE
diff --git a/gcc/testsuite/g++.dg/opt/declone6.C 
b/gcc/testsuite/g++.dg/opt/declone6.C
new file mode 100644
index 00000000000..a81188f911b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/declone6.C
@@ -0,0 +1,61 @@
+// PR c++/125359
+
+// declone6.h holds inline definitions plus `extern template struct B<int>;', 
so the
+// bodies are VISIBLE in both TUs while neither is told to emit them.
+// declone6-aux.cc overrides that with an explicit instantiation DEFINITION, 
which
+// [temp.explicit] permits after the declaration, making it the one designated
+// emitter.  The program is therefore exactly what the standard asks for.
+//
+// At -Os the clones become forwarding thunks to a unified C4 body; if a thunk 
is
+// inlined here, the call to that C4 must not be left behind, because nothing 
in the
+// source can name a C4 and so no TU can be asked to export one.
+//
+// { dg-do run { target c++20 } }
+// { dg-options "-Os" }
+// { dg-additional-sources "declone6-aux.cc" }
+
+#include "declone6.h"
+
+struct D : B<int>                      // derived: uses B<int>::C2 
(base-object ctor)
+{
+  D (int x) : B<int> (x) {}
+};
+
+struct Sink
+{
+  __attribute__((always_inline)) ~Sink () { total += v; }
+  static int total;
+  int v;
+};
+int Sink::total;
+
+template <typename T>
+struct Holder : Sink
+{
+  T held;
+  Holder (const T &x) : held (x) { v = held.n; }
+};
+
+B<int> *p;
+D *q;
+
+__attribute__((noinline)) static int
+use (int x)
+{
+  p = new B<int> (x);                  // uses B<int>::C1 (complete-object 
ctor)
+  q = new D (x);
+  {
+    B<int> t (x);
+    Holder<B<int> > h (t);             // needs B<int>'s copy cdtor HERE, by 
value
+    (void) h;
+  }
+  return p->n + q->n;
+}
+
+int
+main ()
+{
+  int a = use (2);
+  int b = use (2);
+  return !(a == b && Sink::total != 0);
+}
diff --git a/gcc/testsuite/g++.dg/opt/declone6.h 
b/gcc/testsuite/g++.dg/opt/declone6.h
new file mode 100644
index 00000000000..d2e210005af
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/declone6.h
@@ -0,0 +1,26 @@
+// Shared "header" for declone6.C and declone6-aux.cc.  The definitions must be
+// inline to live here, which is the point: the explicit instantiation decl
+// below tells every includer not to emit them, and exactly one TU overrides 
it with
+// an explicit instantiation def to become the designated emitter,
+// declone6-aux.cc
+//
+// Shaped after std::basic_string, which triggered this case.
+
+template <typename T>
+struct B
+{
+  T n;
+
+  constexpr B (T x) : n (x)
+  {
+    for (int i = 0; i < 16; i++)
+      n += (x ^ i) + (n >> 1);
+  }
+
+  constexpr B (const B &o) : n (o.n) { n += 1; }
+
+  constexpr ~B () { n = 0; }
+};
+
+// The declaration: "do not instantiate the definitions here."
+extern template struct B<int>;
-- 
2.50.1 (Apple Git-155)

Reply via email to