[libc-commits] [libc] [libc][gpu] unify time implementation style and fix build (PR #118864)
via libc-commits
libc-commits at lists.llvm.org
Thu Dec 5 12:03:53 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Schrodinger ZHU Yifan (SchrodingerZhu)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/118864.diff
9 Files Affected:
- (modified) libc/src/__support/time/CMakeLists.txt (+11-9)
- (added) libc/src/__support/time/gpu/CMakeLists.txt (+22)
- (added) libc/src/__support/time/gpu/clock_gettime.cpp (+33)
- (renamed) libc/src/__support/time/gpu/time_utils.cpp ()
- (renamed) libc/src/__support/time/gpu/time_utils.h ()
- (modified) libc/src/time/gpu/CMakeLists.txt (+4-26)
- (modified) libc/src/time/gpu/clock.cpp (+1-1)
- (modified) libc/src/time/gpu/clock_gettime.cpp (+6-11)
- (modified) libc/src/time/gpu/nanosleep.cpp (+1-1)
``````````diff
diff --git a/libc/src/__support/time/CMakeLists.txt b/libc/src/__support/time/CMakeLists.txt
index e73f2744b15a0e..8247e792e84105 100644
--- a/libc/src/__support/time/CMakeLists.txt
+++ b/libc/src/__support/time/CMakeLists.txt
@@ -1,5 +1,16 @@
+add_header_library(
+ units
+ HDRS
+ units.h
+ DEPENDS
+ libc.src.__support.common
+ libc.hdr.types.time_t
+)
+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
add_subdirectory(${LIBC_TARGET_OS})
+else()
+ return()
endif()
add_object_library(
@@ -8,12 +19,3 @@ add_object_library(
DEPENDS
libc.src.__support.time.${LIBC_TARGET_OS}.clock_gettime
)
-
-add_header_library(
- units
- HDRS
- units.h
- DEPENDS
- libc.src.__support.common
- libc.hdr.types.time_t
-)
diff --git a/libc/src/__support/time/gpu/CMakeLists.txt b/libc/src/__support/time/gpu/CMakeLists.txt
new file mode 100644
index 00000000000000..efa6cd32454cc0
--- /dev/null
+++ b/libc/src/__support/time/gpu/CMakeLists.txt
@@ -0,0 +1,22 @@
+add_object_library(
+ time_utils
+ SRCS
+ time_utils.cpp
+ HDRS
+ time_utils.h
+ DEPENDS
+ libc.hdr.types.clock_t
+ libc.hdr.time_macros
+)
+
+add_entrypoint_object(
+ clock_gettime
+ SRCS
+ clock_gettime.cpp
+ HDRS
+ ../clock_gettime.h
+ DEPENDS
+ libc.hdr.types.clockid_t
+ libc.hdr.types.struct_timespec
+ .time_utils
+)
diff --git a/libc/src/__support/time/gpu/clock_gettime.cpp b/libc/src/__support/time/gpu/clock_gettime.cpp
new file mode 100644
index 00000000000000..cede72a1f35da4
--- /dev/null
+++ b/libc/src/__support/time/gpu/clock_gettime.cpp
@@ -0,0 +1,33 @@
+//===---------- 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.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/time/clock_gettime.h"
+
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/time/clock_gettime.h"
+#include "src/__support/time/gpu/time_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace internal {
+constexpr uint64_t TICKS_PER_SEC = 1000000000UL;
+
+ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) {
+ if (clockid != CLOCK_MONOTONIC || !ts)
+ return cpp::unexpected(-1);
+
+ uint64_t ns_per_tick = TICKS_PER_SEC / GPU_CLOCKS_PER_SEC;
+ uint64_t ticks = gpu::fixed_frequency_clock();
+
+ ts->tv_nsec = (ticks * ns_per_tick) % TICKS_PER_SEC;
+ ts->tv_sec = (ticks * ns_per_tick) / TICKS_PER_SEC;
+
+ return 0;
+}
+} // namespace internal
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/time/gpu/time_utils.cpp b/libc/src/__support/time/gpu/time_utils.cpp
similarity index 100%
rename from libc/src/time/gpu/time_utils.cpp
rename to libc/src/__support/time/gpu/time_utils.cpp
diff --git a/libc/src/time/gpu/time_utils.h b/libc/src/__support/time/gpu/time_utils.h
similarity index 100%
rename from libc/src/time/gpu/time_utils.h
rename to libc/src/__support/time/gpu/time_utils.h
diff --git a/libc/src/time/gpu/CMakeLists.txt b/libc/src/time/gpu/CMakeLists.txt
index 8da5d3a22f5a09..8ad1f93c53e199 100644
--- a/libc/src/time/gpu/CMakeLists.txt
+++ b/libc/src/time/gpu/CMakeLists.txt
@@ -1,14 +1,3 @@
-add_object_library(
- time_utils
- SRCS
- time_utils.cpp
- HDRS
- time_utils.h
- DEPENDS
- libc.hdr.types.clock_t
- libc.hdr.time_macros
-)
-
add_entrypoint_object(
clock
SRCS
@@ -18,7 +7,8 @@ add_entrypoint_object(
DEPENDS
libc.include.time
libc.src.__support.GPU.utils
- .time_utils
+ libc.src.__support.time.clock_gettime
+ libc.src.__support.time.gpu.time_utils
)
add_entrypoint_object(
@@ -30,19 +20,7 @@ add_entrypoint_object(
DEPENDS
libc.include.time
libc.src.__support.GPU.utils
- .time_utils
-)
-
-add_entrypoint_object(
- clock_gettime
- SRCS
- clock_gettime.cpp
- HDRS
- ../clock_gettime.h
- DEPENDS
- libc.hdr.types.clockid_t
- libc.hdr.types.struct_timespec
- .time_utils
+ libc.src.__support.time.gpu.time_utils
)
add_entrypoint_object(
@@ -54,5 +32,5 @@ add_entrypoint_object(
DEPENDS
libc.hdr.time_macros
libc.hdr.types.struct_timespec
- .time_utils
+ libc.src.__support.time.gpu.time_utils
)
diff --git a/libc/src/time/gpu/clock.cpp b/libc/src/time/gpu/clock.cpp
index 4cdb1d505aed2b..add5b2725ef8f0 100644
--- a/libc/src/time/gpu/clock.cpp
+++ b/libc/src/time/gpu/clock.cpp
@@ -8,7 +8,7 @@
#include "src/time/clock.h"
#include "src/__support/macros/config.h"
-#include "src/time/gpu/time_utils.h"
+#include "src/__support/time/gpu/time_utils.h"
namespace LIBC_NAMESPACE_DECL {
diff --git a/libc/src/time/gpu/clock_gettime.cpp b/libc/src/time/gpu/clock_gettime.cpp
index de7899a2a17cc0..8feac294c68f07 100644
--- a/libc/src/time/gpu/clock_gettime.cpp
+++ b/libc/src/time/gpu/clock_gettime.cpp
@@ -10,23 +10,18 @@
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
-#include "time_utils.h"
+#include "src/__support/time/clock_gettime.h"
+#include "src/__support/time/gpu/time_utils.h"
namespace LIBC_NAMESPACE_DECL {
constexpr uint64_t TICKS_PER_SEC = 1000000000UL;
LLVM_LIBC_FUNCTION(int, clock_gettime, (clockid_t clockid, timespec *ts)) {
- if (clockid != CLOCK_MONOTONIC || !ts)
- return -1;
-
- uint64_t ns_per_tick = TICKS_PER_SEC / GPU_CLOCKS_PER_SEC;
- uint64_t ticks = gpu::fixed_frequency_clock();
-
- ts->tv_nsec = (ticks * ns_per_tick) % TICKS_PER_SEC;
- ts->tv_sec = (ticks * ns_per_tick) / TICKS_PER_SEC;
-
- return 0;
+ ErrorOr<int> result = internal::clock_gettime(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 3f4a609dd40eba..25a22d5703fa78 100644
--- a/libc/src/time/gpu/nanosleep.cpp
+++ b/libc/src/time/gpu/nanosleep.cpp
@@ -9,7 +9,7 @@
#include "src/time/nanosleep.h"
#include "src/__support/macros/config.h"
-#include "time_utils.h"
+#include "src/__support/time/gpu/time_utils.h"
namespace LIBC_NAMESPACE_DECL {
``````````
</details>
https://github.com/llvm/llvm-project/pull/118864
More information about the libc-commits
mailing list