[libc-commits] [libc] ca10bc4 - [libc] Implement the 'nanosleep' function on the GPU

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Wed Aug 30 16:35:07 PDT 2023


Author: Joseph Huber
Date: 2023-08-30T18:34:59-05:00
New Revision: ca10bc4f41481d09c5495a01b2432e04580a2d61

URL: https://github.com/llvm/llvm-project/commit/ca10bc4f41481d09c5495a01b2432e04580a2d61
DIFF: https://github.com/llvm/llvm-project/commit/ca10bc4f41481d09c5495a01b2432e04580a2d61.diff

LOG: [libc] Implement the 'nanosleep' function on the GPU

The GPU has the ability to sleep for very short periods of time. We can
map this to the existing `nanosleep` utility. This patch maps the
nanosleep utility to the existing hardware instructions as best as
possible.

Depends on D159118

Reviewed By: JonChesterfield, sivachandra

Differential Revision: https://reviews.llvm.org/D159225

Added: 
    libc/src/time/gpu/nanosleep.cpp
    libc/src/time/linux/nanosleep.cpp

Modified: 
    libc/config/gpu/api.td
    libc/config/gpu/entrypoints.txt
    libc/docs/gpu/support.rst
    libc/src/time/CMakeLists.txt
    libc/src/time/gpu/CMakeLists.txt
    libc/src/time/linux/CMakeLists.txt
    libc/test/src/time/CMakeLists.txt

Removed: 
    libc/src/time/nanosleep.cpp


################################################################################
diff  --git a/libc/config/gpu/api.td b/libc/config/gpu/api.td
index 8d3879eddf5b5c..4435ded2aa45e4 100644
--- a/libc/config/gpu/api.td
+++ b/libc/config/gpu/api.td
@@ -1,6 +1,7 @@
 include "config/public_api.td"
 
 include "spec/stdc.td"
