[libc-commits] [libc] [libc][POSIX] Add clock_settime() function for NVPTX targets (PR #168494)
Anton Shepelev via libc-commits
libc-commits at lists.llvm.org
Thu Feb 12 09:32:49 PST 2026
https://github.com/amemov updated https://github.com/llvm/llvm-project/pull/168494
>From 66004d397ab93996a400bee25e8d571e6af4ebfd Mon Sep 17 00:00:00 2001
From: amemov <shepelev777 at gmail.com>
Date: Mon, 17 Nov 2025 23:05:07 -0800
Subject: [PATCH 1/3] WIP: Created file for GPU implementation
---
libc/docs/headers/time.rst | 2 +-
libc/src/time/gpu/clock_settime.cpp | 7 +++++++
2 files changed, 8 insertions(+), 1 deletion(-)
create mode 100644 libc/src/time/gpu/clock_settime.cpp
diff --git a/libc/docs/headers/time.rst b/libc/docs/headers/time.rst
index f07e0d93a4ce6..d1030ca339a61 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| |
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
| ctime | |check| | |check| | | |check| | | | | | | | | | |
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
diff --git a/libc/src/time/gpu/clock_settime.cpp b/libc/src/time/gpu/clock_settime.cpp
new file mode 100644
index 0000000000000..f54d3cc3aa213
--- /dev/null
+++ b/libc/src/time/gpu/clock_settime.cpp
@@ -0,0 +1,7 @@
+//===-- Implementation of clock_settime for gpu ----------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
>From 0463988a301d7bc5bc2e43b9a9db348d8e8aacd1 Mon Sep 17 00:00:00 2001
From: amemov <shepelev777 at gmail.com>
Date: Mon, 17 Nov 2025 23:23:30 -0800
Subject: [PATCH 2/3] WIP: Renamed top of headers for consistency
---
libc/src/time/gpu/clock.cpp | 2 +-
libc/src/time/gpu/clock_gettime.cpp | 2 +-
libc/src/time/gpu/clock_settime.cpp | 2 +-
libc/src/time/gpu/nanosleep.cpp | 2 +-
libc/src/time/gpu/timespec_get.cpp | 2 +-
5 files changed, 5 insertions(+), 5 deletions(-)
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
index f54d3cc3aa213..3354cdfeb0cf0 100644
--- a/libc/src/time/gpu/clock_settime.cpp
+++ b/libc/src/time/gpu/clock_settime.cpp
@@ -1,4 +1,4 @@
-//===-- Implementation of clock_settime for gpu ----------------------------===//
+//===---------- 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.
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.
>From d819acacdd50c1dc1d6bb3b9c1642b0d6cac9b1a Mon Sep 17 00:00:00 2001
From: amemov <shepelev777 at gmail.com>
Date: Thu, 12 Feb 2026 09:32:33 -0800
Subject: [PATCH 3/3] Implemented clock_settime for NVIDIA and AMD GPUs
---
libc/config/gpu/amdgpu/entrypoints.txt | 1 +
libc/config/gpu/nvptx/entrypoints.txt | 1 +
libc/docs/headers/time.rst | 2 +-
libc/src/__support/time/gpu/CMakeLists.txt | 11 ++++++++
libc/src/__support/time/gpu/clock_settime.cpp | 26 +++++++++++++++++++
libc/src/time/gpu/CMakeLists.txt | 12 +++++++++
libc/src/time/gpu/clock_settime.cpp | 18 +++++++++++++
libc/test/src/time/clock_settime_test.cpp | 9 ++++++-
8 files changed, 78 insertions(+), 2 deletions(-)
create mode 100644 libc/src/__support/time/gpu/clock_settime.cpp
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 d1030ca339a61..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| | | | | | | | | | |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_settime.cpp b/libc/src/time/gpu/clock_settime.cpp
index 3354cdfeb0cf0..0505e553aade1 100644
--- a/libc/src/time/gpu/clock_settime.cpp
+++ b/libc/src/time/gpu/clock_settime.cpp
@@ -5,3 +5,21 @@
// 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/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 {
More information about the libc-commits
mailing list