[libcxx-commits] [libcxx] [libcxx] Set `_LIBCPP_HAS_CLOCK_GETTIME` for GPU targets (PR #99243)

Joseph Huber via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 16 15:14:38 PDT 2024


https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/99243

>From b15d7edb1db33cfdb4939f7dca3209b26c1c4cdb Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Tue, 16 Jul 2024 16:20:24 -0500
Subject: [PATCH] [libcxx] Set `_LIBCPP_HAS_CLOCK_GETTIME` for GPU targets

Summary:
I am attempting to get the GPU to build and support libc++. One issue
I've encountered is that it will look for `timeval` unless this macro is
set. We can support `CLOCK_MONOTONIC` on the GPU fairly easily as we
have access to a fixed-frequency clock via `__builtin_readsteadycounter`
intrinsics with a known frequency. This also requires `CLOCK_REALTIME`
which we can't support, but provide anyway from the GPU `libc` to make
this happy. It will return an error so at least that will be obvious.

I may need a more consistent configuration for this in the future, maybe
I should put a common macro in a different common header that's just
`__GPU__`? I don't know where I would put such a thing however.
---
 libcxx/include/__config                    | 5 +++++
 libcxx/src/chrono.cpp                      | 4 +++-
 libcxx/src/filesystem/filesystem_clock.cpp | 3 ++-
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/libcxx/include/__config b/libcxx/include/__config
index 108f700823cbf..2a45077cdf4f6 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -1223,6 +1223,11 @@ typedef __char32_t char32_t;
 #    define _LIBCPP_HAS_EXPLICIT_THIS_PARAMETER
 #  endif
 
+// Clang supports targeting GPU architectures.
+#  if (defined(__AMDGPU__) || defined(__NVPTX__)) && defined(_LIBCPP_COMPILER_CLANG_BASED)
+#    define _LIBCPP_TARGETING_GPU
+#endif
+
 #endif // __cplusplus
 
 #endif // _LIBCPP___CONFIG
diff --git a/libcxx/src/chrono.cpp b/libcxx/src/chrono.cpp
index 83e8a64504ae0..e8080cdc9ed86 100644
--- a/libcxx/src/chrono.cpp
+++ b/libcxx/src/chrono.cpp
@@ -12,6 +12,7 @@
 #  define _LARGE_TIME_API
 #endif
 
+#include <__config>
 #include <__system_error/system_error.h>
 #include <cerrno> // errno
 #include <chrono>
@@ -33,7 +34,8 @@
 
 // OpenBSD does not have a fully conformant suite of POSIX timers, but
 // it does have clock_gettime and CLOCK_MONOTONIC which is all we need.
-#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__OpenBSD__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
+#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__OpenBSD__) || defined(_LIBCPP_TARGETING_GPU) ||           \
+    (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
 #  define _LIBCPP_HAS_CLOCK_GETTIME
 #endif
 
diff --git a/libcxx/src/filesystem/filesystem_clock.cpp b/libcxx/src/filesystem/filesystem_clock.cpp
index e13b2853e367c..4af35165f8853 100644
--- a/libcxx/src/filesystem/filesystem_clock.cpp
+++ b/libcxx/src/filesystem/filesystem_clock.cpp
@@ -29,7 +29,8 @@
 #  include <sys/time.h> // for gettimeofday and timeval
 #endif
 
-#if defined(__APPLE__) || defined(__gnu_hurd__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
+#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(_LIBCPP_TARGETING_GPU) ||                                   \
+    (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
 #  define _LIBCPP_HAS_CLOCK_GETTIME
 #endif
 



More information about the libcxx-commits mailing list