On 23/04/2025 20:49, Thomas Schwinge wrote:
'__dso_handle' for '__cxa_atexit', '__cxa_finalize'.  See
<https://itanium-cxx-abi.github.io/cxx-abi/abi.html#dso-dtor>.

        PR target/119853
        PR target/119854
        libgcc/
        * config/gcn/crt0.c (_fini_array): Call
        '__GCC_offload___cxa_finalize'.
        * config/nvptx/gbl-ctors.c (__static_do_global_dtors): Likewise.
        libgomp/
        * target-cxa-dso-dtor.c: New.
        * config/accel/target-cxa-dso-dtor.c: Likewise.
        * Makefile.am (libgomp_la_SOURCES): Add it.
        * Makefile.in: Regenerate.
        * testsuite/libgomp.c++/target-cdtor-1.C: New.
        * testsuite/libgomp.c++/target-cdtor-2.C: Likewise.
---
  libgcc/config/gcn/crt0.c                      |  32 ++++
  libgcc/config/nvptx/gbl-ctors.c               |  16 ++
  libgomp/Makefile.am                           |   2 +-
  libgomp/Makefile.in                           |   7 +-
  libgomp/config/accel/target-cxa-dso-dtor.c    |  62 ++++++++
  libgomp/target-cxa-dso-dtor.c                 |   3 +
  .../testsuite/libgomp.c++/target-cdtor-1.C    | 104 +++++++++++++
  .../testsuite/libgomp.c++/target-cdtor-2.C    | 138 ++++++++++++++++++
  8 files changed, 361 insertions(+), 3 deletions(-)
  create mode 100644 libgomp/config/accel/target-cxa-dso-dtor.c
  create mode 100644 libgomp/target-cxa-dso-dtor.c
  create mode 100644 libgomp/testsuite/libgomp.c++/target-cdtor-1.C
  create mode 100644 libgomp/testsuite/libgomp.c++/target-cdtor-2.C

diff --git a/libgcc/config/gcn/crt0.c b/libgcc/config/gcn/crt0.c
index dbd6749a47f..cc23e214cf9 100644
--- a/libgcc/config/gcn/crt0.c
+++ b/libgcc/config/gcn/crt0.c
@@ -24,6 +24,28 @@ typedef long long size_t;
  /* Provide an entry point symbol to silence a linker warning.  */
  void _start() {}
+
+#define PR119369_fixed 0
+
+
+/* Host/device compatibility: '__cxa_finalize'.  Dummy; if necessary,
+   overridden via libgomp 'target-cxa-dso-dtor.c'.  */
+
+#if PR119369_fixed
+extern void __GCC_offload___cxa_finalize (void *) __attribute__((weak));
+#else
+void __GCC_offload___cxa_finalize (void *) __attribute__((weak));
+
+void __attribute__((weak))
+__GCC_offload___cxa_finalize (void *dso_handle __attribute__((unused)))
+{
+}
+#endif
+
+/* There are no DSOs; this is the main program.  */
+static void * const __dso_handle = 0;
+
+
  #ifdef USE_NEWLIB_INITFINI
extern void __libc_init_array (void) __attribute__((weak));
@@ -38,6 +60,11 @@ void _init_array()
  __attribute__((amdgpu_hsa_kernel ()))
  void _fini_array()
  {
+#if PR119369_fixed
+  if (__GCC_offload___cxa_finalize)
+#endif
+    __GCC_offload___cxa_finalize (__dso_handle);
+
    __libc_fini_array ();
  }
@@ -70,6 +97,11 @@ void _init_array()
  __attribute__((amdgpu_hsa_kernel ()))
  void _fini_array()
  {
+#if PR119369_fixed
+  if (__GCC_offload___cxa_finalize)
+#endif
+    __GCC_offload___cxa_finalize (__dso_handle);
+
    size_t count;
    size_t i;


The GCN part seems OK, albeit the conditionals are confusing.

Andrew

Reply via email to