Skip to content

Commit b15d7ed

Browse files
committed
[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.
1 parent 8393ea5 commit b15d7ed

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

libcxx/include/__config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,11 @@ typedef __char32_t char32_t;
12231223
# define _LIBCPP_HAS_EXPLICIT_THIS_PARAMETER
12241224
# endif
12251225

1226+
// Clang supports targeting GPU architectures.
1227+
# if (defined(__AMDGPU__) || defined(__NVPTX__)) && defined(_LIBCPP_COMPILER_CLANG_BASED)
1228+
# define _LIBCPP_TARGETING_GPU
1229+
#endif
1230+
12261231
#endif // __cplusplus
12271232

12281233
#endif // _LIBCPP___CONFIG

libcxx/src/chrono.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# define _LARGE_TIME_API
1313
#endif
1414

15+
#include <__config>
1516
#include <__system_error/system_error.h>
1617
#include <cerrno> // errno
1718
#include <chrono>
@@ -33,7 +34,8 @@
3334

3435
// OpenBSD does not have a fully conformant suite of POSIX timers, but
3536
// it does have clock_gettime and CLOCK_MONOTONIC which is all we need.
36-
#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__OpenBSD__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
37+
#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__OpenBSD__) || defined(_LIBCPP_TARGETING_GPU) || \
38+
(defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
3739
# define _LIBCPP_HAS_CLOCK_GETTIME
3840
#endif
3941

libcxx/src/filesystem/filesystem_clock.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
# include <sys/time.h> // for gettimeofday and timeval
3030
#endif
3131

32-
#if defined(__APPLE__) || defined(__gnu_hurd__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
32+
#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(_LIBCPP_TARGETING_GPU) || \
33+
(defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
3334
# define _LIBCPP_HAS_CLOCK_GETTIME
3435
#endif
3536

0 commit comments

Comments
 (0)