-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[libc][gpu] unify time implementation style and fix build #118864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-libc Author: Schrodinger ZHU Yifan (SchrodingerZhu) ChangesFull diff: https://github.com/llvm/llvm-project/pull/118864.diff 9 Files Affected:
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 {
|
Contributor
Author
|
This is be absorbed into relanding patch. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.