aboutsummaryrefslogtreecommitdiff
path: root/libc/src
diff options
context:
space:
mode:
authorJoseph Huber <huberjn@outlook.com>2024-03-10 15:49:44 -0500
committerGitHub <noreply@github.com>2024-03-10 15:49:44 -0500
commitea697dcc2ad9e6a1cc313d792b485d9218a943a1 (patch)
treef743a0a61ce4dc66d3faca3a332bd0128ac27022 /libc/src
parenta53401e9dff6168b872eeb61f62b0f60b185328a (diff)
downloadllvm-ea697dcc2ad9e6a1cc313d792b485d9218a943a1.zip
llvm-ea697dcc2ad9e6a1cc313d792b485d9218a943a1.tar.gz
llvm-ea697dcc2ad9e6a1cc313d792b485d9218a943a1.tar.bz2
[libc][NFC] Move GPU allocator implementation to common header (#84690)
Summary: This is a NFC move preceding more radical functional changes to the allocator implementation. We just move it to a common utility so it will be easier to write these in tandem.
Diffstat (limited to 'libc/src')
-rw-r--r--libc/src/__support/GPU/CMakeLists.txt12
-rw-r--r--libc/src/__support/GPU/allocator.cpp45
-rw-r--r--libc/src/__support/GPU/allocator.h23
-rw-r--r--libc/src/stdlib/gpu/CMakeLists.txt4
-rw-r--r--libc/src/stdlib/gpu/free.cpp11
-rw-r--r--libc/src/stdlib/gpu/malloc.cpp12
6 files changed, 88 insertions, 19 deletions
diff --git a/libc/src/__support/GPU/CMakeLists.txt b/libc/src/__support/GPU/CMakeLists.txt
index c181b2e..28fd9a1 100644
--- a/libc/src/__support/GPU/CMakeLists.txt
+++ b/libc/src/__support/GPU/CMakeLists.txt
@@ -12,3 +12,15 @@ add_header_library(
DEPENDS
${target_gpu_utils}
)
+
+add_object_library(
+ allocator
+ SRCS
+ allocator.cpp
+ HDRS
+ allocator.h
+ DEPENDS
+ libc.src.__support.common
+ libc.src.__support.GPU.utils
+ libc.src.__support.RPC.rpc_client
+)
diff --git a/libc/src/__support/GPU/allocator.cpp b/libc/src/__support/GPU/allocator.cpp
new file mode 100644
index 0000000..a049959
--- /dev/null
+++ b/libc/src/__support/GPU/allocator.cpp
@@ -0,0 +1,45 @@
+//===-- GPU memory allocator implementation ---------------------*- C++ -*-===//
+//
+// 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 "allocator.h"
+
+#include "src/__support/GPU/utils.h"
+#include "src/__support/RPC/rpc_client.h"
+
+namespace LIBC_NAMESPACE {
+namespace {
+
+void *rpc_allocate(uint64_t size) {
+ void *ptr = nullptr;
+ rpc::Client::Port port = rpc::client.open<RPC_MALLOC>();
+ port.send_and_recv([=](rpc::Buffer *buffer) { buffer->data[0] = size; },
+ [&](rpc::Buffer *buffer) {
+ ptr = reinterpret_cast<void *>(buffer->data[0]);
+ });
+ port.close();
+ return ptr;
+}
+
+void rpc_free(void *ptr) {
+ rpc::Client::Port port = rpc::client.open<RPC_FREE>();
+ port.send([=](rpc::Buffer *buffer) {
+ buffer->data[0] = reinterpret_cast<uintptr_t>(ptr);
+ });
+ port.close();
+}
+
+} // namespace
+
+namespace gpu {
+
+void *allocate(uint64_t size) { return rpc_allocate(size); }
+
+void deallocate(void *ptr) { rpc_free(ptr); }
+
+} // namespace gpu
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/__support/GPU/allocator.h b/libc/src/__support/GPU/allocator.h
new file mode 100644
index 0000000..99eeb68
--- /dev/null
+++ b/libc/src/__support/GPU/allocator.h
@@ -0,0 +1,23 @@
+//===-- GPU memory allocator implementation ---------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_GPU_ALLOCATOR_H
+#define LLVM_LIBC_SRC___SUPPORT_GPU_ALLOCATOR_H
+
+#include <stdint.h>
+
+namespace LIBC_NAMESPACE {
+namespace gpu {
+
+void *allocate(uint64_t size);
+void deallocate(void *ptr);
+
+} // namespace gpu
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC___SUPPORT_GPU_ALLOCATOR_H
diff --git a/libc/src/stdlib/gpu/CMakeLists.txt b/libc/src/stdlib/gpu/CMakeLists.txt
index 71ae106..f8a11ec 100644
--- a/libc/src/stdlib/gpu/CMakeLists.txt
+++ b/libc/src/stdlib/gpu/CMakeLists.txt
@@ -6,7 +6,7 @@ add_entrypoint_object(
../malloc.h
DEPENDS
libc.include.stdlib
- libc.src.__support.RPC.rpc_client
+ libc.src.__support.GPU.allocator
)
add_entrypoint_object(
@@ -28,5 +28,5 @@ add_entrypoint_object(
../abort.h
DEPENDS
libc.include.stdlib
- libc.src.__support.RPC.rpc_client
+ libc.src.__support.GPU.allocator
)
diff --git a/libc/src/stdlib/gpu/free.cpp b/libc/src/stdlib/gpu/free.cpp
index 3a41e5f..fb5703b 100644
--- a/libc/src/stdlib/gpu/free.cpp
+++ b/libc/src/stdlib/gpu/free.cpp
@@ -7,17 +7,12 @@
//===----------------------------------------------------------------------===//
#include "src/stdlib/free.h"
-#include "src/__support/RPC/rpc_client.h"
+
+#include "src/__support/GPU/allocator.h"
#include "src/__support/common.h"
namespace LIBC_NAMESPACE {
-LLVM_LIBC_FUNCTION(void, free, (void *ptr)) {
- rpc::Client::Port port = rpc::client.open<RPC_FREE>();
- port.send([=](rpc::Buffer *buffer) {
- buffer->data[0] = reinterpret_cast<uintptr_t>(ptr);
- });
- port.close();
-}
+LLVM_LIBC_FUNCTION(void, free, (void *ptr)) { gpu::deallocate(ptr); }
} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdlib/gpu/malloc.cpp b/libc/src/stdlib/gpu/malloc.cpp
index a219690..9355823 100644
--- a/libc/src/stdlib/gpu/malloc.cpp
+++ b/libc/src/stdlib/gpu/malloc.cpp
@@ -7,20 +7,14 @@
//===----------------------------------------------------------------------===//
#include "src/stdlib/malloc.h"
-#include "src/__support/RPC/rpc_client.h"
+
+#include "src/__support/GPU/allocator.h"
#include "src/__support/common.h"
namespace LIBC_NAMESPACE {
LLVM_LIBC_FUNCTION(void *, malloc, (size_t size)) {
- void *ptr = nullptr;
- rpc::Client::Port port = rpc::client.open<RPC_MALLOC>();
- port.send_and_recv([=](rpc::Buffer *buffer) { buffer->data[0] = size; },
- [&](rpc::Buffer *buffer) {
- ptr = reinterpret_cast<void *>(buffer->data[0]);
- });
- port.close();
- return ptr;
+ return gpu::allocate(size);
}
} // namespace LIBC_NAMESPACE