Skip to content

Commit bf42a78

Browse files
authored
[libc] Implement placeholder memory functions on the GPU (#101082)
Summary: These functions are needed for `libc++` to link successfully. We can't implement them well currently, so simply provide some stand-in implementations. `realloc` will currently copy garbage and potentially fault and `aligned_alloc` will work unless your alignment is more than 4K alignment. However, these should work in practice to get tests running. I will write a real allocator soon™.
1 parent 8fc3294 commit bf42a78

File tree

6 files changed

+141
-6
lines changed

6 files changed

+141
-6
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,9 @@ set(TARGET_LIBC_ENTRYPOINTS
166166
libc.src.stdlib.strtoul
167167
libc.src.stdlib.strtoull
168168

169-
# Only implemented in the test suite
169+
# TODO: Implement these correctly
170170
libc.src.stdlib.aligned_alloc
171+
libc.src.stdlib.calloc
171172
libc.src.stdlib.free
172173
libc.src.stdlib.malloc
173174
libc.src.stdlib.realloc

libc/src/stdlib/CMakeLists.txt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,23 @@ if(LIBC_TARGET_OS_IS_GPU)
443443
DEPENDS
444444
.${LIBC_TARGET_OS}.free
445445
)
446-
add_entrypoint_external(
447-
calloc
448-
)
449-
add_entrypoint_external(
446+
add_entrypoint_object(
450447
realloc
448+
ALIAS
449+
DEPENDS
450+
.${LIBC_TARGET_OS}.realloc
451451
)
452-
add_entrypoint_external(
452+
add_entrypoint_object(
453+
calloc
454+
ALIAS
455+
DEPENDS
456+
.${LIBC_TARGET_OS}.calloc
457+
)
458+
add_entrypoint_object(
453459
aligned_alloc
460+
ALIAS
461+
DEPENDS
462+
.${LIBC_TARGET_OS}.aligned_alloc
454463
)
455464
endif()
456465

libc/src/stdlib/gpu/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,39 @@ add_entrypoint_object(
2020
libc.src.__support.RPC.rpc_client
2121
)
2222

23+
add_entrypoint_object(
24+
realloc
25+
SRCS
26+
realloc.cpp
27+
HDRS
28+
../realloc.h
29+
DEPENDS
30+
libc.include.stdlib
31+
libc.src.__support.GPU.allocator
32+
)
33+
34+
add_entrypoint_object(
35+
calloc
36+
SRCS
37+
calloc.cpp
38+
HDRS
39+
../calloc.h
40+
DEPENDS
41+
libc.include.stdlib
42+
libc.src.__support.GPU.allocator
43+
)
44+
45+
add_entrypoint_object(
46+
aligned_alloc
47+
SRCS
48+
aligned_alloc.cpp
49+
HDRS
50+
../aligned_alloc.h
51+
DEPENDS
52+
libc.include.stdlib
53+
libc.src.__support.GPU.allocator
54+
)
55+
2356
add_entrypoint_object(
2457
abort
2558
SRCS

libc/src/stdlib/gpu/aligned_alloc.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- GPU Implementation of aligned_alloc -------------------------------===//
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 "src/stdlib/aligned_alloc.h"
10+
11+
#include "src/__support/GPU/allocator.h"
12+
#include "src/__support/common.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
LLVM_LIBC_FUNCTION(void *, aligned_alloc, (size_t alignment, size_t size)) {
18+
if ((alignment & -alignment) != alignment)
19+
return nullptr;
20+
21+
void *ptr = gpu::allocate(size);
22+
if ((reinterpret_cast<uintptr_t>(ptr) & (alignment - 1)) != 0) {
23+
gpu::deallocate(ptr);
24+
return nullptr;
25+
}
26+
return ptr;
27+
}
28+
29+
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdlib/gpu/calloc.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===-- GPU Implementation of calloc --------------------------------------===//
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 "src/stdlib/calloc.h"
10+
11+
#include "src/__support/GPU/allocator.h"
12+
#include "src/__support/common.h"
13+
#include "src/__support/macros/config.h"
14+
#include "src/string/memory_utils/inline_memset.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
LLVM_LIBC_FUNCTION(void *, calloc, (size_t num, size_t size)) {
19+
size_t bytes = num * size;
20+
if (bytes == 0)
21+
return nullptr;
22+
23+
void *ptr = gpu::allocate(bytes);
24+
if (!ptr)
25+
return nullptr;
26+
27+
inline_memset(ptr, 0, bytes);
28+
return ptr;
29+
}
30+
31+
} // namespace LIBC_NAMESPACE_DECL

libc/src/stdlib/gpu/realloc.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===-- GPU Implementation of realloc -------------------------------------===//
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 "src/stdlib/realloc.h"
10+
11+
#include "src/__support/GPU/allocator.h"
12+
#include "src/__support/common.h"
13+
#include "src/__support/macros/config.h"
14+
#include "src/string/memory_utils/inline_memcpy.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
LLVM_LIBC_FUNCTION(void *, realloc, (void *ptr, size_t size)) {
19+
if (ptr == nullptr)
20+
return gpu::allocate(size);
21+
22+
void *newmem = gpu::allocate(size);
23+
if (newmem == nullptr)
24+
return nullptr;
25+
26+
// This will copy garbage if it goes beyond the old allocation size.
27+
inline_memcpy(newmem, ptr, size);
28+
gpu::deallocate(ptr);
29+
return newmem;
30+
}
31+
32+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)