On 14/03/25 11:39 +0100, Thomas Schwinge wrote:
Hi!

As the first of a few patches to enable libstdc++ for GCN, nvptx targets,
and eventually for OpenACC, OpenMP offloading use, I intend to push the
attached 'GCN, nvptx: Allow for "hosted" libstdc++ build'.  Any
objections?  It's not exactly pretty, but gets the job done.  (..., and
some more fine-tuning is to follow later on.)


Grüße
Thomas



From cb01ed054f767511102264977ad5b33811c8bec5 Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <tschwi...@baylibre.com>
Date: Thu, 20 Feb 2025 16:31:50 +0100
Subject: [PATCH] GCN, nvptx: Allow for "hosted" libstdc++ build

We need '-fno-exceptions', '-fno-rtti', disable generation of PCH files, and
for nvptx twiddle some more knobs.

        PR target/92713
        PR target/101544
        libstdc++-v3/
        * config/cpu/nvptx/cpu_defines.h: New.
        * config/cpu/nvptx/t-nvptx: Likewise.
        * configure.host: Handle GCN, nvptx.
---
libstdc++-v3/config/cpu/nvptx/cpu_defines.h | 36 ++++++++++++++++++++
libstdc++-v3/config/cpu/nvptx/t-nvptx       |  7 ++++
libstdc++-v3/configure.host                 | 37 +++++++++++++++++++++
3 files changed, 80 insertions(+)
create mode 100644 libstdc++-v3/config/cpu/nvptx/cpu_defines.h
create mode 100644 libstdc++-v3/config/cpu/nvptx/t-nvptx

diff --git a/libstdc++-v3/config/cpu/nvptx/cpu_defines.h 
b/libstdc++-v3/config/cpu/nvptx/cpu_defines.h
new file mode 100644
index 00000000000..65744e08527
--- /dev/null
+++ b/libstdc++-v3/config/cpu/nvptx/cpu_defines.h
@@ -0,0 +1,36 @@
+// Specific definitions for nvptx platforms  -*- C++ -*-
+
+// Copyright (C) 2025 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
+
+/** @file bits/cpu_defines.h
+ *  This is an internal header file, included by other library headers.
+ *  Do not attempt to use it directly. @headername{iosfwd}

I have no idea why we suggest <iosfwd> here, that makes no sense to
me. But it's present in config/cpu/generic/cpu_defines.h and other
files, so ... fine, I guess.


+ */
+
+#ifndef _GLIBCXX_CPU_DEFINES
+#define _GLIBCXX_CPU_DEFINES 1
+
+// No support for referencing weak symbols without a definition.
+#define _GLIBCXX_USE_WEAK_REF 0
+
+#endif
diff --git a/libstdc++-v3/config/cpu/nvptx/t-nvptx 
b/libstdc++-v3/config/cpu/nvptx/t-nvptx
new file mode 100644
index 00000000000..a2f0f2dcb60
--- /dev/null
+++ b/libstdc++-v3/config/cpu/nvptx/t-nvptx
@@ -0,0 +1,7 @@
+# Per-file flags, see '../../../configure.host', "inject per-file flags".
+
+# 'ptxas'/CUDA Driver rejects objects with a lot of global constant data:
+#     ptxas error   : File uses too much global constant data ([...])
+# Cut short the assembly-time check; defer to actual use of the object file.
+AM_MAKEFLAGS += CXXFLAGS-src/c++17/floating_to_chars.lo=-Wa,--no-verify
+AM_MAKEFLAGS += CXXFLAGS-src/c++20/tzdb.lo=-Wa,--no-verify

Are you using --with-libstdcxx-zoneinfo=no for nvptx? If you're not,
you might want to edit GLIBCXX_ZONEINFO_DIR in acinclude.m4 so that
embed_zoneinfo=no is used for this target.

That will prevent embedding the "tzdata.zi.h" file, which is probably
the reason there's too much global constant data in tzdb.o (because
otherwise it's just one empty struct, one shared_ptr, and one
atomic<T*>, which isn't a lot.

The patch is OK, but I'd like to know if it is "tzdata.zi.h" that's
too large, and if you can disable it (i.e. is GPU code going to need
to access the time zone database for non-UTC time zones?)


diff --git a/libstdc++-v3/configure.host b/libstdc++-v3/configure.host
index 933a43f241c..cb4c28a62bf 100644
--- a/libstdc++-v3/configure.host
+++ b/libstdc++-v3/configure.host
@@ -150,6 +150,9 @@ case "${host_cpu}" in
  arm*)
    cpu_defines_dir=cpu/arm
    ;;
+  nvptx)
+    cpu_defines_dir=cpu/nvptx
+    ;;
  powerpc* | rs6000)
    cpu_defines_dir=cpu/powerpc
    ;;
@@ -367,6 +370,16 @@ case "${host}" in
  arm*-*-freebsd*)
     
port_specific_symbol_files="\$(srcdir)/../config/os/gnu-linux/arm-eabi-extra.ver"
     ;;
+  nvptx-*-none)
+    # For 'make all-target-libstdc++-v3', we need to inject per-file flags:
+    OPTIMIZE_CXXFLAGS="${OPTIMIZE_CXXFLAGS} \$(CXXFLAGS-\$(subdir)/\$@)"
+    # ..., see:
+    tmake_file="$tmake_file cpu/nvptx/t-nvptx"
+
+    # For 'make all-target-libstdc++-v3', re 'alloca'/VLA usage:
+    EXTRA_CFLAGS="${EXTRA_CFLAGS} -mfake-ptx-alloca"
+    OPTIMIZE_CXXFLAGS="${OPTIMIZE_CXXFLAGS} -mfake-ptx-alloca"
+    ;;
  powerpc*-*-darwin*)
    
port_specific_symbol_files="\$(srcdir)/../config/os/bsd/darwin/ppc-extra.ver"
    ;;
@@ -383,3 +396,27 @@ case "${host}" in
    abi_baseline_subdir_switch=--print-multi-os-directory
    ;;
esac
+
+
+# Dumb down libstdc++ for certain configurations.
+# THIS TABLE IS SORTED.  KEEP IT THAT WAY.
+case "${host}" in
+  amdgcn-*-amdhsa \
+  | nvptx-*-none )
+    # For 'make all-target-libstdc++-v3' and 'make check-target-libstdc++-v3',
+    # exception handling is not supported: in case that exception handling
+    # constructs survive compiler optimizations, the back ends error out:
+    #     sorry, unimplemented: exception handling not supported
+    EXTRA_CXX_FLAGS="${EXTRA_CXX_FLAGS} -fno-exceptions"
+
+    # For 'make all-target-libstdc++-v3' and 'make check-target-libstdc++-v3',
+    # RTTI is not supported.
+    EXTRA_CXX_FLAGS="${EXTRA_CXX_FLAGS} -fno-rtti"
+
+    # Cannot '#include' all newlib header files without explosion, for example:
+    #     error: 'jmp_buf' was not declared in this scope
+    # Just disable generation of PCH files here, instead of attempting to care
+    # for such special cases in some more complex way.
+    enable_libstdcxx_pch=no
+    ;;
+esac
--
2.34.1


Reply via email to