Skip to content

[libc] Implement placeholder memory functions on the GPU #101082

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

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion libc/config/gpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdlib.strtoul
libc.src.stdlib.strtoull

# Only implemented in the test suite
# TODO: Implement these correctly
libc.src.stdlib.aligned_alloc
libc.src.stdlib.calloc
libc.src.stdlib.free
libc.src.stdlib.malloc
libc.src.stdlib.realloc
Expand Down
19 changes: 14 additions & 5 deletions libc/src/stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -443,14 +443,23 @@ if(LIBC_TARGET_OS_IS_GPU)
DEPENDS
.${LIBC_TARGET_OS}.free
)
add_entrypoint_external(
calloc
)
add_entrypoint_external(
add_entrypoint_object(
realloc
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.realloc
)
add_entrypoint_external(
add_entrypoint_object(
calloc
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.calloc
)
add_entrypoint_object(
aligned_alloc
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.aligned_alloc
)
endif()

Expand Down
33 changes: 33 additions & 0 deletions libc/src/stdlib/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,39 @@ add_entrypoint_object(
libc.src.__support.RPC.rpc_client
)

add_entrypoint_object(
realloc
SRCS
realloc.cpp
HDRS
../realloc.h
DEPENDS
libc.include.stdlib
libc.src.__support.GPU.allocator
)

add_entrypoint_object(
calloc
SRCS
calloc.cpp
HDRS
../calloc.h
DEPENDS
libc.include.stdlib
libc.src.__support.GPU.allocator
)

add_entrypoint_object(
aligned_alloc
SRCS
aligned_alloc.cpp
HDRS
../aligned_alloc.h
DEPENDS
libc.include.stdlib
libc.src.__support.GPU.allocator
)

add_entrypoint_object(
abort
SRCS
Expand Down
29 changes: 29 additions & 0 deletions libc/src/stdlib/gpu/aligned_alloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===-- GPU Implementation of aligned_alloc -------------------------------===//
//
// 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/stdlib/aligned_alloc.h"

#include "src/__support/GPU/allocator.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(void *, aligned_alloc, (size_t alignment, size_t size)) {
if ((alignment & -alignment) != alignment)
return nullptr;

void *ptr = gpu::allocate(size);
if ((reinterpret_cast<uintptr_t>(ptr) & (alignment - 1)) != 0) {
gpu::deallocate(ptr);
return nullptr;
}
return ptr;
}

} // namespace LIBC_NAMESPACE_DECL
31 changes: 31 additions & 0 deletions libc/src/stdlib/gpu/calloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===-- GPU Implementation of calloc --------------------------------------===//
//
// 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/stdlib/calloc.h"

#include "src/__support/GPU/allocator.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/string/memory_utils/inline_memset.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(void *, calloc, (size_t num, size_t size)) {
size_t bytes = num * size;
if (bytes == 0)
return nullptr;

void *ptr = gpu::allocate(bytes);
if (!ptr)
return nullptr;

inline_memset(ptr, 0, bytes);
return ptr;
}

} // namespace LIBC_NAMESPACE_DECL
32 changes: 32 additions & 0 deletions libc/src/stdlib/gpu/realloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===-- GPU Implementation of realloc -------------------------------------===//
//
// 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/stdlib/realloc.h"

#include "src/__support/GPU/allocator.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/string/memory_utils/inline_memcpy.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(void *, realloc, (void *ptr, size_t size)) {
if (ptr == nullptr)
return gpu::allocate(size);

void *newmem = gpu::allocate(size);
if (newmem == nullptr)
return nullptr;

// This will copy garbage if it goes beyond the old allocation size.
inline_memcpy(newmem, ptr, size);
gpu::deallocate(ptr);
return newmem;
}

} // namespace LIBC_NAMESPACE_DECL
Loading