Skip to content

Commit ea697dc

Browse files
authored
[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.
1 parent a53401e commit ea697dc

File tree

6 files changed

+88
-19
lines changed

6 files changed

+88
-19
lines changed

libc/src/__support/GPU/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ add_header_library(
1212
DEPENDS
1313
${target_gpu_utils}
1414
)
15+
16+
add_object_library(
17+
allocator
18+
SRCS
19+
allocator.cpp
20+
HDRS
21+
allocator.h
22+
DEPENDS
23+
libc.src.__support.common
24+
libc.src.__support.GPU.utils
25+
libc.src.__support.RPC.rpc_client
26+
)

libc/src/__support/GPU/allocator.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===-- GPU memory allocator implementation ---------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "allocator.h"
10+
11+
#include "src/__support/GPU/utils.h"
12+
#include "src/__support/RPC/rpc_client.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
namespace {
16+
17+
void *rpc_allocate(uint64_t size) {
18+
void *ptr = nullptr;
19+
rpc::Client::Port port = rpc::client.open<RPC_MALLOC>();
20+
port.send_and_recv([=](rpc::Buffer *buffer) { buffer->data[0] = size; },
21+
[&](rpc::Buffer *buffer) {
22+
ptr = reinterpret_cast<void *>(buffer->data[0]);
23+
});
24+
port.close();
25+
return ptr;
26+
}
27+
28+
void rpc_free(void *ptr) {
29+
rpc::Client::Port port = rpc::client.open<RPC_FREE>();
30+
port.send([=](rpc::Buffer *buffer) {
31+
buffer->data[0] = reinterpret_cast<uintptr_t>(ptr);
32+
});
33+
port.close();
34+
}
35+
36+
} // namespace
37+
38+
namespace gpu {
39+
40+
void *allocate(uint64_t size) { return rpc_allocate(size); }
41+
42+
void deallocate(void *ptr) { rpc_free(ptr); }
43+
44+
} // namespace gpu
45+
} // namespace LIBC_NAMESPACE

libc/src/__support/GPU/allocator.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- GPU memory allocator implementation ---------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_GPU_ALLOCATOR_H
10+
#define LLVM_LIBC_SRC___SUPPORT_GPU_ALLOCATOR_H
11+
12+
#include <stdint.h>
13+
14+
namespace LIBC_NAMESPACE {
15+
namespace gpu {
16+
17+
void *allocate(uint64_t size);
18+
void deallocate(void *ptr);
19+
20+
} // namespace gpu
21+
} // namespace LIBC_NAMESPACE
22+
23+
#endif // LLVM_LIBC_SRC___SUPPORT_GPU_ALLOCATOR_H

libc/src/stdlib/gpu/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ add_entrypoint_object(
66
../malloc.h
77
DEPENDS
88
libc.include.stdlib
9-
libc.src.__support.RPC.rpc_client
9+
libc.src.__support.GPU.allocator
1010
)
1111

1212
add_entrypoint_object(
@@ -28,5 +28,5 @@ add_entrypoint_object(
2828
../abort.h
2929
DEPENDS
3030
libc.include.stdlib
31-
libc.src.__support.RPC.rpc_client
31+
libc.src.__support.GPU.allocator
3232
)

libc/src/stdlib/gpu/free.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,12 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/stdlib/free.h"
10-
#include "src/__support/RPC/rpc_client.h"
10+
11+
#include "src/__support/GPU/allocator.h"
1112
#include "src/__support/common.h"
1213

1314
namespace LIBC_NAMESPACE {
1415

15-
LLVM_LIBC_FUNCTION(void, free, (void *ptr)) {
16-
rpc::Client::Port port = rpc::client.open<RPC_FREE>();
17-
port.send([=](rpc::Buffer *buffer) {
18-
buffer->data[0] = reinterpret_cast<uintptr_t>(ptr);
19-
});
20-
port.close();
21-
}
16+
LLVM_LIBC_FUNCTION(void, free, (void *ptr)) { gpu::deallocate(ptr); }
2217

2318
} // namespace LIBC_NAMESPACE

libc/src/stdlib/gpu/malloc.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,14 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/stdlib/malloc.h"
10-
#include "src/__support/RPC/rpc_client.h"
10+
11+
#include "src/__support/GPU/allocator.h"
1112
#include "src/__support/common.h"
1213

1314
namespace LIBC_NAMESPACE {
1415

1516
LLVM_LIBC_FUNCTION(void *, malloc, (size_t size)) {
16-
void *ptr = nullptr;
17-
rpc::Client::Port port = rpc::client.open<RPC_MALLOC>();
18-
port.send_and_recv([=](rpc::Buffer *buffer) { buffer->data[0] = size; },
19-
[&](rpc::Buffer *buffer) {
20-
ptr = reinterpret_cast<void *>(buffer->data[0]);
21-
});
22-
port.close();
23-
return ptr;
17+
return gpu::allocate(size);
2418
}
2519

2620
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)