+include "spec/posix.td"
 include "spec/gpu_ext.td"
 
 def StringAPI : PublicAPI<"string.h"> {
@@ -38,5 +39,7 @@ def IntTypesAPI : PublicAPI<"inttypes.h"> {
 def TimeAPI : PublicAPI<"time.h"> {
   let Types = [
     "clock_t",
+    "time_t",
+    "struct timespec",
   ];
 }

diff  --git a/libc/config/gpu/entrypoints.txt b/libc/config/gpu/entrypoints.txt
index 2cc420b8e528b1..bc8f9649f49168 100644
--- a/libc/config/gpu/entrypoints.txt
+++ b/libc/config/gpu/entrypoints.txt
@@ -98,6 +98,7 @@ set(TARGET_LIBC_ENTRYPOINTS
 
     # time.h entrypoints
     libc.src.time.clock
+    libc.src.time.nanosleep
 
     # gpu/rpc.h entrypoints
     libc.src.gpu.rpc_reset

diff  --git a/libc/docs/gpu/support.rst b/libc/docs/gpu/support.rst
index 623e8462deaa6e..3818b65d12863d 100644
--- a/libc/docs/gpu/support.rst
+++ b/libc/docs/gpu/support.rst
@@ -136,4 +136,5 @@ stdio.h
 Function Name  Available  RPC Required
 =============  =========  ============
 clock          |check|
+nanosleep      |check|
 =============  =========  ============

diff  --git a/libc/src/time/CMakeLists.txt b/libc/src/time/CMakeLists.txt
index d45ef64d891259..5a0b3ab31cf0f8 100644
--- a/libc/src/time/CMakeLists.txt
+++ b/libc/src/time/CMakeLists.txt
@@ -106,19 +106,6 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
-add_entrypoint_object(
-  nanosleep
-  SRCS
-    nanosleep.cpp
-  HDRS
-    nanosleep.h
-  DEPENDS
-    libc.include.time
-    libc.include.sys_syscall
-    libc.src.__support.OSUtil.osutil
-    libc.src.errno.errno
-)
-
 add_entrypoint_object(
   time
   ALIAS
@@ -132,3 +119,10 @@ add_entrypoint_object(
   DEPENDS
     .${LIBC_TARGET_OS}.clock
 )
+
+add_entrypoint_object(
+  nanosleep
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.nanosleep
+)

diff  --git a/libc/src/time/gpu/CMakeLists.txt b/libc/src/time/gpu/CMakeLists.txt
index c55ce23e19cdb2..bb79d92399b378 100644
--- a/libc/src/time/gpu/CMakeLists.txt
+++ b/libc/src/time/gpu/CMakeLists.txt
@@ -17,3 +17,15 @@ add_entrypoint_object(
     libc.src.__support.GPU.utils
     .time_utils
 )
+
+add_entrypoint_object(
+  nanosleep
+  SRCS
+    nanosleep.cpp
+  HDRS
+    ../nanosleep.h
+  DEPENDS
+    libc.include.time
+    libc.src.__support.GPU.utils
+    .time_utils
+)

diff  --git a/libc/src/time/gpu/nanosleep.cpp b/libc/src/time/gpu/nanosleep.cpp
new file mode 100644
index 00000000000000..52cfecbab9be8d
--- /dev/null
+++ b/libc/src/time/gpu/nanosleep.cpp
@@ -0,0 +1,71 @@
+//===-- 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.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/time/nanosleep.h"
+
+#include "time_utils.h"
+
+namespace __llvm_libc {
+
+constexpr uint64_t TICKS_PER_NS = 1000000000UL;
+
+LLVM_LIBC_FUNCTION(int, nanosleep,
+                   (const struct timespec *req, struct timespec *rem)) {
+  if (!GPU_CLOCKS_PER_SEC || !req)
+    return -1;
+
+  uint64_t nsecs = req->tv_nsec + req->tv_sec * TICKS_PER_NS;
+
+  uint64_t start = gpu::fixed_frequency_clock();
+#if defined(LIBC_TARGET_ARCH_IS_NVPTX) && __CUDA_ARCH__ >= 700
+  uint64_t end = start + nsecs / (TICKS_PER_NS / GPU_CLOCKS_PER_SEC);
+  uint64_t cur = gpu::fixed_frequency_clock();
+  // The NVPTX architecture supports sleeping and guaruntees the actual time
+  // slept will be somewhere between zero and twice the requested amount. Here
+  // we will sleep again if we undershot the time.
+  while (cur < end) {
+    LIBC_INLINE_ASM("nanosleep.u32 %0;" ::"r"(nsecs));
+    cur = gpu::fixed_frequency_clock();
+    nsecs -= nsecs > cur - start ? cur - start : 0;
+  }
+#elif defined(LIBC_TARGET_ARCH_IS_AMDGPU)
+  uint64_t end = start + nsecs / (TICKS_PER_NS / GPU_CLOCKS_PER_SEC);
+  uint64_t cur = gpu::fixed_frequency_clock();
+  // The AMDGPU architecture does not provide a sleep implementation with a
+  // known delay so we simply repeatedly sleep with a large value of ~960 clock
+  // cycles and check until we've passed the time using the known frequency.
+  __builtin_amdgcn_s_sleep(2);
+  while (cur < end) {
+    __builtin_amdgcn_s_sleep(15);
+    cur = gpu::fixed_frequency_clock();
+  }
+#else
+  // Sleeping is not supported.
+  if (rem) {
+    rem->tv_sec = req->tv_sec;
+    rem->tv_nsec = req->tv_nsec;
+  }
+  return -1;
+#endif
+  uint64_t stop = gpu::fixed_frequency_clock();
+
+  // Check to make sure we slept for at least the desired duration and set the
+  // remaining time if not.
+  uint64_t elapsed = (stop - start) * (TICKS_PER_NS / GPU_CLOCKS_PER_SEC);
+  if (elapsed < nsecs) {
+    if (rem) {
+      rem->tv_sec = (nsecs - elapsed) / TICKS_PER_NS;
+      rem->tv_nsec = (nsecs - elapsed) % TICKS_PER_NS;
+    }
+    return -1;
+  }
+
+  return 0;
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/time/linux/CMakeLists.txt b/libc/src/time/linux/CMakeLists.txt
index c3f11c317f5d03..8b4976847f82af 100644
--- a/libc/src/time/linux/CMakeLists.txt
+++ b/libc/src/time/linux/CMakeLists.txt
@@ -24,3 +24,17 @@ add_entrypoint_object(
     libc.src.__support.OSUtil.osutil
     libc.src.errno.errno
 )
+
+add_entrypoint_object(
+  nanosleep
+  SRCS
+    nanosleep.cpp
+  HDRS
+    ../nanosleep.h
+  DEPENDS
+    libc.include.time
+    libc.include.sys_syscall
+    libc.src.__support.CPP.limits
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)

diff  --git a/libc/src/time/nanosleep.cpp b/libc/src/time/linux/nanosleep.cpp
similarity index 88%
rename from libc/src/time/nanosleep.cpp
rename to libc/src/time/linux/nanosleep.cpp
index 11fd0966e52902..84f57110683767 100644
--- a/libc/src/time/nanosleep.cpp
+++ b/libc/src/time/linux/nanosleep.cpp
@@ -1,4 +1,4 @@
-//===-- Implementation of nanosleep function ------------------------------===//
+//===-- Linux implementation of 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.
@@ -16,7 +16,6 @@
 
 namespace __llvm_libc {
 
-// TODO(michaelrj): Move this into time/linux with the other syscalls.
 LLVM_LIBC_FUNCTION(int, nanosleep,
                    (const struct timespec *req, struct timespec *rem)) {
 #if SYS_nanosleep

diff  --git a/libc/test/src/time/CMakeLists.txt b/libc/test/src/time/CMakeLists.txt
index 1a829188156a2e..03b1a290f2a351 100644
--- a/libc/test/src/time/CMakeLists.txt
+++ b/libc/test/src/time/CMakeLists.txt
@@ -107,17 +107,12 @@ add_libc_unittest(
     libc.src.time.mktime
 )
 
-add_libc_unittest(
+add_libc_test(
   nanosleep_test
   SUITE
     libc_time_unittests
   SRCS
     nanosleep_test.cpp
-  HDRS
-    TmHelper.h
-    TmMatcher.h
-  CXX_STANDARD
-    20
   DEPENDS
     libc.include.time
     libc.src.time.nanosleep


        


More information about the libc-commits mailing list