[libc-commits] [libc] [libc][POSIX] Add clock_settime() function for NVPTX targets (PR #168494)
via libc-commits
libc-commits at lists.llvm.org
Thu Feb 12 09:35:10 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-amdgpu
Author: Anton Shepelev (amemov)
<details>
<summary>Changes</summary>
Closes #<!-- -->168492
This PR additionally provides coverage for AMD GPUs. Similarly, the clock is not mutable in GCN/RDNA
---
Full diff: https://github.com/llvm/llvm-project/pull/168494.diff
12 Files Affected:
- (modified) libc/config/gpu/amdgpu/entrypoints.txt (+1)
- (modified) libc/config/gpu/nvptx/entrypoints.txt (+1)
- (modified) libc/docs/headers/time.rst (+1-1)
- (modified) libc/src/__support/time/gpu/CMakeLists.txt (+11)
- (added) libc/src/__support/time/gpu/clock_settime.cpp (+26)
- (modified) libc/src/time/gpu/CMakeLists.txt (+12)
- (modified) libc/src/time/gpu/clock.cpp (+1-1)
- (modified) libc/src/time/gpu/clock_gettime.cpp (+1-1)
- (added) libc/src/time/gpu/clock_settime.cpp (+25)
- (modified) libc/src/time/gpu/nanosleep.cpp (+1-1)
- (modified) libc/src/time/gpu/timespec_get.cpp (+1-1)
- (modified) libc/test/src/time/clock_settime_test.cpp (+8-1)
``````````diff
diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 0dda7d5c683ec..70c02dd94d86c 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -258,6 +258,7 @@ set(TARGET_LIBC_ENTRYPOINTS
# time.h entrypoints
libc.src.time.clock
libc.src.time.clock_gettime
+ libc.src.time.clock_settime
libc.src.time.timespec_get
libc.src.time.nanosleep
libc.src.time.strftime
diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 6070fb5b17b3c..eecfd21cc9af6 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -258,6 +258,7 @@ set(TARGET_LIBC_ENTRYPOINTS
# time.h entrypoints
libc.src.time.clock
libc.src.time.clock_gettime
+ libc.src.time.clock_settime
libc.src.time.timespec_get
libc.src.time.nanosleep
libc.src.time.strftime
diff --git a/libc/docs/headers/time.rst b/libc/docs/headers/time.rst
index f07e0d93a4ce6..4f4ec2ff0f297 100644
--- a/libc/docs/headers/time.rst
+++ b/libc/docs/headers/time.rst
@@ -71,7 +71,7 @@ Implementation Status
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| clock_nanosleep | | | | | | | | | | | | | |
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
-| clock_settime | |check| | |check| | | |check| | | | | | | | | | |
+| clock_settime | |check| | |check| | | |check| | | | | | | | | |check| | |check| |
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| ctime | |check| | |check| | | |check| | | | | | | | | | |
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
diff --git a/libc/src/__support/time/gpu/CMakeLists.txt b/libc/src/__support/time/gpu/CMakeLists.txt
index fc465e0cea25f..1e179836826bb 100644
--- a/libc/src/__support/time/gpu/CMakeLists.txt
+++ b/libc/src/__support/time/gpu/CMakeLists.txt
@@ -20,3 +20,14 @@ add_object_library(
libc.hdr.types.struct_timespec
.time_utils
)
+
+add_object_library(
+ clock_settime
+ SRCS
+ clock_settime.cpp
+ HDRS
+ ../clock_settime.h
+ DEPENDS
+ libc.hdr.types.clockid_t
+ libc.hdr.types.struct_timespec
+)
diff --git a/libc/src/__support/time/gpu/clock_settime.cpp b/libc/src/__support/time/gpu/clock_settime.cpp
new file mode 100644
index 0000000000000..0b9d81d8bd636
--- /dev/null
+++ b/libc/src/__support/time/gpu/clock_settime.cpp
@@ -0,0 +1,26 @@
+//===---------- GPU implementation of the clock_settime function ----------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/time/clock_settime.h"
+
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/time/clock_settime.h"
+#include "src/__support/time/gpu/time_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace internal {
+
+ErrorOr<int> clock_settime(clockid_t clockid, const timespec *ts) {
+ // GPU hardware clocks are read-only; setting is not supported.
+ (void)clockid;
+ (void)ts;
+ return -1;
+}
+} // namespace internal
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/gpu/CMakeLists.txt b/libc/src/time/gpu/CMakeLists.txt
index 31a60595d68ff..8e165b6034b77 100644
--- a/libc/src/time/gpu/CMakeLists.txt
+++ b/libc/src/time/gpu/CMakeLists.txt
@@ -48,3 +48,15 @@ add_entrypoint_object(
libc.src.__support.time.gpu.time_utils
libc.src.__support.time.clock_gettime
)
+
+add_entrypoint_object(
+ clock_settime
+ SRCS
+ clock_settime.cpp
+ HDRS
+ ../clock_settime.h
+ DEPENDS
+ libc.hdr.types.clockid_t
+ libc.hdr.types.struct_timespec
+ libc.src.__support.time.clock_settime
+)
diff --git a/libc/src/time/gpu/clock.cpp b/libc/src/time/gpu/clock.cpp
index 8609c5cd6b6b7..4e273866f160f 100644
--- a/libc/src/time/gpu/clock.cpp
+++ b/libc/src/time/gpu/clock.cpp
@@ -1,4 +1,4 @@
-//===-- GPU implementation of the clock function --------------------------===//
+//===---------- GPU implementation of the clock function ------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/src/time/gpu/clock_gettime.cpp b/libc/src/time/gpu/clock_gettime.cpp
index 81547ef7f1ca6..8099a569f46db 100644
--- a/libc/src/time/gpu/clock_gettime.cpp
+++ b/libc/src/time/gpu/clock_gettime.cpp
@@ -1,4 +1,4 @@
-//===---------- GPU implementation of the POSIX clock_gettime function ----===//
+//===---------- GPU implementation of the clock_gettime function ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/src/time/gpu/clock_settime.cpp b/libc/src/time/gpu/clock_settime.cpp
new file mode 100644
index 0000000000000..0505e553aade1
--- /dev/null
+++ b/libc/src/time/gpu/clock_settime.cpp
@@ -0,0 +1,25 @@
+//===---------- GPU implementation of the clock_settime function ----------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/time/clock_settime.h"
+
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/time/clock_settime.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, clock_settime,
+ (clockid_t clockid, const timespec *ts)) {
+ ErrorOr<int> result = internal::clock_settime(clockid, ts);
+ if (result)
+ return result.value();
+ return result.error();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/gpu/nanosleep.cpp b/libc/src/time/gpu/nanosleep.cpp
index d22d9d6bd8d79..3cccbc9055514 100644
--- a/libc/src/time/gpu/nanosleep.cpp
+++ b/libc/src/time/gpu/nanosleep.cpp
@@ -1,4 +1,4 @@
-//===-- GPU implementation of the nanosleep function ----------------------===//
+//===---------- GPU implementation of the nanosleep function --------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/src/time/gpu/timespec_get.cpp b/libc/src/time/gpu/timespec_get.cpp
index 0dd128444aa8e..7b24c9e5f71e9 100644
--- a/libc/src/time/gpu/timespec_get.cpp
+++ b/libc/src/time/gpu/timespec_get.cpp
@@ -1,4 +1,4 @@
-//===-- Implementation of timespec_get for gpu ----------------------------===//
+//===---------- GPU implementation of the timespec_get function -----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/test/src/time/clock_settime_test.cpp b/libc/test/src/time/clock_settime_test.cpp
index ccbad9ed2e847..bbdd8f29db93e 100644
--- a/libc/test/src/time/clock_settime_test.cpp
+++ b/libc/test/src/time/clock_settime_test.cpp
@@ -8,12 +8,12 @@
#include "hdr/time_macros.h"
#include "hdr/types/struct_timespec.h"
+#include "src/__support/macros/properties/architectures.h"
#include "src/time/clock_settime.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
#include "test/UnitTest/Test.h"
using LlvmLibcClockSetTime = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
-
#ifdef CLOCK_MONOTONIC
TEST_F(LlvmLibcClockSetTime, MonotonicIsNotSettable) {
timespec ts = {0, 0};
@@ -40,12 +40,19 @@ TEST_F(LlvmLibcClockSetTime, InvalidTimespecNsec) {
TEST_F(LlvmLibcClockSetTime, NullPointerIsEFAULT) {
int result = LIBC_NAMESPACE::clock_settime(CLOCK_REALTIME, nullptr);
ASSERT_EQ(result, -1);
+#ifdef LIBC_TARGET_ARCH_IS_GPU
+ ASSERT_ERRNO_EQ(EINVAL);
+#endif
ASSERT_ERRNO_EQ(EFAULT);
}
TEST_F(LlvmLibcClockSetTime, ClockIsSet) {
timespec ts = {0, 0};
int result = LIBC_NAMESPACE::clock_settime(CLOCK_REALTIME, &ts);
+#ifdef LIBC_TARGET_ARCH_IS_GPU
+ ASSERT_EQ(result, -1);
+ ASSERT_ERRNO_EQ(EINVAL);
+#endif
if (result == 0) {
ASSERT_ERRNO_SUCCESS();
} else {
``````````
</details>
https://github.com/llvm/llvm-project/pull/168494
More information about the libc-commits
mailing list